onnxruntime/tools/nuget/validate_package.py
raoanag 424107a82a
Merge main to WindowsAI (#18122)
### Description
Merge main to WindowsAI



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->

---------

Signed-off-by: Nash <george.nash@intel.com>
Signed-off-by: Yiming Hu <yiming.hu@amd.com>
Signed-off-by: Liqun Fu <liqfu@microsoft.com>
Co-authored-by: Kaz Nishimura <kazssym@linuxfront.com>
Co-authored-by: Tianlei Wu <tlwu@microsoft.com>
Co-authored-by: Nat Kershaw (MSFT) <nakersha@microsoft.com>
Co-authored-by: Yulong Wang <7679871+fs-eire@users.noreply.github.com>
Co-authored-by: Changming Sun <chasun@microsoft.com>
Co-authored-by: zesongw <zesong.wang@intel.com>
Co-authored-by: Yi Zhang <zhanyi@microsoft.com>
Co-authored-by: Dmitri Smirnov <yuslepukhin@users.noreply.github.com>
Co-authored-by: Yifan Li <109183385+yf711@users.noreply.github.com>
Co-authored-by: simonjub <78098752+simonjub@users.noreply.github.com>
Co-authored-by: PeixuanZuo <94887879+PeixuanZuo@users.noreply.github.com>
Co-authored-by: Adrian Lizarraga <adlizarraga@microsoft.com>
Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
Co-authored-by: Arthur Islamov <arthur@islamov.ai>
Co-authored-by: Jambay Kinley <jambaykinley@microsoft.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Wei-Sheng Chin <wschin@outlook.com>
Co-authored-by: Bowen Bao <bowbao@microsoft.com>
Co-authored-by: Hariharan Seshadri <shariharan91@gmail.com>
Co-authored-by: Numfor Tiapo <numsmt2@gmail.com>
Co-authored-by: Vincent Wang <wangwchpku@outlook.com>
Co-authored-by: Pranav Sharma <prs@microsoft.com>
Co-authored-by: George Nash <george.nash@intel.com>
Co-authored-by: Abhishek Jindal <abjindal@microsoft.com>
Co-authored-by: pengwa <pengwa@microsoft.com>
Co-authored-by: Yiming Hu <woinck@users.noreply.github.com>
Co-authored-by: Jiajia Qin <jiajia.qin@intel.com>
Co-authored-by: Lukas Berbuer <36054362+lukasberbuer@users.noreply.github.com>
Co-authored-by: Wanming Lin <wanming.lin@intel.com>
Co-authored-by: Xavier Dupré <xadupre@users.noreply.github.com>
Co-authored-by: aimilefth <60664743+aimilefth@users.noreply.github.com>
Co-authored-by: Baiju Meswani <bmeswani@microsoft.com>
Co-authored-by: Adam Pocock <adam.pocock@oracle.com>
Co-authored-by: Chi Lo <54722500+chilo-ms@users.noreply.github.com>
Co-authored-by: RandySheriffH <48490400+RandySheriffH@users.noreply.github.com>
Co-authored-by: Randy Shuai <rashuai@microsoft.com>
Co-authored-by: Vadym Stupakov <vadim.stupakov@gmail.com>
Co-authored-by: Jian Chen <cjian@microsoft.com>
Co-authored-by: Brian Lambert <98757707+brian-pieces@users.noreply.github.com>
Co-authored-by: Nicolò Lucchesi <nicolo.lucchesi@gmail.com>
Co-authored-by: liqun Fu <liqfu@microsoft.com>
Co-authored-by: trajep <trajepl@gmail.com>
Co-authored-by: Scott McKay <skottmckay@gmail.com>
Co-authored-by: Mustafa Ateş Uzun <mustafauzun0@gmail.com>
Co-authored-by: MistEO <mistereo@hotmail.com>
Co-authored-by: satyajandhyala <satya.k.jandhyala@gmail.com>
Co-authored-by: shaahji <96227573+shaahji@users.noreply.github.com>
Co-authored-by: Rachel Guo <35738743+YUNQIUGUO@users.noreply.github.com>
Co-authored-by: rachguo <rachguo@rachguos-Mini.attlocal.net>
Co-authored-by: Caroline Zhu <wolfivyaura@gmail.com>
Co-authored-by: Caroline Zhu <carolinezhu@microsoft.com>
Co-authored-by: Guenther Schmuelling <guschmue@microsoft.com>
Co-authored-by: xhcao <xinghua.cao@intel.com>
Co-authored-by: Ella Charlaix <80481427+echarlaix@users.noreply.github.com>
Co-authored-by: Xu Xing <xing.xu@intel.com>
Co-authored-by: Hector Li <hecli@microsoft.com>
Co-authored-by: Ye Wang <52801275+wangyems@users.noreply.github.com>
Co-authored-by: Your Name <you@example.com>
Co-authored-by: Benedikt Hilmes <benedikt.hilmes@rwth-aachen.de>
Co-authored-by: rachguo <rachguo@rachguos-Mac-mini.local>
Co-authored-by: George Wu <jywu@microsoft.com>
Co-authored-by: JiCheng <wejoncy@163.com>
Co-authored-by: Sheil Kumar <smk2007@gmail.com>
Co-authored-by: Sheil Kumar <sheilk@microsoft.com>
Co-authored-by: cloudhan <guangyunhan@microsoft.com>
Co-authored-by: kyoshisuki <143475866+kyoshisuki@users.noreply.github.com>
Co-authored-by: aciddelgado <139922440+aciddelgado@users.noreply.github.com>
Co-authored-by: tlwu@microsoft.com <tlwu@a100.crj0ad2y1kku1j4yxl4sj10o4e.gx.internal.cloudapp.net>
Co-authored-by: Maximilian Müller <44298237+gedoensmax@users.noreply.github.com>
Co-authored-by: Tang, Cheng <souptc@gmail.com>
Co-authored-by: Cheng Tang <chenta@microsoft.com@orttrainingdev9.d32nl1ml4oruzj4qz3bqlggovf.px.internal.cloudapp.net>
Co-authored-by: Cheng Tang <chenta@microsoft.com>
Co-authored-by: Jeff Daily <jeff.daily@amd.com>
Co-authored-by: cloudhan <cloudhan@outlook.com>
Co-authored-by: Yufeng Li <liyufeng1987@gmail.com>
Co-authored-by: Zhang Lei <zhang.huanning@hotmail.com>
Co-authored-by: Dwayne Robinson <fdwr@hotmail.com>
Co-authored-by: Zhipeng Han <zhipeng.han@outlook.com>
Co-authored-by: Thiago Crepaldi <thiago.crepaldi@microsoft.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Patrice Vignola <vignola.patrice@gmail.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: snadampal <87143774+snadampal@users.noreply.github.com>
Co-authored-by: Sumit Agarwal <sumitagarwal330@gmail.com>
Co-authored-by: Ashwini Khade <askhade@microsoft.com>
Co-authored-by: Yang Gu <yang.gu@intel.com>
Co-authored-by: Cheng Tang <chenta@a100.crj0ad2y1kku1j4yxl4sj10o4e.gx.internal.cloudapp.net>
Co-authored-by: mindest <30493312+mindest@users.noreply.github.com>
Co-authored-by: Scott McKay <Scott.McKay@microsoft.com>
Co-authored-by: Xavier Dupre <xadupre@microsoft.com@orttrainingdev9.d32nl1ml4oruzj4qz3bqlggovf.px.internal.cloudapp.net>
Co-authored-by: guyang3532 <62738430+guyang3532@users.noreply.github.com>
Co-authored-by: Carson M <carson@pyke.io>
Co-authored-by: sophies927 <107952697+sophies927@users.noreply.github.com>
2023-10-27 17:08:01 -07:00

374 lines
12 KiB
Python

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import argparse
import glob
import os
import re
import sys
import zipfile # Available Python 3.2 or higher
linux_gpu_package_libraries = [
"libonnxruntime_providers_shared.so",
"libonnxruntime_providers_cuda.so",
"libonnxruntime_providers_tensorrt.so",
]
win_gpu_package_libraries = [
"onnxruntime_providers_shared.lib",
"onnxruntime_providers_shared.dll",
"onnxruntime_providers_cuda.lib",
"onnxruntime_providers_cuda.dll",
"onnxruntime_providers_tensorrt.lib",
"onnxruntime_providers_tensorrt.dll",
]
gpu_related_header_files = [
"cpu_provider_factory.h",
"onnxruntime_c_api.h",
"onnxruntime_cxx_api.h",
"onnxruntime_float16.h",
"onnxruntime_cxx_inline.h",
]
dmlep_related_header_files = [
"cpu_provider_factory.h",
"onnxruntime_c_api.h",
"onnxruntime_cxx_api.h",
"onnxruntime_float16.h",
"onnxruntime_cxx_inline.h",
"dml_provider_factory.h",
]
training_related_header_files = [
"onnxruntime_c_api.h",
"onnxruntime_float16.h",
"onnxruntime_cxx_api.h",
"onnxruntime_cxx_inline.h",
"onnxruntime_training_c_api.h",
"onnxruntime_training_cxx_api.h",
"onnxruntime_training_cxx_inline.h",
]
def parse_arguments():
parser = argparse.ArgumentParser(
description="Validate ONNX Runtime native nuget containing native shared library artifacts spec script",
usage="",
)
# Main arguments
parser.add_argument("--package_type", required=True, help="Specify nuget, tarball or zip.")
parser.add_argument("--package_name", required=True, help="Package name to be validated.")
parser.add_argument(
"--package_path",
required=True,
help="Path containing the package to be validated. Must only contain only one package within this.",
)
parser.add_argument(
"--platforms_supported", required=True, help="Comma separated list (no space). Ex: linux-x64,win-x86,osx-x64"
)
parser.add_argument(
"--verify_nuget_signing",
help="Flag indicating if Nuget package signing is to be verified. Only accepts 'true' or 'false'",
)
return parser.parse_args()
def check_exists(path):
return os.path.exists(path)
def remove_residual_files(path):
if check_exists(path):
os.remove(path)
def is_windows():
return sys.platform.startswith("win")
def check_if_headers_are_present(header_files, header_folder, file_list_in_package, platform):
for header in header_files:
path = header_folder + "/" + header
print("Checking path: " + path)
if path not in file_list_in_package:
print(header + " not found for " + platform)
raise Exception(header + " not found for " + platform)
def check_if_dlls_are_present(
package_type,
is_windows_ai_package,
is_gpu_package,
is_dml_package,
is_training_package,
platforms_supported,
zip_file,
package_path,
):
platforms = platforms_supported.strip().split(",")
if package_type == "tarball":
file_list_in_package = list()
for dirpath, _dirnames, filenames in os.walk(package_path):
file_list_in_package += [os.path.join(dirpath, file) for file in filenames]
else:
file_list_in_package = zip_file.namelist()
for platform in platforms:
if platform.startswith("win"):
native_folder = "_native" if is_windows_ai_package else "native"
if package_type == "nuget":
folder = "runtimes/" + platform + "/" + native_folder
header_folder = "build/native/include"
else: # zip package
folder = package_path + "/lib"
header_folder = package_path + "/include"
path = folder + "/" + "onnxruntime.dll"
print("Checking path: " + path)
if path not in file_list_in_package:
print("onnxruntime.dll not found for " + platform)
raise Exception("onnxruntime.dll not found for " + platform)
if is_gpu_package:
for dll in win_gpu_package_libraries:
path = folder + "/" + dll
print("Checking path: " + path)
if path not in file_list_in_package:
print(dll + " not found for " + platform)
raise Exception(dll + " not found for " + platform)
check_if_headers_are_present(gpu_related_header_files, header_folder, file_list_in_package, platform)
if is_dml_package:
check_if_headers_are_present(dmlep_related_header_files, header_folder, file_list_in_package, platform)
if is_training_package:
check_if_headers_are_present(
training_related_header_files, header_folder, file_list_in_package, platform
)
elif platform.startswith("linux"):
if package_type == "nuget":
folder = "runtimes/" + platform + "/native"
header_folder = "build/native/include"
else: # tarball package
folder = package_path + "/lib"
header_folder = package_path + "/include"
path = folder + "/" + "libonnxruntime.so"
print("Checking path: " + path)
if path not in file_list_in_package:
print("libonnxruntime.so not found for " + platform)
raise Exception("libonnxruntime.so not found for " + platform)
if is_gpu_package:
for so in linux_gpu_package_libraries:
path = folder + "/" + so
print("Checking path: " + path)
if path not in file_list_in_package:
print(so + " not found for " + platform)
raise Exception(so + " not found for " + platform)
for header in gpu_related_header_files:
path = header_folder + "/" + header
print("Checking path: " + path)
if path not in file_list_in_package:
print(header + " not found for " + platform)
raise Exception(header + " not found for " + platform)
elif platform.startswith("osx"):
path = "runtimes/" + platform + "/native/libonnxruntime.dylib"
print("Checking path: " + path)
if path not in file_list_in_package:
print("libonnxruntime.dylib not found for " + platform)
raise Exception("libonnxruntime.dylib not found for " + platform)
else:
raise Exception("Unsupported platform: " + platform)
def check_if_nuget_is_signed(nuget_path):
code_sign_summary_file = glob.glob(os.path.join(nuget_path, "*.md"))
if len(code_sign_summary_file) != 1:
print("CodeSignSummary files found in path: ")
print(code_sign_summary_file)
raise Exception("No CodeSignSummary files / more than one CodeSignSummary files found in the given path.")
print("CodeSignSummary file: " + code_sign_summary_file[0])
with open(code_sign_summary_file[0]) as f:
contents = f.read()
return "Pass" in contents
return False
def validate_tarball(args):
files = glob.glob(os.path.join(args.package_path, args.package_name))
if len(files) != 1:
print("packages found in path: ")
print(files)
raise Exception("No packages / more than one packages found in the given path.")
package_name = args.package_name
if "-gpu-" in package_name.lower():
is_gpu_package = True
else:
is_gpu_package = False
package_folder = re.search("(.*)[.].*", package_name).group(1)
print("tar zxvf " + package_name)
os.system("tar zxvf " + package_name)
is_windows_ai_package = False
zip_file = None
is_dml_package = False
is_training_package = False
check_if_dlls_are_present(
args.package_type,
is_windows_ai_package,
is_gpu_package,
is_dml_package,
is_training_package,
args.platforms_supported,
zip_file,
package_folder,
)
def validate_zip(args):
files = glob.glob(os.path.join(args.package_path, args.package_name))
if len(files) != 1:
print("packages found in path: ")
print(files)
raise Exception("No packages / more than one packages found in the given path.")
package_name = args.package_name
if "-gpu-" in package_name.lower():
is_gpu_package = True
else:
is_gpu_package = False
package_folder = re.search("(.*)[.].*", package_name).group(1)
is_windows_ai_package = False
is_dml_package = False
is_training_package = False
zip_file = zipfile.ZipFile(package_name)
check_if_dlls_are_present(
args.package_type,
is_windows_ai_package,
is_gpu_package,
is_dml_package,
is_training_package,
args.platforms_supported,
zip_file,
package_folder,
)
def validate_nuget(args):
files = glob.glob(os.path.join(args.package_path, args.package_name))
nuget_packages_found_in_path = [i for i in files if i.endswith(".nupkg") and "Managed" not in i]
if len(nuget_packages_found_in_path) != 1:
print("Nuget packages found in path: ")
print(nuget_packages_found_in_path)
raise Exception("No Nuget packages / more than one Nuget packages found in the given path.")
nuget_file_name = nuget_packages_found_in_path[0]
full_nuget_path = os.path.join(args.package_path, nuget_file_name)
if "Gpu" in nuget_file_name:
is_gpu_package = True
else:
is_gpu_package = False
if "directml" in nuget_file_name.lower():
is_dml_package = True
else:
is_dml_package = False
if "Training" in nuget_file_name:
is_training_package = True
else:
is_training_package = False
exit_code = 0
nupkg_copy_name = "NugetCopy.nupkg"
zip_copy_name = "NugetCopy.zip"
zip_file = None
# Remove any residual files
remove_residual_files(nupkg_copy_name)
remove_residual_files(zip_copy_name)
# Do all validations here
try:
if not is_windows():
raise Exception("Nuget validation is currently supported only on Windows")
# Make a copy of the Nuget package
print("Copying [" + full_nuget_path + "] -> [" + nupkg_copy_name + "], and extracting its contents")
os.system("copy " + full_nuget_path + " " + nupkg_copy_name)
# Convert nupkg to zip
os.rename(nupkg_copy_name, zip_copy_name)
zip_file = zipfile.ZipFile(zip_copy_name)
# Check if the relevant dlls are present in the Nuget/Zip
print("Checking if the Nuget contains relevant dlls")
is_windows_ai_package = os.path.basename(full_nuget_path).startswith("Microsoft.AI.MachineLearning")
check_if_dlls_are_present(
args.package_type,
is_windows_ai_package,
is_gpu_package,
is_dml_package,
is_training_package,
args.platforms_supported,
zip_file,
None,
)
verify_nuget_signing = args.verify_nuget_signing.lower()
# Check if the Nuget has been signed
if verify_nuget_signing != "true" and verify_nuget_signing != "false":
raise Exception("Parameter verify_nuget_signing accepts only true or false as an argument")
if verify_nuget_signing == "true":
print("Verifying if Nuget has been signed")
if not check_if_nuget_is_signed(args.package_path):
print("Nuget signing verification failed")
raise Exception("Nuget signing verification failed")
except Exception as e:
print(e)
exit_code = 1
finally:
print("Cleaning up after Nuget validation")
if zip_file is not None:
zip_file.close()
if check_exists(zip_copy_name):
os.remove(zip_copy_name)
if exit_code == 0:
print("Nuget validation was successful")
else:
raise Exception("Nuget validation was unsuccessful")
def main():
args = parse_arguments()
if args.package_type == "nuget":
validate_nuget(args)
elif args.package_type == "tarball":
validate_tarball(args)
elif args.package_type == "zip":
validate_zip(args)
else:
print(f"Package type {args.package_type} is not supported")
if __name__ == "__main__":
sys.exit(main())