diff mbox series

patman: Update to use absolute imports

Message ID 20200321084217.1.Ia00f61964f3e38a72f4eef24878c0756cda8bf9e@changeid
State New
Headers show
Series patman: Update to use absolute imports | expand

Commit Message

Simon Glass March 21, 2020, 2:42 p.m. UTC
Use absolute imports in patman so that installing patman is easier and it
can run with its modules being in the 'patman' subdirectory.

Rename patman.py since Python does not like the Python module name being
the same as the module directory.

Add the required path to sys.path() so that running patman locally works.

Signed-off-by: Simon Glass <sjg at chromium.org>
Reviewed-by: Mike Frysinger <vapier at chromium.org>
---

 tools/patman/checkpatch.py          |  8 +++++---
 tools/patman/command.py             |  5 +++--
 tools/patman/func_test.py           |  8 ++++----
 tools/patman/get_maintainer.py      |  5 +++--
 tools/patman/gitutil.py             | 12 ++++++------
 tools/patman/{patman.py => main.py} | 27 ++++++++++++++-------------
 tools/patman/patchstream.py         |  8 ++++----
 tools/patman/patman                 |  2 +-
 tools/patman/project.py             |  2 +-
 tools/patman/series.py              | 10 +++++-----
 tools/patman/settings.py            |  6 +++---
 tools/patman/test.py                | 10 +++++-----
 tools/patman/tools.py               |  4 ++--
 tools/patman/tout.py                |  2 +-
 14 files changed, 57 insertions(+), 52 deletions(-)
 rename tools/patman/{patman.py => main.py} (93%)
diff mbox series

Patch

diff --git a/tools/patman/checkpatch.py b/tools/patman/checkpatch.py
index d47ea438b7..795b519314 100644
--- a/tools/patman/checkpatch.py
+++ b/tools/patman/checkpatch.py
@@ -3,12 +3,14 @@ 
 #
 
 import collections
-import command
-import gitutil
 import os
 import re
 import sys
-import terminal
+
+from patman import command
+from patman import gitutil
+from patman import terminal
+from patman import tools
 
 def FindCheckPatch():
     top_level = gitutil.GetTopLevel()
diff --git a/tools/patman/command.py b/tools/patman/command.py
index 5fbd2c4a3e..e67ac159e5 100644
--- a/tools/patman/command.py
+++ b/tools/patman/command.py
@@ -3,8 +3,9 @@ 
 #
 
 import os
-import cros_subprocess
-import tools
+
+from patman import cros_subprocess
+from patman import tools
 
 """Shell command ease-ups for Python."""
 
diff --git a/tools/patman/func_test.py b/tools/patman/func_test.py
index 76319fff37..b8dee18725 100644
--- a/tools/patman/func_test.py
+++ b/tools/patman/func_test.py
@@ -17,10 +17,10 @@  try:
 except ImportError:
     from io import StringIO
 
-import gitutil
-import patchstream
-import settings
-import tools
+from patman import gitutil
+from patman import patchstream
+from patman import settings
+from patman import tools
 
 
 @contextlib.contextmanager
diff --git a/tools/patman/get_maintainer.py b/tools/patman/get_maintainer.py
index 0ffb55a821..473f0feebf 100644
--- a/tools/patman/get_maintainer.py
+++ b/tools/patman/get_maintainer.py
@@ -2,10 +2,11 @@ 
 # Copyright (c) 2012 The Chromium OS Authors.
 #
 
-import command
-import gitutil
 import os
 
+from patman import command
+from patman import gitutil
+
 def FindGetMaintainer():
     """Look for the get_maintainer.pl script.
 
diff --git a/tools/patman/gitutil.py b/tools/patman/gitutil.py
index a2a225c6b9..770a051014 100644
--- a/tools/patman/gitutil.py
+++ b/tools/patman/gitutil.py
@@ -2,17 +2,17 @@ 
 # Copyright (c) 2011 The Chromium OS Authors.
 #
 
-import command
 import re
 import os
-import series
 import subprocess
 import sys
-import terminal
 
-import checkpatch
-import settings
-import tools
+from patman import checkpatch
+from patman import command
+from patman import series
+from patman import settings
+from patman import terminal
+from patman import tools
 
 # True to use --no-decorate - we check this in Setup()
 use_no_decorate = True
diff --git a/tools/patman/patman.py b/tools/patman/main.py
similarity index 93%
rename from tools/patman/patman.py
rename to tools/patman/main.py
index cf53e532dd..8cce567c6e 100755
--- a/tools/patman/patman.py
+++ b/tools/patman/main.py
@@ -12,19 +12,20 @@  import re
 import sys
 import unittest
 
+if __name__ == "__main__":
+    # Allow 'from patman import xxx to work'
+    our_path = os.path.dirname(os.path.realpath(__file__))
+    sys.path.append(os.path.join(our_path, '..'))
+
 # Our modules
-try:
-    from patman import checkpatch, command, gitutil, patchstream, \
-        project, settings, terminal, test
-except ImportError:
-    import checkpatch
-    import command
-    import gitutil
-    import patchstream
-    import project
-    import settings
-    import terminal
-    import test
+from patman import checkpatch
+from patman import command
+from patman import gitutil
+from patman import patchstream
+from patman import project
+from patman import settings
+from patman import terminal
+from patman import test
 
 
 parser = OptionParser()
@@ -85,7 +86,7 @@  if __name__ != "__main__":
 # Run our meagre tests
 elif options.test:
     import doctest
-    import func_test
+    from patman import func_test
 
     sys.argv = [sys.argv[0]]
     result = unittest.TestResult()
diff --git a/tools/patman/patchstream.py b/tools/patman/patchstream.py
index df3eb7483b..405297505c 100644
--- a/tools/patman/patchstream.py
+++ b/tools/patman/patchstream.py
@@ -9,10 +9,10 @@  import re
 import shutil
 import tempfile
 
-import command
-import commit
-import gitutil
-from series import Series
+from patman import command
+from patman import commit
+from patman import gitutil
+from patman.series import Series
 
 # Tags that we detect and remove
 re_remove = re.compile('^BUG=|^TEST=|^BRANCH=|^Review URL:'
diff --git a/tools/patman/patman b/tools/patman/patman
index 6cc3d7a56a..11a5d8e18a 120000
--- a/tools/patman/patman
+++ b/tools/patman/patman
@@ -1 +1 @@ 
-patman.py
\ No newline at end of file
+main.py
\ No newline at end of file
diff --git a/tools/patman/project.py b/tools/patman/project.py
index 1d9cfc0625..2dfc303729 100644
--- a/tools/patman/project.py
+++ b/tools/patman/project.py
@@ -4,7 +4,7 @@ 
 
 import os.path
 
-import gitutil
+from patman import gitutil
 
 def DetectProject():
     """Autodetect the name of the current project.
diff --git a/tools/patman/series.py b/tools/patman/series.py
index a15f7625ed..9baf5ce976 100644
--- a/tools/patman/series.py
+++ b/tools/patman/series.py
@@ -7,11 +7,11 @@  from __future__ import print_function
 import itertools
 import os
 
-import get_maintainer
-import gitutil
-import settings
-import terminal
-import tools
+from patman import get_maintainer
+from patman import gitutil
+from patman import settings
+from patman import terminal
+from patman import tools
 
 # Series-xxx tags that we understand
 valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
diff --git a/tools/patman/settings.py b/tools/patman/settings.py
index 5dc83a8500..8acfb92c09 100644
--- a/tools/patman/settings.py
+++ b/tools/patman/settings.py
@@ -12,9 +12,9 @@  except:
 import os
 import re
 
-import command
-import gitutil
-import tools
+from patman import command
+from patman import gitutil
+from patman import tools
 
 """Default settings per-project.
 
diff --git a/tools/patman/test.py b/tools/patman/test.py
index 889e186606..e7f709e34c 100644
--- a/tools/patman/test.py
+++ b/tools/patman/test.py
@@ -8,11 +8,11 @@  import os
 import tempfile
 import unittest
 
-import checkpatch
-import gitutil
-import patchstream
-import series
-import commit
+from patman import checkpatch
+from patman import gitutil
+from patman import patchstream
+from patman import series
+from patman import commit
 
 
 class TestPatch(unittest.TestCase):
diff --git a/tools/patman/tools.py b/tools/patman/tools.py
index 3feddb292f..7b0840d5c3 100644
--- a/tools/patman/tools.py
+++ b/tools/patman/tools.py
@@ -5,7 +5,6 @@ 
 
 from __future__ import print_function
 
-import command
 import glob
 import os
 import shutil
@@ -13,7 +12,8 @@  import struct
 import sys
 import tempfile
 
-import tout
+from patman import command
+from patman import tout
 
 # Output directly (generally this is temporary)
 outdir = None
diff --git a/tools/patman/tout.py b/tools/patman/tout.py
index 2a384851b0..834d4f4db7 100644
--- a/tools/patman/tout.py
+++ b/tools/patman/tout.py
@@ -8,7 +8,7 @@  from __future__ import print_function
 
 import sys
 
-import terminal
+from patman import terminal
 
 # Output verbosity levels that we support
 ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6)