Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created December 1, 2015 14:19
Show Gist options
  • Save xxnjdlys/f112f665e1c4741d6092 to your computer and use it in GitHub Desktop.
Save xxnjdlys/f112f665e1c4741d6092 to your computer and use it in GitHub Desktop.
generate pids
#! /usr/bin/python
# coding=utf-8
import zipfile
import shutil
import os
import sys
# in fact it's an empty file ,just use it for getting pids
src_empty_file = 'info/sycn.txt'
# create an file if not exist
f = open(src_empty_file, 'w')
print("Create an empty file called", src_empty_file)
f.close()
# get current directory's apk files
# see those page below for help :
# http://wangwei007.blog.51cto.com/68019/1104940
# https://docs.python.org/2/library/os.path.html
src_apks = []
for file in os.listdir('.'):
if os.path.isfile(file):
extension = os.path.splitext(file)[1][1:]
if extension in 'apk':
src_apks.append(file)
if len(sys.argv) >= 2 and sys.argv[1].strip():
# get pids from cmd arg
lines = [str(sys.argv[1])]
else:
# get pids from confg file
channel_file = 'info/channel.txt'
f = open(channel_file)
lines = f.readlines()
f.close()
for src_apk in src_apks:
# file name (with extension)
src_apk_file_name = os.path.basename(src_apk)
# split file name and suffix
temp_list = os.path.splitext(src_apk_file_name)
# name without extension
src_apk_name = temp_list[0]
# suffix with "." e.g ".apk"
src_apk_extension = temp_list[1]
# create output directory by file name
output_dir = 'output_' + src_apk_name + '/'
# mkdir if not exist
if not os.path.exists(output_dir):
os.mkdir(output_dir)
# create an apk file for each pid in channel.txt
for line in lines:
# print("Pid == ", line)
# get pid remove "\n"
target_channel = line.strip()
print("Pid == ", target_channel)
# generate an new apk by pid
target_apk = output_dir + src_apk_name + "-" + target_channel + "-" + "release" + src_apk_extension
shutil.copy(src_apk, target_apk)
# see this url for help : https://docs.python.org/2/library/zipfile.html
zipped = zipfile.ZipFile(target_apk, 'a', zipfile.ZIP_DEFLATED)
# write pid to a file and close zip stream.
empty_channel_file = "META-INF/tmwya_{pid}".format(pid=target_channel)
zipped.write(src_empty_file, empty_channel_file)
zipped.close()
print 'Congratulations ...'
# print 'Press Enter Close Window ...'
# raw_input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment