-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·369 lines (305 loc) · 8.92 KB
/
deploy.sh
File metadata and controls
executable file
·369 lines (305 loc) · 8.92 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash
# CRANTpy PyPI Deployment Script
# This script builds and deploys CRANTpy to PyPI using Poetry
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'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${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"
}
# Function to check if poetry is available
check_poetry() {
if ! command -v poetry &> /dev/null; then
print_error "Poetry is not installed or not in PATH"
print_error "Please install poetry: https://python-poetry.org/docs/#installation"
exit 1
fi
print_success "Poetry found"
}
# Function to check if we're in the right directory
check_directory() {
if [[ ! -f "pyproject.toml" ]] || [[ ! -d "src/crantpy" ]]; then
print_error "This script must be run from the project root directory"
print_error "Expected to find: pyproject.toml, src/crantpy/"
exit 1
fi
print_success "Project structure verified"
}
# Function to check git status
check_git_status() {
if [[ -n $(git status --porcelain) ]]; then
print_warning "There are uncommitted changes in your repository"
read -p "Do you want to continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_error "Deployment cancelled"
exit 1
fi
fi
print_success "Git status checked"
}
# Function to get current version
get_current_version() {
local version=$(poetry version --short)
echo "$version"
}
# Function to bump version
bump_version() {
local bump_type="$1"
print_status "Current version: $(get_current_version)"
case $bump_type in
major|minor|patch)
poetry version "$bump_type"
;;
*)
print_error "Invalid version bump type. Use: major, minor, or patch"
exit 1
;;
esac
local new_version=$(get_current_version)
print_success "Version bumped to: $new_version"
return 0
}
# Function to update lazy imports
update_imports() {
print_status "Updating lazy imports with mkinit..."
if ! poetry run mkinit --lazy_loader src/crantpy --recursive --inplace; then
print_error "Failed to update lazy imports"
exit 1
fi
print_success "Lazy imports updated successfully"
}
# Function to run tests
run_tests() {
print_status "Running tests..."
if ! poetry run pytest tests/ -v; then
print_error "Tests failed"
exit 1
fi
print_success "All tests passed"
}
# Function to check code quality
check_code_quality() {
print_status "Checking code quality..."
# Format code
print_status "Formatting code with Black..."
poetry run black src/ tests/
# Check linting (fix what can be fixed automatically)
print_status "Checking and fixing code with Ruff..."
poetry run ruff check src/ tests/ --fix || true
# Check remaining issues (non-blocking for now due to mkinit generated code)
print_status "Final code quality check..."
if poetry run ruff check src/ tests/ --quiet; then
print_success "All code quality checks passed"
else
print_warning "Some code quality issues remain (mostly from auto-generated code)"
print_warning "Continuing with deployment..."
fi
}
# Function to build documentation
build_docs() {
print_status "Building documentation..."
if ! ./build_docs.sh --clean; then
print_error "Documentation build failed"
exit 1
fi
print_success "Documentation built successfully"
}
# Function to build package
build_package() {
print_status "Building package with Poetry..."
# Clean previous builds
rm -rf dist/
if ! poetry build; then
print_error "Package build failed"
exit 1
fi
print_success "Package built successfully"
# Show build artifacts
print_status "Build artifacts:"
ls -la dist/
}
# Function to check package
check_package() {
print_status "Checking package with twine..."
if ! poetry run twine check dist/*; then
print_error "Package check failed"
exit 1
fi
print_success "Package check passed"
}
# Function to deploy to TestPyPI
deploy_test() {
print_status "Deploying to TestPyPI..."
if ! poetry config repositories.testpypi https://test.pypi.org/legacy/; then
print_error "Failed to configure TestPyPI repository"
exit 1
fi
if ! poetry publish -r testpypi; then
print_error "Deployment to TestPyPI failed"
exit 1
fi
local version=$(get_current_version)
print_success "Successfully deployed to TestPyPI"
print_status "Test installation: pip install -i https://test.pypi.org/simple/ crantpy==$version"
}
# Function to deploy to PyPI
deploy_pypi() {
print_status "Deploying to PyPI..."
print_warning "This will deploy to the LIVE PyPI repository!"
read -p "Are you sure you want to continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_error "Deployment cancelled"
exit 1
fi
if ! poetry publish; then
print_error "Deployment to PyPI failed"
exit 1
fi
local version=$(get_current_version)
print_success "Successfully deployed to PyPI"
print_status "Installation: pip install crantpy==$version"
}
# Function to create git tag
create_git_tag() {
local version=$(get_current_version)
local tag="v$version"
print_status "Creating git tag: $tag"
git add .
git commit -m "chore: bump version to $version" || true
git tag -a "$tag" -m "Release version $version"
print_success "Git tag created: $tag"
read -p "Push tag to remote? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git push origin main
git push origin "$tag"
print_success "Tag pushed to remote"
fi
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS] <COMMAND>"
echo ""
echo "Commands:"
echo " test-deploy Deploy to TestPyPI (recommended first)"
echo " deploy Deploy to PyPI (production)"
echo " build-only Build package without deploying"
echo ""
echo "Options:"
echo " --bump LEVEL Bump version (major|minor|patch)"
echo " --skip-tests Skip running tests"
echo " --skip-docs Skip building documentation"
echo " --skip-checks Skip code quality checks"
echo " --help, -h Show this help message"
echo ""
echo "Examples:"
echo " $0 --bump patch test-deploy # Bump patch version and deploy to TestPyPI"
echo " $0 --bump minor deploy # Bump minor version and deploy to PyPI"
echo " $0 build-only # Just build the package"
}
# Parse command line arguments
BUMP_VERSION=""
SKIP_TESTS=false
SKIP_DOCS=false
SKIP_CHECKS=false
COMMAND=""
while [[ $# -gt 0 ]]; do
case $1 in
--bump)
BUMP_VERSION="$2"
shift 2
;;
--skip-tests)
SKIP_TESTS=true
shift
;;
--skip-docs)
SKIP_DOCS=true
shift
;;
--skip-checks)
SKIP_CHECKS=true
shift
;;
--help|-h)
show_usage
exit 0
;;
test-deploy|deploy|build-only)
COMMAND="$1"
shift
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Validate command
if [[ -z "$COMMAND" ]]; then
print_error "No command specified"
show_usage
exit 1
fi
# Main execution
main() {
print_status "Starting CRANTpy deployment process..."
# Pre-flight checks
check_poetry
check_directory
check_git_status
# Bump version if requested
if [[ -n "$BUMP_VERSION" ]]; then
bump_version "$BUMP_VERSION"
fi
# Update lazy imports (always required)
update_imports
# Optional quality checks
if [[ "$SKIP_CHECKS" == false ]]; then
check_code_quality
fi
# Optional tests
if [[ "$SKIP_TESTS" == false ]]; then
run_tests
fi
# Optional documentation build
if [[ "$SKIP_DOCS" == false ]]; then
build_docs
fi
# Build package
build_package
check_package
# Deploy based on command
case $COMMAND in
test-deploy)
deploy_test
;;
deploy)
deploy_pypi
create_git_tag
;;
build-only)
print_success "Package built successfully. Files in dist/"
;;
esac
print_success "Deployment process completed!"
}
# Run main function
main