save-suggestions.py 3.71 KB
Newer Older
1
#!/usr/bin/env python3
2
3
4
5
6
7
8

# This script generates a temporary copy of all translations including suggestions.
# TMPDIR - the directory where the temporary copy is located.
# We have created TMPDIR manually, by git cloning locally.
#
# Usage:
#
9
10
11
#   export DJANGO_SETTINGS_MODULE=weblate.settings
#   export DJANGO_IS_MANAGEMENT_COMMAND=1
#   /var/lib/weblate/scripts/save-suggestions.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25

import logging
import logging.config

import subprocess
import sys

sys.path.append("/usr/local/share/weblate")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

from weblate.trans.models import *

intrigeri's avatar
Lint.    
intrigeri committed
26
TMPDIR = "/var/lib/weblate/repositories/vcs/staging/"
27

28
logging.config.fileConfig('/var/lib/weblate/config/saveSuggestions.conf')
29
30
31

logger = logging.getLogger('saveSuggestions')

32
33
logger.info("Start a new run.")

intrigeri's avatar
Lint.    
intrigeri committed
34

groente's avatar
groente committed
35
def subprocessOutputToLogger(cmd, **kwargs):
intrigeri's avatar
Lint.    
intrigeri committed
36
    logger.info("Running command '%s':", " ".join(cmd))
37
    popen = subprocess.Popen(cmd, universal_newlines=True,
intrigeri's avatar
Lint.    
intrigeri committed
38
39
40
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             **kwargs)
groente's avatar
groente committed
41
42
43
    for stdout_line in iter(popen.stdout.readline, ""):
        logger.debug(stdout_line.strip())
    popen.stdout.close()
groente's avatar
groente committed
44
45
46
    returncode = popen.wait()
    if returncode:
        raise subprocess.CalledProcessError(returncode, cmd)
groente's avatar
groente committed
47

intrigeri's avatar
Lint.    
intrigeri committed
48

49
50
51
52
53
54
55
56
57
58
59
60
def update_unit(translation, unit, target):
    # weblate/trans/models/translation.py: update_units
    src = unit.get_source_plurals()[0]
    add = False

    pounit, add = translation.store.find_unit(unit.context, src)

    # Bail out if we have not found anything
    if pounit is None or pounit.is_obsolete():
        logger.warning('message %s disappeared!', unit)

    pounit.set_target(target)     # update po file with first suggestion
61
    pounit.mark_fuzzy(False)           # mark change as non fuzzy
62
63


64
def commandline():
65
    try:
66
67
68
69
        subprocessOutputToLogger(["git", "clean", "-fd"], cwd=TMPDIR)
        subprocessOutputToLogger(["git", "fetch"], cwd=TMPDIR)
        subprocessOutputToLogger(["git", "reset", 'FETCH_HEAD', "--hard"],
                                 cwd=TMPDIR)
70
    except:
71
        logger.exception("Got an exception")
72
73
        raise

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    logger.info("Start search for suggestions.")
    len_subprojects = len(Component.objects.all())
    subprojects = list(Component.objects.all())


    for i in range(len_subprojects):
        subproject = subprojects[i]
        try:
            for translation in subproject.translation_set.all():
                changed = False
                for unit in translation.unit_set.all():
                    if unit.suggestions:
                        # Get newest most voted suggestion
                        date_sorted = sorted(unit.suggestions, key=lambda i: i.timestamp, reverse=True)
                        s = sorted(date_sorted, key=lambda i: i.get_num_votes(), reverse=True)[0]
                        logger.debug("found suggestion for %s", unit)
                        update_unit(translation, unit, s.target)
                        changed = True
                    elif unit.pending:
                        # Save uncommitted changes
                        logger.debug("uncommitted changes found for %s", unit)
                        update_unit(translation, unit, unit.target)
                        changed = True

                # save with suggestions
                if changed:
                    with open(TMPDIR+"/"+translation.filename, "wb") as f:
                        translation.store.store.serialize(f)
        except:
            logger.exception("Got an exception for %s(%i)", subproject.name, i)
            raise

    logger.info("Successfully updated %s.", TMPDIR)

if __name__ == "__main__":
    commandline()