graph.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """ Modules dependency graph. """
  4. import itertools
  5. import logging
  6. import odoo
  7. import odoo.tools as tools
  8. _logger = logging.getLogger(__name__)
  9. class Graph(dict):
  10. """ Modules dependency graph.
  11. The graph is a mapping from module name to Nodes.
  12. """
  13. def add_node(self, name, info):
  14. max_depth, father = 0, None
  15. for d in info['depends']:
  16. n = self.get(d) or Node(d, self, None) # lazy creation, do not use default value for get()
  17. if n.depth >= max_depth:
  18. father = n
  19. max_depth = n.depth
  20. if father:
  21. return father.add_child(name, info)
  22. else:
  23. return Node(name, self, info)
  24. def update_from_db(self, cr):
  25. if not len(self):
  26. return
  27. # update the graph with values from the database (if exist)
  28. ## First, we set the default values for each package in graph
  29. additional_data = {key: {'id': 0, 'state': 'uninstalled', 'dbdemo': False, 'installed_version': None} for key in self.keys()}
  30. ## Then we get the values from the database
  31. cr.execute('SELECT name, id, state, demo AS dbdemo, latest_version AS installed_version'
  32. ' FROM ir_module_module'
  33. ' WHERE name IN %s',(tuple(additional_data),)
  34. )
  35. ## and we update the default values with values from the database
  36. additional_data.update((x['name'], x) for x in cr.dictfetchall())
  37. for package in self.values():
  38. for k, v in additional_data[package.name].items():
  39. setattr(package, k, v)
  40. def add_module(self, cr, module, force=None):
  41. self.add_modules(cr, [module], force)
  42. def add_modules(self, cr, module_list, force=None):
  43. if force is None:
  44. force = []
  45. packages = []
  46. len_graph = len(self)
  47. for module in module_list:
  48. info = odoo.modules.module.get_manifest(module)
  49. if info and info['installable']:
  50. packages.append((module, info)) # TODO directly a dict, like in get_modules_with_version
  51. elif module != 'studio_customization':
  52. _logger.warning('module %s: not installable, skipped', module)
  53. dependencies = dict([(p, info['depends']) for p, info in packages])
  54. current, later = set([p for p, info in packages]), set()
  55. while packages and current > later:
  56. package, info = packages[0]
  57. deps = info['depends']
  58. # if all dependencies of 'package' are already in the graph, add 'package' in the graph
  59. if all(dep in self for dep in deps):
  60. if not package in current:
  61. packages.pop(0)
  62. continue
  63. later.clear()
  64. current.remove(package)
  65. node = self.add_node(package, info)
  66. for kind in ('init', 'demo', 'update'):
  67. if package in tools.config[kind] or 'all' in tools.config[kind] or kind in force:
  68. setattr(node, kind, True)
  69. else:
  70. later.add(package)
  71. packages.append((package, info))
  72. packages.pop(0)
  73. self.update_from_db(cr)
  74. for package in later:
  75. unmet_deps = [p for p in dependencies[package] if p not in self]
  76. _logger.info('module %s: Unmet dependencies: %s', package, ', '.join(unmet_deps))
  77. return len(self) - len_graph
  78. def __iter__(self):
  79. level = 0
  80. done = set(self.keys())
  81. while done:
  82. level_modules = sorted((name, module) for name, module in self.items() if module.depth==level)
  83. for name, module in level_modules:
  84. done.remove(name)
  85. yield module
  86. level += 1
  87. def __str__(self):
  88. return '\n'.join(str(n) for n in self if n.depth == 0)
  89. class Node(object):
  90. """ One module in the modules dependency graph.
  91. Node acts as a per-module singleton. A node is constructed via
  92. Graph.add_module() or Graph.add_modules(). Some of its fields are from
  93. ir_module_module (set by Graph.update_from_db()).
  94. """
  95. def __new__(cls, name, graph, info):
  96. if name in graph:
  97. inst = graph[name]
  98. else:
  99. inst = object.__new__(cls)
  100. graph[name] = inst
  101. return inst
  102. def __init__(self, name, graph, info):
  103. self.name = name
  104. self.graph = graph
  105. self.info = info or getattr(self, 'info', {})
  106. if not hasattr(self, 'children'):
  107. self.children = []
  108. if not hasattr(self, 'depth'):
  109. self.depth = 0
  110. @property
  111. def data(self):
  112. return self.info
  113. def add_child(self, name, info):
  114. node = Node(name, self.graph, info)
  115. node.depth = self.depth + 1
  116. if node not in self.children:
  117. self.children.append(node)
  118. for attr in ('init', 'update', 'demo'):
  119. if hasattr(self, attr):
  120. setattr(node, attr, True)
  121. self.children.sort(key=lambda x: x.name)
  122. return node
  123. def __setattr__(self, name, value):
  124. super(Node, self).__setattr__(name, value)
  125. if name in ('init', 'update', 'demo'):
  126. tools.config[name][self.name] = 1
  127. for child in self.children:
  128. setattr(child, name, value)
  129. if name == 'depth':
  130. for child in self.children:
  131. setattr(child, name, value + 1)
  132. def __iter__(self):
  133. return itertools.chain(
  134. self.children,
  135. itertools.chain.from_iterable(self.children)
  136. )
  137. def __str__(self):
  138. return self._pprint()
  139. def _pprint(self, depth=0):
  140. s = '%s\n' % self.name
  141. for c in self.children:
  142. s += '%s`-> %s' % (' ' * depth, c._pprint(depth+1))
  143. return s
  144. def should_have_demo(self):
  145. return (hasattr(self, 'demo') or (self.dbdemo and self.state != 'installed')) and all(p.dbdemo for p in self.parents)
  146. @property
  147. def parents(self):
  148. if self.depth == 0:
  149. return []
  150. return (
  151. node for node in self.graph.values()
  152. if node.depth < self.depth
  153. if self in node.children
  154. )