changeset 178:dac26a2d9de5

Cleanup: remove non used walk-code
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 11 Jan 2025 21:18:53 +0100
parents 089c40240061
children 53614a724bf0
files cutils/treesum.py cutils/util/walk.py
diffstat 2 files changed, 3 insertions(+), 337 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Sat Jan 11 17:41:28 2025 +0100
+++ b/cutils/treesum.py	Sat Jan 11 21:18:53 2025 +0100
@@ -293,17 +293,6 @@
                 minimal=opts.minimal).generate(
                     outfp, d, comment=opts.comment)
 
-            generate_treesum_for_directory(
-                outfp, d, opts.algorithm, opts.mmap, opts.base64, opts.logical,
-                opts.follow_directory_symlinks,
-                opts.metadata_mode,
-                opts.metadata_full_mode,
-                opts.metadata_mtime,
-                opts.size_only,
-                opts.print_size,
-                minimal=opts.minimal,
-                comment=opts.comment)
-
 
 class V1DirectoryTreesumGenerator(object):
 
@@ -533,209 +522,6 @@
         return (dir_dgst.digest(), dir_size)
 
 
-def generate_treesum_for_directory(
-        outfp, root, algorithm, use_mmap, use_base64, handle_root_logical,
-        follow_directory_symlinks, with_metadata_mode, with_metadata_full_mode,
-        with_metadata_mtime, size_only, print_size,
-        minimal=None, comment=None):
-    """
-
-    :param outfp: a *binary* file with a "write()" and a "flush()" method
-
-    """
-    outfp.write(format_bsd_line("VERSION", "1", None, False))
-    outfp.flush()
-
-    # Note given non-default flags that are relevant for directory traversal
-    flags = []
-    if with_metadata_full_mode:
-        flags.append("with-metadata-fullmode")
-    elif with_metadata_mode:
-        flags.append("with-metadata-mode")
-    if with_metadata_mtime:
-        flags.append("with-metadata-mtime")
-    if handle_root_logical:
-        flags.append("logical")
-    if follow_directory_symlinks:
-        flags.append("follow-directory-symlinks")
-    if size_only:
-        flags.append("size-only")
-    else:
-        if print_size:
-            flags.append("print-size")
-    if flags:
-        flags.sort()
-        outfp.write(format_bsd_line("FLAGS", ",".join(flags), None, False))
-        outfp.flush()
-
-    if minimal is None:
-        # Write execution timestamps in POSIX epoch and ISO format
-        ts = int(time.time())
-        outfp.write(format_bsd_line("TIMESTAMP", ts, None, False))
-        ts = (datetime.datetime.utcfromtimestamp(ts)).isoformat("T")
-        outfp.write(format_bsd_line("ISOTIMESTAMP", ts, None, False))
-        outfp.flush()
-
-        if comment:
-            for line in comment:
-                outfp.write(format_bsd_line("COMMENT", None, line, False))
-
-    if minimal is not None:
-        outfp.write(
-            format_bsd_line(
-                "ROOT", None, minimal if minimal else "", False))
-    else:
-        outfp.write(format_bsd_line("ROOT", None, root, False))
-    outfp.flush()
-
-    dir_digests = {}
-
-    if not handle_root_logical and os.path.islink(root):
-        linktgt = util.fsencode(os.readlink(root))
-        linkdgst = algorithm[0]()
-        linkdgst.update(
-            util.interpolate_bytes(b"%d:%s,", len(linktgt), linktgt))
-        dir_dgst = algorithm[0]()
-        dir_dgst.update(b"1:L,")
-        dir_dgst.update(
-            util.interpolate_bytes(
-                b"%d:%s,", len(linkdgst.digest()), linkdgst.digest()))
-        if size_only:
-            outfp.write(
-                format_bsd_line(
-                    "SIZE",
-                    None,
-                    "./@",
-                    False,
-                    0))
-        else:
-            outfp.write(
-                format_bsd_line(
-                    algorithm[1],
-                    dir_dgst.digest(),
-                    "./@",
-                    use_base64))
-        outfp.flush()
-        return
-
-    for top, fsobjects in walk.walk(
-            root,
-            follow_symlinks=follow_directory_symlinks):
-        dir_dgst = algorithm[0]()
-        dir_size = 0
-
-        for fso in fsobjects:
-            if fso.is_dir:
-                if fso.is_symlink and not follow_directory_symlinks:
-                    linktgt = util.fsencode(os.readlink(fso.path))
-                    linkdgst = algorithm[0]()
-                    linkdgst.update(
-                        util.interpolate_bytes(
-                            b"%d:%s,", len(linktgt), linktgt))
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"1:S,%d:%s,", len(fso.fsname), fso.fsname))
-                    # no mtime and no mode for symlinks
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"%d:%s,",
-                        len(linkdgst.digest()), linkdgst.digest()))
-                    opath = "/".join(top) + "/" + fso.name if top else fso.name
-                    if size_only:
-                        outfp.write(
-                            format_bsd_line(
-                                "SIZE",
-                                None,
-                                "%s/./@" % (opath,),
-                                False,
-                                0))
-                    else:
-                        outfp.write(
-                            format_bsd_line(
-                                algorithm[1],
-                                linkdgst.digest(),
-                                "%s/./@" % (opath,),
-                                use_base64))
-                    outfp.flush()
-                    continue
-                # fetch from dir_digests
-                dgst, dsz = dir_digests[top + (fso.name,)]
-                dir_size += dsz
-                dir_dgst.update(util.interpolate_bytes(
-                    b"1:d,%d:%s,", len(fso.fsname), fso.fsname))
-                dir_dgst.update(util.interpolate_bytes(
-                    b"%d:%s,", len(dgst), dgst))
-                if with_metadata_full_mode:
-                    modestr = normalized_mode_str(fso.stat.st_mode)
-                    if not isinstance(modestr, bytes):
-                        modestr = modestr.encode("ascii")
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"8:fullmode,%d:%s,", len(modestr), modestr))
-                elif with_metadata_mode:
-                    modestr = normalized_compatible_mode_str(fso.stat.st_mode)
-                    if not isinstance(modestr, bytes):
-                        modestr = modestr.encode("ascii")
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"4:mode,%d:%s,", len(modestr), modestr))
-            else:
-                dir_dgst.update(util.interpolate_bytes(
-                    b"1:f,%d:%s,", len(fso.fsname), fso.fsname))
-                dir_size += fso.stat.st_size
-                if with_metadata_mtime:
-                    mtime = datetime.datetime.utcfromtimestamp(
-                        int(fso.stat.st_mtime))
-                    mtime = mtime.isoformat("T") + "Z"
-                    if not isinstance(mtime, bytes):
-                        mtime = mtime.encode("ascii")
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"5:mtime,%d:%s,", len(mtime), mtime))
-                if with_metadata_full_mode:
-                    modestr = normalized_mode_str(fso.stat.st_mode)
-                    if not isinstance(modestr, bytes):
-                        modestr = modestr.encode("ascii")
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"8:fullmode,%d:%s,", len(modestr), modestr))
-                elif with_metadata_mode:
-                    modestr = normalized_compatible_mode_str(fso.stat.st_mode)
-                    if not isinstance(modestr, bytes):
-                        modestr = modestr.encode("ascii")
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"4:mode,%d:%s,", len(modestr), modestr))
-                if not size_only:
-                    dgst = digest.compute_digest_file(
-                        algorithm[0], fso.path, use_mmap=use_mmap)
-                    dir_dgst.update(util.interpolate_bytes(
-                        b"%d:%s,", len(dgst), dgst))
-                opath = "/".join(top) + "/" + fso.name if top else fso.name
-                if size_only:
-                    outfp.write(
-                        format_bsd_line(
-                            "SIZE", None, opath, False, fso.stat.st_size))
-                else:
-                    if print_size:
-                        outfp.write(
-                            format_bsd_line(
-                                algorithm[1], dgst, opath, use_base64,
-                                fso.stat.st_size))
-                    else:
-                        outfp.write(
-                            format_bsd_line(
-                                algorithm[1], dgst, opath, use_base64))
-                outfp.flush()
-        opath = "/".join(top) + "/" if top else ""
-        if size_only:
-            outfp.write(format_bsd_line(
-                    "SIZE", None, opath, False, dir_size))
-        else:
-            if print_size:
-                outfp.write(format_bsd_line(
-                    algorithm[1], dir_dgst.digest(), opath,
-                    use_base64, dir_size))
-            else:
-                outfp.write(format_bsd_line(
-                    algorithm[1], dir_dgst.digest(), opath, use_base64))
-        outfp.flush()
-        dir_digests[top] = (dir_dgst.digest(), dir_size)
-
-
 def normalized_compatible_mode_str(mode):
     # XXX FIXME: Windows and "executable"
     modebits = stat.S_IMODE(mode)
