~cjohnston/loco-team-portal/722565

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python

from django.core.management.base import LabelCommand

import subprocess
import tempfile
import sys
import os
import re

import settings

try:
    APP_NAME = settings.LP_PROJECT_NAME
except AttributeError:
    # if you prefer not to set LP_PROJECT_NAME in settings.py, define here
    APP_NAME = "loco-directory"


def import_pofile(pofile):
    locale = re.findall("^%s-(.*).po$" % APP_NAME, os.path.basename(pofile))
    if not locale:
        return False
    project_locale_path = os.path.join(settings.PROJECT_PATH, "locale", locale[0])
    if not os.path.exists(project_locale_path) or \
       not os.path.isdir(project_locale_path):
        pwd = os.getcwd()
        os.chdir(settings.PROJECT_PATH)
        print settings.PROJECT_PATH
        subprocess.call(["./manage.py", "makemessages", "-l", locale[0]])
        os.chdir(pwd)
    subprocess.call(["cp", pofile, 
                     os.path.join(project_locale_path, 
                                  "LC_MESSAGES/django.po")])

class Command(LabelCommand):
    help = "Import translations from a translations tarball, that was downloaded from Launchpad."

    def handle_label(self, label, **options):
        if not os.path.exists(label):
            print >> sys.stderr, "File not found: %s" % label
            sys.exit(1)
        if not label.endswith(".tar.gz") and not label.endswith(".tgz"):
            print >> sys.stderr, "File is not a .tar.gz file: %s" % label
            sys.exit(1)

        pwd = os.getcwd()
        dir = tempfile.mkdtemp(APP_NAME)
        subprocess.call(["cp", label, dir])
        os.chdir(dir)
        subprocess.call(["tar", "xfz", os.path.basename(label)])
        
        for dirpath, dirnames, filenames in os.walk(dir):
            pofiles = map(lambda a: os.path.join(dirpath, a), 
                          filter(lambda a: a.endswith(".po"), filenames))
            for pofile in pofiles:
                import_pofile(pofile)
        
        os.chdir(pwd)
        subprocess.call(["rm", "-r", dir])
        print "Make sure, you 'bzr add' all the new translations."