forked from osmlab/atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.sh
executable file
·166 lines (141 loc) · 5.75 KB
/
package.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
#!/usr/bin/env bash
# THIS ENTIRE SCRIPT IS A MASSIVE HACK
# THIS SHOULD REALLY BE DONE WITH GRADLE
# general case script abort if a command fails
# this can be overridden with a custom error message using '|| err_shutdown'
set -e
set -o pipefail
### define utility functions ###
################################
err_shutdown() {
echo "package.sh: ERROR: $1"
deactivate
exit 1
}
### check to prevent users from running this script directly ###
################################################################
if [ "$1" != "ranFromGradle" ];
then
err_shutdown "this script should be run using the atlas gradle task 'packagePyatlas'"
fi
### set up variables to store directory names ###
#################################################
gradle_project_root_dir="$(pwd)"
pyatlas_dir="pyatlas"
pyatlas_srcdir="pyatlas"
doc_dir="doc"
pyatlas_root_dir="$gradle_project_root_dir/$pyatlas_dir"
venv_path="$pyatlas_root_dir/__pyatlas_venv__"
protofiles_dir="$gradle_project_root_dir/src/main/proto"
### abort the script if the pyatlas source folder is not present ###
####################################################################
if [ ! -d "$pyatlas_root_dir/$pyatlas_srcdir" ];
then
err_shutdown "pyatlas source folder not found"
fi
### determine if wget is installed ###
######################################
if command -v wget ;
then
wget_command="$(command -v wget)"
else
err_shutdown "'command -v wget' returned non-zero exit status: install wget to run this script"
fi
### download protoc and compile the atlas proto files into python ###
#####################################################################
echo "Preparing protoc..."
# hack to grab the protoc version from dependencies.gradle
#protoc_version=$(grep 'protoc' "$gradle_project_root_dir/dependencies.gradle" | awk -F':' '{print $2; exit}' | tr -d "'" | tr -d ',')
# TODO HACK FIXME hardcoding a proto3 version until we can upgrade atlas, proto2 is not compat with python3
protoc_version='3.11.1'
protoc_path="$pyatlas_root_dir/protoc"
# detemine what platform we are on
if [ ! -f "$protoc_path" ];
then
if [ "$(uname)" == "Darwin" ];
then
download_link="https://repo1.maven.org/maven2/com/google/protobuf/protoc/${protoc_version}/protoc-${protoc_version}-osx-x86_64.exe"
"$wget_command" "$download_link" -O "$protoc_path" || err_shutdown "wget of '$download_link' failed"
elif [ "$(uname)" == "Linux" ];
then
download_link="https://repo1.maven.org/maven2/com/google/protobuf/protoc/${protoc_version}/protoc-${protoc_version}-linux-x86_64.exe"
"$wget_command" "$download_link" -O "$protoc_path" || err_shutdown "wget of '$download_link' failed"
else
err_shutdown "unrecognized platform $(uname)"
fi
fi
chmod 700 "$protoc_path"
# complicated mess to handle case where a proto filename has a space
# basically, 'find' outputs each file separated by a NUL terminator
# read -r -d '' reads raw input delimited by NUL characters
while IFS= read -r -d '' protofile
do
"$protoc_path" "$protofile" --proto_path="$protofiles_dir" --python_out="$pyatlas_root_dir/$pyatlas_srcdir/autogen" || err_shutdown "protoc invocation failed"
done < <(find "$protofiles_dir" -type f -name "*.proto" -print0)
# TODO HACK FIXME this is to fix badly generated import statements by proto3 library
# See this solution: https://github.com/protocolbuffers/protobuf/issues/4546#issuecomment-384252863
# FIXME this will fail if source file has a space
pushd "$pyatlas_root_dir/$pyatlas_srcdir/autogen"
if [ "$(uname)" == "Darwin" ];
then
sed -i '' 's/^\(import.*pb2\)/from . \1/g' ./*.py
elif [ "$(uname)" == "Linux" ];
then
sed --in-place="" "s/^\(import.*pb2\)/from . \1/g" ./*.py
else
err_shutdown "unrecognized platform $(uname)"
fi
popd
### build the module and documentation ###
##########################################
# start the venv
if [ ! -d "$venv_path" ];
then
err_shutdown "missing $venv_path"
fi
# shellcheck source=/dev/null
source "$venv_path/bin/activate"
# copy the LICENSE to the pyatlas folder
cp "$gradle_project_root_dir/LICENSE" "$pyatlas_root_dir"
# grab the build version from gradle.properties and inject it into setup.py
# remove the -SNAPSHOT text if present
atlas_version=$(grep "version=" "$gradle_project_root_dir/gradle.properties" | cut -f2 -d "=" | sed 's/-SNAPSHOT//g')
# GNU and BSD sed have different "in-place" flag syntax
if [ "$(uname)" == "Darwin" ];
then
sed -i "" "s/version=.*/version=\"$atlas_version\",/" "$pyatlas_root_dir/setup.py"
elif [ "$(uname)" == "Linux" ];
then
sed --in-place="" "s/version=.*/version=\"$atlas_version\",/" "$pyatlas_root_dir/setup.py"
else
err_shutdown "unrecognized platform $(uname)"
fi
# enter the pyatlas project directory so module metadata is generated correctly
pushd "$pyatlas_root_dir"
echo "Building and packaging pyatlas module..."
python "setup.py" sdist -d "dist" bdist_wheel -d "dist"
# self-install and create the docs
pip install -e .
# hack to make pydoc work
export PYTHONPATH="$PYTHONPATH:$pyatlas_root_dir/$pyatlas_srcdir:$pyatlas_root_dir/$pyatlas_srcdir/autogen"
# FIXME this will fail if source file has a space
pydoc -w "$pyatlas_srcdir"/*.py
mv ./*.html "$doc_dir"
# this would be the correct way, but for some reason the 'find exec' fails on atlas.py
#find "$pyatlas_srcdir"/*.py -exec pydoc -w {} \;
#find "$pyatlas_root_dir/"*.html -exec mv {} "$doc_dir" \;
# get back to gradle project directory
popd
# reset version field in setup.py
# GNU and BSD sed have different "in-place" flag syntax
if [ "$(uname)" == "Darwin" ];
then
sed -i "" "s/version=.*/version=/" "$pyatlas_root_dir/setup.py"
elif [ "$(uname)" == "Linux" ];
then
sed --in-place="" "s/version=.*/version=/" "$pyatlas_root_dir/setup.py"
else
err_shutdown "unrecognized platform $(uname)"
fi
# shutdown the venv
deactivate