-
Notifications
You must be signed in to change notification settings - Fork 16
/
version.py
executable file
·61 lines (49 loc) · 1.49 KB
/
version.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
#!/usr/bin/env python3
import argparse
import os
import re
from functools import partial
def escape(match):
g = match.group(1)
if g is None:
return b'@VERSION@'
else:
return b'@X' + g + b'VERSION@'
def unescape(version, match):
g = match.group(1)
if g == b'':
return version
else:
return b'@' + g[1:] + b'VERSION@'
def main():
parser = argparse.ArgumentParser(description='version placeholder tool for noscript')
parser.add_argument('--add', action='store_true', help='add version string')
parser.add_argument('--strip', action='store_true', help='strip version string')
parser.add_argument('version', nargs=1, help='version string to add or strip')
parser.add_argument('path', nargs=1, help='path to xpi source')
args = parser.parse_args()
version = args.version[0].encode('ascii')
path = args.path[0]
if args.add and (not args.strip):
patt = re.compile(b'@(X*)VERSION@')
func = partial(unescape, version)
elif args.strip and (not args.add):
patt = re.compile(re.escape(version) + b'|@(X*)VERSION@')
func = escape
else:
print('Either --add or --strip must be specified')
return
path_patt = re.compile(r'\.(?:dtd|xul|js)$')
for root, dirs, files in os.walk(path):
for name in files:
if not path_patt.search(name):
continue
filepath = os.path.join(root, name)
with open(filepath, 'rb') as f:
data = f.read()
ndata = patt.sub(func, data)
if ndata != data:
with open(filepath, 'wb') as f:
f.write(ndata)
if __name__ == '__main__':
main()