-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsectools.py
More file actions
172 lines (141 loc) · 5.02 KB
/
Copy pathsectools.py
File metadata and controls
172 lines (141 loc) · 5.02 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
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
# ===============================================================================
#
# Copyright (c) 2013-2016 Qualcomm Technologies, Inc.
# All Rights Reserved.
# Confidential and Proprietary - Qualcomm Technologies, Inc.
#
# ===============================================================================
"""Provides a command line interface to the services provided by sectools
.. data:: SECTOOLS_TOOL_NAME
Name of the tool
.. data:: SECTOOLS_TOOL_VERSION
Version of the tool
"""
import optparse
import os
import sys
import traceback
from sectools import SECTOOLS_TOOL_NAME, SECTOOLS_TOOL_VERSION
from sectools.common.utils.c_logging import logger
# List of features
FEATURES_LIST = []
try:
from sectools.features.fbc import fuseblower
FEATURES_LIST.append(fuseblower)
except ImportError:
pass
try:
from sectools.features.isc import secimage
FEATURES_LIST.append(secimage)
except ImportError:
pass
try:
from sectools.features.kpc import keyprovision
FEATURES_LIST.append(keyprovision)
except ImportError:
pass
try:
from sectools.features.aost import aost_license_generator
FEATURES_LIST.append(aost_license_generator)
except ImportError:
pass
try:
from sectools.features.dpc import debugpolicy
FEATURES_LIST.append(debugpolicy)
except ImportError:
pass
try:
from sectools.features.ltf import localtf
FEATURES_LIST.append(localtf)
except ImportError:
pass
try:
from sectools.features.sip import csms_proxy
FEATURES_LIST.append(csms_proxy)
except ImportError:
pass
try:
from sectools.features.mbngen import mbngen
FEATURES_LIST.append(mbngen)
except ImportError:
pass
if not FEATURES_LIST:
raise RuntimeError('Sectools could not find any packaged features')
__version__ = SECTOOLS_TOOL_NAME + ' ' + SECTOOLS_TOOL_VERSION
class SectoolsParser(optparse.OptionParser):
"""Parser for command line arguments supported by Sectools."""
def __init__(self):
# Initialize the base parser
optparse.OptionParser.__init__(self, usage=self.c_usage,
description=self.c_description,
version=self.c_version,
epilog=self.c_epilog)
self.c_add_options()
self.opt_args, self.pos_args = self.parse_args(args=sys.argv[:2])
if len(self.pos_args) == 1:
self.print_help(sys.stdout)
@property
def c_usage(self):
"""(str) Returns the usage of the program.
"""
return self.c_prog + ' [feature]'
@property
def c_prog(self):
"""(str) Returns the name of the program. By default this is the name
of the python file being executed.
"""
return os.path.basename(sys.argv[0])
@property
def c_description(self):
"""(str) Returns the description of the program."""
return 'This program provides an interface to the sectools features'
@property
def c_version(self):
"""(str) Returns the version of the program."""
return __version__
@property
def c_epilog(self):
"""(str) Returns the epilog for the program."""
return ('\n'
'Features available for sectools are: ' + '\n'
' ' + '\n '.join(str(idx + 1) + '. ' + feature.CMD_ARG_TOOL_NAME for idx, feature in enumerate(FEATURES_LIST)) + '\n'
'\n'
'Example usage:' + '\n'
' ' + self.c_prog + ' ' + FEATURES_LIST[0].CMD_ARG_TOOL_NAME + ' -h' + '\n'
'')
def format_epilog(self, formatter):
"""This method is implemented to override the OptionParser's formatting
of the epilog"""
return self.epilog
def c_add_options(self):
"""Adds the command line args supported by sectools."""
pass
def main(args):
"""Parses the command line arguments, performs any basic operations based on
the parsed arguments and starts processing using the isc module.
"""
# Print the tool's launch command
logger.debug2('\n\n Sectools launched as: "' + ' '.join(sys.argv) + '"\n')
if len(args) > 1:
feature = args[1]
for supported_feature in FEATURES_LIST:
if feature == supported_feature.CMD_ARG_TOOL_NAME:
supported_feature.main(supported_feature.parse_args(sys.argv[1:]))
break
else:
raise RuntimeError('Feature provided from command line: "' + feature + '" is invalid.' + '\n'
' ' + 'Please choose from : ' + str([f.CMD_ARG_TOOL_NAME for f in FEATURES_LIST]))
if __name__ == '__main__':
try:
# Check that the command line are valid and are normalized.
args = SectoolsParser().pos_args
main(args)
except Exception:
logger.error(traceback.format_exc())
logger.error(sys.exc_info()[1])
sys.exit(1)
except KeyboardInterrupt:
print
logger.error('Keyboard Interrupt Received. Exiting!')
sys.exit(1)
sys.exit(0)