-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.py
229 lines (192 loc) · 8.07 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
# -*- 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
"""
from __future__ import print_function, with_statement
import os
import os.path as osp
import re
import shutil
# Local imports
from winpython import utils
CHANGELOGS_DIR = osp.join(osp.dirname(__file__), 'changelogs')
assert osp.isdir(CHANGELOGS_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 = "%s %s" % (self.name, self.version)
text += "\r\n%s\r\nWebsite: %s" % (self.description, 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 " * [%s](%s) %s (%s)\r\n" % (self.name, self.url,
self.version, self.description)
def upgrade_wiki(self, other):
assert self.name == other.name
return " * [%s](%s) %s → %s (%s)\r\n" % (self.name, self.url,
other.version, self.version, self.description)
def get_basedir(version, rootdir=None):
"""Return basedir from WinPython version"""
rootdir = rootdir if rootdir is not None else utils.ROOT_DIR
assert rootdir is not None, "The *rootdir* directory must be specified"
return osp.join(rootdir, 'basedir%s' % version[::2][:2])
class PackageIndex(object):
WINPYTHON_PATTERN = r'\#\# WinPython ([0-9\.a-zA-Z]*)'
TOOLS_LINE = '### Tools'
PYTHON_PACKAGES_LINE = '### Python packages'
HEADER_LINE1 = 'Name | Version | Description'
HEADER_LINE2 = '-----|---------|------------'
def __init__(self, version, rootdir=None):
self.version = version
self.other_packages = {}
self.python_packages = {}
basedir = get_basedir(version, rootdir=rootdir)
self.from_file(basedir)
def from_file(self, basedir):
fname = osp.join(basedir, 'build', 'WinPython-%s.txt' % self.version)
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, 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):
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, dict2):
"""Return difference between package dict1 and package dict2"""
text = ""
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%s\r\n" % "".join(upgraded_list)
# 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, rootdir=None):
"""Find version which is the closest to `version`"""
builddir = osp.join(get_basedir(version1, rootdir), 'build')
func = lambda name: re.match(r'WinPython-([0-9\.]*)\.txt', name)
versions = [func(name).groups()[0]
for name in os.listdir(builddir) if func(name)]
try:
index = versions.index(version1)
except ValueError:
raise ValueError("Unknown version %s" % version1)
if index == 0:
raise ValueError("No version prior to %s" % version1)
return versions[index-1]
def compare_package_indexes(version2, version1=None, rootdir=None):
"""Compare two package index Wiki pages"""
if version1 is None:
version1 = find_closer_version(version2, rootdir=rootdir)
text = '\r\n'.join(["## History of changes for WinPython %s" % version2,
"", "The following changes were made to WinPython "
"distribution since version %s." % version1, "", ""])
pi1 = PackageIndex(version1, rootdir=rootdir)
pi2 = PackageIndex(version2, rootdir=rootdir)
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'
return text
def _copy_all_changelogs(version, basedir):
basever = '.'.join(version.split('.')[:2])
for name in os.listdir(CHANGELOGS_DIR):
if re.match(r'WinPython-%s([0-9\.]*)\.txt' % basever, name):
shutil.copyfile(osp.join(CHANGELOGS_DIR, name),
osp.join(basedir, 'build', name))
def write_changelog(version2, version1=None, rootdir=None):
"""Write changelog between version1 and version2 of WinPython"""
basedir = get_basedir(version2, rootdir=rootdir)
_copy_all_changelogs(version2, basedir)
text = compare_package_indexes(version2, version1, rootdir=rootdir)
fname = osp.join(basedir, 'build', 'WinPython-%s_History.txt' % version2)
with open(fname, 'w', encoding='utf-8-sig') as fdesc: # python 3 need
fdesc.write(text)
# Copy to winpython/changelogs
shutil.copyfile(fname, osp.join(CHANGELOGS_DIR, osp.basename(fname)))
def test_parse_package_index_wiki(version, rootdir=None):
"""Parse the package index Wiki page"""
pi = PackageIndex(version, rootdir=rootdir)
utils.print_box("WinPython %s:" % 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):
print(compare_package_indexes(basedir, version2, version1))
if __name__ == '__main__':
# 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('2.7.5.0', '2.7.4.1')
write_changelog('3.3.2.1') # , '2.7.5.0')
write_changelog('2.7.5.1') # , '2.7.5.0')
# write_changelog('3.3.0.0beta2', '3.3.0.0beta1')
# write_changelog('3.3.1.1', '3.3.1.0')
# write_changelog('3.3.2.0', '3.3.1.1')
# write_changelog('3.3.2.1', '3.3.2.0')