Fix return codes once again
[nitsi.git] / nitsi.in
CommitLineData
1ed8ca9f 1#!/usr/bin/python3
f72f2f70 2
7741c94d 3import argparse
6632e137 4import logging
1ed8ca9f 5
6632e137 6from nitsi.logger import init_logging
61b44c10 7from nitsi.recipe import RecipeExeption
6632e137 8from nitsi.test import TestException, test
61b44c10 9
1ed8ca9f
JS
10logger = logging.getLogger("nitsi")
11logger.setLevel(logging.DEBUG)
1ed8ca9f
JS
12# create console handler with a higher log level
13ch = logging.StreamHandler()
14ch.setLevel(logging.DEBUG)
15
16formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
1ed8ca9f
JS
17ch.setFormatter(formatter)
18# add the handlers to the logger
1ed8ca9f 19logger.addHandler(ch)
f72f2f70 20
7741c94d 21def main():
f72f2f70
JS
22 parser = argparse.ArgumentParser()
23
24 parser.add_argument("-d", "--directory", dest="dir")
25
75b554ca 26 parser.add_argument("-v", "--version", help="Display version and exit",
4cba2549
JS
27 action="store_true", dest="version")
28
f72f2f70
JS
29 args = parser.parse_args()
30
7741c94d 31 # We just log the version and exit
4cba2549
JS
32 if args.version:
33 logger.info("nitsi version: {}".format("@PACKAGE_VERSION@"))
7741c94d
JS
34 return 0
35
36 # For all other stuff we need logging to a file
37 log_dir = init_logging(args.dir)
38 # We now going to log everything to log_dir/genaral.log
39 fh = logging.FileHandler("{}/general.log".format(log_dir))
40 fh.setLevel(logging.DEBUG)
41 logger.addHandler(fh)
42 logger.debug("We now logging everything to {}/general.log".format(log_dir))
d7036f7b 43
7741c94d
JS
44 # here we run a test
45 try:
46 currenttest = test(args.dir, log_dir)
47 currenttest.read_settings()
48 currenttest.virtual_environ_setup()
49 currenttest.load_recipe()
50 except RecipeExeption as e:
51 logger.exception(e)
2ffa26cb 52 return 2
61b44c10 53
7741c94d
JS
54 try:
55 currenttest.virtual_environ_start()
56 currenttest.run_recipe()
57 except TestException as e:
58 logger.exception(e)
59 return 1
60 except BaseException as e:
61 logger.exception(e)
62 return 3
63 finally:
64 currenttest.virtual_environ_stop()
65
66 return 0
67
68if __name__ == "__main__":
2ffa26cb
JS
69 return_value = main()
70 logger.debug("Return value of main is: {}".format(return_value))
71 exit(return_value)