`

restore_utf8、utf8togbk

 
阅读更多
restore_utf8.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Run "python restore_utf8.py" to rename *.h.utf8 to *.h.
#

import os

def restore_utf8(dir):
    resultfn = ''
    for fn in os.listdir(dir):
        sfile = os.path.join(dir, fn)
        if os.path.isdir(sfile):
            resultfn += restore_utf8(sfile)
            continue
        if fn.endswith('.utf8'):
            orgfile = sfile[:-5]
            try:
                if os.path.exists(orgfile): os.remove(orgfile)
                os.rename(sfile, orgfile)
                resultfn += fn[:-5] + ' '
            except:
                print('except for %s' %(fn,))
    return resultfn

if __name__=="__main__":
    resultfn = restore_utf8(os.path.abspath('.'))
    resultfn += restore_utf8(os.path.abspath('../core'))
    resultfn += restore_utf8(os.path.abspath('../android'))
    if resultfn != '': print('restore files: ' + resultfn)



#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Run "python utf8togbk.py" to convert source code files to the GBK format on Windows.
#

import os, codecs, sys

reload(sys)
sys.setdefaultencoding('gbk')

def utf8togbk(dir):
    resultfn = ''
    for fn in os.listdir(dir):
        sfile = os.path.join(dir, fn)
        if os.path.isdir(sfile):
            resultfn += utf8togbk(sfile)
            continue
        if fn.endswith('.h') or fn.endswith('.cpp'):
            if os.path.exists(sfile + '.utf8'):
                continue
            try:
                text = open(sfile,'r',-1,'utf-8').read()
                oldtext = text
            except UnicodeDecodeError:
                continue
            except TypeError:
                text = open(sfile).read()
                oldtext = text
                try:
                    if text[:3] == codecs.BOM_UTF8:
                        u = text[3:].decode('utf-8')
                        text = u.encode('gbk')
                except UnicodeEncodeError:
                    continue
                except UnicodeDecodeError:
                    continue
            try:
                text = text.replace('\r\n','\n')
                text = text.replace('\n','\r\n')
                if cmp(text, oldtext) != 0:
                    os.rename(sfile, sfile + '.utf8')
                    open(sfile, 'wb').write(text)
                    resultfn += fn + ' '
                    st = os.stat(sfile + '.utf8')
                    os.utime(sfile, (st.st_atime, st.st_mtime))
            except:
                print('except for %s' %(fn,))
    return resultfn

if __name__=="__main__":
    resultfn = utf8togbk(os.path.abspath('.'))
    resultfn += utf8togbk(os.path.abspath('../core'))
    resultfn += utf8togbk(os.path.abspath('../android'))
    if resultfn != '': print('utf8->gbk: ' + resultfn)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics