"""Copyright (c) 2024 l.feng. All rights reserved.cppcheck-wheel: Package cppcheck as a python wheel."""from__future__importannotationsimportargparseimportfunctoolsimportosimportsubprocessimportsysfrompathlibimportPathfromtypingimportIterablefromimportlib_metadataimportdistributionfrom._versionimportversionas__version____all__=["__version__","cppcheck","get_cppcheck_dir"]
[docs]@functools.lru_cache(maxsize=None)defget_cppcheck_dir()->Path:"""Cppcheck binary directory"""package_files=distribution(__package__).filesassertpackage_filesisnotNone,f"There must exist {__package__} files"forfileinpackage_files:ifstr(file).startswith(f"{__package__}/Cppcheck"):resolved_file=Path(str(file.locate())).resolve(strict=True)cppcheck_path=resolved_file.parents[1]ifcppcheck_path.exists():returncppcheck_pathraiseFileNotFoundError("No found for Cppcheck in package "+__package__)
[docs]defcppcheck()->None:"""Cppcheck entry"""parser=argparse.ArgumentParser(add_help=False)parser.add_argument("--version",action="store_true",help="Show the current version of cppcheck-wheel",)args,_=parser.parse_known_args()ifargs.version:result=subprocess.run([get_cppcheck_dir()/"cppcheck","--version"],capture_output=True,text=True,check=False,)cppcheck_version_out=result.stdout.rstrip()sys.stdout.write(f"{cppcheck_version_out} from cppcheck-wheel {__version__}\n")else:_program_exit("cppcheck",*sys.argv[1:])