Skip to content

Commit

Permalink
chore: refine build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Jan 19, 2025
1 parent f56d4d7 commit 330f0d6
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 65 deletions.
30 changes: 13 additions & 17 deletions .gnfiles/build/feature/ten_package.gni
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ template("ten_package") {
# sources to specified destinations. All the destination folders of
# 'resources' are based on <package_output_root_dir>.
if (defined(invoker.resources) && invoker.resources != []) {
resource_index = 0

foreach(resource, invoker.resources) {
resource_info = {
}
Expand All @@ -250,37 +252,29 @@ template("ten_package") {
[
"--input-string",
"${resource}",
"--delimiter",
"--src-dest-delimiter",
"=>",
"--delimiter",
"|",
"--src-base-delimiter",
":",
],
"json")

resource_src_paths = []
resource_src_paths = resource_info.sources
resource_src_path = resource_info.source
resource_dest_path = resource_info.destination

resource_dest_str = string_replace(resource_dest_path, "/", "_")

unique_resource_action_target_name =
"${_target_name}_copy_resource_to_${resource_dest_str}"
"${_target_name}_resource_${resource_index}"

# The only task handled by resources is copying.
action("${unique_resource_action_target_name}") {
script = "//.gnfiles/build/scripts/copy_fs_entry.py"

inputs = resource_src_paths
inputs = [ resource_src_path ]
outputs = [ "${package_output_root_dir}/${resource_dest_path}" ]

args = []
foreach(resource_src_path, resource_src_paths) {
args += [
"--source",
rebase_path(resource_src_path),
]
}
args += [
args = [
"--source",
rebase_path(resource_src_path),
"--destination",
rebase_path("${package_output_root_dir}/${resource_dest_path}"),
]
Expand All @@ -294,6 +288,8 @@ template("ten_package") {
}

package_deps += [ ":${unique_resource_action_target_name}" ]

resource_index += 1
}
}

Expand Down
42 changes: 14 additions & 28 deletions .gnfiles/build/scripts/copy_fs_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
from typing import Optional
from build.scripts import fs_utils, timestamp_proxy

"""
Copy from the paths of the 1st to N-1 arguments to the path of the Nth argument.
"""


class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.source: list[str]
self.source: str
self.destination: str
self.tg_timestamp_proxy_file: Optional[str] = None

Expand All @@ -25,12 +21,8 @@ def main():
parser = argparse.ArgumentParser(
description="Copy source files to destination."
)
parser.add_argument(
"--source", action="append", required=True, help="Source file paths"
)
parser.add_argument(
"--destination", required=True, help="Destination file path"
)
parser.add_argument("--source", required=True, help="Source path")
parser.add_argument("--destination", required=True, help="Destination path")
parser.add_argument(
"--tg-timestamp-proxy-file",
required=False,
Expand All @@ -40,30 +32,24 @@ def main():
arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)

src_paths = args.source
dst = args.destination

try:
# Check if all the sources are existed.
for src in src_paths:
if not os.path.exists(src):
raise Exception(f"{src} does not exist")
# Check if the source is existed.
if not os.path.exists(args.source):
raise Exception(f"{args.source} does not exist")

try:
# Ensure the destination folder, if specified, does exist.
if os.path.dirname(dst) != "":
fs_utils.mkdir_p(os.path.dirname(dst))
if os.path.dirname(args.destination) != "":
fs_utils.mkdir_p(os.path.dirname(args.destination))
except Exception as e:
raise Exception(f"Failed to create destination directory: {str(e)}")

for src in src_paths:
if src.endswith(timestamp_proxy.TG_TIMESTAMP_PROXY_EXTENSION):
# This special timestamp file does not need to be copied.
continue
try:
fs_utils.copy(src, dst)
except Exception as e:
raise Exception(f"Failed to copy {src} to {dst}: {str(e)}")
try:
fs_utils.copy(args.source, args.destination)
except Exception as e:
raise Exception(
f"Failed to copy {args.source} to {args.destination}: {str(e)}"
)

# Touch the tg_timestamp_proxy_file if specified.
timestamp_proxy.touch_timestamp_proxy_file(args.tg_timestamp_proxy_file)
Expand Down
68 changes: 48 additions & 20 deletions .gnfiles/build/scripts/get_src_and_dest_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
#
import argparse
import json
import re
from typing import Optional


class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.input_string: str
self.delimiter: list[str]
# The delimiter that separates the source and the target.
self.src_dest_delimiter: Optional[str] = None
# The delimiter that separates the source_base.
self.src_base_delimiter: Optional[str] = None


def main():
Expand All @@ -23,35 +26,60 @@ def main():
"--input-string", type=str, required=True, help="String to be split."
)
parser.add_argument(
"--delimiter",
"--src-dest-delimiter",
type=str,
required=True,
action="append",
help="Delimiter to split the string.",
required=False,
help="Delimiter between source and destination.",
)
parser.add_argument(
"--src-base-delimiter",
type=str,
required=False,
help="Delimiter for source base.",
)

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)

# Combine all delimiters into a single regular expression pattern.
combined_delimiter = "|".join(map(re.escape, args.delimiter))
source_base: Optional[str] = None
source: str = ""
destination: Optional[str] = None

parts = re.split(combined_delimiter, args.input_string)
remaining_string = args.input_string

response = {}
# Handle source_base delimiter if specified.
if args.src_base_delimiter:
parts = remaining_string.split(args.src_base_delimiter, 1)
if len(parts) == 2:
source_base, remaining_string = parts
else:
source_base = None

# Check if the delimiter was found in the input string.
if len(parts) == 1 and parts[0] == args.input_string:
# Delimiter not found, treating the whole string as both source and
# destination.
response["sources"] = [args.input_string]
response["destination"] = args.input_string
# Handle src/dest delimiter if specified.
if args.src_dest_delimiter:
parts = remaining_string.split(args.src_dest_delimiter, 1)
if len(parts) == 2:
source_part, destination = parts
else:
source_part = remaining_string
destination = None
else:
# Assume the last part is the destination, and the rest are sources.
response["sources"] = parts[:-1]
response["destination"] = parts[-1]
source_part = remaining_string
destination = None

# Handle source delimiter if specified.
source = source_part

if destination is None:
destination = source

response = {
"source_base": source_base if source_base is not None else "",
"source": source,
"destination": destination,
}

print(json.dumps(response, ensure_ascii=False))
print(json.dumps(response, indent=2))


if __name__ == "__main__":
Expand Down
109 changes: 109 additions & 0 deletions .gnfiles/build/scripts/glob_src_to_dest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#
# Copyright © 2025 Agora
# This file is part of TEN Framework, an open source project.
# Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information.
#
import argparse
import os
import sys
import glob
import json
from typing import Optional


class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.src_base: Optional[str] = None
self.src_base_abs_path: Optional[str] = None
self.src: str
self.src_abs_path: str
self.dest_base: str
self.dest_base_abs_path: str


def glob_fs_entries(source: str) -> list[str]:
results: list[str] = []

for v in glob.glob(source, recursive=True):
results.append(v.replace("\\", "/"))

return results


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--src-base", type=str, required=False)
parser.add_argument("--src-base-abs-path", type=str, required=False)
parser.add_argument("--src", type=str, required=True)
parser.add_argument("--src-abs-path", type=str, required=True)
parser.add_argument("--dest-base", type=str, required=True)
parser.add_argument("--dest-base-abs-path", type=str, required=True)

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)

