-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-latest.py
executable file
·71 lines (52 loc) · 2.15 KB
/
update-latest.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
#!/usr/bin/python3 -u
import os
import stat
import re
import shutil
def main():
base_dir = os.path.dirname(os.path.realpath(__file__))
for category in ['brick', 'bricklet', 'extension']:
start_dir = os.path.join(base_dir, category + 's')
for root, dirs, files in os.walk(start_dir):
if root == start_dir:
continue
names = set()
versions = []
extensions = set()
for file_ in files:
path = os.path.join(root, file_)
s = os.stat(path)
if (stat.S_IMODE(s.st_mode) & 0o777) not in [0o664, 0o644]:
print('WARNING: unexpected mode:', path, oct(s.st_mode))
if file_ == '.metadata':
continue
if re.match(r'^{0}_.*_firmware_latest.z?bin$'.format(category), file_) != None:
continue
m = re.match(r'^{0}_(.*)_firmware_(\d+)_(\d+)_(\d+)\.(z?bin)$'.format(category), file_)
if m == None:
print('WARNING: unexpected filename:', file_, path)
continue
names.add(m.group(1))
versions.append((int(m.group(2)), int(m.group(3)), int(m.group(4))))
extensions.add(m.group(5))
if len(names) == 0:
print('ERROR: no names:', root)
continue
if len(names) > 1:
print('ERROR: mixed names:', root)
continue
if len(extensions) > 1:
print('ERROR: mixed extensions:', root)
continue
name = names.pop()
latest_version = sorted(versions)[-1]
extension = extensions.pop()
source = '{0}_{1}_firmware_{2}_{3}_{4}.{5}'.format(category, name, *latest_version, extension)
target = '{0}_{1}_firmware_latest.{2}'.format(category, name, extension)
try:
os.remove(os.path.join(root, target))
except FileNotFoundError:
pass
os.symlink(source, os.path.join(root, target))
if __name__ == '__main__':
main()