bd631928c682749f8b97c2cc223519183a14febf
[wolnelektury.git] / apps / wolnelektury_core / management / commands / localepack.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from optparse import make_option
6 from django.conf import settings
7 from django.core.management.base import BaseCommand
8 from django.core.management import call_command
9 from .translation2po import get_languages
10
11 import os
12 import shutil
13 import tempfile
14 import sys
15
16 import allauth
17
18 ROOT = os.path.dirname(settings.PROJECT_DIR)
19
20
21 def is_our_app(mod):
22     return mod.__path__[0].startswith(ROOT)
23
24
25 class Locale(object):
26     def save(self, output_directory, languages):
27         pass
28
29     def generate(self, languages):
30         pass
31
32 def copy_f(frm, to):
33     "I can create a necessary dest directiories, yey!"
34     if not os.path.exists(os.path.dirname(to)):
35         os.makedirs(os.path.dirname(to))
36     shutil.copyfile(frm, to)
37
38 class AppLocale(Locale):
39     def __init__(self, appmod):
40         self.app = appmod
41         if not os.path.exists(os.path.join(self.path, 'locale')):
42             raise LookupError('No locale for app %s' % appmod)
43
44     @property
45     def path(self):
46         return self.app.__path__[0]
47
48     @property
49     def name(self):
50         return self.app.__name__
51
52     def save(self, output_directory, languages):
53         for lc in languages:
54             lc = lc[0]
55             if os.path.exists(os.path.join(self.path, 'locale', lc)):
56                 copy_f(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
57                           os.path.join(output_directory, lc, self.name + '.po'))
58
59
60     def load(self, input_directory, languages):
61         for lc in zip(*languages)[0]:
62             if os.path.exists(os.path.join(input_directory, lc, self.name + '.po')):
63                 out = os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po')
64                 if not os.path.exists(os.path.dirname(out)):
65                     os.makedirs(os.path.dirname(out))
66                 copy_f(os.path.join(input_directory, lc, self.name + '.po'),
67                              out)
68
69         wd = os.getcwd()
70         os.chdir(self.path)
71         try:
72             call_command('compilemessages', settings='wolnelektury.settings')
73         except:
74             pass
75         finally:
76             os.chdir(wd)
77
78
79     def generate(self, languages):
80         wd = os.getcwd()
81         os.chdir(self.path)
82         try:
83             call_command('makemessages', all=True)
84         except:
85             pass
86         finally:
87             os.chdir(wd)
88
89
90 class ModelTranslation(Locale):
91     def __init__(self, appname, poname=None):
92         self.appname = appname
93         self.poname = poname and poname or appname
94
95     def save(self, output_directory, languages):
96         call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
97
98     def load(self, input_directory, languages):
99         call_command('translation2po', self.appname, directory=input_directory,
100                      load=True, lang=','.join(zip(*languages)[0]), poname=self.poname, keep_running=True)
101
102
103 class CustomLocale(Locale):
104     def __init__(self, app_dir,
105                  config=os.path.join(ROOT, "babel.cfg"),
106                  out_file=os.path.join(ROOT, 'wolnelektury/locale-contrib/django.pot'),
107                  name=None):
108         self.app_dir = app_dir
109         self.config = config
110         self.out_file = out_file
111         self.name = name
112
113     def generate(self, languages):
114         os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
115         os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
116
117     def po_file(self, language):
118         d = os.path.dirname(self.out_file)
119         n = os.path.basename(self.out_file).split('.')[0]
120         return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
121
122     def save(self, output_directory, languages):
123         for lc in zip(*languages)[0]:
124             if os.path.exists(self.po_file(lc)):
125                 copy_f(self.po_file(lc),
126                              os.path.join(output_directory, lc, self.name + '.po'))
127
128     def load(self, input_directory, languages):
129         for lc in zip(*languages)[0]:
130             copy_f(os.path.join(input_directory, lc, self.name + '.po'),
131                          self.po_file(lc))
132         os.system('pybabel compile -D django -d %s' % os.path.dirname(self.out_file))
133
134
135 SOURCES = []
136
137 for appn in settings.INSTALLED_APPS:
138     app = __import__(appn)
139     if is_our_app(app):
140         try:
141             SOURCES.append(AppLocale(app))
142         except LookupError, e:
143             print "no locales in %s" % app.__name__
144
145 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
146 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
147
148
149 class Command(BaseCommand):
150     option_list = BaseCommand.option_list + (
151         make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load', default=False),
152         make_option('-L', '--lang', help='load just one language', dest='lang', default=None),
153         make_option('-d', '--directory', help='load from this directory', dest='directory', default=None),
154         make_option('-o', '--outfile', help='Resulting zip file', dest='outfile', default='./wl-locale.zip'),
155         make_option('-m', '--merge', help='Use git to merge. Please use with clean working directory.', action='store_true', dest='merge', default=False),
156         make_option('-M', '--message', help='commit message', dest='message', default='New locale'),
157
158         )
159     help = 'Make a locale pack'
160     args = ''
161
162     def current_rev(self):
163         return os.popen('git rev-parse HEAD').read()
164
165     def current_branch(self):
166         return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
167
168     def save(self, options):
169         packname = options.get('outfile')
170         packname_b = os.path.basename(packname).split('.')[0]
171         fmt = '.'.join(os.path.basename(packname).split('.')[1:])
172
173         if fmt != 'zip':
174             raise NotImplementedError('Sorry. Only zip format supported at the moment.')
175
176         tmp_dir = tempfile.mkdtemp('-wl-locale')
177         out_dir = os.path.join(tmp_dir, packname_b)
178         os.mkdir(out_dir)
179
180         try:
181             for lang in settings.LANGUAGES:
182                 os.mkdir(os.path.join(out_dir, lang[0]))
183
184             for src in SOURCES:
185                 src.generate(settings.LANGUAGES)
186                 src.save(out_dir, settings.LANGUAGES)
187                 #                src.save(settings.LANGUAGES)
188
189             # write out revision
190             rev = self.current_rev()
191             rf = open(os.path.join(out_dir, '.revision'), 'w')
192             rf.write(rev)
193             rf.close()
194
195
196             cwd = os.getcwd()
197             try:
198                 os.chdir(os.path.dirname(out_dir))
199                 self.system('zip -r %s %s' % (os.path.join(cwd, packname_b+'.zip'), os.path.basename(out_dir)))
200             finally:
201                 os.chdir(cwd)
202                 #            shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir), base_dir=os.path.basename(out_dir))
203         finally:
204             shutil.rmtree(tmp_dir, ignore_errors=True)
205
206     def load(self, options):
207         langs = get_languages(options['lang'])
208
209         for src in SOURCES:
210             src.load(options['directory'], langs)
211
212     def handle(self, *a, **options):
213         if options['load']:
214             if not options['directory'] or not os.path.exists(options['directory']):
215                 print "Directory not provided or does not exist, please use -d"
216                 sys.exit(1)
217
218             if options['merge']: self.merge_setup(options['directory'])
219             self.load(options)
220             if options['merge']: self.merge_finish(options['message'])
221         else:
222             self.save(options)
223
224     merge_branch = 'wl-locale-merge'
225     last_branch = None
226
227     def merge_setup(self, directory):
228         self.last_branch = self.current_branch()
229         rev = open(os.path.join(directory, '.revision')).read()
230
231         self.system('git checkout -b %s %s' % (self.merge_branch, rev))
232
233     def merge_finish(self, message):
234         self.system('git commit -a -m "%s"' % message.replace('"', '\\"'))
235         self.system('git checkout %s' % self.last_branch)
236         self.system('git merge -s recursive -X theirs %s' % self.merge_branch)
237         self.system('git branch -d %s' % self.merge_branch)
238
239     def system(self, fmt, *args):
240         code = os.system(fmt % args)
241         if code != 0:
242             raise OSError('Command %s returned with exit code %d' % (fmt % args, code))
243         return code