Skip to content

Commit 5ce45a2

Browse files
build-integration-branch: convert to argparse
Convert build-integration-branch to use the stdlib argparse module. Argparse is: * Part of the python standard library and available since 3.2 * Well documented as a stdlib component * Widely used * Fairly simple and direct docopt is: * Clever * Not documented as a dependency of this script (so I bet most users are relying on the fallback behavior) * Of questionable maintenance status with: - No releases since 2014 - Only four PRs merged since 2019 - Last merged PR was merged, recommending an alternate repo, and then disappeared from the commit history of the master branch, indicating a possible maintainership/status discrepancy - Only a couple of commits merged since 2018 (visible on github) * In my opinion: not particularly ergonomic esp. wrt dictionary based key access I feel pretty comfortable making this conversion as I think it will make the script easier to maintain and extend. Signed-off-by: John Mulligan <[email protected]>
1 parent 8a48206 commit 5ce45a2

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

src/script/build-integration-branch

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,9 @@ OR adding an entry like the following to `~/.netrc`:
1313
machine github.com
1414
password ghp_E7ln0tAR34LtoK3nIsw34RyTve2moM3BvK
1515
```
16-
17-
18-
Usage:
19-
build-integration-branch <label> [--no-date]
20-
build-integration-branch -h | --help
21-
22-
Options:
23-
-h --help Show this screen.
24-
--no-date Don't add `{postfix}` to the branch name.
2516
"""
2617

18+
import argparse
2719
import json
2820
import os
2921
import requests
@@ -35,31 +27,52 @@ from subprocess import call, check_output
3527
from urllib.parse import urljoin
3628

3729
TIME_FORMAT = '%Y-%m-%d-%H%M'
38-
postfix = "-" + time.strftime(TIME_FORMAT, time.localtime())
39-
40-
current_branch = check_output('git rev-parse --abbrev-ref HEAD',
41-
shell=True).strip().decode()
42-
if current_branch in 'mimic nautilus octopus pacific quincy reef squid tentacle'.split():
43-
postfix += '-' + current_branch
44-
print(f"Adding current branch name '-{current_branch}' as a postfix")
45-
46-
repo = "ceph/ceph"
47-
48-
try:
49-
from docopt import docopt
50-
arguments = docopt(__doc__.format(postfix=postfix))
51-
label = arguments['<label>']
52-
branch = label
53-
if not arguments['--no-date']:
54-
branch += postfix
55-
except ImportError:
56-
# Fallback without docopt.
57-
label = sys.argv[1]
58-
assert len(sys.argv) == 2
59-
branch = label + postfix
30+
CODENAMES = 'mimic nautilus octopus pacific quincy reef squid tentacle'
31+
REPO = "ceph/ceph"
32+
33+
34+
def get_postfix():
35+
postfix = "-" + time.strftime(TIME_FORMAT, time.localtime())
36+
current_branch = (
37+
check_output('git rev-parse --abbrev-ref HEAD', shell=True)
38+
.strip()
39+
.decode()
40+
)
41+
if current_branch in CODENAMES.split():
42+
postfix += '-' + current_branch
43+
print(f"Adding current branch name '-{current_branch}' as a postfix")
44+
return postfix
45+
46+
47+
def parse_args():
48+
parser = argparse.ArgumentParser(usage=__doc__)
49+
parser.add_argument(
50+
"--no-date",
51+
"--no-postfix",
52+
action="store_true",
53+
help="Don't add `{postfix}` to the branch name.",
54+
)
55+
parser.add_argument(
56+
"--repo",
57+
default=REPO,
58+
help="GitHub repository (in `<org>/<name>` form)",
59+
)
60+
parser.add_argument(
61+
"label",
62+
help="GitHub label to search for",
63+
)
64+
return parser.parse_args()
6065

6166

6267
def main():
68+
cli = parse_args()
69+
if cli.no_date:
70+
branch = cli.label
71+
else:
72+
branch = cli.label + get_postfix()
73+
label = cli.label
74+
repo = cli.repo
75+
6376
token = ''
6477
try:
6578
nrc = netrc.netrc()

0 commit comments

Comments
 (0)