forked from florentbr/SeleniumBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-vb-format.py
More file actions
104 lines (77 loc) · 2.92 KB
/
build-vb-format.py
File metadata and controls
104 lines (77 loc) · 2.92 KB
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
"""Script to build the xpi add-in for firefox
Usage : python build-vb-format.py "x.x.x.x"
"""
import os, re, sys, shutil, datetime, zipfile, glob
CD = os.path.dirname(os.path.abspath(__file__))
SRC_DIR = CD + r'\vb-format'
OUT_DIR = CD + r'\bin'
RDF_PATH = SRC_DIR + r'\install.rdf'
def main(args):
arg_version = args and args[0]
set_working_dir(CD)
last_modified_time = get_file_mtime(RDF_PATH, '%Y-%m-%d %H:%M:%S')
current_version = find_in_file(RDF_PATH, r'version>([.\d]+)<');
print __doc__
print 'Last compilation : ' + (last_modified_time or 'none')
print 'Current Version : ' + current_version
new_version = arg_version or get_input_version(current_version)
print 'New version : ' + new_version + '\n'
print 'Update version number ...'
replace_in_file(RDF_PATH, r'(?<=version>)[.\d]+(?=<)', new_version)
print 'Build formater xpi ...'
make_dir(OUT_DIR)
set_working_dir(SRC_DIR)
with ZipFile(OUT_DIR + r'\vb-formatters.xpi', 'w') as zip:
zip.add(r'*')
print '\nDone'
def set_working_dir(directory):
make_dir(directory)
os.chdir(directory)
def make_dir(directory):
if not os.path.isdir(directory):
os.makedirs(directory)
def clear_dir(directory):
if os.path.isdir(directory):
shutil.rmtree(directory)
os.makedirs(directory)
def get_file_mtime(filepath, format=None):
if(not os.path.isfile(filepath)):
return None
dt = datetime.datetime.fromtimestamp(os.path.getmtime(filepath))
if format:
return dt.strftime(format)
return dt
def delete_file(filepath):
if(os.path.isfile(filepath)):
os.remove(filepath)
def find_in_file(filepath, pattern):
with open(filepath, 'r') as f:
result = re.search(pattern, f.read())
return result.group(result.re.groups)
def replace_in_file(filepath, pattern, replacement):
with open(filepath, 'r') as f:
text = re.sub(pattern, replacement, f.read())
with open(filepath, 'w') as f:
f.write(text)
def get_input(message):
try: return raw_input(message)
except NameError: return input(message)
def get_input_version(version):
while True:
input = get_input('Digit to increment [w.x.y.z] or version [0.0.0.0] or skip [s] ? ').strip()
if re.match(r's|w|x|y|z', input) :
idx = {'s': 99, 'w': 0, 'x': 1, 'y': 2, 'z': 3}[input]
return '.'.join([str((int(v)+(i == idx))*(i <= idx)) for i, v in enumerate(version.split('.'))])
elif re.match(r'\d+\.\d+\.\d+\.\d+', input):
return input
class ZipFile(zipfile.ZipFile):
def __init__(cls, file, mode):
zipfile.ZipFile.__init__(cls, file, mode)
def add(self, path):
for item in glob.glob(path):
if os.path.isdir(item):
self.add(item + r'\*');
else:
self.write(item)
if __name__ == '__main__':
main(sys.argv[1:])