]>
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) | |
41 | ||
f72f2f70 JS |
42 | args = parser.parse_args() |
43 | ||
68190093 | 44 | # We just log the version and exit |
4cba2549 JS |
45 | if args.version: |
46 | logger.info("nitsi version: {}".format("@PACKAGE_VERSION@")) | |
7741c94d JS |
47 | return 0 |
48 | ||
68190093 JS |
49 | |
50 | # Set the log level | |
51 | # We are doing this after we logged the version | |
52 | # to avoid that the version is not shown because of the log level | |
53 | if args.log_level == "info": | |
54 | logger.setLevel(logging.INFO) | |
55 | elif args.log_level == "debug": | |
56 | logger.setLevel(logging.DEBUG) | |
57 | elif args.log_level == "warning": | |
58 | logger.setLevel(logging.WARNING) | |
59 | elif args.log_level == "error": | |
60 | logger.setLevel(logging.ERROR) | |
61 | ||
dd9bd26a | 62 | # Here we are going to run a test |
1795f5e8 JS |
63 | if args.subparsers_name == "run-test": |
64 | if not (args.dir or args.recipe): | |
65 | logger.error("You need to provide at least a director of a test '-d' or a recipe file '-r'") | |
66 | return 4 | |
67 | ||
68 | if (args.dir and args.recipe) or (args.dir and args.settings): | |
69 | logger.error("You cannot use the options '-d' and '-r' or '-d' and '-s' at the same time") | |
70 | return 4 | |
71 | ||
72 | # For all other stuff we need logging to a file | |
73 | if args.dir: | |
74 | log_dir = init_logging(args.dir) | |
75 | elif args.recipe: | |
76 | log_dir = init_logging(args.recipe) | |
77 | ||
78 | # We now going to log everything to log_dir/genaral.log | |
79 | fh = logging.FileHandler("{}/general.log".format(log_dir)) | |
80 | logger.addHandler(fh) | |
81 | logger.debug("We now logging everything to {}/general.log".format(log_dir)) | |
82 | ||
7aa9213e JS |
83 | # Build up settings |
84 | settings = nitsi.settings.NitsiSettings(priority_list=["cmd", "settings-file", "nitsi-default"]) | |
85 | ||
1795f5e8 | 86 | if args.name: |
7aa9213e | 87 | settings.set_config_value("name", args.name, type="cmd") |
1795f5e8 | 88 | if args.desc: |
7aa9213e | 89 | settings.set_config_value("description", args.desc, type="cmd") |
1795f5e8 | 90 | if args.copy_to: |
7aa9213e | 91 | settings.set_config_value("copy_to", args.copy_to, type="cmd") |
1795f5e8 | 92 | if args.copy_from: |
7aa9213e | 93 | settings.set_config_value("copy_from", nitsi.settings.settings_parse_copy_from(args.copy_from), type="cmd") |
1795f5e8 | 94 | if args.virtual_environ_path: |
7aa9213e JS |
95 | path = args.virtual_environ_path |
96 | if not os.path.isabs(path): | |
97 | path = os.path.abspath(path) | |
98 | settings.set_config_value("virtual_environ_path", path, type="cmd") | |
1795f5e8 JS |
99 | |
100 | # here we run a test | |
101 | try: | |
102 | currenttest = nitsi.test.Test( | |
103 | log_dir, | |
104 | dir=args.dir, | |
105 | recipe_file=args.recipe, | |
106 | settings_file=args.settings, | |
7aa9213e | 107 | settings=settings) |
3dac9881 | 108 | currenttest.virtual_environ_setup_stage_1() |
1795f5e8 | 109 | currenttest.load_recipe() |
3dac9881 | 110 | currenttest.virtual_environ_setup_stage_2() |
1795f5e8 JS |
111 | except RecipeExeption as e: |
112 | logger.exception(e) | |
113 | return 2 | |
114 | ||
115 | try: | |
116 | currenttest.virtual_environ_start() | |
117 | currenttest.run_recipe() | |
118 | except nitsi.test.TestException as e: | |
119 | logger.exception(e) | |
120 | return 1 | |
121 | except BaseException as e: | |
122 | logger.exception(e) | |
123 | return 3 | |
124 | finally: | |
125 | currenttest.virtual_environ_stop() | |
126 | ||
127 | return 0 | |
7741c94d JS |
128 | |
129 | if __name__ == "__main__": | |
2ffa26cb JS |
130 | return_value = main() |
131 | logger.debug("Return value of main is: {}".format(return_value)) | |
132 | exit(return_value) |