def _on_stale_md5(args, proguard_cmd, main_dex_list_cmd, paths, main_dex_list_path): base_dir = args.base_dir if os.path.exists(base_dir): shutil.rmtree(base_dir) pass build_utils.make_directory(base_dir) paths_arg = build_utils.CLASSPATH_SEP.join(paths) main_dex_list = '' try: temp_jar_path = os.path.join(base_dir, "proguard.jar") proguard_cmd += ['-injars', paths_arg, '-outjars', temp_jar_path] build_utils.check_output(proguard_cmd, print_stderr=False) main_dex_list_cmd += [temp_jar_path, paths_arg] main_dex_list = build_utils.check_output(main_dex_list_cmd) except build_utils.CalledProcessError as e: if 'output jar is empty' in e.output: pass elif "input doesn't contain any classes" in e.output: pass else: raise with open(main_dex_list_path, 'w') as main_dex_list_file: main_dex_list_file.write(main_dex_list) pass
def _finalize_apk(args): base_dir = args.base_dir build_utils.make_directory(base_dir) build_utils.remove_subtree(base_dir) input_apk_basename = os.path.basename(args.unsigned_apk_path) (input_apk_name, input_apk_extension) = os.path.splitext(input_apk_basename) apk_to_sign_tmp = os.path.join(base_dir, input_apk_basename) if args.load_library_from_zip: apk_to_sign = apk_to_sign_tmp rename_inflate_and_add_page_alignment(args.rezip_apk_jar_path, args.unsigned_apk_path, apk_to_sign) else: apk_to_sign = args.unsigned_apk_path signed_apk_path = os.path.join( base_dir, "%s-signed%s" % (input_apk_name, input_apk_extension)) jar_signer(args.key_path, args.key_name, args.key_passwd, apk_to_sign, signed_apk_path) if args.load_library_from_zip: record_and_align_apk(args.rezip_apk_jar_path, signed_apk_path, args.finalize_apk) else: align_apk(args.zipalign_path, args.page_align_shared_libraries, signed_apk_path, args.final_apk_path) pass
def unzip_aar(aar_path, output_dir, output_path): build_utils.make_directory(output_dir) aar_map = {} with zipfile.ZipFile(aar_path, mode="r") as in_zip: for name in in_zip.namelist(): if name.endswith("/"): continue if build_utils.matches_glob(name, AAR_EXTRACT_GLOB): aar_map[name] = os.path.normpath(os.path.join(output_dir, name)) output_file_set = set(aar_map.values()) for path in (os.path.normpath(x) for x in build_utils.find_in_directory(output_dir, "*")): if path not in output_file_set: os.remove(path) old_metadata = None if os.path.isfile(output_path): try: with open(output_path) as file_obj: old_metadata = md5_metadata.Metadata.from_file(file_obj) except Exception: os.remove(output_path) new_metadata = md5_metadata.Metadata() new_metadata.add_input_file(aar_path) zip_map = create_extract_list(old_metadata, new_metadata, aar_map) on_stale_md5(aar_path, output_dir, zip_map) new_metadata.add_output_file_in_directory(output_dir, "*") build_utils.make_directory(os.path.dirname(output_path)) with open(output_path, mode="w") as file_obj: new_metadata.to_file(file_obj) pass
def download_file(self, maven_url, client_path, force=True, text=False): build_utils.make_directory(os.path.dirname(client_path)) print("%sDOWNLOAD file: %s" % (self.log_prefix, maven_url)) try: request = self.create_maven_request(maven_url) with self._opener.open(request) as response: if response.code != 200: if force: raise Exception("can't download maven_url=%s" % maven_url) else: return False with open(client_path, mode="wb") as fp: while True: data = response.read(io.DEFAULT_BUFFER_SIZE) if not data: break fp.write(data) pass except HTTPError as e: if force: raise RuntimeError(e, "%s" % maven_url) return False if text: with open(client_path, mode="r", encoding="utf-8") as fp: return fp.read(-1) return True
def main(argv): args = parse_args(argv) base_dir = args.base_dir build_utils.delete_directory(base_dir) build_utils.make_directory(base_dir) # generator_args = [ # "optional_field_style=reftypes", # "store_unknown_fields=true" # ] out_arg = "--java_out=" + base_dir cmds = [ args.protoc, "--proto_path", args.proto_path, out_arg ] + args.paths build_utils.check_output(cmds) if args.java_out_dir: build_utils.delete_directory(args.java_out_dir) shutil.copytree(base_dir, args.java_out_dir) else: build_utils.zip_dir(args.srcjar, base_dir) if args.depfile: all_dep_paths = build_utils.get_python_dependencies() build_utils.write_dep_file(args.depfile, all_dep_paths) pass
def on_stale_md5(): build_utils.remove_subtree(args.base_dir) build_utils.make_directory(args.base_dir) for path in inputs: build_utils.extract_all(path, base_dir=args.base_dir) zip_files = build_utils.find_in_directory(args.base_dir, "*") build_utils.do_zip(zip_files, output, args.base_dir) pass
def _run_dx(args, dex_cmd, paths): base_dir = args.base_dir build_utils.make_directory(base_dir) build_utils.remove_subtree(base_dir) if args.multi_dex and args.main_dex_list_path: dex_cmd += [ "--minimal-main-dex", "--main-dex-list=%s" % args.main_dex_list_path ] dex_cmd += paths build_utils.check_output(dex_cmd, print_stderr=False) if args.dex_path.endswith(".zip"): remove_unwanted_files_from_zip(args.dex_path) pass
def _on_stale_md5(args, javac_cmd, java_files): base_dir = args.base_dir build_utils.make_directory(base_dir) build_utils.remove_subtree(base_dir) excluded_jar_path = args.jar_path.replace(".jar", ".excluded.jar") java_sources_file = os.path.join(base_dir, "sources.txt") classes_dir = os.path.join(base_dir, "classes") os.makedirs(classes_dir) if args.java_srcjars: java_dir = os.path.join(base_dir, "java") os.makedirs(java_dir) for srcjar in args.java_srcjars: build_utils.extract_all(srcjar, base_dir=java_dir, pattern="*.java") jar_srcs = build_utils.find_in_directory(java_dir, "*.java") jar_srcs = _filter_java_files(jar_srcs, args.javac_includes) java_files.extend(jar_srcs) pass if java_files: build_utils.write_sources_file( java_sources_file, [os.path.normpath(x) for x in java_files]) cmd = javac_cmd + ["-d", classes_dir, "@" + java_sources_file] subprocess.check_call(cmd) pass glob = args.jar_excluded_classes def includes_jar_predicate(x): return not build_utils.matches_glob(x, glob) def excludes_jar_predicate(x): return build_utils.matches_glob(x, glob) jar.jar_directory(classes_dir, args.jar_path, predicate=includes_jar_predicate) jar.jar_directory(classes_dir, excluded_jar_path, predicate=excludes_jar_predicate) pass
def process_files(env, input_filenames, loader_base_dir, inputs_base_dir, outputs_zip, variables): with build_utils.temp_dir() as temp_dir: for input_filename in input_filenames: relpath = os.path.relpath(os.path.abspath(input_filename), os.path.abspath(inputs_base_dir)) if relpath.startswith(os.pardir): raise Exception( 'input file %s is not contained in inputs base dir %s' % (input_filename, inputs_base_dir)) output_filename = os.path.join(temp_dir, relpath) parent_dir = os.path.dirname(output_filename) build_utils.make_directory(parent_dir) process_file(env, input_filename, loader_base_dir, output_filename, variables) build_utils.zip_dir(outputs_zip, temp_dir) pass
def on_stale_md5(args): build_utils.remove_subtree(args.base_dir) build_utils.make_directory(args.base_dir) def predicate(name): if name.endswith(".class"): return False if name.startswith("META-INF/maven"): return False if name in ("META-INF/MANIFEST.MF", ): return False return True build_utils.extract_all(args.jar_path, args.base_dir, predicate=predicate) zip_files = build_utils.find_in_directory(args.base_dir, "*") build_utils.do_zip(zip_files, args.zipjar_path, args.base_dir) if args.depfile: dep_files = build_utils.get_python_dependencies() build_utils.write_dep_file(args.depfile, dep_files)
def main(argv): args = parse_args(argv) base_dir = args.base_dir build_utils.delete_directory(base_dir) build_utils.make_directory(base_dir) for f in args.paths: classname = os.path.splitext(os.path.basename(f))[0] output = os.path.join(base_dir, classname + ".java") aidl_cmd = [args.aidl_path] for s in args.imports: aidl_cmd += ["-p", s] if args.includes: for s in args.includes: aidl_cmd += ["-I", s] aidl_cmd += [f, output] build_utils.check_output(aidl_cmd) pass with zipfile.ZipFile(args.srcjar, "w") as srcjar: for f in build_utils.find_in_directory(base_dir, "*.java"): with open(f, "r") as fileobj: data = fileobj.read() pass pkg_name = re.compile(r"^\s*package\s+(.*?)\s*;", re.M).search(data).group(1) arcname = "%s/%s" % (pkg_name.replace(".", "/"), os.path.basename(f)) srcjar.writestr(arcname, data) pass if args.depfile: all_dep_paths = build_utils.get_python_dependencies() build_utils.write_dep_file(args.depfile, all_dep_paths) pass
def _on_stale_md5(package_command, args): base_dir = args.base_dir build_utils.make_directory(base_dir) build_utils.remove_subtree(base_dir) if args.resource_zips: for z in args.resource_zips: subdir = os.path.join(base_dir, os.path.basename(z)) if os.path.exists(subdir): raise Exception("Resource zip name conflict: " + os.path.basename(z)) build_utils.extract_all(z, base_dir=subdir) package_command += package_args_for_extracted_zips(subdir) pass build_utils.check_output(args=package_command) if args.create_density_splits or args.language_splits: check_for_missed_configs(args.apk_path, args.create_density_splits, args.language_splits) if args.create_density_splits: rename_density_splits(args.apk_path) pass
def move_images_to_non_mdpi_folders(res_root): """move images from drawable-*-mdpi-* folders to drawable-* folders. """ for src_dir_name in os.listdir(res_root): src_components = src_dir_name.split("-") if src_components[0] != "drawable" or "mdpi" not in src_components: continue src_dir = os.path.join(res_root, src_dir_name) if not os.path.isdir(src_dir): continue dst_components = [c for c in src_components if c != "mdpi"] assert (src_components != dst_components) dst_dir_name = "-".join(dst_components) dst_dir = os.path.join(res_root, dst_dir_name) build_utils.make_directory(dst_dir) for src_file_name in os.listdir(src_dir): if not src_file_name.endswith(".png"): continue src_file = os.path.join(src_dir, src_file_name) dst_file = os.path.join(dst_dir, src_file_name) assert (not os.path.lexists(dst_file)) shutil.move(src_file, dst_file) pass pass
def create_extra_r_java_files(r_dir, extra_packages, extra_r_text_files, include_all): """创建其他的R.java文件 """ if include_all: r_java_files = build_utils.find_in_directory(r_dir, "R.java") if len(r_java_files) != 1: raise Exception("Multiple R.java file are found: %s" % str(r_java_files)) r_java_file = r_java_files[0] r_java_content = codecs.open(r_java_file, mode="r", encoding="utf-8").read() for package in extra_packages: extra_r_java_file_dir = os.path.join(r_dir, *package.split(".")) build_utils.make_directory(extra_r_java_file_dir) extra_r_java_file = os.path.join(extra_r_java_file_dir, "R.java") extra_r_java_content = re.sub("package (.\\w)+;", "package %s;" % package, r_java_content) with open(extra_r_java_file, mode="w+", encoding="utf-8", newline="\n") as file_obj: file_obj.write(extra_r_java_content) pass pass else: if len(extra_packages) != len(extra_r_text_files): raise Exception() r_text_file = os.path.join(r_dir, "R.txt") if not os.path.exists(r_text_file): return all_resources = {} for entry in _parse_text_symbols_file(r_text_file): all_resources[(entry.resource_type, entry.name)] = entry resources_by_package = collections.defaultdict( lambda: collections.defaultdict(list)) for package, r_text_file in zip(extra_packages, extra_r_text_files): if not os.path.exists(r_text_file): continue if package in resources_by_package: raise Exception() resources_by_type = resources_by_package[package] for entry in _parse_text_symbols_file(r_text_file): resources_by_type[entry.resource_type].append(entry) pass for package in extra_packages: extra_r_java_dir = os.path.join(r_dir, *package.split(".")) build_utils.make_directory(extra_r_java_dir) extra_r_java_file = os.path.join(extra_r_java_dir, "R.java") r_java_content = _create_extra_r_java_file( package, resources_by_package[package], False) with open(extra_r_java_file, mode="w+", encoding="utf-8") as file_obj: file_obj.write(r_java_content) pass pass
def _on_stale_md5(args): aapt = args.aapt_path base_dir = args.base_dir build_utils.make_directory(base_dir) build_utils.remove_subtree(base_dir) deps_dir = os.path.join(base_dir, "deps") build_utils.make_directory(deps_dir) v14_dir = os.path.join(base_dir, "v14") build_utils.make_directory(v14_dir) gen_dir = os.path.join(base_dir, "gen") build_utils.make_directory(gen_dir) input_resource_dirs = args.resource_dirs dep_files = args.dependency_res_zips dep_subdirs = [] for z in dep_files: dep_subdir = os.path.join(deps_dir, os.path.basename(z)) if os.path.exists(dep_subdir): raise Exception("%s already exists" % dep_subdir) build_utils.make_directory(dep_subdir) dep_subdirs.append(dep_subdir) build_utils.extract_all(z, dep_subdir) pass package_command = [ aapt, "package", "-m", "-M", args.android_manifest, "--auto-add-overlay", "--no-version-vectors", "-I", args.android_sdk_jar, "--output-text-symbols", gen_dir, "-J", gen_dir, "--ignore-assets", build_utils.AAPT_IGNORE_PATTERN, ] for d in input_resource_dirs: package_command += ["-S", d] for d in dep_subdirs: package_command += ["-S", d] if args.non_constant_id: package_command.append("--non-constant-id") if args.custom_package: package_command += ["--custom-package", args.custom_package] if args.proguard_file: package_command += ["-G", args.proguard_file] if args.shared_resources: package_command += ["--shared-lib"] if args.app_as_shared_lib: package_command += ["--app-as-shared-lib"] build_utils.check_output(args=package_command) base_crunch_dir = os.path.join(base_dir, "crunch") build_utils.make_directory(base_crunch_dir) zip_resources_dirs = list(input_resource_dirs) for idx, input_resource_dir in enumerate(input_resource_dirs): crunch_dir = os.path.join(base_crunch_dir, str(idx)) build_utils.make_directory(crunch_dir) crunch_directory(aapt, input_resource_dir, crunch_dir) zip_resources_dirs.append(crunch_dir) pass create_resources_zip(zip_resources_dirs, args.resource_zip_out) if args.all_resources_zip_out: combine_zips([args.resource_zip_out] + dep_files, args.all_resources_zip_out) if args.extra_res_packages: create_extra_r_java_files(gen_dir, args.extra_res_packages, args.extra_r_text_files, False) pass if args.srcjar_out: build_utils.zip_dir(args.srcjar_out, gen_dir) pass if args.r_text_out: shutil.copyfile(os.path.join(gen_dir, "R.txt"), args.r_text_out) pass pass