Skip to content

Commit 9e78f7c

Browse files
authored
Merge pull request #3 from Cyber-Domain-Ontology/fix_command_in_top_srcdir_case
2 parents e9864e5 + c39cab9 commit 9e78f7c

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

Diff for: cdo_local_uuid/__init__.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,28 @@
3939
_logger = logging.getLogger(pathlib.Path(__file__).name)
4040

4141

42+
def _is_relative_to(p1: pathlib.Path, p2: pathlib.Path) -> bool:
43+
"""
44+
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.
45+
"""
46+
if sys.version_info < (3, 9):
47+
try:
48+
_ = p1.relative_to(p2)
49+
return True
50+
except ValueError:
51+
return False
52+
else:
53+
return p1.is_relative_to(p2)
54+
55+
4256
def configure() -> None:
4357
"""
4458
This function is part of setting up _demo_uuid() to generate non-random UUIDs. See _demo_uuid() documentation for further setup notes.
4559
"""
4660
global DEMO_UUID_BASE
4761

62+
# _logger.debug("sys.argv = %r.", sys.argv)
63+
4864
if os.getenv("DEMO_UUID_REQUESTING_NONRANDOM") == "NONRANDOM_REQUESTED":
4965
warnings.warn(
5066
"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.",
@@ -101,18 +117,23 @@ def configure() -> None:
101117
demo_uuid_base_parts.append(sys.argv[0])
102118
else:
103119
command_original_path = pathlib.Path(sys.argv[0])
120+
# _logger.debug("command_original_path = %r.", command_original_path)
104121
command_resolved_path = command_original_path.resolve()
122+
# _logger.debug("command_resolved_path = %r.", command_resolved_path)
123+
124+
# The command could be a command embedded in a virtual
125+
# environment, or it could be a script external to any virtual
126+
# environment.
105127
venv_original_path = pathlib.Path(env_venv_name)
106128
venv_resolved_path = venv_original_path.resolve()
107-
try:
129+
if _is_relative_to(command_resolved_path, venv_resolved_path):
108130
command_relative_path = command_resolved_path.relative_to(
109131
venv_resolved_path
110132
)
111133
# _logger.debug("command_relative_path = %r.", command_relative_path)
112134
demo_uuid_base_parts.append(str(command_relative_path))
113-
except ValueError:
114-
# _logger.debug("Command path is not relative to virtual environment path.")
115-
demo_uuid_base_parts.append(str(command_resolved_path))
135+
else:
136+
demo_uuid_base_parts.append(str(command_original_path))
116137

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

0 commit comments

Comments
 (0)