Skip to content

Commit 11d943e

Browse files
committed
Split main() into smaller manageable parts and use logging
1 parent efb5024 commit 11d943e

File tree

1 file changed

+107
-59
lines changed

1 file changed

+107
-59
lines changed

git_evtag_py.py

Lines changed: 107 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
import argparse
77
import hashlib
8+
import logging
89
import os
910
import re
1011
import subprocess
11-
import sys
1212
import tempfile
1313
import types
1414
from pathlib import Path
1515
from typing import IO, Self
1616

17+
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
18+
1719
GIT_ENV = {
1820
"GIT_CONFIG_GLOBAL": "/dev/null",
1921
"GIT_CONFIG_NOSYSTEM": "1",
@@ -313,9 +315,59 @@ def checksum_repo(
313315
self.checksum_tree(batch_proc, path, tree_id)
314316

315317

316-
def main() -> None:
318+
def verify_tag(repo: Path, tag: str, tag_evtag_csum: str, calc_evtag_csum: str) -> bool:
319+
matched = tag_evtag_csum == calc_evtag_csum
320+
tag_sig = is_tag_signature_valid(repo, tag)
321+
if matched and tag_sig:
322+
logging.info(
323+
"EVTag checksum and the tag signature were successfully "
324+
"verified for the tag '%s'",
325+
tag,
326+
)
327+
return True
328+
if matched and not tag_sig:
329+
logging.error(
330+
"EVTag checksum was verified but failed to verify the "
331+
"tag signature for the tag '%s'",
332+
tag,
333+
)
334+
elif tag_sig and not matched:
335+
logging.error(
336+
"The tag signature for the tag '%s' was verified but failed "
337+
"to verify the EVTag checksum.\n"
338+
"Checksum from the tag message: %s\n"
339+
"Calculated checksum: %s",
340+
tag,
341+
tag_evtag_csum,
342+
calc_evtag_csum,
343+
)
344+
else:
345+
logging.error(
346+
"Failed to verify both the EVTag checksum and the tag signature "
347+
"for the tag '%s'.\n"
348+
"Checksum from the tag message: %s\n"
349+
"Calculated checksum: %s",
350+
tag,
351+
tag_evtag_csum,
352+
calc_evtag_csum,
353+
)
354+
355+
return False
356+
357+
358+
def validate_args(args: argparse.Namespace) -> bool:
359+
if sum(bool(x) for x in (args.verify, args.sign)) > 1:
360+
logging.error("Cannot use '--verify' and '--sign' simultaneously")
361+
return False
362+
if args.rev and (args.verify or args.sign):
363+
logging.error("'--rev' cannot be used with '--verify' or '--sign'")
364+
return False
365+
return True
366+
367+
368+
def parse_args() -> argparse.Namespace:
317369
parser = argparse.ArgumentParser(description="Tree checksum of a git repository")
318-
parser.add_argument("--rev", default="HEAD", help="Git revision (default: HEAD)")
370+
parser.add_argument("--rev", help="Git revision (default: HEAD)")
319371
parser.add_argument(
320372
"--repo", default=".", help="Path to the git repository (default: current dir)"
321373
)
@@ -328,74 +380,70 @@ def main() -> None:
328380
action="store_true",
329381
help="Produce 'Git-EVTag-v0-SHA512' prefixed output",
330382
)
331-
args = parser.parse_args()
383+
return parser.parse_args()
332384

333-
if args.verify and args.sign:
334-
raise Exception("'--verify' and '--sign' cannot be used simultaneously")
335385

336-
checksum = ChecksumProcessor()
386+
def main() -> int:
387+
args = parse_args()
388+
if not validate_args(args):
389+
return 1
390+
337391
repo = Path(args.repo).resolve()
338392
if not is_git_directory(repo):
339-
raise Exception(f"{repo} is not a git repository")
340-
ensure_submodules_init(repo)
341-
processor = GitProcessor(repo, checksum)
342-
343-
args.rev = ensure_git_rev(args.rev, repo)
344-
393+
logging.error("The path is not a git repository: '%s'", repo)
394+
return 1
395+
396+
resolved_commit: str | None = None
397+
in_tag: str | None = None
398+
tag_evtag_csum: str | None = None
399+
if not args.rev:
400+
resolved_commit = ensure_git_rev("HEAD", repo)
401+
if args.rev:
402+
resolved_commit = ensure_git_rev(args.rev, repo)
345403
if args.verify or args.sign:
346-
tag = args.verify or args.sign
347-
args.rev = ensure_git_rev(tag, repo)
348-
349-
if args.verify:
350-
tag_msg_checksum = extract_checksum_from_tag(repo, tag)
404+
in_tag = args.verify or args.sign
405+
resolved_commit = ensure_git_rev(in_tag, repo)
406+
if not in_tag:
407+
logging.error("Failed to get the input tag")
408+
return 1
409+
if args.verify and in_tag:
410+
tag_evtag_csum = extract_checksum_from_tag(repo, in_tag)
411+
if not tag_evtag_csum:
412+
logging.error(
413+
"'--verify' was passed but did not find the EVTag "
414+
"checksum from the tag '%s'",
415+
in_tag,
416+
)
417+
return 1
418+
if not resolved_commit:
419+
logging.error("Failed to calculate the resolved commit from the input")
420+
return 1
351421

422+
checksum = ChecksumProcessor()
423+
ensure_submodules_init(repo)
424+
processor = GitProcessor(repo, checksum)
352425
with GitBatchProcessor(repo) as batch_proc:
353-
processor.checksum_repo(batch_proc, args.rev, repo)
426+
processor.checksum_repo(batch_proc, resolved_commit, repo)
354427

355-
calculated_digest = checksum.get_digest()
428+
calc_evtag_csum = checksum.get_digest()
356429

357430
if not (args.verify or args.sign):
358431
if args.compat:
359-
print(f"Git-EVTag-v0-SHA512: {calculated_digest}") # noqa: T201
432+
print(f"Git-EVTag-v0-SHA512: {calc_evtag_csum}") # noqa: T201
360433
else:
361-
print(f"Git-EVTag-Py-v0-SHA512: {calculated_digest}") # noqa: T201
362-
elif args.sign:
363-
sign_tree_checksum(repo, args.sign, calculated_digest, args.compat)
364-
elif args.verify:
365-
if not tag_msg_checksum:
366-
print( # noqa: T201
367-
"Checksum was not found from tag but '--verify' was passed",
368-
file=sys.stderr,
369-
)
370-
sys.exit(1)
371-
matched = tag_msg_checksum == calculated_digest
372-
tag_sig = is_tag_signature_valid(repo, args.verify)
373-
if matched and tag_sig:
374-
print("Checksum and signature are successfully verified") # noqa: T201
375-
elif matched and not tag_sig:
376-
print("Checksum was verified but not signature", file=sys.stderr) # noqa: T201
377-
sys.exit(1)
378-
elif tag_sig and not matched:
379-
print( # noqa: T201
380-
(
381-
"Signature was verified but not checksum"
382-
f"\nChecksum from tag message {tag_msg_checksum}"
383-
f"\nCalculated checksum of {args.verify} is {calculated_digest}"
384-
),
385-
file=sys.stderr,
386-
)
387-
sys.exit(1)
388-
else:
389-
print( # noqa: T201
390-
(
391-
"Checksums and signature verification failed"
392-
f"\nChecksum from tag message {tag_msg_checksum}"
393-
f"\nCalculated checksum of {args.verify} is {calculated_digest}"
394-
),
395-
file=sys.stderr,
396-
)
397-
sys.exit(1)
434+
print(f"Git-EVTag-Py-v0-SHA512: {calc_evtag_csum}") # noqa: T201
435+
elif args.sign and in_tag:
436+
sign_tree_checksum(repo, in_tag, calc_evtag_csum, args.compat)
437+
elif (
438+
args.verify
439+
and in_tag
440+
and tag_evtag_csum
441+
and not verify_tag(repo, in_tag, tag_evtag_csum, calc_evtag_csum)
442+
):
443+
return 1
444+
445+
return 0
398446

399447

400448
if __name__ == "__main__":
401-
main()
449+
raise SystemExit(main())

0 commit comments

Comments
 (0)