Fix check if we get correct files for settings and recipe
[nitsi.git] / nitsi.in
CommitLineData
1ed8ca9f 1#!/usr/bin/python3
f72f2f70 2
7741c94d 3import argparse
6632e137 4import logging
1ed8ca9f 5
383a38cd 6from nitsi.logger import init_logging, Log_Formatter
61b44c10 7from nitsi.recipe import RecipeExeption
ee227ea1 8import nitsi.test
61b44c10 9
1ed8ca9f 10logger = logging.getLogger("nitsi")
1ed8ca9f 11
68190093
JS
12# create console handler
13ch = logging.StreamHandler()
383a38cd 14formatter = Log_Formatter()
1ed8ca9f 15ch.setFormatter(formatter)
68190093 16# add the handler to the logger
1ed8ca9f 17logger.addHandler(ch)
f72f2f70 18
7741c94d 19def main():
f72f2f70
JS
20 parser = argparse.ArgumentParser()
21
22 parser.add_argument("-d", "--directory", dest="dir")
23
68190093
JS
24 parser.add_argument( "--log-level", choices=[ "debug", "error", "info", "warning" ], dest="log_level", default="info")
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
68190093 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
68190093
JS
36
37 # Set the log level
38 # We are doing this after we logged the version
39 # to avoid that the version is not shown because of the log level
40 if args.log_level == "info":
41 logger.setLevel(logging.INFO)
42 elif args.log_level == "debug":
43 logger.setLevel(logging.DEBUG)
44 elif args.log_level == "warning":
45 logger.setLevel(logging.WARNING)
46 elif args.log_level == "error":
47 logger.setLevel(logging.ERROR)
48
7741c94d
JS
49 # For all other stuff we need logging to a file
50 log_dir = init_logging(args.dir)
51 # We now going to log everything to log_dir/genaral.log
52 fh = logging.FileHandler("{}/general.log".format(log_dir))
7741c94d
JS
53 logger.addHandler(fh)
54 logger.debug("We now logging everything to {}/general.log".format(log_dir))
d7036f7b 55
7741c94d
JS
56 # here we run a test
57 try:
ab5b8849 58 currenttest = nitsi.test.Test(log_dir, dir=args.dir)
7741c94d
JS
59 currenttest.read_settings()
60 currenttest.virtual_environ_setup()
61 currenttest.load_recipe()
62 except RecipeExeption as e:
63 logger.exception(e)
2ffa26cb 64 return 2
61b44c10 65
7741c94d
JS
66 try:
67 currenttest.virtual_environ_start()
68 currenttest.run_recipe()
ee227ea1 69 except nitsi.test.TestException as e:
7741c94d
JS
70 logger.exception(e)
71 return 1
72 except BaseException as e:
73 logger.exception(e)
74 return 3
75 finally:
76 currenttest.virtual_environ_stop()
77
78 return 0
79
80if __name__ == "__main__":
2ffa26cb
JS
81 return_value = main()
82 logger.debug("Return value of main is: {}".format(return_value))
83 exit(return_value)