/
__main__.py
257 lines (239 loc) · 8.58 KB
/
__main__.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
import argparse
import sys
from typing import Mapping, Optional
from dunamai import check_version, Version, Style, Vcs, VERSION_SOURCE_PATTERN
common_sub_args = [
{
"triggers": ["--metadata"],
"action": "store_true",
"dest": "metadata",
"default": None,
"help": "Always include metadata. Ignored when --format is used",
},
{
"triggers": ["--no-metadata"],
"action": "store_false",
"dest": "metadata",
"default": None,
"help": "Never include metadata. Ignored when --format is used",
},
{
"triggers": ["--dirty"],
"action": "store_true",
"dest": "dirty",
"help": "Include dirty flag if applicable. Ignored when --format is used",
},
{
"triggers": ["--tagged-metadata"],
"action": "store_true",
"dest": "tagged_metadata",
"help": "Include tagged metadata if applicable. Ignored when --format is used",
},
{
"triggers": ["--pattern"],
"default": VERSION_SOURCE_PATTERN,
"help": (
"Regular expression matched against the version source."
" This must contain one capture group named `base` corresponding to"
" the release segment of the source."
" Optionally, it may contain another two groups named `stage` and `revision`"
" corresponding to a prerelease type (such as 'alpha' or 'rc') and number"
" (such as in 'alpha-2' or 'rc3')."
" It may also contain a group named `tagged_metadata` corresponding to extra"
" metadata after the main part of the version (typically after a plus sign)."
" There may also be a group named `epoch` for the PEP 440 concept."
),
},
{
"triggers": ["--format"],
"help": (
"Custom output format. Available substitutions:"
" {base}, {stage}, {revision}, {distance}, {commit}, {dirty},"
" {tagged_metadata}, {epoch}, {branch}, {branch_escaped}, {timestamp}"
),
},
{
"triggers": ["--style"],
"choices": [x.value for x in Style],
"help": (
"Preconfigured output format."
" Will default to PEP 440 if not set and no custom format given."
" If you specify both a style and a custom format, then the format"
" will be validated against the style's rules"
),
},
{
"triggers": ["--latest-tag"],
"action": "store_true",
"dest": "latest_tag",
"default": False,
"help": "Only inspect the latest tag on the latest tagged commit for a pattern match",
},
{
"triggers": ["--debug"],
"action": "store_true",
"dest": "debug",
"default": False,
"help": "Display additional information on stderr for troubleshooting",
},
{
"triggers": ["--bump"],
"action": "store_true",
"dest": "bump",
"default": False,
"help": (
"Increment the last part of the version `base` by 1,"
" unless the `stage` is set, in which case increment the `revision`"
" by 1 or set it to a default of 2 if there was no `revision`"
" Does nothing when on a commit with a version tag."
),
},
]
cli_spec = {
"description": "Generate dynamic versions",
"sub_dest": "command",
"sub": {
"from": {
"description": "Generate version from a particular VCS",
"sub_dest": "vcs",
"sub": {
Vcs.Any.value: {
"description": "Generate version from any detected VCS",
"args": [
*common_sub_args,
{
"triggers": ["--tag-dir"],
"default": "tags",
"help": "Location of tags relative to the root (Subversion)",
},
],
},
Vcs.Git.value: {
"description": "Generate version from Git",
"args": common_sub_args,
},
Vcs.Mercurial.value: {
"description": "Generate version from Mercurial",
"args": common_sub_args,
},
Vcs.Darcs.value: {
"description": "Generate version from Darcs",
"args": common_sub_args,
},
Vcs.Subversion.value: {
"description": "Generate version from Subversion",
"args": [
*common_sub_args,
{
"triggers": ["--tag-dir"],
"default": "tags",
"help": "Location of tags relative to the root",
},
],
},
Vcs.Bazaar.value: {
"description": "Generate version from Bazaar",
"args": common_sub_args,
},
Vcs.Fossil.value: {
"description": "Generate version from Fossil",
"args": common_sub_args,
},
},
},
"check": {
"description": "Check if a version is valid for a style",
"args": [
{
"triggers": [],
"dest": "version",
"help": "Version to check; may be piped in",
"nargs": "?",
},
{
"triggers": ["--style"],
"choices": [x.value for x in Style],
"default": Style.Pep440.value,
"help": "Style against which to check",
},
],
},
},
}
def build_parser(
spec: Mapping, parser: Optional[argparse.ArgumentParser] = None
) -> argparse.ArgumentParser:
if parser is None:
parser = argparse.ArgumentParser(
description=spec["description"], formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
if "args" in spec:
for arg in spec["args"]:
triggers = arg["triggers"]
parser.add_argument(*triggers, **{k: v for k, v in arg.items() if k != "triggers"})
if "sub" in spec:
subparsers = parser.add_subparsers(dest=spec["sub_dest"])
subparsers.required = True
for name, sub_spec in spec["sub"].items():
subparser = subparsers.add_parser(
name,
description=sub_spec.get("description"),
help=sub_spec.get("description"),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
build_parser(sub_spec, subparser)
return parser
def parse_args(argv=None) -> argparse.Namespace:
return build_parser(cli_spec).parse_args(argv)
def from_stdin(value: Optional[str]) -> Optional[str]:
if value is not None:
return value
if not sys.stdin.isatty():
return sys.stdin.readline().strip()
return None
def from_vcs(
vcs: Vcs,
pattern: str,
metadata: Optional[bool],
dirty: bool,
format: Optional[str],
style: Optional[Style],
latest_tag: bool,
tag_dir: str,
debug: bool,
bump: bool,
tagged_metadata: bool,
) -> None:
version = Version.from_vcs(vcs, pattern, latest_tag, tag_dir)
print(version.serialize(metadata, dirty, format, style, bump, tagged_metadata=tagged_metadata))
if debug:
print("# Matched tag: {}".format(version._matched_tag), file=sys.stderr)
print("# Newer unmatched tags: {}".format(version._newer_unmatched_tags), file=sys.stderr)
def main() -> None:
args = parse_args()
try:
if args.command == "from":
tag_dir = getattr(args, "tag_dir", "tags")
from_vcs(
Vcs(args.vcs),
args.pattern,
args.metadata,
args.dirty,
args.format,
Style(args.style) if args.style else None,
args.latest_tag,
tag_dir,
args.debug,
args.bump,
args.tagged_metadata,
)
elif args.command == "check":
version = from_stdin(args.version)
if version is None:
raise ValueError("A version must be specified")
check_version(version, Style(args.style))
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()