--- a/cutils/util/walk.py	Sat Jan 11 17:41:28 2025 +0100
+++ b/cutils/util/walk.py	Sat Jan 11 21:18:53 2025 +0100
@@ -7,8 +7,7 @@
 
 """
 
-__all__ = ["walk",
-           "ScanDir"]
+__all__ = ["ScanDir"]
 
 
 import os
@@ -21,7 +20,6 @@
         scandir = None
 
 from . import PY2
-from .cm import nullcontext
 
 
 class WalkDirEntry(object):
@@ -142,87 +140,9 @@
         return entry._fsname
 
 
-def walk(root, follow_symlinks=False):
-    """A heyvily customized :func:`os.walk` alike that differs from the
-    original:
-
-    - optimized for use in :command:`treesum`
-    - most errors are not suppressed
-    - the `root` is never part of the returned data
-    - the returned directory in "top" is not a string form but a list of
-      individual path segments
-    - there is only one yielded list
-
-      * contains :class:`WalkDirEntry`
-      * sorted by its fsname
-
-      The caller can easily get the old dirs and nondirs by filtering
-      the yielded list using "entry.is_dir".
-
-    - recurse into sub-directories first ("topdown=False")
-    - sort consistently all yielded lists by the filesystem encoding
-
-    .. note:: The implementation is based on Python 3.11 and needs a
-              functional :func:`os.scandir` or :func:`scandir.scandir`
-              implementation. It intentionally follows the logic in
-              Python 3.11 while it could be simplified because we are not
-              implementing some of the original flags (e.g. like
-              `topdown`).
-
-    """
-    normed_root = os.path.normpath(root)
-    yield from _walk(normed_root, tuple(), follow_symlinks=follow_symlinks)
-
-
 if scandir:
 
-    def _walk(root, top, follow_symlinks):
-        """:func:`walk` helper.
-
-        Implemented using :func:`os.scandir`.
-
-        """
-        if top:
-            path = os.path.join(root, *top)
-        else:
-            path = root
-
-        fsobjects, walk_dirs = [], []
-
-        scandir_cm = scandir(path)
-        if not hasattr(scandir_cm, "close"):
-            scandir_cm = nullcontext(scandir_cm)
-        with scandir_cm as scandir_it:
-            while True:
-                try:
-                    entry = WalkDirEntry.from_direntry(next(scandir_it))
-                except StopIteration:
-                    break
-                fsobjects.append(entry)
-                #
-                # Always bottom-up: recurse into sub-directories, but exclude
-                # symlinks to directories if follow_symlinks is False
-                #
-                if entry.is_dir:
-                    if follow_symlinks:
-                        walk_into = True
-                    else:
-                        walk_into = not entry.is_symlink
-                    if walk_into:
-                        walk_dirs.append(entry)
-
-        # Sort by low-level filesystem encoding
-        walk_dirs.sort(key=WalkDirEntry.sort_key)
-        fsobjects.sort(key=WalkDirEntry.sort_key)
-
-        # Recurse into sub-directories
-        for wd in walk_dirs:
-            yield from _walk(root, top + (wd.name,), follow_symlinks)
-        # Yield after recursion if going bottom up
-        yield top, fsobjects
-
-
-    class ScanDir(object):    # noqa: E303   too many blank lines
+    class ScanDir(object):
 
         """An :func:`os.scandir` wrapper that is always an iterator and
         a context manager.
