Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger in fixity script #49

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions fixity/fixity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json
import logging
import os
import sys
import traceback
from argparse import ArgumentParser
from datetime import datetime
from datetime import timezone
from time import sleep
from uuid import uuid4

Expand All @@ -22,7 +25,7 @@
raise ArgumentError("An AIP UUID must be specified when scanning a single AIP")


def parse_arguments():
def parse_arguments(argument):
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
parser = ArgumentParser()
parser.add_argument("command", choices=["scan", "scanall"], help="Command to run.")
parser.add_argument("aip", nargs="?", help="If 'scan', UUID of the AIP to scan")
Expand All @@ -45,7 +48,7 @@
action="store_true",
help="Add a timestamp to the beginning of each line of output.",
)
args = parser.parse_args()
args = parser.parse_args(argument)
replaceafill marked this conversation as resolved.
Show resolved Hide resolved

validate_arguments(args)
return args
Expand Down Expand Up @@ -100,6 +103,7 @@
ss_user,
ss_key,
session,
logger,
report_url=None,
report_auth=(),
session_id=None,
Expand All @@ -123,7 +127,6 @@
:param bool force_local: If True, will request the Storage Service to perform a local fixity check, instead of using the Space's fixity (if available).
:param bool timestamps: If True, will add a timestamp to the beginning of each line of output.
"""

# Ensure the storage service knows about this AIP first;
# get_single_aip() will raise an exception if the storage service
# does not have an AIP with that UUID, or otherwise errors out
Expand All @@ -142,10 +145,7 @@
session_id=session_id,
)
except reporting.ReportServiceException:
utils.pyprint(
f"Unable to POST pre-scan report to {report_url}", timestamps=timestamps
)

logger.log(logging.WARNING, f"Unable to POST pre-scan report to {report_url}")
try:
status, report = storage_service.scan_aip(
aip,
Expand All @@ -157,11 +157,10 @@
force_local=force_local,
)
report_data = json.loads(report.report)
utils.pyprint(
scan_message(aip, status, report_data["message"]), timestamps=timestamps
)
logger.log(logging.WARNING, scan_message(aip, status, report_data["message"]))
except Exception as e:
utils.pyprint(str(e), timestamps=timestamps)
logger.log(logging.WARNING, str(e))

status = None
if hasattr(e, "report") and e.report:
report = e.report
Expand Down Expand Up @@ -190,11 +189,10 @@
aip, report, report_url, report_auth=report_auth, session_id=session_id
)
except reporting.ReportServiceException:
utils.pyprint(
logger.log(
logging.WARNING,
f"Unable to POST report for AIP {aip} to remote service",
timestamps=timestamps,
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
)

if report:
session.add(report)

Expand All @@ -206,6 +204,7 @@
ss_user,
ss_key,
session,
logger,
report_url=None,
report_auth=(),
throttle_time=0,
Expand Down Expand Up @@ -243,6 +242,7 @@
ss_user,
ss_key,
session,
logger,
report_url=report_url,
report_auth=report_auth,
session_id=session_id,
Expand All @@ -252,24 +252,23 @@
if not scan_success:
success = False
except Exception as e:
utils.pyprint(
logger.log(
logging.WARNING,
f"Internal error encountered while scanning AIP {aip['uuid']} ({type(e).__name__})",
timestamps=timestamps,
)
if throttle_time:
sleep(throttle_time)

if count > 0:
utils.pyprint(f"Successfully scanned {count} AIPs", timestamps=timestamps)
replaceafill marked this conversation as resolved.
Show resolved Hide resolved

logger.log(logging.WARNING, f"Successfully scanned {count} AIPs")
return success


def main():
def main(arguements=None):
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
success = 0

try:
args = parse_arguments()
args = parse_arguments(arguements)
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
except ArgumentError as e:
return e

Expand All @@ -280,6 +279,25 @@

session = Session()

logger = logging.getLogger(__name__)

# create console handler which logs stderr messages
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
stderr_handler = logging.StreamHandler(stream=sys.stderr)

# create formatter and add it to the handlers
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
if args.timestamps:
format_time = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S %Z")
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
message_format = logging.Formatter(
"[%(asctime)s] %(message)s",
datefmt=format_time,
)
else:
message_format = logging.Formatter("%(message)s")
stderr_handler.setFormatter(message_format)

# add the handlers to the logger
logger.addHandler(stderr_handler)

status = False

if args.report_user and args.report_pass:
Expand All @@ -296,6 +314,7 @@
args.ss_user,
args.ss_key,
session,
logger,
report_url=report_url,
report_auth=auth,
throttle_time=args.throttle,
Expand All @@ -310,6 +329,7 @@
args.ss_user,
args.ss_key,
session,
logger,
report_url=report_url,
report_auth=auth,
session_id=session_id,
Expand Down Expand Up @@ -339,4 +359,4 @@


if __name__ == "__main__":
sys.exit(main())
sys.exit(main(sys.argv[1:]))

Check warning on line 362 in fixity/fixity.py

View check run for this annotation

Codecov / codecov/patch

fixity/fixity.py#L362

Added line #L362 was not covered by tests
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 0 additions & 11 deletions fixity/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from datetime import datetime
from datetime import timezone
from uuid import UUID
Expand Down Expand Up @@ -34,13 +33,3 @@ def check_valid_uuid(uuid):

def utcnow():
return datetime.now(timezone.utc)


def format_timestamp(t):
return t.strftime("%Y-%m-%d %H:%M:%S %Z")


def pyprint(message, **kwargs):
if kwargs.get("timestamps"):
message = f"[{format_timestamp(utcnow())}] {message}"
print(message, file=sys.stderr)
Loading