mbarkhau / bumpver

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

Greedy pattern matching #215

Closed cereal2nd closed 1 year ago

cereal2nd commented 1 year ago
bumpver update --dry -v
INFO    - fetching tags from remote (to turn off use: -n / --no-fetch)
INFO    - Using pattern YYYY.MM.INC0
INFO    - regex = re.compile(r"""
    (?P<year_y>[1-9][0-9]{3})
    \.
    (?P<month>1[0-2]|[1-9])
    \.
    (?P<inc0>[0-9]+)
""", flags=re.VERBOSE)
INFO    - Old Version: 2023.7.3
INFO    - New Version: 2023.7.4
ERROR   - No match for pattern 'current_version = "YYYY.MM.INC0"'
ERROR   -
# https://regex101.com/?flavor=python&flags=gmx&regex=current_version%5B%20%5D%3D%5B%20%5D%5C%22%0A%28%3FP%3Cyear_y%3E%5B1-9%5D%5B0-9%5D%7B3%7D%29%0A%5C.%0A%28%3FP%3Cmonth%3E1%5B0-2%5D%7C%5B1-9%5D%29%0A%5C.%0A%28%3FP%3Cinc0%3E%5B0-9%5D%2B%29%0A%5C%22
regex = re.compile(r"""
    current_version[ ]=[ ]\"
    (?P<year_y>[1-9][0-9]{3})
    \.
    (?P<month>1[0-2]|[1-9])
    \.
    (?P<inc0>[0-9]+)
    \"
""", flags=re.VERBOSE)
ERROR   - No patterns matched for file 'pyproject.toml'

but if i debug this with bumbver grep it seems to work

╭─cereal@wietje ~/pyDuotecno ‹main●› ‹venv›
╰─$ bumpver grep "current_version = \"YYYY.MM.INC0\"" pyproject.toml
  43: [tool.bumpver]
  44: current_version = "2023.7.3"
  45: version_pattern = "YYYY.MM.INC0"

╭─cereal@wietje ~/pyDuotecno ‹main●› ‹venv›
╰─$ bumpver grep "version = \"YYYY.MM.INC0\"" pyproject.toml
   6: license = {text = "Apache"}
   7: version = "2023.7.3"
   8: description = "Open-source home automation platform running on Python 3."
  43: [tool.bumpver]
  44: current_version = "2023.7.3"
  45: version_pattern = "YYYY.MM.INC0"

this is the pyproject.toml file

╭─cereal@wietje ~/pyDuotecno ‹main●› ‹venv›
╰─$ cat pyproject.toml                                                                                                                                                                                                                                                                                                                                    1 ↵
[build-system]
requires = ["setuptools", "wheel"]

[project]
name = "pyDuotecno"
license = {text = "Apache"}
version = "2023.7.3"
description = "Open-source home automation platform running on Python 3."
readme = "README.md"
authors = [
    {name = "Maikel Punie", email = "maikel.punie@gmail.com"}
]
keywords = ["home", "duotecno", "automation"]
classifiers = [
    "Development Status :: 5 - Production/Stable",
    "Intended Audience :: Developers",
    "License :: OSI Approved :: MIT License",
    "Natural Language :: English",
    "Operating System :: OS Independent",
    "Programming Language :: Python",
    "Programming Language :: Python :: 3.9",
    "Programming Language :: Python :: 3.10",
    "Programming Language :: Python :: 3.11",
    "Topic :: Home Automation",
    "Topic :: Software Development :: Libraries",
    "Topic :: Software Development :: Libraries :: Python Modules",
]
requires-python = ">=3.9.0"
dependencies = []

[project.urls]
"Source Code" = "https://github.com/Cereal2nd/pyDuotecno"
"Bug Reports" = "https://github.com/Cereal2nd/pyDuotecno/issues"

[tool.setuptools]
platforms = ["any"]
zip-safe  = false
include-package-data = true

[tool.setuptools.packages.find]
exclude = ["tests", "tests.*"]

[tool.bumpver]
current_version = "2023.7.3"
version_pattern = "YYYY.MM.INC0"
commit_message = "bump version {old_version} -> {new_version}"
commit = true
tag = true
push = true

[tool.bumpver.file_patterns]
"pyproject.toml" = [
    'version = "{version}"',
    'current_version = "{version}"',
]
mbarkhau commented 1 year ago

That is indeed strange. Try either of these:

"pyproject.toml" = [
    '^version = "{version}"',
    '^current_version = "{version}"',
]
"pyproject.toml" = [
    'current_version = "{version}"',
    'version = "{version}"',
]

What I think is happening: The pattern starting with version = is greedy and matches both for line 7 and 44. Then there is nothing left for the pattern with current_version = to match against.

Can you think of a way to fix this? The least we might be able to do is to generate a better error message.

cereal2nd commented 1 year ago
"pyproject.toml" = [
    '^version = "{version}"',
    '^current_version = "{version}"',
]

This one worked perfect, thanks.

What I think is happening: The pattern starting with version = is greedy and matches both for line 7 and 44. Then there is nothing left for the pattern with current_version = to match against.

This indeed seems to be the case

Can you think of a way to fix this? The least we might be able to do is to generate a better error message.

A better error would already help a lot, do a check and see how many matches you find, is his more then one you can give a nice error message.