def modifySplash(sdk, srcDir, destDir, XcodeProject, orientation): print "[ %s ] modify splash start" % sdk if orientation == "landscape": srcSpalshPath = os.path.join(srcDir, "splash/landscape") else: srcSpalshPath = os.path.join(srcDir, "splash/protrait") print srcSpalshPath if os.path.exists(srcSpalshPath): modified = False for imageName in os.listdir(srcSpalshPath): imagePath = os.path.join(srcSpalshPath, imageName) destPath = getFilePathForProj(imageName, XcodeProject) if destPath: destPath = os.path.join(destDir, destPath) ret = fileutils.copyFile(imagePath, destPath) if ret: print "[ %s ] Error! no src file: %s" % (sdk, imagePath) return 1 else: print "add image:", imagePath XcodeProject.add_file(imagePath) ret = delProjectBuildConfig(XcodeProject, "ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME") if ret: return 1 ret = delInfoPlistKey(sdk, destDir, XcodeProject, "UILaunchStoryboardName") if ret: return 1 print "[ %s ] modify splash finish" % sdk return 0
def modifyIcon(iconPath,appPath): try: filelist=os.listdir(iconPath) leng =len(filelist) except Exception as e: raise e if leng==23: for files in filelist: if os.path.splitext(files)[1]=='.png': for oldIcon in os.listdir(appPath): if files==oldIcon: try: newIconPath=iconPath+'/'+files oldIconPath=appPath+'/'+oldIcon fileutils.copyFile(newIconPath,oldIconPath) except Exception as e: raise e else: print 'icon suffix not png,please check' else: print 'icon count is wrong'
def modifyAonesdkJson(src, dest): if not os.path.exists(src): return 0 if not os.path.exists(dest): fileutils.copyFile(src, dest) return 0 with open(src) as fsrc: srcData = json.load(fsrc) changeFile = False if srcData.has_key("channel"): changeFile = True if changeFile: with open(dest) as fdest: destData = json.load(fdest) destData["channel"] = srcData["channel"] destData = json.dumps(destData, ensure_ascii=False) with open(dest, "w") as fdest: fdest.write(destData) return 0
def modifyUuSdkConfigJson(src, dest): if not os.path.exists(dest): fileutils.copyFile(src, dest) return with open(src) as fsrc: srcData = json.load(fsrc) with open(dest) as fdest: destData = json.load(fdest) # set default sdk defalutSdkNames = [ "defaultUserSdk", "defaultIapSdk", "defaultAnalyticsSdk", "defaultShareSdk", "defaultAdsSdk", "defaultPushSdk" ] for sdkName in defalutSdkNames: if srcData.has_key(sdkName): destData[sdkName] = srcData[sdkName] # add sdk config to uuSdkConfig.json destSdks = [] if destData.has_key("sdks"): destSdks = destData["sdks"] destSdkMap = {} for sdk in destSdks: if sdk.has_key("name"): destSdkMap[sdk["name"]] = sdk if srcData.has_key("sdks"): srcSdks = srcData["sdks"] for sdk in srcSdks: if sdk.has_key("name"): sdkName = sdk["name"] if destSdkMap.has_key(sdkName): destSdks.remove(destSdkMap[sdkName]) destSdks.append(sdk) destSdkMap[sdkName] = sdk destData["sdks"] = destSdks destData = json.dumps(destData, ensure_ascii=False) with open(dest, "w") as fdest: fdest.write(destData)
def cut_image_py(icon_path,path,current_path): tempath =os.path.split(path)[0] ret = fileutils.copyFile(icon_path,os.path.join(tempath,'icon.png')) if ret: return ret print 'copy icon into %s success' %tempath command ='python '+os.path.join(current_path,'modify_icon.py')+' '+os.path.join(tempath,'icon.png') ret =utils.execCommand(command) if ret: print 'cut image fail' return ret icon_arr_path =os.path.join(tempath,'icon') modifyIcon(icon_arr_path,appPath) print 'modify icon success' #修改icon成功,开始清除中途产生的资源 fileutils.clearDir(icon_arr_path) os.rmdir(icon_arr_path) os.remove(os.path.join(tempath,'icon.png'))
def get_provision_path(rootPath): #拿到配置文件路径 profile_path = os.path.join(rootPath, 'Library/MobileDevice/Provisioning Profiles/') if not os.path.exists(profile_path): raise Exception('no ' + profile_path) new_profile_path = os.path.join(rootPath, 'Library', 'tprovison') #拿到新的配置文件路径 if not os.path.exists(new_profile_path): os.makedirs(new_profile_path) else: command = 'rm -rf ' + os.path.join(new_profile_path) utils.execCD(command) os.makedirs(new_profile_path) for files in os.listdir(profile_path): if os.path.splitext(files)[1] == '.mobileprovision': if fileutils.copyFile(os.path.join(profile_path, files), new_profile_path): raise Exception('copy file into provison/ fail') return new_profile_path
def modifyLogo(logoPath,appPath): try: return fileutils.copyFile(logoPath,appPath+'/logo.png') except Exception as e: raise e
def re_sign(appPath,certificateName,provisonpath): #证书名 # codeSign ='' #配置文件路径 mobileprovision_path =provisonpath #授权文件路径 entitlements_path ='' certificateName =certificateName.replace('kongge',' ') certificateName =certificateName.replace('kuohao','(') certificateName =certificateName.replace('fankuoshao',')') print 'certificate name :%s\n' %certificateName if os.path.exists(mobileprovision_path): ret =fileutils.copyFile(mobileprovision_path,os.path.join(appPath,'embedded.mobileprovision')) if ret: raise Exception('copy embedded.mobileprovision faid into'+appPath) print 'replace mobileprovision file success' else: print 'mobileprovision file don\'t exist' raise Exception('mobileprovision file don\'t exist') #生成getEntitlements_plist文件 entitlements_path=getEntitlements_plist(mobileprovision_path,appPath) if entitlements_path: print 'generate entitlements.plist success,path:%s' %entitlements_path else: raise Exception('generate entitlements.plist error') #修改bundleid appIdentifier =getAppIdentifier(entitlements_path) if appIdentifier: ret =modifyPlist('CFBundleIdentifier',appIdentifier,appPath) if ret: raise Exception('modify bundle id fail') else: print 'modify CFBundleIdentifier success value : %s' %appIdentifier else: raise Exception('getAppIdentifier fail') #找到需要签名的文件 print 'resursive file...\n' recursive_file(appPath) print 'resursive success \n' print 'start resign \n' print 'signfile: %s \n' %sign_arr for signfile in sign_arr: execStr = '/usr/bin/codesign -vvv -fs ' +'"'+certificateName+'"'+' --no-strict --entitlements '+entitlements_path+' '+signfile ret = utils.execCommand(execStr) if ret: raise Exception('sign fail --'+signfile) command = '/usr/bin/codesign -vvv -fs ' +'"'+certificateName+'"'+' --no-strict --entitlements '+entitlements_path+' '+appPath ret = utils.execCommand(command) if ret: print 'sign fail' raise Exception('sign fail --'+appPath) command ='/usr/bin/codesign --verify '+appPath ret =utils.execCommand(command) if ret: print 'sign error' raise Exception('verify sign fail') print '%s sign success \n' %appPath command ='/usr/bin/codesign -vv -d '+appPath utils.execCommand(command) #开始压缩 print 'start compress \n' Payload_path = os.path.join(os.path.split(appPath)[0],"Payload") if not os.path.exists(Payload_path): command = "mkdir " + Payload_path if utils.execCommand(command): raise Exception('mkdir Payload fail') print 'mk dir Payload success' else: for files in os.listdir(Payload_path): path = os.path.join(Payload_path,files) command ='rm -f '+path if utils.execCommand(command): raise Exception('remove fail '+path) command = "mv " + appPath + " " + Payload_path if utils.execCommand(command): raise Exception('move .app into Payload dir fail') print 'move .app into /Payload/ success' os.chdir(os.path.split(appPath)[0]) command = "zip -q -r -m " + os.path.join(os.path.split(appPath)[0],'re_sign_dev.ipa') + " " +'./Payload' ret =utils.execCommand(command) if ret: print 'zip fail' raise Exception('zip fail') print 'zip Payload success' print 'delete Payload dir success' for files in os.listdir(os.path.split(appPath)[0]): files_path = os.path.join(os.path.split(appPath)[0],files) if files!='re_sign_dev.ipa': command = 'rm -rf '+files_path utils.execCommand(command) return os.path.join(os.path.split(appPath)[0],'re_sign_dev.ipa')
def Build(configFile, toolPath): # configFile = "./config/config.json" if not os.path.exists(configFile): print "Error! no config file, path: %s" % configFile return config = Configer() config.loadConfig(configFile, toolPath) # unpack apk apkPath = config.getApkPath() if not apkPath: print "Error! no apk path, please set in config.json" return apkPath = os.path.abspath(apkPath) apkName = config.getApkName() destDir = config.getUnpackPath() if not destDir: print "Error! no dest dir, please set in config.json" return destDir = os.path.abspath(destDir) destPath = os.path.join(destDir, apkName) fileutils.copyFile(apkPath, destPath) apkutils.init(toolPath) unpackPath = os.path.join(destDir, "apk_unpack") ret = apkutils.decompileApk(destPath, unpackPath) if ret: return basePath = unpackPath sdks = config.getSdkList() if len(sdks) > 0: basePath = os.path.join(destDir, "apk_base") if os.path.exists(basePath): fileutils.clearDir(basePath) fileutils.copyFiles(unpackPath, basePath) packageName = "" # copy to base dir for sdk in sdks: destPath = basePath # obtain sdk config srcSdkDir = config.getSdkDir(sdk) srcSdkConfigDir = config.getSdkConfigDir(sdk) sdkConfigFile = os.path.join(srcSdkConfigDir, "config.json") if os.path.exists(sdkConfigFile): sdkConfig = copy.deepcopy(config) sdkConfig.loadConfig(sdkConfigFile, None) else: sdkConfig = config # obtain mainfest file manifestFile = os.path.abspath(os.path.join(destPath, "AndroidManifest.xml")) # copy res files ret = apkutils.copySdkFilesToApk(sdk, srcSdkDir, destPath, manifestFile, sdkConfig.getAppOrientation()) if ret: print "[ %s ] Error! copy sdk files to apk" % sdk return # modify aonesdk.json newChannelName = sdkConfig.getChannelName() if newChannelName is not None and len(newChannelName) > 0: destAonesdkJson = os.path.join(destPath, "assets/aonesdk.json") ret = apkutils.modifyAonesdkJsonChannelName(destAonesdkJson, newChannelName) if ret: print "[ %s ] Error! modify aonesdk.json" % sdk return # modify package name if packageName != config.getPackageName(): packageName = apkutils.modifyPackageName(sdk, manifestFile, config.getPackageName()) # run special script ret = runSpecial(sdk, srcSdkDir, destPath, manifestFile, sdkConfigFile) if ret: return # specialScript = os.path.join(srcSdkDir, "special.py") # if os.path.exists(specialScript): # sysPath = copy.deepcopy(sys.path) # sys.path.append(os.path.abspath(srcSdkDir)) # import special # ret = special.run(sdk, manifestFile, sdkConfigFile, os.path.abspath(destPath)) # if ret: # print "[ %s ] Error! run special script" % sdk # return # del sys.modules["special"] # sys.path = sysPath # copy to new channel_sdk dir channelSdks = config.getChannelSdkList() for sdk in channelSdks: destPath = os.path.join(destDir, "apk_" + sdk) if os.path.exists(destPath) and sdk != "base": fileutils.clearDir(destPath) if basePath != destPath: fileutils.copyFiles(basePath, destPath) # obtain sdk config srcSdkDir = config.getSdkDir(sdk) srcSdkConfigDir = config.getSdkConfigDir(sdk) sdkConfigFile = os.path.join(srcSdkConfigDir, "config.json") if os.path.exists(sdkConfigFile): sdkConfig = copy.deepcopy(config) sdkConfig.loadConfig(sdkConfigFile, None) else: sdkConfig = config # obtain mainfest file manifestFile = os.path.abspath(os.path.join(destPath, "AndroidManifest.xml")) # copy res files ret = apkutils.copySdkFilesToApk(sdk, srcSdkDir, destPath, manifestFile, sdkConfig.getAppOrientation()) if ret: print "[ %s ] Error! copy sdk files to apk" % sdk return # modify aonesdk.json srcAonesdkJson = os.path.join(srcSdkDir, "assets/aonesdk.json") destAonesdkJson = os.path.join(destPath, "assets/aonesdk.json") ret = apkutils.modifyAonesdkJson(srcAonesdkJson, destAonesdkJson) if ret: print "[ %s ] Error! modify aonesdk.json" % sdk return # add splash if sdkConfig.getHasSplash() == "True": apkutils.addSplash(sdk, srcSdkDir, destPath) # modify package name if packageName != sdkConfig.getPackageName() or sdk != "base": packageName = apkutils.modifyPackageName(sdk, manifestFile, sdkConfig.getPackageName()) # run special script ret = runSpecial(sdk, srcSdkDir, destPath, manifestFile, sdkConfigFile) if ret: return # specialScript = os.path.join(srcSdkDir, "special.py") # if os.path.exists(specialScript): # sysPath = copy.deepcopy(sys.path) # sys.path.append(os.path.abspath(srcSdkDir)) # import special # ret = special.run(sdk, manifestFile, sdkConfigFile, os.path.abspath(destPath)) # if ret: # print "[ %s ] Error! run special script" % sdk # return # del sys.modules["special"] # sys.path = sysPath # todo. modify app name # apkutils.modifyAppName(sdk, manifestFile, sdkConfig) # generate R file ret = apkutils.generateR(sdk, destPath, manifestFile, packageName) if ret: print "[ %s ] Error! generate R file" % sdk return # run special after generating R file for otherSdk in sdks: ret = runSpecialAfterR(otherSdk, config.getSdkDir(otherSdk), destPath, manifestFile, sdkConfigFile) if ret: return # modify icon ret = apkutils.modifyIcon(sdk, srcSdkDir, destPath) if ret: print "[ %s ] Error! modify icon" % sdk return # compile apk newApkName = apkName[0:-4] + "_" + sdk + ".apk" newApkPath = destDir newApkPath = os.path.join(newApkPath, newApkName) newApkPath = os.path.abspath(newApkPath) ret = apkutils.compileApk(newApkPath, destPath) if ret: print "[ %s ] Error! compile apk" % sdk return # add unknown files to apk unknownDir = os.path.join(destPath, "unknown") if os.path.exists(unknownDir): apkutils.addUnknownFilesToApk(newApkPath, unknownDir) # sign apk ret = apkutils.signApk(newApkPath, sdkConfig.getKeyStoreData()) if ret: print "[ %s ] Error! sign apk" % sdk return # align apk destApkPath = os.path.join(os.path.dirname(apkPath), "output") if not os.path.exists(destApkPath): os.mkdir(destApkPath) destApkPath = os.path.join(destApkPath, newApkName) destApkPath = os.path.abspath(destApkPath) ret = apkutils.alignApk(newApkPath, destApkPath) if ret: print "[ %s ] Error! align apk" % sdk return