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
15 ROOT = os.path.dirname(settings.PROJECT_DIR)
19 return mod.__path__[0].startswith(ROOT)
23 def save(self, output_directory, languages):
26 def generate(self, languages):
30 class AppLocale(Locale):
31 def __init__(self, appmod):
33 if not os.path.exists(os.path.join(self.path, 'locale')):
34 raise LookupError('No locale for app %s' % appmod)
38 return self.app.__path__[0]
42 return self.app.__name__
44 def save(self, output_directory, languages):
47 if os.path.exists(os.path.join(self.path, 'locale', lc)):
48 shutil.copy2(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
49 os.path.join(output_directory, lc, self.name + '.po'))
51 def load(self, input_directory, languages):
52 for lc in zip(*languages)[0]:
53 if os.path.exists(os.path.join(input_directory, lc, self.name + '.po')):
54 shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
55 os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'))
57 def generate(self, languages):
61 call_command('makemessages', all=True)
68 class ModelTranslation(Locale):
69 def __init__(self, appname, poname=None):
70 self.appname = appname
71 self.poname = poname and poname or appname
73 def save(self, output_directory, languages):
74 call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
76 def load(self, input_directory, languages):
77 call_command('translation2po', self.appname, directory=input_directory,
78 load=True, lang=','.join(zip(*languages)[0]), poname=self.poname)
81 class CustomLocale(Locale):
82 def __init__(self, app_dir,
83 config=os.path.join(ROOT, "babel.cfg"),
84 out_file=os.path.join(ROOT, 'wolnelektury/locale-contrib/django.pot'),
86 self.app_dir = app_dir
88 self.out_file = out_file
91 def generate(self, languages):
92 os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
93 os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
95 def po_file(self, language):
96 d = os.path.dirname(self.out_file)
97 n = os.path.basename(self.out_file).split('.')[0]
98 return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
100 def save(self, output_directory, languages):
101 for lc in zip(*languages)[0]:
102 if os.path.exists(self.po_file(lc)):
103 shutil.copy2(self.po_file(lc),
104 os.path.join(output_directory, lc, self.name + '.po'))
106 def load(self, input_directory, languages):
107 for lc in zip(*languages)[0]:
108 shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
110 os.system('pybabel compile -D django -d %s' % os.path.dirname(self.out_file))
115 for appn in settings.INSTALLED_APPS:
116 app = __import__(appn)
119 SOURCES.append(AppLocale(app))
120 except LookupError, e:
121 print "no locales in %s" % app.__name__
123 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
124 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
127 class Command(BaseCommand):
128 option_list = BaseCommand.option_list + (
129 make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load', default=False),
130 make_option('-L', '--lang', help='load just one language', dest='lang', default=None),
131 make_option('-d', '--directory', help='load from this directory', dest='directory', default=None),
132 make_option('-o', '--outfile', help='Resulting zip file', dest='outfile', default='./wl-locale.zip'),
133 make_option('-m', '--merge', help='Use git to merge. Please use with clean working directory.', action='store_true', dest='merge', default=False),
134 make_option('-M', '--message', help='commit message', dest='message', default='New locale'),
137 help = 'Make a locale pack'
140 def current_rev(self):
141 return os.popen('git rev-parse HEAD').read()
143 def current_branch(self):
144 return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
146 def save(self, options):
147 tmp_dir = tempfile.mkdtemp('-wl-locale')
148 out_dir = os.path.join(tmp_dir, 'wl-locale')
152 for lang in settings.LANGUAGES:
153 os.mkdir(os.path.join(out_dir, lang[0]))
156 src.generate(settings.LANGUAGES)
157 src.save(out_dir, settings.LANGUAGES)
158 # src.save(settings.LANGUAGES)
161 rev = self.current_rev()
162 rf = open(os.path.join(out_dir, '.revision'), 'w')
166 packname = options.get('outfile')
167 packname_b = os.path.basename(packname).split('.')[0]
168 fmt = '.'.join(os.path.basename(packname).split('.')[1:])
169 shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir), base_dir=os.path.basename(out_dir))
171 shutil.rmtree(tmp_dir, ignore_errors=True)
173 def load(self, options):
174 langs = get_languages(options['lang'])
177 src.load(options['directory'], langs)
179 def handle(self, *a, **options):
181 if not options['directory'] or not os.path.exists(options['directory']):
182 print "Directory not provided or does not exist, please use -d"
185 if options['merge']: self.merge_setup(options['directory'])
187 if options['merge']: self.merge_finish(options['message'])
191 merge_branch = 'wl-locale-merge'
194 def merge_setup(self, directory):
195 self.last_branch = self.current_branch()
196 rev = open(os.path.join(directory, '.revision')).read()
198 self.system('git checkout -b %s %s' % (self.merge_branch, rev))
200 def merge_finish(self, message):
201 self.system('git commit -a -m "%s"' % message.replace('"', '\\"'))
202 self.system('git checkout %s' % self.last_branch)
203 self.system('git merge -s recursive -X theirs %s' % self.merge_branch)
204 self.system('git branch -d %s' % self.merge_branch)
206 def system(self, fmt, *args):
207 code = os.system(fmt % args)
209 raise OSError('Command %s returned with exit code %d' % (fmt % args, code))