def add_apk_list(srcdir, outdir, sque, dque):
    #获得项目名 --name
    doc_project = ET.parse(os.path.join(srcdir, ".project"))
    project_name = doc_project.find("name").text
    util.print_D(project_name)

    if (os.path.isfile(
            os.path.join(srcdir, "bin", project_name + "-release.apk"))):
        sque.append(os.path.join(srcdir, "bin", project_name + "-release.apk"))
        dque.append(os.path.join(outdir, project_name + "-release.apk"))
    elif (os.path.isfile(
            os.path.join(srcdir, "bin",
                         project_name + "-release-unsigned.apk"))):
        sque.append(
            os.path.join(srcdir, "bin",
                         project_name + "-release-unsigned.apk"))
        dque.append(
            os.path.join(outdir, project_name + "-release-unsigned.apk"))
    elif (os.path.isfile(
            os.path.join(srcdir, "bin", project_name + "-debug.apk"))):
        sque.append(os.path.join(srcdir, "bin", project_name + "-debug.apk"))
        dque.append(os.path.join(outdir, project_name + "-debug.apk"))
    else:
        util.print_E("ERROR: " + srcdir + " has not build !!!")
        sys.exit(-1)
def copy_(src, dst):
    if os.path.isdir(src):
        util.copytree(src, dst)
    elif os.path.isfile(src):
        util.print_D(src + " => " + dst)
        if not os.path.isdir(os.path.dirname(dst)):
            os.makedirs(os.path.dirname(dst))
        shutil.copyfile(src, dst)
    else:
        util.print_E("ERROR: " + src + " is not either a file or dir")
        sys.exit(-1)
def copy_(src, dst):
    if (os.path.isdir(src)):
        util.copytree(src, dst)
    elif (os.path.isfile(src)):
        util.print_D(src + " => " + dst)
        if (not os.path.isdir(os.path.dirname(dst))):
            os.makedirs(os.path.dirname(dst))
        shutil.copyfile(src, dst)
    else:
        util.print_E("ERROR: " + src + " is not either a file or dir")
        sys.exit(-1)
Example #4
0
def run(project_dir):
    if not os.path.isdir(project_dir):
        util.print_E("error: 指定的目录 %s 不存在" % project_dir)
        return
    inifile = os.path.join(project_dir, "project.ini")
    if os.path.isfile(inifile):
        f = open(inifile, "r")
        project = f.read()
        f.close()
        workspace = os.path.join(project_dir, project.strip())
        compile_code(workspace)
    else:
        util.print_E("error: 找不到配置文件信息 %s" % inifile)
def run(project_dir):
    if not os.path.isdir(project_dir):
        util.print_E("error: 指定的目录 %s 不存在" % project_dir)
        return
    inifile = os.path.join(project_dir, "project.ini")
    if os.path.isfile(inifile):
        f = open(inifile, "r")
        project = f.read()
        f.close()
        workspace = os.path.join(project_dir, project.strip())
        compile_code(workspace)
    else:
        util.print_E("error: 找不到配置文件信息 %s" % inifile)
def add_lib_ref_list(srcdir, outdir, sque, dque):
    #读取android.library.reference.n 生成依赖库中的ant配置文件
    ref_library = []
    fproject_properties = open(os.path.join(srcdir, "project.properties"), "r")
    for line in fproject_properties:
        if line.startswith("android.library.reference."):
            ref_library.append(line.split('=')[1].strip().replace("\\", "/"))
    fproject_properties.close()
    util.print_D(ref_library)

    #获得项目名 --name
    doc_project = ET.parse(os.path.join(srcdir, ".project"))
    project_name = doc_project.find("name").text
    util.print_D(project_name)

    for file in os.listdir(os.path.join(srcdir, "libs")):
        if (os.path.splitext(file)[1] == ".jar"):
            sque.append(os.path.join(srcdir, "libs", os.path.basename(file)))
            dque.append(os.path.join(outdir, "libs", os.path.basename(file)))

    if os.path.isdir(os.path.join(srcdir, "jni")):
        if (not os.path.isdir(os.path.join(srcdir, "libs", "armeabi"))):
            util.print_E("ERROR: " + srcdir + "native code is not build!!!")
            sys.exit(-1)
        for d in os.listdir(os.path.join(srcdir, "libs")):
            if ("mips armeabi-v7a armeabi x86".find(os.path.basename(d)) > -1):
                sque.append(os.path.join(srcdir, "libs", os.path.basename(d)))
                dque.append(os.path.join(outdir, "libs", os.path.basename(d)))

    if (os.path.isfile(os.path.join(srcdir, "bin", "classes.jar"))):
        sque.append(os.path.join(srcdir, "bin", "classes.jar"))
        dque.append(os.path.join(outdir, "libs", project_name + ".jar"))
    else:
        util.print_E("ERROR: " + srcdir + " has not build !!!")
        sys.exit(-1)

    if (os.path.isfile(
            os.path.join(srcdir, "bin", "proguard", "obfuscated.jar"))):
        sque.append(os.path.join(srcdir, "bin", "proguard", "obfuscated.jar"))
        dque.append(
            os.path.join(outdir, "libs",
                         project_name + "_proguard_obfuscated.jar"))

    #查询依赖库
    for lib_dir in ref_library:
        add_lib_ref_list(os.path.join(srcdir, lib_dir), outdir, sque, dque)
def add_apk_list(srcdir, outdir, sque, dque):
    # 获得项目名 --name
    doc_project = ET.parse(os.path.join(srcdir, ".project"))
    project_name = doc_project.find("name").text
    util.print_D(project_name)

    if os.path.isfile(os.path.join(srcdir, "bin", project_name + "-release.apk")):
        sque.append(os.path.join(srcdir, "bin", project_name + "-release.apk"))
        dque.append(os.path.join(outdir, project_name + "-release.apk"))
    elif os.path.isfile(os.path.join(srcdir, "bin", project_name + "-release-unsigned.apk")):
        sque.append(os.path.join(srcdir, "bin", project_name + "-release-unsigned.apk"))
        dque.append(os.path.join(outdir, project_name + "-release-unsigned.apk"))
    elif os.path.isfile(os.path.join(srcdir, "bin", project_name + "-debug.apk")):
        sque.append(os.path.join(srcdir, "bin", project_name + "-debug.apk"))
        dque.append(os.path.join(outdir, project_name + "-debug.apk"))
    else:
        util.print_E("ERROR: " + srcdir + " has not build !!!")
        sys.exit(-1)
def add_lib_ref_list(srcdir, outdir, sque, dque):
    # 读取android.library.reference.n 生成依赖库中的ant配置文件
    ref_library = []
    fproject_properties = open(os.path.join(srcdir, "project.properties"), "r")
    for line in fproject_properties:
        if line.startswith("android.library.reference."):
            ref_library.append(line.split("=")[1].strip().replace("\\", "/"))
    fproject_properties.close()
    util.print_D(ref_library)

    # 获得项目名 --name
    doc_project = ET.parse(os.path.join(srcdir, ".project"))
    project_name = doc_project.find("name").text
    util.print_D(project_name)

    for file in os.listdir(os.path.join(srcdir, "libs")):
        if os.path.splitext(file)[1] == ".jar":
            sque.append(os.path.join(srcdir, "libs", os.path.basename(file)))
            dque.append(os.path.join(outdir, "libs", os.path.basename(file)))

    if os.path.isdir(os.path.join(srcdir, "jni")):
        if not os.path.isdir(os.path.join(srcdir, "libs", "armeabi")):
            util.print_E("ERROR: " + srcdir + "native code is not build!!!")
            sys.exit(-1)
        for d in os.listdir(os.path.join(srcdir, "libs")):
            if "mips armeabi-v7a armeabi x86".find(os.path.basename(d)) > -1:
                sque.append(os.path.join(srcdir, "libs", os.path.basename(d)))
                dque.append(os.path.join(outdir, "libs", os.path.basename(d)))

    if os.path.isfile(os.path.join(srcdir, "bin", "classes.jar")):
        sque.append(os.path.join(srcdir, "bin", "classes.jar"))
        dque.append(os.path.join(outdir, "libs", project_name + ".jar"))
    else:
        util.print_E("ERROR: " + srcdir + " has not build !!!")
        sys.exit(-1)

    if os.path.isfile(os.path.join(srcdir, "bin", "proguard", "obfuscated.jar")):
        sque.append(os.path.join(srcdir, "bin", "proguard", "obfuscated.jar"))
        dque.append(os.path.join(outdir, "libs", project_name + "_proguard_obfuscated.jar"))

    # 查询依赖库
    for lib_dir in ref_library:
        add_lib_ref_list(os.path.join(srcdir, lib_dir), outdir, sque, dque)
