summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-09-17 13:49:21 +0100
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-09-17 13:49:21 +0100
commit2ee9e745d46fd01f95cf598025ae10b88d1c051f (patch)
treead58acb636326510658c131cb26068b4d9de09ab
parent6c73c9b3642795b4f601ac3aee325a673e9b139e (diff)
downloadmorph-2ee9e745d46fd01f95cf598025ae10b88d1c051f.tar.gz
Update /fetch API to latest definition
-rwxr-xr-xmorph-cache-server71
1 files changed, 51 insertions, 20 deletions
diff --git a/morph-cache-server b/morph-cache-server
index b4f8fa1a..04a5710c 100755
--- a/morph-cache-server
+++ b/morph-cache-server
@@ -20,6 +20,7 @@ import cliapp
import logging
import os
import urllib
+import urllib2
import shutil
from bottle import Bottle, request, response, run, static_file
@@ -59,6 +60,52 @@ class MorphCacheServer(cliapp.Application):
self.settings.boolean(['enable-writes'],
'enable the write methods (fetch and delete)')
+ def _fetch_artifact(self, url, filename):
+ in_fh = None
+ try:
+ in_fh = urllib2.urlopen(url)
+ with open(filename, "w") as localtmp:
+ shutil.copyfileobj(in_fh, localtmp)
+ in_fh.close()
+ except Exception, e:
+ if in_fh is not None:
+ in_fh.close()
+ raise
+ else:
+ if in_fh is not None:
+ in_fh.close()
+ return os.stat(filename)
+
+ def _fetch_artifacts(self, server, cacheid, artifacts):
+ ret = {}
+ try:
+ for artifact in artifacts:
+ artifact_name = "%s.%s" % (cacheid, artifact)
+ tmpname = os.path.join(self.settings['artifact-dir'],
+ ".dl.%s" % artifact_name)
+ url = "http://%s/1.0/artifacts?filename=%s" % (
+ server, urllib.quote(artifact_name))
+ stinfo = self._fetch_artifact(url, tmpname)
+ ret[artifact_name] = {
+ "size": stinfo.st_size,
+ "used": stinfo.st_blocks * 512,
+ }
+ except Exception, e:
+ for artifact in ret.iterkeys():
+ os.unlink(os.path.join(self.settings['artifact-dir'],
+ ".dl.%s" % artifact))
+ raise
+
+ for artifact in ret.iterkeys():
+ tmpname = os.path.join(self.settings['artifact-dir'],
+ ".dl.%s" % artifact)
+ artifilename = os.path.join(self.settings['artifact-dir'],
+ artifact)
+ os.rename(tmpname, artifilename)
+
+ return ret
+
+
def process_args(self, args):
app = Bottle()
@@ -110,28 +157,12 @@ class MorphCacheServer(cliapp.Application):
@writable('/fetch')
def fetch():
host = self._unescape_parameter(request.query.host)
- artifact = self._unescape_parameter(request.query.artifact)
+ cacheid = self._unescape_parameter(request.query.cacheid)
+ artifacts = self._unescape_parameter(request.query.artifacts)
try:
response.set_header('Cache-Control', 'no-cache')
- in_fh = urllib.urlopen("http://%s/artifacts?basename=%s" %
- (host, urllib.quote(artifact)))
- tmpname = "%s/.dl.%s" % (
- self.settings['artifact-dir'],
- artifact)
- localtmp = open(tmpname, "w")
- shutil.copyfileobj(in_fh, localtmp)
- localtmp.close()
- in_fh.close()
- artifilename = "%s/%s" % (self.settings['artifact-dir'],
- artifact)
- os.rename(tmpname, artifilename)
- stinfo = os.stat(artifilename)
- ret = {}
- ret[artifact] = {
- "size": stinfo.st_size,
- "used": stinfo.st_blocks * 512
- }
- return ret
+ artifacts = artifacts.split(",")
+ return self._fetch_artifacts(host, cacheid, artifacts)
except Exception, e:
response.status = 500