summaryrefslogtreecommitdiff
path: root/src/validators/file-exists
diff options
context:
space:
mode:
Diffstat (limited to 'src/validators/file-exists')
-rwxr-xr-xsrc/validators/file-exists71
1 files changed, 35 insertions, 36 deletions
diff --git a/src/validators/file-exists b/src/validators/file-exists
index e179805ed..5cef6b199 100755
--- a/src/validators/file-exists
+++ b/src/validators/file-exists
@@ -23,40 +23,39 @@ import os
import sys
import argparse
-parser = argparse.ArgumentParser()
-parser.add_argument("-d", "--directory", type=str, help="File must be present in this directory.")
-parser.add_argument("-e", "--error", action="store_true", help="Tread warnings as errors - change exit code to '1'")
-parser.add_argument("file", type=str, help="Path of file to validate")
-args = parser.parse_args()
-
-msg_prefix = "WARNING: "
-if args.error:
- msg_prefix = "ERROR: "
-
-#
-# Always check if the given file exists
-#
-if not os.path.exists(args.file):
- print(msg_prefix + "File '{}' not found".format(args.file))
- if args.error:
- sys.exit(1)
- else:
- sys.exit(0)
-
-#
-# Optional check if the file is under a certain directory path
-#
-if args.directory:
- # remove directory path from path to verify
- rel_filename = args.file.replace(args.directory, '').lstrip('/')
-
- if not os.path.exists(args.directory + '/' + rel_filename):
- print(msg_prefix + "'{}' lies outside of '{}' directory.\n" \
- "It will not get preserved during image upgrade!".format(args.file, args.directory))
- if args.error:
- sys.exit(1)
- else:
- sys.exit(0)
-
-sys.exit(0)
+def exit(strict, message):
+ if strict:
+ sys.exit(f'ERROR: {message}')
+ print(f'WARNING: {message}', file=sys.stderr)
+ sys.exit()
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-d", "--directory", type=str, help="File must be present in this directory.")
+ parser.add_argument("-e", "--error", action="store_true", help="Tread warnings as errors - change exit code to '1'")
+ parser.add_argument("file", type=str, help="Path of file to validate")
+
+ args = parser.parse_args()
+
+ #
+ # Always check if the given file exists
+ #
+ if not os.path.exists(args.file):
+ exit(args.error, f"File '{args.file}' not found")
+
+ #
+ # Optional check if the file is under a certain directory path
+ #
+ if args.directory:
+ # remove directory path from path to verify
+ rel_filename = args.file.replace(args.directory, '').lstrip('/')
+
+ if not os.path.exists(args.directory + '/' + rel_filename):
+ exit(args.error,
+ f"'{args.file}' lies outside of '{args.directory}' directory.\n"
+ "It will not get preserved during image upgrade!"
+ )
+
+ sys.exit()