Command-line wrapper to re-run a script whenever its source changes. Scripts may be specified by filename or module name:: python -m tornado.autoreload -m tornado.test.runtests python -m tornado.autoreload tornado/test/runtests.py Running a script with this wrapper is simi
()
| 244 | |
| 245 | |
| 246 | def main() -> None: |
| 247 | class="st">"""Command-line wrapper to re-run a script whenever its source changes. |
| 248 | |
| 249 | Scripts may be specified by filename or module name:: |
| 250 | |
| 251 | python -m tornado.autoreload -m tornado.test.runtests |
| 252 | python -m tornado.autoreload tornado/test/runtests.py |
| 253 | |
| 254 | Running a script with this wrapper is similar to calling |
| 255 | `tornado.autoreload.wait` at the end of the script, but this wrapper |
| 256 | can catch import-time problems like syntax errors that would otherwise |
| 257 | prevent the script from reaching its call to `wait`. |
| 258 | class="st">""" |
| 259 | class="cm"># Remember that we were launched with autoreload as main. |
| 260 | class="cm"># The main module can be tricky; set the variables both in our globals |
| 261 | class="cm"># (which may be __main__) and the real importable version. |
| 262 | class="cm"># |
| 263 | class="cm"># We use optparse instead of the newer argparse because we want to |
| 264 | class="cm"># mimic the python command-line interface which requires stopping |
| 265 | class="cm"># parsing at the first positional argument. optparse supports |
| 266 | class="cm"># this but as far as I can tell argparse does not. |
| 267 | import optparse |
| 268 | import tornado.autoreload |
| 269 | |
| 270 | global _autoreload_is_main |
| 271 | global _original_argv, _original_spec |
| 272 | tornado.autoreload._autoreload_is_main = _autoreload_is_main = True |
| 273 | original_argv = sys.argv |
| 274 | tornado.autoreload._original_argv = _original_argv = original_argv |
| 275 | original_spec = getattr(sys.modules[class="st">"__main__"], class="st">"__spec__", None) |
| 276 | tornado.autoreload._original_spec = _original_spec = original_spec |
| 277 | |
| 278 | parser = optparse.OptionParser( |
| 279 | prog=class="st">"python -m tornado.autoreload", |
| 280 | usage=_USAGE, |
| 281 | epilog=class="st">"Either -m or a path must be specified, but not both", |
| 282 | ) |
| 283 | parser.disable_interspersed_args() |
| 284 | parser.add_option(class="st">"-m", dest=class="st">"module", metavar=class="st">"module", help=class="st">"module to run") |
| 285 | parser.add_option( |
| 286 | class="st">"--until-success", |
| 287 | action=class="st">"store_true", |
| 288 | help=class="st">"stop reloading after the program exist successfully (status code 0)", |
| 289 | ) |
| 290 | opts, rest = parser.parse_args() |
| 291 | if opts.module is None: |
| 292 | if not rest: |
| 293 | print(class="st">"Either -m or a path must be specified", file=sys.stderr) |
| 294 | sys.exit(1) |
| 295 | path = rest[0] |
| 296 | sys.argv = rest[:] |
| 297 | else: |
| 298 | path = None |
| 299 | sys.argv = [sys.argv[0]] + rest |
| 300 | |
| 301 | class="cm"># SystemExit.code is typed funny: https://github.com/python/typeshed/issues/8513 |
| 302 | class="cm"># All we care about is truthiness |
| 303 | exit_status: Union[int, str, None] = 1 |
no test coverage detected