Mercurial > piecrust2
comparison piecrust/main.py @ 384:d241585412ad
internal: Make it possible to pass `argv` to the main Chef function.
This makes it easy to write system tests against the CLI.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 11 May 2015 22:24:05 -0700 |
parents | c2ca72fb7f0b |
children | 456db44dcc53 |
comparison
equal
deleted
inserted
replaced
383:44cf6ce62467 | 384:d241585412ad |
---|---|
58 try: | 58 try: |
59 locale.getdefaultlocale() | 59 locale.getdefaultlocale() |
60 except ValueError: | 60 except ValueError: |
61 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') | 61 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') |
62 | 62 |
63 argv = sys.argv | 63 argv = sys.argv[1:] |
64 pre_args = _pre_parse_chef_args(argv) | 64 pre_args = _pre_parse_chef_args(argv) |
65 try: | 65 try: |
66 exit_code = _run_chef(pre_args) | 66 exit_code = _run_chef(pre_args, argv) |
67 except Exception as ex: | 67 except Exception as ex: |
68 if pre_args.debug: | 68 if pre_args.debug: |
69 logger.exception(ex) | 69 logger.exception(ex) |
70 else: | 70 else: |
71 log_friendly_exception(logger, ex) | 71 log_friendly_exception(logger, ex) |
98 # We need to parse some arguments before we can build the actual argument | 98 # We need to parse some arguments before we can build the actual argument |
99 # parser, because it can affect which plugins will be loaded. Also, log- | 99 # parser, because it can affect which plugins will be loaded. Also, log- |
100 # related arguments must be parsed first because we want to log everything | 100 # related arguments must be parsed first because we want to log everything |
101 # from the beginning. | 101 # from the beginning. |
102 res = PreParsedChefArgs() | 102 res = PreParsedChefArgs() |
103 i = 1 | 103 i = 0 |
104 while i < len(argv): | 104 while i < len(argv): |
105 arg = argv[i] | 105 arg = argv[i] |
106 if arg.startswith('--root='): | 106 if arg.startswith('--root='): |
107 res.root = os.path.expanduser(arg[len('--root='):]) | 107 res.root = os.path.expanduser(arg[len('--root='):]) |
108 elif arg == '--root': | 108 elif arg == '--root': |
164 file_handler.setLevel(logging.DEBUG) | 164 file_handler.setLevel(logging.DEBUG) |
165 | 165 |
166 return res | 166 return res |
167 | 167 |
168 | 168 |
169 def _run_chef(pre_args): | 169 def _run_chef(pre_args, argv): |
170 # Setup the app. | 170 # Setup the app. |
171 start_time = time.clock() | 171 start_time = time.clock() |
172 root = pre_args.root | 172 root = pre_args.root |
173 if root is None: | 173 if root is None: |
174 try: | 174 try: |
248 for name, desc in help_cmd.getTopics(): | 248 for name, desc in help_cmd.getTopics(): |
249 print_help_item(epilog, name, desc) | 249 print_help_item(epilog, name, desc) |
250 parser.epilog = epilog.getvalue() | 250 parser.epilog = epilog.getvalue() |
251 | 251 |
252 # Parse the command line. | 252 # Parse the command line. |
253 result = parser.parse_args() | 253 result = parser.parse_args(argv) |
254 logger.debug(format_timed(start_time, 'initialized PieCrust', | 254 logger.debug(format_timed(start_time, 'initialized PieCrust', |
255 colored=False)) | 255 colored=False)) |
256 | 256 |
257 # Print the help if no command was specified. | 257 # Print the help if no command was specified. |
258 if not hasattr(result, 'func'): | 258 if not hasattr(result, 'func'): |