~mhall119/ubuntu-website/django-foundations

« back to all changes in this revision

Viewing changes to src/launchpad/management/commands/import-translations.py

  • Committer: Michael Hall
  • Date: 2010-10-31 21:16:39 UTC
  • Revision ID: mhall119@gmail.com-20101031211639-ai39d8vdgz4rv42i
Begin incorporating pieces of loco-directory into base apps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
from django.core.management.base import LabelCommand
 
4
 
 
5
import subprocess
 
6
import tempfile
 
7
import sys
 
8
import os
 
9
import re
 
10
 
 
11
from django.conf import settings
 
12
 
 
13
try:
 
14
    APP_NAME = settings.LP_PROJECT_NAME
 
15
except AttributeError:
 
16
    # if you prefer not to set LP_PROJECT_NAME in settings.py, define here
 
17
    APP_NAME = "loco-directory"
 
18
 
 
19
 
 
20
def import_pofile(pofile):
 
21
    locale = re.findall("^%s-(.*).po$" % APP_NAME, os.path.basename(pofile))
 
22
    if not locale:
 
23
        return False
 
24
    project_locale_path = os.path.join(settings.PROJECT_PATH, "locale", locale[0])
 
25
    if not os.path.exists(project_locale_path) or \
 
26
       not os.path.isdir(project_locale_path):
 
27
        pwd = os.getcwd()
 
28
        os.chdir(settings.PROJECT_PATH)
 
29
        print settings.PROJECT_PATH
 
30
        subprocess.call(["./manage.py", "makemessages", "-l", locale[0]])
 
31
        os.chdir(pwd)
 
32
    subprocess.call(["cp", pofile, 
 
33
                     os.path.join(project_locale_path, 
 
34
                                  "LC_MESSAGES/django.po")])
 
35
 
 
36
class Command(LabelCommand):
 
37
    help = "Import translations from a translations tarball, that was downloaded from Launchpad."
 
38
 
 
39
    def handle_label(self, label, **options):
 
40
        if not os.path.exists(label):
 
41
            print >> sys.stderr, "File not found: %s" % label
 
42
            sys.exit(1)
 
43
        if not label.endswith(".tar.gz") and not label.endswith(".tgz"):
 
44
            print >> sys.stderr, "File is not a .tar.gz file: %s" % label
 
45
            sys.exit(1)
 
46
 
 
47
        pwd = os.getcwd()
 
48
        dir = tempfile.mkdtemp(APP_NAME)
 
49
        subprocess.call(["cp", label, dir])
 
50
        os.chdir(dir)
 
51
        subprocess.call(["tar", "xfz", os.path.basename(label)])
 
52
        
 
53
        for dirpath, dirnames, filenames in os.walk(dir):
 
54
            pofiles = map(lambda a: os.path.join(dirpath, a), 
 
55
                          filter(lambda a: a.endswith(".po"), filenames))
 
56
            for pofile in pofiles:
 
57
                import_pofile(pofile)
 
58
        
 
59
        os.chdir(pwd)
 
60
        subprocess.call(["rm", "-r", dir])
 
61
        print "Make sure, you 'bzr add' all the new translations."