|
| 1 | +import ast |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | + |
| 5 | +from manifestoo_core.core_addons import is_core_addon |
| 6 | +from manifestoo_core.odoo_series import OdooSeries, UnsupportedOdooSeries |
| 7 | + |
| 8 | +from odoo import release |
| 9 | +from odoo.modules import get_module_path |
| 10 | +from odoo.modules.module import MANIFEST_NAMES |
| 11 | +from odoo.tools.cloc import DEFAULT_EXCLUDE, MAX_FILE_SIZE, VALID_EXTENSION, Cloc |
| 12 | + |
| 13 | +DEFAULT_EXCLUDE_INCLUDE_TESTS = [ |
| 14 | + p for p in DEFAULT_EXCLUDE if not p.startswith(("tests/", "static/tests/")) |
| 15 | +] |
| 16 | + |
| 17 | + |
| 18 | +class CustomCloc(Cloc): |
| 19 | + def summary(self): |
| 20 | + return { |
| 21 | + "code": dict(self.code), |
| 22 | + "errors": {m: dict(files) for m, files in self.errors.items()}, |
| 23 | + } |
| 24 | + |
| 25 | + def count_modules(self, env): |
| 26 | + try: |
| 27 | + major, minor = release.version_info[:2] |
| 28 | + series = OdooSeries(f"{major}.{minor}") |
| 29 | + except (ValueError, UnsupportedOdooSeries): |
| 30 | + series = None |
| 31 | + |
| 32 | + domain = [("state", "=", "installed")] |
| 33 | + if env["ir.module.module"]._fields.get("imported"): |
| 34 | + domain.append(("imported", "=", False)) |
| 35 | + module_list = env["ir.module.module"].search(domain).mapped("name") |
| 36 | + |
| 37 | + for module_name in module_list: |
| 38 | + if series and is_core_addon(module_name, series): |
| 39 | + continue |
| 40 | + module_path = os.path.realpath(get_module_path(module_name)) |
| 41 | + if module_path: |
| 42 | + self.count_path(module_path) |
| 43 | + |
| 44 | + # Exact copy of odoo.tools.cloc.Cloc.count_path with DEFAULT_EXCLUDE → DEFAULT_EXCLUDE_INCLUDE_TESTS |
| 45 | + |
| 46 | + # fmt: off |
| 47 | + # pylint: disable=broad-except,except-pass |
| 48 | + # ruff: noqa: E501 |
| 49 | + def count_path(self, path, exclude=None): |
| 50 | + path = path.rstrip('/') |
| 51 | + exclude_list = [] |
| 52 | + for i in MANIFEST_NAMES: |
| 53 | + manifest_path = os.path.join(path, i) |
| 54 | + try: |
| 55 | + with open(manifest_path, 'rb') as manifest: |
| 56 | + exclude_list.extend(DEFAULT_EXCLUDE_INCLUDE_TESTS) |
| 57 | + d = ast.literal_eval(manifest.read().decode('latin1')) |
| 58 | + for j in ['cloc_exclude', 'demo', 'demo_xml']: |
| 59 | + exclude_list.extend(d.get(j, [])) |
| 60 | + break |
| 61 | + except Exception: |
| 62 | + pass |
| 63 | + if not exclude: |
| 64 | + exclude = set() |
| 65 | + for i in filter(None, exclude_list): |
| 66 | + assert '..' not in i, ( |
| 67 | + f"Invalid exclusion path '{i}': '..' is not allowed. Use a normalized path." |
| 68 | + ) |
| 69 | + exclude.update(str(p) for p in pathlib.Path(path).glob(i)) |
| 70 | + |
| 71 | + module_name = os.path.basename(path) |
| 72 | + self.book(module_name) |
| 73 | + for root, _dirs, files in os.walk(path): |
| 74 | + for file_name in files: |
| 75 | + file_path = os.path.join(root, file_name) |
| 76 | + |
| 77 | + if file_path in exclude: |
| 78 | + continue |
| 79 | + |
| 80 | + ext = os.path.splitext(file_path)[1].lower() |
| 81 | + if ext not in VALID_EXTENSION: |
| 82 | + continue |
| 83 | + |
| 84 | + if os.path.getsize(file_path) > MAX_FILE_SIZE: |
| 85 | + self.book(module_name, file_path, (-1, "Max file size exceeded")) |
| 86 | + continue |
| 87 | + |
| 88 | + with open(file_path, 'rb') as f: |
| 89 | + # Decode using latin1 to avoid error that may raise by decoding with utf8 |
| 90 | + # The chars not correctly decoded in latin1 have no impact on how many lines will be counted |
| 91 | + content = f.read().decode('latin1') |
| 92 | + self.book(module_name, file_path, self.parse(content, ext)) |
| 93 | + # fmt: on |
0 commit comments