|
39 | 39 | _logger = logging.getLogger(pathlib.Path(__file__).name)
|
40 | 40 |
|
41 | 41 |
|
| 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 | + |
42 | 56 | def configure() -> None:
|
43 | 57 | """
|
44 | 58 | This function is part of setting up _demo_uuid() to generate non-random UUIDs. See _demo_uuid() documentation for further setup notes.
|
45 | 59 | """
|
46 | 60 | global DEMO_UUID_BASE
|
47 | 61 |
|
| 62 | + # _logger.debug("sys.argv = %r.", sys.argv) |
| 63 | + |
48 | 64 | if os.getenv("DEMO_UUID_REQUESTING_NONRANDOM") == "NONRANDOM_REQUESTED":
|
49 | 65 | warnings.warn(
|
50 | 66 | "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:
|
94 | 110 | demo_uuid_base_parts.append(sys.argv[0])
|
95 | 111 | else:
|
96 | 112 | command_original_path = pathlib.Path(sys.argv[0])
|
| 113 | + # _logger.debug("command_original_path = %r.", command_original_path) |
97 | 114 | 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. |
98 | 120 | venv_original_path = pathlib.Path(env_venv_name)
|
99 | 121 | venv_resolved_path = venv_original_path.resolve()
|
100 |
| - try: |
| 122 | + if _is_relative_to(command_resolved_path, venv_resolved_path): |
101 | 123 | command_relative_path = command_resolved_path.relative_to(
|
102 | 124 | venv_resolved_path
|
103 | 125 | )
|
104 | 126 | # _logger.debug("command_relative_path = %r.", command_relative_path)
|
105 | 127 | 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)) |
109 | 130 |
|
110 | 131 | if len(sys.argv) > 1:
|
111 | 132 | # Component: Arguments of argument vector.
|
|
0 commit comments