Skip to content
Snippets Groups Projects
Commit 78cd06a8 authored by nimrod's avatar nimrod
Browse files

Address pre-commit issues.

parent 300f1b30
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ repos:
rev: v0.14.3
hooks:
- id: detect-secrets
exclude: Pipfile\.lock
- repo: https://github.com/adrienverge/yamllint
rev: v1.25.0
......
recursive-include check_s3_bucket *.py
exclude .pre-commit-config.yaml
exclude .gitlab-ci.yml
include *.rst
include *.txt
exclude .dockerignore
......
......@@ -9,18 +9,19 @@ from __future__ import (
)
import argparse
import datetime
import sys
try:
import botocore.session
import botocore.exceptions
except ImportError:
print("Failed to import botocore.")
exit(3)
sys.exit(3)
try:
import pytz
except ImportError:
print("Failed to import pytz.")
exit(3)
sys.exit(3)
__version__ = "0.2.4"
NOW = datetime.datetime.now(pytz.utc)
......@@ -44,7 +45,7 @@ def get_file_list(conn, bucket, prefix=""):
return files
def main():
def main(): # noqa: C901
"""Main entrypoint."""
# Parse command line arguments.
......@@ -79,8 +80,8 @@ def main():
)
parser.add_argument(
"size_critical_threshold",
help="""Critical threshold for the difference in size between the latest
2 files in percents (default to 50)""",
help="""Critical threshold for the difference in size between the
latest 2 files in percents (default to 50)""",
default=50,
type=int,
nargs="?",
......@@ -96,16 +97,16 @@ def main():
except botocore.exceptions.BotoCoreError as exception:
print("Failed to list the files in the S3 bucket.")
print(str(exception))
exit(3)
sys.exit(3)
if not files:
print("No matching files in bucket.")
exit(2)
sys.exit(2)
# Calculate the age of the latest file and if it's in the thresholds set.
if files[0]["LastModified"] > NOW:
print("Latest file is from the future, something is wrong.")
exit(3)
sys.exit(3)
timedelta = files[0]["HoursSinceLastModified"]
if timedelta > args.age_critical_threshold:
print(
......@@ -113,29 +114,29 @@ def main():
args.age_critical_threshold
)
)
exit(2)
sys.exit(2)
elif timedelta > args.age_warning_threshold:
print(
"Last file modified is older than {} hours.".format(
args.age_warning_threshold
)
)
exit(1)
sys.exit(1)
# Calculate the size ratio between the latest 2 files and check if
# it's in the threshold set.
if files[0]["Size"] == 0:
print("Latest file is empty.")
exit(2)
sys.exit(2)
elif len(files) == 1:
print(
"""Found only 1 file in the bucket, can't calculate size
difference."""
)
exit(3)
sys.exit(3)
elif files[1]["Size"] == 0:
print("The last but 1 file is empty, can't calculate size difference.")
exit(3)
sys.exit(3)
size_ratio = 100 * abs(
(files[1]["Size"] - files[0]["Size"]) / files[1]["Size"]
......@@ -146,14 +147,14 @@ def main():
size_ratio
)
)
exit(2)
sys.exit(2)
if size_ratio > args.size_warning_threshold:
print(
"The size difference between the latest 2 file is {}%.".format(
size_ratio
)
)
exit(1)
sys.exit(1)
else:
print("File found and is within the thresholds set.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment