7
7
# For more info visit https://github.com/pulp/plugin_template
8
8
9
9
import itertools
10
+ import json
10
11
import os
11
12
import re
12
13
import tomllib
14
+ import urllib .request
15
+ from pathlib import Path
13
16
14
17
from git import GitCommandError , Repo
15
18
from packaging .version import parse as parse_version
16
19
20
+
21
+ PYPI_PROJECT = "pulp_deb"
22
+
17
23
# 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" ]
20
25
21
26
CHANGELOG_FILE = tc_settings .get ("filename" , "NEWS.rst" )
22
27
START_STRING = tc_settings .get (
35
40
# see help(re.split) for more info.
36
41
NAME_REGEX = r".*"
37
42
VERSION_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 } )"
39
44
DATE_REGEX = r"[0-9]{4}-[0-9]{2}-[0-9]{2}"
40
45
TITLE_REGEX = (
41
46
"("
@@ -75,6 +80,20 @@ def main():
75
80
branches .sort (key = lambda ref : parse_version (ref .remote_head ), reverse = True )
76
81
branches = [ref .name for ref in branches ]
77
82
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
+
78
97
with open (CHANGELOG_FILE , "r" ) as f :
79
98
main_changelog = f .read ()
80
99
preamble , main_changes = split_changelog (main_changelog )
@@ -95,9 +114,19 @@ def main():
95
114
if left [0 ] != right [0 ]:
96
115
main_changes .append (right )
97
116
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
+
98
127
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) ." )
101
130
with open (CHANGELOG_FILE , "w" ) as fp :
102
131
fp .write (preamble )
103
132
for change in main_changes :
0 commit comments