File: watchman-wait

package info (click to toggle)
watchman 4.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,992 kB
  • sloc: cpp: 27,459; python: 6,538; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (219 lines) | stat: -rwxr-xr-x 7,684 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
from __future__ import print_function
import pywatchman
import argparse
import os
import sys
import time


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('-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.
""")
args = parser.parse_args()


# 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(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)

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

# and start up the client + subscriptions
client = pywatchman.client()
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)