Commit bcb687d1 authored by Michal Čihař's avatar Michal Čihař

Move hooks to separate module

Fixes #804
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent fc2cb11b
...@@ -37,6 +37,13 @@ from weblate.trans.models.dictionary import Dictionary ...@@ -37,6 +37,13 @@ from weblate.trans.models.dictionary import Dictionary
from weblate.trans.models.source import Source from weblate.trans.models.source import Source
from weblate.trans.models.advertisement import Advertisement from weblate.trans.models.advertisement import Advertisement
from weblate.trans.models.whiteboard import WhiteboardMessage from weblate.trans.models.whiteboard import WhiteboardMessage
from weblate.trans.signals import (
vcs_post_push, vcs_post_update, vcs_pre_commit, vcs_post_commit,
)
from weblate.trans.scripts import (
run_post_push_script, run_post_update_script, run_pre_commit_script,
run_post_commit_script,
)
__all__ = [ __all__ = [
'Project', 'SubProject', 'Translation', 'Unit', 'Check', 'Suggestion', 'Project', 'SubProject', 'Translation', 'Unit', 'Check', 'Suggestion',
...@@ -197,3 +204,23 @@ def cleanup_deleted(sender, instance, **kwargs): ...@@ -197,3 +204,23 @@ def cleanup_deleted(sender, instance, **kwargs):
language=None, language=None,
contentsum=contentsum contentsum=contentsum
).delete() ).delete()
@receiver(vcs_post_push)
def post_push(sender, component, **kwargs):
run_post_push_script(component)
@receiver(vcs_post_update)
def post_update(sender, component, **kwargs):
run_post_update_script(component)
@receiver(vcs_pre_commit)
def pre_commit(sender, translation, **kwargs):
run_pre_commit_script(translation.subproject, translation.get_filename())
@receiver(vcs_post_commit)
def post_commit(sender, translation, **kwargs):
run_post_commit_script(translation.subproject, translation.get_filename())
...@@ -29,14 +29,13 @@ from django.utils import timezone ...@@ -29,14 +29,13 @@ from django.utils import timezone
from glob import glob from glob import glob
import os import os
import time import time
import subprocess
import fnmatch import fnmatch
import re import re
from weblate.trans.formats import FILE_FORMAT_CHOICES, FILE_FORMATS from weblate.trans.formats import FILE_FORMAT_CHOICES, FILE_FORMATS
from weblate.trans.mixins import PercentMixin, URLMixin, PathMixin from weblate.trans.mixins import PercentMixin, URLMixin, PathMixin
from weblate.trans.filelock import FileLock from weblate.trans.filelock import FileLock
from weblate.trans.util import ( from weblate.trans.util import (
is_repo_link, get_site_url, cleanup_repo_url, get_clean_env, cleanup_path, is_repo_link, get_site_url, cleanup_repo_url, cleanup_path,
) )
from weblate.trans.signals import vcs_post_push, vcs_post_update from weblate.trans.signals import vcs_post_push, vcs_post_update
from weblate.trans.vcs import RepositoryException, VCS_REGISTRY, VCS_CHOICES from weblate.trans.vcs import RepositoryException, VCS_REGISTRY, VCS_CHOICES
...@@ -693,7 +692,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -693,7 +692,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
ret = self.update_branch(request, method=method) ret = self.update_branch(request, method=method)
# run post update hook # run post update hook
self.run_hook(self.post_update_script)
vcs_post_update.send(sender=self.__class__, subproject=self) vcs_post_update.send(sender=self.__class__, subproject=self)
# create translation objects for all files # create translation objects for all files
...@@ -752,7 +750,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -752,7 +750,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
subproject=self, subproject=self,
) )
self.run_hook(self.post_push_script)
vcs_post_push.send(sender=self.__class__, subproject=self) vcs_post_push.send(sender=self.__class__, subproject=self)
return True return True
...@@ -1487,51 +1484,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -1487,51 +1484,6 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
action=Change.ACTION_UNLOCK, action=Change.ACTION_UNLOCK,
) )
def run_pre_commit_script(self, filename):
"""
Pre commit hook
"""
self.run_hook(self.pre_commit_script, filename)
def run_post_commit_script(self, filename):
"""
Post commit hook
"""
self.run_hook(self.post_commit_script, filename)
def run_hook(self, script, *args):
"""
Generic script hook executor.
"""
if script:
command = [script]
if args:
command.extend(args)
environment = get_clean_env()
if self.is_repo_link:
target = self.linked_subproject
else:
target = self
environment['WL_VCS'] = target.vcs
environment['WL_REPO'] = target.repo
environment['WL_PATH'] = target.get_path()
environment['WL_FILEMASK'] = self.filemask
environment['WL_FILE_FORMAT'] = self.file_format
try:
subprocess.check_call(
command,
env=environment,
cwd=self.get_path(),
)
return True
except (OSError, subprocess.CalledProcessError) as err:
self.log_error(
'failed to run hook script %s: %s',
script,
err
)
return False
def get_editable_template(self): def get_editable_template(self):
if not self.edit_template or not self.has_template(): if not self.edit_template or not self.has_template():
return None return None
......
...@@ -755,7 +755,6 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin): ...@@ -755,7 +755,6 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
msg = self.get_commit_message() msg = self.get_commit_message()
# Pre commit hook # Pre commit hook
self.subproject.run_pre_commit_script(self.get_filename())
vcs_pre_commit.send(sender=self.__class__, translation=self) vcs_pre_commit.send(sender=self.__class__, translation=self)
# Create list of files to commit # Create list of files to commit
...@@ -777,7 +776,6 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin): ...@@ -777,7 +776,6 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
) )
# Post commit hook # Post commit hook
self.subproject.run_post_commit_script(self.get_filename())
vcs_post_commit.send(sender=self.__class__, translation=self) vcs_post_commit.send(sender=self.__class__, translation=self)
# Optionally store updated hash # Optionally store updated hash
......
...@@ -18,8 +18,11 @@ ...@@ -18,8 +18,11 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
"""Hook scripts handling"""
import os.path import os.path
import subprocess
from weblate.trans.util import get_clean_env
def get_script_name(name): def get_script_name(name):
...@@ -28,3 +31,61 @@ def get_script_name(name): ...@@ -28,3 +31,61 @@ def get_script_name(name):
parameters. parameters.
''' '''
return os.path.basename(name).split()[0] return os.path.basename(name).split()[0]
def run_post_push_script(component):
"""Run post push hook"""
run_hook(component, component.post_push_script)
def run_post_update_script(component):
"""Run post update hook"""
run_hook(component, component.post_update_script)
def run_pre_commit_script(component, filename):
"""
Pre commit hook
"""
run_hook(component, component.pre_commit_script, filename)
def run_post_commit_script(component, filename):
"""
Post commit hook
"""
run_hook(component, component.post_commit_script, filename)
def run_hook(component, script, *args):
"""
Generic script hook executor.
"""
if script:
command = [script]
if args:
command.extend(args)
environment = get_clean_env()
if component.is_repo_link:
target = component.linked_subproject
else:
target = component
environment['WL_VCS'] = target.vcs
environment['WL_REPO'] = target.repo
environment['WL_PATH'] = target.get_path()
environment['WL_FILEMASK'] = component.filemask
environment['WL_FILE_FORMAT'] = component.file_format
try:
subprocess.check_call(
command,
env=environment,
cwd=component.get_path(),
)
return True
except (OSError, subprocess.CalledProcessError) as err:
component.log_error(
'failed to run hook script %s: %s',
script,
err
)
return False
...@@ -680,15 +680,6 @@ class SubProjectTest(RepoTestCase): ...@@ -680,15 +680,6 @@ class SubProjectTest(RepoTestCase):
component.save() component.save()
self.assertEqual(component.translation_set.count(), 4) self.assertEqual(component.translation_set.count(), 4)
def test_run_hook(self):
subproject = self.create_subproject()
self.assertFalse(
subproject.run_hook('false')
)
self.assertTrue(
subproject.run_hook('true')
)
def test_lang_code(self): def test_lang_code(self):
subproject = SubProject() subproject = SubProject()
subproject.filemask = 'Solution/Project/Resources.*.resx' subproject.filemask = 'Solution/Project/Resources.*.resx'
......
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2015 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""Tests for hook scripts """
from weblate.trans.tests.test_models import RepoTestCase
from weblate.trans.scripts import run_hook
class ScriptTest(RepoTestCase):
def test_run_hook(self):
subproject = self.create_subproject()
self.assertFalse(
run_hook(subproject, 'false')
)
self.assertTrue(
run_hook(subproject, 'true')
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment