Skip to content

Commit ea630cc

Browse files
committed
Backport
References: Cyber-Domain-Ontology/CDO-Utility-Local-UUID#3 Signed-off-by: Alex Nelson <[email protected]>
1 parent 33d9565 commit ea630cc

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

case_utils/local_uuid.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,25 @@
3232
_logger = logging.getLogger(pathlib.Path(__file__).name)
3333

3434

35+
def _is_relative_to(p1: pathlib.Path, p2: pathlib.Path) -> bool:
36+
"""
37+
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.
38+
"""
39+
if sys.version_info < (3, 9):
40+
try:
41+
_ = p1.relative_to(p2)
42+
return True
43+
except ValueError:
44+
return False
45+
else:
46+
return p1.is_relative_to(p2)
47+
48+
3549
def configure() -> None:
3650
global DEMO_UUID_BASE
3751

52+
# _logger.debug("sys.argv = %r.", sys.argv)
53+
3854
if os.getenv("DEMO_UUID_REQUESTING_NONRANDOM") == "NONRANDOM_REQUESTED":
3955
warnings.warn(
4056
"Environment variable DEMO_UUID_REQUESTING_NONRANDOM is deprecated. See case_utils.local_uuid.demo_uuid for usage notes on its replacement, CASE_DEMO_NONRANDOM_UUID_BASE. Proceeding with random UUIDs.",
@@ -82,18 +98,23 @@ def configure() -> None:
8298
demo_uuid_base_parts.append(sys.argv[0])
8399
else:
84100
command_original_path = pathlib.Path(sys.argv[0])
101+
# _logger.debug("command_original_path = %r.", command_original_path)
85102
command_resolved_path = command_original_path.resolve()
103+
# _logger.debug("command_resolved_path = %r.", command_resolved_path)
104+
105+
# The command could be a command embedded in a virtual
106+
# environment, or it could be a script external to any virtual
107+
# environment.
86108
venv_original_path = pathlib.Path(env_venv_name)
87109
venv_resolved_path = venv_original_path.resolve()
88-
try:
110+
if _is_relative_to(command_resolved_path, venv_resolved_path):
89111
command_relative_path = command_resolved_path.relative_to(
90112
venv_resolved_path
91113
)
92114
# _logger.debug("command_relative_path = %r.", command_relative_path)
93115
demo_uuid_base_parts.append(str(command_relative_path))
94-
except ValueError:
95-
# _logger.debug("Command path is not relative to virtual environment path.")
96-
demo_uuid_base_parts.append(str(command_resolved_path))
116+
else:
117+
demo_uuid_base_parts.append(str(command_original_path))
97118

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

0 commit comments

Comments
 (0)