comparison libervia/server/tasks/task.py @ 1245:079e8eb6e327

server (tasks): refactoring: - moved `TasksManager` to `server.tasks.manager` - tasks modules now must have a `Task` class which will be instanciated by TasksManager - `server.tasks.task` has a basis for `Task` class - `Task.prepare` can now be asynchronous - `importlib` is now used to import tasks, instead of exec - tasks are now parsed/run after pages are imported - `server.BackendReady` is now a coroutine - type hinting for Task attributes
author Goffi <goffi@goffi.org>
date Sat, 25 Apr 2020 16:08:55 +0200
parents libervia/server/tasks.py@f511f8fbbf8a
children 6161076193e0
comparison
equal deleted inserted replaced
1244:2ed4e399e1d4 1245:079e8eb6e327
1 #!/usr/bin/env python3
2
3 # Libervia: a Salut à Toi frontend
4 # Copyright (C) 2011-2020 Jérôme Poisson <goffi@goffi.org>
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 from twisted.python.procutils import which
19 from sat.core.log import getLogger
20 from sat.tools.common import async_process
21 from sat.core import exceptions
22 from sat.core.i18n import _
23 from typing import Optional
24
25 log = getLogger(__name__)
26
27
28 class Task:
29 """Handle tasks of a Libervia site"""
30 # can be "stop" or "continue"
31 ON_ERROR: str = "stop"
32 LOG_OUTPUT: bool = True
33 # list of directories to check for restarting this task
34 WATCH_DIRS: Optional[list] = None
35
36 def __init__(self, manager):
37 self.manager = manager
38
39 @property
40 def host(self):
41 return self.manager.host
42
43 @property
44 def resource(self):
45 return self.manager.resource
46
47 @property
48 def site_path(self):
49 return self.manager.site_path
50
51 @property
52 def build_path(self):
53 """path where generated files will be build for this site"""
54 return self.manager.build_path
55
56 def getConfig(self, key, default=None, value_type=None):
57 return self.host.getConfig(self.resource, key=key, default=default,
58 value_type=value_type)
59
60 @property
61 def site_name(self):
62 return self.resource.site_name
63
64 def findCommand(self, name, *args):
65 """Find full path of a shell command
66
67 @param name(unicode): name of the command to find
68 @param *args(unicode): extra names the command may have
69 @return (unicode): full path of the command
70 @raise exceptions.NotFound: can't find this command
71 """
72 names = (name,) + args
73 for n in names:
74 try:
75 cmd_path = which(n)[0]
76 except IndexError:
77 pass
78 else:
79 return cmd_path
80 raise exceptions.NotFound(_(
81 "Can't find {name} command, did you install it?").format(name=name))
82
83 def runCommand(self, command, *args, **kwargs):
84 kwargs['verbose'] = self.LOG_OUTPUT
85 return async_process.CommandProtocol.run(command, *args, **kwargs)