diff --git a/odoo/addons/openupgrade_records/models/openupgrade_record.py b/odoo/addons/openupgrade_records/models/openupgrade_record.py index d362d2d2039..30a112763a1 100644 --- a/odoo/addons/openupgrade_records/models/openupgrade_record.py +++ b/odoo/addons/openupgrade_records/models/openupgrade_record.py @@ -1,9 +1,12 @@ -# coding: utf-8 # Copyright 2011-2015 Therp BV -# Copyright 2016 Opener B.V. +# Copyright 2016-2020 Opener B.V. +# Copyright 2019 Eficent # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). - +import ast +import os from odoo import api, fields, models +from odoo.exceptions import ValidationError +from odoo.modules.module import get_module_path, MANIFEST_NAMES class Attribute(models.Model): @@ -111,3 +114,43 @@ def field_dump(self): dict([(x.name, x.value) for x in record.attribute_ids])) data.append(repre) return data + + @api.model + def list_modules(self): + """ Return the set of covered modules """ + self.env.cr.execute( + """SELECT DISTINCT(module) FROM openupgrade_record + ORDER BY module""") + return [module for module, in self.env.cr.fetchall()] + + @staticmethod + def _read_manifest(addon_dir): + for manifest_name in MANIFEST_NAMES: + if os.access(os.path.join(addon_dir, manifest_name), os.R_OK): + with open(os.path.join(addon_dir, manifest_name), 'r') as f: + manifest_string = f.read() + return ast.literal_eval(manifest_string) + raise ValidationError('No manifest found in %s' % addon_dir) + + @api.model + def get_xml_records(self, module): + """ Return all XML records from the given module """ + addon_dir = get_module_path(module) + manifest = self._read_manifest(addon_dir) + # The order of the keys are important. + # Load files in the same order as in + # module/loading.py:load_module_graph + files = [] + for key in ['init_xml', 'update_xml', 'data']: + if not manifest.get(key): + continue + for xml_file in manifest[key]: + if not xml_file.lower().endswith('.xml'): + continue + parts = xml_file.split('/') + try: + with open(os.path.join(addon_dir, *parts), 'r') as xml_handle: + files.append(xml_handle.read()) + except UnicodeDecodeError: + continue + return files