From a96ebf4ca55d97eab1a7ff9dd4169232875d229d Mon Sep 17 00:00:00 2001 From: Carl Mai Date: Mon, 20 Jul 2020 21:19:15 +0000 Subject: [PATCH 1/2] support glob keyword in configfile --- README.md | 6 +++++- bumpversion/cli.py | 11 ++++++++--- tests/test_cli.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4f6fbf3..9bad4fc 100644 --- a/README.md +++ b/README.md @@ -296,7 +296,11 @@ values = ### Configuration file -- File specific configuration -This configuration is in the section: `[bumpversion:file:…]` +This configuration is in the section: `[bumpversion:file:…]` or `[bumpversion:glob:…]` + +Both, `file:` and `glob:` are configured the same. Their difference is that +file will match file names directly like `requirements.txt`. While glob also +matches multiple files via wildcards like `**/pom.xml`. Note: The configuration file format requires each section header to be unique. If you want to process a certain file multiple times, diff --git a/bumpversion/cli.py b/bumpversion/cli.py index 67652eb..a903f3e 100644 --- a/bumpversion/cli.py +++ b/bumpversion/cli.py @@ -1,5 +1,6 @@ import argparse from datetime import datetime +import glob import io import itertools import logging @@ -49,7 +50,7 @@ # bumpversion:file ( suffix with spaces):value RE_DETECT_SECTION_TYPE = re.compile( r"^bumpversion:" - r"((?Pfile)(\s*\(\s*(?P[^\):]+)\)?)?|(?Ppart)):" + r"((?Pfile|glob)(\s*\(\s*(?P[^\):]+)\)?)?|(?Ppart)):" r"(?P.+)", ) @@ -353,8 +354,12 @@ def _load_configuration(config_file, explicit_config, defaults): if "replace" not in section_config: section_config["replace"] = defaults.get("replace", "{new_version}") - files.append(ConfiguredFile(filename, VersionConfig(**section_config))) - + version_config = VersionConfig(**section_config) + if section_type.get("file") == "glob": + for filename_glob in glob.glob(filename): + files.append(ConfiguredFile(filename_glob, version_config)) + else: + files.append(ConfiguredFile(filename, version_config)) return config, config_file_exists, config_newlines, part_configs, files diff --git a/tests/test_cli.py b/tests/test_cli.py index 9ee3b86..a99f128 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -292,6 +292,20 @@ def test_default_config_files(tmpdir, configfile): assert "0.10.3" == tmpdir.join("file2").read() +def test_glob_keyword(tmpdir, configfile): + tmpdir.join("file1.txt").write("0.9.34") + tmpdir.join("file2.txt").write("0.9.34") + tmpdir.join(configfile).write("""[bumpversion] +current_version: 0.9.34 +new_version: 0.9.35 +[bumpversion:glob:*.txt]""") + + tmpdir.chdir() + main(["patch"]) + assert "0.9.35" == tmpdir.join("file1.txt").read() + assert "0.9.35" == tmpdir.join("file2.txt").read() + + def test_file_keyword_with_suffix_is_accepted(tmpdir, configfile, file_keyword): tmpdir.join("file2").write("0.10.2") tmpdir.join(configfile).write( From 0bd8d8373b8b9bd383dcef4d54c59e8044e2edc6 Mon Sep 17 00:00:00 2001 From: Carl Mai Date: Fri, 7 Aug 2020 12:42:56 +0000 Subject: [PATCH 2/2] also allow recursive glob with ** --- bumpversion/cli.py | 2 +- tests/test_cli.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/bumpversion/cli.py b/bumpversion/cli.py index a903f3e..8825adf 100644 --- a/bumpversion/cli.py +++ b/bumpversion/cli.py @@ -356,7 +356,7 @@ def _load_configuration(config_file, explicit_config, defaults): version_config = VersionConfig(**section_config) if section_type.get("file") == "glob": - for filename_glob in glob.glob(filename): + for filename_glob in glob.glob(filename, recursive=True): files.append(ConfiguredFile(filename_glob, version_config)) else: files.append(ConfiguredFile(filename, version_config)) diff --git a/tests/test_cli.py b/tests/test_cli.py index a99f128..8a33b0d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -305,6 +305,22 @@ def test_glob_keyword(tmpdir, configfile): assert "0.9.35" == tmpdir.join("file1.txt").read() assert "0.9.35" == tmpdir.join("file2.txt").read() +def test_glob_keyword_recursive(tmpdir, configfile): + tmpdir.mkdir("subdir").mkdir("subdir2") + file1 = tmpdir.join("subdir").join("file1.txt") + file1.write("0.9.34") + file2 = tmpdir.join("subdir").join("subdir2").join("file2.txt") + file2.write("0.9.34") + tmpdir.join(configfile).write("""[bumpversion] +current_version: 0.9.34 +new_version: 0.9.35 +[bumpversion:glob:**/*.txt]""") + + tmpdir.chdir() + main(["patch"]) + assert "0.9.35" == file1.read() + assert "0.9.35" == file2.read() + def test_file_keyword_with_suffix_is_accepted(tmpdir, configfile, file_keyword): tmpdir.join("file2").write("0.10.2")