Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 86 additions & 115 deletions helpers/henv.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,118 +436,89 @@ def get_system_signature(git_commit_type: str = "all") -> Tuple[str, int]:
# #############################################################################


# Copied from helpers.hgit to avoid circular dependencies.


@functools.lru_cache()
def _is_inside_submodule(git_dir: str = ".") -> bool:
"""
Return whether a dir is inside a Git submodule or a Git supermodule.

We determine this checking if the current Git repo is included
inside another Git repo.
"""
cmd = []
# - Find the git root of the current directory
# - Check if the dir one level up is a valid Git repo
# Go to the dir.
cmd.append(f"cd {git_dir}")
# > cd im/
# > git rev-parse --show-toplevel
# /Users/saggese/src/.../amp
cmd.append('cd "$(git rev-parse --show-toplevel)/.."')
# > git rev-parse --is-inside-work-tree
# true
cmd.append("(git rev-parse --is-inside-work-tree | grep -q true)")
cmd_as_str = " && ".join(cmd)
rc = hsystem.system(cmd_as_str, abort_on_error=False)
ret: bool = rc == 0
return ret


@functools.lru_cache()
def _get_client_root(super_module: bool) -> str:
"""
Return the full path of the root of the Git client.

E.g., `/Users/saggese/src/.../amp`.

:param super_module: if True use the root of the Git super_module,
if we are in a submodule. Otherwise use the Git sub_module root
"""
if super_module and _is_inside_submodule():
# https://stackoverflow.com/questions/957928
# > cd /Users/saggese/src/.../amp
# > git rev-parse --show-superproject-working-tree
# /Users/saggese/src/...
cmd = "git rev-parse --show-superproject-working-tree"
else:
# > git rev-parse --show-toplevel
# /Users/saggese/src/.../amp
cmd = "git rev-parse --show-toplevel"
# TODO(gp): Use system_to_one_line().
_, out = hsystem.system_to_string(cmd)
out = out.rstrip("\n")
hdbg.dassert_eq(len(out.split("\n")), 1, msg=f"Invalid out='{out}'")
client_root: str = os.path.realpath(out)
return client_root


# End copy.


def get_repo_config_file(super_module: bool = True) -> str:
"""
Return the absolute path to `repo_config.py` that should be used.

The `repo_config.py` is determined based on an overriding env var or
based on the root of the Git path.
"""
env_var = "CSFY_REPO_CONFIG_PATH"
file_name = get_env_var(env_var, abort_on_missing=False)
if file_name:
_LOG.warning("Using value '%s' for %s from env var", file_name, env_var)
else:
# TODO(gp): We should actually ask Git where the super-module is.
client_root = _get_client_root(super_module)
file_name = os.path.join(client_root, "repo_config.py")
file_name = os.path.abspath(file_name)
return file_name


def _get_repo_config_code(super_module: bool = True) -> str:
"""
Return the text of the code stored in `repo_config.py`.
"""
file_name = get_repo_config_file(super_module)
hdbg.dassert_file_exists(file_name)
code: str = hio.from_file(file_name)
return code


def execute_repo_config_code(code_to_execute: str) -> Any:
"""
Execute code in `repo_config.py` by dynamically finding the correct one.

E.g.,
```
henv.execute_repo_config_code("has_dind_support()")
```
"""
# Read the info from the current repo.
code = _get_repo_config_code()
# TODO(gp): make the linter happy creating this symbol that comes from the
# `exec()`.
try:
exec(code, globals()) # pylint: disable=exec-used
ret = eval(code_to_execute)
except NameError as e:
_LOG.error(
"While executing '%s' caught error:\n%s\nTrying to continue",
code_to_execute,
e,
)
ret = None
_ = e
# raise e
return ret
# # Copied from helpers.hgit to avoid circular dependencies.
#
#
# @functools.lru_cache()
# def _is_inside_submodule(git_dir: str = ".") -> bool:
# """
# Return whether a dir is inside a Git submodule or a Git supermodule.
#
# We determine this checking if the current Git repo is included
# inside another Git repo.
# """
# cmd = []
# # - Find the git root of the current directory
# # - Check if the dir one level up is a valid Git repo
# # Go to the dir.
# cmd.append(f"cd {git_dir}")
# # > cd im/
# # > git rev-parse --show-toplevel
# # /Users/saggese/src/.../amp
# cmd.append('cd "$(git rev-parse --show-toplevel)/.."')
# # > git rev-parse --is-inside-work-tree
# # true
# cmd.append("(git rev-parse --is-inside-work-tree | grep -q true)")
# cmd_as_str = " && ".join(cmd)
# rc = hsystem.system(cmd_as_str, abort_on_error=False)
# ret: bool = rc == 0
# return ret
#
#
# @functools.lru_cache()
# def _get_client_root(super_module: bool) -> str:
# """
# Return the full path of the root of the Git client.
#
# E.g., `/Users/saggese/src/.../amp`.
#
# :param super_module: if True use the root of the Git super_module,
# if we are in a submodule. Otherwise use the Git sub_module root
# """
# if super_module and _is_inside_submodule():
# # https://stackoverflow.com/questions/957928
# # > cd /Users/saggese/src/.../amp
# # > git rev-parse --show-superproject-working-tree
# # /Users/saggese/src/...
# cmd = "git rev-parse --show-superproject-working-tree"
# else:
# # > git rev-parse --show-toplevel
# # /Users/saggese/src/.../amp
# cmd = "git rev-parse --show-toplevel"
# # TODO(gp): Use system_to_one_line().
# _, out = hsystem.system_to_string(cmd)
# out = out.rstrip("\n")
# hdbg.dassert_eq(len(out.split("\n")), 1, msg=f"Invalid out='{out}'")
# client_root: str = os.path.realpath(out)
# return client_root
#
#
# # End copy.


