import glob import os import shutil rootFile = 'D:/Project/P2207-platform/inspur-ui/' modelFile = rootFile + 'bin/model/' jsFile = rootFile + 'src/views/party_screen/details/designer/tools/configure/' vueFile = rootFile + 'src/views/party_screen/details/designer/widget/' mainFile = rootFile + 'src/views/party_screen/details/designer/tools/main.js' # 复制文件到对应位置 def copyfile(srcfile, dstpath, ckey): # 复制函数 if not os.path.isfile(srcfile): print("%s not exist!" % (srcfile)) else: fpath, fname = os.path.split(srcfile) # 分离文件名和路径 if not os.path.exists(dstpath): print('复制路径出现错误') os.makedirs(dstpath) # 创建路径 if fname == 'widget-Mobile.js': fname = 'widget-' + ckey + '.js' fpath = jsFile elif fname == 'widgetMobile.vue': fname = 'widget' + ckey + '.vue' fpath = vueFile shutil.copy(srcfile, fpath + fname) # 复制文件 print("copy %s -> %s" % (srcfile, dstpath + fname)) # 修改main.js def myCopyFileStrring(replaceMessage, removeStrring): firstpath = mainFile contentValue = replaceMessage.replace(removeStrring, '') newcontentValue = contentValue.replace('-', '') newString = '// 111\nimport { ' + newcontentValue + ' } from \"./configure/' + contentValue + '\";' newString2 = '// 222\n ' + newcontentValue + ',' if os.path.isfile(firstpath): f1 = open(firstpath, 'r+', encoding='utf-8') infos = f1.readlines() f1.seek(0, 0) for line in infos: line_new = line.replace('// 111', newString) f1.write(line_new) f1.close() if os.path.isfile(firstpath): f1 = open(firstpath, 'r+', encoding='utf-8') infos = f1.readlines() f1.seek(0, 0) for line in infos: line_new = line.replace('// 222', newString2) f1.write(line_new) f1.close() def tempFileStrring(replaceMessage, removeStrring): firstpath = vueFile + '/temp.vue' contentValue = replaceMessage.replace(removeStrring, '') newcontentValue = contentValue.replace('-', '') newString = '// 111\n import ' + newcontentValue + ' from \"./' + replaceMessage + '\"' newString2 = '// 222\n ' + newcontentValue + ',' if os.path.isfile(firstpath): f1 = open(firstpath, 'r+', encoding='utf-8') infos = f1.readlines() f1.seek(0, 0) for line in infos: line_new = line.replace('// 111', newString) f1.write(line_new) f1.close() if os.path.isfile(firstpath): f1 = open(firstpath, 'r+', encoding='utf-8') infos = f1.readlines() f1.seek(0, 0) for line in infos: line_new = line.replace('// 222', newString2) f1.write(line_new) f1.close() def widgetFileStrring(replaceMessage, removeStrring): firstpath = vueFile + '/widget.vue' contentValue = replaceMessage.replace(removeStrring, '') newcontentValue = contentValue.replace('-', '') newString = '// 111\n import ' + newcontentValue + ' from \"./' + contentValue + '\";' newString2 = '// 222\n ' + newcontentValue + ',' if os.path.isfile(firstpath): f1 = open(firstpath, 'r+', encoding='utf-8') infos = f1.readlines() f1.seek(0, 0) for line in infos: line_new = line.replace('// 111', newString) f1.write(line_new) f1.close() if os.path.isfile(firstpath): f1 = open(firstpath, 'r+', encoding='utf-8') infos = f1.readlines() f1.seek(0, 0) for line in infos: line_new = line.replace('// 222', newString2) f1.write(line_new) f1.close() def lastWay(firstFile, secondFile, keyValue): # D:\Project\report-ui-inspur\src\views\mobileDesigner\designer\tools\configure src_file_list = glob.glob(firstFile + '*') for srcfile in src_file_list: # print(srcfile) copyfile(srcfile, secondFile, keyValue) # 复制文件 def replaceMessage(filePath, originValue , replaceValue): rep = '' with open(filePath, 'r', encoding='utf-8') as fp: contents = fp.read() rep = contents.replace(originValue, replaceValue) with open(filePath, 'w', encoding='utf-8') as fp2: fp2.write(rep) print('正式结束。。。') if __name__ == '__main__': # file:///D:/Project/P2207-platform/inspur-ui/src/views/party_screen/details/designer/widget/widgetMobileGrid.vue endString = 'MobileTabList' firstFileName = 'widget-' + endString + '.js' secondFileName = 'widget' + endString + '.vue' # 第一步复制文件到相关地址 # 替换main.js中文件地址 # 替换temp文件和widge文件中地址 lastWay(modelFile, modelFile, endString) # lastWay() myCopyFileStrring(firstFileName, '.js') tempFileStrring(secondFileName, '.vue') widgetFileStrring(secondFileName, '.vue') replaceMessage(jsFile + firstFileName, 'widget', secondFileName.replace('.vue', '')) replaceMessage(jsFile + firstFileName, 'widget-', firstFileName.replace('.js', '')) replaceMessage(vueFile + secondFileName, 'widget', firstFileName.replace('.js', ''))