This commit is contained in:
wuaho 2021-06-02 20:13:21 +08:00
parent 937d6d56d8
commit f649403571
8 changed files with 542 additions and 263 deletions

View File

@ -4,11 +4,11 @@ import os
import stat
import shutil
import json
print(os.path.abspath(__name__))
import sys
RD, WD, XD = 4, 2, 1
BNS = [RD, WD, XD]
MDS = [
@ -17,6 +17,7 @@ MDS = [
[stat.S_IXUSR, stat.S_IXGRP, stat.S_IXOTH]
]
def chmod(path, mode):
if isinstance(mode, int):
mode = str(mode)
@ -30,6 +31,7 @@ def chmod(path, mode):
mode_num += MDS[bnidx][midx]
# os.chmod(path, mode_num)
# 遍历res下的所有文件
def isFindApk(parent):
for i in os.listdir(parent):
@ -39,6 +41,7 @@ def isFindApk (parent):
print('no apk file=====')
return ''
def readChannelfile(filename):
try:
print(filename)
@ -49,6 +52,7 @@ def readChannelfile(filename):
exit(0)
channelList = json.load(f)
def backUpManifest():
if os.path.exists('./AndroidManifest.xml'):
os.remove('./AndroidManifest.xml')
@ -56,6 +60,7 @@ def backUpManifest():
shutil.copyfile(manifestPath, './AndroidManifest.xml')
print('manifest info--%s' % manifestPath)
def modifyChannel(value):
tempXML = ''
f = open('./AndroidManifest.xml')
@ -73,7 +78,8 @@ def modifyChannel(value):
line = '<meta-data android:name="owner" android:value="%s"/>' % (owner)
elif line.find('package') > 0 and packageId != '':
print('package====%s' % line)
line = '<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="30" android:compileSdkVersionCodename="11" android:installLocation="auto" package="%s" platformBuildVersionCode="30" platformBuildVersionName="11">' % (packageId)
line = '<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="30" android:compileSdkVersionCodename="11" android:installLocation="auto" package="%s" platformBuildVersionCode="30" platformBuildVersionName="11">' % (
packageId)
elif line.find('appid') > 0 and appid != '':
print('appid====%s' % line)
line = '<meta-data android:name="appid" android:value="%s"/>' % (appid)
@ -146,7 +152,8 @@ def modifyChannel(value):
chmod(signedjar, "777")
chmod(unsignedjar, "777")
cmd_sign = r'jarsigner -verbose -digestalg SHA1 -sigalg MD5withRSA -keystore %s -storepass %s -keypass %s -signedjar %s %s %s'% (keystore, storepass, keypass, signedjar, unsignedjar, alianame)
cmd_sign = r'jarsigner -verbose -digestalg SHA1 -sigalg MD5withRSA -keystore %s -storepass %s -keypass %s -signedjar %s %s %s' % (
keystore, storepass, keypass, signedjar, unsignedjar, alianame)
os.system(cmd_sign)
os.remove(unsignedjar);
@ -154,7 +161,7 @@ def modifyChannel(value):
channelList = {}
#
os.system('java -jar apkool.jar empty-framework-dir --force')
readChannelfile('./channel')
readChannelfile('channel.json')
apkName = ""
apkNameSuffix = "0"
@ -192,11 +199,10 @@ lg_reyun_io_appkey = ''
lg_reyun_io_channelId = ''
eventType = ''
# ----------------------- third qudao end------------------------
print ('-------------------- your channel values --------------------')
#print 'channel list: ', channelList
print('-------------------- your channel.json values --------------------')
# print 'channel.json list: ', channelList
if 'apkname' in channelList:
print('apkname==%s' % channelList['apkname'])
@ -207,7 +213,6 @@ if 'apkname' in channelList :
else:
apkName = isFindApk('./')
if 'packageId' in channelList:
print('packageId==%s' % channelList['packageId'])
print('packageId value==%s' % channelList['packageId']['value'])
@ -255,7 +260,6 @@ if 'num' in channelList :
else:
print("no key num")
if 'appid' in channelList:
print('appid==%s' % channelList['appid'])
print('appid value==%s' % channelList['appid']['value'])
@ -361,9 +365,6 @@ if 'eventType' in channelList :
else:
print("no key eventType")
output_apk_dir = "./bin"
if os.path.exists(output_apk_dir):
shutil.rmtree(output_apk_dir)

View File

@ -1 +0,0 @@
{"leguChannel": {"value": "test7", "suffix": "1"}, "num": {"value": "1", "start": "1"}, "keystore": {"filename": "/www/wwwroot/legusdk/app/Components/SDK/ApkTool/keystore/legu729.keystore", "storepass": "123456", "alianame": "legu", "keypass": "123456"}}

17
ApkTool/channel.json Normal file
View File

@ -0,0 +1,17 @@
{
"apkname":{"value":"/data/bale_apk/ApkTool/gmswz_TT_gy_01.apk"},
"leguChannel": {
"value": "test7",
"suffix": "1"
},
"num": {
"value": "1",
"start": "1"
},
"keystore": {
"filename": "/www/wwwroot/legusdk/app/Components/SDK/ApkTool/keystore/legu729.keystore",
"storepass": "123456",
"alianame": "legu",
"keypass": "123456"
}
}

View File

@ -13,13 +13,13 @@ router = APIRouter()
#
# @router.post("/channel")
# async def channel(
# @router.post("/channel.json")
# async def channel.json(
# data_in: schemas.BaleCreate
# ) -> schemas.Msg:
# file = os.path.join(settings.ROOT_DIR, 'ApkTool/channel')
# file = os.path.join(settings.ROOT_DIR, 'ApkTool/channel.json')
# with open(file, 'w') as f:
# json.dump(data_in.channel, f)
# json.dump(data_in.channel.json, f)
#
# resp = schemas.Msg(
# code=0,
@ -130,7 +130,7 @@ async def run(
)
return resp
file = os.path.join(settings.ROOT_DIR, 'ApkTool/channel')
file = os.path.join(settings.ROOT_DIR, 'ApkTool/channel.json')
with open(file, 'w') as f:
json.dump(data_in.channel, f)

View File

@ -1,3 +1,8 @@
from utils import bale
import os
bale.run_bale_apk('aa',334)
from utils import MakeTool
from core.config import settings
make_tool = MakeTool(os.path.join(settings.ROOT_DIR, 'ApkTool/channel.json'))
a, b = make_tool.run()
print(a, b)

View File

@ -1,2 +1,3 @@
# coding:utf-8
from .bale import bale
from .make_tool import MakeTool

View File

@ -5,6 +5,7 @@ import os
import requests
from core.config import settings
from .make_tool import MakeTool
from .put_file import put_file
@ -15,13 +16,25 @@ class Bale:
def run_bale_apk(cls, dir_, id_):
try:
pkg_num = 0
with open(os.path.join(settings.ROOT_DIR, 'ApkTool/channel'), 'r') as f:
with open(os.path.join(settings.ROOT_DIR, 'ApkTool/channel.json'), 'r') as f:
props = json.load(f)
pkg_num = int(props.get('num', {}).get('value', 0))
cls.status = 'busy'
cmd = os.path.join(settings.ROOT_DIR, 'ApkTool/run.sh')
os.system(cmd)
make_tool = MakeTool(os.path.join(settings.ROOT_DIR, 'ApkTool/channel.json'))
a, b = make_tool.run()
if not a:
data = {
'id': id_,
'url': '',
'apkname': '',
'total_pkg': pkg_num,
'current_pkg': 0,
'done': 0,
'code': -1,
'msg': b
}
requests.post('http://gamesdk.legu.cc/api/subpackageRecord/getRecord', data=data)
path = os.path.join(settings.ROOT_DIR, 'ApkTool/bin')
if not os.path.exists(path):
@ -36,14 +49,27 @@ class Bale:
is_ok, msg = put_file(f'{dir_}/{item}', os.path.join(path, item))
os.remove(os.path.join(path, item))
if not is_ok:
continue
data = {
'id': id_,
'url': msg,
'apkname': item,
'done': int(count >= pkg_num)
'total_pkg': pkg_num,
'current_pkg': count,
'done': int(count >= pkg_num),
'code': -1,
'msg': msg
}
resp = requests.post('http://gamesdk.legu.cc/api/subpackageRecord/getRecord', data=data)
requests.post('http://gamesdk.legu.cc/api/subpackageRecord/getRecord', data=data)
data = {
'id': id_,
'url': msg,
'apkname': item,
'total_pkg': pkg_num,
'current_pkg': count,
'done': int(count >= pkg_num),
'code': 0
}
requests.post('http://gamesdk.legu.cc/api/subpackageRecord/getRecord', data=data)
print(f'上传:{item} 完成')

230
utils/make_tool.py Normal file
View File

@ -0,0 +1,230 @@
# coding:utf-8
import json
import os
import shutil
from core.config import settings
class MakeTool:
MakeDir = os.path.join(settings.ROOT_DIR, 'ApkTool')
def __init__(self, channel_path):
self.channel_path = channel_path
self.apkName = ''
self.apkNameSuffix = '0'
self.easyName = ''
self.easyNameSuffix = '0'
self.packageId = ''
self.packageIdSuffix = '0'
self.keystore = ''
self.storepass = ''
self.alianame = ''
self.keypass = ''
self.owner = ''
self.packNum = 0
self.start = 0
self.appid = ''
self.gism_id = ''
self.gism_name = ''
self.gism_channel = ''
self.lg_ad_id = ''
self.lg_ad_name = ''
self.gactionId = ''
self.gappSecretKey = ''
self.baiduId = ''
self.baiduSecretKey = ''
self.lg_reyun_game_appkey = ''
self.lg_reyun_game_channelId = ''
self.lg_reyun_io_appkey = ''
self.lg_reyun_io_channelId = ''
self.eventType = ''
def _init_props(self):
# 提取参数
props = dict()
with open(self.channel_path, 'r') as f:
props = json.load(f)
# 母包路径
self.apkName = props.get('apkname', {}).get('value') or self.apkName
self.apkNameSuffix = props.get('apkname', {}).get('suffix') or self.apkNameSuffix
self.easyName = props.get('leguChannel', {}).get('value') or self.easyName
self.easyNameSuffix = props.get('leguChannel', {}).get('suffix') or self.easyNameSuffix
self.packageId = props.get('packageId', {}).get('value') or self.packageId
self.packageIdSuffix = props.get('packageId', {}).get('suffix') or self.packageIdSuffix
self.keystore = props.get('keystore', {}).get('filename') or self.keystore
self.storepass = props.get('keystore', {}).get('storepass') or self.storepass
self.alianame = props.get('keystore', {}).get('alianame') or self.alianame
self.keypass = props.get('keystore', {}).get('keypass') or self.keypass
self.owner = props.get('owner', {}).get('value') or self.owner
self.packNum = props.get('num', {}).get('value') or self.packNum
self.start = props.get('num', {}).get('start') or self.start
self.start = int(self.start)
self.packNum = int(self.packNum) + 1
self.appid = props.get('appid', {}).get('value') or self.appid
self.gism_id = props.get('gism_id', {}).get('value') or self.gism_id
self.gism_name = props.get('gism_name', {}).get('value') or self.gism_name
self.gism_channel = props.get('gism_channel', {}).get('value') or self.gism_channel
self.lg_ad_id = props.get('lg_ad_id', {}).get('value') or self.lg_ad_id
self.lg_ad_name = props.get('lg_ad_name', {}).get('value') or self.lg_ad_name
self.gactionId = props.get('gactionId', {}).get('value') or self.gactionId
self.gappSecretKey = props.get('gappSecretKey', {}).get('value') or self.gappSecretKey
self.baiduId = props.get('baiduId', {}).get('value') or self.baiduId
self.baiduSecretKey = props.get('baiduSecretKey', {}).get('value') or self.baiduSecretKey
self.lg_reyun_game_appkey = props.get('lg_reyun_game_appkey', {}).get('value') or self.lg_reyun_game_appkey
self.lg_reyun_game_channelId = props.get('lg_reyun_game_channelId', {}).get(
'value') or self.lg_reyun_game_channelId
self.lg_reyun_io_appkey = props.get('lg_reyun_io_appkey', {}).get('value') or self.lg_reyun_io_appkey
self.lg_reyun_io_channelId = props.get('lg_reyun_io_channelId', {}).get('value') or self.lg_reyun_io_channelId
self.eventType = props.get('eventType', {}).get('value') or self.eventType
def _init_path(self):
bin_dir = os.path.join(self.MakeDir, 'bin')
if os.path.exists(bin_dir):
shutil.rmtree(bin_dir)
os.makedirs(bin_dir)
def check_props(self):
if not os.path.exists(self.apkName):
return False, '缺少apkName路径'
return True, 'ok'
def modify_channel(self, value):
tempXML = ''
with open(os.path.join(self.MakeDir, 'AndroidManifest.xml')) as f:
for line in f:
if line.find('leguChannel') > 0 and self.easyName != '':
if self.easyNameSuffix == "1":
if value < 10:
line = '<meta-data android:name="leguChannel" android:value="%s_0%d"/>' % (
self.easyName, value)
else:
line = '<meta-data android:name="leguChannel" android:value="%s_%d"/>' % (
self.easyName, value)
else:
line = '<meta-data android:name="leguChannel" android:value="%s"/>' % (self.easyName)
elif line.find('owner') > 0 and self.owner != '':
print('owner====%s' % line)
line = '<meta-data android:name="owner" android:value="%s"/>' % (self.owner)
elif line.find('package') > 0 and self.packageId != '':
print('package====%s' % line)
line = '<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="30" android:compileSdkVersionCodename="11" android:installLocation="auto" package="%s" platformBuildVersionCode="30" platformBuildVersionName="11">' % (
self.packageId)
elif line.find('appid') > 0 and self.appid != '':
print('appid====%s' % line)
line = '<meta-data android:name="appid" android:value="%s"/>' % (self.appid)
elif line.find('gism_id') > 0 and self.gism_id != '':
print('gism_id====%s' % line)
line = '<meta-data android:name="gism_id" android:value="%s"/>' % (self.gism_id)
elif line.find('gism_name') > 0 and self.gism_name != '':
print('gism_name====%s' % line)
line = '<meta-data android:name="gism_name" android:value="%s"/>' % (self.gism_name)
elif line.find('gism_channel') > 0 and self.gism_channel != '':
print('gism_channel====%s' % line)
line = '<meta-data android:name="gism_channel" android:value="%s"/>' % (self.gism_channel)
elif line.find('lg_ad_id') > 0 and self.lg_ad_id != '':
print('lg_ad_id====%s' % line)
line = '<meta-data android:name="lg_ad_id" android:value="%s"/>' % (self.lg_ad_id)
elif line.find('lg_ad_name') > 0 and self.lg_ad_name != '':
print('lg_ad_name====%s' % line)
line = '<meta-data android:name="lg_ad_name" android:value="%s"/>' % (self.lg_ad_name)
elif line.find('gactionId') > 0 and self.gactionId != '':
print('gactionId====%s' % line)
line = '<meta-data android:name="gactionId" android:value="%s"/>' % (self.gactionId)
elif line.find('gappSecretKey') > 0 and self.gappSecretKey != '':
print('gappSecretKey====%s' % line)
line = '<meta-data android:name="gappSecretKey" android:value="%s"/>' % (self.gappSecretKey)
elif line.find('baiduId') > 0 and self.baiduId != '':
print('baiduId====%s' % line)
line = '<meta-data android:name="baiduId" android:value="%s"/>' % (self.baiduId)
elif line.find('baiduSecretKey') > 0 and self.baiduSecretKey != '':
print('baiduSecretKey====%s' % line)
line = '<meta-data android:name="baiduSecretKey" android:value="%s"/>' % (self.baiduSecretKey)
elif line.find('lg_reyun_game_appkey') > 0 and self.lg_reyun_game_appkey != '':
print('lg_reyun_game_appkey====%s' % line)
line = '<meta-data android:name="lg_reyun_game_appkey" android:value="%s"/>' % (
self.lg_reyun_game_appkey)
elif line.find('lg_reyun_game_channelId') > 0 and self.lg_reyun_game_channelId != '':
print('lg_reyun_game_channelId====%s' % line)
line = '<meta-data android:name="lg_reyun_game_channelId" android:value="%s"/>' % (
self.lg_reyun_game_channelId)
elif line.find('lg_reyun_io_appkey') > 0 and self.lg_reyun_io_appkey != '':
print('lg_reyun_io_appkey====%s' % line)
line = '<meta-data android:name="lg_reyun_io_appkey" android:value="%s"/>' % (
self.lg_reyun_io_appkey)
elif line.find('lg_reyun_io_channelId') > 0 and self.lg_reyun_io_channelId != 0:
print('lg_reyun_io_channelId====%s' % line)
line = '<meta-data android:name="lg_reyun_io_channelId" android:value="%s"/>' % (
self.lg_reyun_io_channelId)
elif line.find('eventType') > 0 and self.eventType != '':
print('eventType====%s' % line)
line = '<meta-data android:name="eventType" android:value="%s"/>' % (self.eventType)
tempXML += line
with open(os.path.join(self.MakeDir, 'temp/AndroidManifest.xml'), 'w') as f:
f.write(tempXML)
unsignApk = os.path.join(self.MakeDir, f'bin/{self.easyName}_{value}_unsigned.apk')
cmdPack = f'cd {self.MakeDir} && java -jar apktool.jar b temp -o {unsignApk}'
os.system(cmdPack)
signedjar = os.path.join(self.MakeDir, f'bin/{self.easyName}_{value}.apk')
unsignedjar = os.path.join(self.MakeDir, f'bin/{self.easyName}_{value}_unsigned.apk')
cmd_sign = f'cd {self.MakeDir} && jarsigner -verbose -digestalg SHA1 -sigalg MD5withRSA -keystore {self.keystore} -storepass {self.storepass} -keypass {self.keypass} -signedjar {signedjar} {unsignedjar} {self.alianame}'
os.system(cmd_sign)
os.remove(unsignedjar)
def _unpack(self, pack_name):
"""
解包
:param pack_name:
:return:
"""
cmd = f'cd {self.MakeDir} && java -jar apktool.jar d -f -s {self.apkName} -o temp'
os.system(cmd)
androidmanifest_path = os.path.join(self.MakeDir, 'AndroidManifest.xml')
if os.path.exists(androidmanifest_path):
os.remove(androidmanifest_path)
new_androidmanifest_path = os.path.join(self.MakeDir, 'temp/AndroidManifest.xml')
shutil.copyfile(new_androidmanifest_path, androidmanifest_path)
def _clear(self):
path = os.path.join(self.MakeDir, 'temp')
if os.path.exists(path):
shutil.rmtree(path)
path = os.path.join(self.MakeDir, 'AndroidManifest.xml')
if os.path.exists(path):
os.remove(path)
def _make(self):
"""
重新编译包
:return:
"""
for i in range(self.start, self.packNum):
self.modify_channel(i)
def run(self):
try:
self._init_props()
self._init_path()
is_ok, msg = self.check_props()
if not is_ok:
return False, msg
self._unpack(self.apkName)
self._make()
self._clear()
except Exception as e:
return False, str(e)
else:
return True, 'ok'