-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_pypi.sh
More file actions
328 lines (270 loc) · 8.29 KB
/
update_pypi.sh
File metadata and controls
328 lines (270 loc) · 8.29 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
# GenIText PyPI Update Script
# Automates the process of building and uploading a new version to PyPI
#
# Usage: ./update_pypi.sh -v VERSION [OPTIONS]
#
# Examples:
# ./update_pypi.sh -v 0.4.3 # Production upload
# ./update_pypi.sh -v 0.4.3 -t # TestPyPI upload
# ./update_pypi.sh -v 0.4.3 -s # Build and test only
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Function to print colored output
/usr/local/bin/python3 -m venv venv
source venv/bin/activate
print_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_info() {
echo -e "${CYAN}[INFO]${NC} $1"
}
# Function to show usage
show_usage() {
echo "GenIText PyPI Update Script"
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -v, --version VERSION New version number (required)"
echo " -t, --test Upload to TestPyPI instead of production"
echo " -s, --skip-upload Skip upload, just build and test"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 -v 0.4.3 # Build and upload v0.4.3 to production PyPI"
echo " $0 -v 0.4.3 -t # Build and upload v0.4.3 to TestPyPI"
echo " $0 -v 0.4.3 -s # Just build and test v0.4.3"
}
# Function to update version in setup.py
update_setup_py_version() {
local new_version=$1
print_step "Updating version in setup.py to $new_version"
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
sed -i '' "s/version=\"[^\"]*\"/version=\"$new_version\"/g" setup.py
else
# Linux
sed -i "s/version=\"[^\"]*\"/version=\"$new_version\"/g" setup.py
fi
print_success "Updated setup.py version"
}
# Function to update version in pyproject.toml
update_pyproject_version() {
local new_version=$1
print_step "Updating version in pyproject.toml to $new_version"
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
sed -i '' "s/version = \"[^\"]*\"/version = \"$new_version\"/g" pyproject.toml
else
# Linux
sed -i "s/version = \"[^\"]*\"/version = \"$new_version\"/g" pyproject.toml
fi
print_success "Updated pyproject.toml version"
}
# Function to check if version is valid
validate_version() {
local version=$1
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?(\+[a-zA-Z0-9]+)?$ ]]; then
print_error "Invalid version format. Expected: x.y.z or x.y.z-suffix"
exit 1
fi
}
# Function to check if we're in the right directory
check_project_directory() {
if [[ ! -f "setup.py" ]] || [[ ! -f "pyproject.toml" ]] || [[ ! -d "GenIText" ]]; then
print_error "Not in GenIText project directory. Please run from the project root."
exit 1
fi
}
# Function to install build tools
install_build_tools() {
print_step "Installing/updating build tools..."
if ! command -v python &> /dev/null; then
print_error "Python is not installed or not in PATH"
exit 1
fi
python -m pip install --upgrade pip
python -m pip install --upgrade build twine
print_success "Build tools installed/updated"
}
# Function to clean previous builds
clean_previous_builds() {
print_step "Cleaning previous builds..."
if [[ -d "dist" ]]; then
rm -rf dist/
fi
if [[ -d "build" ]]; then
rm -rf build/
fi
# Clean egg-info
find . -name "*.egg-info" -type d -exec rm -rf {} + 2>/dev/null || true
print_success "Cleaned previous builds"
}
# Function to build package
build_package() {
print_step "Building package..."
python -m build
if [[ ! -f "dist/genitext-$VERSION.tar.gz" ]] || [[ ! -f "dist/genitext-$VERSION-py3-none-any.whl" ]]; then
print_error "Build failed - distribution files not found"
exit 1
fi
print_success "Package built successfully"
ls -lh dist/
}
# Function to test package installation
test_package() {
print_step "Testing package installation..."
# Create temporary directory for testing
local test_dir="/tmp/genitext_test_$VERSION"
rm -rf "$test_dir"
mkdir -p "$test_dir"
cd "$test_dir"
# Install package
pip install "$OLDPWD/dist/genitext-$VERSION-py3-none-any.whl"
# Test CLI
if ! genitext --help > /dev/null 2>&1; then
print_error "CLI test failed"
cd "$OLDPWD"
rm -rf "$test_dir"
exit 1
fi
# Test import
if ! python -c "import GenIText; print('Import successful')" > /dev/null 2>&1; then
print_error "Import test failed"
cd "$OLDPWD"
rm -rf "$test_dir"
exit 1
fi
# Cleanup
cd "$OLDPWD"
rm -rf "$test_dir"
print_success "Package installation test passed"
}
# Function to upload to PyPI
upload_to_pypi() {
local repository=$1
if [[ "$repository" == "pypi" ]]; then
print_warning "About to upload to PRODUCTION PyPI. This cannot be undone!"
read -p "Are you sure you want to continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Upload cancelled"
exit 0
fi
print_step "Uploading to production PyPI..."
python -m twine upload dist/*
else
print_step "Uploading to TestPyPI..."
python -m twine upload --repository testpypi dist/*
fi
print_success "Package uploaded successfully!"
}
# Parse command line arguments
VERSION=""
UPLOAD_DEST="pypi"
SKIP_UPLOAD=false
while [[ $# -gt 0 ]]; do
case $1 in
-v|--version)
VERSION="$2"
shift 2
;;
-t|--test)
UPLOAD_DEST="testpypi"
shift
;;
-s|--skip-upload)
SKIP_UPLOAD=true
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Validate inputs
if [[ -z "$VERSION" ]]; then
print_error "Version is required. Use -v or --version to specify."
show_usage
exit 1
fi
validate_version "$VERSION"
# Check we're in the right directory
check_project_directory
# Print banner
echo -e "${PURPLE}"
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ GenIText PyPI Update ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
print_info "Version: $VERSION"
print_info "Upload destination: $UPLOAD_DEST"
if [[ "$SKIP_UPLOAD" == true ]]; then
print_info "Upload: SKIPPED (build and test only)"
fi
echo
# Confirm before proceeding
if [[ "$SKIP_UPLOAD" == false ]]; then
if [[ "$UPLOAD_DEST" == "pypi" ]]; then
print_warning "This will upload to PRODUCTION PyPI!"
else
print_warning "This will upload to TestPyPI"
fi
read -p "Continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Operation cancelled"
exit 0
fi
fi
# Main execution
print_step "Starting PyPI update process..."
# Update version numbers
update_setup_py_version "$VERSION"
update_pyproject_version "$VERSION"
# Install build tools
install_build_tools
# Clean and build
clean_previous_builds
build_package
# Test
test_package
# Upload (if not skipped)
if [[ "$SKIP_UPLOAD" == false ]]; then
upload_to_pypi "$UPLOAD_DEST"
else
print_info "Upload skipped as requested"
fi
print_success "🎉 GenIText v$VERSION is ready!"
if [[ "$SKIP_UPLOAD" == false ]]; then
if [[ "$UPLOAD_DEST" == "pypi" ]]; then
print_info "Users can now install with: pip install --upgrade genitext"
else
print_info "Test installation: pip install --index-url https://test.pypi.org/simple/ genitext"
fi
fi
deactivate
rm -rf venv