Skip to content

Commit 6820c51

Browse files
authored
Merge pull request winpython#1546 from stonebig/master
simplify diff.py using some copilote AI suggestions
2 parents 4ca83e7 + 04aebb4 commit 6820c51

File tree

2 files changed

+51
-192
lines changed

2 files changed

+51
-192
lines changed

diff.py

Lines changed: 50 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
# -*- coding: utf-8 -*-
22
#
3+
# WinPython diff.py script
34
# Copyright © 2013 Pierre Raybaut
5+
# Copyright © 2014-2025+ The Winpython development team https://github.com/winpython/
46
# Licensed under the terms of the MIT License
57
# (see winpython/__init__.py for details)
68

7-
"""
8-
WinPython diff script
9-
10-
Created on Tue Jan 29 11:56:54 2013
11-
"""
129

1310
import os
1411
from pathlib import Path
1512
import re
1613
import shutil
17-
18-
# Local imports
1914
from winpython import utils
2015

21-
22-
CHANGELOGS_DIR = str(Path(__file__).parent / "changelogs")
23-
assert Path(CHANGELOGS_DIR).is_dir()
16+
CHANGELOGS_DIR = Path(__file__).parent / "changelogs"
17+
assert CHANGELOGS_DIR.is_dir()
2418

2519

2620
class Package(object):
@@ -54,10 +48,7 @@ def to_wiki(self):
5448
return f" * [{self.name}]({self.url}) {self.version} ({self.description})\r\n"
5549

5650
def upgrade_wiki(self, other):
57-
# wheel replace '-' per '_' in package name
58-
assert (
59-
self.name.replace("-", "_").lower() == other.name.replace("-", "_").lower()
60-
)
51+
assert self.name.replace("-", "_").lower() == other.name.replace("-", "_").lower()
6152
return f" * [{self.name}]({self.url}) {other.version}{self.version} ({self.description})\r\n"
6253

6354

@@ -68,48 +59,34 @@ class PackageIndex(object):
6859
HEADER_LINE1 = "Name | Version | Description"
6960
HEADER_LINE2 = "-----|---------|------------"
7061

71-
def __init__(
72-
self,
73-
version,
74-
basedir=None,
75-
flavor="",
76-
architecture=64,
77-
):
62+
def __init__(self, version, basedir=None, flavor="", architecture=64):
7863
self.version = version
79-
self.other_packages = {}
80-
self.python_packages = {}
8164
self.flavor = flavor
8265
self.basedir = basedir
8366
self.architecture = architecture
67+
self.other_packages = {}
68+
self.python_packages = {}
8469
self.from_file(basedir)
8570

8671
def from_file(self, basedir):
87-
fname = str(
88-
Path(CHANGELOGS_DIR)
89-
/ f"WinPython{self.flavor}-{self.architecture}bit-{self.version}.md"
90-
)
91-
92-
try:
93-
with open(fname, "r", encoding = 'utf-8') as fdesc: # python3 doesn't like 'rb'
94-
text = fdesc.read()
95-
except:
96-
with open(fname, "r") as fdesc: # python3 doesn't like 'rb'
97-
text = fdesc.read()
72+
fname = CHANGELOGS_DIR / f"WinPython{self.flavor}-{self.architecture}bit-{self.version}.md"
73+
if not fname.exists():
74+
raise FileNotFoundError(f"Changelog file not found: {fname}")
75+
with open(fname, "r", encoding = 'utf-8') as fdesc:
76+
text = fdesc.read()
9877
self.from_text(text)
9978

