mbarkhau / bumpver

BumpVer: Automatic Versioning
https://github.com/mbarkhau/bumpver
MIT License
199 stars 36 forks source link

Possible to have two re pattern for one line replacement? #190

Closed unkcpz closed 2 years ago

unkcpz commented 2 years ago

The bumpver tool is really great but we now can not figure out how to use it for our new version replacement case. We have a package dependency defined in setup.cfg that needs to update with every release

aiidalab-qe-workchain@https://github.com/aiidalab/aiidalab-qe/releases/download/v22.09.1/aiidalab_qe_workchain-22.9.1-py3-none-any.whl

We defined the pattern as:

[bumpver]
current_version = "v22.09.1"
version_pattern = "v0Y.0M.PATCH[PYTAGNUM]"
commit_message = "Bump version {old_version} -> {new_version}"
commit = True
tag = True
push = True

[bumpver:file_patterns]
setup.cfg =
    aiidalab-qe-workchain@https://github.com/aiidalab/aiidalab-qe/releases/download/{version}/aiidalab_qe_workchain-{pep440_version}-py3-none-any.whl

However, bumpver update --dry-run end up with the error:

WARNING - Invalid patterns for setup.cfg (aiidalab-qe-workchain@https://github.com/aiidalab/aiidalab-qe/releases/download/{version}/aiidalab_qe_workchain-{pep440_version}-py3-none-any.whl)
Traceback (most recent call last):
  File "/home/aiida/.local/bin/bumpver", line 8, in <module>
    sys.exit(cli())
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/conda/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/cli.py", line 580, in update
    _, cfg = config.init(project_path='.')
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/config.py", line 399, in init
    cfg = parse(ctx, cfg_missing_ok)
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/config.py", line 385, in parse
    return _parse_config(raw_cfg)
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/config.py", line 305, in _parse_config
    file_patterns = _compile_file_patterns(raw_cfg, is_new_pattern)
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/config.py", line 259, in _compile_file_patterns
    for path, patterns in _file_pattern_items:
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/config.py", line 243, in _compile_v2_file_patterns
    v2patterns.compile_pattern(version_pattern, raw_pattern)
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/utils.py", line 27, in wrapper
    cache[key] = func(*args)
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/v2patterns.py", line 221, in compile_pattern
    regexp = _compile_pattern_re(normalized_pattern)
  File "/home/aiida/.local/lib/python3.8/site-packages/bumpver/v2patterns.py", line 214, in _compile_pattern_re
    return re.compile(pattern_str)
  File "/opt/conda/lib/python3.8/re.py", line 252, in compile
    return _compile(pattern, flags)
  File "/opt/conda/lib/python3.8/re.py", line 304, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/opt/conda/lib/python3.8/sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "/opt/conda/lib/python3.8/sre_parse.py", line 948, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/opt/conda/lib/python3.8/sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "/opt/conda/lib/python3.8/sre_parse.py", line 831, in _parse
    raise source.error(err.msg, len(name) + 1) from None
re.error: redefinition of group name 'month' as group 6; was group 2 at position 224

Is that already supported to have two matches in one line? If not how can I set the pattern for the replacement. Thanks!

unkcpz commented 2 years ago

With some investigation, the pattern_str used for this case is:

'aiidalab\\-qe\\-workchain@https://github\\.com/aiidalab/aiidalab\\-qe/releases/download/v(?P<year_y>[0-9]{2})\\.(?P<month>1[0-2]|0[1-9])\\.(?P<patch>[0-9]+)(?:(?P<pytag>post|rc|a|b)(?P<num>[0-9]+))?/aiidalab_qe_workchain\\-0Y\\.(?P<month>1[0-2]|[1-9])\\.PATCH(?:PYTAGNUM)?\\-py3\\-none\\-any\\.whl'

I think only the group index needs to be increased to match the position when more than one pattern is used in one line.

mbarkhau commented 2 years ago

Nice catch, yes it should be possible to handle this somehow.

mbarkhau commented 2 years ago

Fixed with 2022.1019

unkcpz commented 2 years ago

@mbarkhau thanks a lot for the quick fix. 🚀