Add --dry-run option

This commit is contained in:
Joscha 2020-08-26 13:44:49 +00:00
parent 267a51124f
commit 248b61a203
2 changed files with 33 additions and 11 deletions

View file

@ -73,7 +73,8 @@ def run(args: Any) -> None:
for file_info in config_files:
try:
processor.process_file(file_info.path, file_info.header)
processor.process_file(file_info.path, file_info.header,
dry_run=args.dry_run)
except LessCatastrophicError as e:
logger.error(e)
@ -81,6 +82,9 @@ def run(args: Any) -> None:
"program?", "Ca") == "a":
raise CatastrophicError("Aborted")
if args.dry_run:
return
for path in known_files.find_forgotten_files():
logger.info(style_warning("The file ") + style_path(path)
+ style_warning(" is no longer known"))
@ -92,6 +96,7 @@ def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config-file", type=Path)
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-d", "--dry-run", action="store_true")
parser.add_argument("--export-default-config", type=Path)
args = parser.parse_args()

View file

@ -23,7 +23,8 @@ class Processor:
def process_file(self,
path: Path,
header_path: Optional[Path] = None
header_path: Optional[Path] = None,
dry_run: bool = True
) -> None:
logger.info(f"{style_path(path)}:")
@ -31,11 +32,15 @@ class Processor:
config.filename = path.name
if header_path is None:
self._process_file_without_header(path, config)
self._process_file_without_header(path, config, dry_run)
else:
self._process_file_with_header(path, header_path, config)
self._process_file_with_header(path, header_path, config, dry_run)
def _process_file_without_header(self, path: Path, config: Config) -> None:
def _process_file_without_header(self,
path: Path,
config: Config,
dry_run: bool
) -> None:
logger.debug(f"Processing file {style_path(path)} without header")
try:
@ -54,12 +59,13 @@ class Processor:
style_error("Could not parse header of file ") +
style_path(path) + f": {e}")
self._process_parseable(lines, config, path)
self._process_parseable(lines, config, path, dry_run)
def _process_file_with_header(self,
path: Path,
header_path: Path,
config: Config
config: Config,
dry_run: bool
) -> None:
logger.debug(f"Processing file {style_path(path)} "
f"with header {style_path(header_path)}")
@ -77,7 +83,7 @@ class Processor:
style_path(header_path) + f": {e}")
if config.binary:
self._process_binary(path, config)
self._process_binary(path, config, dry_run)
else:
try:
lines = read_file(path).splitlines()
@ -86,9 +92,13 @@ class Processor:
style_error("Could not load file ") +
style_path(path) + f": {e}")
self._process_parseable(lines, config, path)
self._process_parseable(lines, config, path, dry_run)
def _process_binary(self, path: Path, config: Config) -> None:
def _process_binary(self,
path: Path,
config: Config,
dry_run: bool
) -> None:
logger.debug("Processing as a binary file")
if not config.targets:
@ -102,6 +112,9 @@ class Processor:
logger.info("Skipping this target")
continue
if dry_run:
continue
try:
target.parent.mkdir(parents=True, exist_ok=True)
except IOError as e:
@ -128,7 +141,8 @@ class Processor:
def _process_parseable(self,
lines: List[str],
config: Config,
source: Path
source: Path,
dry_run: bool
) -> None:
if not config.targets:
logger.info(" (no targets)")
@ -163,6 +177,9 @@ class Processor:
style_path(target) + f": {e}")
continue
if dry_run:
continue
try:
target.parent.mkdir(parents=True, exist_ok=True)
except IOError as e: