#!/usr/bin/env python3
import argparse
import os
import sys
import time

import pywatchman


def fieldlist(s):
    # helper for splitting a list of fields by comma
    return s.split(",")


parser = argparse.ArgumentParser(
    formatter_class=argparse.RawDescriptionHelpFormatter,
    description="""
watchman-wait waits for changes to files.  It uses the watchman service to
efficiently and recursively watch your specified list of paths.

It is suitable for waiting for changes to files from shell scripts.

It can stop after a configurable number of events are observed.  The default
is a single event.  You may also remove the limit and allow it to execute
continuously.

watchman-wait will print one event per line.  The event information includes
your specified list of fields, with each field separated by a space (or your
choice of --separator).

Events are consolidated and settled by the watchman server before they are
dispatched to watchman-wait.

Exit Status:

The following exit status codes can be used to determine what caused
watchman-wait to exit:

0  After successfully waiting for event(s)
1  In case of a runtime error of some kind
2  The -t/--timeout option was used and that amount of time passed
   before an event was received
3  Execution was interrupted (Ctrl-C)

""",
)
parser.add_argument("path", type=str, nargs="+", help="path(s) to watch")
parser.add_argument(
    "--relative",
    type=str,
    default=".",
    help="print paths relative to this dir (default=PWD)",
)
parser.add_argument(
    "--fields",
    type=fieldlist,
    default=["name"],
    help="""
Comma separated list of file information fields to return.
The default is just the name.  For a list of possible fields, see:
https://facebook.github.io/watchman/docs/cmd/query.html#available-fields
""",
)
parser.add_argument(
    "-s",
    "--separator",
    type=str,
    default=" ",
    help="String to use as field separator for event output.",
)
parser.add_argument(
    "-0",
    "--null",
    action="store_true",
    help="""
Use a NUL byte as a field separator, takes precedence over --separator.
""",
)
parser.add_argument(
    "-m",
    "--max-events",
    type=int,
    default=1,
    help="""
Set the maximum number of events that will be processed.  When the limit
is reached, watchman-wait will exit.  The default is 1.  Setting the
limit to 0 removes the limit, causing watchman-wait to execute indefinitely.
""",
)
parser.add_argument(
    "-p",
    "--pattern",
    type=str,
    nargs="+",
    help="""
Only emit paths that match this list of patterns.  Patterns are
applied by the watchman server and are matched against the root-relative
paths.

You will almost certainly want to use quotes around your pattern list
so that your shell doesn't interpret the pattern.

The pattern syntax is wildmatch style; globbing with recursive matching
via '**'.
""",
)
parser.add_argument(
    "-t",
    "--timeout",
    type=float,
    default=0,
    help="""
Exit if no events trigger within the specified timeout.  If timeout is
zero (the default) then keep running indefinitely.
""",
)
parser.add_argument(
    "--connect-timeout",
    type=float,
    default=100,
    help="""
Initial watchman client connection timeout. It should be sufficiently large to
prevent timeouts when watchman is busy (eg. performing a crawl). The default
value is 100 seconds.
""",
)
args = parser.parse_args()
if args.null:
    args.separator = "\0"


# We parse the list of paths into a set of subscriptions
subscriptions = {}

# Running total of individual file events we've seen
total_events = 0


class Subscription(object):
    root = None  # Watched root
    relpath = None  # Offset to dir of interest
    name = None  # Our name for this subscription
    path = None

    def __init__(self, path):
        if path in subscriptions:
            raise ValueError("path %s already specified" % path)
        self.name = path
        self.path = os.path.abspath(path)
        if not os.path.exists(self.path):
            print(
                """path %s (%s) does not exist.
Perhaps you should use the --pattern option?"""
                % (path, self.path),
                file=sys.stderr,
            )
            sys.exit(1)
        subscriptions[self.name] = self

    def __repr__(self):
        return "Subscription(root=%s, rel=%s, name=%s)" % (
            self.root,
            self.relpath,
            self.name,
        )

    def start(self, client):
        dir_to_watch = self.path
        if args.pattern:
            expr = ["anyof"]
            for p in args.pattern:
                expr.append(["match", p, "wholename", {"includedotfiles": True}])
        else:
            expr = ["true"]
        if not os.path.isdir(self.path):
            # Need to watch its parent
            dir_to_watch = os.path.dirname(self.path)
            expr = ["name", os.path.basename(self.path)]

        query = {"expression": expr, "fields": args.fields}
        watch = client.query("watch-project", dir_to_watch)
        if "warning" in watch:
            print("WARNING: ", watch["warning"], file=sys.stderr)

        self.root = watch["watch"]
        if "relative_path" in watch:
            query["relative_root"] = watch["relative_path"]

        # get the initial clock value so that we only get updates
        query["since"] = client.query("clock", self.root)["clock"]

        sub = client.query("subscribe", self.root, self.name, query)

    def formatField(self, fname, val):
        if fname == "name":
            # Respect the --relative path printing option
            return os.path.relpath(os.path.join(self.name, val), args.relative)
        # otherwise just make sure it's a string so that we can join it
        return str(val)

    def emit(self, client):
        global total_events
        data = client.getSubscription(self.name)
        if data is None:
            return False
        for dat in data:
            for f in dat.get("files", []):
                out = []
                if len(args.fields) == 1:
                    # When only 1 field is specified, the result is a
                    # list of just the values
                    out.append(self.formatField(args.fields[0], f))
                else:
                    # Otherwise it is a list of objects
                    for fname in args.fields:
                        out.append(self.formatField(fname, f[fname]))
                print(args.separator.join(out))
                sys.stdout.flush()
                total_events = total_events + 1
                if args.max_events > 0 and total_events >= args.max_events:
                    sys.exit(0)
        return True


# Translate paths into subscriptions
for path in args.path:
    sub = Subscription(path)

# and start up the client + subscriptions
client = pywatchman.client(timeout=args.connect_timeout)

deadline = None
if args.timeout > 0:
    deadline = time.time() + args.timeout

try:
    client.capabilityCheck(required=["term-dirname", "cmd-watch-project", "wildmatch"])
    for _, sub in subscriptions.items():
        sub.start(client)

except pywatchman.CommandError as ex:
    print("watchman:", ex.msg, file=sys.stderr)
    sys.exit(1)

while deadline is None or time.time() < deadline:
    try:
        if deadline is not None:
            client.setTimeout(deadline - time.time())
        # wait for a unilateral response
        result = client.receive()

        # in theory we can parse just the result variable here, but
        # the client object will accumulate all subscription results
        # over time, so we ask it to remove and return those values
        # for each of the subscriptions
        for _, sub in subscriptions.items():
            sub.emit(client)

    except pywatchman.SocketTimeout as ex:
        if deadline is not None and time.time() >= deadline:
            sys.exit(2)

        # Let's check to see if we're still functional
        try:
            vers = client.query("version")
        except Exception as ex:
            print("watchman:", str(ex), file=sys.stderr)
            sys.exit(1)

    except KeyboardInterrupt:
        # suppress ugly stack trace when they Ctrl-C
        sys.exit(3)
