22from distutils .dir_util import copy_tree
33
44import base64
5- import glob
5+ import errno
66import hashlib
77import json
8+ import logging
89import os
910import shutil
1011import subprocess
1112import sys
1213import tempfile
1314import zipfile
1415
15- def build (src_dir ):
16+ def build (src_dir , output_path ):
1617 with tempfile .TemporaryDirectory () as build_dir :
1718 copy_tree (src_dir , build_dir )
1819 if os .path .exists (os .path .join (src_dir , 'requirements.txt' )):
@@ -27,19 +28,26 @@ def build(src_dir):
2728 check = True ,
2829 stdout = subprocess .DEVNULL ,
2930 )
30- artifact = tempfile .NamedTemporaryFile (delete = False )
31- make_archive (build_dir , artifact )
32- return artifact .name
31+ make_archive (build_dir , output_path )
32+ return output_path
3333
3434
35- def make_archive (src_dir , archive_file ):
36- with zipfile .ZipFile (archive_file , 'w' ) as archive :
35+ def make_archive (src_dir , output_path ):
36+ try :
37+ os .makedirs (os .path .dirname (output_path ))
38+ except OSError as e :
39+ if e .errno == errno .EEXIST :
40+ pass
41+ else :
42+ raise
43+
44+ with zipfile .ZipFile (output_path , 'w' ) as archive :
3745 for root , dirs , files in os .walk (src_dir ):
3846 for file in files :
3947 if file .endswith ('.pyc' ):
4048 break
4149 metadata = zipfile .ZipInfo (
42- os .path .join (root , file ).replace (src_dir , '' )
50+ os .path .join (root , file ).replace (src_dir , '' ). lstrip ( os . sep )
4351 )
4452 with open (os .path .join (root , file ), 'rb' ) as f :
4553 data = f .read ()
@@ -48,17 +56,19 @@ def make_archive(src_dir, archive_file):
4856 data
4957 )
5058
51- def get_hash (archive_file ):
59+ def get_hash (output_path ):
5260 '''
5361 Return base64 encoded sha256 hash of archive file
5462 '''
55- with open (archive_file , 'rb' ) as f :
63+ with open (output_path , 'rb' ) as f :
5664 h = hashlib .sha256 ()
5765 h .update (f .read ())
5866 return base64 .standard_b64encode (h .digest ()).decode ('utf-8' , 'strict' )
5967
6068
6169if __name__ == '__main__' :
70+ logging .basicConfig (level = 'DEBUG' )
6271 query = json .loads (sys .stdin .read ())
63- archive = build (query ['src_dir' ])
72+ logging .debug (query )
73+ archive = build (query ['src_dir' ], query ['output_path' ])
6474 print (json .dumps ({'archive' : archive , "base64sha256" :get_hash (archive )}))
0 commit comments