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.
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
19 ROOT = settings.ROOT_DIR
23 return mod.__path__[0].startswith(ROOT)
27 def save(self, output_directory, languages):
33 def generate(self, languages):
38 makedirs(os.path.dirname(to))
39 shutil.copyfile(frm, to)
42 class AppLocale(Locale):
43 def __init__(self, appmod):
45 if not os.path.exists(os.path.join(self.path, 'locale')):
46 raise LookupError('No locale for app %s' % appmod)
50 return self.app.__path__[0]
54 return self.app.__name__
56 def save(self, output_directory, languages):
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'))
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)
75 call_command('compilemessages', verbosity=0, settings='wolnelektury.settings')
81 def generate(self, languages):
85 call_command('makemessages', all=True)
92 class ModelTranslation(Locale):
93 def __init__(self, appname, poname=None):
94 self.appname = appname
95 self.poname = poname and poname or appname
97 def save(self, output_directory, languages):
98 call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
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)
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'),
110 self.app_dir = app_dir
112 self.out_file = out_file
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)))
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')
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'))
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'),
137 os.system('pybabel compile -D django -d %s' % os.path.dirname(self.out_file))
142 for appn in settings.INSTALLED_APPS:
143 app = __import__(appn)
146 SOURCES.append(AppLocale(app))
147 except LookupError, e:
148 print "no locales in %s" % app.__name__
150 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
151 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
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',
158 make_option('-c', '--compile', help='compile messages', action='store_true', dest='compile',
160 make_option('-g', '--generate', help='generate messages', action='store_true', dest='generate',
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'),
169 help = 'Make a locale pack'
172 def current_rev(self):
173 return os.popen('git rev-parse HEAD').read()
175 def current_branch(self):
176 return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
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:])
184 raise NotImplementedError('Sorry. Only zip format supported at the moment.')
186 tmp_dir = tempfile.mkdtemp('-wl-locale')
187 out_dir = os.path.join(tmp_dir, packname_b)
191 for lang in settings.LANGUAGES:
192 os.mkdir(os.path.join(out_dir, lang[0]))
195 src.generate(settings.LANGUAGES)
196 src.save(out_dir, settings.LANGUAGES)
197 # src.save(settings.LANGUAGES)
200 rev = self.current_rev()
201 rf = open(os.path.join(out_dir, '.revision'), 'w')
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)))
211 # shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir),
212 # base_dir=os.path.basename(out_dir))
214 shutil.rmtree(tmp_dir, ignore_errors=True)
218 src.generate(settings.LANGUAGES)
220 def load(self, options):
221 langs = get_languages(options['lang'])
224 src.load(options['directory'], langs)
230 def handle(self, *a, **options):
232 if not options['directory'] or not os.path.exists(options['directory']):
233 print "Directory not provided or does not exist, please use -d"
237 self.merge_setup(options['directory'])
240 self.merge_finish(options['message'])
241 elif options['generate']:
243 elif options['compile']:
248 merge_branch = 'wl-locale-merge'
251 def merge_setup(self, directory):
252 self.last_branch = self.current_branch()
253 rev = open(os.path.join(directory, '.revision')).read()
255 self.system('git checkout -b %s %s' % (self.merge_branch, rev))
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)
263 def system(self, fmt, *args):
264 code = os.system(fmt % args)
266 raise OSError('Command %s returned with exit code %d' % (fmt % args, code))