localepack command
[wolnelektury.git] / apps / wolnelektury_core / management / commands / localepack.py
1
2 from optparse import make_option
3 from django.conf import settings
4 from django.core.management.base import BaseCommand
5 from django.core.management.color import color_style
6 from django.core.management import call_command
7
8 import os
9 import shutil
10 import tempfile
11
12 import allauth
13
14 ROOT = os.path.dirname(settings.PROJECT_DIR)
15
16
17 def is_our_app(mod):
18     return mod.__path__[0].startswith(ROOT)
19
20
21 class Locale(object):
22     def save(self, output_directory, languages):
23         pass
24
25     def generate(self, languages):
26         pass
27
28
29 class AppLocale(Locale):
30     def __init__(self, appmod):
31         self.app = appmod
32         if not os.path.exists(os.path.join(self.path, 'locale')):
33             raise LookupError('No locale for app %s' % appmod)
34
35     @property
36     def path(self):
37         return self.app.__path__[0]
38
39     @property
40     def name(self):
41         return self.app.__name__
42
43     def save(self, output_directory, languages):
44         for lc in languages:
45             lc = lc[0]
46             if os.path.exists(os.path.join(self.path, 'locale', lc)):
47                 shutil.copy2(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
48                          os.path.join(output_directory, lc, self.name + '.po'))
49
50     def load(self, input_directory, languages):
51         for lc in zip(*languages)[0]:
52             shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
53                          os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'))
54
55     def generate(self, languages):
56         os.chdir(self.path)
57         print "in %s" % os.getcwd()
58         try:
59             call_command('makemessages', all=True)
60         except:
61             pass
62
63
64 class ModelTranslation(Locale):
65     def __init__(self, appname):
66         self.appname = appname
67
68     def save(self, output_directory, languages):
69         call_command('translation2po', self.appname, directory=output_directory)
70
71     def load(self, input_directory, languages):
72         call_command('translation2po', self.appname, directory=input_directory, load=True)
73
74
75 class CustomLocale(Locale):
76     def __init__(self, app_dir,
77                  config=os.path.join(ROOT, "babel.cfg"),
78                  out_file=os.path.join(ROOT, 'wolnelektury/locale-contrib/django.pot'),
79                  name=None):
80         self.app_dir = app_dir
81         self.config = config
82         self.out_file = out_file
83         self.name = name
84
85     def generate(self, languages):
86         os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
87         os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
88
89     def po_file(self, language):
90         d = os.path.dirname(self.out_file)
91         n = os.path.basename(self.out_file).split('.')[0]
92         return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
93
94     def save(self, output_directory, languages):
95         for lc in zip(*languages)[0]:
96             if os.path.exists(self.po_file(lc)):
97                 shutil.copy2(self.po_file(lc),
98                              os.path.join(output_directory, lc, self.name + '.po'))
99
100     def load(self, input_directory, languages):
101         for lc in zip(*languages)[0]:
102             shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
103                          self.po_file(lc))
104
105
106 SOURCES = []
107
108 for appn in settings.INSTALLED_APPS:
109     app = __import__(appn)
110     if is_our_app(app):
111         try:
112             SOURCES.append(AppLocale(app))
113         except LookupError, e:
114             print "no locales in %s" % app
115
116 SOURCES.append(ModelTranslation('infopages'))
117 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
118
119
120 class Command(BaseCommand):
121     option_list = BaseCommand.option_list + (
122         make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load', default=False),
123         make_option('-o', '--outfile', help='Resulting zip file', dest='outfile', default='./wl-locale.zip'),
124         )
125     help = 'Make a locale pack'
126     args = ''
127
128     def handle(self, *a, **options):
129         tmp_dir = tempfile.mkdtemp('-wl-locale')
130         out_dir = os.path.join(tmp_dir, 'wl-locale')
131         os.mkdir(out_dir)
132
133         try:
134             for lang in settings.LANGUAGES:
135                 os.mkdir(os.path.join(out_dir, lang[0]))
136
137             for src in SOURCES:
138                 src.generate(settings.LANGUAGES)
139                 src.save(out_dir, settings.LANGUAGES)
140                 #                src.save(settings.LANGUAGES)
141
142             packname = options.get('outfile')
143             packname_b = os.path.basename(packname).split('.')[0]
144             fmt = '.'.join(os.path.basename(packname).split('.')[1:])
145             shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir), base_dir=os.path.basename(out_dir))
146         finally:
147             shutil.rmtree(tmp_dir, ignore_errors=True)