Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refine build scripts #569

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.replaced_files: list[str]
self.src: str
self.dest: str


def merge_json_file_at_root_level(src: str, dest: str) -> None:
Expand Down Expand Up @@ -43,46 +44,31 @@ def merge_json_file_at_root_level(src: str, dest: str) -> None:
dest_file.truncate()


def split(input: str, split_key: str) -> tuple[str, str]:
split_key_index = str(input).find(split_key)
if split_key_index == -1:
# split_key is not found, this means the source and the destination
# are equal.
return (input, input)
else:
return (
str(input)[:split_key_index],
str(input)[split_key_index + len(split_key) :],
)

def replace_normal_files_or_merge_json_files(src: str, dest: str) -> None:
if not os.path.exists(src):
raise Exception(f"{src} does not exist.")

def replace_normal_files_or_merge_json_files(replaced_files: list[str]) -> None:
for res in replaced_files:
src, dest = split(res, "=>")
if os.path.isdir(src):
raise Exception(f"{src} is a directory, not a file.")

if not os.path.exists(src):
raise Exception(f"{src} does not exist.")
if os.path.exists(dest) and os.path.isdir(dest):
raise Exception(f"{dest} is a directory, not a file.")

if (
src.endswith(".json")
and dest.endswith(".json")
and os.path.exists(dest)
):
merge_json_file_at_root_level(src, dest)
else:
fs_utils.copy(src, dest)
if (
src.endswith(".json")
and dest.endswith(".json")
and os.path.exists(dest)
):
merge_json_file_at_root_level(src, dest)
else:
fs_utils.copy(src, dest)


if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument(
"--replaced-files",
type=str,
action="append",
default=[],
help="Resources to be replaced",
)
parser.add_argument("--src", type=str, required=True)
parser.add_argument("--dest", type=str, required=True)

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)
Expand All @@ -91,7 +77,7 @@ def replace_normal_files_or_merge_json_files(replaced_files: list[str]) -> None:
returncode = 0

