From 5ae8a2eff6f179bdb566bb1556626e708cfa1a8e Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Wed, 1 Oct 2014 16:59:53 +0000 Subject: Allow morph-cache-server to be bound to an ephemeral port --- morph-cache-server | 30 ++++++++++++++++++++++++++++-- morphcacheserver/repocache.py | 6 +++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/morph-cache-server b/morph-cache-server index a3c3c978..4af3cee3 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 0e4d909e..305c187c 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() -- cgit v1.2.1