1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
# Deb-o-Matic
#
# Copyright (C) 2008-2009 David Futcher
# Copyright (C) 2012-2018 Luca Falavigna
#
# Author: David Futcher <bobbo@ubuntu.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
import os
from glob import glob
from logging import debug
from re import findall
from sys import path
from toposort import toposort_flatten as toposort
from Debomatic import dom
from .process import ModulePool
class ModuleArgs():
def __init__(self):
self.opts = dom.opts
self.action = None
self.architecture = None
self.directory = None
self.distribution = None
self.dists = None
self.dsc = None
self.files = None
self.package = None
self.success = False
self.uploader = None
self.hostarchitecture = None
class Module():
def __init__(self):
self.args = ModuleArgs()
self._use_modules = False
if dom.opts.has_option('modules', 'modules'):
if dom.opts.getboolean('modules', 'modules'):
self._use_modules = True
if dom.opts.has_option('modules', 'path'):
mod_path = dom.opts.get('modules', 'path')
for p in mod_path.split(':'):
path.append(p)
else:
self._use_modules = False
modules = set()
try:
for p in mod_path.split(':'):
modules |= set([os.path.splitext(os.path.basename(m))[0] for m in
glob(os.path.join(p, '*.py'))])
except OSError:
self._use_modules = False
if not modules:
self._use_modules = False
if self._use_modules:
self._instances = {}
for module in modules:
try:
_class = 'DebomaticModule_%s' % module
_mod = __import__(module)
self._instances[module] = getattr(_mod, _class)()
self._instances[module]._modulename = module
self._instances[module]._disabled = False
self._instances[module]._depends = set()
self._instances[module]._after = set()
debug(_('Module %s loaded') % module)
except (NameError, SyntaxError):
pass
self._set_relationships()
self._set_blacklisted()
self._disable_modules()
debug(_('Modules will be executed in this order: %s') %
', '.join([m for m in self._sort_modules()
if not self._instances[m]._disabled]))
def _disable_modules(self):
for module in self._sort_modules():
missing = set()
missing_after = set()
for dep in self._instances[module]._depends:
if dep in self._instances:
if self._instances[dep]._disabled:
missing.add(dep)
else:
missing.add(dep)
for dep in self._instances[module]._after:
if dep in self._instances:
if self._instances[dep]._disabled:
missing_after.add(dep)
else:
missing_after.add(dep)
if missing:
self._instances[module]._disabled = True
debug(_('%(mod)s module disabled, needs %(missing)s') %
{'mod': module, 'missing': ', '.join(missing)})
if missing_after:
for dep in missing_after:
self._instances[module]._after.remove(dep)
def _launcher(self, hook):
func, args, module, hookname, dependencies = hook
debug(_('Executing hook %(hook)s from module %(mod)s') %
{'hook': hookname, 'mod': module})
func(args)
def _set_blacklisted(self):
blist = set()
if dom.opts.has_option('modules', 'blacklist'):
_blacklist = dom.opts.get('modules', 'blacklist')
blist = set(_blacklist.split())
for module in self._instances:
if module in blist:
self._instances[module]._disabled = True
debug(_('Module %s is blacklisted') % module)
def _set_relationships(self):
for module in self._instances:
try:
deps = getattr(self._instances[module], 'dependencies')
if deps:
for dep in deps:
self._instances[module]._depends.add(dep)
except AttributeError:
pass
try:
afters = getattr(self._instances[module], 'after')
if afters:
for after in afters:
self._instances[module]._after.add(after)
except AttributeError:
pass
try:
befores = getattr(self._instances[module], 'before')
if befores:
for before in befores:
if before in self._instances:
self._instances[before]._after.add(module)
except AttributeError:
pass
try:
if getattr(self._instances[module], 'first'):
deps = (self._instances[module]._depends.union(
self._instances[module]._after))
if deps:
self._instances[module]._disabled = True
debug(_('Cannot execute %(mod)s as %(order)s module, '
'dependencies found: %(deps)s')
% {'mod': module, 'order': 'first',
'deps': ', '.join(deps)})
else:
for instance in self._instances:
self._instances[instance]._after.add(module)
except AttributeError:
pass
try:
if getattr(self._instances[module], 'last'):
deps = [m for m in self._instances
if module in self._instances[m]._after or
module in self._instances[m]._depends]
if deps:
self._instances[module]._disabled = True
debug(_('Cannot execute %(mod)s as %(order)s module, '
'dependencies found: %(deps)s')
% {'mod': module, 'order': 'last',
'deps': ', '.join(deps)})
else:
for instance in self._instances:
self._instances[module]._after.add(instance)
except AttributeError:
pass
if module in self._instances[module]._depends:
self._instances[module]._depend.remove(module)
if module in self._instances[module]._after:
self._instances[module]._after.remove(module)
def _sort_modules(self):
modules = {}
for instance in self._instances:
if not self._instances[instance]._disabled:
_deps = self._instances[instance]._depends
_afters = self._instances[instance]._after
modules[instance] = _deps.union(_afters)
try:
return [m for m in toposort(modules) if m in self._instances]
except ValueError as e:
circular = findall('.*?\(\'?(\S+?)\'?,', e.args[0])
for instance in circular:
self._instances[instance]._disabled = True
debug(_('Circular dependencies found, disabled modules: %s')
% ', '.join(circular))
return self._sort_modules()
def execute_hook(self, hook):
hooks = []
if self._use_modules:
for module in self._sort_modules():
if self._instances[module]._disabled:
continue
dependencies = set()
instance = self._instances[module]
for dep in instance._depends.union(instance._after):
if dep in self._instances:
if (dep in instance._after and
self._instances[dep]._disabled):
continue
try:
if getattr(self._instances[dep], hook):
dependencies.add(dep)
except AttributeError:
continue
try:
hooks.append((getattr(instance, hook),
self.args, module, hook, dependencies))
except AttributeError:
pass
if dom.opts.has_option('modules', 'threads'):
workers = dom.opts.getint('modules', 'threads')
else:
workers = 1
debug(_('%s hooks launched') % hook)
modulepool = ModulePool(workers)
for hk in hooks:
modulepool.schedule(self._launcher, hk)
modulepool.shutdown()
debug(_('%s hooks finished') % hook)
|