-
Notifications
You must be signed in to change notification settings - Fork 30
/
run
executable file
·422 lines (369 loc) · 12.7 KB
/
run
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python3
import argparse
import glob
import os
import re
import shlex
import shutil
import subprocess
import sys
import json5
class Runner:
def __init__(self):
self.arg_parser = None
self.args = None
self.grammar = 'json5/json5.g'
self.package = 'json5'
self.parser_file = f'{self.package}/parser.py'
self.run_cmd = None
self.subps = None
self.uv_path = None
self.version = json5.__version__
def add_parser(self, cmd, help): # pylint: disable=redefined-builtin
method = getattr(self, 'run_' + cmd.replace('-', '_'))
subp = self.subps.add_parser(cmd, help=help)
subp.add_argument(
'-n',
'--no-execute',
action='store_true',
help="Don't do anything that causes effects.",
)
subp.add_argument(
'-q',
'--quiet',
action='store_true',
help='Suppress output unless something fails.',
)
subp.add_argument(
'-v',
'--verbose',
action='store_true',
help='Echo commands as they are run.',
)
subp.set_defaults(func=lambda _: method())
return subp
def call(self, *args, **kwargs):
cmd = shlex.join(*args)
if self.args.no_execute or self.args.verbose:
print(f'{cmd}')
if self.args.no_execute:
return None
capture_output = kwargs.get('capture_output', self.args.quiet)
if 'capture_output' in kwargs:
del kwargs['capture_output']
proc = subprocess.run(
*args, capture_output=capture_output, check=False, **kwargs
)
if proc.returncode != 0:
if self.args.quiet:
print(proc.stdout)
print(proc.stderr, file=sys.stderr)
sys.exit(proc.returncode)
return proc
def main(self, argv):
self.arg_parser = argparse.ArgumentParser(prog='run')
self.subps = self.arg_parser.add_subparsers(required=True)
self.add_parser('build', help='Build the package.')
self.add_parser('check', help='Check the source code with ruff.')
self.add_parser('checks', help='Same as `run check`.')
self.add_parser('clean', help='Remove any local files.')
subp = self.add_parser(
'coverage', help='Run tests and report code coverage.'
)
subp.add_argument(
'-b',
'--branch',
action='store_true',
help='Report branch coverage.',
)
subp.add_argument(
'--omit',
help='Omit files whose paths match one of these patterns.',
)
subp.add_argument(
'-u', '--unit', action='store_true', help='Only run unit tests.'
)
subp.add_argument('test', nargs='*', action='store')
self.add_parser(
'devenv',
help='Set up a dev venv at //.venv with all the needed packages.',
)
subp = self.add_parser(
'format', help='Format the source code with ruff.'
)
subp.add_argument(
'--check',
action='store_true',
help='Just check to see if any files would be modified.',
)
subp = self.add_parser('help', help='Get help on a subcommand.')
subp.add_argument(
nargs='?',
action='store',
dest='subcommand',
help='The command to get help for.',
)
self.add_parser('lint', help='Lint the source code with pylint.')
self.add_parser('mypy', help='Typecheck the code with mypy.')
subp = self.add_parser(
'presubmit',
help='Run all the steps that should be run prior to commiting.',
)
subp.add_argument(
'-b',
'--branch',
action='store_true',
help='Report branch coverage.',
)
subp.add_argument(
'-f',
'--failfast',
action='store_true',
help='Stop on first fail or error',
)
subp.add_argument(
'--omit',
help='Omit files whose paths match one of these patterns.',
)
subp.add_argument(
'-u', '--unit', action='store_true', help='Only run unit tests.'
)
subp.add_argument('test', nargs='*', action='store')
subp = self.add_parser('publish', help='Publish packages to PyPI.')
subp.add_argument(
'--test',
action='store_true',
help='Upload to the PyPI test instance.',
)
subp.add_argument(
'--prod',
action='store_true',
help='Upload to the real PyPI instance.',
)
subp = self.add_parser('regen', help=f'Regenerate {self.parser_file}.')
subp.add_argument(
'--check',
action='store_true',
help='Just check to see if any files would be modified.',
)
subp = self.add_parser('tests', help='Run the tests.')
subp.add_argument(
'-d',
'--durations',
metavar='N',
action='store',
help='show the N slowed test cases (N=0 for all)',
)
subp.add_argument(
'-f',
'--failfast',
action='store_true',
help='Stop on first fail or error',
)
subp.add_argument(
'-u', '--unit', action='store_true', help='Only run unit tests.'
)
subp.add_argument('test', nargs='*', action='store')
self.args = self.arg_parser.parse_args(self._shuffle_argv(argv))
self.uv_path = shutil.which('uv')
if self.uv_path is None:
print('You need to have `uv` installed to run this script.')
sys.exit(2)
if 'VIRTUAL_ENV' in os.environ:
self.run_cmd = ['python']
elif self.args.quiet:
self.run_cmd = [self.uv_path, 'run', '--quiet', 'python']
else:
self.run_cmd = [self.uv_path, 'run', 'python']
self.args.func(self.args)
def run_build(self):
self._check_version()
cmd = [self.uv_path, 'build']
if self.args.quiet:
cmd.append('--quiet')
self.call(cmd)
def run_check(self):
self.call(self.run_cmd + ['-m', 'ruff', 'check'])
def run_checks(self):
self.run_check()
def run_clean(self):
path = shutil.which('git')
if path is None:
print('You must have git installed to clean out the right files.')
sys.exit(1)
self.call([path, 'clean', '-fd'])
def run_coverage(self):
env = os.environ.copy()
if self.args.unit:
env['SKIP'] = 'integration'
self.args.omit = '*_test.py'
cmd = self.run_cmd + ['-m', 'coverage', 'run']
if self.args.branch:
cmd.append('--branch')
cmd.extend(
[
'-m',
'unittest',
'discover',
'-p',
'*_test.py',
],
)
if self.args.verbose:
cmd.append('-v')
for test in self.args.test:
cmd.extend(['-k', self._trim_test_glob(test)])
self.call(cmd, env=env)
cmd = self.run_cmd + ['-m', 'coverage', 'report', '--show-missing']
if self.args.omit:
cmd.append(f'--omit={self.args.omit}')
self.call(cmd)
def run_devenv(self):
if self.uv_path is None:
print('You need to have `uv` installed to set up a dev env.')
sys.exit(2)
cmd = [self.uv_path, 'sync', '--extra', 'dev']
if self.args.quiet:
cmd.append('--quiet')
in_venv = 'VIRTUAL_ENV' in os.environ
self.call(cmd)
if not in_venv:
print('Run `source .venv/bin/activate` to finish devenv setup.')
def run_format(self):
cmd = self.run_cmd + ['-m', 'ruff', 'format']
if self.args.check:
cmd.append('--check')
self.call(cmd)
def run_help(self):
if self.args.subcommand:
self.main([self.args.subcommand, '--help'])
self.main(['--help'])
def run_lint(self):
self.call(self.run_cmd[:-1] + ['pylint', 'run'] + self._files())
def run_mypy(self):
self.call(self.run_cmd + ['-m', 'mypy'] + self._files())
def run_presubmit(self):
self.args.check = True
self.run_regen()
self.run_format()
self.run_check()
self.run_lint()
self.run_mypy()
self.run_coverage()
def run_publish(self):
if not self.args.test and not self.args.prod:
print('You must specify either --test or --prod to upload.')
sys.exit(2)
self._check_version()
sep = os.path.sep
tgz = f'dist{sep}{self.package}-{self.version}.tar.gz'
wheel = f'dist{sep}{self.package}-{self.version}-py3-none-any.whl'
if not os.path.exists(tgz) or not os.path.exists(wheel):
print('Run `./run build` first')
return
if self.args.test:
test = ['--repository', 'testpypi']
else:
test = []
self.call(
self.run_cmd + ['-m', 'twine', 'upload'] + test + [tgz, wheel]
)
def run_regen(self):
orig_file = f'{self.parser_file}.orig'
with open(self.parser_file, encoding='utf-8') as fp:
old = fp.read()
if self.args.no_execute or self.args.verbose:
print(f'mv {self.parser_file} {orig_file}')
else:
os.rename(self.parser_file, orig_file)
self._gen_parser()
if self.args.no_execute:
if self.args.check:
print(f'diff -q {orig_file} {self.parser_file}')
print(f'mv {orig_file} {self.parser_file}')
return
with open(self.parser_file, encoding='utf-8') as fp:
new = fp.read()
if self.args.check:
os.remove(self.parser_file)
os.rename(orig_file, self.parser_file)
if old != new:
print('Need to regenerate the parser with `run regen`.')
sys.exit(1)
print(f'{self.parser_file} is up to date.')
elif old == new:
print(f'{self.parser_file} is up to date.')
else:
print(f'{self.parser_file} regenerated.')
def run_tests(self):
cmd = self.run_cmd + ['-m', 'unittest', 'discover', '-p', '*_test.py']
if self.args.quiet:
cmd.append('-q')
if self.args.verbose:
cmd.append('-v')
if self.args.failfast:
cmd.append('-f')
if self.args.durations:
cmd.extend(['--durations', self.args.durations])
for test in self.args.test:
cmd.extend(['-k', self._trim_test_glob(test)])
env = os.environ.copy()
if self.args.unit:
env['SKIP'] = 'integration'
self.call(cmd, env=env)
def set_func(self, subp, method):
subp.set_defaults(func=lambda _: method())
def _shuffle_argv(self, argv):
# Take any flags that appear before the subcommand and append
# them after the subcommand but before any flags following the
# subcommand.
leading_args = []
argc = len(argv)
i = 0
while i < argc:
if argv[i][0] != '-':
break
leading_args.append(argv[i])
i += 1
return argv[i : i + 1] + leading_args + argv[i + 1 :]
def _gen_parser(self):
tool = '../glop/glop/tool.py'
if not os.path.exists(tool):
print('Need `glop` repo to be at ../glop.')
sys.exit(1)
self.call(
[
sys.executable,
tool,
'-o',
self.parser_file,
'--no-main',
'--no-memoize',
'-c',
self.grammar,
]
)
self.call(self.run_cmd + ['-m', 'ruff', 'format', self.parser_file])
def _files(self):
return (
['run']
+ glob.glob(f'{self.package}/*.py')
+ glob.glob('tests/*.py')
)
def _trim_test_glob(self, test):
if test.startswith('^'):
test = test[1:]
else:
test = '*' + test
if test.endswith('$'):
test = test[:-1]
else:
test = test + '*'
return test
def _check_version(self):
m = re.match(r'\d+\.\d+\.\d+(\.dev\d+)?', self.version)
if not m:
print(f'Unexpected version format: "{self.version}"')
sys.exit(1)
if __name__ == '__main__':
sys.exit(Runner().main(sys.argv[1:]))