Example #9
0
def compile_code(workspace, debug=False):
    if os.path.isdir(workspace):
        pre_dir = os.getcwd()
        os.chdir(workspace)
        print os.getcwd()
        print "编译准备。"
        if not os.path.isdir("../log"):
            os.mkdir("../log")
        logFile = open(os.path.join("../log", str(time.time()) + ".log"), "w")
        print "删除上次的编译结果。"
        (status, output) = commands.getstatusoutput("ant clean")
        logFile.writelines(output)
        logFile.writelines("\n\n========== ant clean finish! ==========\n\n")
        if status == 0:
            print "删除成功。开始编译。Log信息查看文件:%s" % os.path.abspath(logFile.name)
        else:
            print output
            util.print_E("删除失败,退出编译。Log信息查看文件:%s" %
                         os.path.abspath(logFile.name))
            logFile.close()
            sys.exit(-1)
        if debug:
            util.print_V("ant debug")
            (status, output) = commands.getstatusoutput("ant debug")
        else:
            util.print_V("ant release")
            (status, output) = commands.getstatusoutput("ant release")
        logFile.writelines(output)
        logFile.writelines("\n\n========== ant release finish! ==========\n")
        if status == 0:
            print "编译成功。Log信息查看文件:%s" % os.path.abspath(logFile.name)
        else:
            print output
            util.print_E("编译失败。Log信息查看文件:%s" % os.path.abspath(logFile.name))
            sys.exit(-1)
        logFile.close()
        os.chdir(pre_dir)
    else:
        util.print_E("error: 配置信息错误,目录 %s 不存在" % workspace)
        sys.exit(-1)
def compile_code(workspace, debug=False):
    if os.path.isdir(workspace):
        pre_dir = os.getcwd()
        os.chdir(workspace)
        print os.getcwd()
        print "编译准备。"
        if not os.path.isdir("../log"):
            os.mkdir("../log")
        logFile = open(os.path.join("../log", str(time.time()) + ".log"), "w")
        print "删除上次的编译结果。"
        (status, output) = commands.getstatusoutput("ant clean")
        logFile.writelines(output)
        logFile.writelines("\n\n========== ant clean finish! ==========\n\n")
        if status == 0:
            print "删除成功。开始编译。Log信息查看文件:%s" % os.path.abspath(logFile.name)
        else:
            print output
            util.print_E("删除失败,退出编译。Log信息查看文件:%s" % os.path.abspath(logFile.name))
            logFile.close()
            sys.exit(-1)
        if debug:
            util.print_V("ant debug")
            (status, output) = commands.getstatusoutput("ant debug")
        else:
            util.print_V("ant release")
            (status, output) = commands.getstatusoutput("ant release")
        logFile.writelines(output)
        logFile.writelines("\n\n========== ant release finish! ==========\n")
        if status == 0:
            print "编译成功。Log信息查看文件:%s" % os.path.abspath(logFile.name)
        else:
            print output
            util.print_E("编译失败。Log信息查看文件:%s" % os.path.abspath(logFile.name))
            sys.exit(-1)
        logFile.close()
        os.chdir(pre_dir)
    else:
        util.print_E("error: 配置信息错误,目录 %s 不存在" % workspace)
        sys.exit(-1)
