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
|
import os import json import requests import zipfile
try: import configparser except ImportError: import ConfigParser
Auto_config_path = os.path.dirname(__file__) + '/auto.config'
try: cf = configparser.ConfigParser() print('python3') except Exception as e: cf = ConfigParser.ConfigParser() print('python2')
cf.read(Auto_config_path)
sdk_dir = cf.get('app', 'sdk_dir') git_download_name = cf.get('app', 'git_download_name') assembleRelease = cf.getboolean('app', 'assembleRelease') git_download_url = 'https://codeload.github.com/' + git_download_name + '/zip/master'
base_file_dir = cf.get('dir', 'base_file_dir') dir_name = cf.get('dir', 'dir_name') code_dir_name = cf.get('dir', 'code_dir_name') apk_dir_name = cf.get('dir', 'apk_dir_name') code_zip_name = cf.get('dir', 'code_zip_name') + ".zip" adb_dir = cf.get('dir', 'adb_dir') gradle_dir = cf.get('dir', 'gradle_dir')
file_dir = base_file_dir + '/' + dir_name code_dir = file_dir + '/' + code_dir_name apk_dir = file_dir + "/" + apk_dir_name
if not os.path.exists(file_dir): os.mkdir(file_dir) os.chdir(file_dir) if not os.path.exists(code_dir): os.mkdir(code_dir) if not os.path.exists(apk_dir): os.mkdir(apk_dir) os.chdir(code_dir)
def cleanFile(targetDir): for file in os.listdir(targetDir): targetFile = os.path.join(targetDir, file) if os.path.isfile(targetFile): os.remove(targetFile)
cleanFile(code_dir)
print('下载地址: ' + git_download_url) print('下载中...') response = requests.get(git_download_url) with open(code_zip_name, "wb") as code: code.write(response.content) print(response.status_code) if response.status_code == 200: print('下载完成!') else: print('下载失败!')
z = zipfile.ZipFile(code_zip_name, 'r') zip_name = z.namelist()[0] try: for file in z.namelist(): z.extract(file, code_dir) print('解压完成!') except Exception as e: print('解压失败!')
code_dir = code_dir + "/" + zip_name def createLocalPropertiesFile(sourceDir,fileName,root_sdk_dir): if not os.path.exists(sourceDir): return
fileDir = sourceDir + '/' + fileName
if os.path.exists(fileDir): return
f = open(fileDir, 'w') f.write('sdk.dir=' + root_sdk_dir) f.close()
createLocalPropertiesFile(code_dir, 'local.properties', sdk_dir)
os.chdir(code_dir)
print('gradle clean') os.system(gradle_dir + ' clean') print('gradle assemble, generate apk')
if assembleRelease: print('assembleRelease apk') os.system(gradle_dir + ' assembleRelease') else: print('assembleDebug apk') os.system(gradle_dir + ' assembleDebug')
print('Generate apk finish!')
def moveApk(sourceDir, targetDir): if not os.path.exists(sourceDir): return
for file in os.listdir(sourceDir): sourceFile = os.path.join(sourceDir, file) targetFile = os.path.join(targetDir, file) if os.path.isfile(sourceFile) and ('unaligned' not in os.path.basename(sourceFile)): open(targetFile, "wb").write(open(sourceFile, "rb").read())
source_apk_dir = code_dir + '/' + 'app/build/outputs/apk'
if os.path.exists(source_apk_dir): moveApk(source_apk_dir, apk_dir)
print('apk 复制成功!')
print('abd install') os.system(adb_dir + ' install ' + apk_dir + "/app-debug.apk") print('安装成功!')
|