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