Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
PR #1730: tidy some Python files
Browse files Browse the repository at this point in the history
  • Loading branch information
reidpr authored Sep 15, 2023
1 parent 5c32f0f commit 392abef
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 137 deletions.
27 changes: 12 additions & 15 deletions lib/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,6 @@ def commit(self):
self.git_hash = bu.cache.commit(path, self.sid, str(self),
self.commit_files)

def ready(self):
bu.cache.ready(self.image)

def execute(self):
"""Do what the instruction says. At this point, the unpack directory is
all ready to go. Thus, the method is cache-ignorant."""
Expand Down Expand Up @@ -482,6 +479,9 @@ def prepare(self, miss_ct):
self.git_hash = bu.cache.find_sid(self.sid, self.image.ref.for_path)
return miss_ct + int(self.miss)

def ready(self):
bu.cache.ready(self.image)

def rollback(self):
"""Discard everything done by execute(), which may have completed
partially, fully, or not at all."""
Expand All @@ -490,14 +490,14 @@ def rollback(self):
def unsupported_forever_warn(self, msg):
ch.WARNING("not supported, ignored: %s %s" % (self.str_name, msg))

def unsupported_yet_warn(self, msg, issue_no):
ch.WARNING("not yet supported, ignored: issue #%d: %s %s"
% (issue_no, self.str_name, msg))

def unsupported_yet_fatal(self, msg, issue_no):
ch.FATAL("not yet supported: issue #%d: %s %s"
% (issue_no, self.str_name, msg))

def unsupported_yet_warn(self, msg, issue_no):
ch.WARNING("not yet supported, ignored: issue #%d: %s %s"
% (issue_no, self.str_name, msg))


class Instruction_Unsupported(Instruction):

Expand Down Expand Up @@ -1059,6 +1059,10 @@ def checkout_for_build(self):
assert (isinstance(bu.cache, bu.Disabled_Cache))
super().checkout_for_build(self.base_image)

def execute(self):
# Everything happens in prepare().
pass

def metadata_update(self, *args):
# FROM doesn’t update metadata because it never misses when the cache is
# enabled, so this would never be called, and we want disabled results
Expand Down Expand Up @@ -1142,10 +1146,6 @@ def prepare(self, miss_ct):
# Done.
return int(self.miss) # will still miss in disabled mode

def execute(self):
# Everything happens in prepare().
pass


class Run(Instruction):

Expand All @@ -1168,7 +1168,7 @@ def str_name(self):
elif (cli.force == ch.Force_Mode.SECCOMP):
tag = ".S"
else:
assert False, "unreachable code reached"
assert False, "unreachable code reached (force mode = %s)" % cli.force
return super().str_name + tag

def execute(self):
Expand Down Expand Up @@ -1320,6 +1320,3 @@ def unescape(sl):
sl = '"%s"' % sl
assert (len(sl) >= 2 and sl[0] == '"' and sl[-1] == '"' and sl[-2:] != '\\"')
return ast.literal_eval(sl)


# LocalWords: earley topdown iter lineno sid keypair dst srcs pathlib
48 changes: 24 additions & 24 deletions lib/build_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,22 +1181,6 @@ def tag_delete(self, tag, *args, **kwargs):
"""Delete specified git tag. Used for recovering deleted branches."""
return self.git(["tag", "-d", "&%s" % tag], *args, **kwargs)

def tree_print(self):
# Note the percent codes are interpreted by Git.
# See: https://git-scm.com/docs/git-log#_pretty_formats
args = ["log", "--graph", "--all", "--reflog", "--topo-order"]
if (ch.log_level == ch.Log_Level.INFO):
# ref names, subject (instruction), branch heads.
fmt = "%C(auto)%d %Creset%<|(77,trunc)%s"
args.append("--decorate-refs=refs/heads")
else:
# ref names, short commit hash, subject (instruction), body (state ID)
# FIXME: The body contains a trailing newline I can’t figure out how
# to remove.
fmt = "%C(auto)%d%C(yellow) %h %Creset%s %b"
self.git(args + ["--format=%s" % fmt], quiet=False)
print() # blank line to separate from summary

def tree_dot(self):
have_dot()
path_gv = fs.Path(dot_base + ".gv")
Expand All @@ -1220,6 +1204,22 @@ def tree_dot(self):
ch.VERBOSE("writing %s" % path_pdf)
ch.cmd_quiet(["dot", "-Tpdf", "-o%s" % path_pdf, str(path_gv)])

def tree_print(self):
# Note the percent codes are interpreted by Git.
# See: https://git-scm.com/docs/git-log#_pretty_formats
args = ["log", "--graph", "--all", "--reflog", "--topo-order"]
if (ch.log_level == ch.Log_Level.INFO):
# ref names, subject (instruction), branch heads.
fmt = "%C(auto)%d %Creset%<|(77,trunc)%s"
args.append("--decorate-refs=refs/heads")
else:
# ref names, short commit hash, subject (instruction), body (state ID)
# FIXME: The body contains a trailing newline I can’t figure out how
# to remove.
fmt = "%C(auto)%d%C(yellow) %h %Creset%s %b"
self.git(args + ["--format=%s" % fmt], quiet=False)
print() # blank line to separate from summary

