From 017cb596535230b0940811e2478a2188af8034a3 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Wed, 11 Dec 2024 15:58:12 +0100 Subject: [PATCH] TEMPORARY COMMIT - DO NOT MERGE --- fimfarchive/commands/__init__.py | 2 ++ fimfarchive/commands/check.py | 62 ++++++++++++++++++++++++++++++++ fimfarchive/commands/root.py | 2 ++ 3 files changed, 66 insertions(+) create mode 100644 fimfarchive/commands/check.py diff --git a/fimfarchive/commands/__init__.py b/fimfarchive/commands/__init__.py index 47e813d..2bdc9b4 100644 --- a/fimfarchive/commands/__init__.py +++ b/fimfarchive/commands/__init__.py @@ -24,6 +24,7 @@ from .base import Command from .build import BuildCommand +from .check import CheckCommand from .root import RootCommand from .update import UpdateCommand @@ -32,5 +33,6 @@ 'Command', 'RootCommand', 'BuildCommand', + 'CheckCommand', 'UpdateCommand', ) diff --git a/fimfarchive/commands/check.py b/fimfarchive/commands/check.py new file mode 100644 index 0000000..a69f713 --- /dev/null +++ b/fimfarchive/commands/check.py @@ -0,0 +1,62 @@ +""" +Check command. +""" + + +# +# Fimfarchive, preserves stories from Fimfiction. +# Copyright (C) 2024 Joakim Soderlund +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + + +from argparse import ArgumentParser +from io import BytesIO +from zipfile import ZipFile + +from fimfarchive.fetchers import FimfarchiveFetcher +from fimfarchive.utils import tqdm + +from .base import Command + + +class CheckCommand(Command): + """ + Checks archive and story integrity. + """ + + @property + def parser(self) -> ArgumentParser: + """ + Returns a command line arguments parser. + """ + parser = ArgumentParser(prog='', description=self.__doc__) + parser.add_argument('archive', help="file to check") + + return parser + + def __call__(self, *args: str) -> int: + opts = self.parser.parse_args(args) + fetcher = FimfarchiveFetcher(opts.archive) + + if fetcher.archive.testzip() is not None: + exit("Invalid CRC: Archive") + + for story in tqdm(fetcher, leave=True): + with ZipFile(BytesIO(story.data)) as zobj: + if zobj.testzip() is not None: + exit(f"Invalid CRC: {story.key}") + + return 0 diff --git a/fimfarchive/commands/root.py b/fimfarchive/commands/root.py index cf92524..74efa8b 100644 --- a/fimfarchive/commands/root.py +++ b/fimfarchive/commands/root.py @@ -26,6 +26,7 @@ from .base import Command from .build import BuildCommand +from .check import CheckCommand from .update import UpdateCommand @@ -40,6 +41,7 @@ class RootCommand(Command): """ commands: Dict[str, Type[Command]] = { 'build': BuildCommand, + 'check': CheckCommand, 'update': UpdateCommand, }