Mercurial > libervia-web
annotate libervia/server/tasks/manager.py @ 1258:92ff09cdd6dd
server: gather themes data:
- the new LiberviaRootResource's `site_themes` attribute is a set of available themes for
a site, it is set a template variable for pages.
- if `browser_meta` are available for a theme, the metadata are merged with pages root
`_browser` data
- if python files are found in `browser_path`, they are added to be handled by brython
task.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 03 May 2020 19:28:14 +0200 |
parents | 80a92eb82b7f |
children | 6161076193e0 |
rev | line source |
---|---|
1239 | 1 #!/usr/bin/env python3 |
2 | |
1146 | 3 # Libervia: a Salut à Toi frontend |
1237 | 4 # Copyright (C) 2011-2020 Jérôme Poisson <goffi@goffi.org> |
1146 | 5 |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 import os | |
19 import os.path | |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
20 from pathlib import Path |
1245 | 21 import importlib.util |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
22 from twisted.internet import defer |
1245 | 23 from sat.core.log import getLogger |
1146 | 24 from sat.core import exceptions |
25 from sat.core.i18n import _ | |
1245 | 26 from sat.tools import utils |
1146 | 27 from libervia.server.constants import Const as C |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
28 from . import implicit |
1146 | 29 |
30 log = getLogger(__name__) | |
31 | |
1252
80a92eb82b7f
server (tasks manager): added a label for default site
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
32 DEFAULT_SITE_LABEL = _("default site") |
80a92eb82b7f
server (tasks manager): added a label for default site
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
33 |
1146 | 34 |
1245 | 35 class TasksManager: |
1146 | 36 """Handle tasks of a Libervia site""" |
37 | |
38 def __init__(self, host, site_resource): | |
39 """ | |
40 @param site_resource(LiberviaRootResource): root resource of the site to manage | |
41 """ | |
42 self.host = host | |
43 self.resource = site_resource | |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
44 self.tasks_dir = self.site_path / C.TASKS_DIR |
1245 | 45 self.tasks = {} |
1146 | 46 self._build_path = None |
47 self._current_task = None | |
48 | |
49 @property | |
50 def site_path(self): | |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
51 return Path(self.resource.site_path) |
1146 | 52 |
53 @property | |
54 def build_path(self): | |
55 """path where generated files will be build for this site""" | |
56 if self._build_path is None: | |
57 self._build_path = self.host.getBuildPath(self.site_name) | |
58 return self._build_path | |
59 | |
60 @property | |
61 def site_name(self): | |
62 return self.resource.site_name | |
63 | |
1245 | 64 def validateData(self, task): |
65 """Check workflow attributes in task""" | |
1146 | 66 |
1245 | 67 for var, allowed in (("ON_ERROR", ("continue", "stop")), |
68 ("LOG_OUTPUT", bool), | |
69 ("WATCH_DIRS", list)): | |
70 value = getattr(task, var) | |
1146 | 71 |
72 if isinstance(allowed, type): | |
1245 | 73 if allowed is list and value is None: |
74 continue | |
1146 | 75 if not isinstance(value, allowed): |
76 raise ValueError( | |
1216 | 77 _("Unexpected value for {var}, {allowed} is expected.") |
1245 | 78 .format(var=var, allowed=allowed)) |
1146 | 79 else: |
80 if not value in allowed: | |
1245 | 81 raise ValueError(_("Unexpected value for {var}: {value!r}").format( |
82 var=var, value=value)) | |
1146 | 83 |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
84 async def parseTasksDir(self, dir_path): |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
85 log.debug(f"parsing tasks in {dir_path}") |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
86 tasks_paths = sorted(dir_path.glob('task_*.py')) |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
87 for task_path in tasks_paths: |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
88 if not task_path.is_file(): |
1146 | 89 continue |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
90 task_name = task_path.stem[5:].lower().strip() |
1146 | 91 if not task_name: |
92 continue | |
93 if task_name in self.tasks: | |
94 raise exceptions.ConflictError( | |
1216 | 95 "A task with the name [{name}] already exists".format( |
1146 | 96 name=task_name)) |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
97 log.debug(f"task {task_name} found") |
1252
80a92eb82b7f
server (tasks manager): added a label for default site
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
98 module_name = f"{self.site_name or C.SITE_NAME_DEFAULT}.task.{task_name}" |
1245 | 99 |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
100 spec = importlib.util.spec_from_file_location(module_name, task_path) |
1245 | 101 task_module = importlib.util.module_from_spec(spec) |
102 spec.loader.exec_module(task_module) | |
103 task = task_module.Task(self) | |
104 | |
1154
a1625e68b726
server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
105 # we launch prepare, which is a method used to prepare |
a1625e68b726
server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
106 # data at runtime (e.g. set WATCH_DIRS using config) |
a1625e68b726
server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
107 try: |
1245 | 108 prepare = task.prepare |
109 except AttributeError: | |
1154
a1625e68b726
server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
110 pass |
a1625e68b726
server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents:
1147
diff
changeset
|
111 else: |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
112 try: |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
113 await utils.asDeferred(prepare) |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
114 except exceptions.CancelError as e: |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
115 log.debug(f"Skipping {task_name} which cancelled itself: {e}") |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
116 continue |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
117 |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
118 self.tasks[task_name] = task |
1245 | 119 self.validateData(task) |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
120 if self.host.options['dev_mode']: |
1245 | 121 dirs = task.WATCH_DIRS or [] |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
122 for dir_ in dirs: |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
123 self.host.files_watcher.watchDir( |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
124 dir_, auto_add=True, recursive=True, |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
125 callback=self._autorunTask, task_name=task_name) |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
126 |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
127 async def parseTasks(self): |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
128 # implicit tasks are always run |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
129 implicit_path = Path(implicit.__file__).parent |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
130 await self.parseTasksDir(implicit_path) |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
131 # now we check if there are tasks specific to this site |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
132 if not self.tasks_dir.is_dir(): |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
133 log.debug(_("{name} has no task to launch.").format( |
1252
80a92eb82b7f
server (tasks manager): added a label for default site
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
134 name = self.resource.site_name or DEFAULT_SITE_LABEL)) |
1247
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
135 return |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
136 else: |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
137 await self.parseTasksDir(self.tasks_dir) |
a6c7f07f1e4d
tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents:
1245
diff
changeset
|
138 |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
139 def _autorunTask(self, host, filepath, flags, task_name): |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
140 """Called when an event is received from a watched directory""" |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
141 if flags == ['create']: |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
142 return |
1245 | 143 return defer.ensureDeferred(self.runTask(task_name)) |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
144 |
1245 | 145 async def runTask(self, task_name): |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
146 """Run a single task |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
147 |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
148 @param task_name(unicode): name of the task to run |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
149 """ |
1245 | 150 task = self.tasks[task_name] |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
151 self._current_task = task_name |
1216 | 152 log.info(_('== running task "{task_name}" for {site_name} =='.format( |
1252
80a92eb82b7f
server (tasks manager): added a label for default site
Goffi <goffi@goffi.org>
parents:
1247
diff
changeset
|
153 task_name=task_name, site_name=self.site_name or DEFAULT_SITE_LABEL))) |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
154 os.chdir(self.site_path) |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
155 try: |
1245 | 156 await utils.asDeferred(task.start) |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
157 except Exception as e: |
1245 | 158 on_error = task.ON_ERROR |
1216 | 159 if on_error == 'stop': |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
160 raise e |
1216 | 161 elif on_error == 'continue': |
162 log.warning(_('Task "{task_name}" failed for {site_name}: {reason}') | |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
163 .format(task_name=task_name, site_name=self.site_name, reason=e)) |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
164 else: |
1216 | 165 raise exceptions.InternalError("we should never reach this point") |
1155
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
166 self._current_task = None |
1146 | 167 |
1245 | 168 async def runTasks(self): |
1146 | 169 """Run all the tasks found""" |
170 old_path = os.getcwd() | |
1216 | 171 for task_name, task_value in self.tasks.items(): |
1245 | 172 await self.runTask(task_name) |
1146 | 173 os.chdir(old_path) |