annotate libervia/server/tasks.py @ 1243:8aff742d0dd0

pages: updated `identityGet` call, following backend changes
author Goffi <goffi@goffi.org>
date Sun, 19 Apr 2020 22:56:59 +0200
parents f511f8fbbf8a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1239
f511f8fbbf8a fixed shebangs
Goffi <goffi@goffi.org>
parents: 1237
diff changeset
1 #!/usr/bin/env python3
f511f8fbbf8a fixed shebangs
Goffi <goffi@goffi.org>
parents: 1237
diff changeset
2
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Libervia: a Salut à Toi frontend
1237
987595a254b0 dates update
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
5 # Copyright (C) 2011-2020 Jérôme Poisson <goffi@goffi.org>
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import os
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import os.path
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.internet import defer
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.python.procutils import which
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core import exceptions
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from libervia.server.constants import Const as C
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from collections import OrderedDict
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.log import getLogger
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.tools.common import async_process
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 log = getLogger(__name__)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 class TasksManager(object):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 """Handle tasks of a Libervia site"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
35 FILE_EXTS = {'py'}
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 def __init__(self, host, site_resource):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 """
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 @param site_resource(LiberviaRootResource): root resource of the site to manage
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 """
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.host = host
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.resource = site_resource
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.tasks_dir = os.path.join(self.resource.site_path, C.TASKS_DIR)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.tasks = OrderedDict()
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.parseTasks()
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self._build_path = None
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self._current_task = None
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def site_path(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 return self.resource.site_path
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def build_path(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 """path where generated files will be build for this site"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 if self._build_path is None:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self._build_path = self.host.getBuildPath(self.site_name)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 return self._build_path
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def getConfig(self, key, default=None, value_type=None):
1147
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
61 return self.host.getConfig(self.resource, key=key, default=default,
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
62 value_type=value_type)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def site_name(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 return self.resource.site_name
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def task_data(self):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
70 return self.tasks[self._current_task]['data']
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 def validateData(self, data):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """Check values in data"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
75 for var, default, allowed in (("ON_ERROR", "stop", ("continue", "stop")),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
76 ("LOG_OUTPUT", True, bool),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
77 ("WATCH_DIRS", [], list)):
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 value = data.setdefault(var, default)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if isinstance(allowed, type):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 if not isinstance(value, allowed):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 raise ValueError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
82 _("Unexpected value for {var}, {allowed} is expected.")
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 .format(var = var, allowed = allowed))
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 else:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if not value in allowed:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
86 raise ValueError(_("Unexpected value for {var}: {value}").format(
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 var = var, value = value))
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
89 for var, default, allowed in [["ON_ERROR", "stop", ("continue", "stop")]]:
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 value = data.setdefault(var, default)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if not value in allowed:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
92 raise ValueError(_("Unexpected value for {var}: {value}").format(
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 var = var, value = value))
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def parseTasks(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if not os.path.isdir(self.tasks_dir):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
97 log.debug(_("{name} has no task to launch.").format(
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
98 name = self.resource.site_name or "default site"))
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 filenames = os.listdir(self.tasks_dir)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 filenames.sort()
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 for filename in filenames:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 filepath = os.path.join(self.tasks_dir, filename)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
104 if not filename.startswith('task_') or not os.path.isfile(filepath):
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 continue
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 task_name, ext = os.path.splitext(filename)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 task_name = task_name[5:].lower().strip()
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 if not task_name:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 continue
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 if ext[1:] not in self.FILE_EXTS:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 continue
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 if task_name in self.tasks:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 raise exceptions.ConflictError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
114 "A task with the name [{name}] already exists".format(
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 name=task_name))
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
116 task_data = {"__name__": "{site_name}.task.{name}".format(
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 site_name=self.site_name, name=task_name)}
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self.tasks[task_name] = {
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
119 'path': filepath,
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
120 'data': task_data,
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 }
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
122 exec(compile(open(filepath, "rb").read(), filepath, 'exec'), task_data)
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
123 # 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
124 # 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
125 try:
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
126 prepare = task_data['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
127 except KeyError:
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
128 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
129 else:
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
130 prepare(self)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 self.validateData(task_data)
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
132 if self.host.options['dev_mode']:
1166
5baf7ece44a0 server (tasks): fixed crash when --dev_mode is used
Goffi <goffi@goffi.org>
parents: 1155
diff changeset
133 dirs = task_data.get('WATCH_DIRS', [])
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
134 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
135 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
136 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
137 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
138
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
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
143 return self.runTask(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
144
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
145 @defer.inlineCallbacks
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 def runTask(self, 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
147 """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
148
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 @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
150 """
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 task_value = self.tasks[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
152 self._current_task = task_name
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
153 log.info(_('== running task "{task_name}" for {site_name} =='.format(
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 task_name=task_name, site_name=self.site_name)))
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
155 data = task_value['data']
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
156 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
157 try:
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
158 yield data['start'](self)
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
159 except Exception as e:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
160 on_error = data['ON_ERROR']
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
161 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
162 raise e
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
163 elif on_error == 'continue':
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
164 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
165 .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
166 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
167 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
168 self._current_task = None
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 @defer.inlineCallbacks
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 def runTasks(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 """Run all the tasks found"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 old_path = os.getcwd()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
174 for task_name, task_value in self.tasks.items():
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
175 yield self.runTask(task_name)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 os.chdir(old_path)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 def findCommand(self, name, *args):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 """Find full path of a shell command
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 @param name(unicode): name of the command to find
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 @param *args(unicode): extra names the command may have
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 @return (unicode): full path of the command
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 @raise exceptions.NotFound: can't find this command
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 """
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 names = (name,) + args
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 for n in names:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 try:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
189 cmd_path = which(n)[0]
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 except IndexError:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 pass
1189
170802865156 server (tasks): fixed findCommand when first name can't be found
Goffi <goffi@goffi.org>
parents: 1166
diff changeset
192 else:
170802865156 server (tasks): fixed findCommand when first name can't be found
Goffi <goffi@goffi.org>
parents: 1166
diff changeset
193 return cmd_path
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 raise exceptions.NotFound(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
195 "Can't find {name} command, did you install it?").format(name=name))
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 def runCommand(self, command, *args, **kwargs):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
198 kwargs['verbose'] = self.task_data["LOG_OUTPUT"]
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 return async_process.CommandProtocol.run(command, *args, **kwargs)