Coverage for app/venv/lib/python3.14/site-packages/weblate/addons/scripts.py: 0%
39 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5from __future__ import annotations
7import os
8from typing import TYPE_CHECKING
10from weblate.addons.base import BaseAddon
11from weblate.utils.render import render_template
12from weblate.utils.site import get_site_url
14if TYPE_CHECKING:
15 from weblate.trans.models import Component, Translation
18class BaseScriptAddon(BaseAddon):
19 """Base class for script executing addons."""
21 icon = "script.svg"
22 script: str
23 add_file: str | None = None
24 alert = "AddonScriptError"
26 def run_script(self, component=None, translation=None, env=None) -> None:
27 command = [self.script]
28 if translation:
29 component = translation.component
30 command.append(translation.get_filename())
31 target = component.linked_component if component.is_repo_link else component
32 environment = {
33 "WL_VCS": target.vcs,
34 "WL_REPO": target.repo,
35 "WL_PATH": target.full_path,
36 "WL_FILEMASK": component.filemask,
37 "WL_TEMPLATE": component.template,
38 "WL_NEW_BASE": component.new_base,
39 "WL_FILE_FORMAT": component.file_format,
40 "WL_BRANCH": component.branch,
41 "WL_COMPONENT_SLUG": component.slug,
42 "WL_PROJECT_SLUG": component.project.slug,
43 "WL_COMPONENT_NAME": component.name,
44 "WL_PROJECT_NAME": component.project.name,
45 "WL_COMPONENT_URL": get_site_url(component.get_absolute_url()),
46 "WL_ENGAGE_URL": component.get_share_url(),
47 }
48 if translation:
49 environment["WL_LANGUAGE"] = translation.language_code
50 if env is not None:
51 environment.update(env)
52 self.execute_process(component, command, environment)
53 self.trigger_alerts(component)
55 def post_push(self, component, activity_log_id: int | None = None) -> None:
56 self.run_script(component)
58 def post_update(
59 self,
60 component,
61 previous_head: str,
62 skip_push: bool,
63 activity_log_id: int | None = None,
64 ) -> None:
65 self.run_script(component, env={"WL_PREVIOUS_HEAD": previous_head})
67 def post_commit(
68 self, component: Component, store_hash: bool, activity_log_id: int | None = None
69 ) -> None:
70 self.run_script(component=component)
72 def pre_commit(
73 self,
74 translation: Translation,
75 author: str,
76 store_hash: bool,
77 activity_log_id: int | None = None,
78 ) -> None:
79 self.run_script(translation=translation)
81 if self.add_file:
82 filename = os.path.join(
83 translation.component.full_path,
84 render_template(self.add_file, translation=translation),
85 )
86 translation.addon_commit_files.append(filename)
88 def post_add(self, translation, activity_log_id: int | None = None) -> None:
89 self.run_script(translation=translation)