def unpack_delete(self, image):
"""Wrapper for Image.unpack_delete() that first detaches the work tree's
head. If we delete an image's unpack path without first detaching HEAD,
Expand Down Expand Up @@ -1280,6 +1280,14 @@ def worktree_adopt(self, image, base):
image.unpack_path.rmtree()
ch.storage.image_tmp.rename_(image.unpack_path)

def worktree_head(self, image):
cp = self.git(["rev-parse", "--short", "HEAD"],
fail_ok=True, cwd=image.unpack_path)
if (cp.returncode != 0):
return None
else:
return cp.stdout.strip()

def worktrees_fix(self):
"""Git stores pointers (paths) both from the main repository to each
worktree, and in the other direction from each worktree back to the
Expand Down Expand Up @@ -1328,14 +1336,6 @@ def worktrees_fix(self):
ch.VERBOSE("fixed %d worktrees" % len(wt_actuals))
t.log("re-linked worktrees")

def worktree_head(self, image):
cp = self.git(["rev-parse", "--short", "HEAD"],
fail_ok=True, cwd=image.unpack_path)
if (cp.returncode != 0):
return None
else:
return cp.stdout.strip()


class Rebuild_Cache(Enabled_Cache):

Expand Down
66 changes: 33 additions & 33 deletions lib/charliecloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Download_Mode(enum.Enum):

# Root emulation mode
class Force_Mode(enum.Enum):
FAKEROOT="fakeroot"
SECCOMP="seccomp"
NONE="none"
FAKEROOT = "fakeroot"
SECCOMP = "seccomp"
NONE = "none"

# Log level
@functools.total_ordering
Expand Down Expand Up @@ -81,8 +81,7 @@ def __lt__(self, other):
#
# [1]: https://stackoverflow.com/a/45125525
# [2]: https://github.com/docker-library/bashbrew/blob/v0.1.0/vendor/github.com/docker-library/go-dockerlibrary/architecture/oci-platform.go
ARCH_MAP = { "x86_64": "amd64",
"armv5l": "arm/v5",
ARCH_MAP = { "armv5l": "arm/v5",
"armv6l": "arm/v6",
"aarch32": "arm/v7",
"armv7l": "arm/v7",
Expand All @@ -92,7 +91,8 @@ def __lt__(self, other):
"i686": "386",
"mips64le": "mips64le",
"ppc64le": "ppc64le",
"s390x": "s390x" } # a.k.a. IBM Z
"s390x": "s390x", # a.k.a. IBM Z
"x86_64": "amd64" }

# Some images have oddly specified architecture. For example, as of
# 2022-06-08, on Docker Hub, opensuse/leap:15.1 offers architectures amd64,
Expand Down Expand Up @@ -160,8 +160,8 @@ class Fatal_Error(Exception):
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
class No_Fatman_Error(Exception): pass
class Image_Unavailable_Error(Exception): pass
class No_Fatman_Error(Exception): pass


## Classes ##
Expand Down Expand Up @@ -323,6 +323,11 @@ def __init__(self, msg, unit, divisor, length):
self.display_last = float("-inf")
self.update(0)

def done(self):
self.update(0, True)
if (self.overwrite_p):
INFO("") # newline to release display line

def update(self, increment, last=False):
now = time.monotonic()
self.progress += increment
Expand All @@ -344,11 +349,6 @@ def update(self, increment, last=False):
INFO(line, end=("\r" if self.overwrite_p else "\n"))
self.display_last = now

def done(self):
self.update(0, True)
if (self.overwrite_p):
INFO("") # newline to release display line


class Progress_Reader:
"""Wrapper around a binary file object to maintain a progress meter while
Expand Down Expand Up @@ -562,6 +562,15 @@ def cmd_base(argv, fail_ok=False, **kwargs):
% (cp.returncode, argv_to_string(argv)))
return cp

def cmd_quiet(argv, **kwargs):
"""Run command using cmd() and return the exit code. If logging is verbose
or lower, discard stdout."""
if (log_level >= Log_Level.DEBUG): # debug or higher
stdout=None
else:
stdout=subprocess.DEVNULL
return cmd(argv, stdout=stdout, **kwargs)

def cmd_stdout(argv, encoding="UTF-8", **kwargs):
"""Run command using cmd_base(), capturing its standard output. Return the
CompletedProcess object (its stdout is available in the “stdout”
Expand All @@ -573,15 +582,6 @@ def cmd_stdout(argv, encoding="UTF-8", **kwargs):
sys.stdout.flush()
return cp

def cmd_quiet(argv, **kwargs):
"""Run command using cmd() and return the exit code. If logging is verbose
or lower, discard stdout."""
if (log_level >= Log_Level.DEBUG): # debug or higher
stdout=None
else:
stdout=subprocess.DEVNULL
return cmd(argv, stdout=stdout, **kwargs)

def color_reset(*fps):
for fp in fps:
color_set("0m", fp)
Expand Down Expand Up @@ -719,18 +719,6 @@ def kill_blocking(pid, timeout=10):
FATAL("timeout of %ds exceeded trying to kill PID %d" % (timeout, pid),
BUG_REPORT_PLZ)

def walk(*args, **kwargs):
"""Wrapper for os.walk(). Return a generator of the files in a directory
tree (root specified in *args). For each directory in said tree, yield a
3-tuple (dirpath, dirnames, filenames), where dirpath is a Path object,
and dirnames and filenames are lists of Path objects. For insight into
these being lists rather than generators, see use of ch.walk() in
I_copy.copy_src_dir()."""
for (dirpath, dirnames, filenames) in os.walk(*args, **kwargs):
yield (fs.Path(dirpath),
[fs.Path(dirname) for dirname in dirnames],
[fs.Path(filename) for filename in filenames])

def log(msg, hint, trace, color, prefix, end="\n"):
if (color is not None):
color_set(color, log_fp)
Expand Down Expand Up @@ -890,6 +878,18 @@ def version_check(argv, min_, required=True, regex=r"(\d+)\.(\d+)\.(\d+)"):
VERBOSE("%s version OK: %d.%d.%d ≥ %d.%d.%d" % ((prog,) + v + min_))
return True

def walk(*args, **kwargs):
"""Wrapper for os.walk(). Return a generator of the files in a directory
tree (root specified in *args). For each directory in said tree, yield a
3-tuple (dirpath, dirnames, filenames), where dirpath is a Path object,
and dirnames and filenames are lists of Path objects. For insight into
these being lists rather than generators, see use of ch.walk() in
I_copy.copy_src_dir()."""
for (dirpath, dirnames, filenames) in os.walk(*args, **kwargs):
yield (fs.Path(dirpath),
[fs.Path(dirname) for dirname in dirnames],
[fs.Path(filename) for filename in filenames])

def warnings_dump():
if (len(warnings) > 0):
WARNING("reprinting %d warning(s)" % len(warnings), msg_save=False)
Expand Down
38 changes: 19 additions & 19 deletions lib/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,22 +339,6 @@ def listdir(self):
effort required to make the change."""
return set(ch.ossafe(os.listdir, "can’t list: %s" % self.name, self))

def strip(self, left=0, right=0):
"""Return a copy of myself with n leading components removed. E.g.:
>>> a = Path("/a/b/c")
>>> a.strip(left=1)
Path("a/b/c")
>>> a.strip(right=1)
Path("/a/b")
>>> a.strip(left=1, right=1)
Path("a/b")
It is an error if I don’t have at least left + right components,
i.e., you can strip a path down to nothing but not further."""
assert (len(self.parts) >= left + right)
return Path(*self.parts[left:len(self.parts)-right])

def mkdir_(self):
ch.TRACE("ensuring directory: %s" % self)
try:
Expand Down Expand Up @@ -412,6 +396,22 @@ def stat_(self, links):
return ch.ossafe(os.stat, "can’t stat: %s" % self, self,
follow_symlinks=links)

def strip(self, left=0, right=0):
"""Return a copy of myself with n leading components removed. E.g.:
>>> a = Path("/a/b/c")
>>> a.strip(left=1)
Path("a/b/c")
>>> a.strip(right=1)
Path("/a/b")
>>> a.strip(left=1, right=1)
Path("a/b")
It is an error if I don’t have at least left + right components,
i.e., you can strip a path down to nothing but not further."""
assert (len(self.parts) >= left + right)
return Path(*self.parts[left:len(self.parts)-right])

def symlink(self, target, clobber=False):
if (clobber and self.is_file()):
self.unlink_()
Expand Down Expand Up @@ -522,6 +522,9 @@ def root_env():
def build_large_path(self, name):
return self.build_large // name

def fatman_for_download(self, image_ref):
return self.download_cache // ("%s.fat.json" % image_ref.for_path)

def init(self):
"""Ensure the storage directory exists, contains all the appropriate
top-level directories & metadata, and is the appropriate version."""
Expand Down Expand Up @@ -620,9 +623,6 @@ def manifest_for_download(self, image_ref, digest):
return ( self.download_cache
// ("%s%%%s.manifest.json" % (image_ref.for_path, digest)))

def fatman_for_download(self, image_ref):
return self.download_cache // ("%s.fat.json" % image_ref.for_path)

def reset(self):
if (self.valid_p):
self.root.rmtree()
Expand Down
Loading

0 comments on commit 392abef

Please sign in to comment.