enable isort & fixup code

This helps enforce more style rules not covered by black.

Change-Id: I5006c23fda8054735c8442f4150ee384bed18db5
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repohooks/+/573341
Reviewed-by: Ram Peri <ramperi@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
Commit-Queue: Mike Frysinger <vapier@google.com>
diff --git a/pre-upload.py b/pre-upload.py
index a25cfb9..cb0cf15 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -44,10 +44,10 @@
 # relative imports because this is an executable program, not a module.
 # pylint: disable=wrong-import-position
 import rh
-import rh.results
 import rh.config
 import rh.git
 import rh.hooks
+import rh.results
 import rh.terminal
 import rh.utils
 
diff --git a/pyproject.toml b/pyproject.toml
index 2a923fc..143537a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -17,6 +17,26 @@
 target-version = ['py36']
 exclude = 'tools/cpplint\.py$'
 
+# https://pycqa.github.io/isort/docs/configuration/options.html
+[tool.isort]
+# Be compatible with `black` since it also matches what we want.
+profile = "black"
+
+line_length = 80
+length_sort = false
+force_single_line = true
+lines_after_imports = 2
+from_first = false
+case_sensitive = false
+force_sort_within_sections = true
+order_by_type = false
+
+# Allow importing multiple classes on a single line from these modules.
+# https://google.github.io/styleguide/pyguide#s2.2-imports
+single_line_exclusions = ["abc", "typing"]
+
+extend_skip = ["tools/cpplint.py"]
+
 [tool.pytest.ini_options]
 python_files = "*_unittest.py"
 markers = """
diff --git a/rh/config_unittest.py b/rh/config_unittest.py
index 699c359..80423af 100755
--- a/rh/config_unittest.py
+++ b/rh/config_unittest.py
@@ -30,8 +30,8 @@
 # We have to import our local modules after the sys.path tweak.  We can't use
 # relative imports because this is an executable program, not a module.
 # pylint: disable=wrong-import-position
-import rh.hooks
 import rh.config
+import rh.hooks
 
 
 class PreUploadConfigTests(unittest.TestCase):
diff --git a/rh/shell.py b/rh/shell.py
index 9fe4d5c..1d51197 100644
--- a/rh/shell.py
+++ b/rh/shell.py
@@ -14,8 +14,8 @@
 
 """Functions for working with shell code."""
 
-from pathlib import Path
 import pathlib
+from pathlib import Path
 import sys
 
 
diff --git a/run_tests b/run_tests
index 742a222..8749c2f 100755
--- a/run_tests
+++ b/run_tests
@@ -102,12 +102,32 @@
     ).returncode
 
 
+def run_isort():
+    """Returns the exit code from isort."""
+    argv = ["--version-number"]
+    log_cmd("isort", argv)
+    subprocess.run(
+        [sys.executable, "-m", "isort"] + argv,
+        check=True,
+        cwd=ROOT_DIR,
+    )
+
+    argv = ["--check", ROOT_DIR]
+    log_cmd("isort", argv)
+    return subprocess.run(
+        [sys.executable, "-m", "isort"] + argv,
+        check=False,
+        cwd=ROOT_DIR,
+    ).returncode
+
+
 def main(argv):
     """The main entry."""
     checks = (
         functools.partial(run_pytest, argv),
         run_black,
         run_pylint,
+        run_isort,
     )
     # Run all the tests all the time to get full feedback.  Don't exit on the
     # first error as that makes it more difficult to iterate in the CQ.
diff --git a/tools/android_test_mapping_format.py b/tools/android_test_mapping_format.py
index 68caa94..07fb8ce 100755
--- a/tools/android_test_mapping_format.py
+++ b/tools/android_test_mapping_format.py
@@ -40,6 +40,7 @@
 # pylint: disable=wrong-import-position
 import rh.git
 
+
 _IMPORTS = "imports"
 _NAME = "name"
 _OPTIONS = "options"
diff --git a/tools/pylint.py b/tools/pylint.py
index ebf9575..1f3d7d2 100755
--- a/tools/pylint.py
+++ b/tools/pylint.py
@@ -19,8 +19,8 @@
 import errno
 import os
 from pathlib import Path
-import sys
 import subprocess
+import sys
 from typing import Dict, List, Optional, Set