33Check for PolicyEngine package updates and generate PR summary.
44
55This script checks PyPI for newer versions of PolicyEngine packages,
6- updates setup.py if needed, and generates changelog summaries.
6+ updates pyproject.toml if needed, and generates changelog summaries.
77"""
88
99import os
1010import re
1111import sys
12+ import tomllib
13+ from pathlib import Path
1214
1315import requests
1416
@@ -24,14 +26,21 @@ def parse_version(version_str):
2426 return tuple (map (int , version_str .split ("." )))
2527
2628
27- def get_current_versions (setup_content ):
28- """Extract current package versions from setup.py content."""
29+ def get_current_versions (pyproject_content ):
30+ """Extract current package versions from pyproject.toml content."""
2931 current_versions = {}
32+ dependencies = tomllib .loads (pyproject_content )["project" ].get (
33+ "dependencies" , []
34+ )
3035 for pkg in PACKAGES :
31- pattern = rf'{ pkg .replace ("_" , "[-_]" )} ==([0-9]+\.[0-9]+\.[0-9]+)'
32- match = re .search (pattern , setup_content )
33- if match :
34- current_versions [pkg ] = match .group (1 )
36+ package_names = (pkg , pkg .replace ("_" , "-" ))
37+ for dependency in dependencies :
38+ for package_name in package_names :
39+ if dependency .startswith (f"{ package_name } ==" ):
40+ current_versions [pkg ] = dependency .split ("==" , 1 )[1 ]
41+ break
42+ if pkg in current_versions :
43+ break
3544 return current_versions
3645
3746
@@ -59,12 +68,19 @@ def find_updates(current_versions, latest_versions):
5968 return updates
6069
6170
62- def update_setup_content ( setup_content , updates ):
63- """Update setup.py content with new versions."""
64- new_content = setup_content
71+ def update_pyproject_content ( pyproject_content , updates ):
72+ """Update pyproject.toml content with new versions."""
73+ new_content = pyproject_content
6574 for pkg , versions in updates .items ():
66- pattern = rf'({ pkg .replace ("_" , "[-_]" )} ==)[0-9]+\.[0-9]+\.[0-9]+'
67- new_content = re .sub (pattern , rf'\g<1>{ versions ["new" ]} ' , new_content )
75+ pattern = (
76+ rf'("{ pkg .replace ("_" , "[-_]" )} ==)'
77+ rf'{ re .escape (versions ["old" ])} (")'
78+ )
79+ new_content = re .sub (
80+ pattern ,
81+ rf"\g<1>{ versions ['new' ]} \g<2>" ,
82+ new_content ,
83+ )
6884 return new_content
6985
7086
@@ -214,14 +230,10 @@ def generate_summary(updates):
214230 return "\n \n " .join (summary_parts )
215231
216232
217- def generate_changelog_entry (updates ):
218- """Generate changelog entry for this repo."""
233+ def generate_changelog_fragment (updates ):
234+ """Generate towncrier changelog fragment content for this repo."""
219235 new_version = updates ["policyengine_us" ]["new" ]
220- return f"""- bump: patch
221- changes:
222- changed:
223- - Update PolicyEngine US to { new_version }
224- """
236+ return f"Update PolicyEngine US to { new_version } .\n "
225237
226238
227239def write_github_output (key , value ):
@@ -234,11 +246,11 @@ def write_github_output(key, value):
234246
235247def main ():
236248 """Main entry point for the script."""
237- # Read current versions from setup.py
238- with open ("setup.py " , "r" ) as f :
239- setup_content = f .read ()
249+ # Read current versions from pyproject.toml
250+ with open ("pyproject.toml " , "r" ) as f :
251+ pyproject_content = f .read ()
240252
241- current_versions = get_current_versions (setup_content )
253+ current_versions = get_current_versions (pyproject_content )
242254 print (f"Current versions: { current_versions } " )
243255
244256 # Get latest versions from PyPI
@@ -255,20 +267,24 @@ def main():
255267
256268 print (f"Updates available: { updates } " )
257269
258- # Update setup.py
259- new_setup_content = update_setup_content (setup_content , updates )
260- with open ("setup.py" , "w" ) as f :
261- f .write (new_setup_content )
270+ # Update pyproject.toml
271+ new_pyproject_content = update_pyproject_content (
272+ pyproject_content , updates
273+ )
274+ with open ("pyproject.toml" , "w" ) as f :
275+ f .write (new_pyproject_content )
262276
263277 # Generate and save PR summary
264278 full_summary = generate_summary (updates )
265279 with open ("pr_summary.md" , "w" ) as f :
266280 f .write (full_summary )
267281
268- # Create changelog entry
269- changelog_entry = generate_changelog_entry (updates )
270- with open ("changelog_entry.yaml" , "w" ) as f :
271- f .write (changelog_entry )
282+ # Create changelog fragment
283+ changelog_dir = Path ("changelog.d" )
284+ changelog_dir .mkdir (exist_ok = True )
285+ new_version = updates ["policyengine_us" ]["new" ]
286+ fragment_path = changelog_dir / f"policyengine-us-{ new_version } .changed.md"
287+ fragment_path .write_text (generate_changelog_fragment (updates ))
272288
273289 # Set outputs
274290 write_github_output ("has_updates" , "true" )
0 commit comments