-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexshop-scan.py
executable file
·161 lines (143 loc) · 6 KB
/
texshop-scan.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
#!/usr/bin/env python
import urllib.request
import email.utils
import xmltodict
import re
import subprocess
import os
import errno
DEBUG_MODE = False
if DEBUG_MODE:
APPCAST_URL="http://localhost:4000/texshopappcast.xml"
# python -m SimpleHTTPServer 8000
else:
APPCAST_URL="https://pages.uoregon.edu/koch/texshop/texshop-64/texshopappcast.xml"
def getAppcastData():
# TODO: handle errors, timeouts, ...
# TODO: set data read limit: say, at most 4kb
file = urllib.request.urlopen(APPCAST_URL)
data = file.read()
file.close()
return xmltodict.parse(data)
# Example
#
# OrderedDict([
# (u'rss', OrderedDict([
# (u'@version', u'2.0'),
# (u'@xmlns:sparkle', u'http://www.andymatuschak.org/xml-namespaces/sparkle'),
# (u'@xmlns:dc', u'http://purl.org/dc/elements/1.1/'),
# (u'channel', OrderedDict([
# (u'title', u'TeXShop Versions'),
# (u'link', u'http://pages.uoregon.edu/koch/texshop/texshop-64'),
# (u'description', u'Most recent changes.'),
# (u'language', u'en'),
# (u'item', OrderedDict([
# (u'title', u'Version 3.49'),
# (u'sparkle:releaseNotesLink', u'http://pages.uoregon.edu/koch/texshop/texshop-64/releasenotes349'),
# (u'pubDate', u'Sat, 17 Jan 2015 12:00:00 +0000'),
# (u'enclosure', OrderedDict([
# (u'@url', u'http://pages.uoregon.edu/koch/texshop/texshop-64/texshop349.zip'),
# (u'@sparkle:version', u'3.49'),
# (u'@length', u'39445881'),
# (u'@type', u'application/octet-stream'),
# (u'@sparkle:dsaSignature', u'MCwCFEiGqoxtjz0CyrByjK4d5vkuCLabAhQ9FIFOtNaA6EWxt1YnBNSSZchHGw==')
# ]))
# ]))
# ]))
# ]))
# ])
# OrderedDict([
# (u'rss', OrderedDict([
# (u'@xmlns:sparkle', u'http://www.andymatuschak.org/xml-namespaces/sparkle'),
# (u'@version', u'2.0'),
# (u'channel', OrderedDict([
# (u'title', u'TeXShop'),
# (u'item', OrderedDict([
# (u'title', u'4.15'),
# (u'pubDate', u'Sun, 04 Nov 2018 17:06:30 -0800'),
# (u'sparkle:minimumSystemVersion', u'10.10.0'),
# (u'enclosure', OrderedDict([
# (u'@url', u'https://pages.uoregon.edu/koch/texshop/texshop-64/texshop415.zip'),
# (u'@sparkle:version', u'4.15'),
# (u'@sparkle:shortVersionString', u'4.15'),
# (u'@length', u'45204097'),
# (u'@type', u'application/octet-stream'),
# (u'@sparkle:edSignature', u'PBdl84zUpVBny1a1GYtLR5ZvhBc2ByBD6jgep7Wh147vfxAsH5CqQomxVi1aP1wCqkjWG2tWerdG1nD6Wj8WDA=='),
# (u'@sparkle:dsaSignature', u'MC0CFQCYmCRzgucdVoW08Zxs+fb4MFGY3QIUJ3BY+XkFzFn0k9ATI1xlM2IJxKE=')
# ]))
# ]))
# ]))
# ]))
# ])
# TODO: validate data. E.g.:
# - verify @xmlns:sparkle is present and set to http://www.andymatuschak.org/xml-namespaces/sparkle
def extractAppcastData(data):
rss = data['rss']
assert rss['@xmlns:sparkle'] == 'http://www.andymatuschak.org/xml-namespaces/sparkle'
channel = rss['channel']
item = channel['item']
if isinstance(item, list):
item = item[0]
pubDate = item['pubDate'] # rfc2822 format is directly used by git
releaseNotesLink = item.get('sparkle:releaseNotesLink', None)
#date_tuple = email.utils.parsedate_tz(pubDate) # TODO: can be None
#timestamp = email.utils.mktime_tz(date_tuple)
#date = datetime.datetime.fromtimestamp(timestamp)
enclosure = item['enclosure']
version = enclosure['@sparkle:version']
url = enclosure['@url']
return { 'version': version, 'date': pubDate, 'url': url, 'relnotes': releaseNotesLink }
# From http://stackoverflow.com/questions/600268
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
raw = getAppcastData()
data = extractAppcastData(raw)
binary_url = data['url'];
source_url = re.sub(r'texshop([^/]+)\.zip$', r'texshopsource\1.zip', binary_url)
print("Found version %s (released %s)" % (data['version'], data['date']))
# create directory for that version, if it did not already exist
dir = 'releases/' + data['version'] + '/'
mkdir_p(dir)
# Check if this is a new release
if os.path.isfile(dir + 'DONE'):
print('version already catalogued')
exit(0)
# Download file from given url into file at dst
def download(url, dst):
res = subprocess.call(["curl", "-C", "-", "-o", dst, url])
if res != 0:
print('failed downloading ' + url)
exit(1)
def patch_relnotes(dst):
# read all in
with open(dst, 'r') as file:
str = file.read()
# write out a backup file
with open(dst + ".orig", 'w') as file:
file.write(str)
# remove everything up to and include <div id="main"> (plus any whitespace) thereafter
str = re.sub(r'<!DOCTYPE.*<div id="main">\s*', '', str, flags=re.DOTALL)
# replace everything after </div>
str = re.sub(r'(<p>\s*)*</div>\s*</body>\s*</html>\s*', '', str, flags=re.DOTALL)
# write it back out
with open(dst, 'w') as file:
file.write(str)
print("Downloading appcast from " + APPCAST_URL)
download(APPCAST_URL, dir + 'appcast-%s.xml' % data['version'])
if data['relnotes'] == None:
print("no release notes given")
else:
print("Downloading release notes from " + data['relnotes'])
download(data['relnotes'], dir + 'relnotes-%s.txt' % data['version'])
patch_relnotes(dir + 'relnotes-%s.txt' % data['version'])
print("Downloading source from " + source_url)
download(source_url, dir + 'texshopsource-%s.zip' % data['version'])
print("Downloading binary from " + binary_url)
download(binary_url, dir + 'texshop-%s.zip' % data['version'])
# Once all downloads hav successfully completed, record this
open(dir + 'DONE', 'a').close()