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)
def _get_version(config: Mapping) -> Tuple[Version, str]: vcs = Vcs(config["vcs"]) style = config["style"] if style is not None: style = Style(style) version = Version.from_vcs(vcs, config["pattern"], config["latest-tag"], config["subversion"]["tag-dir"]) if config["format-jinja"]: base = version.base revision = version.revision if config["bump"]: if version.stage is None: base = bump_version(version.base) else: if version.revision is None: revision = 2 else: revision = version.revision + 1 default_context = { "base": base, "version": version, "stage": version.stage, "revision": revision, "distance": version.distance, "commit": version.commit, "dirty": version.dirty, "env": os.environ, "bump_version": bump_version, "tagged_metadata": version.tagged_metadata, "serialize_pep440": serialize_pep440, "serialize_pvp": serialize_pvp, "serialize_semver": serialize_semver, } custom_context = {} # type: dict for entry in config["format-jinja-imports"]: if "module" in entry: module = import_module(entry["module"]) if "item" in entry: custom_context[entry["item"]] = getattr( module, entry["item"]) else: custom_context[entry["module"]] = module serialized = jinja2.Template( config["format-jinja"]).render(**default_context, **custom_context) if style is not None: check_version(serialized, style) else: serialized = version.serialize( metadata=config["metadata"], dirty=config["dirty"], format=config["format"], style=style, bump=config["bump"], tagged_metadata=config["tagged-metadata"], ) return (version, serialized)
def _get_version(config: Mapping, pyproject_path: Path) -> Tuple[Version, str]: if _state.version: return _state.version pyproject = tomlkit.parse(pyproject_path.read_text(encoding='utf8')) if not _state.original_version: _state.original_version = pyproject["tool"]["poetry"]["version"] vcs = Vcs(config["vcs"]) style = config["style"] if style is not None: style = Style(style) version = Version.from_vcs(vcs, config["pattern"], config["latest-tag"], config["subversion"]["tag-dir"]) if config["format-jinja"]: serialized = jinja2.Template(config["format-jinja"]).render( base=version.base, stage=version.stage, revision=version.revision, distance=version.distance, commit=version.commit, dirty=version.dirty, env=os.environ, bump_version=bump_version, serialize_pep440=serialize_pep440, serialize_pvp=serialize_pvp, serialize_semver=serialize_semver, ) if style is not None: check_version(serialized, style) else: serialized = version.serialize(config["metadata"], config["dirty"], config["format"], style) pyproject["tool"]["poetry"]["version"] = serialized pyproject_path.write_text(tomlkit.dumps(pyproject)) _state.version = (version, serialized) return (version, serialized)
def test__check_version__pvp() -> None: style = Style.Pvp check_version("1", style=style) check_version("0.1", style=style) check_version("0.0.1", style=style) check_version("0.0.0.1", style=style) check_version("0.1.0-alpha-1", style=style) with pytest.raises(ValueError): check_version("0.1.0-a.1", style=style)
def test__check_version__semver() -> None: style = Style.SemVer check_version("0.1.0", style=style) check_version("0.1.0-alpha.1", style=style) check_version("0.1.0+abc", style=style) check_version("0.1.0-alpha.1.beta.2+abc.dirty", style=style) with pytest.raises(ValueError): check_version("1", style=style) with pytest.raises(ValueError): check_version("0.1", style=style) with pytest.raises(ValueError): check_version("0.0.0.1", style=style) # "-" is a valid identifier. Version("0.1.0--").serialize(style=style) Version("0.1.0--.-").serialize(style=style) # No leading zeroes in numeric segments: with pytest.raises(ValueError): Version("00.0.0").serialize(style=style) with pytest.raises(ValueError): Version("0.01.0").serialize(style=style) with pytest.raises(ValueError): Version("0.1.0-alpha.02").serialize(style=style) # But leading zeroes are fine for non-numeric parts: Version("0.1.0-alpha.02a").serialize(style=style) # Identifiers can't be empty: with pytest.raises(ValueError): Version("0.1.0-.").serialize(style=style) with pytest.raises(ValueError): Version("0.1.0-a.").serialize(style=style) with pytest.raises(ValueError): Version("0.1.0-.a").serialize(style=style)
def test__check_version__pep440() -> None: check_version("0.1.0") check_version("0.01.0") check_version("2!0.1.0") check_version("0.1.0a1") check_version("0.1.0b1") check_version("0.1.0rc1") with pytest.raises(ValueError): check_version("0.1.0x1") check_version("0.1.0.post0") check_version("0.1.0.dev0") check_version("0.1.0.post0.dev0") with pytest.raises(ValueError): check_version("0.1.0.other0") check_version("0.1.0+abc.dirty") check_version("2!0.1.0a1.post0.dev0+abc.dirty")