From 2d842c47f66b751435c4c2a3e08876447a8111ed Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Mon, 18 Nov 2024 18:17:56 +0100 Subject: [PATCH] Move bumpversion cfg into github template --- plugin-template | 32 +++++++------- requirements.txt | 1 + templates/bootstrap/.bumpversion.cfg.j2 | 20 --------- .../bootstrap/plugin_name/app/__init__.py.j2 | 2 +- templates/bootstrap/setup.py.j2 | 2 +- templates/github/pyproject.toml.tool.j2 | 42 +++++++++++++++++++ test_requirements.txt | 4 +- utils.py | 21 +++++++++- 8 files changed, 84 insertions(+), 40 deletions(-) delete mode 100644 templates/bootstrap/.bumpversion.cfg.j2 create mode 100644 templates/github/pyproject.toml.tool.j2 diff --git a/plugin-template b/plugin-template index 845840eb..8d58b327 100755 --- a/plugin-template +++ b/plugin-template @@ -80,6 +80,7 @@ DEFAULT_SETTINGS = { DEPRECATED_FILES = { "github": [ + ".bumpversion.cfg", ".ci/assets/bindings/.gitkeep", ".ci/scripts/changelog.py", ".ci/scripts/calc_deps_lowerbounds.py", @@ -308,8 +309,8 @@ def main(): return 2 if args.latest_release_branch: - write_new_config = True config["latest_release_branch"] = str(args.latest_release_branch) + write_new_config = True # Config key is used by the template_config.yml.j2 template to dump # the config. (note: uses .copy() to avoid a self reference) @@ -377,11 +378,6 @@ def append_releasing_to_manifest(plugin_root): manifest_file.write_text(manifest_text + "exclude releasing.md\n") -def to_nice_yaml(data): - """Implement a filter for Jinja 2 templates to render human readable YAML.""" - return yaml.dump(data, indent=2, allow_unicode=True, default_flow_style=False) - - def write_template_section(config, name, plugin_root_dir, verbose=False): """ Template or copy all files for the section. @@ -402,7 +398,7 @@ def write_template_section(config, name, plugin_root_dir, verbose=False): env.filters["caps"] = utils.to_caps env.filters["dash"] = utils.to_dash env.filters["snake"] = utils.to_snake - env.filters["to_yaml"] = to_nice_yaml + env.filters["to_yaml"] = utils.to_nice_yaml env.filters["shquote"] = shlex.quote files_templated = 0 @@ -450,12 +446,20 @@ def write_template_section(config, name, plugin_root_dir, verbose=False): if relative_path.endswith(".j2"): template = env.get_template(relative_path) destination = destination_relative_path[: -len(".j2")] - write_template_to_file( - template, - plugin_root_dir, - destination, - template_vars, - ) + if destination.startswith("pyproject.toml."): + utils.merge_toml( + template, + plugin_root_dir, + destination, + template_vars, + ) + else: + template_to_file( + template, + plugin_root_dir, + destination, + template_vars, + ) files_templated += 1 if verbose: print(f"Templated file: {relative_path}") @@ -487,7 +491,7 @@ def generate_relative_path_set(root_dir): return applicable_paths -def write_template_to_file(template, plugin_root_dir, relative_path, config): +def template_to_file(template, plugin_root_dir, relative_path, config): """ Render template with values from the config and write it to the target plugin directory. """ diff --git a/requirements.txt b/requirements.txt index abb4c2fe..abcb9934 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ jinja2 pyyaml requests~=2.32.3 requests_cache +tomlkit diff --git a/templates/bootstrap/.bumpversion.cfg.j2 b/templates/bootstrap/.bumpversion.cfg.j2 deleted file mode 100644 index 267debd0..00000000 --- a/templates/bootstrap/.bumpversion.cfg.j2 +++ /dev/null @@ -1,20 +0,0 @@ -[bumpversion] -current_version = 0.1.0a1.dev -commit = False -tag = False -parse = (?P\d+)\.(?P\d+)\.(0a)?(?P\d+)(\.(?P[a-z]+))? -serialize = - {major}.{minor}.0a{patch}.{release} - {major}.{minor}.0a{patch} - -[bumpversion:part:release] -optional_value = prod -first_value = dev -values = - dev - prod - -[bumpversion:file:./{{ plugin_name | snake }}/app/__init__.py] - -[bumpversion:file:./setup.py] - diff --git a/templates/bootstrap/plugin_name/app/__init__.py.j2 b/templates/bootstrap/plugin_name/app/__init__.py.j2 index d171e7f0..6083139c 100644 --- a/templates/bootstrap/plugin_name/app/__init__.py.j2 +++ b/templates/bootstrap/plugin_name/app/__init__.py.j2 @@ -6,6 +6,6 @@ class {{ plugin_name | camel }}PluginAppConfig(PulpPluginAppConfig): name = "{{ plugin_name | snake }}.app" label = "{{ plugin_app_label }}" - version = "0.1.0a1.dev" + version = "{{ current_version }}" python_package_name = "{{ plugin_name }}" domain_compatible = True diff --git a/templates/bootstrap/setup.py.j2 b/templates/bootstrap/setup.py.j2 index 105748d8..673d696d 100755 --- a/templates/bootstrap/setup.py.j2 +++ b/templates/bootstrap/setup.py.j2 @@ -7,7 +7,7 @@ with open("requirements.txt") as requirements: setup( name="{{ plugin_name | dash }}", - version="0.1.0a1.dev", + version="{{ current_version }}", description="{{ plugin_name | dash }} plugin for the Pulp Project", long_description="{{ plugin_name | dash }} plugin for the Pulp Project", long_description_content_type="text/markdown", diff --git a/templates/github/pyproject.toml.tool.j2 b/templates/github/pyproject.toml.tool.j2 new file mode 100644 index 00000000..52b28f5b --- /dev/null +++ b/templates/github/pyproject.toml.tool.j2 @@ -0,0 +1,42 @@ +[tool.bumpversion] +# This section is managed by the plugin template. Do not edit manually. + +current_version = "{{ current_version }}" +commit = false +tag = false +parse = "(?P\\d+)\\.(?P\\d+)\\.(?P\\d+)(\\.(?P[a-z]+))?" +serialize = [ + "{major}.{minor}.{patch}.{release}", + "{major}.{minor}.{patch}", +] + +[tool.bumpversion.parts.release] +# This section is managed by the plugin template. Do not edit manually. + +optional_value = "prod" +values = [ + "dev", + "prod", +] +{%- for plugin in plugins %} + +[[tool.bumpversion.files]] +# This section is managed by the plugin template. Do not edit manually. + +filename = "./{{ plugin.name }}/app/{% if plugin.name == "pulpcore" %}apps{% else %}__init__{% endif %}.py" +search = "version = \"{current_version}\"" +replace = "version = \"{new_version}\"" +{%- endfor %} +{%- if setup_py %} + +[[tool.bumpversion.files]] +filename = "./setup.py" +{%- else %} + +[[tool.bumpversion.files]] +# This section is managed by the plugin template. Do not edit manually. + +filename = "./pyproject.toml" +search = "version = \"{current_version}\"" +replace = "version = \"{new_version}\"" +{%- endif %} diff --git a/test_requirements.txt b/test_requirements.txt index 5b81b903..1a393a98 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -1,9 +1,7 @@ +-r requirements.txt black check-manifest flake8 -jinja2 mock git+https://github.com/pulp/pulp-smash.git#egg=pulp-smash pytest -pyyaml -requests_cache diff --git a/utils.py b/utils.py index 57429acc..222dc398 100644 --- a/utils.py +++ b/utils.py @@ -3,6 +3,7 @@ import pathlib import re import requests_cache +import tomlkit import tomllib import yaml @@ -21,7 +22,7 @@ def current_version(plugin_root_dir): current_version = line[18:].strip() break except Exception: - current_version = "0.1.0a1.dev" + current_version = "0.0.0.dev" return current_version @@ -64,6 +65,24 @@ def to_snake(name): return name.replace("-", "_") +def to_nice_yaml(data): + """Implement a filter for Jinja 2 templates to render human readable YAML.""" + return yaml.dump(data, indent=2, allow_unicode=True, default_flow_style=False) + + +def merge_toml(template, plugin_root_dir, relative_path, config): + basename, merge_key = relative_path.split(".toml.", maxsplit=1) + data = tomlkit.loads(template.render(**config)) + if merge_key in data: + path = pathlib.Path(plugin_root_dir) / f"{basename}.toml" + old_toml = tomlkit.load(path.open()) + if merge_key not in old_toml: + old_toml[merge_key] = data[merge_key] + else: + old_toml[merge_key].update(data[merge_key]) + tomlkit.dump(old_toml, path.open("w")) + + def get_pulpdocs_members() -> list[str]: """ Get repositories which are members of the Pulp managed documentation.