-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup_script.py
172 lines (141 loc) · 5.07 KB
/
setup_script.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# MIT License
#
# Copyright (c) 2020-2024 SCRUTINY developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from os import replace, remove
from shutil import copyfileobj, rmtree
import subprocess
import sys
from urllib.request import urlopen
from zipfile import ZipFile
from scrutiny.config import URL, Paths
from scrutiny.utils import errmsg
def download_file(tool_name, tool_url, tool_path):
"""
Downloads file and saves it to tool_path
:param tool_name:
:param tool_url:
:param tool_path:
:return:
"""
print("Downloading " + tool_name + "... ", end="")
try:
with urlopen(tool_url) as remote, open(tool_path, "wb") as file:
copyfileobj(remote, file)
print("Done.")
return True
except Exception as ex:
return errmsg(tool_name, "downloading", ex)
def download_and_extract(tool_name, tool_url, file_translations):
"""
Downloads zip archive and extracts it's contents
:param tool_name:
:param tool_url:
:param file_translations:
:return:
"""
archive = tool_name + "_dist.zip"
directory = tool_name + "_extracted"
if not download_file(tool_name, tool_url, archive):
return False
print("Extracting " + tool_name + "... ", end="")
try:
with ZipFile(archive, "r") as zipped:
zipped.extractall(directory)
print("Done.")
except Exception as ex:
errmsg(tool_name, "extracting", ex)
try:
remove(archive)
except Exception as ex:
print(archive,
" could not be removed, please remove it manually.", ex)
return False
return_status = True
print("Finishing " + tool_name + " set-up...", end="")
try:
for (original, destination) in file_translations:
replace(directory + "/" + original, destination)
print("Done.")
except Exception as ex:
errmsg(tool_name + " files", "moving", ex)
return_status = False
print("Cleaning up after " + tool_name + " set-up...", end="")
try:
remove(archive)
rmtree(directory)
print("Done.")
except Exception as ex:
errmsg(tool_name + " set-up", "cleaning after", ex)
print("\tRemove", archive, "and", directory, "directory manually.")
return_status = False
return return_status
def pip_install(package):
"""
Installs pip package
:param package:
:return:
"""
try:
print("Installing package", package, "with pip...")
subprocess.check_call([sys.executable,
"-m", "pip", "install",
package])
print("Done.")
return True
except Exception as ex:
errmsg(package, "installing", ex)
return False
def setup_jcalgtest():
"""
Sets up JCAlgTest binaries and cap files
:return:
"""
jc_files = [Paths.JCALGTEST,
Paths.JCALGTEST_305,
Paths.JCALGTEST_304,
Paths.JCALGTEST_222]
jc_translations = [(dest.split("/")[-1], dest) for dest in jc_files]
return download_and_extract("JCAlgTest", URL.JCALGTEST, jc_translations)
if __name__ == "__main__":
return_value: bool = True
print("Setting up Smart Card List:")
return_value = return_value and \
download_file("Smart Card List",
URL.SMARTCARD_LIST, Paths.SMARTCARD_LIST)
print("Setting up GlobalPlatformPro:")
return_value = return_value and \
download_file("GlobalPlatformPro", URL.GPPRO, Paths.GPPRO)
print()
print("Setting up JCAlgTest:")
return_value = return_value and setup_jcalgtest()
print()
print("Setting up jsonpickle:")
return_value = return_value and pip_install("jsonpickle")
print()
print("Setting up dominate:")
return_value = return_value and pip_install("dominate")
print()
if not return_value:
print("Issues occured during set-up.",
"Check the manual set-up section in README.md for assistance.")
sys.exit(1)
print("Set-up completed without issues.")
sys.exit(0)