Skip to content

Commit 72e2a0b

Browse files
committed
[v14] - [v15] Add assets definition in manifest.
1 parent 38e7a02 commit 72e2a0b

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed
Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,102 @@
1+
# Copyright (C) 2024 - Today: NextERP Romania (https://nexterp.ro)
2+
# @author: Mihai Fekete (https://github.com/NextERP-Romania)
13
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
24

5+
import ast
6+
import json
7+
import lxml.etree as et
8+
import os
9+
310
from odoo_module_migrate.base_migration_script import BaseMigrationScript
411

512

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+
6100
class MigrationScript(BaseMigrationScript):
7-
pass
101+
102+
_GLOBAL_FUNCTIONS = [reformat_assets_definition]

odoo_module_migrate/tools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
33
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
44

5+
import json
56
import subprocess
67
import re
78
import pathlib
@@ -79,3 +80,13 @@ def get_files(module_path, extensions):
7980
file_paths.extend(module_dir.rglob(f"*{ext}"))
8081

8182
return file_paths
83+
84+
85+
def _get_manifest_dict(manifest_path):
86+
"""Load the module manifest from the file system."""
87+
manifest = {}
88+
if not manifest_path:
89+
return {}
90+
with open(manifest_path, mode="r") as f:
91+
manifest.update(ast.literal_eval(f.read()))
92+
return manifest

0 commit comments

Comments
 (0)