convert remaining __file__ usage to pathlib Bug: None Test: `./run_tests` Change-Id: Iaaf7b0c8ce388f6e39123b16bf6c71d3a51edd0f Reviewed-on: https://gerrit-review.googlesource.com/c/git-repohooks/+/565942 Tested-by: Mike Frysinger <vapier@google.com> Reviewed-by: Raul Rangel <rrangel@google.com>
diff --git a/rh/config_test.py b/rh/config_test.py index b63a657..a34b889 100755 --- a/rh/config_test.py +++ b/rh/config_test.py
@@ -17,12 +17,15 @@ import argparse import os +from pathlib import Path import re import sys -REPOTOOLS = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -REPO_ROOT = os.path.dirname(os.path.dirname(REPOTOOLS)) +THIS_FILE = Path(__file__).resolve() +THIS_DIR = THIS_FILE.parent +# This default assumes an Android checkout. +REPO_ROOT = THIS_DIR.parent.parent.parent def assertEqual(msg, exp, actual):
diff --git a/rh/hooks.py b/rh/hooks.py index 506feb1..538d6b0 100644 --- a/rh/hooks.py +++ b/rh/hooks.py
@@ -343,10 +343,10 @@ # Where helper programs exist. -TOOLS_DIR = os.path.realpath(__file__ + "/../../tools") +TOOLS_DIR = THIS_DIR.parent / "tools" -def get_helper_path(tool): +def get_helper_path(tool: str) -> str: """Return the full path to the helper |tool|.""" return os.path.join(TOOLS_DIR, tool) @@ -1338,13 +1338,13 @@ TOOL_PATHS = { "aidl-format": "aidl-format", "alint": "alint", - "android-test-mapping-format": os.path.join( - TOOLS_DIR, "android_test_mapping_format.py" + "android-test-mapping-format": get_helper_path( + "android_test_mapping_format.py" ), "black": "black", "bpfmt": "bpfmt", "clang-format": "clang-format", - "cpplint": os.path.join(TOOLS_DIR, "cpplint.py"), + "cpplint": get_helper_path("cpplint.py"), "git-clang-format": "git-clang-format", "gofmt": "gofmt", "google-java-format": "google-java-format",
diff --git a/tools/pylint.py b/tools/pylint.py index 92b0dd3..ebf9575 100755 --- a/tools/pylint.py +++ b/tools/pylint.py
@@ -18,6 +18,7 @@ import argparse import errno import os +from pathlib import Path import sys import subprocess from typing import Dict, List, Optional, Set @@ -31,9 +32,9 @@ ), f"Python 3.6 or newer is required; found {sys.version}" -DEFAULT_PYLINTRC_PATH = os.path.join( - os.path.dirname(os.path.realpath(__file__)), "pylintrc" -) +THIS_FILE = Path(__file__).resolve() +THIS_DIR = THIS_FILE.parent +DEFAULT_PYLINTRC_PATH = str(THIS_DIR / "pylintrc") def run_lint( @@ -69,9 +70,11 @@ ) except OSError as e: if e.errno == errno.ENOENT: - print(f"{__file__}: unable to run `{cmd[0]}`: {e}", file=sys.stderr) print( - f"{__file__}: Try installing pylint: sudo apt-get install " + f"{THIS_FILE}: unable to run `{cmd[0]}`: {e}", file=sys.stderr + ) + print( + f"{THIS_FILE}: Try installing pylint: sudo apt-get install " f"{os.path.basename(cmd[0])}", file=sys.stderr, ) @@ -80,7 +83,7 @@ raise if result.returncode: - print(f"{__file__}: Using pylintrc: {pylintrc}") + print(f"{THIS_FILE}: Using pylintrc: {pylintrc}") print(result.stdout) return False @@ -101,7 +104,7 @@ if not key.startswith(rootdir): sys.exit( - f"{__file__}: The search directory {key} is outside the " + f"{THIS_FILE}: The search directory {key} is outside the " f"repo dir {rootdir}" ) @@ -155,7 +158,7 @@ pylintrc_map[rc_dir].add(f) break else: - sys.exit(f"{__file__}: Failed to map file {f} to a pylintrc file.") + sys.exit(f"{THIS_FILE}: Failed to map file {f} to a pylintrc file.") return pylintrc_map @@ -195,7 +198,7 @@ first = False assert os.path.abspath(rc_dir) == os.path.abspath( "." - ), f"{__file__}: pylintrc in top dir not first in list" + ), f"{THIS_FILE}: pylintrc in top dir not first in list" if not os.path.exists(pylintrc): pylintrc = DEFAULT_PYLINTRC_PATH if not run_lint(