Add --dry-run option
This commit is contained in:
parent
267a51124f
commit
248b61a203
2 changed files with 33 additions and 11 deletions
|
|
@ -73,7 +73,8 @@ def run(args: Any) -> None:
|
||||||
|
|
||||||
for file_info in config_files:
|
for file_info in config_files:
|
||||||
try:
|
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:
|
except LessCatastrophicError as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
|
|
||||||
|
|
@ -81,6 +82,9 @@ def run(args: Any) -> None:
|
||||||
"program?", "Ca") == "a":
|
"program?", "Ca") == "a":
|
||||||
raise CatastrophicError("Aborted")
|
raise CatastrophicError("Aborted")
|
||||||
|
|
||||||
|
if args.dry_run:
|
||||||
|
return
|
||||||
|
|
||||||
for path in known_files.find_forgotten_files():
|
for path in known_files.find_forgotten_files():
|
||||||
logger.info(style_warning("The file ") + style_path(path)
|
logger.info(style_warning("The file ") + style_path(path)
|
||||||
+ style_warning(" is no longer known"))
|
+ style_warning(" is no longer known"))
|
||||||
|
|
@ -92,6 +96,7 @@ def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-c", "--config-file", type=Path)
|
parser.add_argument("-c", "--config-file", type=Path)
|
||||||
parser.add_argument("-v", "--verbose", action="store_true")
|
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)
|
parser.add_argument("--export-default-config", type=Path)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,8 @@ class Processor:
|
||||||
|
|
||||||
def process_file(self,
|
def process_file(self,
|
||||||
path: Path,
|
path: Path,
|
||||||
header_path: Optional[Path] = None
|
header_path: Optional[Path] = None,
|
||||||
|
dry_run: bool = True
|
||||||
) -> None:
|
) -> None:
|
||||||
logger.info(f"{style_path(path)}:")
|
logger.info(f"{style_path(path)}:")
|
||||||
|
|
||||||
|
|
@ -31,11 +32,15 @@ class Processor:
|
||||||
config.filename = path.name
|
config.filename = path.name
|
||||||
|
|
||||||
if header_path is None:
|
if header_path is None:
|
||||||
self._process_file_without_header(path, config)
|
self._process_file_without_header(path, config, dry_run)
|
||||||
else:
|
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")
|
logger.debug(f"Processing file {style_path(path)} without header")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -54,12 +59,13 @@ class Processor:
|
||||||
style_error("Could not parse header of file ") +
|
style_error("Could not parse header of file ") +
|
||||||
style_path(path) + f": {e}")
|
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,
|
def _process_file_with_header(self,
|
||||||
path: Path,
|
path: Path,
|
||||||
header_path: Path,
|
header_path: Path,
|
||||||
config: Config
|
config: Config,
|
||||||
|
dry_run: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
logger.debug(f"Processing file {style_path(path)} "
|
logger.debug(f"Processing file {style_path(path)} "
|
||||||
f"with header {style_path(header_path)}")
|
f"with header {style_path(header_path)}")
|
||||||
|
|
@ -77,7 +83,7 @@ class Processor:
|
||||||
style_path(header_path) + f": {e}")
|
style_path(header_path) + f": {e}")
|
||||||
|
|
||||||
if config.binary:
|
if config.binary:
|
||||||
self._process_binary(path, config)
|
self._process_binary(path, config, dry_run)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
lines = read_file(path).splitlines()
|
lines = read_file(path).splitlines()
|
||||||
|
|
@ -86,9 +92,13 @@ class Processor:
|
||||||
style_error("Could not load file ") +
|
style_error("Could not load file ") +
|
||||||
style_path(path) + f": {e}")
|
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")
|
logger.debug("Processing as a binary file")
|
||||||
|
|
||||||
if not config.targets:
|
if not config.targets:
|
||||||
|
|
@ -102,6 +112,9 @@ class Processor:
|
||||||
logger.info("Skipping this target")
|
logger.info("Skipping this target")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
target.parent.mkdir(parents=True, exist_ok=True)
|
target.parent.mkdir(parents=True, exist_ok=True)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
|
|
@ -128,7 +141,8 @@ class Processor:
|
||||||
def _process_parseable(self,
|
def _process_parseable(self,
|
||||||
lines: List[str],
|
lines: List[str],
|
||||||
config: Config,
|
config: Config,
|
||||||
source: Path
|
source: Path,
|
||||||
|
dry_run: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
if not config.targets:
|
if not config.targets:
|
||||||
logger.info(" (no targets)")
|
logger.info(" (no targets)")
|
||||||
|
|
@ -163,6 +177,9 @@ class Processor:
|
||||||
style_path(target) + f": {e}")
|
style_path(target) + f": {e}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if dry_run:
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
target.parent.mkdir(parents=True, exist_ok=True)
|
target.parent.mkdir(parents=True, exist_ok=True)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue