From edd8dff0f31946e47b09aadc44f63710b09ad1fb Mon Sep 17 00:00:00 2001 From: KennethWilke Date: Wed, 2 Mar 2016 12:29:32 -0600 Subject: [PATCH] packageurl handler added --- libpackilog/__init__.py | 69 ++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/libpackilog/__init__.py b/libpackilog/__init__.py index 5184e01..af6f46b 100644 --- a/libpackilog/__init__.py +++ b/libpackilog/__init__.py @@ -1,5 +1,21 @@ import requests import os +import json + + +def pull_in_dependencies(manifest, force=False): + ''' Handles pulling in dependencies ''' + # Return immediately if there are no dependencies to handle + if 'dependencies' not in manifest: + return + + for dependency in manifest['dependencies']: + if dependency['type'] not in dependency_handlers: + error = "ERROR: There is no handler for dependency type '{0}'." + print error.format(dependency['type']) + return + else: + dependency_handlers[dependency['type']](dependency, force) def file_dependency(dependency, force=False): @@ -28,25 +44,54 @@ def file_dependency(dependency, force=False): else: error = "File {0} already exists, use -f flag to force overwrite" print error.format(filename) + return else: with open(filename, 'w') as fp: fp.write(req.text) print '{0} downloaded from {1}'.format(filename, source) -dependency_handlers = {'file': file_dependency} +def packageurl_dependency(dependency, force=False): + ''' Pulls down a package via URL ''' + if 'source' not in dependency: + print 'source for a packageurl-based dependency was not defined' + return + source = dependency['source'] -def pull_in_dependencies(manifest, force=False): - ''' Handles pulling in dependencies ''' - # Return immediately if there are no dependencies to handle - if 'dependencies' not in manifest: + # Get package manifest + source_manifest = source + '/packilog.json' + req = requests.get(source_manifest) + if req.status_code != 200: + error = "Failed to download dependency manifest from {0}: {1} {2}" + print error.format(source_manifest, req.status_code, req.reason) return - for dependency in manifest['dependencies']: - if dependency['type'] not in dependency_handlers: - error = "ERROR: There is no handler for dependency type '{0}'." - print error.format(dependency['type']) - return - else: - dependency_handlers[dependency['type']](dependency, force) + package_manifest = json.loads(req.text) + # Recursively handle dependencies + pull_in_dependencies(package_manifest) + + # Pull in files referenced + if 'files' in package_manifest: + for filename in package_manifest['files']: + sourcepath = '{0}/{1}'.format(source, filename) + req = requests.get(sourcepath) + if req.status_code != 200: + error = "Failed to download dependency source from {0}: {1} {2}" + print error.format(sourcepath, req.status_code, req.reason) + return + if os.path.exists(filename) and not force: + if req.text == open(filename).read(): + print '{0} is up to date'.format(filename) + else: + error = "File {0} already exists, use -f flag to force overwrite" + print error.format(filename) + return + else: + with open(filename, 'w') as fp: + fp.write(req.text) + print '{0} downloaded from {1}'.format(filename, source) + + +dependency_handlers = {'file': file_dependency, + 'packageurl': packageurl_dependency}