Improve code readability and log messages
[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
1795f5e8 9import nitsi.settings
61b44c10 10
1ed8ca9f 11logger = logging.getLogger("nitsi")
1ed8ca9f 12
68190093
JS
13# create console handler
14ch = logging.StreamHandler()
383a38cd 15formatter = Log_Formatter()
1ed8ca9f 16ch.setFormatter(formatter)
68190093 17# add the handler to the logger
1ed8ca9f 18logger.addHandler(ch)
f72f2f70 19
7741c94d 20def main():
f72f2f70
JS
21 parser = argparse.ArgumentParser()
22
68190093
JS
23 parser.add_argument( "--log-level", choices=[ "debug", "error", "info", "warning" ], dest="log_level", default="info")
24
75b554ca 25 parser.add_argument("-v", "--version", help="Display version and exit",
4cba2549
JS
26 action="store_true", dest="version")
27
1795f5e8
JS
28 # Subparser for different commands
29 subparsers = parser.add_subparsers(dest="subparsers_name")
30
31 parser_run_test = subparsers.add_parser('run-test', help='run-test help')
32 parser_run_test.add_argument("-d", "--directory", dest="dir", default=None)
33 parser_run_test.add_argument("-r", "--recipe", dest="recipe", default=None)
34 parser_run_test.add_argument("-s", "--settings", dest="settings", default=None)
35 parser_run_test.add_argument("--name", dest="name", default=None)
36 parser_run_test.add_argument("--description", dest="desc", default=None)
37 parser_run_test.add_argument("--copy-to", dest="copy_to", default=None)
38 parser_run_test.add_argument("--copy-from", dest="copy_from", default=None, nargs='+')
39 parser_run_test.add_argument("--virtual-environment", dest="virtual_environ_path", default=None)
40
f72f2f70
JS
41 args = parser.parse_args()
42
68190093 43 # We just log the version and exit
4cba2549
JS
44 if args.version:
45 logger.info("nitsi version: {}".format("@PACKAGE_VERSION@"))
7741c94d
JS
46 return 0
47
68190093
JS
48
49 # Set the log level
50 # We are doing this after we logged the version
51 # to avoid that the version is not shown because of the log level
52 if args.log_level == "info":
53 logger.setLevel(logging.INFO)
54 elif args.log_level == "debug":
55 logger.setLevel(logging.DEBUG)
56 elif args.log_level == "warning":
57 logger.setLevel(logging.WARNING)
58 elif args.log_level == "error":
59 logger.setLevel(logging.ERROR)
60
1795f5e8
JS
61
62 if args.subparsers_name == "run-test":
63 if not (args.dir or args.recipe):
64 logger.error("You need to provide at least a director of a test '-d' or a recipe file '-r'")
65 return 4
66
67 if (args.dir and args.recipe) or (args.dir and args.settings):
68 logger.error("You cannot use the options '-d' and '-r' or '-d' and '-s' at the same time")
69 return 4
70
71 # For all other stuff we need logging to a file
72 if args.dir:
73 log_dir = init_logging(args.dir)
74 elif args.recipe:
75 log_dir = init_logging(args.recipe)
76
77 # We now going to log everything to log_dir/genaral.log
78 fh = logging.FileHandler("{}/general.log".format(log_dir))
79 logger.addHandler(fh)
80 logger.debug("We now logging everything to {}/general.log".format(log_dir))
81
82 # Build up settings dict
83 settings = {}
84 if args.name:
85 settings["name"] = args.name
86 if args.desc:
87 settings["description"] = args.desc
88 if args.copy_to:
89 settings["copy_to"] = args.copy_to
90 if args.copy_from:
91 settings["copy_from"] = nitsi.settings.settings_parse_copy_from(args.copy_from)
92 if args.virtual_environ_path:
93 settings["virtual_environ_path"] = args.virtual_environ_path
94
95 # here we run a test
96 try:
97 currenttest = nitsi.test.Test(
98 log_dir,
99 dir=args.dir,
100 recipe_file=args.recipe,
101 settings_file=args.settings,
102 cmd_settings=settings)
103 currenttest.read_settings()
1795f5e8 104 currenttest.load_recipe()
fbaed8c3 105 currenttest.virtual_environ_setup()
1795f5e8
JS
106 except RecipeExeption as e:
107 logger.exception(e)
108 return 2
109
110 try:
111 currenttest.virtual_environ_start()
112 currenttest.run_recipe()
113 except nitsi.test.TestException as e:
114 logger.exception(e)
115 return 1
116 except BaseException as e:
117 logger.exception(e)
118 return 3
119 finally:
120 currenttest.virtual_environ_stop()
121
122 return 0
7741c94d
JS
123
124if __name__ == "__main__":
2ffa26cb
JS
125 return_value = main()
126 logger.debug("Return value of main is: {}".format(return_value))
127 exit(return_value)