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):
30 def generate(self, languages):
35 makedirs(os.path.dirname(to))
36 shutil.copyfile(frm, to)
39 class AppLocale(Locale):
40 def __init__(self, appmod):
42 if not os.path.exists(os.path.join(self.path, 'locale')):
43 raise LookupError('No locale for app %s' % appmod)
47 return self.app.__path__[0]
51 return self.app.__name__
53 def save(self, output_directory, languages):
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'))
60 def load(self, input_directory, languages):
61 for lc in zip(*languages)[0]:
62 if os.path.exists(os.path.join(input_directory, lc, self.name + '.po')):
63 out = os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po')
64 makedirs(os.path.dirname(out))
65 copy_f(os.path.join(input_directory, lc, self.name + '.po'), out)
70 call_command('compilemessages', settings='wolnelektury.settings')
76 def generate(self, languages):
80 call_command('makemessages', all=True)
87 class ModelTranslation(Locale):
88 def __init__(self, appname, poname=None):
89 self.appname = appname
90 self.poname = poname and poname or appname
92 def save(self, output_directory, languages):
93 call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
95 def load(self, input_directory, languages):
96 call_command('translation2po', self.appname, directory=input_directory,
97 load=True, lang=','.join(zip(*languages)[0]), poname=self.poname, keep_running=True)
100 class CustomLocale(Locale):
101 def __init__(self, app_dir,
102 config=os.path.join(ROOT, "babel.cfg"),
103 out_file=os.path.join(ROOT, 'src/wolnelektury/locale-contrib/django.pot'),
105 self.app_dir = app_dir
107 self.out_file = out_file
110 def generate(self, languages):
111 os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
112 os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
114 def po_file(self, language):
115 d = os.path.dirname(self.out_file)
116 n = os.path.basename(self.out_file).split('.')[0]
117 return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
119 def save(self, output_directory, languages):
120 for lc in zip(*languages)[0]:
121 if os.path.exists(self.po_file(lc)):
122 copy_f(self.po_file(lc),
123 os.path.join(output_directory, lc, self.name + '.po'))
125 def load(self, input_directory, languages):
126 for lc in zip(*languages)[0]:
127 copy_f(os.path.join(input_directory, lc, self.name + '.po'),
129 os.system('pybabel compile -D django -d %s' % os.path.dirname(self.out_file))
134 for appn in settings.INSTALLED_APPS:
135 app = __import__(appn)
138 SOURCES.append(AppLocale(app))
139 except LookupError, e:
140 print "no locales in %s" % app.__name__
142 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
143 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
146 class Command(BaseCommand):
147 option_list = BaseCommand.option_list + (
148 make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load',
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.',
154 action='store_true', dest='merge', default=False),
155 make_option('-M', '--message', help='commit message', dest='message', default='New locale'),
157 help = 'Make a locale pack'
160 def current_rev(self):
161 return os.popen('git rev-parse HEAD').read()
163 def current_branch(self):
164 return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
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:])
172 raise NotImplementedError('Sorry. Only zip format supported at the moment.')
174 tmp_dir = tempfile.mkdtemp('-wl-locale')
175 out_dir = os.path.join(tmp_dir, packname_b)
179 for lang in settings.LANGUAGES:
180 os.mkdir(os.path.join(out_dir, lang[0]))
183 src.generate(settings.LANGUAGES)
184 src.save(out_dir, settings.LANGUAGES)
185 # src.save(settings.LANGUAGES)
188 rev = self.current_rev()
189 rf = open(os.path.join(out_dir, '.revision'), 'w')
195 os.chdir(os.path.dirname(out_dir))
196 self.system('zip -r %s %s' % (os.path.join(cwd, packname_b+'.zip'), os.path.basename(out_dir)))
199 # shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir),
200 # base_dir=os.path.basename(out_dir))
202 shutil.rmtree(tmp_dir, ignore_errors=True)
204 def load(self, options):
205 langs = get_languages(options['lang'])
208 src.load(options['directory'], langs)
210 def handle(self, *a, **options):
212 if not options['directory'] or not os.path.exists(options['directory']):
213 print "Directory not provided or does not exist, please use -d"
217 self.merge_setup(options['directory'])
220 self.merge_finish(options['message'])
224 merge_branch = 'wl-locale-merge'
227 def merge_setup(self, directory):
228 self.last_branch = self.current_branch()
229 rev = open(os.path.join(directory, '.revision')).read()
231 self.system('git checkout -b %s %s' % (self.merge_branch, rev))
233 def merge_finish(self, message):
234 self.system('git commit -a -m "%s"' % message.replace('"', '\\"'))
235 self.system('git checkout %s' % self.last_branch)
236 self.system('git merge -s recursive -X theirs %s' % self.merge_branch)
237 self.system('git branch -d %s' % self.merge_branch)
239 def system(self, fmt, *args):
240 code = os.system(fmt % args)
242 raise OSError('Command %s returned with exit code %d' % (fmt % args, code))