-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpython.sh
More file actions
190 lines (154 loc) · 7.59 KB
/
python.sh
File metadata and controls
190 lines (154 loc) · 7.59 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
#!/usr/bin/env bash
# This script clones the SDK repo and updates it with the generated API modules
# Pre-requisites: Java, goimports, Go
set -eo pipefail
ROOT_DIR=$(git rev-parse --show-toplevel)
SDK_REPO_LOCAL_PATH="${ROOT_DIR}/sdk-repo-updated"
OAS_REPO=https://github.com/stackitcloud/stackit-api-specifications
SERVICES_FOLDER="${SDK_REPO_LOCAL_PATH}/services"
GENERATOR_LOG_LEVEL="error" # Must be a Java log level (error, warn, info...)
generate_python_sdk() {
# Required parameters
local GENERATOR_JAR_PATH=$1
local GIT_HOST=$2
local GIT_USER_ID=$3
# Optional parameters
local GIT_REPO_ID=$4
local SDK_REPO_URL=$5
local SDK_BRANCH=$6
# Check required parameters
if [[ -z ${GIT_HOST} ]]; then
echo "! GIT_HOST not specified."
exit 1
fi
if [[ -z ${GIT_USER_ID} ]]; then
echo "! GIT_USER_ID id not specified."
exit 1
fi
# Check optional parameters and set defaults if not provided
if [[ -z ${GIT_REPO_ID} ]]; then
echo "GIT_REPO_ID not specified, default will be used."
GIT_REPO_ID="stackit-sdk-python"
fi
if [[ -z ${SDK_REPO_URL} ]]; then
echo "SDK_REPO_URL not specified, default will be used."
SDK_REPO_URL="https://github.com/stackitcloud/stackit-sdk-python.git"
fi
# Prepare folders
if [[ ! -d $SERVICES_FOLDER ]]; then
mkdir -p "$SERVICES_FOLDER"
fi
# Clone SDK repo
if [ -d "${SDK_REPO_LOCAL_PATH}" ]; then
echo "Old SDK repo clone was found, it will be removed."
rm -rf "${SDK_REPO_LOCAL_PATH}"
fi
git clone --quiet -b "${SDK_BRANCH}" "${SDK_REPO_URL}" "${SDK_REPO_LOCAL_PATH}"
# Install SDK project tools
cd "${ROOT_DIR}"
make project-tools LANGUAGE=python
# Backup of the current state of the SDK services dir (services/)
sdk_services_backup_dir=$(mktemp -d)
if [[ ! ${sdk_services_backup_dir} || -d {sdk_services_backup_dir} ]]; then
echo "! Unable to create temporary directory"
exit 1
fi
cleanup() {
rm -rf "${sdk_services_backup_dir}"
}
cp -a "${SERVICES_FOLDER}/." "${sdk_services_backup_dir}"
# Cleanup after we are done
trap cleanup EXIT
# Remove old contents of services dir (services/)
rm -rf "${SERVICES_FOLDER}"
warning=""
# Generate SDK for each service
for service_json in ${ROOT_DIR}/oas/legacy/*.json; do
service="${service_json##*/}"
service="${service%.json}"
# Remove invalid characters to ensure a valid Go pkg name
service="${service//-/}" # remove dashes
service="${service// /}" # remove empty spaces
service="${service//_/}" # remove underscores
service=$(echo "${service}" | tr '[:upper:]' '[:lower:]') # convert upper case letters to lower case
service=$(echo "${service}" | tr -d -c '[:alnum:]') # remove non-alphanumeric characters
if grep -E "^$service$" "${ROOT_DIR}/languages/python/blocklist.txt"; then
echo "Skipping blocklisted service ${service}"
warning+="Skipping blocklisted service ${service}\n"
continue
fi
echo ">> Generating \"${service}\" service..."
cd "${ROOT_DIR}"
mkdir -p "${SERVICES_FOLDER}/${service}/"
cp "${ROOT_DIR}/languages/python/.openapi-generator-ignore" "${SERVICES_FOLDER}/${service}/.openapi-generator-ignore"
# Run the generator
java -Dlog.level="${GENERATOR_LOG_LEVEL}" -jar "${jar_path}" generate \
--generator-name python \
--input-spec "${service_json}" \
--output "${SERVICES_FOLDER}/${service}" \
--package-name "stackit.${service}" \
--template-dir "${ROOT_DIR}/languages/python/templates/" \
--git-host "${GIT_HOST}" \
--git-user-id "${GIT_USER_ID}" \
--git-repo-id "${GIT_REPO_ID}" \
--global-property apis,models,modelTests=false,modelDocs=false,apiDocs=false,apiTests=false,supportingFiles \
--additional-properties=pythonPackageName="stackit-${service},removeEnumValuePrefix=false" >/dev/null \
--http-user-agent "stackit-sdk-python/${service}"
# Remove unnecessary files
rm "${SERVICES_FOLDER}/${service}/.openapi-generator-ignore"
rm -r "${SERVICES_FOLDER}/${service}/.openapi-generator/"
rm "${SERVICES_FOLDER}/${service}/stackit/__init__.py"
rm "${SERVICES_FOLDER}/${service}/.github/workflows/python.yml"
# Create source layout
mkdir "${SERVICES_FOLDER}/${service}/src"
mv "${SERVICES_FOLDER}/${service}/stackit/" "${SERVICES_FOLDER}/${service}/src/"
# If the service has a wait package files, move them inside the service folder
if [ -d "${sdk_services_backup_dir}/${service}/src/wait" ]; then
echo "Found ${service} \"wait\" package"
cp -r "${sdk_services_backup_dir}/${service}/src/wait" "${SERVICES_FOLDER}/${service}/src/wait"
fi
# If the service has a README.md file, move them inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/README.md" ]; then
echo "Found ${service} \"README.md\" file"
cp -r "${sdk_services_backup_dir}/${service}/README.md" "${SERVICES_FOLDER}/${service}/README.md"
fi
# If the service has a pyproject.toml file, move them inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/pyproject.toml" ]; then
echo "Found ${service} \"pyproject.toml\" file"
cp -r "${sdk_services_backup_dir}/${service}/pyproject.toml" "${SERVICES_FOLDER}/${service}/pyproject.toml"
fi
# If the service has a uv.lock file, move them inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/uv.lock" ]; then
echo "Found ${service} \"uv.lock\" file"
cp -r "${sdk_services_backup_dir}/${service}/uv.lock" "${SERVICES_FOLDER}/${service}/uv.lock"
fi
# If the service has a CHANGELOG file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/CHANGELOG.md" ]; then
echo "Found ${service} \"CHANGELOG\" file"
cp -r "${sdk_services_backup_dir}/${service}/CHANGELOG.md" "${SERVICES_FOLDER}/${service}/CHANGELOG.md"
fi
# If the service has a LICENSE file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/LICENSE.md" ]; then
echo "Found ${service} \"LICENSE\" file"
cp -r "${sdk_services_backup_dir}/${service}/LICENSE.md" "${SERVICES_FOLDER}/${service}/LICENSE.md"
fi
# If the service has a NOTICE file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/NOTICE.txt" ]; then
echo "Found ${service} \"NOTICE\" file"
cp -r "${sdk_services_backup_dir}/${service}/NOTICE.txt" "${SERVICES_FOLDER}/${service}/NOTICE.txt"
fi
# If the service has oas_commit file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/oas_commit" ]; then
echo "Found ${service} \"oas_commit\" file"
cp -r "${sdk_services_backup_dir}/${service}/oas_commit" "${SERVICES_FOLDER}/${service}/oas_commit"
fi
cd "${SERVICES_FOLDER}/${service}"
# Run formatter
isort .
autoimport --ignore-init-modules .
black .
done
if [[ -n "$warning" ]]; then
echo -e "\nSome of the services were skipped during creation!\n$warning"
fi
}