From e3f8275b7150fd84c7e2a5ca7d7d45c991f6fcee Mon Sep 17 00:00:00 2001 From: Hauke D Date: Fri, 31 May 2024 19:23:00 +0000 Subject: [PATCH] Improved --checksum filename escaping --- CHANGELOG.md | 7 +++++++ pyproject.toml | 2 +- unzipwalk/__init__.py | 20 +++++++++++--------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab172ae..246cdfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,16 @@ Changelog for unzipwalk ======================= +1.2.1 - Fri, May 31 2024 +------------------------ + +- Improved escaping of filenames with `--checksum` CLI option. + 1.2.0 - Fri, May 31 2024 ------------------------ +`commit b9fd55222886dd3e074ea374dbd901f9024ee497` + - Added `matcher` argument to `unzipwalk()` and corresponding `--exclude` CLI option. 1.1.0 - Fri, May 31 2024 diff --git a/pyproject.toml b/pyproject.toml index c0aef15..fc30e51 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "unzipwalk" description = "Recursively walk into directories and archives" -version = "1.2.0" +version = "1.2.1" authors = [ { name="Hauke D", email="haukex@zero-g.net" } ] readme = "README.md" requires-python = ">=3.9" diff --git a/unzipwalk/__init__.py b/unzipwalk/__init__.py index 4598348..6438781 100644 --- a/unzipwalk/__init__.py +++ b/unzipwalk/__init__.py @@ -343,17 +343,19 @@ def matcher(paths :Sequence[PurePath]) -> bool: return not any( fnmatch(paths[-1].name, pat) for pat in args.exclude ) for result in unzipwalk( args.paths if args.paths else Path(), matcher=matcher ): names = tuple( str(n) for n in result.names ) - if (args.dump or args.checksum) and result.typ==FileType.FILE: - assert result.hnd is not None # make type checker happy; we know this is true because we checked it's a file - if args.checksum: + if args.checksum: + name = names[0] if len(names)==1 and not names[0].startswith('(') and '\n' not in names[0] and '\r' not in names[0] else repr(names) + if result.typ==FileType.FILE: + assert result.hnd is not None h = hashlib.new(args.checksum) h.update(result.hnd.read()) - print(f"{h.hexdigest()} *{names[0] if len(names)==1 else repr(names)}") - else: + print(f"{h.hexdigest()} *{name}") + elif args.all_files: + print(f"# {result.typ.name} {name}") + else: + if result.typ==FileType.FILE and args.dump: + assert result.hnd is not None print(f"{result.typ.name} {names!r} {result.hnd.read()!r}") - elif result.typ==FileType.FILE or args.all_files: - if args.checksum: - print(f"# {result.typ.name} {names[0] if len(names)==1 else repr(names)}") - else: + elif result.typ==FileType.FILE or args.all_files: print(f"{result.typ.name} {names!r}") parser.exit(0)