def op_flag_encoder(op): import importlib spec = op.opdef.flag_encoder if not spec: return None parts = spec.split(":") if len(parts) != 2: log.warning("invalid flag decoder %r - must be MODULE:FUNCTION", spec) return None mod_name, fun_name = parts try: mod = importlib.import_module(mod_name) except Exception as e: if log.getEffectiveLevel() <= logging.DEBUG: log.exception("importing %s", mod_name) else: log.warning("cannot load flag decoder %r: %s", spec, e) return None fun = getattr(mod, fun_name, None) if fun is None: log.warning( "cannot load flag decoder %r: no such attribute in %s", spec, mod_name ) return None return fun
def _real_hostname(): import socket try: return socket.gethostname() except Exception: if log.getEffectiveLevel() <= logging.DEBUG: log.exception("socket.gethostname()") return ""
def _try_copy_file(src, dest): try: shutil.copyfile(src, dest) except (IOError, OSError) as e: # This is not an error we want to stop an operation for. Log # and continue. if log.getEffectiveLevel() <= logging.DEBUG: log.exception("copy %s to %s", src, dest) else: log.warning("could not copy source code file %s: %s", src, e)
def _interrupt_handler(): """Returns interrupt handler that's independent of module namespace.""" if os.getenv("HANDLE_KEYBOARD_INTERRUPT"): return None # Save everything we need to handle interrupt. log_exception = log.getEffectiveLevel() <= logging.DEBUG log_exception_f = log.exception def handler(): if log_exception: log_exception_f("interrupted") return handler
def _init_warnings(): if log.getEffectiveLevel() > logging.DEBUG: warnings.simplefilter(action="ignore", category=FutureWarning) warnings.filterwarnings("ignore", message="numpy.dtype size changed") warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
def _internal_error(msg): sys.stderr.write("guild.op_main: %s\n" % msg) sys.exit(exit_code.INTERNAL_ERROR) def _error(msg): sys.stderr.write("guild: %s\n" % msg) sys.exit(exit_code.DEFAULT) if __name__ == "__main__": try: main() except Exception as e: if log is None or log.getEffectiveLevel() <= logging.DEBUG: raise import traceback exc_lines = traceback.format_exception(*sys.exc_info()) if len(exc_lines) < 3 or len(__argv0) < 2: # Assertion failure, but we want to be defensive in # deference to the actual error. raise # Print exception start with mod (argv[0]) filtered_exc_lines = [] mod_path = __argv0[1] for line in exc_lines[1:]: if filtered_exc_lines or mod_path in line: filtered_exc_lines.append(line) if not filtered_exc_lines: raise