forked from jmecahansen/gcp-cloud-function-python-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack
executable file
·53 lines (36 loc) · 1.8 KB
/
pack
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
#!/bin/sh
# script to pack the application contents (files, scripts, etc.) into a .zip file for uploading as a Cloud Function
# fetch the current date
CURRENT_DATE=$(date +%Y-%m-%d)
# fetch the application file basename
BASENAME=$(basename $(pwd))
# count the number of versions of the filename
VERSION_COUNT=$(($(find ./dist -type f -name "*$BASENAME-$CURRENT_DATE*.zip" | wc -l)+1))
# compose the (final) filename
FILENAME=$BASENAME-$CURRENT_DATE-v$VERSION_COUNT.zip
# fetch (and compose) the list of pre-packaged Python libraries
PREPACKAGED_LIBRARIES=$(wget -q -O- https://raw.githubusercontent.com/GoogleCloudPlatform/buildpacks/af44f02668ce11175634137df2a406655addd69c/cmd/python/functions_framework/converter/requirements.txt | sed 's/==.*//g' | sed '/^$/d' | tr '\n' '|' | sed 's/.$//')
PREPACKAGED_LIBRARIES_PATTERN="/$PREPACKAGED_LIBRARIES|pytest/d"
# compose the list of source folders
SOURCE_FOLDERS=$(find * -type d | egrep -v '.*__|.*/|^.git|^(build|dist|tests|venv)')
# remove the contents of the build/ directory
rm -rf build/*
# copy the requirements.txt file to the build/ directory
cp requirements.txt build/
# remove local and/or unit testing references
sed -i.bak -E $PREPACKAGED_LIBRARIES_PATTERN build/requirements.txt
rm build/requirements.txt.bak
# install all dependencies into the build/ directory
pip install -t build/ -r build/requirements.txt
# remove all __pycache__ directory entries
find . -type d -name '*pycache*' -prune -exec rm -rf {} \;
# remove all .pytest_cache directory entries
find . -type d -name '*pytest_cache*' -prune -exec rm -rf {} \;
# copy all application contents to the build/ directory
cp -rf $SOURCE_FOLDERS main.py build/
# enter the build/ directory
cd build
# zip the application contents
zip -q -r -9 ../dist/$FILENAME .
# return to the application root directory
cd ..