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