|
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 | """ |
37 | 51 | This function is part of setting up demo_uuid() to generate non-random UUIDs. See demo_uuid() documentation for further setup notes. |
38 | 52 | """ |
39 | 53 | global DEMO_UUID_BASE |
40 | 54 |
|
| 55 | + # _logger.debug("sys.argv = %r.", sys.argv) |
| 56 | + |
41 | 57 | if os.getenv("DEMO_UUID_REQUESTING_NONRANDOM") == "NONRANDOM_REQUESTED": |
42 | 58 | warnings.warn( |
43 | 59 | "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.", |
@@ -87,18 +103,23 @@ def configure() -> None: |
87 | 103 | demo_uuid_base_parts.append(sys.argv[0]) |
88 | 104 | else: |
89 | 105 | command_original_path = pathlib.Path(sys.argv[0]) |
| 106 | + # _logger.debug("command_original_path = %r.", command_original_path) |
90 | 107 | command_resolved_path = command_original_path.resolve() |
| 108 | + # _logger.debug("command_resolved_path = %r.", command_resolved_path) |
| 109 | + |
| 110 | + # The command could be a command embedded in a virtual |
| 111 | + # environment, or it could be a script external to any virtual |
| 112 | + # environment. |
91 | 113 | venv_original_path = pathlib.Path(env_venv_name) |
92 | 114 | venv_resolved_path = venv_original_path.resolve() |
93 | | - try: |
| 115 | + if _is_relative_to(command_resolved_path, venv_resolved_path): |
94 | 116 | command_relative_path = command_resolved_path.relative_to( |
95 | 117 | venv_resolved_path |
96 | 118 | ) |
97 | 119 | # _logger.debug("command_relative_path = %r.", command_relative_path) |
98 | 120 | demo_uuid_base_parts.append(str(command_relative_path)) |
99 | | - except ValueError: |
100 | | - # _logger.debug("Command path is not relative to virtual environment path.") |
101 | | - demo_uuid_base_parts.append(str(command_resolved_path)) |
| 121 | + else: |
| 122 | + demo_uuid_base_parts.append(str(command_original_path)) |
102 | 123 |
|
103 | 124 | if len(sys.argv) > 1: |
104 | 125 | # Component: Arguments of argument vector. |
|
0 commit comments