77# For more info visit https://github.com/pulp/plugin_template
88
99import itertools
10+ import json
1011import os
1112import re
1213import tomllib
14+ import urllib .request
15+ from pathlib import Path
1316
1417from git import GitCommandError , Repo
1518from packaging .version import parse as parse_version
1619
20+
21+ PYPI_PROJECT = "pulp_python"
22+
1723# Read Towncrier settings
18- with open ("pyproject.toml" , "rb" ) as fp :
19- tc_settings = tomllib .load (fp )["tool" ]["towncrier" ]
24+ tc_settings = tomllib .loads (Path ("pyproject.toml" ).read_text ())["tool" ]["towncrier" ]
2025
2126CHANGELOG_FILE = tc_settings .get ("filename" , "NEWS.rst" )
2227START_STRING = tc_settings .get (
3540# see help(re.split) for more info.
3641NAME_REGEX = r".*"
3742VERSION_REGEX = r"[0-9]+\.[0-9]+\.[0-9][0-9ab]*"
38- VERSION_CAPTURE_REGEX = rf"({ VERSION_REGEX } )"
43+ VERSION_CAPTURE_REGEX = rf"(?:YANKED )?( { VERSION_REGEX } )"
3944DATE_REGEX = r"[0-9]{4}-[0-9]{2}-[0-9]{2}"
4045TITLE_REGEX = (
4146 "("
@@ -75,6 +80,20 @@ def main():
7580 branches .sort (key = lambda ref : parse_version (ref .remote_head ), reverse = True )
7681 branches = [ref .name for ref in branches ]
7782
83+ changed = False
84+
85+ try :
86+ response = urllib .request .urlopen (f"https://pypi.org/pypi/{ PYPI_PROJECT } /json" )
87+ pypi_record = json .loads (response .read ())
88+ yanked_versions = {
89+ parse_version (version ): release [0 ]["yanked_reason" ]
90+ for version , release in pypi_record ["releases" ].items ()
91+ if release [0 ]["yanked" ] is True
92+ }
93+ except Exception :
94+ # If something failed, just don't mark anything as yanked.
95+ yanked_versions = {}
96+
7897 with open (CHANGELOG_FILE , "r" ) as f :
7998 main_changelog = f .read ()
8099 preamble , main_changes = split_changelog (main_changelog )
@@ -95,9 +114,19 @@ def main():
95114 if left [0 ] != right [0 ]:
96115 main_changes .append (right )
97116
117+ if yanked_versions :
118+ for change in main_changes :
119+ if change [0 ] in yanked_versions and "YANKED" not in change [1 ].split ("\n " )[0 ]:
120+ reason = yanked_versions [change [0 ]]
121+ version = str (change [0 ])
122+ change [1 ] = change [1 ].replace (version , "YANKED " + version , count = 1 )
123+ if reason :
124+ change [1 ] = change [1 ].replace ("\n " , f"\n \n Yank reason: { reason } \n " , count = 1 )
125+ changed = True
126+
98127 new_length = len (main_changes )
99- if old_length < new_length :
100- print (f"{ new_length - old_length } new versions have been added." )
128+ if old_length < new_length or changed :
129+ print (f"{ new_length - old_length } new versions have been added (or something has changed) ." )
101130 with open (CHANGELOG_FILE , "w" ) as fp :
102131 fp .write (preamble )
103132 for change in main_changes :
0 commit comments