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

# 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:
#
#   DJANGO_SETTINGS_MODULE=weblate.settings
#   DJANGO_IS_MANAGEMENT_COMMAND=1
11
#   python /var/lib/weblate/scripts/save-suggestions.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

import logging
import logging.config

import os
import shutil
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 *

TMPDIR="/var/lib/weblate/repositories/vcs/staging/"

30
logging.config.fileConfig('/var/lib/weblate/config/saveSuggestions.conf')
31
32
33

logger = logging.getLogger('saveSuggestions')

34
35
logger.info("Start a new run.")

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

49
try:
50
    subprocessOutputToLogger(["git","clean","-fd"], cwd=TMPDIR)
groente's avatar
groente committed
51
52
    subprocessOutputToLogger(["git","fetch"], cwd=TMPDIR)
    subprocessOutputToLogger(["git","reset",'FETCH_HEAD',"--hard"], cwd=TMPDIR)
53
54
55
56
except:
    logger.exception("Got an exception")
    raise

groente's avatar
groente committed
57
logger.info("Start search for suggestions.")
58
59
60
61
62
63
64
len_subprojects = len(SubProject.objects.all())
subprojects = list(SubProject.objects.all())
for i in range(len_subprojects):
    subproject = subprojects[i]
    try:
        for translation in subproject.translation_set.all():
            for unit in translation.unit_set.all():
65
66
67
                if unit.suggestions:
                    s = list(unit.suggestions)[-1]
                    logger.debug("found suggestion for %s", unit)
68
                    # weblate/trans/models/translations.py: update_units
69
70
71
72
73
74
75
76
77
78
                    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)
                        continue

79
80
81
82
83
84
85
86
87
88
                    pounit.set_target(s.target)     # update po file with first suggestion
            # save with suggestions
            with open(TMPDIR+"/"+translation.filename, "wb") as f:
                translation.store.store.serialize(f)
            # now: build the wiki :)
    except:
        logger.exception("Got an exception for %s(%i)", subproject.name, i)
        raise

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