10079
def from_text(self, text):
10180
version = re.match(self.WINPYTHON_PATTERN + self.flavor, text).groups()[0]
10281
assert version == self.version
103-
tools_flag = False
104-
python_flag = False
82+
tools_flag = python_flag = False
10583
for line in text.splitlines():
10684
if line:
10785
if line == self.TOOLS_LINE:
108-
tools_flag = True
86+
tools_flag, python_flag = True, False
10987
continue
11088
elif line == self.PYTHON_PACKAGES_LINE:
111-
tools_flag = False
112-
python_flag = True
89+
tools_flag, python_flag = False, True
11390
continue
11491
elif line in (
11592
self.HEADER_LINE1,
@@ -127,42 +104,24 @@ def from_text(self, text):
127104
self.python_packages[package.name] = package
128105

129106

130-
def diff_package_dicts(dict1_in, dict2_in):
131-
"""Return difference between package dict1 and package dict2"""
132-
text = ""
107+
def diff_package_dicts(old_packages, new_packages):
108+
"""Return difference between package old and package new"""
109+
133110
# wheel replace '-' per '_' in key
134-
dict1 = {}
135-
dict2 = {}
136-
for key in dict1_in:
137-
dict1[key.replace("-", "_").lower()] = dict1_in[key]
138-
for key in dict2_in:
139-
dict2[key.replace("-", "_").lower()] = dict2_in[key]
140-
set1, set2 = set(dict1.keys()), set(dict2.keys())
111+
old = {k.replace("-", "_").lower(): v for k, v in old_packages.items()}
112+
new = {k.replace("-", "_").lower(): v for k, v in new_packages.items()}
113+
text = ""
141114
# New packages
142-
new = sorted(set2 - set1)
143-
if new:
144-
text += "New packages:\r\n\r\n"
145-
for name in new:
146-
package = dict2[name]
147-
text += package.to_wiki()
148-
text += "\r\n"
115+
if new_keys := sorted(set(new) - set(old)):
116+
text += "New packages:\r\n\r\n" + "".join(new[k].to_wiki() for k in new_keys) + "\r\n"
117+
149118
# Upgraded packages
150-
upgraded_list = []
151-
for name in sorted(set1 & set2):
152-
package1 = dict1[name]
153-
package2 = dict2[name]
154-
if package1.version != package2.version:
155-
upgraded_list.append(package2.upgrade_wiki(package1))
156-
if upgraded_list:
157-
text += "Upgraded packages:\r\n\r\n" + f"{''.join(upgraded_list)}" + "\r\n"
119+
if upgraded := [new[k].upgrade_wiki(old[k]) for k in sorted(set(old) & set(new)) if old[k].version != new[k].version]:
120+
text += "Upgraded packages:\r\n\r\n" + f"{''.join(upgraded)}" + "\r\n"
121+
158122
# Removed packages
159-
removed = sorted(set1 - set2)
160-
if removed:
161-
text += "Removed packages:\r\n\r\n"
162-
for name in removed:
163-
package = dict1[name]
164-
text += package.to_wiki()
165-
text += "\r\n"
123+
if removed_keys := sorted(set(old) - set(new)):
124+
text += "Removed packages:\r\n\r\n" + "".join(old[k].to_wiki() for k in removed_keys) + "\r\n"
166125
return text
167126

168127

@@ -190,23 +149,13 @@ def find_closer_version(version1, basedir=None, flavor="", architecture=64):
190149
else:
191150
return version_below
192151

193-
def compare_package_indexes(
194-
version2,
195-
version1=None,
196-
basedir=None,
197-
flavor="",
198-
flavor1=None,
199-
architecture=64,
200-
):
152+
def compare_package_indexes(version2, version1=None, basedir=None, flavor="", flavor1=None,architecture=64):
201153
"""Compare two package index Wiki pages"""
202-
if version1 is None:
203-
version1 = find_closer_version(
204-
version2,
205-
basedir=basedir,
206-
flavor=flavor,
207-
architecture=architecture,
208-
)
209-
flavor1 = flavor1 if flavor1 is not None else flavor
154+
version1 = version1 if version1 else find_closer_version(version2, basedir, flavor, architecture)
155+
flavor1 = flavor1 if flavor1 else flavor
156+
pi1 = PackageIndex(version1, basedir, flavor1, architecture)
157+
pi2 = PackageIndex(version2, basedir, flavor, architecture)
158+
210159
text = "\r\n".join(
211160
[
212161
f"## History of changes for WinPython-{architecture}bit {version2 + flavor}",
@@ -218,18 +167,7 @@ def compare_package_indexes(
218167
"",
219168
]
220169
)
221-
pi1 = PackageIndex(
222-
version1,
223-
basedir=basedir,
224-
flavor=flavor1,
225-
architecture=architecture,
226-
)
227-
pi2 = PackageIndex(
228-
version2,
229-
basedir=basedir,
230-
flavor=flavor,
231-
architecture=architecture,
232-
)
170+
233171
tools_text = diff_package_dicts(pi1.other_packages, pi2.other_packages)
234172
if tools_text:
235173
text += PackageIndex.TOOLS_LINE + "\r\n\r\n" + tools_text
@@ -248,101 +186,22 @@ def _copy_all_changelogs(version, basedir, flavor="", architecture=64):
248186
% (flavor, architecture, basever),
249187
name,
250188
):
251-
shutil.copyfile(
252-
str(Path(CHANGELOGS_DIR) / name),
253-
str(Path(basedir) / f"bu{flavor}" / name),
254-
)
255-
256-
257-
def write_changelog(
258-
version2,
259-
version1=None,
260-
basedir=None,
261-
flavor="",
262-
architecture=64,
263-
):
264-
"""Write changelog between version1 and version2 of WinPython"""
265-
_copy_all_changelogs(
266-
version2,
267-
basedir,
268-
flavor=flavor,
269-
architecture=architecture,
270-
)
271-
print(
272-
"comparing_package_indexes",
273-
version2,
274-
basedir,
275-
flavor,
276-
architecture,
277-
)
278-
text = compare_package_indexes(
279-
version2,
280-
version1,
281-
basedir=basedir,
282-
flavor=flavor,
283-
architecture=architecture,
284-
)
285-
fname = str(
286-
Path(basedir)
287-
/ f"bu{flavor}"
288-
/ f"WinPython{flavor}-{architecture}bit-{version2}_History.md"
289-
)
189+
shutil.copyfile(CHANGELOGS_DIR / name, Path(basedir) / f"bu{flavor}" / name)
290190

291-
with open(fname, "w", encoding="utf-8-sig") as fdesc: # python 3 need
292-
fdesc.write(text)
293-
# Copy to winpython/changelogs
294-
shutil.copyfile(fname, str(Path(CHANGELOGS_DIR) / Path(fname).name))
295191

192+
def write_changelog(version2, version1=None, basedir=None, flavor="", architecture=64):
193+
"""Write changelog between version1 and version2 of WinPython"""
194+
_copy_all_changelogs(version2, basedir, flavor, architecture)
195+
print("comparing_package_indexes", version2, basedir, flavor, architecture)
196+
changelog_text = compare_package_indexes(version2, version1, basedir, flavor, architecture=architecture)
197+
output_file = Path(basedir) / f"bu{flavor}" / f"WinPython{flavor}-{architecture}bit-{version2}_History.md"
296198

297-
def test_parse_package_index_wiki(version, basedir=None, flavor="", architecture=64):
298-
"""Parse the package index Wiki page"""
299-
pi = PackageIndex(
300-
version,
301-
basedir=basedir,
302-
flavor=flavor,
303-
architecture=architecture,
304-
)
305-
utils.print_box(f"WinPython {pi.version}:")
306-
utils.print_box("Tools:")
307-
for package in pi.other_packages.values():
308-
print(package)
309-
print("")
310-
utils.print_box("Python packages:")
311-
for package in pi.python_packages.values():
312-
print(package)
313-
print("")
314-
315-
316-
def test_compare(basedir, version2, version1, architecture=64):
317-
print(
318-
compare_package_indexes(
319-
basedir,
320-
version2,
321-
version1,
322-
architecture=architecture,
323-
)
324-
)
199+
with open(output_file, "w", encoding="utf-8-sig") as fdesc:
200+
fdesc.write(changelog_text)
201+
# Copy to winpython/changelogs
202+
shutil.copyfile(output_file, CHANGELOGS_DIR / output_file.name)
325203

326204

327205
if __name__ == "__main__":
328-
print(
329-
compare_package_indexes(
330-
version2="3.7.4.0",
331-
version1="3.7.2.0",
332-
basedir=r"C:\WinP\bd37",
333-
flavor="Zero",
334-
flavor1="Zero",
335-
architecture=32,
336-
)
337-
)
338-
write_changelog(
339-
version2="3.7.4.0",
340-
version1="3.7.2.0",
341-
basedir=r"C:\WinP\bd37",
342-
flavor="Ps2",
343-
architecture=64,
344-
)
345-
# test_parse_package_index_wiki('2.7.3.3')
346-
# print(compare_package_indexes('2.7.3.3', '2.7.3.1'))
347-
# write_changelog('2.7.4.1', '2.7.4.0')
348-
# write_changelog('3.3.0.0beta2', '3.3.0.0beta1')
206+
print(compare_package_indexes("3.7.4.0", "3.7.2.0", "C:\WinP\bd37", "Zero", architecture=32))
207+
write_changelog("3.7.4.0", "3.7.2.0", r"C:\WinP\bd37", "Ps2", architecture=64)

winpython/wppm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def main(test=False):
252252
if test:
253253
sbdir = Path(__file__).parents[0].parent.parent.parent / "sandbox"
254254
tmpdir = sbdir / "tobedeleted"
255-
fname = sbdir / "VTK-5.10.0-Qt-4.7.4.win32-py2.7.exe")
255+
fname = sbdir / "VTK-5.10.0-Qt-4.7.4.win32-py2.7.exe"
256256
print(Package(str(fname)))
257257
sys.exit()
258258
target = Path(utils.BASE_DIR) / "build" / "winpython-2.7.3" / "python-2.7.3"

0 commit comments

Comments
 (0)