-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpublish.py
More file actions
executable file
·121 lines (98 loc) · 3.66 KB
/
publish.py
File metadata and controls
executable file
·121 lines (98 loc) · 3.66 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
"""
Simple script to publish git-smart-squash to PyPI.
Usage:
python publish.py [VERSION]
If VERSION is not provided, it will use the current version from VERSION file.
If VERSION is provided, it will update the VERSION file and then publish.
"""
import subprocess
import sys
import os
import shutil
from pathlib import Path
def run_command(cmd, description=""):
"""Run a command and handle errors."""
print(f"→ {description if description else ' '.join(cmd)}")
try:
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
if result.stdout.strip():
print(f" {result.stdout.strip()}")
return True
except subprocess.CalledProcessError as e:
print(f" ❌ Error: {e}")
if e.stderr:
print(f" {e.stderr}")
return False
def get_current_version():
"""Get current version from VERSION file."""
version_file = Path("git_smart_squash/VERSION")
if version_file.exists():
return version_file.read_text().strip()
return None
def update_version(new_version):
"""Update the VERSION file."""
version_file = Path("git_smart_squash/VERSION")
version_file.write_text(new_version)
print(f"→ Updated version to {new_version}")
def clean_build_artifacts():
"""Clean up build artifacts."""
print("🧹 Cleaning build artifacts...")
# Remove build directories
for dir_name in ["build", "dist", "*.egg-info"]:
for path in Path(".").glob(dir_name):
if path.is_dir():
shutil.rmtree(path)
print(f" Removed {path}")
# Remove __pycache__ directories
for pycache in Path(".").rglob("__pycache__"):
shutil.rmtree(pycache)
def build_package():
"""Build the package."""
print("📦 Building package...")
return run_command([sys.executable, "setup.py", "sdist", "bdist_wheel"], "Building distribution packages")
def upload_to_pypi():
"""Upload to PyPI using twine."""
print("🚀 Uploading to PyPI...")
# Check if twine is installed
try:
subprocess.run(["twine", "--version"], check=True, capture_output=True)
except (subprocess.CalledProcessError, FileNotFoundError):
print("❌ twine not found. Installing...")
if not run_command([sys.executable, "-m", "pip", "install", "twine"], "Installing twine"):
return False
# Upload to PyPI
return run_command(["twine", "upload", "dist/*"], "Uploading to PyPI")
def main():
"""Main function."""
print("🔧 Git Smart Squash PyPI Publisher")
print("=" * 40)
# Check if we're in the right directory
if not Path("setup.py").exists() or not Path("git_smart_squash").exists():
print("❌ Error: Must be run from the git-smart-squash root directory")
sys.exit(1)
# Handle version argument
if len(sys.argv) > 1:
new_version = sys.argv[1]
update_version(new_version)
# Get current version
version = get_current_version()
if not version:
print("❌ Error: Could not read version from git_smart_squash/VERSION")
sys.exit(1)
print(f"📋 Publishing version: {version}")
# Clean build artifacts
clean_build_artifacts()
# Build package
if not build_package():
print("❌ Build failed")
sys.exit(1)
# Upload to PyPI
if not upload_to_pypi():
print("❌ Upload failed")
sys.exit(1)
print("✅ Successfully published to PyPI!")
print(f"📦 Version {version} is now available")
print(f"🔗 Install with: pip install --upgrade git-smart-squash")
if __name__ == "__main__":
main()