This repository has been archived by the owner on Feb 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathversion_checker.py
executable file
·501 lines (370 loc) · 13.2 KB
/
version_checker.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env python3
from collections import OrderedDict
import bz2
import lzma
import os
import re
import subprocess
import sys
import urllib.request
def run_command(command,
stdin_data=None,
cwd=None,
shell=False,
universal_newlines=True):
try:
process = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd,
shell=shell,
universal_newlines=universal_newlines
)
output, error = process.communicate(input=stdin_data)
exit_code = process.returncode
return (exit_code, output, error)
except:
raise Exception('Failed to run command: "%s"' % ' '.join(command))
def unquote(string):
if len(string) >= 3 and string.startswith('"') and \
string.endswith('"') and not string.endswith(r'\"'):
return string[1:-1]
else:
return string
def parse_params(params):
strpairs = list()
inquote = False
curpair = str()
prevchar = ''
for c in params:
if c == '"' and prevchar != '\\':
inquote = not inquote
elif c == ',' and not inquote:
strpairs.append(curpair.strip())
curpair = str()
prevchar = ''
continue
prevchar = c
curpair += c
if curpair:
strpairs.append(curpair.strip())
pairs = dict()
for pair in strpairs:
partition = pair.partition('=')
key = unquote(partition[0].strip())
value = unquote(partition[2].strip())
pairs[key] = value
return pairs
def get_bash_var(var):
if not var:
raise Exception('No variable(s) specified')
if type(var) == list:
formatted = str()
for v in var:
formatted += '%s=${%s} ' % (v, v)
exit_code, output, error = run_command(
['bash', '-c', 'source %s && echo %s' % (filename, formatted)]
)
if not output or not output.partition('\n')[0]:
return None
return output.partition('\n')[0].split(' ')
else:
exit_code, output, error = run_command(
['bash', '-c', 'source %s && echo ${%s}' % (filename, var)]
)
if not output or not output.partition('\n')[0]:
return None
return output.partition('\n')[0]
def replace_bash_vars(formatstr):
split = re.split(r'(\${.+?})', formatstr)
result = str()
for piece in split:
if not piece.startswith('$'):
result += piece
else:
var = re.search(r'\${(.+?)}', piece)
if not var:
raise Exception('Failed to parse "%s"' % piece)
value = get_bash_var(var.group(1))
if not value:
raise Exception('Failed to get ${%s} from %s' % (var, filename))
result += value
return result
def get_pkgbuild_ver(params):
for param in params:
value = params[param]
if param == 'format':
return replace_bash_vars(value)
elif param == 'auto':
pkgver = get_bash_var('pkgver')
actualver = get_bash_var('_actual_ver')
extraver = get_bash_var('_extra_ver')
ubuntuver = get_bash_var('_ubuntu_ver')
ubunturel = get_bash_var('_ubuntu_rel')
ppaver = get_bash_var('_ppa_ver')
pparel = get_bash_var('_ppa_rel')
version = str()
if actualver and extraver:
version += actualver + extraver
else:
version += pkgver
if ubuntuver:
version += ' Ubuntu ' + ubuntuver
elif ppaver:
version += ' PPA ' + ppaver
if ubunturel:
version += ' ' + ubunturel
elif pparel:
version += ' ' + pparel
return version
else:
raise Exception('Invalid parameter "%s"' % param)
def get_ubuntu_ver(params):
repo = params['repo']
name = replace_bash_vars(params['name'])
native = 'native' in params
url = 'https://launchpad.net/ubuntu/%s/+source/%s' % (repo, name)
if native:
regex = r'^.*current\ release\ \((.*)\).*$'
else:
regex = r'^.*current\ release\ \((.*)-(.*)\).*$'
with urllib.request.urlopen(url) as f:
for line in f:
match = re.search(regex, line.decode('UTF-8'))
if not match:
continue
if native:
return match.group(1)
else:
return ' '.join(match.groups())
return None
def get_archlinux_ver(params):
repo = params['repo']
name = replace_bash_vars(params['name'])
arch = params['arch']
url = 'https://www.archlinux.org/packages/%s/%s/%s/' % (repo, arch, name)
regex = r'^.*%s\ (.*)-(.*)\ .+$' % re.escape(name)
with urllib.request.urlopen(url) as f:
for line in f:
if not '<title>' in line.decode('UTF-8'):
continue
match = re.search(regex, line.decode('UTF-8'))
if not match:
continue
return ' '.join(match.groups())
return None
def get_gnome_ver(params):
name = replace_bash_vars(params['name'])
majorver = params['majorver']
url = 'http://ftp.gnome.org/pub/GNOME/sources/%s/%s/' % (name, majorver)
regex = r'>LATEST-IS-(.*)<'
# Sometimes, there are multiple "LATEST-IS-..." files
latest = None
with urllib.request.urlopen(url) as f:
for line in f:
match = re.search(regex, line.decode('UTF-8'))
if not match:
continue
latest = match.group(1)
return latest
def get_launchpad_ver(params):
name = replace_bash_vars(params['name'])
if 'tarname' in params:
tarname = replace_bash_vars(params['tarname'])
else:
tarname = name
url = 'https://launchpad.net/%s/+download' % name
regex = r'>%s[-_]+(.+?)\.tar' % re.escape(tarname)
with urllib.request.urlopen(url) as f:
for line in f:
match = re.search(regex, line.decode('UTF-8'))
if not match:
continue
return match.group(1)
return None
def get_ppa_ver(params):
name = replace_bash_vars(params['name'])
ppaurl = params['url']
native = 'native' in params
norel = 'norel' in params
ppaurl = ppaurl.lstrip('ppa:')
url = 'http://ppa.launchpad.net/%s/ubuntu/pool/main/%s/%s/?C=M;O=A' % \
(ppaurl, name[0:1], name)
if native:
regex = r'>%s_(.+?)-(.+?)\.tar\.[a-z\.]+<' % re.escape(name)
elif norel:
regex = r'>%s_(.+?)\.(debian|diff)\.[a-z\.]+<' % re.escape(name)
else:
regex = r'>%s_(.+?)-(.+?)\.(?:debian|diff)\.[a-z\.]+<' % re.escape(name)
latest = None
with urllib.request.urlopen(url) as f:
for line in f:
match = re.search(regex, line.decode('UTF-8'))
if not match:
continue
if native:
latest = match.group(1) + ' ' + match.group(2)
elif norel:
latest = match.group(1)
else:
latest = ' '.join(match.groups())
return latest
def get_ubuntudb_ver(params):
pkgprefix = params['pkgprefix']
verprefix = params['verprefix']
relprefix = None
if 'relprefix' in params:
relprefix = params['relprefix']
ignorerel = 'ignorerel' in params
repo = params['repo']
skip = list()
if 'skip' in params:
for pkg in params['skip'].split(';'):
skip.append(pkgprefix + pkg.strip())
onlyver = None
if 'onlyver' in params:
onlyver = params['onlyver']
url = 'http://archive.ubuntu.com/ubuntu/dists/%s/main/source/Sources.' % repo
if repo[0] >= 'x':
url += 'xz'
method = lzma
else:
url += 'bz2'
method = bz2
packages = list()
repovers = list()
pkgbuildvers = list()
with urllib.request.urlopen(url) as f:
with method.open(f, 'r') as b:
curpkg = None
for line in b:
linestr = line.decode('UTF-8')
if linestr.startswith('Package: '):
if linestr.startswith('Package: ' + pkgprefix):
curpkg = linestr[9:-1]
if curpkg in skip:
curpkg = None
elif curpkg and linestr.startswith('Version: '):
if ignorerel:
repover = linestr[9:-1].partition('-')[0]
else:
repover = linestr[9:-1].replace('-', ' ')
# Strip epoch
if ':' in repover:
repover = repover.partition(':')[2]
if onlyver and not re.search(onlyver, repover):
curpkg = None
continue
packages.append(curpkg)
repovers.append(repover)
curpkg = None
shortnames = [p[len(pkgprefix):].replace('-', '_') for p in packages]
vers = get_bash_var([(verprefix + name) for name in shortnames])
if relprefix:
rels = get_bash_var([(relprefix + name) for name in shortnames])
for i in range(0, len(packages)):
ver = vers[i].partition('=')[2]
if relprefix:
rel = rels[i].partition('=')[2]
pkgbuildvers.append('%s %s' % (ver, rel))
else:
pkgbuildvers.append(ver)
newpackages = list()
newrepovers = list()
newpkgbuildvers = list()
for i in range(0, len(packages)):
if repovers[i] == pkgbuildvers[i]:
continue
newpackages.append(packages[i])
newrepovers.append(repovers[i])
newpkgbuildvers.append(pkgbuildvers[i])
return newpackages, newrepovers, newpkgbuildvers
def parse_pkgbuild_meta():
vcinfos = dict()
with open(filename, 'r') as f:
for line in f:
if not line.startswith('#'):
continue
else:
vcline = re.search(r'^#\s*vercheck-(.+?)\s*:\s*(.+)$', line)
if not vcline:
continue
else:
source = vcline.group(1).strip()
params = vcline.group(2).strip()
vcinfos[source] = params
return vcinfos
def print_versions(vcinfos):
# Special functions for split packages with different versions in each
# split package
if 'ubuntudb' in vcinfos:
params = parse_params(vcinfos['ubuntudb'])
packages, repovers, pkgbuildvers = get_ubuntudb_ver(params)
for i in range(0, len(packages)):
print('Package: ' + packages[i])
print('PKGBUILD version: ' + pkgbuildvers[i])
print('Ubuntu version: ' + repovers[i])
print()
return
versions = dict()
names = dict()
names['pkgbuild'] = 'PKGBUILD version'
names['ubuntu'] = 'Ubuntu version'
names['archlinux'] = 'Arch Linux version'
names['gnome'] = 'GNOME version'
names['launchpad'] = 'Launchpad version'
names['ppa'] = 'PPA version'
maxlen = max(len(names[s]) for s in vcinfos) + 1
vcinfos = OrderedDict(sorted(vcinfos.items(), key=lambda t: t[0]))
if 'pkgbuild' in vcinfos:
params = parse_params(vcinfos['pkgbuild'])
version = get_pkgbuild_ver(params)
print('%s %s' % ((names['pkgbuild'] + ':').ljust(maxlen), version))
del vcinfos['pkgbuild']
if 'ubuntu' in vcinfos:
params = parse_params(vcinfos['ubuntu'])
version = get_ubuntu_ver(params)
print('%s %s' % ((names['ubuntu'] + ':').ljust(maxlen), version))
del vcinfos['ubuntu']
for source in vcinfos:
params = parse_params(vcinfos[source])
if source == 'archlinux':
version = get_archlinux_ver(params)
elif source == 'gnome':
version = get_gnome_ver(params)
elif source == 'launchpad':
version = get_launchpad_ver(params)
elif source == 'ppa':
version = get_ppa_ver(params)
else:
raise Exception('Unknown source "%s"' % source)
versions[source] = 'blah'
print('%s %s' % ((names[source] + ':').ljust(maxlen), version))
def find_pkgbuild():
# Check if this script is symlinked to a directory containing a PKGBUILD
directory = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(directory, 'PKGBUILD')
if os.path.exists(path):
return path
# Check if a directory was passed
if len(sys.argv) > 1:
directory = sys.argv[1]
path = os.path.join(directory, 'PKGBUILD')
if os.path.exists(path):
return path
# Check if we're in a package's directory
path = os.path.join(os.getcwd(), 'PKGBUILD')
if os.path.exists(path):
return path
raise Exception('Could not find PKGBUILD file')
try:
filename = find_pkgbuild()
vcinfos = parse_pkgbuild_meta()
if not vcinfos:
print('No version checker metadata in the PKGBUILD')
else:
print_versions(vcinfos)
except KeyboardInterrupt:
pass