-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublish.py
More file actions
executable file
·92 lines (70 loc) · 2.21 KB
/
publish.py
File metadata and controls
executable file
·92 lines (70 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
"""Build and publish GangDan to PyPI.
Usage:
python publish.py # Build only
python publish.py test # Build + upload to TestPyPI
python publish.py release # Build + upload to PyPI
"""
import subprocess
import sys
import shutil
from pathlib import Path
ROOT = Path(__file__).parent
DIST = ROOT / "dist"
def run(cmd, **kwargs):
print(f"\n>>> {cmd}")
result = subprocess.run(cmd, shell=True, **kwargs)
if result.returncode != 0:
print(f"FAILED (exit {result.returncode})")
sys.exit(result.returncode)
return result
def ensure_tools():
for pkg in ("build", "twine"):
try:
__import__(pkg)
except ImportError:
print(f"Installing {pkg}...")
run(f"{sys.executable} -m pip install {pkg}")
def clean():
for d in (DIST, ROOT / "build", ROOT / "gangdan.egg-info"):
if d.exists():
print(f"Removing {d}")
shutil.rmtree(d)
def build():
clean()
run(f"{sys.executable} -m build")
wheels = list(DIST.glob("*.whl"))
tarballs = list(DIST.glob("*.tar.gz"))
print(f"\nBuilt: {[f.name for f in wheels + tarballs]}")
def upload(repository=None):
cmd = f"{sys.executable} -m twine upload"
if repository:
cmd += f" --repository {repository}"
cmd += " dist/*"
run(cmd)
def main():
ensure_tools()
action = sys.argv[1] if len(sys.argv) > 1 else "build"
if action == "build":
build()
print("\nBuild complete. To upload run:")
print(" python publish.py test # TestPyPI")
print(" python publish.py release # PyPI")
elif action == "test":
build()
print("\nUploading to TestPyPI...")
upload("testpypi")
print("\nDone! Install with:")
print(" pip install --index-url https://test.pypi.org/simple/ gangdan")
elif action == "release":
build()
print("\nUploading to PyPI...")
upload()
print("\nDone! Install with:")
print(" pip install gangdan")
else:
print(f"Unknown action: {action}")
print("Usage: python publish.py [build|test|release]")
sys.exit(1)
if __name__ == "__main__":
main()