summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@gmail.com>2014-10-01 16:59:53 +0000
committerSam Thursfield <sam.thursfield@codethink.co.uk>2017-10-02 10:51:38 +0100
commit228b683d377161f62fea40389259ca089da0fb74 (patch)
tree13bc5faea69032f9fe1cc6f1ce344b1ff6f69bed
parentacefe33868585cf31cda53474a3004da42e00896 (diff)
downloadmorph-cache-server-228b683d377161f62fea40389259ca089da0fb74.tar.gz
Allow morph-cache-server to be bound to an ephemeral port
-rwxr-xr-xmorph-cache-server30
-rw-r--r--morphcacheserver/repocache.py6
2 files changed, 31 insertions, 5 deletions
diff --git a/morph-cache-server b/morph-cache-server
index a3c3c97..4af3cee 100755
--- a/morph-cache-server
+++ b/morph-cache-server
@@ -45,6 +45,10 @@ class MorphCacheServer(cliapp.Application):
'port to listen on',
metavar='PORTNUM',
default=defaults['port'])
+ self.settings.string(['port-file'],
+ 'write port number to FILE',
+ metavar='FILE',
+ default='')
self.settings.string(['repo-dir'],
'path to the repository cache directory',
metavar='PATH',
@@ -322,7 +326,8 @@ class MorphCacheServer(cliapp.Application):
% artifact)
return
- filename = os.path.join(self.settings['artifact-dir'], artifact)
+ filename = os.path.join(self.settings['artifact-dir'],
+ artifact)
results[artifact] = os.path.exists(filename)
if results[artifact]:
@@ -338,8 +343,29 @@ class MorphCacheServer(cliapp.Application):
if self.settings['fcgi-server']:
WSGIServer(root).run()
+ elif self.settings['port-file']:
+ import wsgiref.simple_server
+
+ server_port_file = self.settings['port-file']
+ class DebugServer(wsgiref.simple_server.WSGIServer):
+ '''WSGI-like server that uses an ephemeral port.
+
+ Rather than use a specified port, or default, the
+ DebugServer binds to an ephemeral port on 127.0.0.1
+ and writes its number to port-file, so a non-racy
+ temporary port can be used.
+
+ '''
+
+ def __init__(self, (host, port), *args, **kwargs):
+ wsgiref.simple_server.WSGIServer.__init__(
+ self, ('127.0.0.1', 0), *args, **kwargs)
+ with open(server_port_file, 'w') as f:
+ f.write(str(self.server_port) + '\n')
+ run(root, server_class=DebugServer, debug=True)
else:
- run(root, host='0.0.0.0', port=self.settings['port'], reloader=True)
+ run(root, host='0.0.0.0', port=self.settings['port'],
+ reloader=True)
def _unescape_parameter(self, param):
return urllib.unquote(param)
diff --git a/morphcacheserver/repocache.py b/morphcacheserver/repocache.py
index 0e4d909..305c187 100644
--- a/morphcacheserver/repocache.py
+++ b/morphcacheserver/repocache.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2013 Codethink Limited
+# Copyright (C) 2013,2014 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -91,7 +91,7 @@ class RepoCache(object):
raise RepositoryNotFoundError(repo_url)
try:
sha1 = self._rev_parse(repo_dir, ref)
- except:
+ except BaseException:
raise InvalidReferenceError(repo_url, ref)
return self._cat_file(repo_dir, sha1, filename)
@@ -110,7 +110,7 @@ class RepoCache(object):
try:
sha1 = self._rev_parse(repo_dir, ref)
- except:
+ except BaseException:
raise InvalidReferenceError(repo_url, ref)
lines = self._ls_tree(repo_dir, sha1, path).strip()