scan-view 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python
  2. """The clang static analyzer results viewer.
  3. """
  4. import sys
  5. import imp
  6. import os
  7. import posixpath
  8. import thread
  9. import time
  10. import urllib
  11. import webbrowser
  12. # How long to wait for server to start.
  13. kSleepTimeout = .05
  14. kMaxSleeps = int(60 / kSleepTimeout)
  15. # Default server parameters
  16. kDefaultHost = '127.0.0.1'
  17. kDefaultPort = 8181
  18. kMaxPortsToTry = 100
  19. ###
  20. def url_is_up(url):
  21. try:
  22. o = urllib.urlopen(url)
  23. except IOError:
  24. return False
  25. o.close()
  26. return True
  27. def start_browser(port, options):
  28. import urllib
  29. import webbrowser
  30. url = 'http://%s:%d' % (options.host, port)
  31. # Wait for server to start...
  32. if options.debug:
  33. sys.stderr.write('%s: Waiting for server.' % sys.argv[0])
  34. sys.stderr.flush()
  35. for i in range(kMaxSleeps):
  36. if url_is_up(url):
  37. break
  38. if options.debug:
  39. sys.stderr.write('.')
  40. sys.stderr.flush()
  41. time.sleep(kSleepTimeout)
  42. else:
  43. print >> sys.stderr, 'WARNING: Unable to detect that server started.'
  44. if options.debug:
  45. print >> sys.stderr, '%s: Starting webbrowser...' % sys.argv[0]
  46. webbrowser.open(url)
  47. def run(port, options, root):
  48. # Prefer to look relative to the installed binary
  49. share = os.path.dirname(__file__) + "/../share/scan-view"
  50. if not os.path.isdir(share):
  51. # Otherwise look relative to the source
  52. share = os.path.dirname(__file__) + "/../../scan-view/share"
  53. sys.path.append(share)
  54. import ScanView
  55. try:
  56. print 'Starting scan-view at: http://%s:%d' % (options.host,
  57. port)
  58. print ' Use Ctrl-C to exit.'
  59. httpd = ScanView.create_server((options.host, port),
  60. options, root)
  61. httpd.serve_forever()
  62. except KeyboardInterrupt:
  63. pass
  64. def port_is_open(port):
  65. import SocketServer
  66. try:
  67. t = SocketServer.TCPServer((kDefaultHost, port), None)
  68. except:
  69. return False
  70. t.server_close()
  71. return True
  72. def main():
  73. import argparse
  74. parser = argparse.ArgumentParser(description="The clang static analyzer "
  75. "results viewer.")
  76. parser.add_argument("root", metavar="<results directory>", type=str)
  77. parser.add_argument(
  78. '--host', dest="host", default=kDefaultHost, type=str,
  79. help="Host interface to listen on. (default=%s)" % kDefaultHost)
  80. parser.add_argument('--port', dest="port", default=None, type=int,
  81. help="Port to listen on. (default=%s)" % kDefaultPort)
  82. parser.add_argument("--debug", dest="debug", default=0,
  83. action="count",
  84. help="Print additional debugging information.")
  85. parser.add_argument("--auto-reload", dest="autoReload", default=False,
  86. action="store_true",
  87. help="Automatically update module for each request.")
  88. parser.add_argument("--no-browser", dest="startBrowser", default=True,
  89. action="store_false",
  90. help="Don't open a webbrowser on startup.")
  91. parser.add_argument("--allow-all-hosts", dest="onlyServeLocal",
  92. default=True, action="store_false",
  93. help='Allow connections from any host (access '
  94. 'restricted to "127.0.0.1" by default)')
  95. args = parser.parse_args()
  96. # Make sure this directory is in a reasonable state to view.
  97. if not posixpath.exists(posixpath.join(args.root, 'index.html')):
  98. parser.error('Invalid directory, analysis results not found!')
  99. # Find an open port. We aren't particularly worried about race
  100. # conditions here. Note that if the user specified a port we only
  101. # use that one.
  102. if args.port is not None:
  103. port = args.port
  104. else:
  105. for i in range(kMaxPortsToTry):
  106. if port_is_open(kDefaultPort + i):
  107. port = kDefaultPort + i
  108. break
  109. else:
  110. parser.error('Unable to find usable port in [%d,%d)' %
  111. (kDefaultPort, kDefaultPort+kMaxPortsToTry))
  112. # Kick off thread to wait for server and start web browser, if
  113. # requested.
  114. if args.startBrowser:
  115. t = thread.start_new_thread(start_browser, (port, args))
  116. run(port, args, args.root)
  117. if __name__ == '__main__':
  118. main()