-
Notifications
You must be signed in to change notification settings - Fork 64
/
cli.py
635 lines (548 loc) · 16 KB
/
cli.py
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import os.path as osp
from glob import glob
from pathlib import Path
import click
from jupyter_releaser import changelog
from jupyter_releaser import lib
from jupyter_releaser import npm
from jupyter_releaser import python
from jupyter_releaser import util
class ReleaseHelperGroup(click.Group):
"""Click group tailored to jupyter-releaser"""
_needs_checkout_dir = dict()
def invoke(self, ctx):
"""Handle jupyter-releaser config while invoking a command"""
# Get the command name and make sure it is valid
cmd_name = ctx.protected_args[0]
if not cmd_name in self.commands:
super().invoke(ctx)
if cmd_name == "list-envvars":
envvars = dict()
for cmd_name in self.commands:
for param in self.commands[cmd_name].params:
if isinstance(param, click.Option):
if param.envvar:
envvars[param.name] = param.envvar
for key in sorted(envvars):
util.log(f"{key.replace('_', '-')}: {envvars[key]}")
return
orig_dir = os.getcwd()
if cmd_name.replace("-", "_") in self._needs_checkout_dir:
if not osp.exists(util.CHECKOUT_NAME):
raise ValueError("Please run prep-git first")
os.chdir(util.CHECKOUT_NAME)
# Read in the config
config = util.read_config()
hooks = config.get("hooks", {})
options = config.get("options", {})
skip = config.get("skip", [])
if "--force" in ctx.args:
skip = []
ctx.args.remove("--force")
# Print a separation header
util.log(f'\n\n{"-" * 50}')
util.log(cmd_name)
util.log(f'{"-" * 50}\n\n')
if cmd_name in skip or cmd_name.replace("-", "_") in skip:
util.log("*** Skipping based on skip config")
return
# Handle all of the parameters
for param in self.commands[cmd_name].get_params(ctx):
# Defer to env var overrides
if param.envvar and os.environ.get(param.envvar):
continue
name = param.name
if name in options or name.replace("_", "-") in options:
arg = f"--{name.replace('_', '-')}"
# Defer to cli overrides
if arg not in ctx.args:
val = options.get(name, options.get(name.replace("_", "-")))
if isinstance(val, list):
for v in val:
ctx.args.append(arg)
ctx.args.append(v)
else:
ctx.args.append(arg)
ctx.args.append(val)
# Handle before hooks
before = f"before-{cmd_name}"
if before in hooks:
before_hooks = hooks[before]
if isinstance(before_hooks, str):
before_hooks = [before_hooks]
if before_hooks:
util.log(f"\nRunning hooks for {before}")
for hook in before_hooks:
util.run(hook)
# Run the actual command
super().invoke(ctx)
# Handle after hooks
# Re-read config if we just did a git checkout
if cmd_name in ["prep-git", "extract-release"]:
os.chdir(util.CHECKOUT_NAME)
config = util.read_config()
hooks = config.get("hooks", {})
after = f"after-{cmd_name}"
if after in hooks:
after_hooks = hooks[after]
if isinstance(after_hooks, str):
after_hooks = [after_hooks]
if after_hooks:
util.log(f"\nRunning hooks for {after}")
for hook in after_hooks:
util.run(hook)
os.chdir(orig_dir)
def list_commands(self, ctx):
"""List commands in insertion order"""
return self.commands.keys()
@click.group(cls=ReleaseHelperGroup)
@click.option(
"--force", default=False, help="Force a command to run even when skipped by config"
)
def main(force):
"""Jupyter Releaser scripts"""
pass
# Extracted common options
version_spec_options = [
click.option(
"--version-spec",
envvar="RH_VERSION_SPEC",
default="",
help="The new version specifier",
)
]
version_cmd_options = [
click.option(
"--version-cmd", envvar="RH_VERSION_COMMAND", help="The version command"
)
]
branch_options = [
click.option("--ref", envvar="RH_REF", help="The source reference"),
click.option("--branch", envvar="RH_BRANCH", help="The target branch"),
click.option("--repo", envvar="RH_REPOSITORY", help="The git repo"),
]
auth_options = [
click.option("--auth", envvar="GITHUB_ACCESS_TOKEN", help="The GitHub auth token"),
]
username_options = [
click.option("--username", envvar="GITHUB_ACTOR", help="The git username")
]
dist_dir_options = [
click.option(
"--dist-dir",
envvar="RH_DIST_DIR",
default="dist",
help="The folder to use for dist files",
)
]
dry_run_options = [
click.option(
"--dry-run", is_flag=True, envvar="RH_DRY_RUN", help="Run as a dry run"
)
]
git_url_options = [
click.option("--git-url", help="A custom url for the git repository")
]
changelog_path_options = [
click.option(
"--changelog-path",
envvar="RH_CHANGELOG",
default="CHANGELOG.md",
help="The path to changelog file",
),
]
since_options = [
click.option(
"--since",
envvar="RH_SINCE",
default=None,
help="Use PRs with activity since this date or git reference",
),
click.option(
"--since-last-stable",
is_flag=True,
envvar="RH_SINCE_LAST_STABLE",
help="Use PRs with activity since the last stable git tag",
),
]
changelog_options = (
branch_options
+ auth_options
+ changelog_path_options
+ since_options
+ [
click.option(
"--resolve-backports",
envvar="RH_RESOLVE_BACKPORTS",
default=True,
help="Resolve backport PRs to their originals",
),
]
)
npm_install_options = [
click.option(
"--npm-install-options",
envvar="RH_NPM_INSTALL_OPTIONS",
default="",
help="Options to pass when calling npm install",
)
]
def add_options(options):
"""Add extracted common options to a click command"""
# https://stackoverflow.com/a/40195800
def _add_options(func):
for option in reversed(options):
func = option(func)
return func
return _add_options
def use_checkout_dir():
"""Use the checkout dir created by prep-git"""
def inner(func):
ReleaseHelperGroup._needs_checkout_dir[func.__name__] = True
return func
return inner
@main.command()
def list_envvars():
"""List the environment variables"""
# This is implemented in ReleaseHelperGroup.invoke
pass
@main.command()
@add_options(branch_options)
@add_options(auth_options)
@add_options(username_options)
@add_options(git_url_options)
def prep_git(ref, branch, repo, auth, username, git_url):
"""Prep git and env variables and bump version"""
lib.prep_git(ref, branch, repo, auth, username, git_url)
@main.command()
@add_options(version_spec_options)
@add_options(version_cmd_options)
@use_checkout_dir()
def bump_version(version_spec, version_cmd):
"""Prep git and env variables and bump version"""
lib.bump_version(version_spec, version_cmd)
@main.command()
@add_options(changelog_options)
@use_checkout_dir()
def build_changelog(
ref, branch, repo, auth, changelog_path, since, since_last_stable, resolve_backports
):
"""Build changelog entry"""
changelog.build_entry(
ref,
branch,
repo,
auth,
changelog_path,
since,
since_last_stable,
resolve_backports,
)
@main.command()
@add_options(version_spec_options)
@add_options(branch_options)
@add_options(since_options)
@add_options(auth_options)
@add_options(changelog_path_options)
@add_options(dry_run_options)
@use_checkout_dir()
def draft_changelog(
version_spec,
ref,
branch,
repo,
since,
since_last_stable,
auth,
changelog_path,
dry_run,
):
"""Create a changelog entry PR"""
lib.draft_changelog(
version_spec,
branch,
repo,
since,
since_last_stable,
auth,
changelog_path,
dry_run,
)
@main.command()
@add_options(changelog_options)
@click.option(
"--output", envvar="RH_CHANGELOG_OUTPUT", help="The output file for changelog entry"
)
@use_checkout_dir()
def check_changelog(
ref,
branch,
repo,
auth,
changelog_path,
since,
since_last_stable,
resolve_backports,
output,
):
"""Check changelog entry"""
changelog.check_entry(
ref,
branch,
repo,
auth,
changelog_path,
since,
since_last_stable,
resolve_backports,
output,
)
@main.command()
@add_options(dist_dir_options)
@use_checkout_dir()
def build_python(dist_dir):
"""Build Python dist files"""
if not util.PYPROJECT.exists() and not util.SETUP_PY.exists():
util.log("Skipping build-python since there are no python package files")
return
python.build_dist(dist_dir)
@main.command()
@add_options(dist_dir_options)
@use_checkout_dir()
def check_python(dist_dir):
"""Check Python dist files"""
for dist_file in glob(f"{dist_dir}/*"):
if Path(dist_file).suffix not in [".gz", ".whl"]:
util.log(f"Skipping non-python dist file {dist_file}")
continue
python.check_dist(dist_file)
@main.command()
@add_options(dist_dir_options)
@click.argument("package", default=".")
@use_checkout_dir()
def build_npm(package, dist_dir):
"""Build npm package"""
if not osp.exists("./package.json"):
util.log("Skipping build-npm since there is no package.json file")
return
npm.build_dist(package, dist_dir)
@main.command()
@add_options(dist_dir_options)
@add_options(npm_install_options)
@use_checkout_dir()
def check_npm(dist_dir, npm_install_options):
"""Check npm package"""
if not osp.exists("./package.json"):
util.log("Skipping check-npm since there is no package.json file")
return
npm.check_dist(dist_dir, npm_install_options)
@main.command()
@use_checkout_dir()
def check_manifest():
"""Check the project manifest"""
if util.PYPROJECT.exists() or util.SETUP_PY.exists():
util.run("check-manifest -v")
else:
util.log("Skipping check-manifest since there are no python package files")
@main.command()
@click.option(
"--ignore-glob",
default=[],
multiple=True,
help="Ignore test file paths based on glob pattern",
)
@click.option(
"--ignore-links",
multiple=True,
help="Ignore links based on regex pattern(s)",
)
@click.option(
"--cache-file",
envvar="RH_CACHE_FILE",
default="~/.cache/pytest-link-check",
help="The cache file to use",
)
@click.option(
"--links-expire",
default=604800,
envvar="RH_LINKS_EXPIRE",
help="Duration in seconds for links to be cached (default one week)",
)
@use_checkout_dir()
def check_links(ignore_glob, ignore_links, cache_file, links_expire):
"""Check URLs for HTML-containing files."""
lib.check_links(ignore_glob, ignore_links, cache_file, links_expire)
@main.command()
@add_options(dist_dir_options)
@click.option(
"--release-message",
envvar="RH_RELEASE_MESSAGE",
default="Publish {version}",
help="The message to use for the release commit",
)
@click.option(
"--tag-format",
envvar="RH_TAG_FORMAT",
default="v{version}",
help="The format to use for the release tag",
)
@click.option(
"--tag-message",
envvar="RH_TAG_MESSAGE",
default="Release {tag_name}",
help="The message to use for the release tag",
)
@click.option(
"--no-git-tag-workspace",
is_flag=True,
help="Whether to skip tagging npm workspace packages",
)
@use_checkout_dir()
def tag_release(
dist_dir, release_message, tag_format, tag_message, no_git_tag_workspace
):
"""Create release commit and tag"""
lib.tag_release(
dist_dir, release_message, tag_format, tag_message, no_git_tag_workspace
)
@main.command()
@add_options(branch_options)
@add_options(auth_options)
@add_options(changelog_path_options)
@add_options(version_cmd_options)
@add_options(dist_dir_options)
@add_options(dry_run_options)
@click.option(
"--post-version-spec",
envvar="RH_POST_VERSION_SPEC",
help="The post release version (usually dev)",
)
@click.option(
"--post-version-message",
default="Bumped version to {post_version}",
envvar="RH_POST_VERSION_MESSAGE",
help="The post release message",
)
@click.argument("assets", nargs=-1)
@use_checkout_dir()
def draft_release(
ref,
branch,
repo,
auth,
changelog_path,
version_cmd,
dist_dir,
dry_run,
post_version_spec,
post_version_message,
assets,
):
"""Publish Draft GitHub release"""
lib.draft_release(
ref,
branch,
repo,
auth,
changelog_path,
version_cmd,
dist_dir,
dry_run,
post_version_spec,
post_version_message,
assets,
)
@main.command()
@add_options(auth_options)
@click.argument("release-url", nargs=1)
@use_checkout_dir()
def delete_release(auth, release_url):
"""Delete a draft GitHub release by url to the release page"""
lib.delete_release(auth, release_url)
@main.command()
@add_options(auth_options)
@add_options(dist_dir_options)
@add_options(dry_run_options)
@add_options(npm_install_options)
@click.argument("release-url", nargs=1)
def extract_release(auth, dist_dir, dry_run, release_url, npm_install_options):
"""Download and verify assets from a draft GitHub release"""
lib.extract_release(auth, dist_dir, dry_run, release_url, npm_install_options)
@main.command()
@add_options(dist_dir_options)
@click.option("--npm-token", help="A token for the npm release", envvar="NPM_TOKEN")
@click.option(
"--npm-cmd",
help="The command to run for npm release",
envvar="RH_NPM_COMMAND",
default="npm publish",
)
@click.option(
"--twine-cmd",
help="The twine to run for Python release",
envvar="TWINE_COMMAND",
default="twine upload",
)
@click.option(
"--npm-registry",
help="The npm registry to target for publishing",
envvar="NPM_REGISTRY",
default="https://registry.npmjs.org/",
)
@click.option(
"--twine-registry",
help="The pypi register to target for publishing",
envvar="TWINE_REGISTRY",
default="https://pypi.org/simple/",
)
@add_options(dry_run_options)
@click.argument("release-url", nargs=1, required=False)
@use_checkout_dir()
def publish_assets(
dist_dir,
npm_token,
npm_cmd,
twine_cmd,
npm_registry,
twine_registry,
dry_run,
release_url,
):
"""Publish release asset(s)"""
lib.publish_assets(
dist_dir,
npm_token,
npm_cmd,
twine_cmd,
npm_registry,
twine_registry,
dry_run,
release_url,
)
@main.command()
@add_options(auth_options)
@click.argument("release-url", nargs=1)
@use_checkout_dir()
def publish_release(auth, release_url):
"""Publish GitHub release"""
lib.publish_release(auth, release_url)
@main.command()
@add_options(auth_options)
@add_options(branch_options)
@add_options(username_options)
@add_options(changelog_path_options)
@add_options(dry_run_options)
@click.argument("release-url")
@use_checkout_dir()
def forwardport_changelog(
auth, ref, branch, repo, username, changelog_path, dry_run, release_url
):
"""Forwardport Changelog Entries to the Default Branch"""
lib.forwardport_changelog(
auth, ref, branch, repo, username, changelog_path, dry_run, release_url
)
if __name__ == "__main__": # pragma: no cover
main()