Commit 27a8a4d6 by devttys0

Consolidated code into the binwalk.module.Module parent class.

parent 3040e25b
......@@ -86,13 +86,23 @@ class Result(object):
class Error(Result):
'''
A subclass of binwalk.module.Result.
Accepts all the same kwargs as binwalk.module.Result, but the following are also suggested:
'''
def __init__(self, **kwargs):
'''
Accepts all the same kwargs as binwalk.module.Result, but the following are also added:
@exception - In case of an exception, this is the exception object.
__init__ returns None.
Returns None.
'''
pass
self.exception = None
Result.__init__(self, **kwargs)
if self.exception:
sys.stderr.write(str(self.exception) + "\n")
elif self.description:
sys.stderr.write(self.description + "\n")
class Module(object):
'''
......@@ -215,6 +225,8 @@ class Module(object):
'''
e = Error(**kwargs)
self.errors.append(e)
if e.exception:
raise e.exception
def main(self):
'''
......@@ -222,7 +234,13 @@ class Module(object):
Returns the value returned from self.run.
'''
try:
self.init()
except KeyboardInterrupt as e:
raise e
except Exception as e:
self.error(exception=e)
return False
self.config.display.format_strings(self.HEADER_FORMAT, self.RESULT_FORMAT)
if type(self.HEADER) == type([]):
......@@ -231,7 +249,15 @@ class Module(object):
self.config.display.header(self.HEADER)
self._plugins_pre_scan()
try:
retval = self.run()
except KeyboardInterrupt as e:
raise e
except Exception as e:
self.error(exception=e)
return False
self._plugins_post_scan()
self.config.display.footer()
......
......@@ -5,7 +5,7 @@ from binwalk.common import BlockFile
class Plotter(binwalk.module.Module):
'''
Base class for plotting binaries in Qt.
Base class for visualizing binaries in Qt.
Other plotter classes are derived from this.
'''
VIEW_DISTANCE = 1024
......@@ -41,6 +41,7 @@ class Plotter(binwalk.module.Module):
binwalk.module.ModuleKwarg(name='show_grids', default=False),
]
# There isn't really any useful data to print to console. Disable header and result output.
HEADER = None
RESULT = None
......@@ -61,7 +62,7 @@ class Plotter(binwalk.module.Module):
self.MAX_PLOT_POINTS = self.MAX_3D_PLOT_POINTS
self._generate_data_point = self._generate_3d_data_point
else:
raise Exception("Invalid Plotter axis specified: %d. Must be one of: [2, 3]." % self.axis)
raise Exception("Invalid Plotter axis specified: %d. Must be one of: [2,3]" % self.axis)
if not self.max_points:
self.max_points = self.MAX_PLOT_POINTS
......@@ -78,7 +79,7 @@ class Plotter(binwalk.module.Module):
Print console messages. For internal use only.
'''
if self.verbose:
print (message)
print(message)
def _generate_plot_points(self, data_points):
'''
......
......@@ -142,8 +142,10 @@ class Configuration(binwalk.module.Module):
# Make sure we can open the target files
try:
self.target_files.append(binwalk.common.BlockFile(tfile, length=self.length, offset=self.offset))
except KeyboardInterrupt as e:
raise e
except Exception as e:
sys.stderr.write("Cannot open file : %s\n" % str(e))
self.error(description="Cannot open file : %s\n" % str(e))
# Unless -O was specified, don't run the scan unless we are able to scan all specified files
if len(self.target_files) != len(self.files) and not self.skip_unopened:
......@@ -192,7 +194,7 @@ class Update(object):
self.update_zlib()
self.update_compressd()
def _do_update_from_svn(self, prefix, fname):
def _do_update_from_git(self, prefix, fname):
'''
Updates the specified file to the latest version of that file in SVN.
......@@ -217,8 +219,10 @@ class Update(object):
data = urllib2.urlopen(url).read()
open(self.config.paths['system'][fname], "wb").write(data)
except KeyboardInterrupt as e:
raise e
except Exception as e:
raise Exception("Update._do_update_from_svn failed to update file '%s': %s" % (url, str(e)))
raise Exception("Update._do_update_from_git failed to update file '%s': %s" % (url, str(e)))
def update_binwalk(self):
'''
......@@ -226,7 +230,7 @@ class Update(object):
Returns None.
'''
self._do_update_from_svn(self.MAGIC_PREFIX, self.config.BINWALK_MAGIC_FILE)
self._do_update_from_git(self.MAGIC_PREFIX, self.config.BINWALK_MAGIC_FILE)
def update_bincast(self):
'''
......@@ -234,7 +238,7 @@ class Update(object):
Returns None.
'''
self._do_update_from_svn(self.MAGIC_PREFIX, self.config.BINCAST_MAGIC_FILE)
self._do_update_from_git(self.MAGIC_PREFIX, self.config.BINCAST_MAGIC_FILE)
def update_binarch(self):
'''
......@@ -242,7 +246,7 @@ class Update(object):
Returns None.
'''
self._do_update_from_svn(self.MAGIC_PREFIX, self.config.BINARCH_MAGIC_FILE)
self._do_update_from_git(self.MAGIC_PREFIX, self.config.BINARCH_MAGIC_FILE)
def update_zlib(self):
'''
......@@ -250,7 +254,7 @@ class Update(object):
Returns None.
'''
self._do_update_from_svn(self.MAGIC_PREFIX, self.config.ZLIB_MAGIC_FILE)
self._do_update_from_git(self.MAGIC_PREFIX, self.config.ZLIB_MAGIC_FILE)
def update_compressd(self):
'''
......@@ -258,7 +262,7 @@ class Update(object):
Returns None.
'''
self._do_update_from_svn(self.MAGIC_PREFIX, self.config.COMPRESSD_MAGIC_FILE)
self._do_update_from_git(self.MAGIC_PREFIX, self.config.COMPRESSD_MAGIC_FILE)
def update_extract(self):
'''
......@@ -266,6 +270,6 @@ class Update(object):
Returns None.
'''
self._do_update_from_svn(self.CONFIG_PREFIX, self.config.EXTRACT_FILE)
self._do_update_from_git(self.CONFIG_PREFIX, self.config.EXTRACT_FILE)
......@@ -216,6 +216,8 @@ class HexDiff(binwalk.module.Module):
for j in range(0, block):
try:
self._build_block("%.2X " % ord(data[f.name][j+i]), highlight=diff_same[j])
except KeyboardInterrupt as e:
raise e
except Exception as e:
self._build_block(" ")
......
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