if (args.src_base is None) != (args.src_base_abs_path is None):
print(
"Error: src_base and src_base_abs_path must both be provided "
"or both be absent.",
file=sys.stderr,
)
sys.exit(1)

if args.src_base is None:
args.src_base = args.src
args.src_base_abs_path = args.src_abs_path

result_srcs_abs_path = glob_fs_entries(args.src_abs_path)

result_srcs: list[str] = []
result_dests: list[str] = []
result_dests_abs_path: list[str] = []

for result_src_abs_path in result_srcs_abs_path:
# Compute relative path to src_base_abs_path.
relative_path = os.path.relpath(
result_src_abs_path, args.src_base_abs_path
)

# Ensure consistency in path separators.
relative_path = relative_path.replace("\\", "/")
if relative_path == ".":
relative_path = None

result_src = args.src_base
result_dest = args.dest_base
result_dest_abs_path = args.dest_base_abs_path

if relative_path:
result_src = os.path.join(result_src, relative_path)
result_dest = os.path.join(result_dest, relative_path)
result_dest_abs_path = os.path.join(
result_dest_abs_path, relative_path
)

result_src = result_src.replace("\\", "/")
result_srcs.append(result_src)

result_dest = result_dest.replace("\\", "/")
result_dests.append(result_dest)

result_dest_abs_path = result_dest_abs_path.replace("\\", "/")
result_dests_abs_path.append(result_dest_abs_path)

output = {
"srcs": result_srcs,
"srcs_abs_path": result_srcs_abs_path,
"dests": result_dests,
"dests_abs_path": result_dests_abs_path,
}

json_output = json.dumps(output, indent=2)
print(json_output)

sys.exit(0)


if __name__ == "__main__":
main()

0 comments on commit 330f0d6

Please sign in to comment.