Skip to content

Commit

Permalink
feat: add a python script to update "supports" in manifest (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Oct 14, 2024
1 parent c8269de commit 96f853e
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 1 deletion.
5 changes: 5 additions & 0 deletions build/ten_runtime/feature/install_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self):
self.build_type: str
self.config_file: str
self.log_level: int
self.assume_yes: bool


if __name__ == "__main__":
Expand Down Expand Up @@ -56,6 +57,7 @@ def __init__(self):
parser.add_argument(
"--log-level", type=int, required=True, help="specify log level"
)
parser.add_argument("--assume-yes", type=bool, default=False)

arg_info = ArgumentInfo()
args = parser.parse_args(namespace=arg_info)
Expand All @@ -74,6 +76,9 @@ def __init__(self):
if args.config_file is not None:
cmd += ["--config-file=" + args.config_file]

if args.assume_yes:
cmd += ["--yes"]

cmd += ["install"]

if args.build_type is not None:
Expand Down
7 changes: 7 additions & 0 deletions build/ten_runtime/feature/publish.gni
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ template("ten_package_publish") {
args += [ "--no-enable-publish" ]
}

args += [
"--os",
target_os,
"--cpu",
target_cpu,
]

args += [
"--log-level",
"${log_level}",
Expand Down
48 changes: 48 additions & 0 deletions build/ten_runtime/feature/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
from build.scripts import cmd_exec, touch
from common.scripts import delete_files
import subprocess


class ArgumentInfo(argparse.Namespace):
Expand All @@ -20,6 +21,8 @@ def __init__(self):
self.config_file: str
self.log_level: int
self.enable_publish: bool
self.os: str
self.cpu: str


def extract_publish_path(text: str) -> str | None:
Expand All @@ -35,8 +38,49 @@ def write_published_results_to_file(
file.write(published_results)


def update_manifest(
base_dir: str, os_str: str, cpu_str: str, log_level: int
) -> None:
manifest_path = os.path.join(base_dir, "manifest.json")

os_arch_pair = f"{os_str}:{cpu_str}"

script_dir = os.path.dirname(os.path.abspath(__file__))
update_script_path = os.path.abspath(
os.path.join(
script_dir,
"../../../tools/supports/update_supports_in_manifest_json.py",
)
)

try:
subprocess.run(
[
"python",
update_script_path,
"--input-file",
manifest_path,
"--output-file",
manifest_path,
"--os-arch-pairs",
os_arch_pair,
"--log-level",
str(log_level),
],
check=True,
)
if log_level > 0:
print(
f"Manifest updated successfully with os/cpu: {os_str}/{cpu_str}"
)
except subprocess.CalledProcessError as e:
print(f"Failed to update manifest.json: {e}")
sys.exit(1)


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

parser.add_argument(
"--tman-path",
type=str,
Expand All @@ -62,13 +106,17 @@ def write_published_results_to_file(
action=argparse.BooleanOptionalAction,
default=True,
)
parser.add_argument("--os", type=str, required=True)
parser.add_argument("--cpu", type=str, required=True)

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

if args.enable_publish is False:
sys.exit(0)

update_manifest(args.base_dir, args.os, args.cpu, args.log_level)

# Use 'tman publish' to perform the uploading.
origin_wd = os.getcwd()

Expand Down
10 changes: 10 additions & 0 deletions build/ten_runtime/feature/test.gni
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ template("ten_package_test_prepare_app") {
args += [ "release" ]
}

args += [
"--assume-yes",
"True",
]

args += [
"--config-file",
rebase_path("${root_out_dir}/tests/local_registry/config.json"),
Expand Down Expand Up @@ -649,6 +654,11 @@ template("ten_package_standalone_pkg") {
args += [ "release" ]
}

args += [
"--assume-yes",
"True",
]

args += [
"--config-file",
rebase_path("${root_out_dir}/tests/local_registry/config.json"),
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ pub async fn execute_cmd(
&all_existing_local_pkgs,
);

if has_conflict {
if has_conflict && !tman_config.assume_yes {
// "y" for continuing to install, "n" for stopping.
let ans = Confirm::new(
"Warning!!! Some local packages will be overwritten, \
Expand Down
8 changes: 8 additions & 0 deletions core/src/ten_manager/src/cmd_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ fn create_cmd() -> clap::ArgMatches {
.help("Enable verbose output")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("ASSUME_YES")
.short('y')
.long("yes")
.help("Automatically answer 'yes' to all prompts")
.action(clap::ArgAction::SetTrue),
)
.subcommand(crate::cmd::cmd_install::create_sub_cmd(&args_cfg))
.subcommand(crate::cmd::cmd_uninstall::create_sub_cmd(&args_cfg))
.subcommand(crate::cmd::cmd_package::create_sub_cmd(&args_cfg))
Expand All @@ -107,6 +114,7 @@ pub fn parse_cmd(
tman_config.user_token = matches.get_one::<String>("USER_TOKEN").cloned();
tman_config.mi_mode = matches.get_flag("MI");
tman_config.verbose = matches.get_flag("VERBOSE");
tman_config.assume_yes = matches.get_flag("ASSUME_YES");

match matches.subcommand() {
Some(("install", sub_cmd_args)) => crate::cmd::CommandData::Install(
Expand Down
2 changes: 2 additions & 0 deletions core/src/ten_manager/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct TmanConfig {

pub mi_mode: bool,
pub verbose: bool,
pub assume_yes: bool,
}

// Determine the config file path based on the platform.
Expand Down Expand Up @@ -94,5 +95,6 @@ pub fn read_config(config_file_path: &Option<String>) -> TmanConfig {
user_token: config_file_content.user_token,
mi_mode: false,
verbose: false,
assume_yes: false,
}
}
1 change: 1 addition & 0 deletions core/src/ten_manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn merge(cmd_line: TmanConfig, config_file: TmanConfig) -> TmanConfig {
user_token: cmd_line.user_token.or(config_file.user_token),
mi_mode: cmd_line.mi_mode,
verbose: cmd_line.verbose,
assume_yes: cmd_line.assume_yes,
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/ten_runtime/integration/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ addopts =
--ignore=tests/ten_runtime/integration/python/cpp_app_python/cpp_app_python_app_source/ten_packages/extension/default_extension_python/tests/
--ignore=tests/ten_runtime/integration/python/get_set_prop_python/get_set_prop_python_app/ten_packages/extension/default_extension_python/tests/
--ignore=tests/ten_runtime/integration/python/go_app_python/go_app_python_app/ten_packages/extension/default_extension_python/tests/
--ignore=tests/ten_runtime/integration/python/go_app_cythonize/go_app_cythonize_app/ten_packages/extension/default_extension_python/tests/
--ignore=tests/ten_runtime/integration/python/go_app_partially_cythonize/go_app_partially_cythonize_app/ten_packages/extension/default_extension_python/tests/
--ignore=tests/ten_runtime/integration/python/large_json_python/large_json_python_app/ten_packages/extension/default_extension_python/tests/
--ignore=tests/ten_runtime/integration/python/multi_process_python/multi_process_python_app/ten_packages/extension/default_extension_python/tests/
--ignore=tests/ten_runtime/integration/python/multiple_results_python/multiple_results_python_app/ten_packages/extension/default_extension_python/tests/
Expand Down
80 changes: 80 additions & 0 deletions tools/supports/update_supports_in_manifest_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#
# Copyright © 2024 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 json
import argparse


class ArgumentInfo(argparse.Namespace):
def __init__(self):
self.input_file: str
self.output_file: str
self.log_level: int
self.os_arch_pairs: list[str]


def update_supports(
input_file: str,
output_file: str,
os_arch_pairs: list[list[str]],
log_level: int,
):
with open(input_file, "r") as file:
data = json.load(file)

supports = [{"os": pair[0], "arch": pair[1]} for pair in os_arch_pairs]

if "supports" in data:
del data["supports"]

data["supports"] = supports

with open(output_file, "w") as file:
json.dump(data, file, indent=2)

if log_level > 0:
print(f"Updated {output_file} with new 'supports' field.")


def main():
parser = argparse.ArgumentParser(
description="Add OS:Arch pairs to the supports field of a JSON file."
)

parser.add_argument("--input-file", type=str, help="Input JSON file path")
parser.add_argument("--output-file", type=str, help="Output JSON file path")
parser.add_argument(
"--log-level", type=int, required=False, help="specify log level"
)
parser.add_argument(
"--os-arch-pairs",
metavar="os:arch",
type=str,
nargs="+",
help="OS:Arch pairs (e.g., mac:x64 linux:arm)",
)

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

os_arch_pairs = [pair.split(":") for pair in args.os_arch_pairs]

for pair in os_arch_pairs:
if (
len(pair) != 2
or pair[0] not in ["linux", "mac", "win"]
or pair[1] not in ["x86", "x64", "arm", "arm64"]
):
print(f"Invalid OS:Arch pair: {':'.join(pair)}")
return

update_supports(
args.input_file, args.output_file, os_arch_pairs, args.log_level
)


if __name__ == "__main__":
main()

0 comments on commit 96f853e

Please sign in to comment.