Skip to content

Commit

Permalink
Merge pull request #3 from Cyber-Domain-Ontology/fix_command_in_top_s…
Browse files Browse the repository at this point in the history
…rcdir_case
  • Loading branch information
kchason authored Dec 14, 2023
2 parents e9864e5 + c39cab9 commit 9e78f7c
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions cdo_local_uuid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,28 @@
_logger = logging.getLogger(pathlib.Path(__file__).name)


def _is_relative_to(p1: pathlib.Path, p2: pathlib.Path) -> bool:
"""
This function provides pathlib.is_relative_to to Pythons before 3.9. After the End of Life of Python 3.8, this function can be removed.
"""
if sys.version_info < (3, 9):
try:
_ = p1.relative_to(p2)
return True
except ValueError:
return False
else:
return p1.is_relative_to(p2)


def configure() -> None:
"""
This function is part of setting up _demo_uuid() to generate non-random UUIDs. See _demo_uuid() documentation for further setup notes.
"""
global DEMO_UUID_BASE

# _logger.debug("sys.argv = %r.", sys.argv)

if os.getenv("DEMO_UUID_REQUESTING_NONRANDOM") == "NONRANDOM_REQUESTED":
warnings.warn(
"Environment variable DEMO_UUID_REQUESTING_NONRANDOM is deprecated. See cdo_local_uuid._demo_uuid for usage notes on its replacement, CDO_DEMO_NONRANDOM_UUID_BASE. Proceeding with random UUIDs.",
Expand Down Expand Up @@ -101,18 +117,23 @@ def configure() -> None:
demo_uuid_base_parts.append(sys.argv[0])
else:
command_original_path = pathlib.Path(sys.argv[0])
# _logger.debug("command_original_path = %r.", command_original_path)
command_resolved_path = command_original_path.resolve()
# _logger.debug("command_resolved_path = %r.", command_resolved_path)

# The command could be a command embedded in a virtual
# environment, or it could be a script external to any virtual
# environment.
venv_original_path = pathlib.Path(env_venv_name)
venv_resolved_path = venv_original_path.resolve()
try:
if _is_relative_to(command_resolved_path, venv_resolved_path):
command_relative_path = command_resolved_path.relative_to(
venv_resolved_path
)
# _logger.debug("command_relative_path = %r.", command_relative_path)
demo_uuid_base_parts.append(str(command_relative_path))
except ValueError:
# _logger.debug("Command path is not relative to virtual environment path.")
demo_uuid_base_parts.append(str(command_resolved_path))
else:
demo_uuid_base_parts.append(str(command_original_path))

if len(sys.argv) > 1:
# Component: Arguments of argument vector.
Expand Down

0 comments on commit 9e78f7c

Please sign in to comment.