]> git.ipfire.org Git - nitsi.git/blobdiff - nitsi.in
Merge branch 'master' of ssh://git.ipfire.org/pub/git/people/jschlag/nitsi
[nitsi.git] / nitsi.in
index a87aa0a82d407f9c56dd9b94fbe7157f33522e1b..55c7a7cefe297f707428586e149e58623c20508c 100755 (executable)
--- a/nitsi.in
+++ b/nitsi.in
@@ -1,41 +1,83 @@
 #!/usr/bin/python3
 
-from nitsi.test import test
+import argparse
 import logging
 
+from nitsi.logger import init_logging, Log_Formatter
+from nitsi.recipe import RecipeExeption
+from nitsi.test import TestException, test
+
 logger = logging.getLogger("nitsi")
-logger.setLevel(logging.DEBUG)
-# create file handler which logs even debug messages
-fh = logging.FileHandler('nitsi.log')
-fh.setLevel(logging.DEBUG)
-# create console handler with a higher log level
-ch = logging.StreamHandler()
-ch.setLevel(logging.DEBUG)
 
-formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
-fh.setFormatter(formatter)
+# create console handler
+ch = logging.StreamHandler()
+formatter = Log_Formatter()
 ch.setFormatter(formatter)
-# add the handlers to the logger
-logger.addHandler(fh)
+# add the handler to the logger
 logger.addHandler(ch)
 
-if __name__ == "__main__":
-    import argparse
-
+def main():
     parser = argparse.ArgumentParser()
 
     parser.add_argument("-d", "--directory", dest="dir")
 
+    parser.add_argument( "--log-level", choices=[ "debug", "error", "info", "warning" ], dest="log_level", default="info")
+
+    parser.add_argument("-v", "--version", help="Display version and exit",
+                    action="store_true", dest="version")
+
     args = parser.parse_args()
 
-    currenttest = test(args.dir)
-    currenttest.read_settings()
-    currenttest.virtual_environ_setup()
-    currenttest.load_recipe()
+  # We just log the version and exit
+    if args.version:
+        logger.info("nitsi version: {}".format("@PACKAGE_VERSION@"))
+        return 0
+
+
+    # Set the log level
+    # We are doing this after we logged the version
+    # to avoid that the version is not shown because of the log level
+    if args.log_level == "info":
+        logger.setLevel(logging.INFO)
+    elif args.log_level == "debug":
+        logger.setLevel(logging.DEBUG)
+    elif args.log_level == "warning":
+        logger.setLevel(logging.WARNING)
+    elif args.log_level == "error":
+        logger.setLevel(logging.ERROR)
+
+    # For all other stuff we need logging to a file
+    log_dir = init_logging(args.dir)
+    # We now going to log everything to log_dir/genaral.log
+    fh = logging.FileHandler("{}/general.log".format(log_dir))
+    logger.addHandler(fh)
+    logger.debug("We now logging everything to {}/general.log".format(log_dir))
+
+    # here we run a test
+    try:
+        currenttest = test(args.dir, log_dir)
+        currenttest.read_settings()
+        currenttest.virtual_environ_setup()
+        currenttest.load_recipe()
+    except RecipeExeption as e:
+        logger.exception(e)
+        return 2
+
     try:
         currenttest.virtual_environ_start()
         currenttest.run_recipe()
+    except TestException as e:
+        logger.exception(e)
+        return 1
     except BaseException as e:
-        print(e)
+        logger.exception(e)
+        return 3
     finally:
-        currenttest.virtual_environ_stop()
\ No newline at end of file
+        currenttest.virtual_environ_stop()
+
+    return 0
+
+if __name__ == "__main__":
+   return_value = main()
+   logger.debug("Return value of main is: {}".format(return_value))
+   exit(return_value)