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