Skip to content

Commit

Permalink
Create arguement to display timestamp
Browse files Browse the repository at this point in the history
The --timestamp option will display the date and time of the AIP fixity scan.
  • Loading branch information
Dhwaniartefact committed Jun 14, 2024
1 parent fd80ed0 commit 03ae867
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
31 changes: 25 additions & 6 deletions fixity/fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def parse_arguments():
action="store_true",
help="Force a local fixity check on the Storage Service.",
)
parser.add_argument(
"--timestamp",
action="store_true",
help="Display the timestamp of the AIP fixity scan.",
)
args = parser.parse_args()

validate_arguments(args)
Expand Down Expand Up @@ -99,6 +104,7 @@ def scan(
report_auth=(),
session_id=None,
force_local=False,
timestamp=False,
):
"""
Instruct the storage service to scan a single AIP.
Expand Down Expand Up @@ -135,7 +141,9 @@ def scan(
session_id=session_id,
)
except reporting.ReportServiceException:
utils.pyprint(f"Unable to POST pre-scan report to {report_url}")
utils.pyprint(
f"Unable to POST pre-scan report to {report_url}", display_time=timestamp
)

try:
status, report = storage_service.scan_aip(
Expand All @@ -148,9 +156,11 @@ def scan(
force_local=force_local,
)
report_data = json.loads(report.report)
utils.pyprint(scan_message(aip, status, report_data["message"]))
utils.pyprint(
scan_message(aip, status, report_data["message"]), display_time=timestamp
)
except Exception as e:
utils.pyprint(str(e))
utils.pyprint(str(e), display_time=timestamp)
status = None
if hasattr(e, "report") and e.report:
report = e.report
Expand Down Expand Up @@ -181,7 +191,10 @@ def scan(
aip, report, report_url, report_auth=report_auth, session_id=session_id
)
except reporting.ReportServiceException:
utils.pyprint(f"Unable to POST report for AIP {aip} to remote service")
utils.pyprint(
f"Unable to POST report for AIP {aip} to remote service",
display_time=timestamp,
)

if report:
session.add(report)
Expand All @@ -198,6 +211,7 @@ def scanall(
report_auth=(),
throttle_time=0,
force_local=False,
timestamp=False,
):
"""
Run a fixity scan on every AIP in a storage service instance.
Expand All @@ -208,7 +222,8 @@ def scanall(
:param str report_url: The base URL to a server to which the report will be POSTed after the scan completes. If absent, the report will not be transmitted.
:param report_auth: Authentication for the report_url. Tupel of (user, password) for HTTP auth.
:param int throttle_time: Time to wait between scans.
:param bool force_local: If True, will will request the Storage Service to perform a local fixity check, instead of using the Space's fixity (if available).
: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 timestamp: If True, will display the local time stamp of the AIP fixity scan.
"""
success = True

Expand All @@ -233,19 +248,21 @@ def scanall(
report_auth=report_auth,
session_id=session_id,
force_local=force_local,
timestamp=timestamp,
)
if not scan_success:
success = False
except Exception as e:
utils.pyprint(
f"Internal error encountered while scanning AIP {aip['uuid']} ({type(e).__name__})",
file=sys.stdout,
display_time=timestamp,
)
if throttle_time:
sleep(throttle_time)

if count > 0:
utils.pyprint(f"Successfully scanned {count} AIPs")
utils.pyprint(f"Successfully scanned {count} AIPs", display_time=timestamp)

return success

Expand Down Expand Up @@ -285,6 +302,7 @@ def main():
report_auth=auth,
throttle_time=args.throttle,
force_local=args.force_local,
timestamp=args.timestamp,
)
elif args.command == "scan":
session_id = str(uuid4())
Expand All @@ -298,6 +316,7 @@ def main():
report_auth=auth,
session_id=session_id,
force_local=args.force_local,
timestamp=args.timestamp,
)
else:
return Exception(f'Error: "{args.command}" is not a valid command.')
Expand Down
6 changes: 6 additions & 0 deletions fixity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ 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("display_time"):
message = f"{[format_timestamp(utcnow())]} " + message
print(message, file=kwargs.get("file", sys.stderr))
35 changes: 35 additions & 0 deletions tests/test_fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from fixity import fixity
from fixity import reporting
from fixity import utils
from fixity.models import Report
from fixity.models import Session

Expand Down Expand Up @@ -74,6 +75,40 @@ def test_scan(_get, mock_check_fixity):
]


@mock.patch("requests.get")
def test_scan_if_timestamp_exists(_get, mock_check_fixity, capsys):
_get.side_effect = mock_check_fixity
aip_id = uuid.uuid4()

response = fixity.scan(
aip=str(aip_id),
ss_url=STORAGE_SERVICE_URL,
ss_user=STORAGE_SERVICE_USER,
ss_key=STORAGE_SERVICE_KEY,
session=SESSION,
timestamp=True,
)

assert response is True

captured = capsys.readouterr()
assert captured.out == ""
assert (
captured.err.strip()
== f"['{utils.utcnow().strftime('%Y-%m-%d %H:%M:%S %Z')}'] Fixity scan succeeded for AIP: {aip_id}"
)
assert _get.mock_calls == [
mock.call(
f"{STORAGE_SERVICE_URL}api/v2/file/{aip_id}/",
params={"username": STORAGE_SERVICE_USER, "api_key": STORAGE_SERVICE_KEY},
),
mock.call(
f"{STORAGE_SERVICE_URL}api/v2/file/{aip_id}/check_fixity/",
params={"username": STORAGE_SERVICE_USER, "api_key": STORAGE_SERVICE_KEY},
),
]


@mock.patch("fixity.utils.utcnow")
@mock.patch("requests.get")
@mock.patch(
Expand Down

0 comments on commit 03ae867

Please sign in to comment.