diff --git a/README.md b/README.md index 3ef3d0b..db3fe38 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ Exiting because of '--dry'. Would have written to pycalver.toml: [pycalver] current_version = "v202010.1001-alpha" - version_pattern = "vYYYY0M.BUILD[-TAG]" + version_pattern = "vYYYY0M.BUILD[-RELEASE]" commit_message = "bump version to {new_version}" commit = true tag = true @@ -125,7 +125,7 @@ This will add the something like the following to your `setup.cfg` # setup.cfg [pycalver] current_version = "v201902.1001-alpha" -version_pattern = "vYYYY0M.BUILD[-TAG]" +version_pattern = "vYYYY0M.BUILD[-RELEASE]" commit_message = "bump version to {new_version}" commit = True tag = True @@ -238,7 +238,7 @@ These patterns are closely based on https://calver.org/ | `00J` | 001, 002..366 | `%j` | | `BUILD` | 0011, 1001, 1002, .. | build number (lexid) | | `BLD` | 11, 1001, 1002, .. | zero truncated `BUILD` | -| `TAG` | alpha, beta, rc | `--release=` | +| `RELEASE` | alpha, beta, rc | `--release=` | | `PYTAG` | a, b, rc | `--release=` | | `NUM` | 0, 1, 2... | release tag number | | `MAJOR` | 0..9, 10..99, 100.. | `--major` | @@ -280,7 +280,7 @@ According to these rules: For example: -- Pattern: `vYY.0M.0D[-TAG]` +- Pattern: `vYY.0M.0D[-RELEASE]` - Version: `v20.08.02-beta` - PEP440 : `20.8.2b0` @@ -298,22 +298,22 @@ It may also be confusing to your users if they a list of version numbers, sorted If you wish to avoid this, you should use a pattern which maintains lexiographical ordering. -| pattern | example | lexio. | PEP440 | lexio. | -|-----------------------|---------|--------|--------|--------| -| `YYYY0M.BUILD[-TAG]` | | yes | | yes | -| `YYYY.BUILD[-TAG]` | | yes | | yes | -| `YYYY0M.MINOR[-TAG]` | | yes² | | yes | -| `YY0M.BUILD[-TAG]` | | yes¹ | | yes¹ | -| `YYYY.MM.MINOR[-TAG]` | | no | | no | -| `YYYY.0M.MINOR[-TAG]` | | yes² | | no | -| `YYYY.WW.MINOR[-TAG]` | | no | | no | -| `YYYY.0W.MINOR[-TAG]` | | yes² | | no | -| `YYYY.0M.0D` | | yes | | no | -| `YYYY.MM.DD` | | no | | no | -| `vYYYY.0W` | | yes | | no | -| `vYYYY.WW` | | no | | no | -| `YYYY.0M` | | yes | | no | -| `YYYY.MM` | | no | | no | +| pattern | example | lexio. | PEP440 | lexio. | +|---------------------------|---------|--------|--------|--------| +| `YYYY0M.BUILD[-RELEASE]` | | yes | | yes | +| `YYYY.BUILD[-RELEASE]` | | yes | | yes | +| `YYYY0M.MINOR[-RELEASE]` | | yes² | | yes | +| `YY0M.BUILD[-RELEASE]` | | yes¹ | | yes¹ | +| `YYYY.MM.MINOR[-RELEASE]` | | no | | no | +| `YYYY.0M.MINOR[-RELEASE]` | | yes² | | no | +| `YYYY.WW.MINOR[-RELEASE]` | | no | | no | +| `YYYY.0W.MINOR[-RELEASE]` | | yes² | | no | +| `YYYY.0M.0D` | | yes | | no | +| `YYYY.MM.DD` | | no | | no | +| `vYYYY.0W` | | yes | | no | +| `vYYYY.WW` | | no | | no | +| `YYYY.0M` | | yes | | no | +| `YYYY.MM` | | no | | no | - ¹ Until 2099. If your project has new releases after 2099, future maintainers can change `YY`/`0Y` -> `YYYY` so that they don't release `00.xx`. - ² As long as `MINOR <= 9` @@ -427,15 +427,15 @@ $ pycalver test 'v18.1.2' 'vYY.MINOR.PATCH' --minor New Version: v19.2.0 PEP440 : 19.2.0 -$ pycalver test 'v201811.1051-beta' 'vYYYYMM.BUILD[-TAG]' +$ pycalver test 'v201811.1051-beta' 'vYYYYMM.BUILD[-RELEASE]' New Version: v201902.1052-beta PEP440 : 201902.1052b0 -$ pycalver test 'v201811.0051-beta' 'vYYYYMM.BUILD[-TAG]' --release rc +$ pycalver test 'v201811.0051-beta' 'vYYYYMM.BUILD[-RELEASE]' --release rc New Version: v201902.1052-rc PEP440 : 201902.1052rc0 -$ pycalver test 'v201811.0051-beta' 'vYYYYMM.BUILD[-TAG]' --release final +$ pycalver test 'v201811.0051-beta' 'vYYYYMM.BUILD[-RELEASE]' --release final New Version: v201902.1052 PEP440 : 201902.1052 ``` @@ -509,7 +509,7 @@ section: ```ini [pycalver] current_version = "v202010.1006-beta" -version_pattern = "vYYYY0M.BUILD[-TAG]" +version_pattern = "vYYYY0M.BUILD[-RELEASE]" commit_message = "bump version to {new_version}" commit = True tag = True @@ -549,7 +549,7 @@ $ pycalver bump --dry [pycalver] -current_version = v202010.1005-beta +current_version = v202010.1006-beta - version_pattern = "vYYYY0M.BUILD[-TAG]" + version_pattern = "vYYYY0M.BUILD[-RELEASE]" commit_message = "bump version to {new_version}" commit = True ... diff --git a/pylint-ignore.md b/pylint-ignore.md index 3d65cdb..ed68081 100644 --- a/pylint-ignore.md +++ b/pylint-ignore.md @@ -23,7 +23,6 @@ The recommended approach to using `pylint-ignore` is: # Overview - - [E1123: unexpected-keyword-arg (1x)](#e1123-unexpected-keyword-arg) - [W0511: fixme (9x)](#w0511-fixme) - [W0703: broad-except (1x)](#w0703-broad-except) @@ -74,8 +73,8 @@ The recommended approach to using `pylint-ignore` is: 154: assert "setup.py" in cfg.file_patterns 155: assert "setup.cfg" in cfg.file_patterns > 156: # TODO (mb 2020-09-18): - 157: # assert cfg.file_patterns["setup.py" ] == ["vYYYY0M.BUILD[-TAG]", "YYYY0M.BLD[PYTAGNUM]"] - 158: # assert cfg.file_patterns["setup.cfg" ] == ['current_version = "vYYYY0M.BUILD[-TAG]"'] + 157: # assert cfg.file_patterns["setup.py" ] == ["vYYYY0M.BUILD[-RELEASE]", "YYYY0M.BLD[PYTAGNUM]"] + 158: # assert cfg.file_patterns["setup.cfg" ] == ['current_version = "vYYYY0M.BUILD[-RELEASE]"'] ``` @@ -96,71 +95,54 @@ The recommended approach to using `pylint-ignore` is: ``` -## File src/pycalver/v1patterns.py - Line 212 - W0511 (fixme) +## File src/pycalver/v1patterns.py - Line 214 - W0511 (fixme) - `message: TODO (mb 2020-09-19): replace {version} etc with version_pattern` - `author : Manuel Barkhau ` - `date : 2020-09-19T16:24:10` ``` - 199: def _compile_pattern_re(version_pattern: str, raw_pattern: str) -> typ.Pattern[str]: + 201: def _compile_pattern_re(version_pattern: str, raw_pattern: str) -> typ.Pattern[str]: ... - 210: escaped_pattern = escaped_pattern.replace(char, escaped) - 211: -> 212: # TODO (mb 2020-09-19): replace {version} etc with version_pattern - 213: pattern_str = _replace_pattern_parts(escaped_pattern) - 214: return re.compile(pattern_str) + 212: escaped_pattern = escaped_pattern.replace(char, escaped) + 213: +> 214: # TODO (mb 2020-09-19): replace {version} etc with version_pattern + 215: pattern_str = _replace_pattern_parts(escaped_pattern) + 216: return re.compile(pattern_str) ``` -## File src/pycalver/__main__.py - Line 247 - W0511 (fixme) +## File src/pycalver/__main__.py - Line 250 - W0511 (fixme) - `message: TODO (mb 2020-09-18): Investigate error messages` - `author : Manuel Barkhau ` - `date : 2020-09-19T16:24:10` ``` - 219: def _bump( + 222: def _bump( ... - 245: sys.exit(1) - 246: except Exception as ex: -> 247: # TODO (mb 2020-09-18): Investigate error messages - 248: logger.error(str(ex)) - 249: sys.exit(1) + 248: sys.exit(1) + 249: except Exception as ex: +> 250: # TODO (mb 2020-09-18): Investigate error messages + 251: logger.error(str(ex)) + 252: sys.exit(1) ``` -## File src/pycalver/v2patterns.py - Line 256 - W0511 (fixme) - -- `message: TODO (mb 2020-09-19): replace {version} etc with version_pattern` -- `author : Manuel Barkhau ` -- `date : 2020-09-19T16:24:10` - -``` - 240: def _compile_pattern_re(version_pattern: str, raw_pattern: str) -> typ.Pattern[str]: - ... - 254: print("<<<<", (normalized_pattern,)) - 255: -> 256: # TODO (mb 2020-09-19): replace {version} etc with version_pattern - 257: pattern_str = _replace_pattern_parts(escaped_pattern) - 258: return re.compile(pattern_str) -``` - - -## File src/pycalver/config.py - Line 264 - W0511 (fixme) +## File src/pycalver/config.py - Line 273 - W0511 (fixme) - `message: TODO (mb 2020-09-18): Validate Pattern` - `author : Manuel Barkhau ` - `date : 2020-09-18T19:04:06` ``` - 250: def _parse_config(raw_cfg: RawConfig) -> Config: + 251: def _parse_config(raw_cfg: RawConfig) -> Config: ... - 262: is_new_pattern = "{" not in version_pattern and "}" not in version_pattern - 263: -> 264: # TODO (mb 2020-09-18): Validate Pattern - 265: # detect YY with WW or UU -> suggest GG with VV - 266: # detect YYMM -> suggest YY0M + 271: ) + 272: +> 273: # TODO (mb 2020-09-18): Validate Pattern + 274: # detect YY with WW or UU -> suggest GG with VV + 275: # detect YYMM -> suggest YY0M ``` @@ -178,22 +160,39 @@ The recommended approach to using `pylint-ignore` is: ``` +## File src/pycalver/v2version.py - Line 551 - W0511 (fixme) + +- `message: TODO (mb 2020-09-20): New Rollover Behaviour:` +- `author : Manuel Barkhau ` +- `date : 2020-09-20T17:36:38` + +``` + 508: def incr( + ... + 549: cur_vinfo = cur_vinfo._replace(patch=cur_vinfo.patch + 1) + 550: +> 551: # TODO (mb 2020-09-20): New Rollover Behaviour: + 552: # Reset major, minor, patch to zero if any part to the left of it is incremented + 553: +``` + + # W0703: broad-except -## File src/pycalver/__main__.py - Line 246 - W0703 (broad-except) +## File src/pycalver/__main__.py - Line 249 - W0703 (broad-except) - `message: Catching too general exception Exception` - `author : Manuel Barkhau ` - `date : 2020-09-05T14:30:17` ``` - 219: def _bump( + 222: def _bump( ... - 244: logger.error(str(ex)) - 245: sys.exit(1) -> 246: except Exception as ex: - 247: # TODO (mb 2020-09-18): Investigate error messages - 248: logger.error(str(ex)) + 247: logger.error(str(ex)) + 248: sys.exit(1) +> 249: except Exception as ex: + 250: # TODO (mb 2020-09-18): Investigate error messages + 251: logger.error(str(ex)) ``` diff --git a/setup.cfg b/setup.cfg index 520c0de..22e319c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -92,28 +92,34 @@ addopts = --doctest-modules [pycalver] -current_version = v202007.1036 -version_pattern = "{pycalver}" +current_version = "v202007.1036" +version_pattern = "vYYYY0M.BUILD[-RELEASE[NUM]]" +commit_message = "bump version to {new_version}" commit = True tag = True push = True [pycalver:file_patterns] bootstrapit.sh = - PACKAGE_VERSION="{pycalver}" + PACKAGE_VERSION="{version}" setup.cfg = - current_version = {pycalver} + current_version = "{version}" setup.py = - version="{pep440_pycalver}" + version="{pep440_version}" src/pycalver/__init__.py = - __version__ = "{pycalver}" -src/pycalver/cli.py = - click.version_option(version="{pycalver}") + __version__ = "{version}" +src/pycalver/__main__.py = + click.version_option(version="{version}") +src/pycalver*/*.py = + Copyright (c) 2018-YYYY +LICENSE = + Copyright (c) 2018-YYYY +license.header = + Copyright (c) 2018-YYYY README.md = - [PyCalVer {version}] + \[PyCalVer {version}\] img.shields.io/static/v1.svg?label=PyCalVer&message={version}&color=blue Successfully installed pycalver-{pep440_version} - pycalver, version {version} [tool:pylint] diff --git a/src/pycalver/v1version.py b/src/pycalver/v1version.py index 11fdb82..978eb39 100644 --- a/src/pycalver/v1version.py +++ b/src/pycalver/v1version.py @@ -97,7 +97,7 @@ def _parse_field_values(field_values: FieldValues) -> version.V1VersionInfo: tag = fvals.get('tag') if tag is None: tag = "final" - tag = version.TAG_BY_PEP440_TAG.get(tag, tag) + tag = version.RELEASE_BY_PEP440_TAG.get(tag, tag) assert tag is not None bid = fvals['bid'] if 'bid' in fvals else "0001" @@ -338,15 +338,15 @@ def format_version(vinfo: version.V1VersionInfo, raw_pattern: str) -> str: kwargs: typ.Dict[str, typ.Union[str, int, None]] = vinfo._asdict() - tag = vinfo.tag - if tag == 'final': + release_tag = vinfo.tag + if release_tag == 'final': kwargs['release' ] = "" kwargs['pep440_tag'] = "" else: - kwargs['release' ] = "-" + tag - kwargs['pep440_tag'] = version.PEP440_TAG_BY_TAG[tag] + "0" + kwargs['release' ] = "-" + release_tag + kwargs['pep440_tag'] = version.PEP440_TAG_BY_RELEASE[release_tag] + "0" - kwargs['release_tag'] = tag + kwargs['release_tag'] = release_tag year = vinfo.year if year: diff --git a/src/pycalver/v2patterns.py b/src/pycalver/v2patterns.py index 8001208..5306209 100644 --- a/src/pycalver/v2patterns.py +++ b/src/pycalver/v2patterns.py @@ -5,7 +5,7 @@ # SPDX-License-Identifier: MIT """Compose Regular Expressions from Patterns. ->>> pattern = compile_pattern("vYYYY0M.BUILD[-TAG]") +>>> pattern = compile_pattern("vYYYY0M.BUILD[-RELEASE]") >>> version_info = pattern.regexp.match("v201712.0123-alpha") >>> assert version_info.groupdict() == { ... "version": "v201712.0123-alpha", @@ -80,45 +80,57 @@ PART_PATTERNS = { 'VV': r"(?:5[0-3]|[1-4][0-9]|[1-9])", '0V': r"(?:5[0-3]|[1-4][0-9]|0[1-9])", # non calver parts - 'MAJOR': r"[0-9]+", - 'MINOR': r"[0-9]+", - 'PATCH': r"[0-9]+", - 'BUILD': r"[0-9]+", - 'BLD' : r"[1-9][0-9]*", - 'TAG' : r"(?:preview|final|alpha|beta|post|pre|dev|rc|a|b|c|r)", - 'PYTAG': r"(?:post|dev|rc|a|b)", - 'NUM' : r"[0-9]+", + 'MAJOR' : r"[0-9]+", + 'MINOR' : r"[0-9]+", + 'PATCH' : r"[0-9]+", + 'BUILD' : r"[0-9]+", + 'BLD' : r"[1-9][0-9]*", + 'RELEASE': r"(?:preview|final|alpha|beta|post|pre|dev|rc|a|b|c|r)", + 'PYTAG' : r"(?:post|dev|rc|a|b)", + 'NUM' : r"[0-9]+", } PATTERN_PART_FIELDS = { - 'YYYY' : 'year_y', - 'YY' : 'year_y', - '0Y' : 'year_y', - 'GGGG' : 'year_g', - 'GG' : 'year_g', - '0G' : 'year_g', - 'Q' : 'quarter', - 'MM' : 'month', - '0M' : 'month', - 'DD' : 'dom', - '0D' : 'dom', - 'JJJ' : 'doy', - '00J' : 'doy', - 'MAJOR': 'major', - 'MINOR': 'minor', - 'PATCH': 'patch', - 'BUILD': 'bid', - 'BLD' : 'bid', - 'TAG' : 'tag', - 'PYTAG': 'pytag', - 'NUM' : 'num', - 'WW' : 'week_w', - '0W' : 'week_w', - 'UU' : 'week_u', - '0U' : 'week_u', - 'VV' : 'week_v', - '0V' : 'week_v', + 'YYYY' : 'year_y', + 'YY' : 'year_y', + '0Y' : 'year_y', + 'GGGG' : 'year_g', + 'GG' : 'year_g', + '0G' : 'year_g', + 'Q' : 'quarter', + 'MM' : 'month', + '0M' : 'month', + 'DD' : 'dom', + '0D' : 'dom', + 'JJJ' : 'doy', + '00J' : 'doy', + 'MAJOR' : 'major', + 'MINOR' : 'minor', + 'PATCH' : 'patch', + 'BUILD' : 'bid', + 'BLD' : 'bid', + 'RELEASE': 'tag', + 'PYTAG' : 'pytag', + 'NUM' : 'num', + 'WW' : 'week_w', + '0W' : 'week_w', + 'UU' : 'week_u', + '0U' : 'week_u', + 'VV' : 'week_v', + '0V' : 'week_v', +} + + +PEP440_PART_SUBSTITUTIONS = { + '0W' : "WW", + '0U' : "UU", + '0V' : "VV", + '0M' : "MM", + '0D' : "DD", + '00J' : "JJJ", + 'BUILD' : "BLD", + 'RELEASE': "PYTAG", } @@ -174,33 +186,33 @@ def _fmt_0v(week_v: FieldValue) -> str: PART_FORMATS: typ.Dict[str, typ.Callable[[FieldValue], str]] = { - 'YYYY' : _fmt_num, - 'YY' : _fmt_yy, - '0Y' : _fmt_0y, - 'GGGG' : _fmt_num, - 'GG' : _fmt_gg, - '0G' : _fmt_0g, - 'Q' : _fmt_num, - 'MM' : _fmt_num, - '0M' : _fmt_0m, - 'DD' : _fmt_num, - '0D' : _fmt_0d, - 'JJJ' : _fmt_num, - '00J' : _fmt_00j, - 'MAJOR': _fmt_num, - 'MINOR': _fmt_num, - 'PATCH': _fmt_num, - 'BUILD': _fmt_num, - 'BLD' : _fmt_bld, - 'TAG' : _fmt_num, - 'PYTAG': _fmt_num, - 'NUM' : _fmt_num, - 'WW' : _fmt_num, - '0W' : _fmt_0w, - 'UU' : _fmt_num, - '0U' : _fmt_0u, - 'VV' : _fmt_num, - '0V' : _fmt_0v, + 'YYYY' : _fmt_num, + 'YY' : _fmt_yy, + '0Y' : _fmt_0y, + 'GGGG' : _fmt_num, + 'GG' : _fmt_gg, + '0G' : _fmt_0g, + 'Q' : _fmt_num, + 'MM' : _fmt_num, + '0M' : _fmt_0m, + 'DD' : _fmt_num, + '0D' : _fmt_0d, + 'JJJ' : _fmt_num, + '00J' : _fmt_00j, + 'MAJOR' : _fmt_num, + 'MINOR' : _fmt_num, + 'PATCH' : _fmt_num, + 'BUILD' : _fmt_num, + 'BLD' : _fmt_bld, + 'RELEASE': _fmt_num, + 'PYTAG' : _fmt_num, + 'NUM' : _fmt_num, + 'WW' : _fmt_num, + '0W' : _fmt_0w, + 'UU' : _fmt_num, + '0U' : _fmt_0u, + 'VV' : _fmt_num, + '0V' : _fmt_0v, } @@ -209,15 +221,37 @@ def _convert_to_pep440(version_pattern: str) -> str: # corner cases as specified in PEP440, in particular # related to post and dev releases. - version_pattern = version_pattern.lstrip("v") + pep440_pattern = version_pattern + + if pep440_pattern.startswith("v"): + pep440_pattern = pep440_pattern[1:] + + pep440_pattern = pep440_pattern.replace(r"\[", "") + pep440_pattern = pep440_pattern.replace(r"\]", "") + + pep440_pattern, _ = re.subn(r"[^a-zA-Z0-9\.\[\]]", "", pep440_pattern) part_names = list(PATTERN_PART_FIELDS.keys()) part_names.sort(key=len, reverse=True) - if version_pattern == "vYYYY0M.BUILD[-TAG]": - return "YYYY0M.BLD[PYTAGNUM]" - # TODO (mb 2020-09-20) - raise NotImplementedError + for part_name in part_names: + if part_name not in version_pattern: + continue + if part_name not in PEP440_PART_SUBSTITUTIONS: + continue + + substitution = PEP440_PART_SUBSTITUTIONS[part_name] + + is_numerical_part = part_name not in ('RELEASE', 'PYTAG') + if is_numerical_part: + part_index = pep440_pattern.find(part_name) + is_zero_truncation_part = part_index == 0 or pep440_pattern[part_index - 1] == "." + if is_zero_truncation_part: + pep440_pattern = pep440_pattern.replace(part_name, substitution) + else: + pep440_pattern = pep440_pattern.replace(part_name, substitution) + + return pep440_pattern def normalize_pattern(version_pattern: str, raw_pattern: str) -> str: @@ -236,10 +270,10 @@ def _replace_pattern_parts(pattern: str) -> str: # The pattern is escaped, so that everything besides the format # string variables is treated literally. while True: - new_pattern, n = re.subn(r"([^\\]|^)\[", r"\1(?:", pattern) - new_pattern, m = re.subn(r"([^\\]|^)\]", r"\1)?" , new_pattern) + new_pattern, _n = re.subn(r"([^\\]|^)\[", r"\1(?:", pattern) + new_pattern, _m = re.subn(r"([^\\]|^)\]", r"\1)?" , new_pattern) pattern = new_pattern - if n + m == 0: + if _n + _m == 0: break SortKey = typ.Tuple[int, int] @@ -274,7 +308,7 @@ def _compile_pattern_re(version_pattern: str, raw_pattern: str) -> typ.Pattern[s normalized_pattern = normalize_pattern(version_pattern, raw_pattern) escaped_pattern = normalized_pattern for char, escaped in RE_PATTERN_ESCAPES: - # [] braces are used for optional parts, such as [-TAG]/[-beta] + # [] braces are used for optional parts, such as [-RELEASE]/[-beta] # and need to be escaped manually. is_semantic_char = char in "[]\\" if not is_semantic_char: diff --git a/src/pycalver/v2rewrite.py b/src/pycalver/v2rewrite.py index 49e4c22..ce1013c 100644 --- a/src/pycalver/v2rewrite.py +++ b/src/pycalver/v2rewrite.py @@ -28,7 +28,7 @@ def rewrite_lines( """Replace occurances of patterns in old_lines with new_vinfo. >>> from .v2patterns import compile_pattern - >>> version_pattern = "vYYYY0M.BUILD[-TAG]" + >>> version_pattern = "vYYYY0M.BUILD[-RELEASE]" >>> new_vinfo = v2version.parse_version_info("v201811.0123-beta", version_pattern) >>> patterns = [compile_pattern(version_pattern, '__version__ = "{version}"')] >>> rewrite_lines(patterns, new_vinfo, ['__version__ = "v201809.0002-alpha" ']) @@ -73,9 +73,9 @@ def rfd_from_content( ) -> rewrite.RewrittenFileData: r"""Rewrite pattern occurrences with version string. - >>> version_pattern = "vYYYY0M.BUILD[-TAG]" + >>> version_pattern = "vYYYY0M.BUILD[-RELEASE]" >>> new_vinfo = v2version.parse_version_info("v201809.0123", version_pattern) - >>> raw_patterns = ['__version__ = "vYYYY0M.BUILD[-TAG]"'] + >>> raw_patterns = ['__version__ = "vYYYY0M.BUILD[-RELEASE]"'] >>> patterns = >>> content = '__version__ = "v201809.0001-alpha"' >>> rfd = rfd_from_content(patterns, new_vinfo, content) @@ -115,8 +115,8 @@ def iter_rewritten( ) -> typ.Iterable[rewrite.RewrittenFileData]: r'''Iterate over files with version string replaced. - >>> version_pattern = "vYYYY0M.BUILD[-TAG]" - >>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "vYYYY0M.BUILD[-TAG]"']} + >>> version_pattern = "vYYYY0M.BUILD[-RELEASE]" + >>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "vYYYY0M.BUILD[-RELEASE]"']} >>> new_vinfo = v2version.parse_version_info("v201809.0123", version_pattern) >>> rewritten_datas = iter_rewritten(file_patterns, new_vinfo) >>> rfd = list(rewritten_datas)[0] @@ -153,7 +153,7 @@ def diff( >>> old_vinfo = v2version.parse_version_info("v201809.0123", version_pattern) >>> new_vinfo = v2version.parse_version_info("v201810.1124", version_pattern) - >>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "vYYYY0M.BUILD[-TAG]"']} + >>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "vYYYY0M.BUILD[-RELEASE]"']} >>> diff_str = diff(old_vinfo, new_vinfo, file_patterns) >>> lines = diff_str.split("\n") >>> lines[:2] diff --git a/src/pycalver/v2version.py b/src/pycalver/v2version.py index 62929b0..ddf3ea2 100644 --- a/src/pycalver/v2version.py +++ b/src/pycalver/v2version.py @@ -149,9 +149,9 @@ def _parse_version_info(field_values: FieldValues) -> version.V2VersionInfo: pytag = fvals.get('pytag') or "" if tag and not pytag: - pytag = version.PEP440_TAG_BY_TAG[tag] + pytag = version.PEP440_TAG_BY_RELEASE[tag] elif pytag and not tag: - tag = version.TAG_BY_PEP440_TAG[pytag] + tag = version.RELEASE_BY_PEP440_TAG[pytag] date: typ.Optional[dt.date] = None @@ -221,19 +221,19 @@ def _parse_version_info(field_values: FieldValues) -> version.V2VersionInfo: def parse_version_info( - version_str: str, raw_pattern: str = "vYYYY0M.BUILD[-TAG[NUM]]" + version_str: str, raw_pattern: str = "vYYYY0M.BUILD[-RELEASE[NUM]]" ) -> version.V2VersionInfo: """Parse normalized V2VersionInfo. - >>> vinfo = parse_version_info("v201712.0033-beta0", raw_pattern="vYYYY0M.BUILD[-TAG[NUM]]") + >>> vinfo = parse_version_info("v201712.0033-beta0", raw_pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") >>> fvals = {'year_y': 2017, 'month': 12, 'bid': "0033", 'tag': "beta", 'num': 0} >>> assert vinfo == _parse_version_info(fvals) - >>> vinfo = parse_version_info("v201712.0033-beta", raw_pattern="vYYYY0M.BUILD[-TAG[NUM]]") + >>> vinfo = parse_version_info("v201712.0033-beta", raw_pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") >>> fvals = {'year_y': 2017, 'month': 12, 'bid': "0033", 'tag': "beta"} >>> assert vinfo == _parse_version_info(fvals) - >>> vinfo = parse_version_info("v201712.0033", raw_pattern="vYYYY0M.BUILD[-TAG[NUM]]") + >>> vinfo = parse_version_info("v201712.0033", raw_pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") >>> fvals = {'year_y': 2017, 'month': 12, 'bid': "0033"} >>> assert vinfo == _parse_version_info(fvals) @@ -254,10 +254,10 @@ def parse_version_info( return _parse_version_info(field_values) -def is_valid(version_str: str, raw_pattern: str = "vYYYY0M.BUILD[-TAG]") -> bool: +def is_valid(version_str: str, raw_pattern: str = "vYYYY0M.BUILD[-RELEASE[NUM]]") -> bool: """Check if a version matches a pattern. - >>> is_valid("v201712.0033-beta", raw_pattern="vYYYY0M.BUILD[-TAG]") + >>> is_valid("v201712.0033-beta", raw_pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") True >>> is_valid("v201712.0033-beta", raw_pattern="MAJOR.MINOR.PATCH") False @@ -274,7 +274,7 @@ def is_valid(version_str: str, raw_pattern: str = "vYYYY0M.BUILD[-TAG]") -> bool TemplateKwargs = typ.Dict[str, typ.Union[str, int, None]] -PartValues = typ.List[typ.Tuple[str, str]] +PartValues = typ.List[typ.Tuple[str, str]] def _format_part_values(vinfo: version.V2VersionInfo) -> PartValues: @@ -285,9 +285,9 @@ def _format_part_values(vinfo: version.V2VersionInfo) -> PartValues: It may for example have month=9, but not the formatted representation '09' for '0M'. - >>> vinfo = parse_version_info("v200709.1033-beta", pattern="vYYYY0M.BUILD[-TAG]") + >>> vinfo = parse_version_info("v200709.1033-beta", pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") >>> kwargs = dict(_format_part_values(vinfo)) - >>> (kwargs['YYYY'], kwargs['0M'], kwargs['BUILD'], kwargs['TAG']) + >>> (kwargs['YYYY'], kwargs['0M'], kwargs['BUILD'], kwargs['RELEASE[NUM]']) ('2007', '09', '1033', 'beta') >>> (kwargs['YY'], kwargs['0Y'], kwargs['MM'], kwargs['PYTAG']) ('7', '07', '9', 'b') @@ -381,8 +381,8 @@ FormattedSegmentParts = typ.List[str] def _format_segment_tree( - seg_tree: SegmentTree, - part_values : PartValues, + seg_tree : SegmentTree, + part_values: PartValues, ) -> FormattedSegmentParts: result_parts = [] for seg in seg_tree: @@ -391,7 +391,7 @@ def _format_segment_tree( else: # NOTE (mb 2020-09-24): If a segment has any zero parts, # the whole segment is skipped. - is_zero_seg = False + is_zero_seg = False formatted_seg = seg # unescape braces formatted_seg = formatted_seg.replace(r"\[", r"[") @@ -400,8 +400,7 @@ def _format_segment_tree( for part, part_value in part_values: if part in formatted_seg: is_zero_part = ( - part in version.ZERO_VALUES - and str(part_value) == version.ZERO_VALUES[part] + part in version.ZERO_VALUES and str(part_value) == version.ZERO_VALUES[part] ) if is_zero_part: is_zero_seg = True @@ -418,7 +417,7 @@ def format_version(vinfo: version.V2VersionInfo, raw_pattern: str) -> str: """Generate version string. >>> import datetime as dt - >>> vinfo = parse_version_info("v200712.0033-beta", pattern="vYYYY0M.BUILD[-TAG[NUM]]") + >>> vinfo = parse_version_info("v200712.0033-beta", pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") >>> vinfo_a = vinfo._replace(**cal_info(date=dt.date(2007, 1, 1))._asdict()) >>> vinfo_b = vinfo._replace(**cal_info(date=dt.date(2007, 12, 31))._asdict()) @@ -429,86 +428,86 @@ def format_version(vinfo: version.V2VersionInfo, raw_pattern: str) -> str: '200701.0033b' >>> format_version(vinfo_a, pattern="vYY.BLD[-PYTAGNUM]") 'v7.33-b0' - >>> format_version(vinfo_a, pattern="v0Y.BLD[-TAG]") + >>> format_version(vinfo_a, pattern="v0Y.BLD[-RELEASE[NUM]]") 'v07.33-beta' - >>> format_version(vinfo_a, pattern="vYYYY0M.BUILD[-TAG]") + >>> format_version(vinfo_a, pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") 'v200701.0033-beta' - >>> format_version(vinfo_b, pattern="vYYYY0M.BUILD[-TAG]") + >>> format_version(vinfo_b, pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") 'v200712.0033-beta' - >>> format_version(vinfo_a, pattern="vYYYYw0W.BUILD[-TAG]") + >>> format_version(vinfo_a, pattern="vYYYYw0W.BUILD[-RELEASE[NUM]]") 'v2007w01.0033-beta' - >>> format_version(vinfo_a, pattern="vYYYYwWW.BLD[-TAG]") + >>> format_version(vinfo_a, pattern="vYYYYwWW.BLD[-RELEASE[NUM]]") 'v2007w1.33-beta' - >>> format_version(vinfo_b, pattern="vYYYYw0W.BUILD[-TAG]") + >>> format_version(vinfo_b, pattern="vYYYYw0W.BUILD[-RELEASE[NUM]]") 'v2007w53.0033-beta' - >>> format_version(vinfo_a, pattern="vYYYYd00J.BUILD[-TAG]") + >>> format_version(vinfo_a, pattern="vYYYYd00J.BUILD[-RELEASE[NUM]]") 'v2007d001.0033-beta' - >>> format_version(vinfo_a, pattern="vYYYYdJJJ.BUILD[-TAG]") + >>> format_version(vinfo_a, pattern="vYYYYdJJJ.BUILD[-RELEASE[NUM]]") 'v2007d1.0033-beta' - >>> format_version(vinfo_b, pattern="vYYYYd00J.BUILD[-TAG]") + >>> format_version(vinfo_b, pattern="vYYYYd00J.BUILD[-RELEASE[NUM]]") 'v2007d365.0033-beta' >>> format_version(vinfo_a, pattern="vGGGGwVV.BLD[PYTAGNUM]") 'v2007w1.33b0' - >>> format_version(vinfo_a, pattern="vGGGGw0V.BUILD[-TAG]") + >>> format_version(vinfo_a, pattern="vGGGGw0V.BUILD[-RELEASE[NUM]]") 'v2007w01.0033-beta' - >>> format_version(vinfo_b, pattern="vGGGGw0V.BUILD[-TAG]") + >>> format_version(vinfo_b, pattern="vGGGGw0V.BUILD[-RELEASE[NUM]]") 'v2008w01.0033-beta' >>> vinfo_c = vinfo_b._replace(major=1, minor=2, patch=34, tag='final') - >>> format_version(vinfo_c, pattern="vYYYYwWW.BUILD-TAG") + >>> format_version(vinfo_c, pattern="vYYYYwWW.BUILD-RELEASE") 'v2007w53.0033-final' - >>> format_version(vinfo_c, pattern="vYYYYwWW.BUILD[-TAG]") + >>> format_version(vinfo_c, pattern="vYYYYwWW.BUILD[-RELEASE[NUM]]") 'v2007w53.0033' >>> format_version(vinfo_c, pattern="vMAJOR.MINOR.PATCH") 'v1.2.34' >>> vinfo_d = vinfo_b._replace(major=1, minor=0, patch=0, tag='final') - >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH-TAGNUM") + >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH-RELEASENUM") 'v1.0.0-final0' - >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH-TAG[NUM]") + >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH-RELEASE[NUM]") 'v1.0.0-final' - >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH-TAG") + >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH-RELEASE") 'v1.0.0-final' - >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH[-TAG]") + >>> format_version(vinfo_d, pattern="vMAJOR.MINOR.PATCH[-RELEASE[NUM]]") 'v1.0.0' - >>> format_version(vinfo_d, pattern="vMAJOR.MINOR[.PATCH[-TAG]]") + >>> format_version(vinfo_d, pattern="vMAJOR.MINOR[.PATCH[-RELEASE[NUM]]]") 'v1.0' - >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-TAG]]]") + >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-RELEASE[NUM]]]]") 'v1' >>> vinfo_d = vinfo_b._replace(major=1, minor=0, patch=1, tag='rc', num=0) >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH]]") 'v1.0.1' - >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-TAG[NUM]]]]") + >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-RELEASE[NUM]]]]") 'v1.0.1-rc' - >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-TAGNUM]]]") + >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-RELEASENUM]]]") 'v1.0.1-rc0' >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH]]") 'v1.0.1' >>> vinfo_d = vinfo_b._replace(major=1, minor=0, patch=0, tag='rc', num=2) - >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-TAG[NUM]]]]") + >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-RELEASE[NUM]]]]") 'v1.0.0-rc2' >>> vinfo_d = vinfo_b._replace(major=1, minor=0, patch=0, tag='rc', num=2) - >>> format_version(vinfo_d, pattern='__version__ = "vMAJOR[.MINOR[.PATCH[-TAG[NUM]]]]"') + >>> format_version(vinfo_d, pattern='__version__ = "vMAJOR[.MINOR[.PATCH[-RELEASE[NUM]]]]"') '__version__ = "v1.0.0-rc2"' """ - part_values = _format_part_values(vinfo) - seg_tree = _parse_segment_tree(raw_pattern) + part_values = _format_part_values(vinfo) + seg_tree = _parse_segment_tree(raw_pattern) version_str_parts = _format_segment_tree(seg_tree, part_values) return "".join(version_str_parts) def incr( old_version: str, - raw_pattern: str = "vYYYY0M.BUILD[-TAG]", + raw_pattern: str = "vYYYY0M.BUILD[-RELEASE]", *, release : typ.Optional[str] = None, major : bool = False, diff --git a/src/pycalver/version.py b/src/pycalver/version.py index c3b06bf..9169cd2 100644 --- a/src/pycalver/version.py +++ b/src/pycalver/version.py @@ -79,7 +79,7 @@ class V2VersionInfo(typ.NamedTuple): TODAY = dt.datetime.utcnow().date() -TAG_BY_PEP440_TAG = { +RELEASE_BY_PEP440_TAG = { 'a' : 'alpha', 'b' : 'beta', '' : 'final', @@ -89,7 +89,7 @@ TAG_BY_PEP440_TAG = { } -PEP440_TAG_BY_TAG = { +PEP440_TAG_BY_RELEASE = { 'a' : 'a', 'b' : 'b', 'dev' : 'dev', @@ -105,17 +105,17 @@ PEP440_TAG_BY_TAG = { 'rev' : 'post', } -assert set(TAG_BY_PEP440_TAG.keys()) == set(PEP440_TAG_BY_TAG.values()) -assert set(TAG_BY_PEP440_TAG.values()) < set(PEP440_TAG_BY_TAG.keys()) +assert set(RELEASE_BY_PEP440_TAG.keys()) == set(PEP440_TAG_BY_RELEASE.values()) +assert set(RELEASE_BY_PEP440_TAG.values()) < set(PEP440_TAG_BY_RELEASE.keys()) ZERO_VALUES = { - 'MAJOR': "0", - 'MINOR': "0", - 'PATCH': "0", - 'TAG' : "final", - 'PYTAG': "", - 'NUM' : "0", + 'MAJOR' : "0", + 'MINOR' : "0", + 'PATCH' : "0", + 'RELEASE': "final", + 'PYTAG' : "", + 'NUM' : "0", } diff --git a/test/test_config.py b/test/test_config.py index da611b1..5953f8d 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -66,7 +66,7 @@ setup.cfg = NEW_PATTERN_CFG_FIXTURE = """ [pycalver] current_version = "v201808.1456-beta" -version_pattern = "vYYYY0M.BUILD[-TAG]" +version_pattern = "vYYYY0M.BUILD[-RELEASE]" commit_message = "bump version to {new_version}" commit = True tag = True @@ -154,8 +154,8 @@ def test_parse_v2_cfg(): assert "setup.py" in cfg.file_patterns assert "setup.cfg" in cfg.file_patterns # TODO (mb 2020-09-18): - # assert cfg.file_patterns["setup.py" ] == ["vYYYY0M.BUILD[-TAG]", "YYYY0M.BLD[PYTAGNUM]"] - # assert cfg.file_patterns["setup.cfg" ] == ['current_version = "vYYYY0M.BUILD[-TAG]"'] + # assert cfg.file_patterns["setup.py" ] == ["vYYYY0M.BUILD[-RELEASE]", "YYYY0M.BLD[PYTAGNUM]"] + # assert cfg.file_patterns["setup.cfg" ] == ['current_version = "vYYYY0M.BUILD[-RELEASE]"'] # assert cfg.file_patterns["src/project/*.py"] == ['Copyright (c) 2018-YYYY"'] diff --git a/test/test_patterns.py b/test/test_patterns.py index 37c3b45..aebb99c 100644 --- a/test/test_patterns.py +++ b/test/test_patterns.py @@ -129,25 +129,25 @@ V2_PART_PATTERN_CASES = [ (['0V'], "53", "53"), (['0V'], "54", None), (['MAJOR', 'MINOR', 'PATCH'], "0", "0"), - (['TAG' ], "alpha" , "alpha"), - (['TAG' ], "alfa" , None), - (['TAG' ], "beta" , "beta"), - (['TAG' ], "dev" , "dev"), - (['TAG' ], "rc" , "rc"), - (['TAG' ], "post" , "post"), - (['TAG' ], "final" , "final"), - (['TAG' ], "latest", None), - (['PYTAG'], "a" , "a"), - (['PYTAG'], "b" , "b"), - (['PYTAG'], "dev" , "dev"), - (['PYTAG'], "rc" , "rc"), - (['PYTAG'], "post" , "post"), - (['PYTAG'], "post" , "post"), - (['PYTAG'], "x" , None), - (['NUM' ], "a" , None), - (['NUM' ], "0" , "0"), - (['NUM' ], "1" , "1"), - (['NUM' ], "10" , "10"), + (['RELEASE'], "alpha" , "alpha"), + (['RELEASE'], "alfa" , None), + (['RELEASE'], "beta" , "beta"), + (['RELEASE'], "dev" , "dev"), + (['RELEASE'], "rc" , "rc"), + (['RELEASE'], "post" , "post"), + (['RELEASE'], "final" , "final"), + (['RELEASE'], "latest", None), + (['PYTAG' ], "a" , "a"), + (['PYTAG' ], "b" , "b"), + (['PYTAG' ], "dev" , "dev"), + (['PYTAG' ], "rc" , "rc"), + (['PYTAG' ], "post" , "post"), + (['PYTAG' ], "post" , "post"), + (['PYTAG' ], "x" , None), + (['NUM' ], "a" , None), + (['NUM' ], "0" , "0"), + (['NUM' ], "1" , "1"), + (['NUM' ], "10" , "10"), ] diff --git a/test/test_rewrite.py b/test/test_rewrite.py index 9b2e61e..92cd854 100644 --- a/test/test_rewrite.py +++ b/test/test_rewrite.py @@ -141,8 +141,8 @@ def test_v1_optional_release(): def test_v2_optional_release(): old_lines = OPTIONAL_RELEASE_FIXTURE.splitlines() - pattern = "YYYY.BUILD[-TAG]" - patterns = ['__version__ = "YYYY.BUILD[-TAG]"'] + pattern = "YYYY.BUILD[-RELEASE]" + patterns = ['__version__ = "YYYY.BUILD[-RELEASE]"'] new_vinfo = v2version.parse_version_info("2019.0003", pattern) new_lines = v2rewrite.rewrite_lines(patterns, new_vinfo, old_lines) diff --git a/test/test_version.py b/test/test_version.py index 6e469cf..3ed968b 100644 --- a/test/test_version.py +++ b/test/test_version.py @@ -201,40 +201,29 @@ def test_v1_parse_versions(pattern_str, line, expected_vinfo): # def test_v2_parse_versions(pattern_str, line, expected_vinfo): def test_v2_parse_versions(): - _vnfo = v2version.parse_version_info("v201712.0033", raw_pattern="vYYYY0M.BUILD[-TAG[NUM]]") + _vnfo = v2version.parse_version_info("v201712.0033", raw_pattern="vYYYY0M.BUILD[-RELEASE[NUM]]") fvals = {'year_y': 2017, 'month': 12, 'bid': "0033"} assert _vnfo == v2version._parse_version_info(fvals) -def test_make_segments(): - segs = v2version._make_segments("vYYYY0M.BUILD[-TAG[NUM]]") - assert segs == ["vYYYY0M.BUILD", "-TAG", "NUM", "", ""] - - segs = v2version._make_segments('__version__ = "YYYY0M.BLD[PYTAGNUM]"') - assert segs == ['__version__ = "YYYY0M.BLD', 'PYTAGNUM', '"'] - - segs = v2version._make_segments('__version__ = "YYYY.BUILD[-TAG]"') - assert segs == ['__version__ = "YYYY.BUILD', '-TAG', '"'] - - def test_v2_format_version(): - version_pattern = "vYYYY0M.BUILD[-TAG[NUM]]" + version_pattern = "vYYYY0M.BUILD[-RELEASE[NUM]]" in_version = "v200701.0033-beta" vinfo = v2version.parse_version_info(in_version, raw_pattern=version_pattern) out_version = v2version.format_version(vinfo, raw_pattern=version_pattern) assert in_version == out_version - result = v2version.format_version(vinfo, raw_pattern="v0Y.BUILD[-TAG]") + result = v2version.format_version(vinfo, raw_pattern="v0Y.BUILD[-RELEASE]") assert result == "v07.0033-beta" - result = v2version.format_version(vinfo, raw_pattern="vYY.BLD[-TAG]") + result = v2version.format_version(vinfo, raw_pattern="vYY.BLD[-RELEASE]") assert result == "v7.33-beta" - result = v2version.format_version(vinfo, raw_pattern="vYY.BLD-TAG") + result = v2version.format_version(vinfo, raw_pattern="vYY.BLD-RELEASE") assert result == "v7.33-beta" - result = v2version.format_version(vinfo, raw_pattern='__version__ = "YYYY.BUILD[-TAG]"') + result = v2version.format_version(vinfo, raw_pattern='__version__ = "YYYY.BUILD[-RELEASE]"') assert result == '__version__ = "2007.0033-beta"' result = v2version.format_version(vinfo, raw_pattern='__version__ = "YYYY.BLD"')