"""
版本兼容性检查
检查当前代码仓与基础 CANN 包间的兼容性.
"""
import argparse
import logging
from pathlib import Path
from typing import NoReturn
class VersionChecker:
@classmethod
def main(cls) -> NoReturn:
parser = argparse.ArgumentParser(description="Check Version Compatible", epilog="Best Regards!")
sub_parser = parser.add_subparsers(help="Sub-Command")
parser.add_argument("--cann_path", required=True, nargs=1, type=str,
help="CANN install path")
parser.add_argument("--cann_package_name", required=True, nargs=1, type=str,
help="CANN package name")
p_chk = sub_parser.add_parser('check_code_compatible', help="Check Version Compatible.")
p_chk.add_argument("--code_version_info_file", required=True, nargs=1, type=str,
help="Code version info file path")
p_chk.set_defaults(func=VersionChecker._check_compatible)
p_get = sub_parser.add_parser('get_package_version', help="Get Package Version")
p_get.set_defaults(func=VersionChecker._get_package_version)
args = parser.parse_args()
cann_version_info_file = Path(args.cann_path[0], args.cann_package_name[0], 'version.info').absolute()
if not cann_version_info_file.exists():
raise ValueError(f"CANN version info file({cann_version_info_file}) not exist.")
ret, cann_version = cls._get_version_str(file=cann_version_info_file)
if not ret:
raise ValueError(f"Can't get version from CANN version info file({cann_version_info_file}).")
rst = args.func(cann_version, args)
return rst
@classmethod
def _check_compatible(cls, cann_version: str, args) -> str:
code_version_info_file = Path(args.code_version_info_file[0]).absolute()
if not code_version_info_file.exists():
raise ValueError(f"Code version info file({code_version_info_file}) not exist.")
ret, code_version = cls._get_version_str(file=code_version_info_file)
if not ret:
raise ValueError(f"Can't get version from Code version info file({code_version_info_file}).")
cann_sub_version = cann_version.rsplit('.', 1)[0]
code_sub_version = code_version.rsplit('.', 1)[0]
if cann_sub_version != code_sub_version:
raise ValueError(f"The version number of the current code is {code_sub_version}, "
f"and the version number of the cann package used is {cann_sub_version}. "
f"Please install version {code_sub_version} of the cann package.")
return cann_sub_version
@classmethod
def _get_package_version(cls, cann_version: str, args) -> str:
cann_sub_version = cann_version.rsplit('.', 1)[0]
return cann_sub_version
@classmethod
def _get_version_str(cls, file: Path):
with open(file, 'r') as fh:
lines = fh.readlines()
for line in lines:
if not line.startswith('Version='):
continue
version = line[8:].replace('\r', '').replace('\n', '')
return True, version
return False, ''
if __name__ == "__main__":
logging.basicConfig(format='%(filename)s:%(lineno)d [%(levelname)s] %(message)s', level=logging.INFO)
try:
print(VersionChecker.main())
except Exception as e:
logging.error(e)
exit(1)