kitchen/releaseutils.py

77 lines
2.2 KiB
Python
Raw Permalink Normal View History

2015-10-08 16:26:18 +00:00
#!/usr/bin/python -tt
2016-07-08 23:18:01 +00:00
try:
import configparser
except ImportError:
import ConfigParser as configparser
2015-10-08 16:26:18 +00:00
import glob
import os
import shutil
2016-07-08 23:18:01 +00:00
try:
# Only availabe on py2
from kitchen.pycompat27 import subprocess
except:
import subprocess
2015-10-08 16:26:18 +00:00
class MsgFmt(object):
def run(self, args):
cmd = subprocess.Popen(args, shell=False)
cmd.wait()
2016-07-08 23:18:01 +00:00
2015-10-08 16:26:18 +00:00
def setup_message_compiler():
# Look for msgfmt
try:
subprocess.Popen(['msgfmt', '-h'], stdout=subprocess.PIPE)
except OSError:
import babel.messages.frontend
return (babel.messages.frontend.CommandLineInterface(),
'pybabel compile -D %(domain)s -d locale -i %(pofile)s -l %(lang)s'
)
else:
return (MsgFmt(), 'msgfmt -c -o locale/%(lang)s/LC_MESSAGES/%(domain)s.mo %(pofile)s')
2016-07-08 23:18:01 +00:00
2015-10-08 16:26:18 +00:00
def main():
# Get the directory with message catalogs
# Reuse transifex's config file first as it will know this
2016-07-08 23:18:01 +00:00
cfg = configparser.SafeConfigParser()
2015-10-08 16:26:18 +00:00
cfg.read('.tx/config')
cmd, args = setup_message_compiler()
try:
shutil.rmtree('locale')
2016-07-08 23:18:01 +00:00
except OSError as e:
2015-10-08 16:26:18 +00:00
# If the error is that locale does not exist, we're okay. We're
# deleting it here, after all
2015-10-08 16:26:18 +00:00
if e.errno != 2:
raise
for section in [s for s in cfg.sections() if s != 'main']:
try:
file_filter = cfg.get(section, 'file_filter')
source_file = cfg.get(section, 'source_file')
2016-07-08 23:18:01 +00:00
except configparser.NoOptionError:
2015-10-08 16:26:18 +00:00
continue
glob_pattern = file_filter.replace('<lang>', '*')
pot = os.path.basename(source_file)
if pot.endswith('.pot'):
pot = pot[:-4]
arg_values = {'domain': pot}
for po_file in glob.glob(glob_pattern):
file_pattern = os.path.basename(po_file)
lang = file_pattern.replace('.po','')
os.makedirs(os.path.join('locale', lang, 'LC_MESSAGES'))
arg_values['pofile'] = po_file
arg_values['lang'] = lang
compile_args = args % arg_values
compile_args = compile_args.split(' ')
cmd.run(compile_args)
if __name__ == '__main__':
main()