|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from . import ( |
| 8 | + Bit7zLibrary, |
| 9 | + BitArchiveReader, |
| 10 | + BitArchiveWriter, |
| 11 | + BitCompressionLevel, |
| 12 | + BitInOutFormat, |
| 13 | + FormatBZip2, |
| 14 | + FormatGZip, |
| 15 | + FormatSevenZip, |
| 16 | + FormatTar, |
| 17 | + FormatWim, |
| 18 | + FormatXz, |
| 19 | + FormatZip, |
| 20 | + OverwriteMode, |
| 21 | + lib7zip_context, |
| 22 | +) |
| 23 | + |
| 24 | +WRITABLE_FORMATS = { |
| 25 | + "7z": FormatSevenZip, |
| 26 | + "zip": FormatZip, |
| 27 | + "tar": FormatTar, |
| 28 | + "gzip": FormatGZip, |
| 29 | + "bzip2": FormatBZip2, |
| 30 | + "xz": FormatXz, |
| 31 | + "wim": FormatWim, |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +def main() -> None: |
| 36 | + """ |
| 37 | + Main function to parse command-line arguments and perform compression/decompression operations. |
| 38 | + """ |
| 39 | + parser = argparse.ArgumentParser( |
| 40 | + description="pybit7z command-line compression tool" |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + "action", |
| 44 | + choices=["compress", "decompress", "list"], |
| 45 | + help="Operation type (compress/decompress/list)", |
| 46 | + ) |
| 47 | + parser.add_argument("paths", nargs="+", help="File/directory paths to process") |
| 48 | + parser.add_argument( |
| 49 | + "-f", |
| 50 | + "--format", |
| 51 | + choices=WRITABLE_FORMATS.keys(), |
| 52 | + default="7z", |
| 53 | + help="Supported writable formats: 7z, zip, tar, gzip, bzip2, xz, wim", |
| 54 | + ) |
| 55 | + output_group = parser.add_mutually_exclusive_group() |
| 56 | + output_group.add_argument( |
| 57 | + "-o", |
| 58 | + "--output", |
| 59 | + help="Output file/directory path (required for compress/decompress)", |
| 60 | + ) |
| 61 | + output_group.add_argument( |
| 62 | + "-v", "--verbose", action="store_true", help="Show detailed file information" |
| 63 | + ) |
| 64 | + parser.add_argument("-p", "--password", help="Encryption password") |
| 65 | + parser.add_argument( |
| 66 | + "-l", |
| 67 | + "--level", |
| 68 | + type=str, |
| 69 | + choices=BitCompressionLevel.__members__.keys(), |
| 70 | + default=BitCompressionLevel.Nothing.value, |
| 71 | + help="Compression level (%(choices)s)", |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + "--overwrite", |
| 75 | + choices=OverwriteMode.__members__.keys(), |
| 76 | + default=OverwriteMode.Overwrite.value, |
| 77 | + help="Overwrite mode when output exists (%(choices)s)", |
| 78 | + ) |
| 79 | + |
| 80 | + args = parser.parse_args() |
| 81 | + |
| 82 | + if args.action in ["compress", "decompress"] and not args.output: |
| 83 | + parser.error("Output file/directory path is required") |
| 84 | + |
| 85 | + try: |
| 86 | + with lib7zip_context() as lib: |
| 87 | + if args.action == "compress": |
| 88 | + compress(lib, args) |
| 89 | + elif args.action == "decompress": |
| 90 | + decompress(lib, args) |
| 91 | + else: |
| 92 | + list_archive(lib, args) |
| 93 | + except Exception as e: # pylint: disable=W0718 |
| 94 | + sys.stderr.write(f"Error: {e}\n") |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + |
| 98 | +def compress(lib: Bit7zLibrary, args: argparse.Namespace) -> None: |
| 99 | + """ |
| 100 | + Compress files/directories into a specified archive format. |
| 101 | + Args: |
| 102 | + lib (Bit7zLibrary): The 7z library instance. |
| 103 | + args (argparse.Namespace): Parsed command-line arguments. |
| 104 | + """ |
| 105 | + fmt = WRITABLE_FORMATS[args.format] |
| 106 | + assert isinstance(fmt, BitInOutFormat), ( |
| 107 | + f"Format {args.format} is not supported for compression" |
| 108 | + ) |
| 109 | + |
| 110 | + writer = BitArchiveWriter(lib, fmt) |
| 111 | + |
| 112 | + if args.password: |
| 113 | + writer.set_password(args.password) |
| 114 | + |
| 115 | + writer.set_compression_level(BitCompressionLevel(args.level)) |
| 116 | + writer.set_overwrite_mode(OverwriteMode(args.overwrite)) |
| 117 | + |
| 118 | + writer.add_items([path for path in args.paths if Path(path).exists()]) |
| 119 | + |
| 120 | + writer.compress_to(args.output) |
| 121 | + sys.stdout.write(f"Successfully created archive: {args.output}") |
| 122 | + |
| 123 | + |
| 124 | +def decompress(lib: Bit7zLibrary, args: argparse.Namespace) -> None: |
| 125 | + """ |
| 126 | + Decompress an archive to a specified directory. |
| 127 | + Args: |
| 128 | + lib (Bit7zLibrary): The 7z library instance. |
| 129 | + args (argparse.Namespace): Parsed command-line arguments. |
| 130 | + """ |
| 131 | + reader = BitArchiveReader(lib, args.paths[0]) |
| 132 | + |
| 133 | + if args.password: |
| 134 | + reader.set_password(args.password) |
| 135 | + |
| 136 | + reader.extract_to(args.output) |
| 137 | + sys.stdout.write(f"Successfully extracted to: {args.output}") |
| 138 | + |
| 139 | + |
| 140 | +def list_archive(lib: Bit7zLibrary, args: argparse.Namespace) -> None: |
| 141 | + """ |
| 142 | + List the contents of an archive. |
| 143 | + Args: |
| 144 | + lib (Bit7zLibrary): The 7z library instance. |
| 145 | + args (argparse.Namespace): Parsed command-line arguments. |
| 146 | + """ |
| 147 | + reader = BitArchiveReader(lib, args.paths[0]) |
| 148 | + |
| 149 | + if args.password: |
| 150 | + reader.set_password(args.password) |
| 151 | + |
| 152 | + # Printing archive metadata |
| 153 | + sys.stdout.write( |
| 154 | + f"Archive properties:\n Items count: {reader.items_count()}\n" |
| 155 | + f" Folders count: {reader.folders_count()}\n" |
| 156 | + f" Files count: {reader.files_count()}\n" |
| 157 | + f" Size: {reader.size()}\n" |
| 158 | + f" Packed size: {reader.pack_size()}" |
| 159 | + ) |
| 160 | + |
| 161 | + # Printing the metadata of the archived items |
| 162 | + sys.stdout.write("Archived items") |
| 163 | + for item in reader.items(): |
| 164 | + sys.stdout.write( |
| 165 | + f" Item index: {item.index()}\n" |
| 166 | + f" Name: {item.name()}\n" |
| 167 | + f" Extension: {item.extension()}\n" |
| 168 | + f" Path: {item.path()}\n" |
| 169 | + f" IsDir: {item.is_dir()}\n" |
| 170 | + f" Size: {item.size()}\n" |
| 171 | + f" Packed size: {item.pack_size()}\n" |
| 172 | + f" CRC: {item.crc()}" |
| 173 | + ) |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + main() |
0 commit comments