forked from florianschanda/miss_hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mh_sl_unpack
executable file
·75 lines (64 loc) · 3.17 KB
/
mh_sl_unpack
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
#!/usr/bin/env python3
##############################################################################
## ##
## MATLAB Independent, Small & Safe, High Integrity Tools ##
## ##
## Copyright (C) 2020, Florian Schanda ##
## ##
## This file is part of MISS_HIT. ##
## ##
## MATLAB Independent, Small & Safe, High Integrity Tools (MISS_HIT) is ##
## free software: you can redistribute it and/or modify it under the ##
## terms of the GNU General Public License as published by the Free ##
## Software Foundation, either version 3 of the License, or (at your ##
## option) any later version. ##
## ##
## MISS_HIT is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with MISS_HIT. If not, see <http://www.gnu.org/licenses/>. ##
## ##
##############################################################################
# This tool unpacks new-style simulink models so they can be more
# easily inspected.
import argparse
import os.path
import shutil
import sys
import zipfile
def process(filename):
core_name = filename.replace(".slx", "_unpacked")
if os.path.isfile(core_name):
print("cannot unpack %s, unpacked name exists and is a file" %
filename)
sys.exit(1)
elif os.path.isdir(core_name):
shutil.rmtree(core_name)
with zipfile.ZipFile(filename, "r") as zf:
for name in zf.namelist():
dst_name = os.path.join(core_name, name)
os.makedirs(os.path.dirname(dst_name), exist_ok=True)
with zf.open(name) as in_fd:
with open(dst_name, "wb") as out_fd:
out_fd.write(in_fd.read())
def main():
ap = argparse.ArgumentParser()
ap.add_argument("name", metavar="FILE|DIR")
options = ap.parse_args()
if os.path.isfile(options.name):
if options.name.endswith(".slx"):
process(options.name)
else:
ap.error("must be a .slx file")
elif os.path.isdir(options.name):
for path, _, files in os.walk(options.name):
for f in files:
if f.endswith(".slx"):
process(os.path.join(path, f))
else:
ap.error("cannot find file or directory")
if __name__ == "__main__":
main()