-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_to_pypi.py
More file actions
132 lines (105 loc) · 4.1 KB
/
upload_to_pypi.py
File metadata and controls
132 lines (105 loc) · 4.1 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
122
123
124
125
126
127
128
129
130
131
132
# Upload Script for PyPI
# Save your PyPI token and run this script
# STEP 1: Get your PyPI token
# Go to: https://pypi.org/manage/account/token/
# Create a token named "dtor-upload"
# Copy the token (starts with pyp-)
# STEP 2: Set your token as environment variable (RECOMMENDED)
# Windows PowerShell:
# $env:TWINE_PASSWORD = "your-token-here"
# python upload_to_pypi.py
# OR use interactive mode (less secure - token visible in terminal)
import os
import subprocess
import sys
def upload_to_pypi():
"""Upload package to PyPI"""
# Check if distribution files exist
if not os.path.exists('dist'):
print("❌ Error: dist/ directory not found!")
print("Run 'python -m build' first to create distribution files.")
sys.exit(1)
# Check for token in environment
token = os.environ.get('TWINE_PASSWORD')
if not token:
print("⚠️ TWINE_PASSWORD not found in environment variables")
print("\nOptions:")
print("1. Set environment variable (Recommended):")
print(" PowerShell: $env:TWINE_PASSWORD = 'your-token-here'")
print(" Then run this script again")
print("\n2. Enter token interactively (less secure)")
choice = input("\nEnter '1' to exit and set env var, or '2' to continue: ").strip()
if choice == '1':
print("\n✋ Exiting. Please set TWINE_PASSWORD and run again.")
sys.exit(0)
elif choice == '2':
token = input("\n🔑 Enter your PyPI token: ").strip()
if not token:
print("❌ No token provided. Exiting.")
sys.exit(1)
else:
print("❌ Invalid choice. Exiting.")
sys.exit(1)
# Set up environment for twine
env = os.environ.copy()
env['TWINE_USERNAME'] = '__token__'
env['TWINE_PASSWORD'] = token
print("\n📦 Uploading to PyPI...")
print("=" * 60)
try:
# Upload to PyPI
result = subprocess.run(
['twine', 'upload', 'dist/*'],
env=env,
check=True,
capture_output=False
)
print("=" * 60)
print("✅ Successfully uploaded to PyPI!")
print("\n📥 Install with: pip install dtor")
print("🔗 View on PyPI: https://pypi.org/project/dtor/")
except subprocess.CalledProcessError as e:
print("=" * 60)
print(f"❌ Upload failed!")
print(f"\nError: {e}")
sys.exit(1)
except FileNotFoundError:
print("❌ Error: 'twine' not found!")
print("Install with: pip install twine")
sys.exit(1)
def upload_to_test_pypi():
"""Upload to TestPyPI first (for testing)"""
token = os.environ.get('TEST_PYPI_TOKEN')
if not token:
print("⚠️ TEST_PYPI_TOKEN not found")
token = input("Enter TestPyPI token (or press Enter to skip): ").strip()
if not token:
print("⏭️ Skipping TestPyPI upload")
return
env = os.environ.copy()
env['TWINE_USERNAME'] = '__token__'
env['TWINE_PASSWORD'] = token
print("\n📦 Uploading to TestPyPI...")
try:
subprocess.run(
['twine', 'upload', '--repository', 'testpypi', 'dist/*'],
env=env,
check=True
)
print("✅ TestPyPI upload successful!")
print("Test install: pip install --index-url https://test.pypi.org/simple/ dtor")
except subprocess.CalledProcessError:
print("⚠️ TestPyPI upload failed (may already exist)")
if __name__ == '__main__':
print("🚀 dtor PyPI Upload Script")
print("=" * 60)
# Ask if user wants to test on TestPyPI first
test_first = input("\n🧪 Upload to TestPyPI first? (y/n): ").strip().lower()
if test_first == 'y':
upload_to_test_pypi()
proceed = input("\n➡️ Continue to real PyPI? (y/n): ").strip().lower()
if proceed != 'y':
print("✋ Stopped. Run again when ready for PyPI.")
sys.exit(0)
# Upload to real PyPI
upload_to_pypi()