changeset 198:c1e875ba4bdc

Put the effective filesystem encoding into the treesum digest file using FSENCODING = <encoding>
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 16 Jan 2025 23:18:04 +0100
parents 48e2610978e5
children b2aba84ca426
files cutils/treesum.py cutils/util/walk.py docs/notes.rst
diffstat 3 files changed, 25 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Thu Jan 16 20:40:24 2025 +0100
+++ b/cutils/treesum.py	Thu Jan 16 23:18:04 2025 +0100
@@ -352,6 +352,8 @@
         self._outfp = outfp
         self._outfp.resetdigest()
         self._outfp.write(format_bsd_line("VERSION", "1", None, False))
+        self._outfp.write(format_bsd_line(
+            "FSENCODING", util.n(walk.getfsencoding()), None, False))
         self._outfp.flush()
 
         #
@@ -628,7 +630,8 @@
     if what == b"TIMESTAMP":
         assert filename is None
         return util.interpolate_bytes(b"TIMESTAMP = %d%s", value, ls)
-    if what in (b"ISOTIMESTAMP", b"FLAGS", b"VERSION", b"CRC32"):
+    if what in (b"FSENCODING", b"ISOTIMESTAMP", b"FLAGS", b"VERSION",
+                b"CRC32"):
         assert filename is None
         return util.interpolate_bytes(b"%s = %s%s", what, util.b(value), ls)
     assert filename is not None
@@ -664,7 +667,7 @@
     """
 
     PATTERN0 = re.compile(br"\A[ \t]*\r?\n\Z")     # empty lines
-    PATTERN1 = re.compile(br"\A(VERSION|FLAGS|TIMESTAMP|ISOTIMESTAMP|CRC32)[ \t]*=[ \t]*([^ \t]+)[ \t]*\r?\n\Z")      # noqa: E501  line too long
+    PATTERN1 = re.compile(br"\A(VERSION|FSENCODING|FLAGS|TIMESTAMP|ISOTIMESTAMP|CRC32)[ \t]*=[ \t]*([^ \t]+)[ \t]*\r?\n\Z")      # noqa: E501  line too long
     PATTERN2 = re.compile(br"\A(ROOT|COMMENT)[ \t]*\((.*)\)[ \t]*\r?\n\Z")
     PATTERN3 = re.compile(br"\ASIZE[ \t]*\((.*)\)[ \t]*=[ \t]*(\d+)[ \t]*\r?\n\Z")                                    # noqa: E501  line too long
     PATTERN4 = re.compile(br"\A([A-Za-z0-9_-]+)[ \t]*\((.*)\)[ \t]*=[ \t]*([A-Za-z0-9=+/]+)(,(\d+))?[ \t]*\r?\n\Z")   # noqa: E501  line too long
@@ -880,7 +883,7 @@
                     root = record[1]
                 elif record[0] == "COMMENT":
                     comments.append(record[1])
-                elif record[0] in ("TIMESTAMP", "ISOTIMESTAMP"):
+                elif record[0] in ("FSENCODING", "TIMESTAMP", "ISOTIMESTAMP"):
                     pass
                 elif record[0] == "CRC32":
                     pass
--- a/cutils/util/walk.py	Thu Jan 16 20:40:24 2025 +0100
+++ b/cutils/util/walk.py	Thu Jan 16 23:18:04 2025 +0100
@@ -10,7 +10,7 @@
 from __future__ import print_function, absolute_import
 
 
-__all__ = ["ScanDir"]
+__all__ = ["ScanDir", "getfsencoding"]
 
 
 import os
@@ -21,10 +21,14 @@
         from scandir import scandir
     except ImportError:
         scandir = None
+import sys
 
 from . import PY2
 
 
+_FSENCODING = sys.getfilesystemencoding()
+
+
 class WalkDirEntry(object):
 
     """A :class:`os.DirEntry` alike to be used in :func:`walk` and for
@@ -41,6 +45,7 @@
             assert isinstance(name, bytes)
             self._fsname = name
         else:
+            self._name = name
             self._fsname = os.fsencode(name)
         self._path = None
         self._fspath = None
@@ -209,3 +214,8 @@
 
         def __exit__(self, *args, **kwds):
             pass
+
+
+def getfsencoding():
+    """Return the stored _FSENCODING of this module"""
+    return _FSENCODING
--- a/docs/notes.rst	Thu Jan 16 20:40:24 2025 +0100
+++ b/docs/notes.rst	Thu Jan 16 23:18:04 2025 +0100
@@ -21,3 +21,11 @@
   ``/./@`
 
       Symlink to other filesystem object
+
+
+Fields:
+
+  ``FSENCODING``
+
+      The result of :func:`os.getfilesystemencoding` that is in effect when
+      generating treesum digests