|
1 | 1 | import os |
2 | 2 |
|
3 | | -from .fsutils import ensure_tree, unlink |
| 3 | +from .exceptions import FilesInUseError |
| 4 | +from .fsutils import atomic_unlink, ensure_tree, unlink |
4 | 5 | from .logging import LOGGER |
5 | 6 | from .pathutils import Path |
6 | 7 | from .tagutils import install_matches_any |
@@ -165,24 +166,6 @@ def create_alias(cmd, install, alias, target, *, script_code=None, _link=os.link |
165 | 166 | LOGGER.info("Failed to remove %s.", p_script, exc_info=True) |
166 | 167 |
|
167 | 168 |
|
168 | | -def cleanup_alias(cmd): |
169 | | - if not cmd.global_dir or not cmd.global_dir.is_dir(): |
170 | | - return |
171 | | - |
172 | | - alias_written = cmd.scratch.get("aliasutils.create_alias.alias_written") or () |
173 | | - |
174 | | - for alias in cmd.global_dir.glob("*.exe"): |
175 | | - target = alias.with_name(alias.name + ".__target__") |
176 | | - script = alias.with_name(alias.name + ".__script__.py") |
177 | | - if alias.stem.casefold() not in alias_written: |
178 | | - LOGGER.debug("Unlink %s", alias) |
179 | | - unlink(alias, f"Attempting to remove {alias} is taking some time. " + |
180 | | - "Ensure it is not is use, and please continue to wait " + |
181 | | - "or press Ctrl+C to abort.") |
182 | | - unlink(target) |
183 | | - unlink(script) |
184 | | - |
185 | | - |
186 | 169 | def _parse_entrypoint_line(line): |
187 | 170 | line = line.partition("#")[0] |
188 | 171 | name, sep, rest = line.partition("=") |
@@ -283,32 +266,27 @@ def scan_and_create_entrypoints(cmd, install, shortcut, _create_alias=create_ali |
283 | 266 | _create_alias(cmd, install, alias, target, script_code=code) |
284 | 267 |
|
285 | 268 |
|
286 | | -def cleanup_entrypoints(cmd, install_shortcut_pairs): |
287 | | - seen_names = set() |
288 | | - for install, shortcut in install_shortcut_pairs: |
289 | | - for alias, code in _scan(install["prefix"], shortcut.get("dirs")): |
290 | | - seen_names.add(alias["name"].casefold()) |
| 269 | +def cleanup_alias(cmd, site_dirs_written, *, _unlink_many=atomic_unlink, _scan=_scan): |
| 270 | + if not cmd.global_dir or not cmd.global_dir.is_dir(): |
| 271 | + return |
291 | 272 |
|
292 | | - # Scan existing aliases |
293 | | - scripts = cmd.global_dir.glob("*-script.py") |
| 273 | + expected = set() |
| 274 | + for i in cmd.get_installs(): |
| 275 | + expected.update(a.get("name", "").casefold() for a in i.get("alias", ())) |
294 | 276 |
|
295 | | - # Excluding any in seen_names, delete unused aliases |
296 | | - for script in scripts: |
297 | | - name = script.name.rpartition("-")[0] |
298 | | - if name.casefold() in seen_names: |
299 | | - continue |
| 277 | + for i, s in site_dirs_written or (): |
| 278 | + for alias, code in _scan(i["prefix"], s.get("dirs")): |
| 279 | + expected.add(alias.get("name", "").casefold()) |
300 | 280 |
|
301 | | - alias = cmd.global_dir / (name + ".exe") |
302 | | - if not alias.is_file(): |
| 281 | + for alias in cmd.global_dir.glob("*.exe"): |
| 282 | + if alias.stem.casefold() in expected or alias.name.casefold() in expected: |
303 | 283 | continue |
304 | | - |
305 | | - try: |
306 | | - unlink(alias) |
307 | | - LOGGER.debug("Deleted %s", alias) |
308 | | - except OSError: |
309 | | - LOGGER.warn("Failed to delete %s", alias) |
| 284 | + target = alias.with_name(alias.name + ".__target__") |
| 285 | + script = alias.with_name(alias.name + ".__script__.py") |
| 286 | + LOGGER.debug("Unlink %s", alias) |
310 | 287 | try: |
311 | | - unlink(script) |
312 | | - LOGGER.debug("Deleted %s", script) |
313 | | - except OSError: |
314 | | - LOGGER.warn("Failed to delete %s", script) |
| 288 | + _unlink_many([alias, target, script]) |
| 289 | + except (OSError, FilesInUseError): |
| 290 | + LOGGER.warn("Failed to remove %s. Ensure it is not in use and run " |
| 291 | + "py install --refresh to try again.", alias.name) |
| 292 | + LOGGER.debug("TRACEBACK", exc_info=True) |
0 commit comments