Repository navigation
Expand file tree
/
Copy pathcreateZip.py
More file actions
125 lines (102 loc) · 4.49 KB
/
Copy pathcreateZip.py
File metadata and controls
125 lines (102 loc) · 4.49 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
#!.venv/bin/python3
#######################################################################
# Create zip file for function code and dependencies
# - create dependencies if requirements.txt changed
# - create zip file with code from src and dependencies
#
# The zip file will have the following structure:
# .
# ├── dependencies
# │ ├── <installed packages from requirements.txt>
# │ └── ...
# ├── src
# │ ├── app.py
# │ ├── api
# │ │ └── ...
# │ └── static
# │ └── ...
# └── bootstrap
#
#######################################################################
import os
import subprocess
import hashlib
import shutil
from zipfile import ZIP_DEFLATED, ZipFile
TARGET_FOLDER = "./target"
OUTPUT_ZIP = f"{TARGET_FOLDER}/code.zip"
ZIP_HASH_FILE = f"{TARGET_FOLDER}/.zip_hash"
REQUIREMENTS_FILE = "./requirements.txt"
REQUIREMENTS_HASH_FILE = f"{TARGET_FOLDER}/.requirements_hash"
DEPENDENCIES = "dependencies"
DEPENDENCIES_FOLDER = f"{TARGET_FOLDER}/{DEPENDENCIES}"
####################################################################
# Zip Code and dependencies
####################################################################
def createZippedFunctionCode():
requirements_hash = hashlib.md5(open(REQUIREMENTS_FILE, "rb").read()).hexdigest()
OLD_requirements_hash = ""
if os.path.exists(REQUIREMENTS_HASH_FILE):
with open(REQUIREMENTS_HASH_FILE, "r") as f:
OLD_requirements_hash = f.read().strip()
if requirements_hash != OLD_requirements_hash:
print(f"Changes in {REQUIREMENTS_FILE} - updating dependencies")
shutil.rmtree({TARGET_FOLDER}, ignore_errors=True)
os.makedirs(DEPENDENCIES_FOLDER, exist_ok=True)
# create dependencies
p = subprocess.run(
[f"python3 -m pip install -t {DEPENDENCIES_FOLDER} -r {REQUIREMENTS_FILE}"],
shell=True,
)
if p.returncode != 0:
print(f"Error installing dependencies, returncode {p.returncode}")
exit(p.returncode)
# write new hash
with open(REQUIREMENTS_HASH_FILE, "w") as f:
f.write(requirements_hash)
else:
print(f"No changes in {REQUIREMENTS_FILE} - skip updating dependencies")
CURRENT_FOLDER=os.getcwd()
print(f"Current folder: {CURRENT_FOLDER}")
with ZipFile(OUTPUT_ZIP, "w", ZIP_DEFLATED) as zip:
# add files from folder src
src = "./src"
for dirname, subdirs, files in os.walk(src):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[absname.rindex(f"{CURRENT_FOLDER}") + len(f"{CURRENT_FOLDER}") :]
# print("zipping %s as %s" % (os.path.join(dirname, filename), arcname))
if (
"__pycache__" not in absname
and "__pycache__" not in arcname
and "bootstrap" not in arcname
):
#print("zipping %s as %s" % (os.path.join(dirname, filename), arcname))
zip.write(absname, arcname)
# add files from dependencies
FULL_DEPENDENCIES_FOLDER=f"{CURRENT_FOLDER}/target/{DEPENDENCIES}"
for dirname, subdirs, files in os.walk(DEPENDENCIES_FOLDER):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[
# remove all parent folder names from absname
absname.rindex(f"{FULL_DEPENDENCIES_FOLDER}/") + len(f"{FULL_DEPENDENCIES_FOLDER}/") :
]
arcname = f"dependencies/{arcname}"
if (
#not arcname.startswith("pip") and
not arcname.startswith("_distutils")
and not arcname.startswith("setuptools")
#and ".dist-info" not in absname
and "__pycache__" not in absname
and "__pycache__" not in arcname
):
#print("zipping %s as %s --- %s" % (os.path.join(dirname, filename), arcname, absname))
zip.write(absname, arcname)
zip.write("./src/bootstrap", "bootstrap")
print(f"created: {OUTPUT_ZIP}")
zip_hash = hashlib.md5(open(OUTPUT_ZIP, "rb").read()).hexdigest()
with open(ZIP_HASH_FILE, "w") as f:
f.write(zip_hash)
if __name__ == "__main__":
createZippedFunctionCode()