|
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 cdo_local_uuid._demo_uuid for usage notes on its replacement, CDO_DEMO_NONRANDOM_UUID_BASE. Proceeding with random UUIDs.", |
@@ -101,18 +117,23 @@ def configure() -> None: |
101 | 117 | demo_uuid_base_parts.append(sys.argv[0]) |
102 | 118 | else: |
103 | 119 | command_original_path = pathlib.Path(sys.argv[0]) |
| 120 | + # _logger.debug("command_original_path = %r.", command_original_path) |
104 | 121 | command_resolved_path = command_original_path.resolve() |
| 122 | + # _logger.debug("command_resolved_path = %r.", command_resolved_path) |
| 123 | + |
| 124 | + # The command could be a command embedded in a virtual |
| 125 | + # environment, or it could be a script external to any virtual |
| 126 | + # environment. |
105 | 127 | venv_original_path = pathlib.Path(env_venv_name) |
106 | 128 | venv_resolved_path = venv_original_path.resolve() |
107 | | - try: |
| 129 | + if _is_relative_to(command_resolved_path, venv_resolved_path): |
108 | 130 | command_relative_path = command_resolved_path.relative_to( |
109 | 131 | venv_resolved_path |
110 | 132 | ) |
111 | 133 | # _logger.debug("command_relative_path = %r.", command_relative_path) |
112 | 134 | demo_uuid_base_parts.append(str(command_relative_path)) |
113 | | - except ValueError: |
114 | | - # _logger.debug("Command path is not relative to virtual environment path.") |
115 | | - demo_uuid_base_parts.append(str(command_resolved_path)) |
| 135 | + else: |
| 136 | + demo_uuid_base_parts.append(str(command_original_path)) |
116 | 137 |
|
117 | 138 | if len(sys.argv) > 1: |
118 | 139 | # Component: Arguments of argument vector. |
|
0 commit comments