Skip to content

Commit 12fabd0

Browse files
authored
Add files via upload
1 parent 2855dc5 commit 12fabd0

2 files changed

Lines changed: 244 additions & 503 deletions

File tree

archivefile_py3.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@
22
# -*- coding: utf-8 -*-
33
"""archivefile.py (Python 3 only)
44
5-
This script is part of the archivefile/pyarchivefile project.
5+
CLI wrapper for the pyarchivefile module.
66
7-
This refactor removes Python 2 compatibility code paths while preserving the
8-
original CLI behavior and wiring into `pyarchivefile`.
7+
This version keeps the original CLI behavior and call wiring, while removing
8+
Python 2 compatibility remnants and tightening a few small implementation details.
99
"""
1010

11+
from __future__ import annotations
12+
1113
import argparse
1214
import binascii
1315
import logging
1416
import os
1517
import sys
16-
from io import BytesIO, StringIO # noqa: F401 (kept for parity with original)
17-
18-
import pyarchivefile_py3 as pyarchivefile
19-
20-
# Text streams (as provided by Python)
21-
PY_STDIN_TEXT = sys.stdin
22-
PY_STDOUT_TEXT = sys.stdout
23-
PY_STDERR_TEXT = sys.stderr
24-
25-
# Binary-friendly streams (.buffer exists on Python 3 text streams)
26-
PY_STDIN_BUF = sys.stdin.buffer
27-
PY_STDOUT_BUF = sys.stdout.buffer
28-
PY_STDERR_BUF = sys.stderr.buffer
18+
from io import BytesIO
19+
from typing import Optional, Dict, Any
20+
21+
# Prefer local Python-3-only builds when present, but fall back to the canonical name.
22+
try:
23+
import pyarchivefile_py3_fixed_v2 as pyarchivefile # type: ignore
24+
except ImportError:
25+
try:
26+
import pyarchivefile_py3_fixed as pyarchivefile # type: ignore
27+
except ImportError:
28+
import pyarchivefile # type: ignore
2929

3030
# Keep original behavior: log to stdout with simple message format.
31-
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
31+
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
3232

33-
# Unix SIGPIPE handling (matches original intent: exit cleanly on broken pipe)
33+
# Unix SIGPIPE handling (exit cleanly on broken pipe).
3434
if os.name != "nt":
3535
import signal
3636

3737
if hasattr(signal, "SIGPIPE"):
3838

39-
def _sigpipe_handler(signum, frame):
39+
def _sigpipe_handler(signum, frame): # noqa: ARG001
4040
pyarchivefile.VerbosePrintOut("Received SIGPIPE, exiting gracefully.", "info")
4141
raise SystemExit(0)
4242

@@ -91,7 +91,7 @@ def _build_argparser() -> argparse.ArgumentParser:
9191
# Operations
9292
p.add_argument("-c", "--create", action="store_true", help="Perform only the concatenation operation.")
9393
p.add_argument("-e", "--extract", action="store_true", help="Perform only the extraction operation.")
94-
p.add_argument("-t", "--convert", action="store_true", help="Convert a tar/zip/rar/7zip file to a archive file.")
94+
p.add_argument("-t", "--convert", action="store_true", help="Convert a tar/zip/rar/7zip file to an archive file.")
9595
p.add_argument("-r", "--repack", action="store_true", help="Re-concatenate files, fixing checksum errors if any.")
9696
p.add_argument("-S", "--filestart", type=int, default=0, help="Start reading file at.")
9797

@@ -128,7 +128,7 @@ def _build_argparser() -> argparse.ArgumentParser:
128128
return p
129129

130130

131-
def _resolve_format(getargs):
131+
def _resolve_format(getargs: argparse.Namespace) -> Any:
132132
"""Compute the format dict exactly as the original script did."""
133133
global __file_format_default__ # keep parity with original module-level behavior
134134

@@ -158,7 +158,7 @@ def _resolve_format(getargs):
158158
return fnamedict
159159

160160

161-
def main(argv=None) -> int:
161+
def main(argv: Optional[list[str]] = None) -> int:
162162
argparser = _build_argparser()
163163
getargs = argparser.parse_args(argv)
164164

@@ -174,7 +174,6 @@ def main(argv=None) -> int:
174174
# Preserve original behavior: do nothing if no action flag is set.
175175
return 0
176176

177-
# Execute the appropriate functions based on determined actions and arguments
178177
if active_action == "create":
179178
if getargs.convert:
180179
checkcompressfile = pyarchivefile.CheckCompressionSubType(input_file, fnamedict, 0, True)
@@ -251,7 +250,6 @@ def main(argv=None) -> int:
251250
(pyarchivefile.IsNestedDict(fnamedict) and checkcompressfile in fnamedict)
252251
or (pyarchivefile.IsSingleDict(fnamedict) and checkcompressfile == fnamedict["format_magic"])
253252
):
254-
# NOTE: original script forgot to store the return value (tmpout) here.
255253
tmpout = pyarchivefile.RePackArchiveFile(
256254
input_file,
257255
getargs.output,

0 commit comments

Comments
 (0)