1717# specific language governing permissions and limitations
1818# under the License.
1919#
20-
21- import sys , json , urllib .request , os , shutil , zipfile , tempfile
20+ import sys
21+ import requests
22+ import os
23+ import shutil
24+ import zipfile
25+ import tempfile
2226from pathlib import Path
2327
2428if len (sys .argv ) != 3 :
3943dest_path = sys .argv [2 ]
4044
4145workflow_run_url = LIST_URL % workflow_run_id
42- request = urllib .request .Request (workflow_run_url ,
43- headers = {'Accept' : ACCEPT_HEADER , 'Authorization' : 'Bearer ' + GITHUB_TOKEN })
44- with urllib .request .urlopen (request ) as response :
45- data = json .loads (response .read ().decode ("utf-8" ))
46- for artifact in data ['artifacts' ]:
47- name = artifact ['name' ]
48- url = artifact ['archive_download_url' ]
46+ headers = {'Accept' : ACCEPT_HEADER , 'Authorization' : f'Bearer { GITHUB_TOKEN } ' }
4947
50- print ('Downloading %s from %s' % (name , url ))
51- artifact_request = urllib .request .Request (url ,
52- headers = {'Authorization' : 'Bearer ' + GITHUB_TOKEN })
53- with urllib .request .urlopen (artifact_request ) as response :
54- tmp_zip = tempfile .NamedTemporaryFile (delete = False )
55- try :
56- #
57- shutil .copyfileobj (response , tmp_zip )
58- tmp_zip .close ()
48+ response = requests .get (workflow_run_url , headers = headers )
49+ response .raise_for_status ()
5950
60- dest_dir = os .path .join (dest_path , name )
61- Path (dest_dir ).mkdir (parents = True , exist_ok = True )
62- with zipfile .ZipFile (tmp_zip .name , 'r' ) as z :
63- z .extractall (dest_dir )
64- finally :
65- os .unlink (tmp_zip .name )
51+ data = response .json ()
52+ for artifact in data ['artifacts' ]:
53+ name = artifact ['name' ]
54+ url = artifact ['archive_download_url' ]
6655
67- for root , dirs , files in os .walk (dest_path , topdown = False ):
68- for name in files :
69- shutil .move (os .path .join (root , name ), dest_path )
70- if not os .listdir (root ):
71- os .rmdir (root )
56+ print (f'Downloading { name } from { url } ' )
57+ artifact_response = requests .get (url , headers = headers , stream = True )
58+ artifact_response .raise_for_status ()
7259
60+ with tempfile .NamedTemporaryFile (delete = False ) as tmp_zip :
61+ for chunk in artifact_response .iter_content (chunk_size = 8192 ):
62+ tmp_zip .write (chunk )
63+ tmp_zip_path = tmp_zip .name
7364
65+ try :
66+ dest_dir = os .path .join (dest_path , name )
67+ Path (dest_dir ).mkdir (parents = True , exist_ok = True )
68+ with zipfile .ZipFile (tmp_zip_path , 'r' ) as z :
69+ z .extractall (dest_dir )
70+ finally :
71+ os .unlink (tmp_zip_path )
7472
73+ for root , dirs , files in os .walk (dest_path , topdown = False ):
74+ for name in files :
75+ shutil .move (os .path .join (root , name ), dest_path )
76+ if not os .listdir (root ):
77+ os .rmdir (root )
0 commit comments