Skip to content

Commit 7218997

Browse files
committed
Merge branch 'HotFix-CDO-Utility-Local-UUID-Pull-3' into release-0.14.1
2 parents 0f4a54d + ea630cc commit 7218997

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
@@ -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 case_utils.local_uuid._demo_uuid for usage notes on its replacement, CASE_DEMO_NONRANDOM_UUID_BASE. Proceeding with random UUIDs.",
@@ -94,18 +110,23 @@ def configure() -> None:
94110
demo_uuid_base_parts.append(sys.argv[0])
95111
else:
96112
command_original_path = pathlib.Path(sys.argv[0])
113+
# _logger.debug("command_original_path = %r.", command_original_path)
97114
command_resolved_path = command_original_path.resolve()
115+
# _logger.debug("command_resolved_path = %r.", command_resolved_path)
116+
117+
# The command could be a command embedded in a virtual
118+
# environment, or it could be a script external to any virtual
119+
# environment.
98120
venv_original_path = pathlib.Path(env_venv_name)
99121
venv_resolved_path = venv_original_path.resolve()
100-
try:
122+
if _is_relative_to(command_resolved_path, venv_resolved_path):
101123
command_relative_path = command_resolved_path.relative_to(
102124
venv_resolved_path
103125
)
104126
# _logger.debug("command_relative_path = %r.", command_relative_path)
105127
demo_uuid_base_parts.append(str(command_relative_path))
106-
except ValueError:
107-
# _logger.debug("Command path is not relative to virtual environment path.")
108-
demo_uuid_base_parts.append(str(command_resolved_path))
128+
else:
129+
demo_uuid_base_parts.append(str(command_original_path))
109130

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

0 commit comments

Comments
 (0)