@@ -253,47 +173,7 @@
 
 else:
 
-    def _walk(root, top, follow_symlinks):
-        """:func:`walk` helper.
-
-        Implemented using :func:`os.listdir`.
-
-        """
-        if top:
-            path = os.path.join(root, *top)
-        else:
-            path = root
-
-        fsobjects, walk_dirs = [], []
-
-        names = os.listdir(path)
-        for name in names:
-            entry = WalkDirEntry.from_path_name(path, name)
-            fsobjects.append(entry)
-            #
-            # Always bottom-up: recurse into sub-directories, but exclude
-            # symlinks to directories if follow_symlinks is False
-            #
-            if entry.is_dir:
-                if follow_symlinks:
-                    walk_into = True
-                else:
-                    walk_into = not entry.is_symlink
-                if walk_into:
-                    walk_dirs.append(entry)
-
-        # Sort by low-level filesystem encoding
-        walk_dirs.sort(key=WalkDirEntry.sort_key)
-        fsobjects.sort(key=WalkDirEntry.sort_key)
-
-        # Recurse into sub-directories
-        for wd in walk_dirs:
-            yield from _walk(root, top + (wd.name,), follow_symlinks)
-        # Yield after recursion if going bottom up
-        yield top, fsobjects
-
-
-    class ScanDir(object):    # noqa: E303   too many blank lines
+    class ScanDir(object):
 
         """An :func:`os.scandir` wrapper that is always an iterator and
         a context manager.