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 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 jar_directory(classes_dir, jar_path, manifest_file=None, predicate=None): class_files = build_utils.find_in_directory(classes_dir, "*.class") if predicate: class_files = [f for f in class_files if predicate(f)] jar(class_files, classes_dir, jar_path, manifest_file=manifest_file, predicate=predicate) pass
def _expand_paths(paths): """解决assets的路径问题 """ ret = [] for path in paths: src_path, dest_path = _split_asset_path(path) if os.path.isdir(src_path): for f in build_utils.find_in_directory(src_path, "*"): ret.append((f, os.path.join(dest_path, f[len(src_path) + 1:]))) else: ret.append((src_path, dest_path)) ret.sort(key=lambda t: t[1]) return ret
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 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) # print(args) possible_output_paths = [ args.resource_zip_out, args.r_text_out, args.srcjar_out, args.all_resources_zip_out, ] output_paths = [x for x in possible_output_paths if x] input_strings = args.extra_res_packages + [ args.aapt_path, args.android_sdk_jar, args.custom_package, args.non_constant_id, args.v14_skip, ] input_paths = [args.android_manifest] input_paths.extend(args.dependency_res_zips) input_paths.extend( [path for path in args.extra_r_text_files if os.path.exists(path)]) resource_names = [] for resource_dir in args.resource_dirs: for resource_file in build_utils.find_in_directory(resource_dir, "*"): input_paths.append(resource_file) resource_names.append(os.path.relpath(resource_file, resource_dir)) pass input_strings.extend(sorted(resource_names)) build_utils.call_and_write_dep_file_if_stale(lambda: _on_stale_md5(args), args, input_paths=input_paths, input_strings=input_strings, output_paths=output_paths) pass
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 add_output_file_in_directory(self, directory, filename_filter): for path in build_utils.find_in_directory(directory, filename_filter): self.add_output_file(path) 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