hxp
2018-08-10 ccfc87e02e2ae7d153bbab0754639a1a663d34d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
##@package ÊÖÓÎÈÎÎñ.ÈÎÎñÄ£°å.Éú³É.CreatFile
#
# @todo:¸ù¾ÝÈÎÎñÄ£°åÉú³ÉÈÎÎñxmlÎļþ
# @author hxp
# @date 2018-06-01
# @version 1.0
#
# ÏêϸÃèÊö: ¸ù¾ÝÈÎÎñÄ£°åÉú³ÉÈÎÎñxmlÎļþ
#
#-------------------------------------------------------------------------------
#"""Version = 2018-06-01 11:30"""
#-------------------------------------------------------------------------------
 
import os
import shutil
import ConfigParser
import sys
 
 
CParser = ConfigParser.ConfigParser()
CParser.readfp(open('../../µ¼±í¹¤¾ß/TaskManager.ini'))
ServerRootPath = CParser.get('TaskManager', 'ServerRootPath')
 
sys.path.append("../../µ¼±í¹¤¾ß\PythonLib\lib")
 
import Log4P
Log4P.SetDebug(CParser.getint('TaskManager', 'Debug'))
 
 
def ToInt(inputValue, defaultValue=0):
    try:
        return int(inputValue)
    except ValueError:
        return defaultValue
    
def CreatDir(pathDir):
    #ÅжÏÄ¿±ê·¾¶ÊÇ·ñ´æÔÚ
    if os.path.isdir(pathDir):
        #´æÔÚÔòɾ³ý
        shutil.rmtree(pathDir)
    #´´½¨Ä¿±êĿ¼·¾¶
    os.makedirs(pathDir)
    #ɾ³ýÄ¿±êĿ¼
    os.rmdir(pathDir)
    return
 
