Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 789f085

Browse files
Henry McConvillemichael-dickinson-sainsburys
Henry McConville
authored andcommitted
Create destination directory if it doesn't exist
1 parent fd1e36f commit 789f085

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

zip_file/archive.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ def get_all_file_paths(directory):
2525
return file_paths
2626

2727

28-
def main():
29-
data = json.load(sys.stdin)
30-
file_paths = get_all_file_paths(data['source_dir'])
28+
def create_zip_files(data, file_paths):
3129
try:
3230
with zipfile.ZipFile(data['output_path'], mode='w') as zf:
3331
for file_path in sorted(file_paths, key=lambda d: d['filename']):
@@ -46,7 +44,6 @@ def main():
4644
info.external_attr = 0o777 << 16
4745
zf.writestr(info, file_bytes)
4846
zf.close()
49-
finally:
5047
zip_data = {
5148
"output_path":
5249
str(data['output_path']),
@@ -61,7 +58,28 @@ def main():
6158
"output_base64sha256":
6259
str(base64sha256(data['output_path']))
6360
}
64-
print(json.dumps(zip_data))
61+
return zip_data
62+
63+
except Exception as e:
64+
sys.stderr.write(str(e))
65+
sys.exit(1)
66+
67+
68+
def main():
69+
# Read JSON data from stdin
70+
data = json.load(sys.stdin)
71+
72+
# Create build directory if it doesn't exist
73+
build_directory = os.path.dirname(data['output_path'])
74+
if not os.path.exists(build_directory):
75+
os.makedirs(build_directory)
76+
77+
file_paths = get_all_file_paths(data['source_dir'])
78+
79+
zip_data = create_zip_files(data, file_paths)
80+
81+
# Output result to stdout
82+
print(json.dumps(zip_data))
6583

6684

6785
if __name__ == "__main__":

0 commit comments

Comments
 (0)