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