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