11#!/usr/bin/env python
22
3- # WARNING: DO NOT EDIT!
4- #
5- # This file was generated by plugin_template, and is managed by it. Please use
6- # './plugin-template --github pulp_python' to update this file.
7- #
8- # For more info visit https://github.com/pulp/plugin_template
9-
103import argparse
114import re
125import os
6+ import tomllib
137import yaml
8+ from pathlib import Path
149from tempfile import TemporaryDirectory
1510from packaging .version import Version
1611from git import Repo
1712
18- UPSTREAM_REMOTE = "https://github.com/pulp/pulp_python.git"
19- DEFAULT_BRANCH = "main"
2013RELEASE_BRANCH_REGEX = r"^([0-9]+)\.([0-9]+)$"
2114Y_CHANGELOG_EXTS = [".feature" , ".removal" , ".deprecation" ]
2215Z_CHANGELOG_EXTS = [".bugfix" , ".doc" , ".misc" ]
2316
2417
25- def main ():
18+ def options ():
2619 """Check which branches need a release."""
2720 parser = argparse .ArgumentParser ()
2821 parser .add_argument (
@@ -32,17 +25,57 @@ def main():
3225 "'supported'. Defaults to 'supported', see `supported_release_branches` in "
3326 "`plugin_template.yml`." ,
3427 )
35- opts = parser .parse_args ()
28+ return parser .parse_args ()
29+
30+
31+ def template_config ():
32+ # Assume this script lies in .ci/scripts
33+ path = Path (__file__ ).absolute ().parent .parent .parent / "template_config.yml"
34+ return yaml .safe_load (path .read_text ())
35+
3636
37+ def current_version (repo , commitish ):
38+ try :
39+ pyproject_toml = tomllib .loads (repo .git .show (f"{ commitish } :pyproject.toml" ))
40+ current_version = pyproject_toml ["project" ]["version" ]
41+ except Exception :
42+ current_version = repo .git .grep (
43+ "current_version" , commitish , "--" , ".bumpversion.cfg"
44+ ).split ("=" )[- 1 ]
45+ return Version (current_version )
46+
47+
48+ def check_pyproject_dependencies (repo , from_commit , to_commit ):
49+ try :
50+ old_pyproject = tomllib .load (repo .show ("{from_commit}:pyproject.toml" ))
51+ old_dependencies = set (old_pyproject ["project" ]["dependencies" ])
52+ new_pyproject = tomllib .load (repo .show ("{to_commit}:pyproject.toml" ))
53+ new_dependencies = set (new_pyproject ["project" ]["dependencies" ])
54+ if old_dependencies != new_dependencies :
55+ return ["dependencies" ]
56+ else :
57+ return []
58+ except Exception as e :
59+ print (f"WARNING: Comparing the dependencies in pyproject.toml failed. ({ e } )" )
60+ # Gathering more details failed.
61+ return ["pyproject.toml changed somehow (PLEASE check if dependencies are affected)." ]
62+
63+
64+ def main (options , template_config ):
3765 with TemporaryDirectory () as d :
3866 # Clone from upstream to ensure we have updated branches & main
67+ GITHUB_ORG = template_config ["github_org" ]
68+ PLUGIN_NAME = template_config ["plugin_name" ]
69+ UPSTREAM_REMOTE = f"https://github.com/{ GITHUB_ORG } /{ PLUGIN_NAME } .git"
70+ DEFAULT_BRANCH = template_config ["plugin_default_branch" ]
71+
3972 repo = Repo .clone_from (UPSTREAM_REMOTE , d , filter = "blob:none" )
4073 heads = [h .split ("/" )[- 1 ] for h in repo .git .ls_remote ("--heads" ).split ("\n " )]
4174 available_branches = [h for h in heads if re .search (RELEASE_BRANCH_REGEX , h )]
4275 available_branches .sort (key = lambda ver : Version (ver ))
4376 available_branches .append (DEFAULT_BRANCH )
4477
45- branches = opts .branches
78+ branches = options .branches
4679 if branches == "supported" :
4780 with open (f"{ d } /template_config.yml" , mode = "r" ) as f :
4881 tc = yaml .safe_load (f )
@@ -90,9 +123,7 @@ def main():
90123 f"{ last_tag } " , f"origin/{ branch } " , "--name-only" , "--" , "pyproject.toml"
91124 )
92125 if pyproject_diff :
93- reasons .append (
94- "pyproject.toml changed (PLEASE check if dependencies are affected."
95- )
126+ reasons .extend (check_pyproject_dependencies (repo , last_tag , f"origin/{ branch } " ))
96127
97128 if reasons :
98129 curr_version = Version (last_tag )
@@ -113,15 +144,10 @@ def main():
113144 for change in changes .split ("\n " ):
114145 _ , ext = os .path .splitext (change )
115146 if ext in Y_CHANGELOG_EXTS :
116- # We don't put Y release bumps in the commit message, check file instead
117- # The 'current_version' is always the next version to release
118- next_version = repo .git .grep (
119- "current_version" , DEFAULT_BRANCH , "--" , ".bumpversion.cfg"
120- ).split ("=" )[- 1 ]
121- next_version = Version (next_version )
122- print (
123- f"A new Y-release is needed! New Version: { next_version .base_version } "
124- )
147+ # We don't put Y release bumps in the commit message, check file instead.
148+ # The 'current_version' is always the dev of the next version to release.
149+ next_version = current_version (repo , DEFAULT_BRANCH ).base_version
150+ print (f"A new Y-release is needed! New Version: { next_version } " )
125151 releases .append (next_version )
126152 break
127153
@@ -130,4 +156,4 @@ def main():
130156
131157
132158if __name__ == "__main__" :
133- main ()
159+ main (options (), template_config () )
0 commit comments