|
5 | 5 |
|
6 | 6 | import logging
|
7 | 7 | import os
|
8 |
| -import subprocess |
9 |
| -import sys |
10 |
| -from typing import Tuple |
11 | 8 |
|
12 |
| -_LOG = logging.getLogger(__name__) |
13 |
| - |
14 |
| - |
15 |
| -# We can't use `hsystem` as this is a bootstrapping script. |
16 |
| -def _system_to_string( |
17 |
| - cmd: str, abort_on_error: bool = True, verbose: bool = False |
18 |
| -) -> Tuple[int, str]: |
19 |
| - assert isinstance(cmd, str), "Type of '%s' is %s" % (str(cmd), type(cmd)) |
20 |
| - if verbose: |
21 |
| - print(f"> {cmd}") |
22 |
| - stdout = subprocess.PIPE |
23 |
| - stderr = subprocess.STDOUT |
24 |
| - with subprocess.Popen( |
25 |
| - cmd, shell=True, executable="/bin/bash", stdout=stdout, stderr=stderr |
26 |
| - ) as p: |
27 |
| - output = "" |
28 |
| - while True: |
29 |
| - line = p.stdout.readline().decode("utf-8") # type: ignore |
30 |
| - if not line: |
31 |
| - break |
32 |
| - # print((line.rstrip("\n"))) |
33 |
| - output += line |
34 |
| - p.stdout.close() # type: ignore |
35 |
| - rc = p.wait() |
36 |
| - if abort_on_error and rc != 0: |
37 |
| - msg = ( |
38 |
| - "cmd='%s' failed with rc='%s'" % (cmd, rc) |
39 |
| - ) + "\nOutput of the failing command is:\n%s" % output |
40 |
| - _LOG.error(msg) |
41 |
| - sys.exit(-1) |
42 |
| - return rc, output |
43 |
| - |
44 |
| - |
45 |
| -# We can't use `hgit` as this is a bootstrapping script. |
46 |
| -def _get_git_root_dir() -> str: |
47 |
| - """ |
48 |
| - Return the absolute path to the outermost Git repository root. |
| 9 | +# This can be imported because this module is in the same dir as the script |
| 10 | +# being executed. |
| 11 | +import thin_client_utils as tcu # noqa: E402 |
49 | 12 |
|
50 |
| - If inside a Git submodule, this returns the parent (superproject) |
51 |
| - root. Otherwise, it returns the current repository's root. |
52 |
| - :return: absolute path to the outermost Git repository root |
53 |
| - """ |
54 |
| - cmd = "git rev-parse --show-superproject-working-tree --show-toplevel | head -n1" |
55 |
| - _, git_root_dir = _system_to_string(cmd) |
56 |
| - git_root_dir = git_root_dir.strip() |
57 |
| - return git_root_dir |
58 |
| - |
59 |
| - |
60 |
| -# We need to tweak `PYTHONPATH` directly since we are bootstrapping the system. |
61 |
| -sys.path.append("helpers_root/dev_scripts_helpers/thin_client") |
62 |
| -import thin_client_utils as tcu |
63 |
| - |
64 |
| -sys.path.append("helpers_root/helpers") |
| 13 | +# The `tcu` module adds root of helpers (or `helpers_root` when used in as |
| 14 | +# module) to the path, thus allowing imports from helpers. |
| 15 | +import helpers.hgit as hgit |
65 | 16 | import helpers.repo_config_utils as hrecouti
|
66 | 17 |
|
| 18 | +_LOG = logging.getLogger(__name__) |
| 19 | + |
67 | 20 | # Get the real file path rather than the symlink path.
|
68 | 21 | current_file_path = os.path.realpath(__file__)
|
69 | 22 | current_dir = os.path.dirname(current_file_path)
|
70 |
| -# Change to the repo directory so that it can find the repo config. |
| 23 | +# Change to the directory where the file is located so it can find its way to |
| 24 | +# the Git root. |
| 25 | +# This is necessary when the script is symlinked (e.g., `~/go_cmamp.py`) and |
| 26 | +# executed from a different directory. |
71 | 27 | os.chdir(current_dir)
|
72 | 28 |
|
73 |
| -# Change to the outermost Git repository root. |
74 |
| -git_root_dir = _get_git_root_dir() |
| 29 | +# Change to the outermost Git repository root so that it can find and use |
| 30 | +# the correct repo config. |
| 31 | +git_root_dir = hgit.find_git_root() |
75 | 32 | os.chdir(git_root_dir)
|
76 | 33 |
|
77 | 34 | if __name__ == "__main__":
|
|
0 commit comments