Skip to content

Commit add14ec

Browse files
authored
HelpersTask644_Bug_in_go_XYZ.py_2 (#667)
* Improve Pre-commit checks: - 'check_master' passed - 'check_author' passed - 'check_file_size' passed - 'check_python_compile' passed - 'check_gitleaks' passed All checks passed ✅ * Lint Pre-commit checks: - 'check_master' passed - 'check_author' passed - 'check_file_size' passed - 'check_python_compile' passed - 'check_gitleaks' passed All checks passed ✅ * Checkpoint Pre-commit checks: - 'check_master' passed - 'check_author' passed - 'check_file_size' passed - 'check_python_compile' passed - 'check_gitleaks' passed All checks passed ✅ * Improve Pre-commit checks: - 'check_master' passed - 'check_author' passed - 'check_file_size' passed - 'check_python_compile' passed - 'check_gitleaks' passed All checks passed ✅ * Checkpoint Pre-commit checks: - 'check_master' passed - 'check_author' passed - 'check_file_size' passed - 'check_python_compile' passed - 'check_gitleaks' passed All checks passed ✅ * Lint Pre-commit checks: - 'check_master' passed - 'check_author' passed - 'check_file_size' passed - 'check_python_compile' passed - 'check_gitleaks' passed All checks passed ✅
1 parent b72fc24 commit add14ec

File tree

1 file changed

+15
-58
lines changed
  • dev_scripts_helpers/thin_client

1 file changed

+15
-58
lines changed

dev_scripts_helpers/thin_client/tmux.py

+15-58
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,30 @@
55

66
import logging
77
import os
8-
import subprocess
9-
import sys
10-
from typing import Tuple
118

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
4912

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
6516
import helpers.repo_config_utils as hrecouti
6617

18+
_LOG = logging.getLogger(__name__)
19+
6720
# Get the real file path rather than the symlink path.
6821
current_file_path = os.path.realpath(__file__)
6922
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.
7127
os.chdir(current_dir)
7228

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()
7532
os.chdir(git_root_dir)
7633

7734
if __name__ == "__main__":

0 commit comments

Comments
 (0)