#!/usr/bin/python
# coding=utf-8

#Copyright:     
#File:          rewrite_file.py
#Author:        Benny Wong
#Email:         
#Date:          2017-08-31
#Description:   批量修改音效图片文件，避免查重

import os
import io
import re
import struct
# import sh


SEARCH_PATH = "Resource"

# 跳过目录列表
INGONRE_DIRS = [".svn"]

# 文件格式
FORMAT_SCRIPT_IMAGE = [".png", ".lua", ".jpg"]
FORMAT_SCRIPT_AUDIO = [".ogg", ".mp3"]

MP3_INFO_PATH = os.path.realpath("mp3info.exe")

def RewriteFile(path):
    print("rewrite file %s" % path)
    file = open(path, "a")
    file.writelines("\r\n--test")
    file.close()

def RewriteAudio(path):
    print("rewrite audio  MP3_INFO_PATH %s" % MP3_INFO_PATH)
    print("rewrite audio %s" % path)
    # file = open(path, "a")
    # file.writelines("\r\n--test")
    # file.close()

    shellCmd = MP3_INFO_PATH + " -c  --test " + path
    os.system(shellCmd)

def delFile(file):
    if os.path.exists(file):
        if os.path.isfile(file):
            os.remove(file)
        elif os.path.isdir(file):
            shutil.rmtree(file)

def RewriteFiles(src):
    for root, subdirs, files in os.walk(src):
        if os.path.split(root)[-1] in INGONRE_DIRS:
            break

        #文件
        for path in files:
            filePath = os.path.join(root, path)
            # new = re.sub(pattern, replace, path)
            # os.rename(root + "/" + path, root + "/" + new)

            format_file = os.path.splitext(filePath)[-1]
            if format_file in FORMAT_SCRIPT_IMAGE:
                RewriteFile(filePath)
            elif format_file in FORMAT_SCRIPT_AUDIO:
                RewriteAudio(filePath)

# -------------- main --------------
if __name__ == '__main__':
    RewriteFiles(SEARCH_PATH)
    os.system("pause")
