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
16 ROOT = os.path.dirname(settings.PROJECT_DIR)
20 return mod.__path__[0].startswith(ROOT)
24 def save(self, output_directory, languages):
27 def generate(self, languages):
31 class AppLocale(Locale):
32 def __init__(self, appmod):
34 if not os.path.exists(os.path.join(self.path, 'locale')):
35 raise LookupError('No locale for app %s' % appmod)
39 return self.app.__path__[0]
43 return self.app.__name__
45 def save(self, output_directory, languages):
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'))
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'),
65 call_command('compilemessages', settings='wolnelektury.settings')
72 def generate(self, languages):
76 call_command('makemessages', all=True)
83 class ModelTranslation(Locale):
84 def __init__(self, appname, poname=None):
85 self.appname = appname
86 self.poname = poname and poname or appname
88 def save(self, output_directory, languages):
89 call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
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)
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'),
101 self.app_dir = app_dir
103 self.out_file = out_file
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)))
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')
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'))
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'),
125 os.system('pybabel compile -D django -d %s' % os.path.dirname(self.out_file))
130 for appn in settings.INSTALLED_APPS:
131 app = __import__(appn)
134 SOURCES.append(AppLocale(app))
135 except LookupError, e:
136 print "no locales in %s" % app.__name__
138 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
139 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
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'),
152 help = 'Make a locale pack'
155 def current_rev(self):
156 return os.popen('git rev-parse HEAD').read()
158 def current_branch(self):
159 return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
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:])
167 raise NotImplementedError('Sorry. Only zip format supported at the moment.')
169 tmp_dir = tempfile.mkdtemp('-wl-locale')
170 out_dir = os.path.join(tmp_dir, packname)
174 for lang in settings.LANGUAGES:
175 os.mkdir(os.path.join(out_dir, lang[0]))
178 src.generate(settings.LANGUAGES)
179 src.save(out_dir, settings.LANGUAGES)
180 # src.save(settings.LANGUAGES)
183 rev = self.current_rev()
184 rf = open(os.path.join(out_dir, '.revision'), 'w')
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)))
195 # shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir), base_dir=os.path.basename(out_dir))
197 shutil.rmtree(tmp_dir, ignore_errors=True)
199 def load(self, options):
200 langs = get_languages(options['lang'])
203 src.load(options['directory'], langs)
205 def handle(self, *a, **options):
207 if not options['directory'] or not os.path.exists(options['directory']):
208 print "Directory not provided or does not exist, please use -d"
211 if options['merge']: self.merge_setup(options['directory'])
213 if options['merge']: self.merge_finish(options['message'])
217 merge_branch = 'wl-locale-merge'
220 def merge_setup(self, directory):
221 self.last_branch = self.current_branch()
222 rev = open(os.path.join(directory, '.revision')).read()
224 self.system('git checkout -b %s %s' % (self.merge_branch, rev))
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)
232 def system(self, fmt, *args):
233 code = os.system(fmt % args)
235 raise OSError('Command %s returned with exit code %d' % (fmt % args, code))