Mercurial > piecrust2
comparison piecrust/main.py @ 979:45ad976712ec
tests: Big push to get the tests to pass again.
- Lots of fixes everywhere in the code.
- Try to handle debug logging in the multiprocessing worker pool when running in pytest. Not perfect, but usable for now.
- Replace all `.md` test files with `.html` since now a auto-format extension always sets the format.
- Replace `out` with `outfiles` in most places since now blog archives are added to the bake output and I don't want to add expected outputs for blog archives everywhere.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 29 Oct 2017 22:51:57 -0700 |
parents | eed19a80c00e |
children | 8adc27285d93 |
comparison
equal
deleted
inserted
replaced
978:7e51d14097cb | 979:45ad976712ec |
---|---|
141 """ | 141 """ |
142 _command_caches = { | 142 _command_caches = { |
143 'serve': 'server'} | 143 'serve': 'server'} |
144 | 144 |
145 | 145 |
146 def _pre_parse_chef_args(argv): | 146 def _make_chef_state(): |
147 return [] | |
148 | |
149 | |
150 def _recover_pre_chef_state(state): | |
151 for s in state: | |
152 s() | |
153 | |
154 | |
155 def _pre_parse_chef_args(argv, *, bypass_setup=False, state=None): | |
147 # We need to parse some arguments before we can build the actual argument | 156 # We need to parse some arguments before we can build the actual argument |
148 # parser, because it can affect which plugins will be loaded. Also, log- | 157 # parser, because it can affect which plugins will be loaded. Also, log- |
149 # related arguments must be parsed first because we want to log everything | 158 # related arguments must be parsed first because we want to log everything |
150 # from the beginning. | 159 # from the beginning. |
151 parser = argparse.ArgumentParser() | 160 parser = argparse.ArgumentParser() |
152 _setup_main_parser_arguments(parser) | 161 _setup_main_parser_arguments(parser) |
153 parser.add_argument('extra_args', nargs=argparse.REMAINDER) | 162 parser.add_argument('extra_args', nargs=argparse.REMAINDER) |
154 res, _ = parser.parse_known_args(argv) | 163 res, _ = parser.parse_known_args(argv) |
164 if bypass_setup: | |
165 return res | |
155 | 166 |
156 # Setup the logger. | 167 # Setup the logger. |
157 if res.debug and res.quiet: | 168 if res.debug and res.quiet: |
158 raise Exception("You can't specify both --debug and --quiet.") | 169 raise Exception("You can't specify both --debug and --quiet.") |
159 | 170 |
161 if res.no_color: | 172 if res.no_color: |
162 strip_colors = True | 173 strip_colors = True |
163 | 174 |
164 colorama.init(strip=strip_colors) | 175 colorama.init(strip=strip_colors) |
165 root_logger = logging.getLogger() | 176 root_logger = logging.getLogger() |
177 previous_level = root_logger.level | |
166 root_logger.setLevel(logging.INFO) | 178 root_logger.setLevel(logging.INFO) |
167 if res.debug or res.log_debug: | 179 if res.debug or res.log_debug: |
168 root_logger.setLevel(logging.DEBUG) | 180 root_logger.setLevel(logging.DEBUG) |
181 if state is not None: | |
182 state.append(lambda: root_logger.setLevel(previous_level)) | |
169 | 183 |
170 if res.debug_only: | 184 if res.debug_only: |
171 for n in res.debug_only: | 185 for n in res.debug_only: |
172 logging.getLogger(n).setLevel(logging.DEBUG) | 186 sub_logger = logging.getLogger(n) |
187 previous_level = sub_logger.level | |
188 sub_logger.setLevel(logging.DEBUG) | |
189 if state is not None: | |
190 state.append(lambda: sub_logger.setLevel(previous_level)) | |
173 | 191 |
174 log_handler = logging.StreamHandler(sys.stdout) | 192 log_handler = logging.StreamHandler(sys.stdout) |
175 if res.debug or res.debug_only: | 193 if res.debug or res.debug_only: |
176 log_handler.setLevel(logging.DEBUG) | 194 log_handler.setLevel(logging.DEBUG) |
177 log_handler.setFormatter(ColoredFormatter("[%(name)s] %(message)s")) | 195 log_handler.setFormatter(ColoredFormatter("[%(name)s] %(message)s")) |
180 log_handler.setLevel(logging.WARNING) | 198 log_handler.setLevel(logging.WARNING) |
181 else: | 199 else: |
182 log_handler.setLevel(logging.INFO) | 200 log_handler.setLevel(logging.INFO) |
183 log_handler.setFormatter(ColoredFormatter("%(message)s")) | 201 log_handler.setFormatter(ColoredFormatter("%(message)s")) |
184 root_logger.addHandler(log_handler) | 202 root_logger.addHandler(log_handler) |
203 if state is not None: | |
204 state.append(lambda: root_logger.removeHandler(log_handler)) | |
185 | 205 |
186 if res.log_file: | 206 if res.log_file: |
187 file_handler = logging.FileHandler(res.log_file, mode='w') | 207 file_handler = logging.FileHandler(res.log_file, mode='w') |
188 root_logger.addHandler(file_handler) | 208 root_logger.addHandler(file_handler) |
189 if res.log_debug: | 209 if res.log_debug: |
190 file_handler.setLevel(logging.DEBUG) | 210 file_handler.setLevel(logging.DEBUG) |
211 if state is not None: | |
212 state.append(lambda: root_logger.removeHandler(file_handler)) | |
191 | 213 |
192 # PID file. | 214 # PID file. |
193 if res.pid_file: | 215 if res.pid_file: |
194 try: | 216 try: |
195 pid_file_dir = os.path.dirname(res.pid_file) | 217 pid_file_dir = os.path.dirname(res.pid_file) |