Apr-24-2017, 05:15 PM
Great, thank you all !!!
This is my new version for now.
I was thinking should i use kwargs for passing parameters
and can i concatenate those beginning and end prints.
This is my new version for now.
sys.path.insert(0, './libs') from artifactory import ArtifactoryPath, md5sum, _ArtifactoryAccessor server = '192.168.10.2' server_url = 'http://{}/artifactory'.format(server) apk_file='apk_list.json' get_class = _ArtifactoryAccessor() def main(d): print('\n') print('=' * 80) print('Downloading APKs from {}'.format(server)) print('=' * 80) print('\n') for a, repos in enumerate(d['REPO']): repo = repos['name'] for b, groups in enumerate(d['REPO'][a]['GROUP_ID']): group_id = groups['name'] for artifacts in d['REPO'][a]['GROUP_ID'][b]['ARTIFACT_ID']: artifact_id = artifacts['name'] release = artifacts['version'] path = artifacts['path'] file_type = artifacts['type'] artifactory_path = '{}/{}/{}/{}/{}/{}-{}.{}'.format( server_url, repo, group_id, artifact_id, release, artifact_id, release, file_type) artifactory_path_release = '{}/{}/{}/{}/[RELEASE]/{}-[RELEASE].{}'.format( server_url, repo, group_id, artifact_id, artifact_id, file_type) local_path = '/{}/{}.{}'.format(path, artifact_id, file_type) try: local_md5sum = md5sum(local_path) except IOError as ioex: print('Downloading: {}.{}'.format(artifact_id, file_type)) if not download_artifact(artifactory_path, local_path, artifact_id): print("WARNING:", artifactory_path, "Not Found") print("Trying latest available version...") if not download_artifact(artifactory_path_release, local_path, artifact_id): sys.exit("ERROR: Cannot Download Artifact Aborting !!!") else: if not compare_checksum(artifactory_path, local_md5sum, artifact_id): print("WARNING:", artifactory_path, "Not Found") print("Trying latest available version...") if not compare_checksum(artifactory_path_release, local_md5sum, artifact_id): sys.exit('Cannot connect to {}'.format(server)) print('\n') print('=' * 80) print('Downloading DONE') print('=' * 80) print('\n') def download_artifact(url, local_path, artifact_id): path = ArtifactoryPath(url) try: with path.open() as fd: with open(local_path, "wb") as out: out.write(fd.read()) except (RuntimeError, OSError) as err: return False else: local_md5sum = md5sum(local_path) compare_checksum(url, local_md5sum, artifact_id) return True def compare_checksum(url, local_md5sum, artifact_id): try: get_stat = get_class.rest_get(url) remote_md5sum = get_stat[0]['X-Checksum-Md5'] # using OSError here not enough in python2 except Exception as err: return False else: if local_md5sum == remote_md5sum: print('STATUS: {} -OK-'.format(artifact_id)) return True else: print("MD5checksum does not match downloading again") download_artifact(url) if __name__ == "__main__": try: with open(apk_file) as json_data: data = json.load(json_data) main(data) except IOError as ioerr: sys.exit('{} File not found'.format(apk_file))I will add comments and docstings later on :)
I was thinking should i use kwargs for passing parameters
and can i concatenate those beginning and end prints.