[Python] 纯文本查看 复制代码
import os
import re
import concurrent.futures
import datetime
import json
import subprocess
import logging
import re
from multiprocessing import Process
###定义类###
class APK:
def __init__(self, name, url, dir_path, file_path):
self.__name = name
self.__url = url
self.__dir_path = dir_path
self.__file_path = file_path
def get_name(self):
return self.__name
def get_url(self):
return self.__url
def get_dir_path(self):
return self.__dir_path
def get_file_path(self):
return self.__file_path
###下载模块###
def download_apk(apk):
try:
if 'make.file' in apk.get_url():
cmd = ["wget", token, "-O Android.mk", apk.get_url()]
else:
cmd = ["wget", token, apk.get_url()]
sp_output = None
if os.path.exists(apk.get_dir_path()):
os.chdir(apk.get_dir_path())
for i in ["app-release.apk", "Android.mk", "app-debug.apk"]:
if os.path.exists(i):
os.remove(i)
logging.info("%s start download ", apk.get_file_path())
logging.info("Download url is %s ",apk.get_url())
sp_output = subprocess.check_output(" ".join(cmd), shell=True, stderr=subprocess.PIPE)
logging.info("%s download successfully", apk.get_file_path())
else:
os.makedirs(apk.get_dir_path())
os.chdir(apk.get_dir_path())
logging.info("%s start download ", apk.get_file_path())
logging.info("Download url is %s ",apk.get_url())
sp_output = subprocess.check_output(" ".join(cmd), shell=True, stderr=subprocess.PIPE)
logging.info("%s download successfully", apk.get_file_path())
except subprocess.CalledProcessError as e:
logging.error(e)
print(sp_output)
print(e.output)
raise e
if __name__=='__main__':
log_file = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
logging.basicConfig(level=logging.INFO, filename=log_file, filemode='a', format='%(asctime)s : %(message)s')
url_prefix = 'http://ip:port/dir/apps'
down_dir = '/home/xxx/xxx'
token = "--user=xxx --password=xxx"
###处理字符串,返回下载列表###
file_list = []
with open('result.json','r') as f:
for number,line in enumerate(f,start=1):
if "V_Build" in line:
continue
if 'make.file' in line or 'apk' in line:
line_formated = line.split(':')[-1].replace('"','').replace(',','').replace('\n','').replace(' ','')
res = re.search(r"\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}", line_formated)
if not res:
file_list.append(line_formated)
print(file_list)
print(len(file_list))
###获取类的集合###
down_list = []
for name in file_list:
dir_path = '/'.join(name.split('/')[:-1])
abs_dir_path = down_dir + dir_path
abs_file_path = abs_dir_path + name.split('/')[-1]
url = url_prefix + name
apk = APK(name, url, abs_dir_path, abs_file_path)
down_list.append(apk)
###线程/进程集合###
task = []
# for apk in down_list:
# p = Process(target=download_apk,args=(apk,))
# p.start()
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as e:
for apk in down_list:
task.append(e.submit(download_apk, apk))
###读取返回值###
for i in task:
if i:
print(i)