Skip to content

Commit 8361596

Browse files
committed
Make debug print prettier and add useful options in publish script
1 parent 2c73ad4 commit 8361596

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

poetry.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

publish.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from __future__ import annotations
22

33
import json
4+
import os
45
import shutil
56
import subprocess
67
from pathlib import Path
78
from typing import TYPE_CHECKING
89
from urllib.request import urlopen
910

11+
from natsort import natsorted
12+
1013
if TYPE_CHECKING:
1114
from collections.abc import KeysView
1215

@@ -18,22 +21,18 @@ def download_openapi_generator_jar(version: str) -> None:
1821
MVN_BASE_URL
1922
+ "/remotecontent?filepath=org/openapitools/openapi-generator-cli/"
2023
+ version
21-
+ "/openapi-generator-cli-"
22-
+ version
23-
+ ".jar"
24+
+ f"/openapi-generator-cli-{version}.jar"
2425
)
2526

2627
Path("openapi-generator.jar").unlink(missing_ok=True)
2728

28-
print(download_url)
29+
print(f"[{version}] URL: {download_url!r}")
2930
response = urlopen(download_url) # noqa: S310
3031

3132
if response.status != 200: # noqa: PLR2004
3233
msg = f"{response.status}: {download_url}"
3334
raise RuntimeError(msg)
3435

35-
print("Downloading complete")
36-
3736
with Path("openapi_generator_cli/openapi-generator.jar").open("wb") as openapi_generator_jar:
3837
openapi_generator_jar.write(response.read())
3938
openapi_generator_jar.close()
@@ -78,18 +77,43 @@ def get_published_vesions() -> KeysView[str]:
7877
return published_releases.keys()
7978

8079

81-
def publish() -> None:
82-
latest_version = get_available_versions()[0]
83-
published_versions = get_published_vesions()
80+
def download_latest_jar_for_test() -> None:
81+
latest_version = natsorted(get_available_versions())[-1]
82+
print(f"[{latest_version}] Downloading...")
83+
download_openapi_generator_jar(latest_version)
84+
print(f"[{latest_version}] Downloaded!")
85+
86+
87+
def publish(*, dryrun: bool = False) -> None:
88+
pytest_path = shutil.which("pytest")
89+
poetry_path = shutil.which("poetry")
90+
91+
unpublished_versions = natsorted(set(get_available_versions()) - set(get_published_vesions()))
92+
93+
for publishing_version in unpublished_versions:
94+
print(f"[{publishing_version}] Downloading...")
95+
download_openapi_generator_jar(publishing_version)
96+
97+
print(f"[{publishing_version}] Testing...")
98+
subprocess.check_call([pytest_path])
99+
100+
if dryrun:
101+
continue
102+
103+
print(f"[{publishing_version}] Building...")
104+
subprocess.check_call([poetry_path, "build", "-v"]) # noqa: S603
105+
106+
print(f"[{publishing_version}] Publishing to TestPyPI...")
107+
subprocess.check_call([poetry_path, "publish", "-r", "testpypi", "-v"]) # noqa: S603
108+
109+
print(f"[{publishing_version}] Publishing to PyPI...")
110+
subprocess.check_call([poetry_path, "publish", "-v"]) # noqa: S603
84111

85-
if latest_version not in published_versions:
86-
print("Publishing version " + latest_version)
87-
download_openapi_generator_jar(latest_version)
88-
poetry_path = shutil.which("poetry")
89-
subprocess.check_call([poetry_path, "build"]) # noqa: S603
90-
subprocess.check_call([poetry_path, "publish", "-r", "testpypi"]) # noqa: S603
91-
subprocess.check_call([poetry_path, "publish"]) # noqa: S603
112+
print(f"[{publishing_version}] Published!")
92113

93114

94115
if __name__ == "__main__":
95-
publish()
116+
if os.getenv("DOWNLOAD_LATEST_ONLY") == "1":
117+
download_latest_jar_for_test()
118+
else:
119+
publish(dryrun=os.getenv("DRYRUN") == "1")

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mypy = ">=0.991,<1.14"
4444
pre-commit = ">=2.20,<4.0"
4545
taskipy = "^1.10.3"
4646
pytest = ">=7.2.2,<9.0.0"
47+
natsort = "^8.4.0"
4748

4849
[tool.poetry.scripts]
4950
openapi-generator-cli = "openapi_generator_cli:cli"
@@ -66,6 +67,9 @@ lint.ignore = [
6667
lint.per-file-ignores."publish.py" = [
6768
"T201", # `print` found
6869
]
70+
lint.per-file-ignores."tests/test_*.py" = [
71+
"S101", # Use of `assert` detected
72+
]
6973

7074
[tool.mypy]
7175
pretty = true

0 commit comments

Comments
 (0)