Example #11
0
        指定编译debug版本,默认编译release版本""")


if __name__ == "__main__":

    reload(sys)
    sys.setdefaultencoding('utf-8')

    build_debug = False
    project_dir = ""

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hp:d",
                                   ["help", "path=", "debug"])
    except getopt.GetoptError:
        util.print_E("参数错误\n")
        _help()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            _help()
            sys.exit(0)
        elif opt in ("-d", "--debug"):
            build_debug = True
        elif opt in ("-p", "--path"):
            project_dir = arg

    if len(args) > 1:
        util.print_E("参数错误\n")
        _help()
Example #12
0
def config(workspace):

    extern_jar = []
    islib = ""
    ref_library = []

    print "\n处理workspace = %s " % workspace

    if not os.path.isdir(workspace):
        util.print_E("ERROR: 文件夹不存在")
        return

    #.classpath  project.properties local.properties
    #sed -i 's@\\@/@g' project.properties
    for f in [".classpath", "project.properties", "local.properties"]:
        if os.path.isfile(os.path.join(workspace, f)):
          sed_cmd = "sed -i 's@\\\\@/@g' %s" % (os.path.join(workspace, f))
          util.print_D(sed_cmd)
          os.system(sed_cmd)

    #获得项目名 --name
    doc_project = ET.parse(os.path.join(workspace, ".project"))
    project_name = doc_project.find("name").text
    util.print_D(project_name)

    #获得external jar   
    doc_classpath = ET.parse(os.path.join(workspace, ".classpath"))
    for node in doc_classpath.findall("classpathentry"):
        if node.get("kind", default=None) == "lib":
            extern_jar.append(node.get("path", default=None))
    util.print_D(extern_jar)

    #获得android.library
    fproject_properties = open(os.path.join(workspace, "project.properties"), "r+")
    for line in fproject_properties:
        util.print_D(line)
        if line.startswith("android.library="):
            islib = line.split('=')[1].strip()

    if ostype == "cygwin" or ostype == "win32":
        android_bat = "android.bat"
    else:
        android_bat = "android"

    #运行android.bat 脚本生成ant配置文件
    if not islib.lower() == "true":
        command = "%s update project --path %s --name %s --target android-19" \
            % (android_bat, workspace, project_name)
    else :
        command = "%s update lib-project --path %s --target android-19" \
            % (android_bat, workspace)
    if os.path.isfile(os.path.join(workspace, "build.xml")):
        util.print_W("WARNNING: 该项目已经存在build.xml文件,如需重新生成,请删除该项目中的build.xml文件")
        status,output = 0, ""
    else:
        util.print_D(command)
        (status, output) = commands.getstatusoutput(command)
        print output
    if (not status == 0) or (output.find("Error:") > -1):
         util.print_E("ERROR: ant配置文件更新失败")
    else:
        #读取android.library.reference.n 生成依赖库中的ant配置文件
        fproject_properties.seek(0)
        for line in fproject_properties:
            if line.startswith("android.library.reference."):
                ref_library.append(line.split('=')[1].strip().replace("\\", "/"))
        util.print_D(ref_library)

        #将external jar 写入project.properties文件
        if len(extern_jar) > 0:
            fproject_properties.seek(0)
            lines = fproject_properties.readlines()
            new_lines = []
            skip = False
            for line in lines:
                if line.startswith("java.compiler.classpath="):
                    new_lines.append("java.compiler.classpath=" + ";".join(extern_jar) + "\n")
                    skip = True
                else:
                    new_lines.append(line)
            if not skip:
                new_lines.append("\njava.compiler.classpath=" + ";".join(extern_jar))
                new_lines.append("\n");
            util.print_D(new_lines)
            fproject_properties.truncate(0)
            fproject_properties.seek(0)
            fproject_properties.write("".join(new_lines))
            fproject_properties.close()

        #如果有jni目录,添加ndk编译
        if os.path.isdir(os.path.join(workspace, "jni")):
            util.print_W("添加NDK编译")
            util.print_D(ndk_build_xml_str)
            fcustom_rule = open(os.path.join(workspace, "custom_rules.xml"), "w") 
            fcustom_rule.write(ndk_build_xml_str)
            fcustom_rule.close()

        #添加签名信息
        if not islib.lower() == "true":
            current = os.path.abspath(workspace)
            while True:
                if os.path.isdir(os.path.join(current, "common")):
                    break
                else:
                    current = os.path.join(current, "..")
            signature_properties_string = (signature_properties_str % current.split(os.path.basename(os.path.abspath(workspace))+"/")[1])
            fant_proper = open(os.path.join(workspace, "ant.properties"), "w")
            fant_proper.write(signature_properties_string)
            fant_proper.close()

        #生成依赖库的ant脚本
        for lib_dir in ref_library:
            config(os.path.join(workspace, lib_dir))

        #如果lib需要混淆,则把lib的proguard-project.txt include到本工程中proguard-project.txt
        fproguard = open(os.path.join(workspace, "proguard-project.txt"), "a")
        for lib_dir in ref_library:
            f = open(os.path.join(workspace, lib_dir, "project.properties"), "r")
            for line in f.readlines():
                if line.startswith("proguard.config="):
                    fproguard.write("\n-include " + os.path.join(lib_dir, "proguard-project.txt") + "\n") 
                    current = os.path.abspath(workspace)
                    while True:
                        if os.path.isdir(os.path.join(current, "common")):
                            break
                        else:
                            current = os.path.join(current, "..")
                    fproguard.write("\n-include " + os.path.join(current.split(os.path.basename(os.path.abspath(workspace))+"/")[1], \
                                "common", "LK_AntiRobot", "base-proguard-project.txt") + "\n") 
                    os.system("sed -i 's/^#proguard.config=/proguard.config=/' " + os.path.join(workspace, "project.properties"))
                    util.print_W("\n" + workspace + " 混淆打开")
            f.close()
        fproguard.close()
    同ant_configure.py path 将当前目录作为path传入 当前目录含有config.xml文件""")

