| 1 |
import argparse |
| 2 |
import sys |
| 3 |
from pathlib import Path |
| 4 |
from typing import Mapping, Optional |
| 5 |
|
| 6 |
from dunamai import check_version, Version, Pattern, Style, Vcs, VERSION_SOURCE_PATTERN |
| 7 |
|
| 8 |
|
| 9 |
common_sub_args = [ |
| 10 |
{ |
| 11 |
"triggers": ["--metadata"], |
| 12 |
"action": "store_true", |
| 13 |
"dest": "metadata", |
| 14 |
"default": None, |
| 15 |
"help": "Always include metadata. Ignored when --format is used", |
| 16 |
}, |
| 17 |
{ |
| 18 |
"triggers": ["--no-metadata"], |
| 19 |
"action": "store_false", |
| 20 |
"dest": "metadata", |
| 21 |
"default": None, |
| 22 |
"help": "Never include metadata. Ignored when --format is used", |
| 23 |
}, |
| 24 |
{ |
| 25 |
"triggers": ["--dirty"], |
| 26 |
"action": "store_true", |
| 27 |
"dest": "dirty", |
| 28 |
"help": "Include dirty flag if applicable. Ignored when --format is used", |
| 29 |
}, |
| 30 |
{ |
| 31 |
"vcs": [Vcs.Git], |
| 32 |
"triggers": ["--ignore-untracked"], |
| 33 |
"action": "store_true", |
| 34 |
"dest": "ignore_untracked", |
| 35 |
"help": "Ignore untracked files when determining whether the repository is dirty", |
| 36 |
}, |
| 37 |
{ |
| 38 |
"triggers": ["--tagged-metadata"], |
| 39 |
"action": "store_true", |
| 40 |
"dest": "tagged_metadata", |
| 41 |
"help": "Include tagged metadata if applicable. Ignored when --format is used", |
| 42 |
}, |
| 43 |
{ |
| 44 |
"triggers": ["--pattern"], |
| 45 |
"default": VERSION_SOURCE_PATTERN, |
| 46 |
"help": ( |
| 47 |
"Regular expression matched against the version source." |
| 48 |
" This must contain one capture group named `base` corresponding to" |
| 49 |
" the release segment of the source." |
| 50 |
" Optionally, it may contain another two groups named `stage` and `revision`" |
| 51 |
" corresponding to a prerelease type (such as 'alpha' or 'rc') and number" |
| 52 |
" (such as in 'alpha-2' or 'rc3')." |
| 53 |
" It may also contain a group named `tagged_metadata` corresponding to extra" |
| 54 |
" metadata after the main part of the version (typically after a plus sign)." |
| 55 |
" There may also be a group named `epoch` for the PEP 440 concept." |
| 56 |
" If the `base` group is not present, then instead this will be interpreted" |
| 57 |
" as a named preset, which may be one of the following: {}" |
| 58 |
).format(", ".join(["`{}`".format(x.value) for x in Pattern])), |
| 59 |
}, |
| 60 |
{ |
| 61 |
"triggers": ["--pattern-prefix"], |
| 62 |
"help": "Insert this after the pattern's start anchor (`^`).", |
| 63 |
}, |
| 64 |
{ |
| 65 |
"triggers": ["--format"], |
| 66 |
"help": ( |
| 67 |
"Custom output format. Available substitutions:" |
| 68 |
" {base}, {stage}, {revision}, {distance}, {commit}, {dirty}," |
| 69 |
" {tagged_metadata}, {epoch}, {branch}, {branch_escaped}, {timestamp}," |
| 70 |
" {major}, {minor}, {patch}" |
| 71 |
), |
| 72 |
}, |
| 73 |
{ |
| 74 |
"triggers": ["--style"], |
| 75 |
"choices": [x.value for x in Style], |
| 76 |
"help": ( |
| 77 |
"Preconfigured output format." |
| 78 |
" Will default to PEP 440 if not set and no custom format given." |
| 79 |
" If you specify both a style and a custom format, then the format" |
| 80 |
" will be validated against the style's rules" |
| 81 |
), |
| 82 |
}, |
| 83 |
{ |
| 84 |
"triggers": ["--latest-tag"], |
| 85 |
"action": "store_true", |
| 86 |
"dest": "latest_tag", |
| 87 |
"default": False, |
| 88 |
"help": "Only inspect the latest tag on the latest tagged commit for a pattern match", |
| 89 |
}, |
| 90 |
{ |
| 91 |
"triggers": ["--highest-tag"], |
| 92 |
"action": "store_true", |
| 93 |
"dest": "highest_tag", |
| 94 |
"default": False, |
| 95 |
"help": "Only inspect the numerically highest tag across all tagged commits for a pattern match", |
| 96 |
}, |
| 97 |
{ |
| 98 |
"triggers": ["--strict"], |
| 99 |
"action": "store_true", |
| 100 |
"dest": "strict", |
| 101 |
"default": False, |
| 102 |
"help": ("Elevate warnings to errors. When there are no tags, fail instead of falling back to 0.0.0"), |
| 103 |
}, |
| 104 |
{ |
| 105 |
"triggers": ["--path"], |
| 106 |
"help": "Directory to inspect, if not the current working directory", |
| 107 |
}, |
| 108 |
{ |
| 109 |
"triggers": ["--debug"], |
| 110 |
"action": "store_true", |
| 111 |
"dest": "debug", |
| 112 |
"default": False, |
| 113 |
"help": "Display additional information on stderr for troubleshooting", |
| 114 |
}, |
| 115 |
{ |
| 116 |
"triggers": ["--bump"], |
| 117 |
"action": "store_true", |
| 118 |
"dest": "bump", |
| 119 |
"default": False, |
| 120 |
"help": ( |
| 121 |
"Increment the last part of the version `base` by 1," |
| 122 |
" unless the `stage` is set, in which case increment the `revision`" |
| 123 |
" by 1 or set it to a default of 2 if there was no `revision`" |
| 124 |
" Does nothing when on a commit with a version tag." |
| 125 |
), |
| 126 |
}, |
| 127 |
{ |
| 128 |
"vcs": [Vcs.Git, Vcs.Mercurial], |
| 129 |
"triggers": ["--full-commit"], |
| 130 |
"action": "store_true", |
| 131 |
"dest": "full_commit", |
| 132 |
"default": False, |
| 133 |
"help": "Get the full commit hash instead of the short form", |
| 134 |
}, |
| 135 |
{ |
| 136 |
"triggers": ["--commit-length"], |
| 137 |
"dest": "commit_length", |
| 138 |
"type": int, |
| 139 |
"help": "Use this many characters from the start of the full commit hash", |
| 140 |
}, |
| 141 |
{ |
| 142 |
"triggers": ["--commit-prefix"], |
| 143 |
"dest": "commit_prefix", |
| 144 |
"type": str, |
| 145 |
"help": "Add this prefix when serializing commit IDs", |
| 146 |
}, |
| 147 |
{ |
| 148 |
"triggers": ["--escape-with"], |
| 149 |
"dest": "escape_with", |
| 150 |
"type": str, |
| 151 |
"help": "When escaping, replace with this substitution. The default is simply to remove invalid characters.", |
| 152 |
}, |
| 153 |
{ |
| 154 |
"vcs": [Vcs.Git], |
| 155 |
"triggers": ["--tag-branch"], |
| 156 |
"help": "Branch on which to find tags, if different than the current branch", |
| 157 |
}, |
| 158 |
{ |
| 159 |
"vcs": [Vcs.Subversion], |
| 160 |
"triggers": ["--tag-dir"], |
| 161 |
"default": "tags", |
| 162 |
"help": "Location of tags relative to the root", |
| 163 |
}, |
| 164 |
] |
| 165 |
cli_spec = { |
| 166 |
"description": "Generate dynamic versions", |
| 167 |
"sub_dest": "command", |
| 168 |
"sub": { |
| 169 |
"from": { |
| 170 |
"description": "Generate version from a particular VCS", |
| 171 |
"sub_dest": "vcs", |
| 172 |
"sub": { |
| 173 |
Vcs.Any.value: { |
| 174 |
"description": "Generate version from any detected VCS", |
| 175 |
"args": common_sub_args, |
| 176 |
}, |
| 177 |
Vcs.Git.value: { |
| 178 |
"description": "Generate version from Git", |
| 179 |
"args": common_sub_args, |
| 180 |
}, |
| 181 |
Vcs.Mercurial.value: { |
| 182 |
"description": "Generate version from Mercurial", |
| 183 |
"args": common_sub_args, |
| 184 |
}, |
| 185 |
Vcs.Darcs.value: { |
| 186 |
"description": "Generate version from Darcs", |
| 187 |
"args": common_sub_args, |
| 188 |
}, |
| 189 |
Vcs.Subversion.value: { |
| 190 |
"description": "Generate version from Subversion", |
| 191 |
"args": common_sub_args, |
| 192 |
}, |
| 193 |
Vcs.Bazaar.value: { |
| 194 |
"description": "Generate version from Bazaar", |
| 195 |
"args": common_sub_args, |
| 196 |
}, |
| 197 |
Vcs.Fossil.value: { |
| 198 |
"description": "Generate version from Fossil", |
| 199 |
"args": common_sub_args, |
| 200 |
}, |
| 201 |
Vcs.Pijul.value: { |
| 202 |
"description": "Generate version from Pijul", |
| 203 |
"args": common_sub_args, |
| 204 |
}, |
| 205 |
}, |
| 206 |
}, |
| 207 |
"check": { |
| 208 |
"description": "Check if a version is valid for a style", |
| 209 |
"args": [ |
| 210 |
{ |
| 211 |
"triggers": [], |
| 212 |
"dest": "version", |
| 213 |
"help": "Version to check; may be piped in", |
| 214 |
"nargs": "?", |
| 215 |
}, |
| 216 |
{ |
| 217 |
"triggers": ["--style"], |
| 218 |
"choices": [x.value for x in Style], |
| 219 |
"default": Style.Pep440.value, |
| 220 |
"help": "Style against which to check", |
| 221 |
}, |
| 222 |
], |
| 223 |
}, |
| 224 |
}, |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
def build_parser( |
| 229 |
spec: Mapping, parser: Optional[argparse.ArgumentParser] = None, vcs: Optional[Vcs] = None |
| 230 |
) -> argparse.ArgumentParser: |
| 231 |
if parser is None: |
| 232 |
parser = argparse.ArgumentParser( |
| 233 |
description=spec["description"], formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 234 |
) |
| 235 |
if "args" in spec: |
| 236 |
for arg in spec["args"]: |
| 237 |
help = arg["help"] |
| 238 |
if "vcs" in arg: |
| 239 |
if vcs not in [*arg["vcs"], Vcs.Any]: |
| 240 |
continue |
| 241 |
help += " (only: {})".format(", ".join([x.name for x in arg["vcs"]])) |
| 242 |
triggers = arg["triggers"] |
| 243 |
parser.add_argument( |
| 244 |
*triggers, |
| 245 |
help=help, |
| 246 |
**{k: v for k, v in arg.items() if k not in ["triggers", "help", "vcs"]}, |
| 247 |
) |
| 248 |
if "sub" in spec: |
| 249 |
subparsers = parser.add_subparsers(dest=spec["sub_dest"]) |
| 250 |
subparsers.required = True |
| 251 |
for name, sub_spec in spec["sub"].items(): |
| 252 |
subparser = subparsers.add_parser( |
| 253 |
name, |
| 254 |
description=sub_spec.get("description"), |
| 255 |
help=sub_spec.get("description"), |
| 256 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 257 |
) |
| 258 |
build_parser(sub_spec, subparser, Vcs(name) if spec["sub_dest"] == "vcs" else None) |
| 259 |
|
| 260 |
return parser |
| 261 |
|
| 262 |
|
| 263 |
def get_parser() -> argparse.ArgumentParser: |
| 264 |
return build_parser(cli_spec) |
| 265 |
|
| 266 |
|
| 267 |
def parse_args(argv=None) -> argparse.Namespace: |
| 268 |
return get_parser().parse_args(argv) |
| 269 |
|
| 270 |
|
| 271 |
def from_stdin(value: Optional[str]) -> Optional[str]: |
| 272 |
if value is not None: |
| 273 |
return value |
| 274 |
|
| 275 |
if not sys.stdin.isatty(): |
| 276 |
return sys.stdin.readline().strip() |
| 277 |
|
| 278 |
return None |
| 279 |
|
| 280 |
|
| 281 |
def from_vcs( |
| 282 |
vcs: Vcs, |
| 283 |
pattern: str, |
| 284 |
metadata: Optional[bool], |
| 285 |
dirty: bool, |
| 286 |
format: Optional[str], |
| 287 |
style: Optional[Style], |
| 288 |
latest_tag: bool, |
| 289 |
tag_dir: str, |
| 290 |
debug: bool, |
| 291 |
bump: bool, |
| 292 |
tagged_metadata: bool, |
| 293 |
tag_branch: Optional[str], |
| 294 |
full_commit: bool, |
| 295 |
strict: bool, |
| 296 |
path: Optional[Path], |
| 297 |
pattern_prefix: Optional[str], |
| 298 |
ignore_untracked: bool, |
| 299 |
commit_length: Optional[int], |
| 300 |
commit_prefix: Optional[str], |
| 301 |
escape_with: Optional[str], |
| 302 |
highest_tag: bool, |
| 303 |
) -> None: |
| 304 |
version = Version.from_vcs( |
| 305 |
vcs, |
| 306 |
pattern, |
| 307 |
latest_tag, |
| 308 |
tag_dir, |
| 309 |
tag_branch, |
| 310 |
full_commit, |
| 311 |
strict, |
| 312 |
path, |
| 313 |
pattern_prefix, |
| 314 |
ignore_untracked, |
| 315 |
commit_length, |
| 316 |
highest_tag, |
| 317 |
) |
| 318 |
|
| 319 |
for concern in version.concerns: |
| 320 |
print("Warning: {}".format(concern.message()), file=sys.stderr) |
| 321 |
|
| 322 |
print(version.serialize(metadata, dirty, format, style, bump, tagged_metadata, commit_prefix, escape_with)) |
| 323 |
|
| 324 |
if debug: |
| 325 |
print("# Matched tag: {}".format(version._matched_tag), file=sys.stderr) |
| 326 |
print("# Newer unmatched tags: {}".format(version._newer_unmatched_tags), file=sys.stderr) |
| 327 |
|
| 328 |
|
| 329 |
def main() -> None: |
| 330 |
args = parse_args() |
| 331 |
try: |
| 332 |
if args.command == "from": |
| 333 |
tag_dir = getattr(args, "tag_dir", "tags") |
| 334 |
tag_branch = getattr(args, "tag_branch", None) |
| 335 |
full_commit = getattr(args, "full_commit", False) |
| 336 |
ignore_untracked = getattr(args, "ignore_untracked", False) |
| 337 |
commit_length = getattr(args, "commit_length", None) |
| 338 |
commit_prefix = getattr(args, "commit_prefix", None) |
| 339 |
escape_with = getattr(args, "escape_with", None) |
| 340 |
from_vcs( |
| 341 |
Vcs(args.vcs), |
| 342 |
args.pattern, |
| 343 |
args.metadata, |
| 344 |
args.dirty, |
| 345 |
args.format, |
| 346 |
Style(args.style) if args.style else None, |
| 347 |
args.latest_tag, |
| 348 |
tag_dir, |
| 349 |
args.debug, |
| 350 |
args.bump, |
| 351 |
args.tagged_metadata, |
| 352 |
tag_branch, |
| 353 |
full_commit, |
| 354 |
args.strict, |
| 355 |
Path(args.path) if args.path is not None else None, |
| 356 |
args.pattern_prefix, |
| 357 |
ignore_untracked, |
| 358 |
commit_length, |
| 359 |
commit_prefix, |
| 360 |
escape_with, |
| 361 |
args.highest_tag, |
| 362 |
) |
| 363 |
elif args.command == "check": |
| 364 |
version = from_stdin(args.version) |
| 365 |
if version is None: |
| 366 |
raise ValueError("A version must be specified") |
| 367 |
check_version(version, Style(args.style)) |
| 368 |
except Exception as e: |
| 369 |
print(e, file=sys.stderr) |
| 370 |
sys.exit(1) |
| 371 |
|
| 372 |
|
| 373 |
if __name__ == "__main__": |
| 374 |
main() |