Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
more pythonic way
#1
Hello all,

I'm trying to improve my python skills and
I made one small script and like to share it with you guys
If someone can give me some remarks how to improve it and
make it more Pythonic I'm eager to learn.


#!/usr/bin/env python2

import sys
import os
import json
sys.path.insert(0, './libs')
from artifactory import ArtifactoryPath, md5sum, _ArtifactoryAccessor
# cannot post clickable links ...
SERVER = '192.168.10.2'
URL = 'localhost:8081/artifactory'
get_class = _ArtifactoryAccessor()

print('\n')
print('=' * 80)
print("Downloading APKs from %s" % SERVER)
print('=' * 80)
print('\n')

def download_artifact(url):
    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 1
    else:
        local_md5sum = md5sum(LOCAL_PATH)
        compare_checksum(url, local_md5sum)

def compare_checksum(url, local_md5sum):
    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:
        sys.exit(("Cannot connect to %s") % SERVER)
    else:
        if local_md5sum == remote_md5sum:
            print(("STATUS: %s -OK-") % ARTIFACT_ID)
        else:
            print("MD5checksum does not match downloading again")
            download_artifact(url)

with open('repos.json') as json_data:
    d = json.load(json_data)

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']
            TYPE = artifacts['type']
            
            ARTIFACTORY_PATH = "%s/%s/%s/%s/%s/%s-%s.%s" % (
                URL, REPO, GROUP_ID, ARTIFACT_ID, RELEASE, ARTIFACT_ID, RELEASE, TYPE)
            ARTIFACTORY_PATH_RELEASE = "%s/%s/%s/%s/[RELEASE]/%s-[RELEASE].%s" % (
                URL, REPO, GROUP_ID, ARTIFACT_ID, ARTIFACT_ID, TYPE)
            LOCAL_PATH = "/%s/%s.%s" % (PATH, ARTIFACT_ID, TYPE)

            try:
                local_md5sum = md5sum(LOCAL_PATH)
            except IOError as ioex:
                print("Downloading: %s.%s" % (ARTIFACT_ID, TYPE))
                if download_artifact(ARTIFACTORY_PATH):
                    print("WARNING:", ARTIFACTORY_PATH, "Not Found")
                    print("Trying latest available version...")
                    if download_artifact(ARTIFACTORY_PATH_RELEASE):
                        sys.exit("ERROR: Cannot Download Artifact Aborting !!!")
            else:
                compare_checksum(ARTIFACTORY_PATH, local_md5sum)
                        

print('\n')
print('=' * 80)
print('Downloading DONE')
print('=' * 80)
print('\n')
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