def compressModule(inDir=None, outDir=None, accountList=None, just_check=False): try: validateApiKey(curApiKeyIndex) except Exception, exc: print exc util.safeQuit(None, None)
def compressSinglePicLogic(src, dest, keyName, mintor_md5_filename): global picErrorCount global serverErrorCount global curApiKeyIndex # print "compressSinglePicLogic()...%s" %src ret = compressSinglePic(src, dest) if 0 == ret: oldSize = os.path.getsize(src) newSize = os.path.getsize(dest) print "%s compressed from:%s to:%s -------->saving:%s" % ( keyName, oldSize, newSize, oldSize - newSize) # 3,删除源文件,把新文件重新命名为源文件 shutil.move(dest, src) # 1,在md5文件中删除老的一行,并把新的md5写入 util.deleteLine(mintor_md5_filename, keyName) newMd5 = util.generateMd5_2(src) md5Line = "%s--MD5-->%s" % (keyName, newMd5) # print md5Line util.writeLine(mintor_md5_filename, md5Line) elif -1 == ret: curApiKeyIndex += 1 validateApiKey(curApiKeyIndex) print "retry compressPic()..-1.src:%s" % src compressSinglePicLogic(src, dest, keyName, mintor_md5_filename) elif -2 == ret: util.safeQuit(None, None) elif -3 == ret: if serverErrorCount >= MAX_SERVER_ERROR_COUNT: util.safeQuit(None, None) else: serverErrorCount += 1 print "we will retry in 2 minute later!!" time.sleep(60 * 2) print "retry compressPic()..-3.src:%s" % src compressSinglePicLogic(src, dest, keyName, mintor_md5_filename) pass elif -4 == ret: if picErrorCount >= MAX_PIC_ERROR_COUNT: util.safeQuit(None) else: picErrorCount += 1 print "retry compressPic()..-4.src:%s" % src compressSinglePicLogic(src, dest, keyName, mintor_md5_filename) pass elif -5 == ret: util.safeQuit(None, None) else: util.safeQuit(None, None) pass
def remove_resource_file(issue, filepath, ignore_layouts): """ Delete a file from the filesystem """ print "remove_resource_file()...%s --> %s %s" % (issue.elements[0][0], filepath, ignore_layouts) if os.path.exists(filepath) and (ignore_layouts is False or issue.elements[0][0] != 'layout'): if JUST_CHECK: title = "you have unUnused Resource from lint check!!!" content = "please local run:python lcba/lintResource.py to delete unUsedResources!!!" print title print content glo.set_value('_reason', title) glo.set_value('_title', title) glo.set_value('_content', content) util.safeQuit(None, None) pass # tory fix 如果是资源文件保存id if issue.elements[0][0] == 'layout': if not LAYOUT_IDS_PATH: print "you need to set the id path to save the layout @+id/***" util.safeQuit(None, None) pass util.createIdsFile(LAYOUT_IDS_PATH) print "remove_resource_file()...delete layout:%s where idsPath:%s" % ( filepath, ids_path) with open(filepath) as f: for line in f.readlines(): # print "layout_line:%s"%line #多行并列的情况怎么搞 pattern = re.compile('@\+id/\\b[^"]+') match_result = re.findall(pattern, line) #print "match_resutl:%s"%match_result if match_result: bits = match_result[0].rsplit('/', 2) #print "bits0:%s bits1:%s"%(bits[0],bits[1]) line = '<item name="%s" type="id" />' % bits[1] print "write:%s to file:%s" % (line, ids_path) util.writeIdsLine(ids_path, line) pass pass pass pass print('removing resource: {0}'.format(filepath)) os.remove(os.path.abspath(filepath))
def useAndResGuard(inputApk, outDir, configPath=None): commandLine = "java -jar " curPath = os.path.dirname(os.path.realpath(__file__)) andResJarPath = os.path.join(curPath, 'AndResGuard-cli-1.2.0.jar') if not os.path.exists(andResJarPath): print "AndResGuard-cli-1.2.0.jar not exists !!!" util.safeQuit(None, None) pass commandLine = commandLine + " %s " % andResJarPath if not os.path.exists(inputApk): print "inputApk:%s not exists !!!" % inputApk return pass commandLine = commandLine + (" %s " % inputApk) print "configPath:%s" % configPath if not configPath or not os.path.exists(configPath): configPath = os.path.join(curPath, 'AndResGuard_config.xml') if not os.path.exists(configPath): print "AndResGuard_config.xml not exists!!! path:%s" % configPath util.safeQuit(None, None) pass commandLine = commandLine + " -config %s " % configPath commandLine = commandLine + " -out %s " % outDir output = os.popen('which 7za') zipTool = output.read() if zipTool.strip() == '': print "you have not install 7za command!!!" print "window: 对于window应下载命名行版本,若将7za指定到环境变量,即无须设置。地址:http://sparanoid.com/lab/7z/download.html" print "linux:sudo apt-get install p7zip-full" print "mac:brew install p7zip" util.safeQuit(None, None) pass zipAlign_path = os.path.join(curPath, 'zipalign') if not os.path.exists(zipAlign_path): print "not find zipalign tools,zipalign may in your $ANDROID_SDK/build-tools/25.0.1/zipalign" print "please config const.ZIPALIGN_PATH !!!" util.safeQuit(None, None) pass commandLine = commandLine + " -zipalign %s " % zipAlign_path # print commandLine # (status, output) = commands.getstatusoutput(commandLine) # print status,output print "###############################" print "Now begin sign and zipalign : %s" % commandLine print "###############################" os.system(commandLine)
def validateApiKey(index): global curApiKeyIndex if index >= len(ACCOUNT_KEY_LIST): util.safeQuit(None, None) return print "validateApiKey()... %s" % ACCOUNT_KEY_LIST[index] try: tinify.key = ACCOUNT_KEY_LIST[index] tinify.validate except tinify.Error, e: # Validation of API key failed. print "The error message is: %s index:%s" % (e.message, curApiKeyIndex) if index >= ACCOUNT_KEY_LIST.size(): util.safeQuit(None, None) return else: curApiKeyIndex += 1 validateApiKey(curApiKeyIndex) pass
def checkMintorDir(app_dir, mintor_dir_path, mintor_md5_filepath): print "checkMintorDir()...%s from file: %s where module is:%s" % ( mintor_dir_path, mintor_md5_filepath, app_dir) if not app_dir or not mintor_dir_path or not mintor_md5_filepath: print "###########ERROR###############" print "checkMintorDir param error app_dir:%s mintor_dir_path:%s mintor_md5_filepath:%s" % ( app_dir, mintor_dir_path, mintor_md5_filepath) return pass # 读区已经压缩过文件和md5到hashmap md5HashMap = HashMap() if os.path.exists(mintor_md5_filepath): for line in open(mintor_md5_filepath): # print line line = line.replace('\r', '').replace('\n', '').replace('\t', '') lineItems = line.rsplit('--MD5-->', 2) #print "%s %s" %(lineItems[0],lineItems[1]) md5HashMap.add(lineItems[0], lineItems[1]) pass else: f = open(mintor_md5_filepath, 'w') f.close() pass # 遍历被监控的文件夹,计算新的md5值和老的md5Hashmap值是否相等 for dirpath, dirnames, filenames in os.walk(mintor_dir_path): # print "dirpath:%s dirnames:%s filenames:%s" %(dirpath,dirnames,filenames) # if "build" not in dirpath and "intermediates" not in dirpath and "generated" not in dirpath and "outputs" not in dirpath and "gen" not in dirpath: # continue for file in filenames: fullpath = os.path.join(dirpath, file) simplepath = fullpath.replace(app_dir, '') if simplepath and simplepath.startswith('/'): simplepath = simplepath[1:len(simplepath)] pass # print "this is: %s" %simplepath # 9.png 压缩了之后不能缩放 if simplepath.endswith(".9.png"): print "ignore: %s" % simplepath elif simplepath.endswith(".png") or simplepath.endswith(".jpg"): oldMd5 = None try: oldMd5 = md5HashMap.get(simplepath) except KeyError, e: pass md5 = util.generateMd5_2(fullpath) print "\ncompressing %s oldMd5:%s md5:%s" % (simplepath, oldMd5, md5) if cmp(oldMd5, md5) == 0: print "%s is already compressed!!! skiped" % simplepath else: if JUST_CHECK: title = "you have picture not compressed yet!!!" content = "please local run:python lcba/lintCompress.py to compress Resources!!!" print title print content glo.set_value('_reason', title) glo.set_value('_title', title) glo.set_value('_content', content) util.safeQuit(None, None) pass # 2,上传tinypng压缩图片 compressSinglePicLogic(fullpath, fullpath + ".tiny", simplepath, mintor_md5_filepath) pass pass pass