Mercurial > hgrepos > Python > apps > py-cutils
changeset 117:e51f34ad6d71
Move constant definitions into new module cutils.util.constants
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 29 Dec 2024 17:39:00 +0100 |
| parents | 1856374bbd40 |
| children | 12339ac2148d |
| files | cutils/shasum.py cutils/util/constants.py |
| diffstat | 2 files changed, 49 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/cutils/shasum.py Sun Dec 29 15:43:27 2024 +0100 +++ b/cutils/shasum.py Sun Dec 29 17:39:00 2024 +0100 @@ -24,32 +24,14 @@ except ImportError: mmap = None import os -try: - import pathlib -except ImportError: - pathlib = None import re import stat import sys +from .util import constants from . import (__version__, __revision__) -PY2 = sys.version_info[0] < 3 - -if PY2: - PATH_TYPES = (unicode, str) # noqa: F821 (undefined name 'unicode') -else: - if pathlib: - PATH_TYPES = (str, bytes, pathlib.Path) - else: - PATH_TYPES = (str, bytes) - -READ_CHUNK_SIZE = 2 * 1024 * 1024 # like BUFSIZE_MAX on FreeBSD -MAX_AUTO_MAP_SIZE = 8 * 1024 * 1024 -MAP_WINDOW_SIZE = MAX_AUTO_MAP_SIZE # do not totally trash memory on big files - - def main(argv=None): aparser = argparse.ArgumentParser( description="Python implementation of shasum", @@ -196,7 +178,7 @@ opts.base64) else: if not opts.files or (len(opts.files) == 1 and opts.files[0] == '-'): - if PY2: + if constants.PY2: if sys.platform == "win32": import msvcrt # noqa: E401 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) @@ -265,7 +247,7 @@ dest = opts.dest or sys.stdout exit_code = 0 if not opts.files or (len(opts.files) == 1 and opts.files[0] == '-'): - if PY2: + if constants.PY2: if sys.platform == "win32": import os, msvcrt # noqa: E401 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) @@ -596,7 +578,7 @@ """ h = hashobj() - if isinstance(path, PATH_TYPES): + if isinstance(path, constants.PATH_TYPES): flags = os.O_RDONLY | getattr(os, "O_BINARY", 0) \ | getattr(os, "O_SEQUENTIAL", 0) | getattr(os, "O_NOCTTY", 0) fd = os.open(path, flags) @@ -617,7 +599,7 @@ if stat.S_ISREG(st[stat.ST_MODE]): filesize = st[stat.ST_SIZE] if (use_mmap is None) \ - and (filesize > MAX_AUTO_MAP_SIZE): + and (filesize > constants.MAX_AUTO_MAP_SIZE): # # This is borrowed from FreeBSD's cp(1) implementation: # Mmap and process if less than 8M (the limit is @@ -637,9 +619,9 @@ fadvise = getattr(os, "posix_fadvise", None) if fadvise: fadvise(fd, 0, 0, os.POSIX_FADV_SEQUENTIAL) - if not PY2: + if not constants.PY2: fileobj = io.FileIO(fd, mode="r", closefd=False) - buf = bytearray(READ_CHUNK_SIZE) + buf = bytearray(constants.READ_CHUNK_SIZE) with memoryview(buf) as full_view: while True: try: @@ -652,7 +634,7 @@ else: if n == 0: break - if n == READ_CHUNK_SIZE: + if n == constants.READ_CHUNK_SIZE: h.update(buf) else: with full_view[:n] as partial_view: @@ -660,7 +642,7 @@ else: while True: try: - buf = os.read(fd, READ_CHUNK_SIZE) + buf = os.read(fd, constants.READ_CHUNK_SIZE) except OSError as e: if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK, @@ -678,10 +660,10 @@ # So ensure to not call mmap.mmap() if the file size is 0. # madvise = getattr(mmap.mmap, "madvise", None) - if filesize <= MAP_WINDOW_SIZE: + if filesize <= constants.MAP_WINDOW_SIZE: mapsize = filesize else: - mapsize = MAP_WINDOW_SIZE + mapsize = constants.MAP_WINDOW_SIZE mapoffset = 0 rest = filesize while rest > 0: @@ -717,7 +699,7 @@ h = hashobj() while True: try: - buf = instream.read(READ_CHUNK_SIZE) + buf = instream.read(constants.READ_CHUNK_SIZE) except OSError as e: if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK, errno.EINTR): raise
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cutils/util/constants.py Sun Dec 29 17:39:00 2024 +0100 @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# :- +# :Copyright: (c) 2020-2022 Franz Glasner +# :License: BSD-3-Clause +# :- +r"""Common constants and compatibility definitions. + +""" + +__all__ = ["PY2", + "PATH_TYPES", + "READ_CHUNK_SIZE", + "MAX_AUTO_MAP_SIZE", + "MAP_WINDOW_SIZE" + ] + + +import sys +try: + import pathlib +except ImportError: + pathlib = None + + +PY2 = sys.version_info[0] < 3 + +if PY2: + PATH_TYPES = (unicode, str) # noqa: F821 (undefined name 'unicode') +else: + if pathlib: + PATH_TYPES = (str, bytes, pathlib.Path) + else: + PATH_TYPES = (str, bytes) + +READ_CHUNK_SIZE = 2 * 1024 * 1024 # like BUFSIZE_MAX on FreeBSD +MAX_AUTO_MAP_SIZE = 8 * 1024 * 1024 +MAP_WINDOW_SIZE = MAX_AUTO_MAP_SIZE # do not totally trash memory on big files
