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