try:
replace_normal_files_or_merge_json_files(args.replaced_files)
replace_normal_files_or_merge_json_files(args.src, args.dest)
except Exception as exc:
returncode = 1
print(exc)
Expand Down
55 changes: 13 additions & 42 deletions build/ten_runtime/feature/test.gni
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,8 @@ template("ten_package_test_prepare_app") {
replace_path_index = 0

foreach(replace_path, invoker.replace_paths_after_install_app) {
replace_path_info = {
}

replace_path_info =
exec_script("//.gnfiles/build/scripts/get_src_and_dest_file.py",
[
"--input-string",
replace_path,
"--src-dest-delimiter",
"=>",
"--src-base-delimiter",
":",
],
"json")

replace_src_path = replace_path_info.source
replace_dest_path =
"${test_case_out_dir_rel_path}/${replace_path_info.destination}"
replace_src_path = replace_path
replace_dest_path = "${test_case_out_dir_rel_path}/${replace_path}"

replace_src_abs_path = rebase_path(replace_src_path)
replace_dest_abs_path = rebase_path(replace_dest_path)
Expand All @@ -144,12 +128,12 @@ template("ten_package_test_prepare_app") {
script = "//build/ten_runtime/feature/replace_normal_files_or_merge_json_files.py"

args = [
"--replaced-files",
"${replace_src_abs_path}=>${replace_dest_abs_path}",
"--src",
"${replace_src_abs_path}",
"--dest",
"${replace_dest_abs_path}",
]

# Wait for the completion of 'install_app'.
inputs = []
sources = [ replace_src_path ]
outputs = [ replace_dest_path ]

Expand Down Expand Up @@ -236,34 +220,21 @@ template("ten_package_test_prepare_app") {
replace_path_index = 0

foreach(replace_path, invoker.replace_paths_after_install_all) {
replace_path_info = {
}

replace_path_info =
exec_script("//.gnfiles/build/scripts/get_src_and_dest_file.py",
[
"--input-string",
replace_path,
"--src-dest-delimiter",
"=>",
"--src-base-delimiter",
":",
],
"json")

replace_src_path = replace_path_info.source
replace_dest_path =
"${test_case_out_dir_rel_path}/${replace_path_info.destination}"
replace_src_path = replace_path
replace_dest_path = "${test_case_out_dir_rel_path}/${replace_path}"

replace_src_abs_path = rebase_path(replace_src_path)
replace_dest_abs_path = rebase_path(replace_dest_path)

action(
"${test_case_unique_target_name}_${_target_name}_after_install_all_replace_file_${replace_path_index}") {
script = "//build/ten_runtime/feature/replace_normal_files_or_merge_json_files.py"

args = [
"--replaced-files",
"${replace_src_abs_path}=>${replace_dest_abs_path}",
"--src",
"${replace_src_abs_path}",
"--dest",
"${replace_dest_abs_path}",
]

sources = [ replace_src_path ]
Expand Down
2 changes: 1 addition & 1 deletion packages/example_extensions/ffmpeg_demuxer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ten_package("ffmpeg_demuxer") {
resources = [
"manifest.json",
"property.json",
"res",
"res/test.mp4",
]

sources = [
Expand Down
24 changes: 10 additions & 14 deletions tests/ten_runtime/integration/common/build_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ def _replace_after_install_app(
):
return 0

replaced_files = []
for replace_file in replace_paths_after_install_app:
src_file = os.path.join(
assemble_info_dir,
Expand All @@ -370,13 +369,12 @@ def _replace_after_install_app(
return 1

dst_file = os.path.join(test_case_base_dir, replace_file)
replaced_files.append((src_file, dst_file))

try:
replace.replace_normal_files_or_merge_json_files(replaced_files)
except Exception as exc:
print(exc)
return 1
try:
replace.replace_normal_files_or_merge_json_files(src_file, dst_file)
except Exception as exc:
print(exc)
return 1

return 0

Expand All @@ -403,7 +401,6 @@ def _replace_after_install_all(
):
return 0

replaced_files = []
for replace_file in replace_paths_after_install_all:
src_file = os.path.join(
assemble_info_dir,
Expand All @@ -416,13 +413,12 @@ def _replace_after_install_all(
return 1

dst_file = os.path.join(test_case_base_dir, replace_file)
replaced_files.append((src_file, dst_file))

try:
replace.replace_normal_files_or_merge_json_files(replaced_files)
except Exception as exc:
print(exc)
return 1
try:
replace.replace_normal_files_or_merge_json_files(src_file, dst_file)
except Exception as exc:
print(exc)
return 1

return 0

Expand Down
69 changes: 20 additions & 49 deletions tests/ten_runtime/integration/common/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.replaced_files: list[str]
self.src: str
self.dest: str


def merge_json_file_at_root_level(src: str, dest: str) -> None:
Expand Down Expand Up @@ -43,59 +44,31 @@ def merge_json_file_at_root_level(src: str, dest: str) -> None:
dest_file.truncate()


def split(input: str, split_key: str) -> tuple[str, str]:
split_key_index = str(input).find(split_key)
if split_key_index == -1:
# split_key is not found, this means the source and the destination
# are equal.
return (input, input)
else:
return (
str(input)[:split_key_index],
str(input)[split_key_index + len(split_key) :],
)


def _parsing_replace_files(
replaced_files: list[str],
) -> list[tuple[str, str]]:
result = []
for res in replaced_files:
res_src, res_dest = split(res, "=>")
result.append((res_src, res_dest))

return result

def replace_normal_files_or_merge_json_files(src: str, dest: str) -> None:
if not os.path.exists(src):
raise Exception(f"{src} does not exist.")

def replace_normal_files_or_merge_json_files(
replaced_files: list[tuple[str, str]]
) -> None:
for res in replaced_files:
res_src, res_dest = res
if os.path.isdir(src):
raise Exception(f"{src} is a directory, not a file.")

if not os.path.exists(res_src):
raise Exception(f"{res_src} does not exist.")
if os.path.exists(dest) and os.path.isdir(dest):
raise Exception(f"{dest} is a directory, not a file.")

if (
res_src.endswith(".json")
and res_dest.endswith(".json")
and os.path.exists(res_dest)
):
merge_json_file_at_root_level(res_src, res_dest)
else:
fs_utils.copy(res_src, res_dest)
if (
src.endswith(".json")
and dest.endswith(".json")
and os.path.exists(dest)
):
merge_json_file_at_root_level(src, dest)
else:
fs_utils.copy(src, dest)


if __name__ == "__main__":
parser = argparse.ArgumentParser()

parser.add_argument(
"--replaced-files",
type=str,
action="append",
default=[],
help="Resources to be replaced",
)
parser.add_argument("--src", type=str, required=True)
parser.add_argument("--dest", type=str, required=True)

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)
Expand All @@ -104,9 +77,7 @@ def replace_normal_files_or_merge_json_files(
returncode = 0

try:
replace_normal_files_or_merge_json_files(
_parsing_replace_files(args.replaced_files)
)
replace_normal_files_or_merge_json_files(args.src, args.dest)
except Exception as exc:
returncode = 1
print(exc)
Expand Down
1 change: 1 addition & 0 deletions tests/ten_runtime/integration/cpp/large_result/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ten_package_test_prepare_app("large_result_app") {
replace_paths_after_install_all = [
"large_result_app/ten_packages/extension/default_extension_cpp/src/main.cc",
]

if (ten_enable_ten_manager) {
deps = [
"//core/src/ten_manager",
Expand Down
18 changes: 16 additions & 2 deletions tests/ten_runtime/integration/cpp/multi_apps/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ ten_package_test_prepare_app("app_1") {
replace_paths_after_install_app = [
"app_1/manifest.json",
"app_1/property.json",
"app_1/ten_packages",
"app_1/ten_packages/extension/ext_a/src/main.cc",
"app_1/ten_packages/extension/ext_a/BUILD.gn",
"app_1/ten_packages/extension/ext_a/manifest.json",
"app_1/ten_packages/extension/ext_a/property.json",
"app_1/ten_packages/extension/ext_b/src/main.cc",
"app_1/ten_packages/extension/ext_b/BUILD.gn",
"app_1/ten_packages/extension/ext_b/manifest.json",
"app_1/ten_packages/extension/ext_b/property.json",
]

if (ten_enable_ten_manager) {
Expand All @@ -37,7 +44,14 @@ ten_package_test_prepare_app("app_2") {
replace_paths_after_install_app = [
"app_2/manifest.json",
"app_2/property.json",
"app_2/ten_packages",
"app_2/ten_packages/extension/ext_c/src/main.cc",
"app_2/ten_packages/extension/ext_c/BUILD.gn",
"app_2/ten_packages/extension/ext_c/manifest.json",
"app_2/ten_packages/extension/ext_c/property.json",
"app_2/ten_packages/extension/ext_d/src/main.cc",
"app_2/ten_packages/extension/ext_d/BUILD.gn",
"app_2/ten_packages/extension/ext_d/manifest.json",
"app_2/ten_packages/extension/ext_d/property.json",
]

if (ten_enable_ten_manager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ ten_package_test_prepare_app("access_property_go_app") {
replace_paths_after_install_app = [
"access_property_go_app/manifest.json",
"access_property_go_app/property.json",
"access_property_go_app/ten_packages",
"access_property_go_app/ten_packages/extension/extension_a/extension.go",
"access_property_go_app/ten_packages/extension/extension_a/go.mod",
"access_property_go_app/ten_packages/extension/extension_a/manifest.json",
"access_property_go_app/ten_packages/extension/extension_a/property.json",
]

deps = [
Expand Down
Loading
Loading