[U-Boot,05/12] binman: Avoid setting sys.path globally

Message ID 20180516015258.187295-6-sjg@chromium.org
State New
Delegated to: Simon Glass
Headers show
  • binman: A few more features
Related show

Commit Message

Simon Glass May 16, 2018, 1:52 a.m.
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>

 tools/binman/binman.py            |  3 ---
 tools/binman/{etype => }/entry.py | 10 ++++++++++
 2 files changed, 10 insertions(+), 3 deletions(-)
 rename tools/binman/{etype => }/entry.py (96%)


diff --git a/tools/binman/binman.py b/tools/binman/binman.py
index fa2f551f554..d49402a977e 100755
--- a/tools/binman/binman.py
+++ b/tools/binman/binman.py
@@ -23,9 +23,6 @@  for dirname in ['../patman', '../dtoc', '..']:
 # Bring in the libfdt module
 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 command
 import control
diff --git a/tools/binman/etype/entry.py b/tools/binman/entry.py
similarity index 96%
rename from tools/binman/etype/entry.py
rename to tools/binman/entry.py
index cbcabe20582..5374178542e 100644
--- a/tools/binman/etype/entry.py
+++ b/tools/binman/entry.py
@@ -14,10 +14,14 @@  except:
     have_importlib = False
 import fdt_util
+import os
+import sys
 import tools
 modules = {}
+our_path = os.path.dirname(os.path.realpath(__file__))
 class Entry(object):
     """An Entry in the section
@@ -80,8 +84,12 @@  class Entry(object):
             module_name = module_name.split('@')[0]
         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.
         if not module:
+            old_path = sys.path
+            sys.path.insert(0, os.path.join(our_path, 'etype'))
                 if have_importlib:
                     module = importlib.import_module(module_name)
@@ -90,6 +98,8 @@  class Entry(object):
             except ImportError:
                 raise ValueError("Unknown entry type '%s' in node '%s'" %
                         (etype, node.path))
+            finally:
+                sys.path = old_path
             modules[module_name] = module
         # Call its constructor to get the object we want.