binman: Avoid setting sys.path globally

At present we set the Python path at the start of binman so we can read
modules in the 'etype' directory. This is a bit messy since it affects
'import' statements through binman.

Adjust the code to set the path locally, just where it is needed. Move
the 'entry' module in with the other base modules to help with this. It
makes more sense here anyway since it does not implement an entry type.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2018-06-01 09:38:15 -06:00
parent 25ac0e61fe
commit badf0ec6e4
2 changed files with 10 additions and 3 deletions

View file

@ -23,9 +23,6 @@ for dirname in ['../patman', '../dtoc', '..']:
# Bring in the libfdt module # Bring in the libfdt module
sys.path.insert(0, 'scripts/dtc/pylibfdt') sys.path.insert(0, 'scripts/dtc/pylibfdt')
# Also allow entry-type modules to be brought in from the etype directory.
sys.path.insert(0, os.path.join(our_path, 'etype'))
import cmdline import cmdline
import command import command
import control import control

View file

@ -14,10 +14,14 @@ except:
have_importlib = False have_importlib = False
import fdt_util import fdt_util
import os
import sys
import tools import tools
modules = {} modules = {}
our_path = os.path.dirname(os.path.realpath(__file__))
class Entry(object): class Entry(object):
"""An Entry in the section """An Entry in the section
@ -80,8 +84,12 @@ class Entry(object):
module_name = module_name.split('@')[0] module_name = module_name.split('@')[0]
module = modules.get(module_name) module = modules.get(module_name)
# Also allow entry-type modules to be brought in from the etype directory.
# Import the module if we have not already done so. # Import the module if we have not already done so.
if not module: if not module:
old_path = sys.path
sys.path.insert(0, os.path.join(our_path, 'etype'))
try: try:
if have_importlib: if have_importlib:
module = importlib.import_module(module_name) module = importlib.import_module(module_name)
@ -90,6 +98,8 @@ class Entry(object):
except ImportError: except ImportError:
raise ValueError("Unknown entry type '%s' in node '%s'" % raise ValueError("Unknown entry type '%s' in node '%s'" %
(etype, node.path)) (etype, node.path))
finally:
sys.path = old_path
modules[module_name] = module modules[module_name] = module
# Call its constructor to get the object we want. # Call its constructor to get the object we want.