diff options
author | Daniel Dunbar <daniel@zuster.org> | 2008-09-22 02:53:12 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2008-09-22 02:53:12 +0000 |
commit | 7ad535db066b6f5e31776f8a74b8f1e98b14cc7e (patch) | |
tree | 760119468c9ffef17a34005013259620487fc6b0 /tools/scan-view | |
parent | 8dcd940c0f00530e0e8ca55be1193382e80227ca (diff) |
scan-view: Search for available port if default is unavailable.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@56426 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/scan-view')
-rw-r--r-- | tools/scan-view/ScanView.py | 7 | ||||
-rwxr-xr-x | tools/scan-view/scan-view | 50 |
2 files changed, 40 insertions, 17 deletions
diff --git a/tools/scan-view/ScanView.py b/tools/scan-view/ScanView.py index f1e488dceb..3c605211ff 100644 --- a/tools/scan-view/ScanView.py +++ b/tools/scan-view/ScanView.py @@ -81,7 +81,7 @@ class ScanViewServer(BaseHTTPServer.HTTPServer): # Add defaults self.config.add_section('ScanView') - for r in Reporter.getReporters(): + for r in self.reporters: self.config.add_section(r.getName()) for p in r.getParameterNames(): self.config.set(r.getName(), p, '') @@ -529,13 +529,12 @@ function updateReporterOptions() { return self.send_string(data, ctype, mtime=fs.st_mtime) -def create_server(options, root): +def create_server(address, options, root): import Reporter reporters = Reporter.getReporters() - return ScanViewServer((options.host, options.port), - ScanViewRequestHandler, + return ScanViewServer(address, ScanViewRequestHandler, root, reporters, options) diff --git a/tools/scan-view/scan-view b/tools/scan-view/scan-view index e005a26b40..844a173ba6 100755 --- a/tools/scan-view/scan-view +++ b/tools/scan-view/scan-view @@ -16,8 +16,9 @@ kMaxSleeps = 100 # Default server parameters -kDefaultHost = 'localhost' +kDefaultHost = '127.0.0.1' kDefaultPort = 8181 +kMaxPortsToTry = 100 ### @@ -29,10 +30,10 @@ def url_is_up(url): o.close() return True -def start_browser(options): +def start_browser(port, options): import urllib, webbrowser - url = 'http://%s:%d'%(options.host, options.port) + url = 'http://%s:%d'%(options.host, port) # Wait for server to start... if options.debug: @@ -52,19 +53,28 @@ def start_browser(options): print >>sys.stderr,'%s: Starting webbrowser...' % sys.argv[0] webbrowser.open(url) -def run(options, root): +def run(port, options, root): import ScanView try: - if options.debug: - print >>sys.stderr,'%s: SERVER: starting %s:%d'%(sys.argv[0], - options.host, - options.port) - httpd = ScanView.create_server(options, root) + print 'Starting scan-view at: http://%s:%d'%(options.host, + port) + print ' Use Ctrl-C to exit.' + httpd = ScanView.create_server((options.host, port), + options, root) httpd.serve_forever() except KeyboardInterrupt: pass -def main(): +def port_is_open(port): + import SocketServer + try: + t = SocketServer.TCPServer((kDefaultHost,port),None) + except: + return False + t.server_close() + return True + +def main(): from optparse import OptionParser parser = OptionParser('usage: %prog [options] <results directory>') parser.set_description(__doc__) @@ -72,7 +82,7 @@ def main(): '--host', dest="host", default=kDefaultHost, type="string", help="Host interface to listen on. (default=%s)" % kDefaultHost) parser.add_option( - '--port', dest="port", default=kDefaultPort, type="int", + '--port', dest="port", default=None, type="int", help="Port to listen on. (default=%s)" % kDefaultPort) parser.add_option("--debug", dest="debug", default=0, action="count", @@ -93,12 +103,26 @@ def main(): if not posixpath.exists(posixpath.join(root,'index.html')): parser.error('Invalid directory, analysis results not found!') + # Find an open port. We aren't particularly worried about race + # conditions here. Note that if the user specified a port we only + # use that one. + if options.port is not None: + port = options.port + else: + for i in range(kMaxPortsToTry): + if port_is_open(kDefaultPort + i): + port = kDefaultPort + i + break + else: + parser.error('Unable to find usable port in [%d,%d)'%(kDefaultPort, + kDefaultPort+kMaxPortsToTry)) + # Kick off thread to wait for server and start web browser, if # requested. if options.startBrowser: - t = thread.start_new_thread(start_browser, (options,)) + t = thread.start_new_thread(start_browser, (port,options)) - run(options, root) + run(port, options, root) if __name__ == '__main__': main() |