comparison libervia/web/server/tasks/task.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/server/tasks/task.py@106bae41f5c8
children
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3 # Libervia: a Salut à Toi frontend
4 # Copyright (C) 2011-2021 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 libervia.backend.core.log import getLogger
20 from libervia.backend.tools.common import async_process
21 from libervia.backend.core import exceptions
22 from libervia.backend.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 # Task.on_dir_event will be called if it exists, otherwise
35 # the task will be run and Task.start will be called
36 WATCH_DIRS: Optional[list] = None
37 # list of task names which must be prepared/started before this one
38 AFTER: Optional[list] = None
39
40 def __init__(self, manager, task_name):
41 self.manager = manager
42 self.name = task_name
43
44 @property
45 def host(self):
46 return self.manager.host
47
48 @property
49 def resource(self):
50 return self.manager.resource
51
52 @property
53 def site_path(self):
54 return self.manager.site_path
55
56 @property
57 def build_path(self):
58 """path where generated files will be build for this site"""
59 return self.manager.build_path
60
61 def config_get(self, key, default=None, value_type=None):
62 return self.host.config_get(self.resource, key=key, default=default,
63 value_type=value_type)
64
65 @property
66 def site_name(self):
67 return self.resource.site_name
68
69 def find_command(self, name, *args):
70 """Find full path of a shell command
71
72 @param name(unicode): name of the command to find
73 @param *args(unicode): extra names the command may have
74 @return (unicode): full path of the command
75 @raise exceptions.NotFound: can't find this command
76 """
77 names = (name,) + args
78 for n in names:
79 try:
80 cmd_path = which(n)[0]
81 except IndexError:
82 pass
83 else:
84 return cmd_path
85 raise exceptions.NotFound(_(
86 "Can't find {name} command, did you install it?").format(name=name))
87
88 def runCommand(self, command, *args, **kwargs):
89 kwargs['verbose'] = self.LOG_OUTPUT
90 return async_process.CommandProtocol.run(command, *args, **kwargs)