<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import argparse <EOL> import os <EOL> import requests <EOL> import zipfile <EOL> def make_auth_headers ( ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . expanduser ( "<STR_LIT>" ) <EOL> if not os . path . exists ( path ) : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> with open ( path ) as f : <EOL> token = f . read ( ) . strip ( ) <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( token ) , <EOL> } <EOL> return headers <EOL> def download_latest_artifacts ( account_project , build_id ) : <EOL> """<STR_LIT>""" <EOL> if build_id is None : <EOL> url = "<STR_LIT>" . format ( account_project ) <EOL> else : <EOL> url = "<STR_LIT>" . format ( account_project , build_id ) <EOL> build = requests . get ( url , headers = make_auth_headers ( ) ) . json ( ) <EOL> jobs = build [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> print ( u"<STR_LIT>" . format ( build , len ( jobs ) ) ) <EOL> for job in jobs : <EOL> name = job [ '<STR_LIT:name>' ] <EOL> print ( u"<STR_LIT>" . format ( name , job ) ) <EOL> url = "<STR_LIT>" . format ( job [ '<STR_LIT>' ] ) <EOL> response = requests . get ( url , headers = make_auth_headers ( ) ) <EOL> artifacts = response . json ( ) <EOL> for artifact in artifacts : <EOL> is_zip = artifact [ '<STR_LIT:type>' ] == "<STR_LIT>" <EOL> filename = artifact [ '<STR_LIT>' ] <EOL> print ( u"<STR_LIT>" . format ( filename , artifact [ '<STR_LIT:size>' ] ) ) <EOL> url = "<STR_LIT>" . format ( job [ '<STR_LIT>' ] , filename ) <EOL> download_url ( url , filename , make_auth_headers ( ) ) <EOL> if is_zip : <EOL> unpack_zipfile ( filename ) <EOL> os . remove ( filename ) <EOL> def ensure_dirs ( filename ) : <EOL> """<STR_LIT>""" <EOL> dirname , _ = os . path . split ( filename ) <EOL> if dirname and not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> def download_url ( url , filename , headers ) : <EOL> """<STR_LIT>""" <EOL> ensure_dirs ( filename ) <EOL> response = requests . get ( url , headers = headers , stream = True ) <EOL> if response . status_code == <NUM_LIT:200> : <EOL> with open ( filename , '<STR_LIT:wb>' ) as f : <EOL> for chunk in response . iter_content ( <NUM_LIT:16> * <NUM_LIT> ) : <EOL> f . write ( chunk ) <EOL> else : <EOL> print ( u"<STR_LIT>" . format ( url , response ) ) <EOL> def unpack_zipfile ( filename ) : <EOL> """<STR_LIT>""" <EOL> with open ( filename , '<STR_LIT:rb>' ) as fzip : <EOL> z = zipfile . ZipFile ( fzip ) <EOL> for name in z . namelist ( ) : <EOL> print ( u"<STR_LIT>" . format ( name ) ) <EOL> ensure_dirs ( name ) <EOL> z . extract ( name ) <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> nargs = '<STR_LIT:?>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> args = parser . parse_args ( ) <EOL> download_latest_artifacts ( args . id , args . build ) </s>
