Commit aba30b02 by devttys0

Added dependency feature to modules.

parent 6106d337
import io
import sys
import inspect
import argparse
import binwalk.common
from binwalk.compat import *
......@@ -42,15 +43,69 @@ class ModuleKwarg(object):
self.default = default
self.description = description
def list_modules():
def process_kwargs(module, kwargs):
return Modules(dummy=True).kwargs(module, kwargs)
class Modules(object):
def __init__(self, dummy=False):
self.config = None
if not dummy:
from binwalk.modules.configuration import Configuration
self.config = self.load(Configuration)
def list(self):
import binwalk.modules
objects = []
for (name, obj) in inspect.getmembers(binwalk.modules):
if inspect.isclass(obj) and hasattr(obj, "run"):
objects.append(obj)
return objects
def run(self, module):
results = None
obj = self.load()
if obj.enabled:
try:
obj.run()
except AttributeError:
pass
def process_argv(module, config=None, argv=sys.argv[1:]):
return results
def load(self, module):
kwargs = self.argv(module)
kwargs.update(self.dependencies(module))
return module(**kwargs)
def dependencies(self, module):
kwargs = {}
if hasattr(module, "DEPENDS"):
# Disable output when modules are loaded as dependencies
orig_log = self.config.display.log
orig_quiet = self.config.display.quiet
self.config.display.log = False
self.config.display.quiet = True
for (kwarg, mod) in module.DEPENDS:
kwargs[kwarg] = self.run(mod)
self.config.display.log = orig_log
self.config.display.quiet = orig_quiet
return kwargs
def argv(self, module, argv=sys.argv[1:]):
'''
Processes argv for any options specific to the specified module.
@module - The module to process argv for.
@config - An instance of the binwalk.modules.configuration.Configuration class.
@argv - A list of command line arguments (excluding argv[0]).
Returns a dictionary of kwargs for the specified module.
......@@ -117,15 +172,21 @@ def process_argv(module, config=None, argv=sys.argv[1:]):
else:
kwargs[name] = value
else:
raise Exception("binwalk.module.process_argv: %s has no attribute 'CLI'" % str(module))
raise Exception("binwalk.module.argv: %s has no attribute 'CLI'" % str(module))
if config is not None and not has_key(kwargs, 'config'):
kwargs['config'] = config
if self.config is not None and not has_key(kwargs, 'config'):
kwargs['config'] = self.config
return kwargs
# If some command line arguments for this module were parsed, then set it to enabled.
# Else, disable it by default.
if kwargs:
kwargs['enabled'] = True
else:
kwargs['enabled'] = False
return kwargs
def process_kwargs(module, kwargs):
def kwargs(self, module, kwargs):
'''
Processes a module's kwargs. All modules should use this for kwarg processing.
......@@ -146,5 +207,5 @@ def process_kwargs(module, kwargs):
if has_key(kwargs, 'config'):
setattr(module, 'config', kwargs['config'])
else:
raise Exception("binwalk.module.parse_module_kwargs: %s has no attribute 'KWARGS'" % str(module))
raise Exception("binwalk.module.process_kwargs: %s has no attribute 'KWARGS'" % str(module))
from configuration import Configuration
from hashmatch import HashFile, HashFiles, HashDirectories
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