Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

Commit

Permalink
packageurl handler added
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethWilke committed Mar 2, 2016
1 parent 2bc5dde commit edd8dff
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions libpackilog/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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}

0 comments on commit edd8dff

Please sign in to comment.