diff --git a/taskcluster_util/model/login_policy.py b/taskcluster_util/model/login_policy.py new file mode 100644 index 0000000..494bea6 --- /dev/null +++ b/taskcluster_util/model/login_policy.py @@ -0,0 +1,16 @@ +import logging + +LOGGING_POLICY = { + 'default': { + 'level': logging.INFO, + 'format': '%(levelname)s: %(message)s' + }, + 'verbose': { + 'level': logging.DEBUG, + 'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + }, + 'no_logs': { + 'level': logging.CRITICAL, + 'format': '%(message)s' + } +} diff --git a/taskcluster_util/taskcluster_download.py b/taskcluster_util/taskcluster_download.py index 8c4a94b..84682d5 100755 --- a/taskcluster_util/taskcluster_download.py +++ b/taskcluster_util/taskcluster_download.py @@ -10,6 +10,7 @@ from util.finder import * from util.downloader import * from model.credentials import Credentials +from model.login_policy import LOGGING_POLICY logger = logging.getLogger(__name__) @@ -24,10 +25,12 @@ def __init__(self, connection_options=None): self.connection_options = connection_options self.namespace = None self.task_id = None - self.aritfact_name = None + self.artifact_name = None self.dest_dir = None self.artifact_downloader = None self.taskcluster_credentials = os.path.join(os.path.expanduser('~'), 'tc_credentials.json') + self.should_display_signed_url_only = False + self.is_verbose = False def parser(self): # argument parser @@ -59,6 +62,7 @@ def parser(self): help='The artifact name on Taskcluster') artifact_group.add_argument('-d', '--dest-dir', action='store', dest='dest_dir', help='The dest folder (default: current working folder)') + artifact_group.add_argument('-u', '--signed-url-only', action='store_true', help='Retrieve the signed url and display it. No download is done') parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', default=False, help='Turn on verbose output, with all the debug logger.') return parser.parse_args(sys.argv[1:]) @@ -69,14 +73,28 @@ def cli(self): """ # parser the argv options = self.parser() - # setup the logging config - if options.verbose is True: - verbose_formatter = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - logging.basicConfig(level=logging.DEBUG, format=verbose_formatter) + + self.namespace = options.namespace + self.task_id = options.task_id + self.artifact_name = options.aritfact_name + self.dest_dir = options.dest_dir + self.is_verbose = options.verbose + self.should_display_signed_url_only = options.signed_url_only + + self._configure_login() + self._check_crendentials_file(options) + return self + + def _configure_login(self): + if self.is_verbose is True: + logging_config = LOGGING_POLICY['verbose'] + elif self.should_display_signed_url_only is True: + logging_config = LOGGING_POLICY['no_logs'] else: - formatter = '%(levelname)s: %(message)s' - logging.basicConfig(level=logging.INFO, format=formatter) - # check credentials file + logging_config = LOGGING_POLICY['default'] + logging.basicConfig(level=logging_config['level'], format=logging_config['format']) + + def _check_crendentials_file(self, options): try: abs_credentials_path = os.path.abspath(options.credentials) credentials = Credentials.from_file(abs_credentials_path) @@ -85,12 +103,6 @@ def cli(self): except Exception as e: logger.warning('No credentials. Run with "--help" for more information.') logger.debug(e) - # assign the variable - self.namespace = options.namespace - self.task_id = options.task_id - self.aritfact_name = options.aritfact_name - self.dest_dir = options.dest_dir - return self def show_latest_artifacts(self, task_id): """ @@ -127,14 +139,16 @@ def run(self): task_id = self.task_id self.artifact_downloader = Downloader(self.connection_options) - if self.aritfact_name is None: + if self.artifact_name is None: # no artifact_name, then get the latest artifacts list self.show_latest_artifacts(task_id) + elif self.should_display_signed_url_only is True: + print self.artifact_downloader.get_signed_url(task_id, self.artifact_name) else: # has artifact_name, then download it - logger.info('Downloading latest artifact [{}] of TaskID [{}] ...'.format(self.aritfact_name, task_id)) - local_file = self.artifact_downloader.download_latest_artifact(task_id, self.aritfact_name, self.dest_dir) - logger.info('Download [{}] from TaskID [{}] to [{}] done.'.format(self.aritfact_name, task_id, local_file)) + logger.info('Downloading latest artifact [{}] of TaskID [{}] ...'.format(self.artifact_name, task_id)) + local_file = self.artifact_downloader.download_latest_artifact(task_id, self.artifact_name, self.dest_dir) + logger.info('Download [{}] from TaskID [{}] to [{}] done.'.format(self.artifact_name, task_id, local_file)) def main(): diff --git a/taskcluster_util/util/downloader.py b/taskcluster_util/util/downloader.py index 5d652db..284914c 100644 --- a/taskcluster_util/util/downloader.py +++ b/taskcluster_util/util/downloader.py @@ -32,6 +32,12 @@ def get_latest_artifacts(self, task_id): ret = self.queue.listLatestArtifacts(task_id) return ret + def get_signed_url(self, task_id, full_filename): + # if there is no credentials, then try to download artifact as public file + return self.queue.buildSignedUrl('getLatestArtifact', task_id, full_filename) \ + if self.queue._hasCredentials() \ + else self.queue.buildUrl('getLatestArtifact', task_id, full_filename) + def download_latest_artifact(self, task_id, full_filename, dest_dir): """ Download latest artifact. @@ -43,12 +49,7 @@ def download_latest_artifact(self, task_id, full_filename, dest_dir): temp_dir = tempfile.mkdtemp(prefix='tmp_tcdl_') base_filename = os.path.basename(full_filename) - # if there is no credentials, then try to download artifact as public file. - # - Has credentials => buildSignedUrl() - # - No credentials => buildUrl() - signed_url = self.queue.buildSignedUrl('getLatestArtifact', task_id, full_filename) \ - if self.queue._hasCredentials() \ - else self.queue.buildUrl('getLatestArtifact', task_id, full_filename) + signed_url = self.get_signed_url(task_id, full_filename) url_handler = urllib2.urlopen(signed_url) total_length = 0