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