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