|
3 | 3 |
|
4 | 4 | This fixes https://github.com/python-poetry/poetry/issues/4526.
|
5 | 5 | Should be fixed in poetry 1.2, but it's not available yet.
|
6 |
| -Run this if test_package() fails on pytest run. |
7 |
| -
|
| 6 | +Run this if test_package() fails on pytest run |
| 7 | +(mckit of wrong version is found on python site). |
8 | 8 | """
|
9 |
| -import platform |
| 9 | + |
| 10 | +from typing import Optional, TypeVar |
| 11 | + |
10 | 12 | import shutil
|
11 |
| -import sys |
| 13 | +import site |
| 14 | +import subprocess |
12 | 15 |
|
13 | 16 | from pathlib import Path
|
14 | 17 |
|
| 18 | +import tomli |
15 | 19 |
|
16 |
| -def get_packages_dir() -> Path: |
17 |
| - """Define packages location depending on system.""" |
18 |
| - system = platform.system() |
19 |
| - if system == "Windows": |
20 |
| - site_packages = Path("Lib", "site-packages") |
21 |
| - else: |
22 |
| - python = f"python{sys.version_info.major}.{sys.version_info.minor}" |
23 |
| - site_packages = Path("lib", python, "site-packages") |
24 |
| - result = Path(sys.prefix, site_packages) |
25 |
| - if not result.is_dir(): |
26 |
| - raise ValueError( |
27 |
| - f"Cannot find site package for system {system} in folder {result}" |
| 20 | +PathLike = TypeVar("PathLike", str, Path) |
| 21 | + |
| 22 | + |
| 23 | +def search_upwards_for_file(filename: PathLike) -> Optional[Path]: |
| 24 | + """Search upward from the current directory for a `filename`. |
| 25 | +
|
| 26 | + Args: |
| 27 | + filename: the file name to look for if available. |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Path: the location of the first file found or None, if none was found |
| 31 | + """ |
| 32 | + d = Path.cwd() |
| 33 | + root = Path(d.root) |
| 34 | + |
| 35 | + while d != root: |
| 36 | + attempt = d / filename |
| 37 | + if attempt.exists(): |
| 38 | + return attempt |
| 39 | + d = d.parent |
| 40 | + |
| 41 | + return None |
| 42 | + |
| 43 | + |
| 44 | +def get_project_name() -> str: |
| 45 | + """Find project name, which is prefix for info distributions. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + str: the name of package specified in pyproject.toml |
| 49 | +
|
| 50 | + Raises: |
| 51 | + EnvironmentError: if file pyproject.toml is not found. |
| 52 | + """ |
| 53 | + pyproject_path = search_upwards_for_file("pyproject.toml") |
| 54 | + if pyproject_path is None: |
| 55 | + raise EnvironmentError( |
| 56 | + "Illegal directory: cannot find file pyproject.toml " |
| 57 | + f"from current directory: {Path.cwd()}" |
28 | 58 | )
|
29 |
| - return result |
| 59 | + pyproject = tomli.loads(pyproject_path.read_text()) |
| 60 | + name = pyproject["tool"]["poetry"]["name"].replace("-", "_") |
| 61 | + print(f"Package {name} is found in {pyproject_path.absolute()}") |
| 62 | + return name |
| 63 | + |
| 64 | + |
| 65 | +def clear_previous_distributions_info() -> None: |
| 66 | + """Remove all dist-info folders from previous installations.""" |
| 67 | + packages_dir = Path(site.getsitepackages()[0]) |
| 68 | + name = get_project_name() |
| 69 | + dists = list(packages_dir.glob(f"{name}-*.dist-info")) |
| 70 | + if dists: |
| 71 | + for dist in dists: |
| 72 | + print("Removing distribution", dist) |
| 73 | + shutil.rmtree(dist) |
| 74 | + else: |
| 75 | + print("Nothing to remove for package", name) |
30 | 76 |
|
31 | 77 |
|
32 |
| -def clear_mapstp_info_dists() -> None: |
33 |
| - """Remove all mapstp dist-info folders.""" |
34 |
| - packages_dir = get_packages_dir() |
35 |
| - dists = list(packages_dir.glob("mapstp-*.dist-info")) |
36 |
| - for dist in dists: |
37 |
| - shutil.rmtree(dist) |
38 |
| - print("Run `poetry install` after running this script.") |
| 78 | +def run_poetry_install() -> None: |
| 79 | + """Refresh installation of the package.""" |
| 80 | + print("Running `poetry install`.") |
| 81 | + subprocess.run(["poetry", "install"]) |
39 | 82 |
|
40 | 83 |
|
41 | 84 | if __name__ == "__main__":
|
42 |
| - clear_mapstp_info_dists() |
| 85 | + clear_previous_distributions_info() |
| 86 | + run_poetry_install() |
0 commit comments