Debian Codegraph @

setup.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 import io
3 import os
4 import sys
5 import shutil
6 import subprocess
7 from setuptools import setup, Command, find_packages
8 from distutils.dir_util import remove_tree
9
10 MODULE_NAME = "binwalk"
11 MODULE_VERSION = "2.4.3"
12 SCRIPT_NAME = MODULE_NAME
13 MODULE_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
14
15 # If this version of binwalk was checked out from the git repository,
16 # include the git commit hash as part of the version number reported
17 # by binwalk.
18 try:
19 label = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"],
20 stderr=subprocess.DEVNULL).decode('utf-8')
21 MODULE_VERSION = "%s+%s" % (MODULE_VERSION, label.strip())
22 except KeyboardInterrupt:
23 raise
24 except Exception:
25 pass
26
27
28 def which(command):
29 # /usr/local/bin is usually the default install path, though it may not be in $PATH
30 usr_local_bin = os.path.sep.join([os.path.sep, 'usr', 'local', 'bin', command])
31
32 try:
33 location = subprocess.Popen(
34 ["which", command],
35 shell=False, stdout=subprocess.PIPE).communicate()[0].strip()
36 except KeyboardInterrupt as e:
37 raise e
38 except Exception as e:
39 pass
40
41 if not location and os.path.exists(usr_local_bin):
42 location = usr_local_bin
43
44 return location
45
46
47 def find_binwalk_module_paths():
48 paths = []
49
50 try:
51 import binwalk
52 paths = binwalk.__path__
53 except KeyboardInterrupt as e:
54 raise e
55 except Exception:
56 pass
57
58 return paths
59
60
61 def remove_binwalk_module(pydir=None, pybin=None):
62 if pydir:
63 module_paths = [pydir]
64 else:
65 module_paths = find_binwalk_module_paths()
66
67 for path in module_paths:
68 try:
69 remove_tree(path)
70 except OSError:
71 pass
72
73 if not pybin:
74 pybin = which(MODULE_NAME)
75
76 if pybin:
77 try:
78 sys.stdout.write("removing '%s'\n" % pybin)
79 os.remove(pybin)
80 except KeyboardInterrupt:
81 pass
82 except Exception:
83 pass
84
85
86 class IDAUnInstallCommand(Command):
87 description = "Uninstalls the binwalk IDA plugin module"
88 user_options = [
89 ('idadir=', None, 'Specify the path to your IDA install directory.'),
90 ]
91
92 def initialize_options(self):
93 self.idadir = None
94 self.mydir = os.path.join(os.path.dirname(
95 os.path.realpath(__file__)), "src")
96
97 def finalize_options(self):
98 pass
99
100 def run(self):
101 if self.idadir is None:
102 sys.stderr.write(
103 "Please specify the path to your IDA install directory with the '--idadir' option!\n")
104 return
105
106 binida_dst_path = os.path.join(self.idadir, 'plugins', 'binida.py')
107 binwalk_dst_path = os.path.join(self.idadir, 'python', 'binwalk')
108
109 if os.path.exists(binida_dst_path):
110 sys.stdout.write("removing '%s'\n" % binida_dst_path)
111 os.remove(binida_dst_path)
112 if os.path.exists(binwalk_dst_path):
113 sys.stdout.write("removing '%s'\n" % binwalk_dst_path)
114 shutil.rmtree(binwalk_dst_path)
115
116
117 class IDAInstallCommand(Command):
118 description = "Installs the binwalk IDA plugin module"
119 user_options = [
120 ('idadir=', None, 'Specify the path to your IDA install directory.'),
121 ]
122
123 def initialize_options(self):
124 self.idadir = None
125 self.mydir = os.path.join(os.path.dirname(
126 os.path.realpath(__file__)), "src")
127
128 def finalize_options(self):
129 pass
130
131 def run(self):
132 if self.idadir is None:
133 sys.stderr.write(
134 "Please specify the path to your IDA install directory with the '--idadir' option!\n")
135 return
136
137 binida_src_path = os.path.join(self.mydir, 'scripts', 'binida.py')
138 binida_dst_path = os.path.join(self.idadir, 'plugins')
139
140 if not os.path.exists(binida_src_path):
141 sys.stderr.write(
142 "ERROR: could not locate IDA plugin file '%s'!\n" %
143 binida_src_path)
144 return
145 if not os.path.exists(binida_dst_path):
146 sys.stderr.write(
147 "ERROR: could not locate the IDA plugins directory '%s'! Check your --idadir option.\n" %
148 binida_dst_path)
149 return
150
151 binwalk_src_path = os.path.join(self.mydir, 'binwalk')
152 binwalk_dst_path = os.path.join(self.idadir, 'python')
153
154 if not os.path.exists(binwalk_src_path):
155 sys.stderr.write(
156 "ERROR: could not locate binwalk source directory '%s'!\n" %
157 binwalk_src_path)
158 return
159 if not os.path.exists(binwalk_dst_path):
160 sys.stderr.write(
161 "ERROR: could not locate the IDA python directory '%s'! Check your --idadir option.\n" %
162 binwalk_dst_path)
163 return
164
165 binida_dst_path = os.path.join(binida_dst_path, 'binida.py')
166 binwalk_dst_path = os.path.join(binwalk_dst_path, 'binwalk')
167
168 if os.path.exists(binida_dst_path):
169 os.remove(binida_dst_path)
170 if os.path.exists(binwalk_dst_path):
171 shutil.rmtree(binwalk_dst_path)
172
173 sys.stdout.write("copying %s -> %s\n" %
174 (binida_src_path, binida_dst_path))
175 shutil.copyfile(binida_src_path, binida_dst_path)
176
177 sys.stdout.write("copying %s -> %s\n" %
178 (binwalk_src_path, binwalk_dst_path))
179 shutil.copytree(binwalk_src_path, binwalk_dst_path)
180
181
182 class UninstallCommand(Command):
183 description = "Uninstalls the Python module"
184 user_options = [
185 ('pydir=', None, 'Specify the path to the binwalk python module to be removed.'),
186 ('pybin=', None, 'Specify the path to the binwalk executable to be removed.'),
187 ]
188
189 def initialize_options(self):
190 self.pydir = None
191 self.pybin = None
192
193 def finalize_options(self):
194 pass
195
196 def run(self):
197 remove_binwalk_module(self.pydir, self.pybin)
198
199
200 class CleanCommand(Command):
201 description = "Clean Python build directories"
202 user_options = []
203
204 def initialize_options(self):
205 pass
206
207 def finalize_options(self):
208 pass
209
210 def run(self):
211 try:
212 remove_tree(os.path.join(MODULE_DIRECTORY, "build"))
213 except KeyboardInterrupt as e:
214 raise e
215 except Exception:
216 pass
217
218 try:
219 remove_tree(os.path.join(MODULE_DIRECTORY, "dist"))
220 except KeyboardInterrupt as e:
221 raise e
222 except Exception:
223 pass
224
225
226 class AutoCompleteCommand(Command):
227 description = "Install bash autocomplete file"
228 user_options = []
229
230 def initialize_options(self):
231 pass
232
233 def finalize_options(self):
234 pass
235
236 def run(self):
237 options = []
238 autocomplete_file_path = "/etc/bash_completion.d/%s" % MODULE_NAME
239 auto_complete = '''_binwalk()
240 {
241 local cur prev opts
242 COMPREPLY=()
243 cur="${COMP_WORDS[COMP_CWORD]}"
244 prev="${COMP_WORDS[COMP_CWORD-1]}"
245 opts="%s"
246
247 if [[ ${cur} == -* ]] ; then
248 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
249 return 0
250 fi
251 }
252 complete -F _binwalk binwalk'''
253
254 (out, err) = subprocess.Popen(["binwalk", "--help"], stdout=subprocess.PIPE).communicate()
255 for line in out.splitlines():
256 if b'--' in line:
257 long_opt = line.split(b'--')[1].split(b'=')[0].split()[0].strip()
258 options.append('--' + long_opt.decode('utf-8'))
259
260 with open(autocomplete_file_path, "w") as fp:
261 fp.write(auto_complete % ' '.join(options))
262
263 this_directory = os.path.abspath(os.path.dirname(__file__))
264 with io.open(os.path.join(this_directory, 'README.md'), encoding='utf-8') as f:
265 long_description = f.read()
266
267 # Install the module, script, and support files
268 setup(
269 name=MODULE_NAME,
270 version=MODULE_VERSION,
271 description="Firmware analysis tool",
272 long_description=long_description,
273 long_description_content_type='text/markdown',
274 author="Craig Heffner",
275 url="https://github.com/OSPG/%s" % MODULE_NAME,
276 requires=[],
277 python_requires=">=3",
278 package_dir={"": "src"},
279 packages=find_packages("src"),
280 include_package_data=True,
281 entry_points={
282 'console_scripts': ['binwalk=binwalk.__main__:main'],
283 },
284 cmdclass={
285 'clean': CleanCommand,
286 'uninstall': UninstallCommand,
287 'idainstall': IDAInstallCommand,
288 'idauninstall': IDAUnInstallCommand,
289 'autocomplete' : AutoCompleteCommand,
290 },
291 )