forked from apache/fury
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CI] Implement C++ CI scripts using python
Signed-off-by: LiangliangSui <[email protected]>
- Loading branch information
1 parent
204fd97
commit c51f8a2
Showing
2 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
import argparse | ||
import subprocess | ||
import platform | ||
import psutil | ||
import urllib.request as ulib | ||
import os | ||
|
||
|
||
def _exec_cmd(cmd: str): | ||
print(f"run command: {cmd}") | ||
result = subprocess.check_output(cmd, shell=True).decode("utf-8") | ||
print(result) | ||
return result | ||
|
||
|
||
def _get_os_name_lower(): | ||
return platform.system().lower() | ||
|
||
|
||
def _get_os_machine(): | ||
return platform.machine() | ||
|
||
|
||
def _install_bazel(): | ||
os_name = _get_os_name_lower() | ||
if os_name == "windows": | ||
# TODO: Implement Windows download bazel. | ||
bazel_cmd = "./bazel" | ||
else: | ||
URL = f"https://github.com/bazelbuild/bazel/releases/download/6.3.2/bazel-6.3.2-installer-{os_name}-{_get_os_machine()}.sh" | ||
local_name = "installer.sh" | ||
ulib.urlretrieve(URL, local_name) | ||
os.chmod(local_name, 0o777) | ||
_exec_cmd(f"./{local_name} --user") | ||
bazel_cmd = "bazel" | ||
|
||
# bazel install status check | ||
_exec_cmd(f"{bazel_cmd} --version") | ||
|
||
# default is byte | ||
total_mem = psutil.virtual_memory().total | ||
limit_jobs = int(total_mem / 1024 / 1024 / 1024 / 3) | ||
with open(".bazelrc", "a") as file: | ||
file.write(f"\nbuild --jobs={limit_jobs}") | ||
|
||
|
||
def _run_cpp(): | ||
bazel_cmd = "./bazel" if _get_os_name_lower() == "windows" else "bazel" | ||
|
||
# run test | ||
query_result = _exec_cmd(f"{bazel_cmd} query //...") | ||
_exec_cmd( | ||
"{} test {}".format( | ||
bazel_cmd, query_result.replace("\n", " ").replace("\r", " ") | ||
) | ||
) | ||
|
||
|
||
def _parse_args(): | ||
parser = argparse.ArgumentParser( | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter | ||
) | ||
parser.set_defaults(func=parser.print_help) | ||
subparsers = parser.add_subparsers() | ||
|
||
cpp_parser = subparsers.add_parser( | ||
"cpp", | ||
description="Run C++ CI", | ||
help="Run C++ CI", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
cpp_parser.set_defaults(func=_run_cpp) | ||
|
||
bazel_parser = subparsers.add_parser( | ||
"install-bazel", | ||
description="Install bazel on the current machine", | ||
help="Install bazel on the current machine", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
bazel_parser.set_defaults(func=_install_bazel) | ||
|
||
args = parser.parse_args() | ||
arg_dict = dict(vars(args)) | ||
del arg_dict["func"] | ||
args.func(**arg_dict) | ||
|
||
|
||
if __name__ == "__main__": | ||
_parse_args() |