|
5 | 5 | import subprocess
|
6 | 6 | import sys
|
7 | 7 |
|
| 8 | +from pydicom import __version__ |
| 9 | + |
8 | 10 |
|
9 | 11 | BASE_DIRECTORY = Path(__file__).parent.parent
|
10 | 12 | SRC_DIRECTORY = BASE_DIRECTORY / "custom"
|
11 |
| -DST_DIRECTORY = BASE_DIRECTORY / "pydicom-stubs" |
| 13 | +DST_DIRECTORY = BASE_DIRECTORY / "src" / "pydicom-stubs" |
12 | 14 | PYDICOM_DIRECTORY = BASE_DIRECTORY.parent / "pydicom" / "src" / "pydicom"
|
13 | 15 |
|
14 | 16 |
|
|
31 | 33 | ]
|
32 | 34 |
|
33 | 35 |
|
34 |
| -if __name__ == "__main__": |
| 36 | +def update_stubs() -> None: |
35 | 37 | # Clear out the stub files
|
36 | 38 | if DST_DIRECTORY.exists():
|
37 | 39 | shutil.rmtree(DST_DIRECTORY)
|
38 | 40 |
|
39 |
| - # Generate basic stub files using mypy's `stubgen` |
| 41 | + if (BASE_DIRECTORY / "pydicom").exists(): |
| 42 | + shutil.rmtree(BASE_DIRECTORY / "pydicom") |
| 43 | + |
40 | 44 | print("Generating basic stub files with stubgen")
|
41 |
| - subprocess.run(["which", "stubgen"], shell=True) |
42 |
| - returncode = subprocess.run( |
43 |
| - [ |
44 |
| - f". {os.fspath(BASE_DIRECTORY / 'env' / 'env310' / 'bin' / 'activate')};" |
45 |
| - f"stubgen {os.fspath(PYDICOM_DIRECTORY)} -o .", |
46 |
| - ], |
| 45 | + p = subprocess.run( |
| 46 | + [f"stubgen {os.fspath(PYDICOM_DIRECTORY)} -o ."], |
47 | 47 | shell=True,
|
48 | 48 | )
|
49 | 49 |
|
50 |
| - if not list(DST_DIRECTORY.glob("*.pyi")): |
| 50 | + if p.returncode != 0: |
51 | 51 | print(" Failed to generate the basic stub files")
|
52 | 52 | sys.exit(1)
|
53 | 53 |
|
54 |
| - # Generate the custom stub files |
| 54 | + print(f"Moving basic stub files to {DST_DIRECTORY}") |
| 55 | + shutil.move(BASE_DIRECTORY / "pydicom", DST_DIRECTORY) |
| 56 | + |
55 | 57 | print("Generating custom stub files")
|
56 | 58 | if not SRC_DIRECTORY.exists():
|
57 | 59 | SRC_DIRECTORY.mkdir(parents=True, exist_ok=True)
|
58 | 60 |
|
59 |
| - subprocess.run( |
60 |
| - [ |
61 |
| - f". {os.fspath(BASE_DIRECTORY / 'env' / 'env310' / 'bin' / 'activate')};" |
62 |
| - "python scripts/generate_stubs.py", |
63 |
| - ], |
64 |
| - shell=True, |
65 |
| - ) |
| 61 | + subprocess.run(["python scripts/generate_stubs.py"], shell=True) |
66 | 62 |
|
67 |
| - # Replace basic stub files with custom ones |
68 | 63 | print("Replacing basic stub files with custom ones")
|
69 | 64 | for path in SRC_DIRECTORY.glob("*.pyi"):
|
70 | 65 | shutil.copyfile(path, DST_DIRECTORY / path.name)
|
71 | 66 |
|
72 |
| - # Remove unnecessary stubs |
73 | 67 | print("Removing unnecessary stubs")
|
74 | 68 | for path in REMOVALS:
|
75 | 69 | path.unlink(missing_ok=True)
|
| 70 | + |
| 71 | + |
| 72 | +def update_version() -> None: |
| 73 | + # Get the current package version |
| 74 | + typd_version = "" |
| 75 | + contents = [] |
| 76 | + with open(BASE_DIRECTORY / "pyproject.toml", "r") as f: |
| 77 | + for line in f.readlines(): |
| 78 | + contents.append(line.rstrip()) |
| 79 | + if line.startswith("version = "): |
| 80 | + typd_version = line.rstrip() |
| 81 | + |
| 82 | + # types-pydicom version: X.Y.Z.N[.dev0] |
| 83 | + typd_version = typd_version.strip("version = ") |
| 84 | + typd_version = typd_version.strip("\"") |
| 85 | + print(f"Found current package version '{typd_version}'") |
| 86 | + typd_version = typd_version.split(".") |
| 87 | + if not typd_version or len(typd_version) < 3: |
| 88 | + raise RuntimeError( |
| 89 | + f"Unable to determine the current package version from '{typd_version}'" |
| 90 | + ) |
| 91 | + |
| 92 | + # pydicom version: X.Y.Z[.dev0] |
| 93 | + pyd_version = __version__.strip().split(".") |
| 94 | + if len(pyd_version) not in (3, 4): |
| 95 | + raise RuntimeError(f"Unexpected pydicom version string '{pyd_version}'") |
| 96 | + |
| 97 | + # Determine new package version |
| 98 | + if pyd_version[-1] == "dev0": |
| 99 | + # If pydicom is dev0 then use X.Y.Z.0.dev0: |
| 100 | + version = f"{'.'.join(pyd_version[:-1])}.0.dev0" |
| 101 | + elif pyd_version == typd_version[:3]: |
| 102 | + # If X.Y.Z match -> increment N |
| 103 | + version = f"{'.'.join(pyd_version)}.{int(typd_version[3]) + 1}" |
| 104 | + else: |
| 105 | + # If X.Y.Z don't match, use X.Y.Z.0 |
| 106 | + version = f"{'.'.join(pyd_version)}.0" |
| 107 | + |
| 108 | + if version.split(".") == typd_version: |
| 109 | + print(f"No package version change required") |
| 110 | + return |
| 111 | + |
| 112 | + print(f"Changing package version to '{version}'") |
| 113 | + for idx, line in enumerate(contents): |
| 114 | + if line.startswith("version = "): |
| 115 | + contents[idx] = f"version = \"{version}\"" |
| 116 | + break |
| 117 | + |
| 118 | + with open(BASE_DIRECTORY / "pyproject.toml", "w") as f: |
| 119 | + f.write("\n".join(contents)) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + update_stubs() |
| 124 | + update_version() |
0 commit comments