def _getorcreateinfinitepushlogger(op): logger = op.records["infinitepushlogger"] if not logger: ui = op.repo.ui try: username = util.getuser() except Exception: username = "******" # Generate random request id to be able to find all logged entries # for the same request. Since requestid is pseudo-generated it may # not be unique, but we assume that (hostname, username, requestid) # is unique. random.seed() requestid = random.randint(0, 2000000000) hostname = socket.gethostname() logger = functools.partial( ui.log, "infinitepush", user=username, requestid=requestid, hostname=hostname, reponame=ui.config("infinitepush", "reponame"), ) op.records.add("infinitepushlogger", logger) else: logger = logger[0] return logger
def __init__(self, ui): self.ui = ui self.vfs = vfsmod.vfs( ccutil.getuserconfigpath(self.ui, "user_token_path")) self.vfs.createmode = 0o600 # using platform username self.secretname = (self.servicename + "_" + util.getuser()).upper() self.usesecretstool = self.ui.configbool("commitcloud", "use_secrets_tool")
def __init__(self, repo): self.user = util.getuser() self.ui = repo.ui self.localvfs = repo.localvfs self.sharedfeatures = repo.sharedfeatures if repo.shared() and "journal" in self.sharedfeatures: self.sharedvfs = repo.sharedvfs else: self.sharedvfs = None
def infinitepushbackuplogs(ui, repo): """Contents of recent infinitepush log files.""" logdir = ui.config("infinitepushbackup", "logdir") if not logdir: return "infinitepushbackup.logdir not set" try: # the user name from the machine username = util.getuser() except Exception: username = "******" userlogdir = os.path.join(logdir, username) if not os.path.exists(userlogdir): return "log directory does not exist: %s" % userlogdir reponame = os.path.basename(repo.origroot) logfiles = [f for f in os.listdir(userlogdir) if f[:-8] == reponame] if not logfiles: return "no log files found for %s in %s" % (reponame, userlogdir) return _tail(userlogdir, logfiles, 100)
def log(self, event, *msg, **opts): global lastui super(blackboxui, self).log(event, *msg, **opts) if not "*" in self.track and not event in self.track: return if not msg or not msg[0]: return if self._bbvfs: ui = self else: # certain ui instances exist outside the context of # a repo, so just default to the last blackbox that # was seen. ui = lastui() if not ui: return vfs = ui._bbvfs if not vfs: return repo = getattr(ui, "_bbrepo", lambda: None)() if not lastui() or repo: lastui = weakref.ref(ui) if getattr(ui, "_bbinlog", False): # recursion and failure guard return ui._bbinlog = True default = self.configdate("devel", "default-date") date = util.datestr(default, "%Y/%m/%d %H:%M:%S") user = util.getuser() pid = "%d" % util.getpid() if len(msg) == 1: # Don't even try to format the string if there is only one # argument. formattedmsg = msg[0] else: try: formattedmsg = msg[0] % msg[1:] except TypeError: # If fails with `TypeError: not enough arguments for format # string`, concatenate the arguments gracefully. formattedmsg = " ".join(msg) rev = "(unknown)" changed = "" # Only log the current commit if the changelog has already been # loaded. if repo and "changelog" in repo.__dict__: try: ctx = repo[None] parents = ctx.parents() rev = "+".join([hex(p.node()) for p in parents]) except Exception: # This can happen if the dirstate file is sufficiently # corrupt that we can't extract the parents. In that case, # just don't set the rev. pass if ui.configbool("blackbox", "dirty") and ctx.dirty( missing=True, merge=False, branch=False): changed = "+" if ui.configbool("blackbox", "logsource"): src = " [%s]" % event else: src = "" requestid = ui.environ.get("HGREQUESTID") or "" if requestid: src += "[%s]" % requestid try: fmt = "%s %s @%s%s (%s)%s> %s" args = (date, user, rev, changed, pid, src, formattedmsg) with _openlogfile(ui, vfs) as fp: line = fmt % args if not line.endswith("\n"): line += "\n" fp.write(encodeutf8(line)) except (IOError, OSError) as err: self.debug("warning: cannot write to blackbox.log: %s\n" % err.strerror) # do not restore _bbinlog intentionally to avoid failed # logging again else: ui._bbinlog = False
def backgroundbackup(repo, command=None, dest=None): """start background backup""" ui = repo.ui if command is not None: background_cmd = command elif workspace.currentworkspace(repo): background_cmd = ["hg", "cloud", "sync"] else: background_cmd = ["hg", "cloud", "backup"] infinitepush_bgssh = ui.config("infinitepush", "bgssh") if infinitepush_bgssh: background_cmd += ["--config", "ui.ssh=%s" % infinitepush_bgssh] # developer config: infinitepushbackup.bgdebuglocks if ui.configbool("infinitepushbackup", "bgdebuglocks"): background_cmd += ["--config", "devel.debug-lockers=true"] # developer config: infinitepushbackup.bgdebug if ui.configbool("infinitepushbackup", "bgdebug", False): background_cmd.append("--debug") if dest: background_cmd += ["--dest", dest] logfile = None logdir = ui.config("infinitepushbackup", "logdir") if logdir: # make newly created files and dirs non-writable oldumask = os.umask(0o022) try: try: # the user name from the machine username = util.getuser() except Exception: username = "******" if not _checkcommonlogdir(logdir): raise WrongPermissionsException(logdir) userlogdir = os.path.join(logdir, username) util.makedirs(userlogdir) if not _checkuserlogdir(userlogdir): raise WrongPermissionsException(userlogdir) reponame = os.path.basename(repo.sharedroot) _removeoldlogfiles(userlogdir, reponame) logfile = getlogfilename(logdir, username, reponame) except (OSError, IOError) as e: ui.debug("background backup log is disabled: %s\n" % e) except WrongPermissionsException as e: ui.debug( ( "%s directory has incorrect permission, " + "background backup logging will be disabled\n" ) % e.logdir ) finally: os.umask(oldumask) if not logfile: logfile = os.devnull with open(logfile, "a") as f: timestamp = util.datestr(util.makedate(), "%Y-%m-%d %H:%M:%S %z") fullcmd = " ".join(util.shellquote(arg) for arg in background_cmd) f.write("\n%s starting: %s\n" % (timestamp, fullcmd)) Stdio = bindings.process.Stdio out = Stdio.open(logfile, append=True, create=True) bindings.process.Command.new(background_cmd[0]).args( background_cmd[1:] ).avoidinherithandles().newsession().stdin(Stdio.null()).stdout(out).stderr( out ).spawn()