diff libervia/server/server.py @ 1146:76d75423ef53

server: tasks manager first draft: A new task manager will check /tasks directory of website to scripts to execute before launching the site. This allows to generate docs, scripts, or do anything else useful. Generated files are put in in sat local dir, in cache, and are accessible from the website using the new "build_dir" variable.
author Goffi <goffi@goffi.org>
date Fri, 25 Jan 2019 08:58:41 +0100
parents 2af117bfe6cc
children 02afab1b15c5
line wrap: on
line diff
--- a/libervia/server/server.py	Fri Jan 25 08:41:43 2019 +0100
+++ b/libervia/server/server.py	Fri Jan 25 08:58:41 2019 +0100
@@ -17,6 +17,16 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import re
+import glob
+import os.path
+import sys
+import tempfile
+import shutil
+import uuid
+import urlparse
+import urllib
+import time
 from twisted.application import service
 from twisted.internet import reactor, defer
 from twisted.web import server
@@ -42,25 +52,16 @@
 from sat.core.i18n import _, D_
 from sat.core import exceptions
 from sat.tools import utils
+from sat.tools import config
 from sat.tools.common import regex
 from sat.tools.common import template
 from sat.tools.common import uri as common_uri
-
-import re
-import glob
-import os.path
-import sys
-import tempfile
-import shutil
-import uuid
-import urlparse
-import urllib
-import time
 from httplib import HTTPS_PORT
 import libervia
 from libervia.server import websockets
 from libervia.server.pages import LiberviaPage
 from libervia.server.utils import quote, ProgressHandler
+from libervia.server.tasks import TasksManager
 from functools import partial
 
 try:
@@ -1706,6 +1707,7 @@
         self.options = options
         self.initialised = defer.Deferred()
         self.waiting_profiles = WaitingRequests()  # FIXME: should be removed
+        self._main_conf = None
 
         if self.options["base_url_ext"]:
             self.base_url_ext = self.options.pop("base_url_ext")
@@ -1754,6 +1756,13 @@
             roots.insert(0, default)
         return roots
 
+    @property
+    def main_conf(self):
+        """SafeConfigParser instance opened on configuration file (sat.conf)"""
+        if self._main_conf is None:
+            self._main_conf = config.parseMainConf()
+        return self._main_conf
+
     def _namespacesGetCb(self, ns_map):
         self.ns_map = ns_map
 
@@ -1794,6 +1803,7 @@
         if default_dict:
             conf[u''] = default_dict
 
+    @defer.inlineCallbacks
     def backendReady(self, __):
         self.media_dir = self.bridge.getConfig("", "media_dir")
         self.local_dir = self.bridge.getConfig("", "local_dir")
@@ -1813,6 +1823,8 @@
         self.sat_root = default_root = LiberviaRootResource(
             host=self, host_name=u'', site_name=u'', site_path=default_site_path,
             path=self.html_dir)
+        tasks_manager = TasksManager(self, self.sat_root)
+        yield tasks_manager.runTasks()
         LiberviaPage.importPages(self, self.sat_root)
         # FIXME: handle _setMenu in a more generic way, taking care of external sites
         self.sat_root._setMenu(self.options["menu_json"])
@@ -1844,6 +1856,9 @@
                     site_name=site_name,
                     site_path=site_path,
                     path=root_path)
+                tasks_manager = TasksManager(self, res)
+                yield tasks_manager.runTasks()
+                res.putChild(C.BUILD_DIR, static.File(self.getBuildPath(site_name)))
             self.vhost_root.addHost(host_name.encode('utf-8'), res)
             LiberviaPage.importPages(self, res)
             # FIXME: default pages are accessible if not overriden by external website
@@ -2361,6 +2376,20 @@
         for root in self.roots:
             root.putChild(path, wrapped_res)
 
+    def getBuildPath(self, site_name):
+        """Generate build path for a given site name
+
+        @param site_name(unicode): name of the site
+        @return (unicode): path to the build directory
+        """
+        build_path_elts = [
+            config.getConfig(self.main_conf, "", "local_dir"),
+            C.CACHE_DIR,
+            C.LIBERVIA_CACHE,
+            regex.pathEscape(site_name)]
+        build_path = u"/".join(build_path_elts)
+        return os.path.abspath(os.path.expanduser(build_path))
+
     def getExtBaseURLData(self, request):
         """Retrieve external base URL Data