|
| 1 | +# Copyright (C) 2024 - Today: NextERP Romania (https://nexterp.ro) |
| 2 | +# @author: Mihai Fekete (https://github.com/NextERP-Romania) |
1 | 3 | # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
2 | 4 |
|
| 5 | +import ast |
| 6 | +import json |
| 7 | +import lxml.etree as et |
| 8 | +import os |
| 9 | + |
3 | 10 | from odoo_module_migrate.base_migration_script import BaseMigrationScript
|
4 | 11 |
|
5 | 12 |
|
| 13 | +def add_asset_to_manifest(assets, manifest): |
| 14 | + """Add an asset to a manifest file.""" |
| 15 | + if "assets" not in manifest: |
| 16 | + manifest["assets"] = {} |
| 17 | + for asset_type, asset_files in assets.items(): |
| 18 | + if asset_type not in manifest["assets"]: |
| 19 | + manifest["assets"][asset_type] = [] |
| 20 | + manifest["assets"][asset_type].extend(asset_files) |
| 21 | + |
| 22 | + |
| 23 | +def remove_asset_file_from_manifest(file, manifest): |
| 24 | + """Remove asset file from manifest views.""" |
| 25 | + if "data" not in manifest: |
| 26 | + return |
| 27 | + for file_path in manifest["data"]: |
| 28 | + if file_path == file: |
| 29 | + manifest["data"].remove(file) |
| 30 | + |
| 31 | + |
| 32 | +def remove_node_from_xml(record_node, node): |
| 33 | + """Remove a node from an XML tree.""" |
| 34 | + to_remove = True |
| 35 | + if node.getchildren(): |
| 36 | + to_remove = False |
| 37 | + if to_remove: |
| 38 | + parent = node.getparent() if node.getparent() is not None else record_node |
| 39 | + parent.remove(node) |
| 40 | + |
| 41 | + |
| 42 | +def reformat_assets_definition( |
| 43 | + logger, module_path, module_name, manifest_path, migration_steps, tools |
| 44 | +): |
| 45 | + """Reformat assets declaration in XML files.""" |
| 46 | + |
| 47 | + manifest = tools._get_manifest_dict(manifest_path) |
| 48 | + parser = et.XMLParser(remove_blank_text=True) |
| 49 | + assets_views = [ |
| 50 | + "web.assets_backend", |
| 51 | + "web.assets_common", |
| 52 | + "web.assets_frontend", |
| 53 | + "web.assets_qweb", |
| 54 | + "web.assets_tests", |
| 55 | + "website.assets_frontend", |
| 56 | + "website.assets_editor", |
| 57 | + "website.assets_frontend_editor", |
| 58 | + "website.assets_wysiwyg", |
| 59 | + "web_enterprise.assets_backend", |
| 60 | + "web_enterprise.assets_common", |
| 61 | + "web_enterprise._assets_backend_helpers", |
| 62 | + ] |
| 63 | + for file_path in manifest.get("data", []): |
| 64 | + if not file_path.endswith(".xml"): |
| 65 | + continue |
| 66 | + xml_file = open(os.path.join(module_path, file_path), "r") |
| 67 | + tree = et.parse(xml_file, parser) |
| 68 | + record_node = tree.getroot() |
| 69 | + for node in record_node.getchildren(): |
| 70 | + if node.get("inherit_id") in assets_views: |
| 71 | + for xpath_elem in node.xpath("xpath[@expr]"): |
| 72 | + for file in xpath_elem.getchildren(): |
| 73 | + elem_file_path = False |
| 74 | + if file.get("src"): |
| 75 | + elem_file_path = ["".join(file.get("src"))] |
| 76 | + elif file.get("href"): |
| 77 | + elem_file_path = ["".join(file.get("href"))] |
| 78 | + if elem_file_path: |
| 79 | + add_asset_to_manifest( |
| 80 | + {node.get("inherit_id"): elem_file_path}, |
| 81 | + manifest, |
| 82 | + ) |
| 83 | + remove_node_from_xml(record_node, file) |
| 84 | + remove_node_from_xml(record_node, xpath_elem) |
| 85 | + remove_node_from_xml(record_node, node) |
| 86 | + # write back the node to the XML file |
| 87 | + with open(os.path.join(module_path, file_path), "wb") as f: |
| 88 | + et.indent(tree) |
| 89 | + tree.write(f, encoding="utf-8", xml_declaration=True) |
| 90 | + if not record_node.getchildren(): |
| 91 | + remove_asset_file_from_manifest(file_path, manifest) |
| 92 | + os.remove(os.path.join(module_path, file_path)) |
| 93 | + manifest_content = json.dumps(manifest, indent=4, default=str) |
| 94 | + manifest_content = manifest_content.replace(": true,", ": True,").replace( |
| 95 | + ": false,", ": False," |
| 96 | + ) |
| 97 | + tools._write_content(manifest_path, manifest_content) |
| 98 | + |
| 99 | + |
6 | 100 | class MigrationScript(BaseMigrationScript):
|
7 |
| - pass |
| 101 | + |
| 102 | + _GLOBAL_FUNCTIONS = [reformat_assets_definition] |
0 commit comments