Make the onlswi script marginally less stupid.

This commit is contained in:
Jeffrey Townsend
2018-12-04 14:27:30 +00:00
parent 3c7c37f706
commit 4404a41f68
2 changed files with 51 additions and 5 deletions

View File

@@ -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)

View File

@@ -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)