]>
Commit | Line | Data |
---|---|---|
1ed8ca9f | 1 | #!/usr/bin/python3 |
f72f2f70 | 2 | |
7741c94d | 3 | import argparse |
6632e137 | 4 | import logging |
7aa9213e | 5 | import os |
1ed8ca9f | 6 | |
383a38cd | 7 | from nitsi.logger import init_logging, Log_Formatter |
61b44c10 | 8 | from nitsi.recipe import RecipeExeption |
ee227ea1 | 9 | import nitsi.test |
7aa9213e | 10 | from nitsi.settings import settings_parse_copy_from, NitsiSettings |
61b44c10 | 11 | |
1ed8ca9f | 12 | logger = logging.getLogger("nitsi") |
1ed8ca9f | 13 | |
68190093 JS |
14 | # create console handler |
15 | ch = logging.StreamHandler() | |
383a38cd | 16 | formatter = Log_Formatter() |
1ed8ca9f | 17 | ch.setFormatter(formatter) |
68190093 | 18 | # add the handler to the logger |
1ed8ca9f | 19 | logger.addHandler(ch) |
f72f2f70 | 20 | |
7741c94d | 21 | def main(): |
f72f2f70 JS |
22 | parser = argparse.ArgumentParser() |
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 | ||
1795f5e8 JS |
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) | |
bee8ead7 | 41 | parser_run_test.add_argument("--interactive-error-handling", dest="interactive_error_handling", default=False, action="store_true") |
1795f5e8 | 42 | |
f72f2f70 JS |
43 | args = parser.parse_args() |
44 | ||
68190093 | 45 | # We just log the version and exit |
4cba2549 JS |
46 | if args.version: |
47 | logger.info("nitsi version: {}".format("@PACKAGE_VERSION@")) | |
7741c94d JS |
48 | return 0 |
49 | ||
68190093 JS |
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 | ||
dd9bd26a | 63 | # Here we are going to run a test |
1795f5e8 JS |
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 | ||
7aa9213e JS |
84 | # Build up settings |
85 | settings = nitsi.settings.NitsiSettings(priority_list=["cmd", "settings-file", "nitsi-default"]) | |
86 | ||
1795f5e8 | 87 | if args.name: |
7aa9213e | 88 | settings.set_config_value("name", args.name, type="cmd") |
1795f5e8 | 89 | if args.desc: |
7aa9213e | 90 | settings.set_config_value("description", args.desc, type="cmd") |
1795f5e8 | 91 | if args.copy_to: |
7aa9213e | 92 | settings.set_config_value("copy_to", args.copy_to, type="cmd") |
1795f5e8 | 93 | if args.copy_from: |
7aa9213e | 94 | settings.set_config_value("copy_from", nitsi.settings.settings_parse_copy_from(args.copy_from), type="cmd") |
1795f5e8 | 95 | if args.virtual_environ_path: |
7aa9213e JS |
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") | |
bee8ead7 JS |
100 | if args.interactive_error_handling: |
101 | settings.set_config_value("interactive_error_handling", True, type="cmd") | |
1795f5e8 JS |
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, | |
7aa9213e | 110 | settings=settings) |
3dac9881 | 111 | currenttest.virtual_environ_setup_stage_1() |
1795f5e8 | 112 | currenttest.load_recipe() |
3dac9881 | 113 | currenttest.virtual_environ_setup_stage_2() |
1795f5e8 JS |
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 | |
7741c94d JS |
131 | |
132 | if __name__ == "__main__": | |
2ffa26cb JS |
133 | return_value = main() |
134 | logger.debug("Return value of main is: {}".format(return_value)) | |
135 | exit(return_value) |