-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslconfig.py
50 lines (42 loc) · 1.51 KB
/
slconfig.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
import pathlib
import zlib
from struct import unpack, pack
from sys import argv
def uncompress(src, dst):
with src.open('rb') as f:
header = f.read(4)
compressed_data = f.read()
f.close()
expected_size = unpack('>I', header)[0]
decompressed_data = zlib.decompress(compressed_data)
if expected_size != len(decompressed_data):
print("WARN: size mismatch")
with dst.open('wb') as f:
f.write(decompressed_data)
f.close()
def compress(src, dst):
with src.open('rb') as f:
decompressed_data = f.read()
f.close()
header = pack('>I', len(decompressed_data))
compressed_data = zlib.compress(decompressed_data)
with dst.open('wb') as f:
f.write(header)
f.write(compressed_data)
f.close()
def main():
if len(argv) > 1:
outfile = pathlib.Path(argv[2]) if len(argv) > 2 else None
infile = pathlib.Path(argv[1])
ext = infile.suffix.lower()
if ext == '.slc':
uncompress(infile, outfile or infile.with_suffix('.xml'))
elif ext == '.xml':
compress(infile, outfile or infile.with_suffix('.slc'))
else:
print('Поддерживаются только файлы .slc и .xml')
else:
print(f'Программа для распаковки/упаковки файлов конфигурации Starline')
print(f'Использование: {pathlib.Path(argv[0]).name} slc|xml [outfile]')
if __name__ == "__main__":
main()