if __name__ == "__main__":
    
    reload(sys)
    sys.setdefaultencoding('utf-8')

    argc = len(sys.argv)
    if argc > 2:
        if(sys.argv[1] == "-p"):
            project = sys.argv[2]
            config(os.path.abspath(project))
            print "\n配置完成"
            sys.exit(0)
        else:
            util.print_E("输入的参赛不对")
            _help()
            sys.exit(1)
    else:
        if (argc > 1):
            if(sys.argv[1] == "-h"):
                _help()
                sys.exit(0)
            p_dir = sys.argv[1]
        else:
            p_dir = "."
    xmlfile = os.path.join(p_dir, "config.xml")

    if(os.path.isfile(xmlfile)):
        pathList = []
        util.go_through_xml(xmlfile, pathList)
if __name__ == "__main__":

    srcqueue = deque([])
    dstqueue = deque([])
    srcdir = os.getcwd()
    dstdir = os.path.join(os.getcwd(), "..", "release")

    if os.path.isdir(dstdir):
        print dstdir
        util.print_W("release文件夹已存在,清除文件夹内容重新拷贝?")
        ans = raw_input("y删除原来文件夹,n直接覆盖原文件夹(y/n):")
        if ans.lower() == "y":
            shutil.rmtree(dstdir)
            os.makedirs(dstdir)
    elif os.path.isfile(dstdir):
        print dstdir
        ans = raw_input("release是一个文件,删除该文件?(y/n)")
        if ans.lower() == "y":
            os.remove(dstdir)
            os.makedirs(dstdir)
        else:
            util.print_E("拷贝失败!请确认release文件内容,或者备份之后再执行该脚本。")
    else:
        os.makedirs(dstdir)

    add_lib_ref_list(srcdir, dstdir, srcqueue, dstqueue)
    add_res_list(srcdir, dstdir, srcqueue, dstqueue)
    copy_result_list(srcqueue, dstqueue)
    del_svn(dstdir)
    print "拷贝完成"
if __name__ == "__main__":

    srcqueue = deque([])
    dstqueue = deque([])
    srcdir = os.getcwd()
    dstdir = os.path.join(os.getcwd(), "..", "release")

    if os.path.isdir(dstdir):
        print dstdir
        util.print_W("release文件夹已存在,清除文件夹内容重新拷贝?")
        ans = raw_input("y删除原来文件夹,n直接覆盖原文件夹(y/n):")
        if ans.lower() == "y":
            shutil.rmtree(dstdir)
            os.makedirs(dstdir)
    elif os.path.isfile(dstdir):
        print dstdir
        ans = raw_input("release是一个文件,删除该文件?(y/n)")
        if ans.lower() == "y":
            os.remove(dstdir)
            os.makedirs(dstdir)
        else:
            util.print_E("拷贝失败!请确认release文件内容,或者备份之后再执行该脚本。")
    else:
        os.makedirs(dstdir)

    add_lib_ref_list(srcdir, dstdir, srcqueue, dstqueue)
    add_res_list(srcdir, dstdir, srcqueue, dstqueue)
    copy_result_list(srcqueue, dstqueue)
    del_svn(dstdir)
    print "拷贝完成"