def handle(self, *args, **options):
        # Automatically exit if our own code changes.
        # This is not based on a published API, so quite likely will fail
        # and need to be updated in a future version of django

        # Start our work in a background thread
        bthread = threading.Thread(target=self.inner_handle)
        bthread.setDaemon(True)
        bthread.start()

        reloader = autoreload.get_reloader()
        while not reloader.should_stop:
            reloader.run(bthread)

        self.stderr.write("Underlying code changed, exiting for a restart")
        sys.exit(0)
Beispiel #2
0
    def handle(self, *args, **options):
        # Set up our background thread to run the inner handler
        self.run_args = args
        self.run_options = options

        if settings.RELOAD_WATCH_DIRECTORIES:
            wm = pyinotify.WatchManager()

            class EventHandler(pyinotify.ProcessEvent):
                def __init__(self, parent):
                    self.parent = parent
                    super().__init__()

                def process_default(self, event):
                    # We only exit if it's a python or precompiled python change
                    if not event.pathname.endswith(
                            '.py') and not event.pathname.endswith('.pyc'):
                        return

                    self.parent.stderr.write("Detected change in {}\n".format(
                        event.pathname))
                    self.parent.stderr.write("Exiting for restart\n")
                    os._exit(0)

            notifier = pyinotify.ThreadedNotifier(wm, EventHandler(self))
            notifier.start()
            for d in settings.RELOAD_WATCH_DIRECTORIES:
                wm.add_watch(d,
                             pyinotify.IN_DELETE | pyinotify.IN_CREATE
                             | pyinotify.IN_MODIFY | pyinotify.IN_ATTRIB,
                             rec=True)

            # Runt he main task on the primary thread
            self._inner_handle()
        else:
            bthread = threading.Thread(target=self._inner_handle)
            bthread.setDaemon(True)
            bthread.start()

            reloader = autoreload.get_reloader()
            while not reloader.should_stop:
                reloader.run(bthread)

            self.stderr.write(
                "Underlying code changed, exiting for a restart\n")
            sys.exit(0)
Beispiel #3
0
    def handle(self, *args, **kwargs):
        self.project_dir = os.getcwd()

        signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
        try:
            if os.environ.get(autoreload.DJANGO_AUTORELOAD_ENV) == "true":
                reloader = autoreload.get_reloader()
                reloader.watch_dir(os.path.join(self.project_dir, "doc"),
                                   "**/*.rst")
                autoreload.logger.info("Watching for file changes with %s",
                                       reloader.__class__.__name__)
                autoreload.start_django(reloader, self.build_and_start_server,
                                        **kwargs)
            else:
                exit_code = autoreload.restart_with_reloader()
                sys.exit(exit_code)
        except KeyboardInterrupt:
            pass
 def test_watchman_available(self, mocked_available):
     # If WatchmanUnavailable isn't raised, Watchman will be chosen.
     mocked_available.return_value = None
     result = autoreload.get_reloader()
     self.assertIsInstance(result, autoreload.WatchmanReloader)
 def test_watchman_unavailable(self, mocked_watchman):
     mocked_watchman.check_availability.side_effect = WatchmanUnavailable
     self.assertIsInstance(autoreload.get_reloader(), autoreload.StatReloader)
Beispiel #6
0
 def test_watchman_available(self, mocked_available):
     # If WatchmanUnavailable isn't raised, Watchman will be chosen.
     mocked_available.return_value = None
     result = autoreload.get_reloader()
     self.assertIsInstance(result, autoreload.WatchmanReloader)
Beispiel #7
0
 def test_watchman_unavailable(self, mocked_watchman):
     mocked_watchman.check_availability.side_effect = WatchmanUnavailable
     self.assertIsInstance(autoreload.get_reloader(), autoreload.StatReloader)
Beispiel #8
0
def gen_filenames():
    return get_reloader().watched_files()
 def gen_filenames():
     return get_reloader().watched_files()