diff options
author | clutton <clutton@zoho.com> | 2016-04-14 22:33:21 +0300 |
---|---|---|
committer | clutton <clutton@zoho.com> | 2016-04-14 22:33:21 +0300 |
commit | 7dfb29db81c7a07bfaefd148258c6a897e8bac1b (patch) | |
tree | fe4ea88adc063545bd34b0dbd124fd1bc769b44c /include_server/include_server.py | |
parent | 50e5b666d85c92a963ed6c1d4150683e87b8b9a5 (diff) | |
download | distcc-git-python3.tar.gz |
include_server python(3) readypython3
Diffstat (limited to 'include_server/include_server.py')
-rwxr-xr-x | include_server/include_server.py | 123 |
1 files changed, 59 insertions, 64 deletions
diff --git a/include_server/include_server.py b/include_server/include_server.py index 039a9ef..3b50247 100755 --- a/include_server/include_server.py +++ b/include_server/include_server.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2.4 +#!/usr/bin/env python3 # Copyright 2007 Google Inc. # @@ -32,7 +32,7 @@ import os import re import shutil import signal -import SocketServer +import socketserver import sys import tempfile import traceback @@ -44,7 +44,7 @@ import include_analyzer_memoizing_node import statistics # The default size passed to listen by a streaming socket server of -# SocketServer is only 5. Make it 128 (which appears to be the hard +# socketserver is only 5. Make it 128 (which appears to be the hard # built-in limit for Linux). This enables requests to the include # server to be buffered better. REQUEST_QUEUE_SIZE = 128 @@ -61,7 +61,7 @@ NotCoveredTimeOutError = basics.NotCoveredTimeOutError # USAGE def Usage(): - print """Usage: + print("""Usage: include_server --port INCLUDE_SERVER_PORT [OPTIONS] @@ -77,15 +77,15 @@ OPTIONS: 1 = warnings 2 = trace some functions other powers of two: see basics.py. - + -e, --email Send email to discc-pump developers when include server gets in trouble. --email_bound NUMBER Maximal number of emails to send (in addition to a final email). Default: 3. - + --no-email Do not send email. - + --path_observation_re=RE Issue warning message whenever a filename is resolved to a realpath that is matched by RE, which is a regular expression in Python syntax. @@ -95,9 +95,9 @@ OPTIONS: --pid_file FILEPATH The pid of the include server is written to file FILEPATH. - + -s, --statistics Print information to stdout about include analysis. - + --stat_reset_triggers=LIST Flush stat caches when the timestamp of any filepath in LIST changes or the filepath comes in or out of existence. LIST is a colon separated @@ -110,7 +110,7 @@ OPTIONS: -t, --time Print elapsed, user, and system time to stderr. - --unsafe_absolute_includes Do preprocessing on the compilation server even if + --unsafe_absolute_includes Do preprocessing on the compilation server even if includes of absolute filepaths are encountered. Such includes are then ignored for the purposes of gathering the include closure. See the @@ -122,17 +122,17 @@ OPTIONS: some cases, but will break builds which use include structures like "<foo/../file.h>" without including other files in foo/. - + -v, --verify Verify that files in CPP closure are contained in closure calculated by include processor. - + -w, --write_include_closure Write a .d_approx file which lists all the included files calculated by the include server; with -x, additionally write the included files as calculated by CPP to a .d_exact file. -x, --exact_analysis Use CPP instead, do not omit system headers files. -""" +""") # TODO(klarlund) # --simple_algorithm not currently implemented @@ -142,10 +142,10 @@ OPTIONS: def _PrintStackTrace(fd): """Print stacktrace to file object.""" - print >> fd, '------- Include server stack trace -----------' + print('------- Include server stack trace -----------', file=fd) # Limit is 1000 entries. traceback.print_exc(1000, fd) - print >> fd, '----------------------------------------------' + print('----------------------------------------------', file=fd) class _EmailSender(object): @@ -176,7 +176,7 @@ class _EmailSender(object): basics.EMAIL_SUBJECT, basics.DCC_EMAILLOG_WHOM_TO_BLAME, user_addr, - "Automated email number %d in include server session.\n" % + "Automated email number %d in include server session.\n" % self.number_sent, fd.read()) s = smtplib.SMTP() @@ -195,7 +195,7 @@ class _EmailSender(object): Arguments: fd: a file object that will be closed. force: send the mail even if number of emails sent exceed - basics.opt_email_bound + basics.opt_email_bound """ fd.seek(0, 0) Debug(DEBUG_WARNING, "%s", fd.read()) @@ -257,7 +257,7 @@ def ExactDependencies(cmd, realpath_map, systemdir_prefix_cache, fd_d_.read()))) fd_d_.close() _delete_temp_files() - # The sets of dependencies is a set the of realpath indices of the + # The sets of dependencies is a set the of realpath indices of the # absolute filenames corresponding to files in the dotd file. deps = set([ rp_idx for filepath in dotd.split() @@ -282,7 +282,7 @@ def WriteDependencies(deps, result_file, realpath_map): fd.write("\n".join([realpath_map.string[d] for d in deps])) fd.write("\n") fd.close() - except (IOError, OSError), why: + except (IOError, OSError) as why: raise NotCoveredError("Could not write to '%s': %s" % (result_file, why)) @@ -319,12 +319,12 @@ def VerifyExactDependencies(include_closure, # A SOCKET SERVER -class QueuingSocketServer(SocketServer.UnixStreamServer): +class Queuingsocketserver(socketserver.UnixStreamServer): """A socket server whose request queue have size REQUEST_QUEUE_SIZE.""" request_queue_size = REQUEST_QUEUE_SIZE def handle_error(self, _, client_address): - """Re-raise current exception; overrides SocketServer.handle_error. + """Re-raise current exception; overrides socketserver.handle_error. """ raise @@ -337,7 +337,7 @@ def DistccIncludeHandlerGenerator(include_analyzer): # TODO(klarlund): Can we do this without dynamic type generation? - class IncludeHandler(SocketServer.StreamRequestHandler): + class IncludeHandler(socketserver.StreamRequestHandler): """Define a handle() method that invokes the include closure algorithm .""" def handle(self): @@ -389,15 +389,14 @@ def DistccIncludeHandlerGenerator(include_analyzer): # all circumstances. include_analyzer.timer.Cancel() - except NotCoveredError, inst: + except NotCoveredError as inst: # Warn user. The 'Preprocessing locally' message is meant to # assure the user that the build process is otherwise intact. - fd = os.tmpfile() - print >> fd, ( - "Preprocessing locally. Include server not covering: %s for " + fd = tempfile.TemporaryFile(mode='w+') + print(("Preprocessing locally. Include server not covering: %s for " + "translation unit '%s'") % ( (inst.args and inst.args[0] or "unknown reason", - include_analyzer.translation_unit)), + include_analyzer.translation_unit)), file=fd, end=' ') # We don't include a stack trace here. include_analyzer.email_sender.MaybeSendEmail(fd, never=not inst.send_email) @@ -409,7 +408,7 @@ def DistccIncludeHandlerGenerator(include_analyzer): Debug(DEBUG_TRACE, "Clearing caches because of include server timeout.") include_analyzer.ClearStatCaches() - + except SignalSIGTERM: # Normally, we will get this exception when the include server is no # longer needed. But we also handle it here, during the servicing of a @@ -419,32 +418,30 @@ def DistccIncludeHandlerGenerator(include_analyzer): except KeyboardInterrupt: # Propagate to the last-chance exception handler in Main. raise - except SystemExit, inst: + except SystemExit as inst: # When handler tries to exit (by invoking sys.exit, which in turn raises # SystemExit), something is really wrong. Terminate the include # server. But, print out an informative message first. - fd = os.tmpfile() - print >> fd, ( - ("Preprocessing locally. Include server fatal error: '%s' for " + fd = tempfile.TemporaryFile(mode='w+') + print(("Preprocessing locally. Include server fatal error: '%s' for " + "translation unit '%s'") % ( - (inst.args, include_analyzer.translation_unit))), + (inst.args, include_analyzer.translation_unit)), file=fd, end=' ') _PrintStackTrace(fd) include_analyzer.email_sender.MaybeSendEmail(fd, force=True) distcc_pump_c_extensions.XArgv(self.wfile.fileno(), []) sys.exit("Now terminating include server.") # All other exceptions are trapped here. - except Exception, inst: + except Exception as inst: # Internal error. Better be safe than sorry: terminate include # server. But show error to user on stderr. We hope this message will be # reported. - fd = os.tmpfile() - print >> fd, ( - ("Preprocessing locally. Include server internal error: '%s: %s' " - + "for translation unit '%s'") % ( - (inst.__class__, inst.args, include_analyzer.translation_unit))), + fd = tempfile.TemporaryFile(mode='w+') + print(("Preprocessing locally. Include server internal error: '%s: %s'" + + " for translation unit '%s'") % ( + (inst.__class__, inst.args, include_analyzer.translation_unit)), file=fd) _PrintStackTrace(fd) - # Force this email through (if basics.opt_send_email is True), because - # this is the last one and this is an important case to report. + # # Force this email through (if basics.opt_send_email is True), because + # # this is the last one and this is an important case to report. include_analyzer.email_sender.MaybeSendEmail(fd, force=True) distcc_pump_c_extensions.XArgv(self.wfile.fileno(), []) raise SignalSIGTERM # to be caught in Main with no further stack trace @@ -468,7 +465,7 @@ def _ParseCommandLineOptions(): Returns: (include_server_port, pid_file), where include_server_port - is a string and pid_file is a string or None + is a string and pid_file is a string or None Modifies: option variables in module basics """ @@ -500,8 +497,8 @@ def _ParseCommandLineOptions(): for opt, arg in opts: try: if opt in ("-d", "--debug_pattern"): - basics.opt_debug_pattern = int(arg) - if opt in ("--port",): + basics.opt_debug_pattern = int(arg) + if opt in ("--port", ): include_server_port = arg if opt in ("--pid_file",): pid_file = arg @@ -541,9 +538,8 @@ def _ParseCommandLineOptions(): sys.exit(1) # We must have a port! if not include_server_port: - print >> sys.stderr, "INCLUDE_SERVER_PORT not provided. Aborting." - print >> sys.stderr, "-------------------------------------------" - print >> sys.stderr + print("INCLUDE_SERVER_PORT not provided. Aborting.", file=sys.stderr) + print("-------------------------------------------", "\n", file=sys.stderr) Usage() sys.exit(1) return (include_server_port, pid_file) @@ -561,10 +557,9 @@ def _PrintTimes(times_at_start, times_at_fork, times_child): + times_child[1] + times_child[1]) total_cpu = total_u + total_s total_e = times_child[4] - times_at_start[4] - print >> sys.stderr, "Include server timing. ", - print >> sys.stderr, ( - "Elapsed: %3.1fs User: %3.1fs System: %3.1fs User + System: %3.1fs" % - (total_e, total_u, total_s, total_cpu)) + print("Include server timing. ", sys.stderr) + print("Elapsed: %3.1fs User: %3.1fs System: %3.1fs User + System: %3.1fs" % + (total_e, total_u, total_s, total_cpu), file=sys.stderr) class _IncludeServerPortReady(object): @@ -581,12 +576,12 @@ class _IncludeServerPortReady(object): def Acquire(self): """Acquire the semaphore after fork; blocks until a call of Release.""" - if os.read(self.read_fd, 1) != '\n': + if os.read(self.read_fd, 1) != b'\n': sys.exit("Include server: _IncludeServerPortReady.Acquire failed.") def Release(self): """Release the semaphore after fork.""" - if os.write(self.write_fd, '\n') != 1: + if os.write(self.write_fd, b'\n') != 1: sys.exit("Include server: _IncludeServerPortReady.Release failed.") @@ -594,7 +589,7 @@ def _SetUp(include_server_port): """Setup include_analyzer and socket server. Returns: (include_analyzer, server)""" - + try: os.unlink(include_server_port) except (IOError, OSError): @@ -615,9 +610,9 @@ def _SetUp(include_server_port): client_root_keeper, basics.opt_stat_reset_triggers)) include_analyzer.email_sender = _EmailSender() - + # Wrap it inside a handler that is a part of a UnixStreamServer. - server = QueuingSocketServer( + server = Queuingsocketserver( include_server_port, # Now, produce a StreamRequestHandler subclass whose new objects has # a handler which calls the include_analyzer just made. @@ -647,12 +642,12 @@ def Main(): # the process id of child to the pid file. times_at_fork = os.times() pid = os.fork() - if pid != 0: + if pid != 0: # In parent. # if pid_file: pid_file_fd = open(pid_file, "w") - print >> pid_file_fd, pid + print(pid, file=pid_file_fd) pid_file_fd.close() # Just run to completion now -- after making sure that child is ready. include_server_port_ready.Acquire() @@ -673,15 +668,15 @@ def Main(): # gc.set_debug(gc.DEBUG_STATS + gc.DEBUG_COLLECTABLE) server.serve_forever() except KeyboardInterrupt: - print >> sys.stderr, ( - "Include server: keyboard interrupt, quitting after cleaning up.") + print("Include server: keyboard interrupt, quitting after cleaning up.", + file=sys.stderr) _CleanOut(include_analyzer, include_server_port) except SignalSIGTERM: Debug(DEBUG_TRACE, "Include server shutting down.") _CleanOut(include_analyzer, include_server_port) except: - print >> sys.stderr, ( - "Include server: exception occurred, quitting after cleaning up.") + print("Include server: exception occurred, quitting after cleaning up.", + file=sys.stderr) _PrintStackTrace(sys.stderr) _CleanOut(include_analyzer, include_server_port) raise # reraise exception @@ -689,8 +684,8 @@ def Main(): if basics.opt_print_times: _PrintTimes(times_at_start, times_at_fork, os.times()) - + if __name__ == "__main__": - # Treat SIGTERM (the default of kill) as Ctrl-C. + # Treat SIGTERM (the default of kill) as Ctrl-C. signal.signal(signal.SIGTERM, basics.RaiseSignalSIGTERM) Main() |