-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmanage.py
189 lines (147 loc) · 5.46 KB
/
manage.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
"""nprintML development management commands
This module populates ``manage`` sub-commands.
"""
import copy
import pathlib
import re
from argparse import REMAINDER
import argparse_formatter
from argcmdr import local, Local, localmethod, LocalRoot
DISTRO_NAME = 'nprintml'
REPO_OWNER = 'nprint'
REPO_NAME = 'nprintML'
REPO_URI = f'https://github.com/{REPO_OWNER}/{REPO_NAME}'
REGISTRY_URI = f'ghcr.io/{REPO_OWNER}/{DISTRO_NAME}'
REPO_ROOT = pathlib.Path(__file__).absolute().parent
class Manage(LocalRoot):
"""manage the nprintML repository"""
@Manage.register
@local('remainder', metavar='...', nargs=REMAINDER,
help="additional arguments for tox (include a '-' to pass options)")
def test(self, args):
"""run tests"""
if args.remainder and args.remainder[0] == '-':
remainder = args.remainder[1:]
else:
remainder = args.remainder
return (self.local.FG, self.local['tox'][remainder])
@Manage.register
class Version(Local):
"""bump version
optionally: build & release
"""
bump_default_message = "Bump version: {current_version} → {new_version}"
formatter_class = argparse_formatter.ParagraphFormatter
def __init__(self, parser):
parser.add_argument(
'part',
choices=('major', 'minor', 'patch'),
help="part of the version to be bumped",
)
parser.add_argument(
'-m', '--message',
help=f"Tag message (in addition to default: "
f"'{self.bump_default_message}')",
)
parser.add_argument(
'--build',
action='store_true',
help='build the new version',
)
parser.add_argument(
'--release',
action='store_true',
help='release the new build',
)
def prepare(self, args, parser):
if args.message:
tag_message = f"{self.bump_default_message}\n\n{args.message}"
else:
tag_message = self.bump_default_message
(_code,
stdout,
_err) = yield self.local['bumpversion'][
'--tag-message', tag_message,
'--list',
args.part,
]
if args.build:
yield self.root['build'].prepare()
if args.release:
rel_args = copy.copy(args)
if stdout is None:
rel_args.versions = ('DRY-RUN',)
else:
(version_match,) = re.finditer(
r'^new_version=([\d.]+)$',
stdout,
re.M,
)
rel_args.versions = version_match.groups()
yield self.root['release'].prepare(rel_args)
elif args.release:
parser.error('will not release package without build')
@Manage.register
class Build(Local):
"""build package"""
def prepare(self):
return self.local.FG, self.local['python'][
'setup.py',
'sdist',
'bdist_wheel',
]
@Manage.register
class Release(Local):
"""upload package(s) to pypi"""
# TODO: add support for upload to test.pypi.org
# (See also: https://github.com/bast/pypi-howto)
#
# NOTE: also, could set up a Github workflow that automatically builds for
# us, (triggered by say a tag or *maybe* even a push); perhaps stores that
# artifact in Github Packages; and even uploads it to PyPI, or at least to
# test.pypi.org.
# (This might be convenient. It also might alleviate set-up work -- and any
# concerns -- over credentials sharing.)
# (See also: https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/)
def __init__(self, parser):
parser.add_argument(
'versions',
metavar='version',
nargs='*',
)
def prepare(self, args):
if args.versions:
target = [f'dist/{DISTRO_NAME}-{version}*' for version in args.versions]
else:
target = [f'dist/{DISTRO_NAME}-*']
return self.local.FG, self.local['twine']['upload'][target]
@Manage.register
class Image(Local):
"""manage docker image"""
@localmethod('version', help="version of nprintML to build (e.g. 1.0.4)")
@localmethod('--no-latest', action='store_false', dest='latest',
help='do NOT tag image as "latest"')
@localmethod('--push', action='store_true', help="push built image to registry")
def build(self, args):
"""build image"""
cmd = self.local['docker'][
'build',
'--label', f'org.opencontainers.image.source={REPO_URI}',
'--build-arg', f'NML_VERSION={args.version}',
'--tag', f'{REGISTRY_URI}:{args.version}',
]
if args.latest:
cmd = cmd['--tag', f'{REGISTRY_URI}:latest']
yield self.local.FG, cmd[REPO_ROOT / 'image']
if args.push:
push_args = copy.copy(args)
push_args.version = [args.version]
if args.latest:
push_args.version.append('latest')
yield from self['push'].prepare(push_args)
@localmethod('version', nargs='*', help="subset of version(s) to push")
def push(self, args):
"""push image"""
targets = [f'{REGISTRY_URI}:{version}' for version in args.version] or [REGISTRY_URI]
for target in targets:
yield self.local.FG, self.local['docker']['push', target]