diff --git a/packages/base/all/vendor-config-onl/src/bin/onlswi b/packages/base/all/vendor-config-onl/src/bin/onlswi index c57d2c7b..166edbbd 100755 --- a/packages/base/all/vendor-config-onl/src/bin/onlswi +++ b/packages/base/all/vendor-config-onl/src/bin/onlswi @@ -1,5 +1,25 @@ -#!/bin/sh -############################################################ -onlfs mount images --rw -(cd /mnt/onl/images && rm -f *.swi && wget $1) -onlfs mount images +#!/usr/bin/python +import os +import sys +import argparse +import subprocess +from onl.util import wget +from onl.mounts import OnlMountContextReadWrite + +ap = argparse.ArgumentParser('onlswi') +ap.add_argument('url', help="The SWI URL") +ap.add_argument('--onl-data', help='Use /mnt/onl/data for temporary storage.', action='store_true') + +ops = ap.parse_args() +(ex, filename, directory) = wget(ops.url, extension=".swi", temp_directory='/mnt/onl/data' if ops.onl_data else None) + +if ex: + print ex + sys.exit(1) + +with OnlMountContextReadWrite('ONL-IMAGES', None): + subprocess.check_call("rm -f /mnt/onl/images/*.swi", shell=True) + subprocess.check_call("cp %s /mnt/onl/images" % os.path.join(directory, filename), shell=True) + +subprocess.check_call("rm -rf %s" % directory, shell=True) +subprocess.check_call("ls -l /mnt/onl/images", shell=True) diff --git a/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py b/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py index f625f2b5..804afcc2 100644 --- a/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py +++ b/packages/base/all/vendor-config-onl/src/python/onl/util/__init__.py @@ -1,5 +1,8 @@ import subprocess import logging +import urllib2 +import os +import tempfile class OnlServiceMixin(object): @@ -120,3 +123,26 @@ def dmerge(d1, d2): q.append((v1, v2, c3[k],)) return merged + + +def wget(url, directory=None, temp_directory=None, extension=None): + try: + response = urllib2.urlopen(url) + filename = os.path.basename(urllib2.urlparse.urlparse(response.url).path) + except Exception, e: + return (e, None, None) + + if extension and not filename.endswith("%s" % extension): + return (ValueError("The requested filename does not have the correct extension (%s)" % extension), None, None) + + if directory is None: + directory = tempfile.mkdtemp(dir=temp_directory) + + if os.path.exists(os.path.join(directory, filename)): + return (ValueError("The requested filename already exists in the target directory."), None, None) + + try: + subprocess.check_call("wget -P %s %s" % (directory, url), shell=True) + return (None, filename, directory) + except subprocess.CalledProcessError, e: + return (e, None, None)