Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
more pythonic way
#9
Great, thank you all !!!
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.
Reply


Messages In This Thread
more pythonic way - by fstefanov - Apr-24-2017, 06:57 AM
RE: more pythonic way - by ichabod801 - Apr-24-2017, 10:48 AM
RE: more pythonic way - by volcano63 - Apr-24-2017, 11:57 AM
RE: more pythonic way - by Larz60+ - Apr-24-2017, 12:02 PM
RE: more pythonic way - by buran - Apr-24-2017, 12:02 PM
RE: more pythonic way - by Larz60+ - Apr-24-2017, 12:47 PM
RE: more pythonic way - by ichabod801 - Apr-24-2017, 12:51 PM
RE: more pythonic way - by buran - Apr-24-2017, 12:52 PM
RE: more pythonic way - by fstefanov - Apr-24-2017, 05:15 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020