def main():
    
    #µ±Ç°·þÎñÆ÷ËùÓÐNPCID, ÐèÈ·±£±¾µØ·þÎñÆ÷tagChinNPC.txt±íÎļþÊÇ×îеÄ
    allNPCIDList = []
    chinNPCPath = ServerRootPath + "\\db\\PyMongoDataServer\\SysDB\\tagChinNPC.txt"
    if not os.path.isfile(chinNPCPath):
        Log4P.Error("ÕÒ²»µ½·þÎñ¶ËNPC±í·¾¶!", chinNPCPath, "ÇëÐ޸ĠTaskManager.ini ÖеÄÅäÖàServerRootPath")
        return
    chinNPC = file(chinNPCPath)
    for line in chinNPC.readlines():
        npcInfoList = line.split('\t')
        if not npcInfoList:
            continue
        npcID = npcInfoList[0]
        if not npcID.isdigit():
            continue
        npcID = int(npcID)
        allNPCIDList.append(npcID)
        
    #µ±Ç°·þÎñÆ÷ËùÓÐÎïÆ·ID, ÐèÈ·±£±¾µØ·þÎñÆ÷tagChinItem.txt±íÎļþÊÇ×îеÄ
    allItemIDList = []
    chinItemPath = ServerRootPath + "\\db\\PyMongoDataServer\\SysDB\\tagChinItem.txt"
    if not os.path.isfile(chinItemPath):
        Log4P.Error("ÕÒ²»µ½·þÎñ¶ËÎïÆ·±í·¾¶!", chinItemPath, "ÇëÐ޸ĠTaskManager.ini ÖеÄÅäÖàServerRootPath")
        return
    chinItem = open(chinItemPath)
    for line in chinItem.readlines():
        itemInfoList = line.split('\t')
        if not itemInfoList:
            continue
        itemID = itemInfoList[0]
        if not itemID.isdigit():
            continue
        itemID = int(itemID)
        allItemIDList.append(itemID)
        
        
    TaskFolder = "ÈÎÎñ"
    TemplateFolder = "Ä£°å"
 
    TempleList = os.listdir(TemplateFolder)
    
    # ´´½¨ÈÎÎñ×ÜÎļþ¼Ð
    if not os.path.isdir(TaskFolder):
        os.mkdir(TaskFolder)
        
    # ¶ÁÈ¡ÈÎÎñ±íÎļþ
    renwuBiaoFile = open('RenWuBiao.txt')
    renwuBiaoContent = renwuBiaoFile.read()
    renwuBiaoFile.close()
    
    taskList = renwuBiaoContent.split('\n')
    keyTypeList = taskList[0].split('\t') # Ìæ»»keyÀàÐÍÁбí
    keyList = taskList[1].split('\t') # Ìæ»»keyÁбí
    
    for col, keyType in enumerate(keyTypeList, 1):
        if keyType not in ["int", "string"]:
            Log4P.Error("²»Ö§³Ö¸ÃkeyÀàÐÍ£¨%s£©, µÚ%sÁÐ!" % (keyType, col))
            return
        
    keyTypeLen = len(keyTypeList)
    keyLen = len(keyList)
    
    if keyTypeLen != keyLen:
        Log4P.Error("Ìæ»»keyÓëkeyÀàÐ͸öÊý²»Æ¥Åä!keyTypeLen=%s,keyLen=%s" % (keyTypeLen, keyLen))
        return
    
    # Ìæ»»keyµÄʱºò°´key³¤¶Èµ¹ÐòÅÅ£¬·ÀÖ¹ÏÈÌæ»»¶ÌµÄkeyʱ£¬Èç¹û¶ÌkeyÄÚÈÝÔÚ³¤keyÄÚÈÝÀ¿ÉÄܵ¼ÖÂÌæ»»ÄÚÈÝÒì³£µÄbug
    sortKeyList = []
    for key in keyList:
        sortKeyList.append([len(key), key])
    sortKeyList.sort(reverse=True)
    
    taskCount = 0
    taskList = taskList[2:] # Êµ¼Ê´ý´¦ÀíÈÎÎñÁбí
    for num, task in enumerate(taskList, 1):
        taskInfo = task.split('\t')
        taskInfoLen = len(taskInfo)
        if taskInfoLen != keyLen:
            if num == len(taskList):
                break
            Log4P.Error("µÚ%s¸öÈÎÎñÄÚÈÝÓëÌæ»»key¸öÊý²»Æ¥Åä! ÈÎÎñÄÚÈݳ¤¶È=%s" % (num, taskInfoLen))
            return
        
        tempType = taskInfo[0]
        taskID = taskInfo[1]
        
        if tempType not in TempleList:
            Log4P.Error("ÈÎÎñÄ£°åÀàÐͲ»´æÔÚ!ÈÎÎñID=%s,  Ä£°å£º %s" % (taskID, tempType))
            return
        
        # ¼ì²ékeyÀàÐͶÔÓ¦ÄÚÈÝÊÇ·ñÕýÈ·
        for keyIndex, keyType in enumerate(keyTypeList):
            keyInfo = taskInfo[keyIndex]
            if not keyInfo:
                continue
            if keyType == "int" and ToInt(keyInfo, None) == None:
                Log4P.Error("ÈÎÎñ(%s), [%s]=%s, ÓëkeyÀàÐÍ(%s)²»Ò»ÖÂ!" % (taskID, keyList[keyIndex], keyInfo, keyType))
                return
            
        # ¸´ÖÆÄ£°åµ½Ä¿±êÈÎÎñIDÎļþ¼Ð
        taskDir = TaskFolder + "\\" + taskID
        if not os.path.isdir(taskDir):
            os.mkdir(taskDir)
        CreatDir(taskDir)
        shutil.copytree(TemplateFolder + "\\" + tempType, taskDir)
        
        Log4P.Info("µ¼³öÈÎÎñ: %s,  Ä£°å: %s" % (taskID, tempType))
        
        # Ìæ»»key
        for parent, dirnames, filenames in os.walk(taskDir):
            for filename in filenames:
                filePath = os.path.join(parent, filename)
                fileName, fileType = filename.split(".")
                
                # Ìæ»»ÎļþÃû
                if fileName in keyList:
                    keyIndex = keyList.index(fileName)
                    replaceInfo = taskInfo[keyIndex]
                    if not replaceInfo:
                        Log4P.Error("ÈÎÎñ(%s), Ìæ»»keyÄÚÈݲ»ÄÜΪ¿Õ£¡ key=%s" % (taskID, fileName), 
                                    "·¾¶: %s" % filePath,
                                    "Ä£°å: %s" % tempType)
                        return
                    keyType = keyTypeList[keyIndex]
                    if keyType == "int" and not CheckNPCIDItemIDIsExist(taskID, fileName, replaceInfo, allNPCIDList, allItemIDList):
                        return
                    
                    newFileName = "%s.%s" % (replaceInfo, fileType)
                    newFilePath = os.path.join(parent, newFileName)
                    os.rename(filePath, newFilePath)
                    filePath = newFilePath
                    
                # Ìæ»»ÎļþÄÚÈÝ
                readFile = open(filePath)
                fileContent = readFile.read()
                readFile.close()
                
                if "encoding" not in fileContent:
                    Log4P.Error("ÐèÒªÉèÖÃencoding±àÂë: %s" % filePath, "×¢ÒâÎļþ±ØÐëÁí´æÎªANSI±àÂë¸ñʽ!")
                    return
                
                for keyInfo in sortKeyList:
                    key = keyInfo[1]
                    if key not in fileContent:
                        continue
                    keyIndex = keyList.index(key)
                    replaceInfo = taskInfo[keyIndex]
                    if not replaceInfo:
                        Log4P.Error("ÈÎÎñ(%s), Ìæ»»keyÄÚÈݲ»ÄÜΪ¿Õ£¡ key=%s" % (taskID, key), 
                                    "·¾¶: %s" % filePath,
                                    "Ä£°å: %s" % tempType)
                        return
                    
                    keyType = keyTypeList[keyIndex]
                    if keyType == "int" and not CheckNPCIDItemIDIsExist(taskID, key, replaceInfo, allNPCIDList, allItemIDList):
                        return
                    
                    fileContent = fileContent.replace(key, replaceInfo)
                    
                readFile = open(filePath, "w")
                readFile.write(fileContent)
                readFile.close()
                
                #Log4P.Debug("    Ìæ»»Îļþ: %s" % filePath)
                
        taskCount += 1
        
    Log4P.Info("", "ÈÎÎñÄ£°åÉú³ÉÈÎÎñ³É¹¦! ÈÎÎñÊý: %s" % taskCount)
    return
 
def CheckNPCIDItemIDIsExist(taskID, replaceKey, replaceInfo, allNPCIDList, allItemIDList):
    if "NPCID" in replaceKey and int(replaceInfo) not in allNPCIDList:
        Log4P.Error("ÈÎÎñ(%s), Ìæ»»NPCID²»´æÔÚ! NPCID=%s" % (taskID, replaceInfo))
        return False
    if "ÎïÆ·ID" in replaceKey and int(replaceInfo) not in allItemIDList:
        Log4P.Error("ÈÎÎñ(%s), Ìæ»»ÎïÆ·ID²»´æÔÚ! ÎïÆ·ID=%s" % (taskID, replaceInfo))
        return False
    return True
 
main()