__author__ = 'Administrator'
import os
import shutil


class CFileOperator(object):
    def __init__(self):
        self._m_FilePath = os.getcwd()
        self._m_FileContent = []
        self._m_sError = ""


    def GetFileContent(self, filepath="", filecontent=[]):

        if not self.IsFileExit(filepath):
            self._m_sError = "File Path is not exit %s" % filepath
            return False
        openFile = open(filepath, 'r')
        try:
            for line in openFile:
                filecontent.append(line)
        finally:
            openFile.close()
        return True

    def WriteFileContet(self, filepath="", filecontent=[], isAdd=True):
        if not self.IsFileExit(filepath):
            dirpath = filepath[0:filepath.rfind("/")]
            if not self.ISDirExit(dirpath):
                self.CreateDir(dirpath)
        if (True == isAdd):
            openfile = open(filepath, "a")
        else:
            openfile = open(filepath, 'w')
        try:
            openfile.writelines(filecontent)
        finally:
            openfile.close()

    def ListFile(self, filepath="", result=[]):

        FileName = []
        self.GetCurrentDirAndFilePath(filepath, FileName)
        for file in FileName:
            if file == "." or file == "..":
                continue
            else:
                newfile = filepath + "/" + file
                if self.ISDirExit(newfile):
                    self.ListFile(newfile, result)
                else:
                    result.append(newfile)
        return result

    def GetCurrentDirAndFilePath(self, path="", content=[]):

        if not self.ISDirExit(path):
            self._m_sError = "the file dir is not exit %s" % path
            return False
        content.extend(os.listdir(path))
        return True

    def GetCurrentFilePath(self, path="", content=[]):

        if not self.ISDirExit(path):
            self._m_sError = "the file dir is not exit %s" % path
            return False

        DirFilecontent = os.listdir(path)
        for elem in DirFilecontent:
            if self.IsFileExit(path + "/" + elem):
                content.append(elem)
        return True

    def CreateDir(self, filepaht):
        os.makedirs(filepaht)

    def RmDir(self, filepath):

        if self.ISDirExit(filepath):
            shutil.rmtree(filepath)

    def IsFileExit(self, filepath):

        return os.path.isfile(filepath)

    def ISDirExit(self, DirPath):

        return os.path.isdir(DirPath)

    def TarFile(self, filepath):
        os.chdir(filepath[0:filepath.rfind("/")])
        command = "tar -cvf ." + filepath[filepath.rfind("/"):len(filepath)] + \
                  ".tar" + " ." + filepath[filepath.rfind("/"):len(filepath)]
        os.chdir(self._m_FilePath)
        print command
        os.system(command)

    def UNtarFile(self, filepath):
        command = "tar -xvf " + filepath
        print command
        os.system(command)

    def GetError(self):

        return self._m_sError


def modefycpp(elem):
    cCFileOperator = CFileOperator()
    content = []
    cCFileOperator.GetFileContent(elem, content)
    for index in range(len(content)):
        if str(content[index]).find("CCAssert") != -1:
            line = "//" + str(content[index])
            content[index] = line
    cCFileOperator.WriteFileContet(elem, content, False)


if __name__ == "__main__":

    cCFileOperator = CFileOperator()
    result = []
    cCFileOperator.ListFile("D:\cocos2dx\cocos2d-x-2.2.1", result)
    cppfile = []
    for elem in result:
        if str(elem).endswith(".cpp"):
            modefycpp(elem)
            # else:
            #   print elem