-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_poetry_app.sh
executable file
·233 lines (197 loc) · 6.38 KB
/
create_poetry_app.sh
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
#!/usr/bin/env sh
set -e
# This script automates the process of creating a new Python project using Poetry.
# It allows the user to specify the project name, author name, author email, Python version,
# the upper limit of the Python version, and whether to create the virtual environment
# inside the project directory. The script also updates the pyproject.toml file
# with the specified Python version, description, and authors.
# Default values
DEFAULT_PROJECT_NAME="projectname"
DEFAULT_MY_NAME="main"
DEFAULT_PYTHON_VERSION="^3.12"
DEFAULT_VENV_CONFIG="true"
DEFAULT_DESCRIPTION="description"
DEFAULT_AUTHOR_NAME="Your Name"
DEFAULT_AUTHOR_EMAIL="[email protected]"
# Initialize variables with default values
PROJECT_NAME=""
MY_NAME=""
PYTHON_VERSION=""
UPPER_PYTHON_VERSION=""
VENV_CONFIG=""
DESCRIPTION=""
AUTHOR_NAME=""
AUTHOR_EMAIL=""
USE_DEFAULTS=false
# Function to validate email format
validate_email() {
if echo "$1" | grep -Eq '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'; then
return 0
else
return 1
fi
}
# Function to clean and validate upper Python version format
clean_upper_version() {
if [ -z "$1" ]; then
echo ""
else
CLEANED_VERSION=$(echo "$1" | tr -d -c '0-9.')
if echo "$CLEANED_VERSION" | grep -Eq '^[0-9]+\.[0-9]+(\.[0-9]+)?$'; then
echo "$CLEANED_VERSION"
else
echo "Invalid upper Python version format. It should be in the format x.y.z." >&2
exit 1
fi
fi
}
# Function to convert package name to valid format
convert_package_name() {
echo "$1" | tr '-' '_'
}
# Function to sanitize input before writing to pyproject.toml
sanitize_input() {
echo "$1" | sed 's/[^0-9.]//g'
}
# Function to parse command line options
parse_options() {
while getopts "yp:n:v:u:d:a:e:c:" opt; do
case $opt in
y) USE_DEFAULTS=true ;;
p) PROJECT_NAME=$OPTARG ;;
n) MY_NAME=$OPTARG ;;
v) PYTHON_VERSION=$OPTARG ;;
u) UPPER_PYTHON_VERSION=$OPTARG ;;
d) DESCRIPTION=$OPTARG ;;
a) AUTHOR_NAME=$OPTARG ;;
e) AUTHOR_EMAIL=$OPTARG ;;
c) VENV_CONFIG=$OPTARG ;;
\?) echo "Invalid option -$OPTARG" >&2 ;;
esac
done
}
# Function to prompt for input values if not provided via command line options or use default values
prompt_for_inputs() {
if [ -z "$PROJECT_NAME" ]; then
echo "Enter project name (default: $DEFAULT_PROJECT_NAME):"
read INPUT
PROJECT_NAME=${INPUT:-$DEFAULT_PROJECT_NAME}
fi
if [ "$USE_DEFAULTS" = false ]; then
if [ -z "$MY_NAME" ]; then
echo "Enter your package name (default: $DEFAULT_MY_NAME):"
read INPUT
MY_NAME=${INPUT:-$DEFAULT_MY_NAME}
fi
MY_NAME=$(convert_package_name "$MY_NAME")
if [ -z "$PYTHON_VERSION" ]; then
echo "Enter Python version (default: $DEFAULT_PYTHON_VERSION, e.g., 3.12 or ^3.12 or ~3.12):"
read INPUT
PYTHON_VERSION="${INPUT:-$DEFAULT_PYTHON_VERSION}"
fi
if [ -z "$UPPER_PYTHON_VERSION" ]; then
echo "Enter upper Python version limit (optional, numbers only, e.g., 3.33):"
read INPUT
UPPER_PYTHON_VERSION=$(clean_upper_version "${INPUT}")
fi
if [ -z "$DESCRIPTION" ]; then
echo "Enter project description (default: $DEFAULT_DESCRIPTION):"
read INPUT
DESCRIPTION=${INPUT:-$DEFAULT_DESCRIPTION}
fi
if [ -z "$AUTHOR_NAME" ]; then
echo "Enter author name (default: $DEFAULT_AUTHOR_NAME):"
read INPUT
AUTHOR_NAME=${INPUT:-$DEFAULT_AUTHOR_NAME}
fi
if [ -z "$AUTHOR_EMAIL" ]; then
while true; do
echo "Enter author email (default: $DEFAULT_AUTHOR_EMAIL):"
read INPUT
AUTHOR_EMAIL=${INPUT:-$DEFAULT_AUTHOR_EMAIL}
if [ -z "$AUTHOR_EMAIL" ] || validate_email "$AUTHOR_EMAIL"; then
if [ -z "$AUTHOR_EMAIL" ]; then
AUTHOR_EMAIL=$DEFAULT_AUTHOR_EMAIL
fi
break
else
echo "Invalid email format. Please enter a valid email address."
fi
done
fi
if [ -z "$VENV_CONFIG" ]; then
echo "Set virtualenvs.in-project to true/false (default: $DEFAULT_VENV_CONFIG):"
read INPUT
VENV_CONFIG=${INPUT:-$DEFAULT_VENV_CONFIG}
fi
else
MY_NAME=$DEFAULT_MY_NAME
PYTHON_VERSION=$DEFAULT_PYTHON_VERSION
UPPER_PYTHON_VERSION=""
AUTHOR_NAME=$DEFAULT_AUTHOR_NAME
AUTHOR_EMAIL=$DEFAULT_AUTHOR_EMAIL
VENV_CONFIG=$DEFAULT_VENV_CONFIG
fi
}
# Function to set dependency version based on PYTHON_VERSION and UPPER_PYTHON_VERSION
set_dependency_version() {
if [ -n "$UPPER_PYTHON_VERSION" ]; then
DEPENDENCY_VERSION=">=$(sanitize_input "$PYTHON_VERSION"),<$(clean_upper_version "$UPPER_PYTHON_VERSION")"
else
if echo "$PYTHON_VERSION" | grep -q '^[^0-9]'; then
DEPENDENCY_VERSION="$PYTHON_VERSION"
else
DEPENDENCY_VERSION="^$PYTHON_VERSION"
fi
fi
}
# Function to create the pyproject.toml file
create_pyproject_toml() {
AUTHOR="${AUTHOR_NAME} <${AUTHOR_EMAIL}>"
cat <<EOL > pyproject.toml
[tool.poetry]
name = "$MY_NAME"
version = "0.1.0"
description = "$DESCRIPTION"
authors = ["$AUTHOR"]
readme = "README.md"
packages = [{include = "$MY_NAME", from = "src"}]
[tool.poetry.dependencies]
python = "$DEPENDENCY_VERSION"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
EOL
}
# Main function to create the project
main() {
# Parse command line options
parse_options "$@"
# Prompt for inputs if necessary
prompt_for_inputs
# Set dependency version
set_dependency_version
# Create a new Poetry project
poetry new "$PROJECT_NAME" --name "$MY_NAME" --src
# Change to the project directory
cd "$PROJECT_NAME" || exit
# Create pyproject.toml
create_pyproject_toml
# Output the contents of pyproject.toml to the console
echo "Generated pyproject.toml:"
cat pyproject.toml
# Configure Poetry to create virtual environments inside the project directory if specified
poetry config virtualenvs.in-project "$VENV_CONFIG"
# Use the specified Python version for the project's virtual environment
ESCAPED_PYTHON_VERSION=$(sanitize_input "$PYTHON_VERSION")
echo "Using Python $ESCAPED_PYTHON_VERSION for poetry env use"
poetry env use "$ESCAPED_PYTHON_VERSION"
# Install the project dependencies
poetry install
# Activate the virtual environment
. .venv/bin/activate
# Print a success message
echo "Project '$PROJECT_NAME' created and virtual environment activated with Python $PYTHON_VERSION"
}
# Run the main function
main "$@"