Example #1
0
 def _reset_routes(pod):
     with timer.Timer() as router_time:
         pod.router.routes.reset()
         pod.router.add_all(concrete=False, use_cache=False)
         # Trigger the dev handler hook.
         pod.extensions_controller.trigger('dev_handler', pod.router.routes)
     pod.logger.info('{} routes rebuilt in {:.3f} s'.format(
         len(pod.router.routes), router_time.secs))
Example #2
0
 def _reset_routes(pod):
     if pod.use_reroute:
         with timer.Timer() as router_time:
             pod.router.routes.reset()
             pod.router.add_all(concrete=False)
         pod.logger.info('{} routes rebuilt in {:.3f} s'.format(
             len(pod.router.routes), router_time.secs))
     else:
         pod.routes.reset_cache(rebuild=True)
Example #3
0
    def server_activate(self):
        super(CallbackHTTPServer, self).server_activate()
        _, port = self.server_address
        self.pod.env.port = port
        if self.pod.use_reroute:
            with timer.Timer() as router_time:
                self.pod.router.add_all(concrete=False)
            self.pod.logger.info('{} routes built in {:.3f} s'.format(
                len(self.pod.router.routes), router_time.secs))
        else:
            with timer.Timer() as load_timer:
                self.pod.load()
            self.pod.logger.info('Pod loaded in {:.3f} s'.format(
                load_timer.secs))

        url = print_server_ready_message(self.pod, self.pod.env.host, port)
        if self.open_browser:
            start_browser_in_thread(url)
        if self.update_check:
            check_func = sdk_utils.check_for_sdk_updates
            thread = threading.Thread(target=check_func, args=(True, ))
            thread.start()
Example #4
0
    def server_activate(self):
        super(CallbackHTTPServer, self).server_activate()
        _, port = self.server_address
        self.pod.env.port = port
        with timer.Timer() as router_time:
            try:
                self.pod.router.add_all(concrete=False)
            except bulk_errors.BulkErrors as err:
                bulk_errors.display_bulk_errors(err)
                sys.exit(-1)
        self.pod.logger.info('{} routes built in {:.3f} s'.format(
            len(self.pod.router.routes), router_time.secs))

        url = print_server_ready_message(self.pod, self.pod.env.host, port)
        if self.open_browser:
            start_browser_in_thread(url)
        if self.update_check:
            update_checker = updater.Updater(self.pod)
            check_func = update_checker.check_for_updates
            thread = threading.Thread(target=check_func, args=(True, ))
            thread.start()
Example #5
0
    def on_file_changed(self, pod_path):
        """Handle when a single file has changed in the pod."""
        # Remove any raw file in the cache.
        self.podcache.file_cache.remove(pod_path)

        basename = os.path.basename(pod_path)
        ignore_doc = basename.startswith(collection.Collection.IGNORE_INITIAL)

        if pod_path == '/{}'.format(self.FILE_PODSPEC):
            self.reset_yaml()
            self.podcache.reset()
            if self.use_reroute:
                with timer.Timer() as router_time:
                    self.router.routes.reset()
                    self.router.add_all(concrete=False)
                self.logger.info('{} routes rebuilt in {:.3f} s'.format(
                    len(self.router.routes), router_time.secs))
            else:
                self.routes.reset_cache(rebuild=True)
        elif (pod_path.endswith(collection.Collection.BLUEPRINT_PATH)
              and pod_path.startswith(collection.Collection.CONTENT_PATH)):
            doc = self.get_doc(pod_path)
            self.podcache.collection_cache.remove_collection(doc.collection)
            if self.use_reroute:
                with timer.Timer() as router_time:
                    self.router.routes.reset()
                    self.router.add_all(concrete=False)
                self.logger.info('{} routes rebuilt in {:.3f} s'.format(
                    len(self.router.routes), router_time.secs))
            else:
                self.routes.reset_cache(rebuild=True)
        elif pod_path.startswith(
                collection.Collection.CONTENT_PATH) and not ignore_doc:
            trigger_doc = self.get_doc(pod_path)
            col = trigger_doc.collection
            base_docs = []
            original_docs = []
            trigger_docs = col.list_servable_document_locales(pod_path)

            for dep_path in self.podcache.dependency_graph.get_dependents(
                    pod_path):
                base_docs.append(self.get_doc(dep_path))
                original_docs += col.list_servable_document_locales(dep_path)

            for doc in base_docs:
                self.podcache.document_cache.remove(doc)
                self.podcache.collection_cache.remove_document_locales(doc)

            # Force load the docs and fix locales.
            docs_loader.DocsLoader.load(base_docs, ignore_errors=True)
            docs_loader.DocsLoader.fix_default_locale(self,
                                                      base_docs,
                                                      ignore_errors=True)

            # The routing map should remain unchanged most of the time.
            added_docs = []
            removed_docs = []
            for original_doc in original_docs:
                # Removed documents should be removed.
                if not original_doc.exists:
                    removed_docs.append(original_doc)
                    continue

                updated_doc = self.get_doc(original_doc.pod_path,
                                           original_doc._locale_kwarg)

                # When the serving path has changed, updated in routes.
                if (updated_doc.has_serving_path()
                        and original_doc.get_serving_path() !=
                        updated_doc.get_serving_path()):
                    added_docs.append(updated_doc)
                    removed_docs.append(original_doc)

                # If the locales change then we need to adjust the routes.
                original_locales = set([str(l) for l in original_doc.locales])
                updated_locales = set([str(l) for l in updated_doc.locales])

                new_locales = updated_locales - original_locales
                for locale in new_locales:
                    new_doc = self.get_doc(original_doc.pod_path, locale)
                    if new_doc.has_serving_path(
                    ) and new_doc not in added_docs:
                        added_docs.append(new_doc)

                removed_locales = original_locales - updated_locales
                for locale in removed_locales:
                    removed_doc = self.get_doc(original_doc.pod_path, locale)
                    if removed_doc.has_serving_path():
                        if removed_doc not in removed_docs:
                            removed_docs.append(removed_doc)

            # Check for new docs.
            for trigger_doc in trigger_docs:
                if trigger_doc.has_serving_path():
                    if self.use_reroute:
                        if not self.router.routes.match(
                                trigger_doc.get_serving_path()):
                            added_docs.append(trigger_doc)
                    else:
                        try:
                            route_env = self.env.to_wsgi_env()
                            _ = self.routes.match(
                                trigger_doc.get_serving_path(), env=route_env)
                        except webob_exc.HTTPNotFound:
                            added_docs.append(trigger_doc)
            if added_docs or removed_docs:
                if self.use_reroute:
                    self.router.reconcile_documents(remove_docs=removed_docs,
                                                    add_docs=added_docs)
                else:
                    self.routes.reconcile_documents(remove_docs=removed_docs,
                                                    add_docs=added_docs)
        elif pod_path == '/{}'.format(podcache.FILE_OBJECT_CACHE):
            self.podcache.update(obj_cache=self._parse_object_cache_file())
            if self.podcache.is_dirty:
                logging.info('Object cache changed, updating with new data.')
                self.podcache.write()
        elif self.use_reroute:
            # Check if the file is a static file that needs to have the
            # fingerprint updated.
            for config in self.static_configs:
                if config.get('dev') and not self.env.dev:
                    continue
                fingerprinted = config.get('fingerprinted', False)
                if not fingerprinted:
                    continue
                if pod_path.startswith(config['static_dir']):
                    static_doc = self.get_static(pod_path, locale=None)
                    self.router.add_static_doc(static_doc)
                    break
Example #6
0
 def reset_cache(self, rebuild=True, inject=False):
     if rebuild:
         self.pod.logger.info('Rebuilding routes...')
         with timer.Timer() as t:
             self._build_routing_map(inject=False)
         self.pod.logger.info('Routes rebuilt in {:.3f} s'.format(t.secs))