Skip to content

Commit 8c5e503

Browse files
committed
improve markup_report.py script
1 parent 32a29fb commit 8c5e503

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

markup_report.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
"""
77

88
import json
9+
import os
910
import sys
10-
from argparse import ArgumentParser
1111
from pathlib import Path
1212
from typing import Dict, List, Tuple
1313

1414
from meta_cred import MetaCred
1515
from meta_row import MetaRow, _get_source_gen
1616

1717

18-
def prepare_meta(meta_dir) -> Dict[Tuple[str, int, int, int, int], List[MetaRow]]:
18+
def prepare_meta(meta_dir: Path) -> Dict[Tuple[str, int, int, int, int], List[MetaRow]]:
1919
meta_dict: Dict[Tuple[str, int, int, int, int], List[MetaRow]] = {}
2020

21-
for row in _get_source_gen(Path(meta_dir)):
21+
for row in _get_source_gen(meta_dir):
2222
meta_row = MetaRow(row)
2323
markup_key = (meta_row.FilePath, meta_row.LineStart, meta_row.LineEnd, meta_row.ValueStart, meta_row.ValueEnd)
2424
if meta_list := meta_dict.get(markup_key):
@@ -30,10 +30,10 @@ def prepare_meta(meta_dir) -> Dict[Tuple[str, int, int, int, int], List[MetaRow]
3030
return meta_dict
3131

3232

33-
def main(report_file: str, meta_dir: str):
33+
def markup(report_file: Path, meta_dir: Path):
3434
errors = 0
3535

36-
with open(report_file, "r") as f:
36+
with open(report_file) as f:
3737
creds = json.load(f)
3838

3939
meta_dict = prepare_meta(meta_dir)
@@ -60,19 +60,37 @@ def main(report_file: str, meta_dir: str):
6060
# something was wrong
6161
errors += 1
6262

63-
with open(f"{report_file}", "w") as f:
63+
with open(report_file, mode='w') as f:
6464
json.dump(creds, f, indent=4)
6565

6666
return errors
6767

6868

69-
if __name__ == "__main__":
70-
parser = ArgumentParser(prog="python markup_report.py",
71-
description="Console script for markup report file from credsweeper")
72-
73-
parser.add_argument("load_report", help="Load json report from CredSweeper")
74-
parser.add_argument("meta_dir", help="Markup location", nargs='?', default="meta")
69+
def main(argv) -> int:
70+
meta_dir = Path(__file__).parent / "meta"
71+
reports = []
72+
for n, arg in enumerate(argv[1:]):
73+
arg_path = Path(arg)
74+
if arg_path.is_dir() and arg_path.exists():
75+
print(f"Use specific meta dir: {arg_path}", flush=True)
76+
meta_dir = arg_path
77+
elif arg_path.is_file() and arg_path.exists():
78+
reports.append(arg_path)
79+
else:
80+
print(f"Unrecognized {n + 1} argument: '{arg}'", flush=True)
81+
if not (meta_dir.is_dir() and meta_dir.exists()):
82+
print(f"Not exists meta dir: '{meta_dir.absolute()}'", flush=True)
83+
return 1
84+
error_code = os.EX_OK
85+
for report_file in reports:
86+
try:
87+
if errors := markup(report_file, meta_dir):
88+
print(f"{errors} candidates were not found in {meta_dir} markup", flush=True)
89+
error_code = 1
90+
except Exception as exc:
91+
print(f"{report_file} - failure: {exc}")
92+
error_code = 1
93+
return error_code
7594

76-
_args = parser.parse_args()
77-
exit_code = main(_args.load_report, _args.meta_dir)
78-
sys.exit(exit_code)
95+
if __name__ == "__main__":
96+
sys.exit(main(sys.argv))

0 commit comments

Comments
 (0)