|
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.", |
@@ -84,18 +100,23 @@ def configure() -> None: |
84 | 100 | demo_uuid_base_parts.append(sys.argv[0]) |
85 | 101 | else: |
86 | 102 | command_original_path = pathlib.Path(sys.argv[0]) |
| 103 | + # _logger.debug("command_original_path = %r.", command_original_path) |
87 | 104 | command_resolved_path = command_original_path.resolve() |
| 105 | + # _logger.debug("command_resolved_path = %r.", command_resolved_path) |
| 106 | + |
| 107 | + # The command could be a command embedded in a virtual |
| 108 | + # environment, or it could be a script external to any virtual |
| 109 | + # environment. |
88 | 110 | venv_original_path = pathlib.Path(env_venv_name) |
89 | 111 | venv_resolved_path = venv_original_path.resolve() |
90 | | - try: |
| 112 | + if _is_relative_to(command_resolved_path, venv_resolved_path): |
91 | 113 | command_relative_path = command_resolved_path.relative_to( |
92 | 114 | venv_resolved_path |
93 | 115 | ) |
94 | 116 | # _logger.debug("command_relative_path = %r.", command_relative_path) |
95 | 117 | demo_uuid_base_parts.append(str(command_relative_path)) |
96 | | - except ValueError: |
97 | | - # _logger.debug("Command path is not relative to virtual environment path.") |
98 | | - demo_uuid_base_parts.append(str(command_resolved_path)) |
| 118 | + else: |
| 119 | + demo_uuid_base_parts.append(str(command_original_path)) |
99 | 120 |
|
100 | 121 | if len(sys.argv) > 1: |
101 | 122 | # Component: Arguments of argument vector. |
|
0 commit comments