project: Use repo logger

Bug: b/292704435
Change-Id: I510fc911530db2c84a7ee099fa2905ceac35d0b7
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/386295
Reviewed-by: Jason Chang <jasonnc@google.com>
Tested-by: Aravind Vasudevan <aravindvasudev@google.com>
Commit-Queue: Aravind Vasudevan <aravindvasudev@google.com>
This commit is contained in:
Aravind Vasudevan 2023-09-14 08:17:20 +00:00 committed by LUCI
parent c993c5068e
commit 7a1f1f70f0

View File

@ -56,9 +56,13 @@ import git_superproject
from git_trace2_event_log import EventLog from git_trace2_event_log import EventLog
import platform_utils import platform_utils
import progress import progress
from repo_logging import RepoLogger
from repo_trace import Trace from repo_trace import Trace
logger = RepoLogger(__file__)
class SyncNetworkHalfResult(NamedTuple): class SyncNetworkHalfResult(NamedTuple):
"""Sync_NetworkHalf return value.""" """Sync_NetworkHalf return value."""
@ -115,16 +119,6 @@ def _lwrite(path, content):
raise raise
def _error(fmt, *args):
msg = fmt % args
print("error: %s" % msg, file=sys.stderr)
def _warn(fmt, *args):
msg = fmt % args
print("warn: %s" % msg, file=sys.stderr)
def not_rev(r): def not_rev(r):
return "^" + r return "^" + r
@ -436,7 +430,7 @@ class _CopyFile(object):
mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
os.chmod(dest, mode) os.chmod(dest, mode)
except IOError: except IOError:
_error("Cannot copy file %s to %s", src, dest) logger.error("error: Cannot copy file %s to %s", src, dest)
class _LinkFile(object): class _LinkFile(object):
@ -471,7 +465,9 @@ class _LinkFile(object):
os.makedirs(dest_dir) os.makedirs(dest_dir)
platform_utils.symlink(relSrc, absDest) platform_utils.symlink(relSrc, absDest)
except IOError: except IOError:
_error("Cannot link file %s to %s", relSrc, absDest) logger.error(
"error: Cannot link file %s to %s", relSrc, absDest
)
def _Link(self): def _Link(self):
"""Link the self.src & self.dest paths. """Link the self.src & self.dest paths.
@ -499,7 +495,7 @@ class _LinkFile(object):
dest = _SafeExpandPath(self.topdir, self.dest) dest = _SafeExpandPath(self.topdir, self.dest)
# Entity contains a wild card. # Entity contains a wild card.
if os.path.exists(dest) and not platform_utils.isdir(dest): if os.path.exists(dest) and not platform_utils.isdir(dest):
_error( logger.error(
"Link error: src with wildcard, %s must be a directory", "Link error: src with wildcard, %s must be a directory",
dest, dest,
) )
@ -1201,7 +1197,7 @@ class Project(object):
tar.extractall(path=path) tar.extractall(path=path)
return True return True
except (IOError, tarfile.TarError) as e: except (IOError, tarfile.TarError) as e:
_error("Cannot extract archive %s: %s", tarpath, str(e)) logger.error("error: Cannot extract archive %s: %s", tarpath, e)
return False return False
def Sync_NetworkHalf( def Sync_NetworkHalf(
@ -1234,10 +1230,7 @@ class Project(object):
) )
msg_args = self.name msg_args = self.name
msg = msg_template % msg_args msg = msg_template % msg_args
_error( logger.error(msg_template, msg_args)
msg_template,
msg_args,
)
return SyncNetworkHalfResult( return SyncNetworkHalfResult(
False, SyncNetworkHalfError(msg, project=self.name) False, SyncNetworkHalfError(msg, project=self.name)
) )
@ -1250,7 +1243,7 @@ class Project(object):
try: try:
self._FetchArchive(tarpath, cwd=topdir) self._FetchArchive(tarpath, cwd=topdir)
except GitError as e: except GitError as e:
_error("%s", e) logger.error("error: %s", e)
return SyncNetworkHalfResult(False, e) return SyncNetworkHalfResult(False, e)
# From now on, we only need absolute tarpath. # From now on, we only need absolute tarpath.
@ -1267,7 +1260,7 @@ class Project(object):
try: try:
platform_utils.remove(tarpath) platform_utils.remove(tarpath)
except OSError as e: except OSError as e:
_warn("Cannot remove archive %s: %s", tarpath, str(e)) logger.warn("warn: Cannot remove archive %s: %s", tarpath, e)
self._CopyAndLinkFiles() self._CopyAndLinkFiles()
return SyncNetworkHalfResult(True) return SyncNetworkHalfResult(True)
@ -1762,17 +1755,17 @@ class Project(object):
""" """
if self.IsDirty(): if self.IsDirty():
if force: if force:
print( logger.warn(
"warning: %s: Removing dirty project: uncommitted changes " "warning: %s: Removing dirty project: uncommitted changes "
"lost." % (self.RelPath(local=False),), "lost.",
file=sys.stderr, self.RelPath(local=False),
) )
else: else:
msg = ( msg = (
"error: %s: Cannot remove project: uncommitted" "error: %s: Cannot remove project: uncommitted"
"changes are present.\n" % self.RelPath(local=False) "changes are present.\n" % self.RelPath(local=False)
) )
print(msg, file=sys.stderr) logger.error(msg)
raise DeleteDirtyWorktreeError(msg, project=self) raise DeleteDirtyWorktreeError(msg, project=self)
if not quiet: if not quiet:
@ -1819,12 +1812,11 @@ class Project(object):
platform_utils.rmtree(self.gitdir) platform_utils.rmtree(self.gitdir)
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
print("error: %s: %s" % (self.gitdir, e), file=sys.stderr) logger.error("error: %s: %s", self.gitdir, e)
print( logger.error(
"error: %s: Failed to delete obsolete checkout; remove " "error: %s: Failed to delete obsolete checkout; remove "
"manually, then run `repo sync -l`." "manually, then run `repo sync -l`.",
% (self.RelPath(local=False),), self.RelPath(local=False),
file=sys.stderr,
) )
raise DeleteWorktreeError(aggregate_errors=[e]) raise DeleteWorktreeError(aggregate_errors=[e])
@ -1840,10 +1832,7 @@ class Project(object):
platform_utils.remove(path) platform_utils.remove(path)
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
print( logger.error("error: %s: Failed to remove: %s", path, e)
"error: %s: Failed to remove: %s" % (path, e),
file=sys.stderr,
)
failed = True failed = True
errors.append(e) errors.append(e)
dirs[:] = [ dirs[:] = [
@ -1862,10 +1851,7 @@ class Project(object):
platform_utils.remove(d) platform_utils.remove(d)
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
print( logger.error("error: %s: Failed to remove: %s", d, e)
"error: %s: Failed to remove: %s" % (d, e),
file=sys.stderr,
)
failed = True failed = True
errors.append(e) errors.append(e)
elif not platform_utils.listdir(d): elif not platform_utils.listdir(d):
@ -1873,21 +1859,16 @@ class Project(object):
platform_utils.rmdir(d) platform_utils.rmdir(d)
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
print( logger.error("error: %s: Failed to remove: %s", d, e)
"error: %s: Failed to remove: %s" % (d, e),
file=sys.stderr,
)
failed = True failed = True
errors.append(e) errors.append(e)
if failed: if failed:
print( logger.error(
"error: %s: Failed to delete obsolete checkout." "error: %s: Failed to delete obsolete checkout.",
% (self.RelPath(local=False),), self.RelPath(local=False),
file=sys.stderr,
) )
print( logger.error(
" Remove manually, then run `repo sync -l`.", " Remove manually, then run `repo sync -l`.",
file=sys.stderr,
) )
raise DeleteWorktreeError(aggregate_errors=errors) raise DeleteWorktreeError(aggregate_errors=errors)
@ -2781,7 +2762,7 @@ class Project(object):
print("Curl output:\n%s" % output) print("Curl output:\n%s" % output)
return False return False
elif curlret and not verbose and output: elif curlret and not verbose and output:
print("%s" % output, file=sys.stderr) logger.error("%s", output)
if os.path.exists(tmpPath): if os.path.exists(tmpPath):
if curlret == 0 and self._IsValidBundle(tmpPath, quiet): if curlret == 0 and self._IsValidBundle(tmpPath, quiet):
@ -2800,10 +2781,7 @@ class Project(object):
return True return True
else: else:
if not quiet: if not quiet:
print( logger.error("Invalid clone.bundle file; ignoring.")
"Invalid clone.bundle file; ignoring.",
file=sys.stderr,
)
return False return False
except OSError: except OSError:
return False return False
@ -2923,9 +2901,8 @@ class Project(object):
self._CheckDirReference(self.objdir, self.gitdir) self._CheckDirReference(self.objdir, self.gitdir)
except GitError as e: except GitError as e:
if force_sync: if force_sync:
print( logger.error(
"Retrying clone after deleting %s" % self.gitdir, "Retrying clone after deleting %s", self.gitdir
file=sys.stderr,
) )
try: try:
platform_utils.rmtree( platform_utils.rmtree(
@ -3046,8 +3023,8 @@ class Project(object):
# hardlink below. # hardlink below.
if not filecmp.cmp(stock_hook, dst, shallow=False): if not filecmp.cmp(stock_hook, dst, shallow=False):
if not quiet: if not quiet:
_warn( logger.warn(
"%s: Not replacing locally modified %s hook", "warn: %s: Not replacing locally modified %s hook",
self.RelPath(local=False), self.RelPath(local=False),
name, name,
) )
@ -3158,7 +3135,12 @@ class Project(object):
src = platform_utils.realpath(src_path) src = platform_utils.realpath(src_path)
# Fail if the links are pointing to the wrong place. # Fail if the links are pointing to the wrong place.
if src != dst: if src != dst:
_error("%s is different in %s vs %s", name, destdir, srcdir) logger.error(
"error: %s is different in %s vs %s",
name,
destdir,
srcdir,
)
raise GitError( raise GitError(
"--force-sync not enabled; cannot overwrite a local " "--force-sync not enabled; cannot overwrite a local "
"work tree. If you're comfortable with the " "work tree. If you're comfortable with the "
@ -4206,7 +4188,7 @@ class ManifestProject(MetaProject):
"manifest.standalone" "manifest.standalone"
) )
if was_standalone_manifest and not manifest_url: if was_standalone_manifest and not manifest_url:
print( logger.error(
"fatal: repo was initialized with a standlone manifest, " "fatal: repo was initialized with a standlone manifest, "
"cannot be re-initialized without --manifest-url/-u" "cannot be re-initialized without --manifest-url/-u"
) )
@ -4224,7 +4206,7 @@ class ManifestProject(MetaProject):
is_new = not self.Exists is_new = not self.Exists
if is_new: if is_new:
if not manifest_url: if not manifest_url:
print("fatal: manifest url is required.", file=sys.stderr) logger.error("fatal: manifest url is required.")
return False return False
if verbose: if verbose:
@ -4280,7 +4262,7 @@ class ManifestProject(MetaProject):
if manifest_branch == "HEAD": if manifest_branch == "HEAD":
manifest_branch = self.ResolveRemoteHead() manifest_branch = self.ResolveRemoteHead()
if manifest_branch is None: if manifest_branch is None:
print("fatal: unable to resolve HEAD", file=sys.stderr) logger.error("fatal: unable to resolve HEAD")
return False return False
self.revisionExpr = manifest_branch self.revisionExpr = manifest_branch
else: else:
@ -4305,7 +4287,7 @@ class ManifestProject(MetaProject):
elif platform in all_platforms: elif platform in all_platforms:
groups.append(platformize(platform)) groups.append(platformize(platform))
elif platform != "none": elif platform != "none":
print("fatal: invalid platform flag", file=sys.stderr) logger.error("fatal: invalid platform flag", file=sys.stderr)
return False return False
self.config.SetString("manifest.platform", platform) self.config.SetString("manifest.platform", platform)
@ -4326,35 +4308,29 @@ class ManifestProject(MetaProject):
if worktree: if worktree:
if mirror: if mirror:
print( logger.error("fatal: --mirror and --worktree are incompatible")
"fatal: --mirror and --worktree are incompatible",
file=sys.stderr,
)
return False return False
if submodules: if submodules:
print( logger.error(
"fatal: --submodules and --worktree are incompatible", "fatal: --submodules and --worktree are incompatible"
file=sys.stderr,
) )
return False return False
self.config.SetBoolean("repo.worktree", worktree) self.config.SetBoolean("repo.worktree", worktree)
if is_new: if is_new:
self.use_git_worktrees = True self.use_git_worktrees = True
print("warning: --worktree is experimental!", file=sys.stderr) logger.warn("warning: --worktree is experimental!")
if archive: if archive:
if is_new: if is_new:
self.config.SetBoolean("repo.archive", archive) self.config.SetBoolean("repo.archive", archive)
else: else:
print( logger.error(
"fatal: --archive is only supported when initializing a " "fatal: --archive is only supported when initializing a "
"new workspace.", "new workspace."
file=sys.stderr,
) )
print( logger.error(
"Either delete the .repo folder in this workspace, or " "Either delete the .repo folder in this workspace, or "
"initialize in another location.", "initialize in another location."
file=sys.stderr,
) )
return False return False
@ -4362,24 +4338,21 @@ class ManifestProject(MetaProject):
if is_new: if is_new:
self.config.SetBoolean("repo.mirror", mirror) self.config.SetBoolean("repo.mirror", mirror)
else: else:
print( logger.error(
"fatal: --mirror is only supported when initializing a new " "fatal: --mirror is only supported when initializing a new "
"workspace.", "workspace."
file=sys.stderr,
) )
print( logger.error(
"Either delete the .repo folder in this workspace, or " "Either delete the .repo folder in this workspace, or "
"initialize in another location.", "initialize in another location."
file=sys.stderr,
) )
return False return False
if partial_clone is not None: if partial_clone is not None:
if mirror: if mirror:
print( logger.error(
"fatal: --mirror and --partial-clone are mutually " "fatal: --mirror and --partial-clone are mutually "
"exclusive", "exclusive"
file=sys.stderr,
) )
return False return False
self.config.SetBoolean("repo.partialclone", partial_clone) self.config.SetBoolean("repo.partialclone", partial_clone)
@ -4409,11 +4382,10 @@ class ManifestProject(MetaProject):
self.config.SetBoolean("repo.git-lfs", git_lfs) self.config.SetBoolean("repo.git-lfs", git_lfs)
if not is_new: if not is_new:
print( logger.warn(
"warning: Changing --git-lfs settings will only affect new " "warning: Changing --git-lfs settings will only affect new "
"project checkouts.\n" "project checkouts.\n"
" Existing projects will require manual updates.\n", " Existing projects will require manual updates.\n"
file=sys.stderr,
) )
if clone_filter_for_depth is not None: if clone_filter_for_depth is not None:
@ -4437,9 +4409,7 @@ class ManifestProject(MetaProject):
).success ).success
if not success: if not success:
r = self.GetRemote() r = self.GetRemote()
print( logger.error("fatal: cannot obtain manifest %s", r.url)
"fatal: cannot obtain manifest %s" % r.url, file=sys.stderr
)
# Better delete the manifest git dir if we created it; otherwise # Better delete the manifest git dir if we created it; otherwise
# next time (when user fixes problems) we won't go through the # next time (when user fixes problems) we won't go through the
@ -4460,14 +4430,13 @@ class ManifestProject(MetaProject):
self.StartBranch("default") self.StartBranch("default")
except GitError as e: except GitError as e:
msg = str(e) msg = str(e)
print( logger.error(
f"fatal: cannot create default in manifest {msg}", "fatal: cannot create default in manifest %s", msg
file=sys.stderr,
) )
return False return False
if not manifest_name: if not manifest_name:
print("fatal: manifest name (-m) is required.", file=sys.stderr) logger.error("fatal: manifest name (-m) is required.")
return False return False
elif is_new: elif is_new:
@ -4482,11 +4451,8 @@ class ManifestProject(MetaProject):
try: try:
self.manifest.Link(manifest_name) self.manifest.Link(manifest_name)
except ManifestParseError as e: except ManifestParseError as e:
print( logger.error("fatal: manifest '%s' not available", manifest_name)
"fatal: manifest '%s' not available" % manifest_name, logger.error("fatal: %s", e)
file=sys.stderr,
)
print("fatal: %s" % str(e), file=sys.stderr)
return False return False
if not this_manifest_only: if not this_manifest_only:
@ -4528,13 +4494,13 @@ class ManifestProject(MetaProject):
submanifest = "" submanifest = ""
if self.manifest.path_prefix: if self.manifest.path_prefix:
submanifest = f"for {self.manifest.path_prefix} " submanifest = f"for {self.manifest.path_prefix} "
print( logger.warn(
f"warning: git update of superproject {submanifest}failed, " "warning: git update of superproject %s failed, "
"repo sync will not use superproject to fetch source; " "repo sync will not use superproject to fetch source; "
"while this error is not fatal, and you can continue to " "while this error is not fatal, and you can continue to "
"run repo sync, please run repo init with the " "run repo sync, please run repo init with the "
"--no-use-superproject option to stop seeing this warning", "--no-use-superproject option to stop seeing this warning",
file=sys.stderr, submanifest,
) )
if sync_result.fatal and use_superproject is not None: if sync_result.fatal and use_superproject is not None:
return False return False