Skip to content

Commit

Permalink
Add the suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhwaniartefact committed Jun 19, 2024
1 parent 03ae867 commit 1333731
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
31 changes: 16 additions & 15 deletions fixity/fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def parse_arguments():
help="Force a local fixity check on the Storage Service.",
)
parser.add_argument(
"--timestamp",
"--timestamps",
action="store_true",
help="Display the timestamp of the AIP fixity scan.",
help="Add a timestamp to the beginning of each line of output.",
)
args = parser.parse_args()

Expand Down Expand Up @@ -104,7 +104,7 @@ def scan(
report_auth=(),
session_id=None,
force_local=False,
timestamp=False,
timestamps=False,
):
"""
Instruct the storage service to scan a single AIP.
Expand All @@ -120,7 +120,8 @@ def scan(
: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 session_id: Identifier for this session, allowing every scan from one run to be identified.
: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 timestamps: If True, will add a timestamp to the beginning of each line of output.
"""

# Ensure the storage service knows about this AIP first;
Expand All @@ -142,7 +143,7 @@ def scan(
)
except reporting.ReportServiceException:
utils.pyprint(
f"Unable to POST pre-scan report to {report_url}", display_time=timestamp
f"Unable to POST pre-scan report to {report_url}", timestamps=timestamps
)

try:
Expand All @@ -157,10 +158,10 @@ def scan(
)
report_data = json.loads(report.report)
utils.pyprint(
scan_message(aip, status, report_data["message"]), display_time=timestamp
scan_message(aip, status, report_data["message"]), timestamps=timestamps
)
except Exception as e:
utils.pyprint(str(e), display_time=timestamp)
utils.pyprint(str(e), timestamps=timestamps)
status = None
if hasattr(e, "report") and e.report:
report = e.report
Expand Down Expand Up @@ -193,7 +194,7 @@ def scan(
except reporting.ReportServiceException:
utils.pyprint(
f"Unable to POST report for AIP {aip} to remote service",
display_time=timestamp,
timestamps=timestamps,
)

if report:
Expand All @@ -211,7 +212,7 @@ def scanall(
report_auth=(),
throttle_time=0,
force_local=False,
timestamp=False,
timestamps=False,
):
"""
Run a fixity scan on every AIP in a storage service instance.
Expand All @@ -223,7 +224,7 @@ def scanall(
: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 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.
:param bool timestamps: If True, will add a timestamp to the beginning of each line of output.
"""
success = True

Expand All @@ -248,21 +249,21 @@ def scanall(
report_auth=report_auth,
session_id=session_id,
force_local=force_local,
timestamp=timestamp,
timestamps=timestamps,
)
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,
timestamps=timestamps,
)
if throttle_time:
sleep(throttle_time)

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

return success

Expand Down Expand Up @@ -302,7 +303,7 @@ def main():
report_auth=auth,
throttle_time=args.throttle,
force_local=args.force_local,
timestamp=args.timestamp,
timestamps=args.timestamps,
)
elif args.command == "scan":
session_id = str(uuid4())
Expand All @@ -316,7 +317,7 @@ def main():
report_auth=auth,
session_id=session_id,
force_local=args.force_local,
timestamp=args.timestamp,
timestamps=args.timestamps,
)
else:
return Exception(f'Error: "{args.command}" is not a valid command.')
Expand Down
4 changes: 2 additions & 2 deletions fixity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def format_timestamp(t):


def pyprint(message, **kwargs):
if kwargs.get("display_time"):
message = f"{[format_timestamp(utcnow())]} " + message
if kwargs.get("timestamps"):
message = f"{[format_timestamp(utcnow())]} {message}"
print(message, file=kwargs.get("file", sys.stderr))
10 changes: 6 additions & 4 deletions tests/test_fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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 @@ -75,18 +74,21 @@ def test_scan(_get, mock_check_fixity):
]


@mock.patch("fixity.utils.utcnow")
@mock.patch("requests.get")
def test_scan_if_timestamp_exists(_get, mock_check_fixity, capsys):
def test_scan_if_timestamps_is_True(_get, utcnow, mock_check_fixity, capsys):
_get.side_effect = mock_check_fixity
aip_id = uuid.uuid4()
timestamp = 1514775600
utcnow.return_value = datetime.fromtimestamp(timestamp, timezone.utc)

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,
timestamps=True,
)

assert response is True
Expand All @@ -95,7 +97,7 @@ def test_scan_if_timestamp_exists(_get, mock_check_fixity, capsys):
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}"
== f"['2018-01-01 03:00:00 UTC'] Fixity scan succeeded for AIP: {aip_id}"
)
assert _get.mock_calls == [
mock.call(
Expand Down

0 comments on commit 1333731

Please sign in to comment.