# def execute_repo_config_code(code_to_execute: str) -> Any:
# """
# Execute code in `repo_config.py` by dynamically finding the correct one.
#
# E.g.,
# ```
# henv.execute_repo_config_code("has_dind_support()")
# ```
# """
# # Read the info from the current repo.
# code = _get_repo_config_code()
# # TODO(gp): make the linter happy creating this symbol that comes from the
# # `exec()`.
# try:
# exec(code, globals()) # pylint: disable=exec-used
# ret = eval(code_to_execute)
# except NameError as e:
# _LOG.error(
# "While executing '%s' caught error:\n%s\nTrying to continue",
# code_to_execute,
# e,
# )
# ret = None
# _ = e
# # raise e
# return ret
26 changes: 15 additions & 11 deletions helpers/hgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def get_client_root(super_module: bool) -> str:


# TODO(gp): Replace `get_client_root` with this.
# TODO(gp): -> get_client_root2() or get_outermost_supermodule_root()
def find_git_root(path: str = ".") -> str:
"""
Find recursively the dir of the outermost super module.
Expand Down Expand Up @@ -292,9 +293,9 @@ def get_project_dirname(only_index: bool = False) -> str:
Return the name of the project name (e.g., `/Users/saggese/src/amp1` ->
`amp1`).

NOTE: this works properly only outside Docker, e.g., when calling from `invoke`.
Inside Docker the result might be incorrect since the Git client is mapped on
`/app`.
NOTE: this works properly only outside Docker, e.g., when calling from
`invoke`. Inside Docker the result might be incorrect since the Git client
is mapped on `/app`.

:param only_index: return only the index of the client if possible, e.g.,
E.g., for `/Users/saggese/src/amp1` it returns the string `1`
Expand Down Expand Up @@ -354,8 +355,8 @@ def is_helpers() -> bool:
"""
Return whether we are inside `helpers` repo.

Either as super module, or a sub module depending on a current
working directory.
Either as super module, or a sub module depending on a current working
directory.
"""
return _is_repo("helpers")

Expand Down Expand Up @@ -803,6 +804,7 @@ def get_all_repo_names(
return sorted(list(repo_map.keys()))


# TODO(gp): This should be injected from repo_config.py
def get_task_prefix_from_repo_short_name(short_name: str) -> str:
"""
Return the task prefix for a repo (e.g., "amp" -> "AmpTask").
Expand Down Expand Up @@ -888,6 +890,7 @@ def get_path_from_git_root(
return ret


# TODO(gp): Just do a find
@functools.lru_cache()
def get_amp_abs_path() -> str:
"""
Expand Down Expand Up @@ -935,6 +938,7 @@ def get_repo_dirs() -> List[str]:
return dir_names


# TODO(gp): It should go in hdocker?
def find_docker_file(
file_name: str,
*,
Expand All @@ -947,15 +951,15 @@ def find_docker_file(
Convert a file or dir that was generated inside Docker to a file in the
current Git client.

This operation is best effort since it might not be able to find the
This operation is best-effort since it might not be able to find the
corresponding file in the current repo.

E.g.,
- A file like '/app/amp/core/dataflow_model/utils.py', in a Docker container with
Git root in '/app' becomes 'amp/core/dataflow_model/utils.py'
- For a file like '/app/amp/core/dataflow_model/utils.py' outside Docker, we look
for the file 'dataflow_model/utils.py' in the current client and then normalize
with respect to the
- A file like '/app/amp/core/dataflow_model/utils.py', in a Docker container
with Git root in '/app' becomes 'amp/core/dataflow_model/utils.py'
- For a file like '/app/amp/core/dataflow_model/utils.py' outside Docker, we
look for the file 'dataflow_model/utils.py' in the current client and
then normalize with respect to the

:param dir_depth: same meaning as in `find_file_with_dir()`
:param mode: same as `system_interaction.select_result_file_from_list()`
Expand Down
Loading
Loading