Commit d3a03b2d by devttys0

Updated setup.py

parent 43c8dd8c
...@@ -2,41 +2,45 @@ ...@@ -2,41 +2,45 @@
from __future__ import print_function from __future__ import print_function
import os import os
import sys import sys
from distutils.core import setup import tempfile
import subprocess
from distutils.core import setup, Command
from distutils.dir_util import remove_tree from distutils.dir_util import remove_tree
MODULE_NAME = "binwalk"
# Python2/3 compliance # Python2/3 compliance
try: try:
raw_input raw_input
except NameError: except NameError:
raw_input = input raw_input = input
def cleanup_build_directory(): # This is super hacky.
# Requires to chdir into the src directory first if "--yes" in sys.argv:
try: sys.argv.pop(sys.argv.index("--yes"))
remove_tree("build") IGNORE_WARNINGS = True
except KeyboardInterrupt as e: else:
raise e IGNORE_WARNINGS = False
except Exception:
pass # cd into the src directory, no matter where setup.py was invoked from
os.chdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), "src"))
def which(command):
# /usr/local/bin is usually the default install path, though it may not be in $PATH
usr_local_bin = os.path.sep.join([os.path.sep, 'usr', 'local', 'bin', command])
def cleanup_module_directory():
# Installing doesn't remove old files that may have been deleted from the module.
if "install" in sys.argv:
try:
import binwalk
for path in binwalk.__path__:
try: try:
remove_tree(path + os.path.sep + "*") location = subprocess.Popen(["which", command], shell=False, stdout=subprocess.PIPE).communicate()[0].strip()
except OSError as e:
pass
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
raise e raise e
except Exception: except Exception as e:
pass pass
if not location and os.path.exists(usr_local_bin):
location = usr_local_bin
return location
# Change to the binwalk src directory
def warning(lines, terminate=True, prompt=True): def warning(lines, terminate=True, prompt=True):
WIDTH = 115 WIDTH = 115
...@@ -55,102 +59,105 @@ def warning(lines, terminate=True, prompt=True): ...@@ -55,102 +59,105 @@ def warning(lines, terminate=True, prompt=True):
if terminate: if terminate:
sys.exit(1) sys.exit(1)
# This is super hacky. class UninstallCommand(Command):
if "--yes" in sys.argv: description = "Uninstalls the Python module"
sys.argv.pop(sys.argv.index("--yes")) user_options = []
IGNORE_WARNINGS = True
else:
IGNORE_WARNINGS = False
# cd into the src directory, no matter where setup.py was invoked from
os.chdir(os.path.join(os.path.dirname(os.path.realpath(__file__)), "src"))
print("checking pre-requisites") def initialize_options(self):
try: pass
import magic
try:
magic.MAGIC_NO_CHECK_TEXT
except AttributeError as e:
msg = ["Pre-requisite failure: " + str(e),
"It looks like you have an old or incompatible magic module installed.",
"Please install the official python-magic module, or download and install it from source: ftp://ftp.astron.com/pub/file/"
]
warning(msg) def finalize_options(self):
except ImportError as e: pass
msg = ["Pre-requisite failure:", str(e),
"Please install the python-magic module, or download and install it from source: ftp://ftp.astron.com/pub/file/",
]
warning(msg) def run(self):
try:
import binwalk
for path in binwalk.__path__:
try:
remove_tree(path)
except OSError as e:
pass
except KeyboardInterrupt as e:
raise e
except Exception as e:
pass
try: script_path = which(MODULE_NAME)
import pyqtgraph if script_path:
except ImportError as e: try:
msg = ["Pre-requisite check warning: " + str(e), print("removing '%s'" % script_path)
"To take advantage of this tool's graphing capabilities, please install the pyqtgraph module.", os.unlink(script_path)
] except KeyboardInterrupt as e:
pass
except Exception as e:
pass
warning(msg, prompt=True) class CleanCommand(Command):
description = "Clean Python build directories"
user_options = []
# Build / install C compression libraries def initialize_options(self):
c_lib_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "C") pass
c_lib_makefile = os.path.join(c_lib_dir, "Makefile")
working_directory = os.getcwd() def finalize_options(self):
os.chdir(c_lib_dir) pass
status = 0
if not os.path.exists(c_lib_makefile): def run(self):
status |= os.system("./configure") try:
remove_tree("build")
except KeyboardInterrupt as e:
raise e
except Exception:
pass
status |= os.system("make") try:
remove_tree("dist")
except KeyboardInterrupt as e:
raise e
except Exception:
pass
if status != 0: # Check pre-requisite Python modules during an install
msg = ["Build warning: failed to build C libraries.", if "install" in sys.argv:
"Some features will not work without these libraries." print("checking pre-requisites")
] try:
warning(msg, prompt=True) import pyqtgraph
elif "install" in sys.argv: import QtPyWhatever
if os.system("make install") != 0: except ImportError as e:
msg = ["Install warning: failed to install C libraries.", msg = ["Pre-requisite check warning: " + str(e),
"Some features will not work without these libraries." "To take advantage of this %s's graphing capabilities, please install this module." % MODULE_NAME,
] ]
warning(msg, prompt=True)
os.system("make distclean") warning(msg, prompt=True)
os.chdir(working_directory)
cleanup_build_directory()
cleanup_module_directory()
# Generate a new magic file from the files in the magic directory # Re-build the magic file during a build/install
print("creating binwalk magic file") if "install" in sys.argv or "build" in sys.argv:
magic_files = os.listdir("magic") # Generate a new magic file from the files in the magic directory
magic_files.sort() print("creating %s magic file" % MODULE_NAME)
fd = open("binwalk/magic/binwalk", "wb") magic_files = os.listdir("magic")
for magic in magic_files: magic_files.sort()
fd = open("%s/magic/%s" % (MODULE_NAME, MODULE_NAME), "wb")
for magic in magic_files:
fpath = os.path.join("magic", magic) fpath = os.path.join("magic", magic)
if os.path.isfile(fpath): if os.path.isfile(fpath):
fd.write(open(fpath, "rb").read()) fd.write(open(fpath, "rb").read())
fd.close() fd.close()
# The data files to install along with the binwalk module # The data files to install along with the module
install_data_files = ["magic/*", "config/*", "plugins/*", "modules/*", "core/*"] install_data_files = ["magic/*", "config/*", "plugins/*", "modules/*", "core/*"]
# Install the binwalk module, script and support files # Install the module, script, and support files
setup( name = "binwalk", setup(name = MODULE_NAME,
version = "2.0.0 beta", version = "2.0.0 beta",
description = "Firmware analysis tool", description = "Firmware analysis tool",
author = "Craig Heffner", author = "Craig Heffner",
url = "https://github.com/devttys0/binwalk", url = "https://github.com/devttys0/%s" % MODULE_NAME,
requires = ["magic", "pyqtgraph"], requires = ["magic", "pyqtgraph"],
packages = ["binwalk"], packages = [MODULE_NAME],
package_data = {"binwalk" : install_data_files}, package_data = {MODULE_NAME : install_data_files},
scripts = ["scripts/binwalk"], scripts = [os.path.join("scripts", MODULE_NAME)],
)
cleanup_build_directory() cmdclass = {'clean' : CleanCommand, 'uninstall' : UninstallCommand}
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment