|
32 | 32 | _logger = logging.getLogger(pathlib.Path(__file__).name)
|
33 | 33 |
|
34 | 34 |
|
| 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 | + |
35 | 49 | def configure() -> None:
|
36 | 50 | global DEMO_UUID_BASE
|
37 | 51 |
|
| 52 | + # _logger.debug("sys.argv = %r.", sys.argv) |
| 53 | + |
38 | 54 | if os.getenv("DEMO_UUID_REQUESTING_NONRANDOM") == "NONRANDOM_REQUESTED":
|
39 | 55 | warnings.warn(
|
40 | 56 | "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:
|
82 | 98 | demo_uuid_base_parts.append(sys.argv[0])
|
83 | 99 | else:
|
84 | 100 | command_original_path = pathlib.Path(sys.argv[0])
|
| 101 | + # _logger.debug("command_original_path = %r.", command_original_path) |
85 | 102 | 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. |
86 | 108 | venv_original_path = pathlib.Path(env_venv_name)
|
87 | 109 | venv_resolved_path = venv_original_path.resolve()
|
88 |
| - try: |
| 110 | + if _is_relative_to(command_resolved_path, venv_resolved_path): |
89 | 111 | command_relative_path = command_resolved_path.relative_to(
|
90 | 112 | venv_resolved_path
|
91 | 113 | )
|
92 | 114 | # _logger.debug("command_relative_path = %r.", command_relative_path)
|
93 | 115 | 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)) |
97 | 118 |
|
98 | 119 | if len(sys.argv) > 1:
|
99 | 120 | # Component: Arguments of argument vector.
|
|
0 commit comments