1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 # Copyright (C) 2010-2011 OpenERP s.a. (<http://openerp.com>).
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 """ Modules dependency graph. """
26 from os.path import join as opj
32 import openerp.osv as osv
33 import openerp.tools as tools
34 import openerp.tools.osutil as osutil
35 from openerp.tools.safe_eval import safe_eval as eval
36 from openerp.tools.translate import _
39 import openerp.release as release
43 from zipfile import PyZipFile, ZIP_DEFLATED
44 from cStringIO import StringIO
48 _logger = logging.getLogger(__name__)
51 """ Modules dependency graph.
53 The graph is a mapping from module name to Nodes.
57 def add_node(self, name, info):
58 max_depth, father = 0, None
59 for n in [Node(x, self, None) for x in info['depends']]:
60 if n.depth >= max_depth:
64 return father.add_child(name, info)
66 return Node(name, self, info)
68 def update_from_db(self, cr):
71 # update the graph with values from the database (if exist)
72 ## First, we set the default values for each package in graph
73 additional_data = dict.fromkeys(self.keys(), {'id': 0, 'state': 'uninstalled', 'dbdemo': False, 'installed_version': None})
74 ## Then we get the values from the database
75 cr.execute('SELECT name, id, state, demo AS dbdemo, latest_version AS installed_version'
76 ' FROM ir_module_module'
77 ' WHERE name IN %s',(tuple(additional_data),)
80 ## and we update the default values with values from the database
81 additional_data.update(dict([(x.pop('name'), x) for x in cr.dictfetchall()]))
83 for package in self.values():
84 for k, v in additional_data[package.name].items():
85 setattr(package, k, v)
87 def add_module(self, cr, module, force=None):
88 self.add_modules(cr, [module], force)
90 def add_modules(self, cr, module_list, force=None):
95 for module in module_list:
96 # This will raise an exception if no/unreadable descriptor file.
97 # NOTE The call to load_information_from_description_file is already
98 # done by db.initialize, so it is possible to not do it again here.
99 info = openerp.modules.module.load_information_from_description_file(module)
100 if info and info['installable']:
101 packages.append((module, info)) # TODO directly a dict, like in get_modules_with_version
103 _logger.warning('module %s: not installable, skipped', module)
105 dependencies = dict([(p, info['depends']) for p, info in packages])
106 current, later = set([p for p, info in packages]), set()
108 while packages and current > later:
109 package, info = packages[0]
110 deps = info['depends']
112 # if all dependencies of 'package' are already in the graph, add 'package' in the graph
113 if reduce(lambda x, y: x and y in self, deps, True):
114 if not package in current:
118 current.remove(package)
119 node = self.add_node(package, info)
121 for kind in ('init', 'demo', 'update'):
122 if package in tools.config[kind] or 'all' in tools.config[kind] or kind in force:
123 setattr(node, kind, True)
126 packages.append((package, info))
129 self.update_from_db(cr)
131 for package in later:
132 unmet_deps = filter(lambda p: p not in self, dependencies[package])
133 _logger.error('module %s: Unmet dependencies: %s', package, ', '.join(unmet_deps))
135 result = len(self) - len_graph
136 if result != len(module_list):
137 _logger.warning('Some modules were not loaded.')
143 done = set(self.keys())
145 level_modules = sorted((name, module) for name, module in self.items() if module.depth==level)
146 for name, module in level_modules:
152 class Singleton(object):
153 def __new__(cls, name, graph, info):
157 inst = object.__new__(cls)
164 class Node(Singleton):
165 """ One module in the modules dependency graph.
167 Node acts as a per-module singleton. A node is constructed via
168 Graph.add_module() or Graph.add_modules(). Some of its fields are from
169 ir_module_module (setted by Graph.update_from_db()).
173 def __init__(self, name, graph, info):
175 if not hasattr(self, 'children'):
177 if not hasattr(self, 'depth'):
180 def add_child(self, name, info):
181 node = Node(name, self.graph, info)
182 node.depth = self.depth + 1
183 if node not in self.children:
184 self.children.append(node)
185 for attr in ('init', 'update', 'demo'):
186 if hasattr(self, attr):
187 setattr(node, attr, True)
188 self.children.sort(lambda x, y: cmp(x.name, y.name))
191 def __setattr__(self, name, value):
192 super(Singleton, self).__setattr__(name, value)
193 if name in ('init', 'update', 'demo'):
194 tools.config[name][self.name] = 1
195 for child in self.children:
196 setattr(child, name, value)
198 for child in self.children:
199 setattr(child, name, value + 1)
202 return itertools.chain(iter(self.children), *map(iter, self.children))
205 return self._pprint()
207 def _pprint(self, depth=0):
208 s = '%s\n' % self.name
209 for c in self.children:
210 s += '%s`-> %s' % (' ' * depth, c._pprint(depth+1))
214 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: