forked from winpython/winpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.py
349 lines (313 loc) · 10.2 KB
/
diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# -*- coding: utf-8 -*-
#
# Copyright © 2013 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see winpython/__init__.py for details)
"""
WinPython diff script
Created on Tue Jan 29 11:56:54 2013
"""
import os
from pathlib import Path
import re
import shutil
# Local imports
from winpython import utils
CHANGELOGS_DIR = str(Path(__file__).parent / "changelogs")
assert Path(CHANGELOGS_DIR).is_dir()
class Package(object):
# SourceForge Wiki syntax:
PATTERN = r"\[([a-zA-Z\-\:\/\.\_0-9]*)\]\(([^\]\ ]*)\) \| ([^\|]*) \| ([^\|]*)"
# Google Code Wiki syntax:
PATTERN_OLD = r"\[([a-zA-Z\-\:\/\.\_0-9]*) ([^\]\ ]*)\] \| ([^\|]*) \| ([^\|]*)"
def __init__(self):
self.name = None
self.version = None
self.description = None
self.url = None
def __str__(self):
text = f"{self.name} {self.version}"
text += f"\r\n{self.description}\r\nWebsite: {self.url}"
return text
def from_text(self, text):
try:
self.url, self.name, self.version, self.description = re.match(
self.PATTERN_OLD, text
).groups()
except AttributeError:
self.name, self.url, self.version, self.description = re.match(
self.PATTERN, text
).groups()
def to_wiki(self):
return f" * [{self.name}]({self.url}) {self.version} ({self.description})\r\n"
def upgrade_wiki(self, other):
# wheel replace '-' per '_' in package name
assert (
self.name.replace("-", "_").lower() == other.name.replace("-", "_").lower()
)
return f" * [{self.name}]({self.url}) {other.version} → {self.version} ({self.description})\r\n"
class PackageIndex(object):
WINPYTHON_PATTERN = r"\#\# WinPython\-*[0-9b-t]* ([0-9\.a-zA-Z]*)"
TOOLS_LINE = "### Tools"
PYTHON_PACKAGES_LINE = "### Python packages"
HEADER_LINE1 = "Name | Version | Description"
HEADER_LINE2 = "-----|---------|------------"
def __init__(
self,
version,
basedir=None,
flavor="",
architecture=64,
):
self.version = version
self.other_packages = {}
self.python_packages = {}
self.flavor = flavor
self.basedir = basedir
self.architecture = architecture
self.from_file(basedir)
def from_file(self, basedir):
fname = str(
Path(CHANGELOGS_DIR)
/ f"WinPython{self.flavor}-{self.architecture}bit-{self.version}.md"
)
try:
with open(fname, "r", encoding = 'utf-8') as fdesc: # python3 doesn't like 'rb'
text = fdesc.read()
except:
with open(fname, "r") as fdesc: # python3 doesn't like 'rb'
text = fdesc.read()
self.from_text(text)
def from_text(self, text):
version = re.match(self.WINPYTHON_PATTERN + self.flavor, text).groups()[0]
assert version == self.version
tools_flag = False
python_flag = False
for line in text.splitlines():
if line:
if line == self.TOOLS_LINE:
tools_flag = True
continue
elif line == self.PYTHON_PACKAGES_LINE:
tools_flag = False
python_flag = True
continue
elif line in (
self.HEADER_LINE1,
self.HEADER_LINE2,
"<details>",
"</details>",
):
continue
if tools_flag or python_flag:
package = Package()
package.from_text(line)
if tools_flag:
self.other_packages[package.name] = package
else:
self.python_packages[package.name] = package
def diff_package_dicts(dict1_in, dict2_in):
"""Return difference between package dict1 and package dict2"""
text = ""
# wheel replace '-' per '_' in key
dict1 = {}
dict2 = {}
for key in dict1_in:
dict1[key.replace("-", "_").lower()] = dict1_in[key]
for key in dict2_in:
dict2[key.replace("-", "_").lower()] = dict2_in[key]
set1, set2 = set(dict1.keys()), set(dict2.keys())
# New packages
new = sorted(set2 - set1)
if new:
text += "New packages:\r\n\r\n"
for name in new:
package = dict2[name]
text += package.to_wiki()
text += "\r\n"
# Upgraded packages
upgraded_list = []
for name in sorted(set1 & set2):
package1 = dict1[name]
package2 = dict2[name]
if package1.version != package2.version:
upgraded_list.append(package2.upgrade_wiki(package1))
if upgraded_list:
text += "Upgraded packages:\r\n\r\n" + f"{''.join(upgraded_list)}" + "\r\n"
# Removed packages
removed = sorted(set1 - set2)
if removed:
text += "Removed packages:\r\n\r\n"
for name in removed:
package = dict1[name]
text += package.to_wiki()
text += "\r\n"
return text
def find_closer_version(version1, basedir=None, flavor="", architecture=64):
"""Find version which is the closest to `version`"""
builddir = str(Path(basedir) / f"bu{flavor}")
func = lambda name: re.match(
r"WinPython%s-%sbit-([0-9\.]*)\.(txt|md)" % (flavor, architecture),
name,
)
versions = [func(name).groups()[0] for name in os.listdir(builddir) if func(name)]
# versions:['3.10.0.1', '3.10.10.0', '3.10.2.0'.... '3.10.8.1', '3.10.9.0']
try:
index = versions.index(version1)
except ValueError:
raise ValueError(f"Unknown version {version1}")
from packaging import version
version_below = '0.0.0.0'
for v in versions:
if version.parse(v) > version.parse(version_below) and version.parse(v)<version.parse(version1):
version_below = v
if version_below =='0.0.0.0':
return version1
else:
return version_below
def compare_package_indexes(
version2,
version1=None,
basedir=None,
flavor="",
flavor1=None,
architecture=64,
):
"""Compare two package index Wiki pages"""
if version1 is None:
version1 = find_closer_version(
version2,
basedir=basedir,
flavor=flavor,
architecture=architecture,
)
flavor1 = flavor1 if flavor1 is not None else flavor
text = "\r\n".join(
[
f"## History of changes for WinPython-{architecture}bit {version2 + flavor}",
"",
f"The following changes were made to WinPython-{architecture}bit"
f" distribution since version {version1 + flavor1}.",
"",
"<details>",
"",
]
)
pi1 = PackageIndex(
version1,
basedir=basedir,
flavor=flavor1,
architecture=architecture,
)
pi2 = PackageIndex(
version2,
basedir=basedir,
flavor=flavor,
architecture=architecture,
)
tools_text = diff_package_dicts(pi1.other_packages, pi2.other_packages)
if tools_text:
text += PackageIndex.TOOLS_LINE + "\r\n\r\n" + tools_text
py_text = diff_package_dicts(pi1.python_packages, pi2.python_packages)
if py_text:
text += PackageIndex.PYTHON_PACKAGES_LINE + "\r\n\r\n" + py_text
text += "\r\n</details>\r\n* * *\r\n"
return text
def _copy_all_changelogs(version, basedir, flavor="", architecture=64):
basever = ".".join(version.split(".")[:2])
for name in os.listdir(CHANGELOGS_DIR):
if re.match(
r"WinPython%s-%sbit-%s([0-9\.]*)\.(txt|md)"
% (flavor, architecture, basever),
name,
):
shutil.copyfile(
str(Path(CHANGELOGS_DIR) / name),
str(Path(basedir) / f"bu{flavor}" / name),
)
def write_changelog(
version2,
version1=None,
basedir=None,
flavor="",
release_level="",
architecture=64,
):
"""Write changelog between version1 and version2 of WinPython"""
_copy_all_changelogs(
version2,
basedir,
flavor=flavor,
architecture=architecture,
)
print(
"comparing_package_indexes",
version2,
basedir,
flavor,
architecture,
)
text = compare_package_indexes(
version2,
version1,
basedir=basedir,
flavor=flavor,
architecture=architecture,
)
fname = str(
Path(basedir)
/ f"bu{flavor}"
/ f"WinPython{flavor}-{architecture}bit-{version2}_History.md"
)
with open(fname, "w", encoding="utf-8-sig") as fdesc: # python 3 need
fdesc.write(text)
# Copy to winpython/changelogs
shutil.copyfile(fname, str(Path(CHANGELOGS_DIR) / Path(fname).name))
def test_parse_package_index_wiki(version, basedir=None, flavor="", architecture=64):
"""Parse the package index Wiki page"""
pi = PackageIndex(
version,
basedir=basedir,
flavor=flavor,
architecture=architecture,
)
utils.print_box(f"WinPython {pi.version}:")
utils.print_box("Tools:")
for package in pi.other_packages.values():
print(package)
print("")
utils.print_box("Python packages:")
for package in pi.python_packages.values():
print(package)
print("")
def test_compare(basedir, version2, version1, architecture=64):
print(
compare_package_indexes(
basedir,
version2,
version1,
architecture=architecture,
)
)
if __name__ == "__main__":
print(
compare_package_indexes(
version2="3.7.4.0",
version1="3.7.2.0",
basedir=r"C:\WinP\bd37",
flavor="Zero",
flavor1="Zero",
architecture=32,
)
)
write_changelog(
version2="3.7.4.0",
version1="3.7.2.0",
basedir=r"C:\WinP\bd37",
flavor="Ps2",
architecture=64,
)
# test_parse_package_index_wiki('2.7.3.3')
# print(compare_package_indexes('2.7.3.3', '2.7.3.1'))
# write_changelog('2.7.4.1', '2.7.4.0')
# write_changelog('3.3.0.0beta2', '3.3.0.0beta1')