def print_notice_and_exit(self): """A context manager used to print the notice and terminate execution. This decorator executes the function and prints the monitoring notice if necessary. If an exception is raised, we will catch it, and print it before printing the metrics collection notice. This will call sys.exit() with an appropriate exit code to ensure the notice is the last thing printed.""" # Needed to preserve the __name__ and __doc__ attributes of func. try: yield exception = None # pylint: disable=bare-except except: exception = sys.exc_info() # Print the exception before the metrics notice, so that the notice is # clearly visible even if gclient fails. if exception: if isinstance(exception[1], KeyboardInterrupt): sys.stderr.write('Interrupted\n') elif not isinstance(exception[1], SystemExit): traceback.print_exception(*exception) # Print the notice if (not DISABLE_METRICS_COLLECTION and self.config.is_googler and self.config.opted_in is None): metrics_utils.print_notice(self.config.countdown) self.config.decrease_countdown() sys.exit(metrics_utils.return_code_from_exception(exception))
def _collect_metrics(self, func, command_name, *args, **kwargs): self.add('command', command_name) try: start = time.time() func(*args, **kwargs) exception = None # pylint: disable=bare-except except: exception = sys.exc_info() finally: self.add('execution_time', time.time() - start) # Print the exception before the metrics notice, so that the notice is # clearly visible even if gclient fails. if exception and not isinstance(exception[1], SystemExit): traceback.print_exception(*exception) exit_code = metrics_utils.return_code_from_exception(exception) self.add('exit_code', exit_code) # Print the metrics notice only if the user has not explicitly opted in # or out. if self.config.opted_in is None: metrics_utils.print_notice(self.config.countdown) # Add metrics regarding environment information. self.add('timestamp', metrics_utils.seconds_to_weeks(time.time())) self.add('python_version', metrics_utils.get_python_version()) self.add('host_os', gclient_utils.GetMacWinOrLinux()) self.add('host_arch', detect_host_arch.HostArch()) self.add('depot_tools_age', metrics_utils.get_repo_timestamp(DEPOT_TOOLS)) self._upload_metrics_data() sys.exit(exit_code)
def _decorator(func): # Do this first so we don't have to read, and possibly create a config # file. if DISABLE_METRICS_COLLECTION: return func # If the user has opted out or the user is not a googler, then there is no # need to do anything. if self.config.opted_in == False or not self.config.is_googler: return func # If the user hasn't opted in or out, and the countdown is not yet 0, just # display the notice. if self.config.opted_in == None and self.config.countdown > 0: metrics_utils.print_notice(self.config.countdown) self.config.decrease_countdown() return func # Otherwise, collect the metrics. # Needed to preserve the __name__ and __doc__ attributes of func. @functools.wraps(func) def _inner(*args, **kwargs): self._collect_metrics(func, command_name, *args, **kwargs) return _inner