#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os
import sys
import traceback
import subprocess

currpath = os.path.join(os.getcwd(),os.path.dirname(__file__))
if currpath not in sys.path:
    sys.path.append(currpath)
#-----------------------------------------
# global
#-----------------------------------------
i18ndir = os.path.join(currpath,'i18n/')
#-----------------------------------------
#
#-----------------------------------------    
def cust_popen(cmd,close_fds=True):
    try:
        proc = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=close_fds)
        retcode = proc.wait()
        return retcode,proc
    except Exception,e:
        print traceback.print_exc()
#-----------------------------------------
#
#-----------------------------------------
def autogettext():
    for lang in ['zh_CN','en_US']:
        popath = os.path.join(i18ndir,lang,'LC_MESSAGES','messages.po')
        mopath = os.path.join(i18ndir,lang,'LC_MESSAGES','messages.mo')
        if os.path.exists(popath):
            retcode,proc = cust_popen('msgfmt -o %s %s' % (mopath,popath))
            print 'cover source file from "%s" to "%s"' % (popath,mopath)
            if not retcode == 0:
                print proc.stderr.read()
#-----------------------------------------
#
#-----------------------------------------
if __name__ == '__main__':
    autogettext()
