def update_channels(config_path, channel, index): try: tree = ET.parse(config_path) root = tree.getroot() channels = root.findall('channel') channel_node = channels[index] params = channel_node.findall('param') for param_node in params: key = param_node.get('name') param_node.set('value', channel[key]) channel_node.remove(channel_node.find('sdk-params')) sdk_params_node = ET.SubElement(channel_node, 'sdk-params') for p in channel['sdkParams']: p_node = ET.SubElement(sdk_params_node, 'param') p_node.set('name', p['name']) p_node.set('value', p['value']) indent(root) tree = ET.ElementTree(root) tree.write(config_path, xml_declaration=True, encoding='utf-8', method='xml') return True except Exception as e: LogUtils.error('=> can not parse config.xml path: %s', config_path) LogUtils.error('parse xml exception!\n%s', e.__str__()) return False
def get_channels(config_path): try: tree = ET.parse(config_path) root = tree.getroot() channels = root.findall('channel') if channels is None or len(channels) <= 0: return None channel_list = [] for cNode in channels: channel = {} params = cNode.findall('param') for cParam in params: key = cParam.get('name') val = cParam.get('value') channel[key] = val sdk_params = {} sdk_params_node = cNode.find('sdk-params') if sdk_params_node is not None: sdk_param_nodes = sdk_params_node.findall('param') if sdk_param_nodes is not None and len(sdk_param_nodes) > 0: for cParam in sdk_param_nodes: key = cParam.get('name') val = cParam.get('value') sdk_params[key] = val ret = set_channel_params(channel, sdk_params) if ret: channel_list.append(channel) return channel_list except Exception as e: LogUtils.error('=> can not parse config.xml: %s', config_path) LogUtils.error('parse xml exception!\n%s', e.__str__()) return None
def merge_manifest(decompile_dir, sdk_dir): target_manifest = os.path.join(decompile_dir, 'AndroidManifest.xml') sdk_manifest = os.path.join(sdk_dir, 'SDKManifest.xml') if not os.path.exists(target_manifest) or not os.path.exists(sdk_manifest): LogUtils.error( 'the manifest file is not exists.\n targetManifest: %s\n sdkManifest: %s', target_manifest, sdk_manifest) return 1 ET.register_namespace('android', androidNS) target_tree = ET.parse(target_manifest) target_root = target_tree.getroot() # 去除manifest标签中compileSdkVersion属性 attrs = target_root.attrib target_root.attrib = {} for s in attrs: if s.find('compileSdkVersion') < 0: target_root.set(s, attrs[s]) ET.register_namespace('android', androidNS) sdk_tree = ET.parse(sdk_manifest) sdk_root = sdk_tree.getroot() f = open(target_manifest, 'r', encoding='utf-8') target_content = f.read() f.close() permission_config_node = sdk_root.find('permissionConfig') if permission_config_node is not None and len(permission_config_node) > 0: for child in list(permission_config_node): key = '{' + androidNS + '}name' val = child.get(key) if val is not None and len(val) > 0: if -1 == target_content.find(val): target_root.append(child) app_config_node = sdk_root.find('applicationConfig') app_node = target_root.find('application') if app_config_node is not None: proxy_application_name = app_config_node.get('proxyApplication') if proxy_application_name is not None and len( proxy_application_name) > 0: meta_node = SubElement(app_node, 'meta-data') key = '{' + androidNS + '}name' val = '{' + androidNS + '}value' meta_node.set(key, 'SS_APPLICATION_PROXY_NAME') meta_node.set(val, proxy_application_name) for child in list(app_config_node): target_root.find('application').append(child) target_tree.write(target_manifest, xml_declaration=True, encoding='utf-8', method='xml') LogUtils.info('merge manifest file success.') return 0
def del_channel(config_path, index): try: tree = ET.parse(config_path) root = tree.getroot() channels = root.findall('channel') root.remove(channels[index]) indent(root) tree = ET.ElementTree(root) tree.write(config_path, xml_declaration=True, encoding='utf-8', method='xml') except Exception as e: LogUtils.error('=> can not parse config.xml path: %s', config_path) LogUtils.error('parse xml exception!\n%s', e.__str__())
def dex2smali(dex_file, target_dir, path): """ Transfer the dex to smali. """ if not os.path.exists(dex_file): LogUtils.error('the dexfile is not exists. path:%s', dex_file) return 1 if not os.path.exists(target_dir): os.makedirs(target_dir) smali_tool = Utils.get_full_path('tools/baksmali.jar') java = Utils.get_full_path('tools/jdk/bin/java') cmd = '%s -jar %s -o %s %s' % (java, smali_tool, target_dir, dex_file) return Utils.exec_cmd(cmd)
def do_sdk_script(channel, decompile_dir, package_name, sdk_dir): script = os.path.join(sdk_dir, 'script.pyc') if not os.path.exists(script): return 0 try: sys.path.append(sdk_dir) import script script.execute(channel, decompile_dir, package_name) del sys.modules['script'] sys.path.remove(sdk_dir) return 0 except Exception as e: LogUtils.error('%s execute failed:%s', script, e.__str__()) return 1
def del_file(src): if os.path.exists(src): if os.path.isfile(src): try: src = src.replace('\\', '/') os.remove(src) except Exception as e: LogUtils.error('delete file exception!\n%s', e.__str__()) return 1 elif os.path.isdir(src): for f in os.listdir(src): item_src = os.path.join(src, f) del_file(item_src) os.rmdir(src) return 0
def get_local_config(): config_file = 'config.ini' if not os.path.exists(config_file): LogUtils.error('config.ini is not exists. ==> ' + config_file) return None cf = open(config_file, 'r', encoding='utf-8-sig') lines = cf.readlines() cf.close() config = {} for line in lines: if line.startswith('#') or len(line.strip()) <= 0: continue line = line.strip() dup = line.split('=') config[dup[0]] = dup[1] return config
def update_games(config_path, game, index): try: tree = ET.parse(config_path) root = tree.getroot() games = root.findall('game') game_node = games[index] params = game_node.findall('param') for param_node in params: key = param_node.get('name') param_node.set('value', game[key]) indent(root) tree = ET.ElementTree(root) tree.write(config_path, xml_declaration=True, encoding='utf-8', method='xml') return True except Exception as e: LogUtils.error('==> can not parse games.xml: %s', config_path) LogUtils.error('parse xml exception!\n%s', e.__str__()) return False
def classes_split(decompile_dir, sdk_dest_dir): smali_path = decompile_dir + '/smali' filter_path = sdk_dest_dir + '/classes.filter' if not os.path.exists(filter_path): return 0 filter_file = open(filter_path, encoding='utf-8') content = filter_file.read() filter_file.close() smali2_path = decompile_dir + '/smali_classes2' if not os.path.exists(smali2_path): os.mkdir(smali2_path) try: filters = eval(content) for dir in filters: class_src_dir = smali_path + '/' + dir if os.path.exists(class_src_dir): class_dest_dir = smali2_path + '/' + dir shutil.move(class_src_dir, class_dest_dir) return 0 except Exception as e: LogUtils.error('%s parse failed: %s', filter_path, e.__str__()) return 1
def get_games(config_path): try: tree = ET.parse(config_path) root = tree.getroot() games = root.findall('game') if games is None or len(games) <= 0: return None game_list = [] for cNode in games: game = {} params = cNode.findall('param') for cParam in params: key = cParam.get('name') val = cParam.get('value') game[key] = val game_list.append(game) return game_list except Exception as e: LogUtils.error('==> can not parse games.xml: %s', config_path) LogUtils.error('parse xml exception!\n%s', e.__str__()) return None
def copy_file(src, dest): if not os.path.exists(src): LogUtils.error('the src is not exists.path:' + src) return 1 if os.path.isfile(src): try: dest_file_stream = open(dest, 'wb') source_file_stream = open(src, 'rb') dest_file_stream.write(source_file_stream.read()) dest_file_stream.close() source_file_stream.close() except Exception as e: LogUtils.error('copy file exception!\n%s', e.__str__()) return 1 else: if not os.path.exists(dest): os.mkdir(dest) for f in os.listdir(src): source_file = src + "/" + f target_file = dest + "/" + f copy_file(source_file, target_file) return 0
def get_app_icon_name(decompile_dir): try: ET.register_namespace('android', androidNS) tree = ET.parse(decompile_dir + '/AndroidManifest.xml') root = tree.getroot() application = root.find('application') key = '{' + androidNS + '}icon' icon_name = application.get(key) LogUtils.info('=>AndroidManifest key iconName: %s', icon_name) if icon_name is None: name = 'ic_launcher' else: name = icon_name.split('/')[-1] application.set(key, '@mipmap/' + name) tree.write(decompile_dir + '/AndroidManifest.xml', xml_declaration=True, encoding='utf-8', method='xml') return name except Exception as e: LogUtils.error("get_app_icon_name exception") LogUtils.error('parse xml exception!\n%s', e.__str__()) return None
def exec_cmd2(cmd): try: LogUtils.info('*********************cmd start***********************') cmd = cmd.replace('\\', '/') cmd = re.sub('/+', '/', cmd) st = subprocess.STARTUPINFO st.dwFlags = subprocess.STARTF_USESHOWWINDOW st.wShowWindow = subprocess.SW_HIDE LogUtils.info('cmd: %s', cmd) sub = subprocess.run(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) code, std, err = sub.returncode, sub.stdout, sub.stderr if code: LogUtils.error('===>exec Fail<===') LogUtils.error("\n" + err.decode('gbk')) return None else: return std.decode('utf-8') except Exception as e: LogUtils.error('===>exec Fail<===') LogUtils.error('Exception:' + e.__str__()) return None finally: LogUtils.info('*********************cmd end***********************')
def set_channel_params(channel, sdk_params): config_file = get_full_path('channelsdk/' + channel['sdk'] + '/config.xml') if not os.path.exists(config_file): LogUtils.error(' => the %s config.xml is not exists path: %s', channel['name'], config_file) return False try: tree = ET.parse(config_file) root = tree.getroot() channel['sdkParams'] = [] param_nodes = root.find('params') for param_node in param_nodes: param = {} param['name'] = param_node.get('name') key = param_node.get('name') if key in sdk_params and sdk_params[key] is not None: param['value'] = sdk_params[key] else: LogUtils.info(' => the sdk %s have a new parameter: %s', channel['name'], key) param['value'] = "" param['showName'] = param_node.get('showName') param['writeIn'] = param_node.get('writeIn') channel['sdkParams'].append(param) channel['plugins'] = [] plugin_nodes = root.find('plugins') for p_node in plugin_nodes: p = {} p['name'] = p_node.get('name') p['type'] = p_node.get('type') channel['plugins'].append(p) version_node = root.find('version') version_update_time = version_node.find('updateTime') channel['sdkUpdateTime'] = version_update_time.text version_name_node = version_node.find('versionName') channel['sdkVersionName'] = version_name_node.text return True except Exception as e: LogUtils.error(' => can not parse config.xml: %s', config_file) LogUtils.error('parse xml exception!\n%s', e.__str__()) return False
def write_plugin_config(channel, plugin_path): if 'plugins' not in channel: LogUtils.error("%s SDK no plugins", channel['sdk']) return 1 try: if os.path.exists(plugin_path): root = ET.parse(plugin_path).getroot() else: root = ET.Element('plugins') for plugin in channel['plugins']: param_node = ET.SubElement(root, 'plugin') param_node.set('name', plugin['name']) param_node.set('type', plugin['type']) indent(root) tree = ET.ElementTree(root) tree.write(plugin_path, xml_declaration=True, encoding='utf-8', method='xml') return 0 except Exception as e: LogUtils.error("write plugins.xml exception") LogUtils.error('parse xml exception!\n%s', e.__str__()) return 1