]> git.ipfire.org Git - nitsi.git/blobdiff - nitsi.in
.gitignore: Don't list generated nitsi script as untracked file
[nitsi.git] / nitsi.in
index 2090e0dafd7a928d05588114dcca4c64a7f4ff6c..fc1ce8e0af9f2b0ee5fa44f37fd2989752cacebc 100755 (executable)
--- a/nitsi.in
+++ b/nitsi.in
@@ -1,9 +1,12 @@
 #!/usr/bin/python3
 
-from nitsi.test import test
-from nitsi.logger import init_logging
+import argparse
 import logging
 
+from nitsi.logger import init_logging
+from nitsi.recipe import RecipeExeption
+from nitsi.test import TestException, test
+
 logger = logging.getLogger("nitsi")
 logger.setLevel(logging.DEBUG)
 # create console handler with a higher log level
@@ -15,36 +18,54 @@ ch.setFormatter(formatter)
 # add the handlers 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("-v" "--version", help="Display version and exit",
+    parser.add_argument("-v", "--version", help="Display version and exit",
                     action="store_true", dest="version")
 
     args = parser.parse_args()
 
+    # We just log the version and exit
     if args.version:
         logger.info("nitsi version: {}".format("@PACKAGE_VERSION@"))
-    else:
-        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))
-        fh.setLevel(logging.DEBUG)
-        logger.addHandler(fh)
-        logger.debug("We now logging everything to {}/general.log".format(log_dir))
+        return 0
 
+    # 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))
+    fh.setLevel(logging.DEBUG)
+    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()
-        try:
-            currenttest.virtual_environ_start()
-            currenttest.run_recipe()
-        except BaseException as e:
-            print(e)
-        finally:
-            currenttest.virtual_environ_stop()
\ No newline at end of file
+    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:
+        logger.exception(e)
+        return 3
+    finally:
+        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)