| 14 | |
| 15 | |
| 16 | def main(): |
| 17 | os.chdir(str(Path(__file__).resolve().parent.parent)) |
| 18 | parser = argparse.ArgumentParser( |
| 19 | formatter_class=argparse.RawTextHelpFormatter) |
| 20 | parser.add_argument("--check", action="store_true", help="check model") |
| 21 | parser.add_argument( |
| 22 | "--cmake_files", |
| 23 | nargs="+", |
| 24 | default=None, |
| 25 | dest="cmake_files", |
| 26 | help="cmake files to format, please split with space", |
| 27 | ) |
| 28 | args = parser.parse_args() |
| 29 | |
| 30 | handle_files = [] |
| 31 | if args.cmake_files: |
| 32 | handle_files = args.cmake_files |
| 33 | for cmake_file in handle_files: |
| 34 | assert os.path.isfile( |
| 35 | cmake_file |
| 36 | ), "error input --cmake_files, can not find file: {}".format( |
| 37 | cmake_file) |
| 38 | else: |
| 39 | for cmake_file_dir in CMAKE_FILS_DIRS: |
| 40 | assert os.path.isdir( |
| 41 | cmake_file_dir |
| 42 | ), "{} is not a directory, may config error for CMAKE_FILS_DIRS".format( |
| 43 | cmake_file_dir) |
| 44 | for cmake_file in [ |
| 45 | os.path.join(root, file) |
| 46 | for root, dirs, files in os.walk(cmake_file_dir) |
| 47 | for file in files if file.endswith("CMakeLists.txt") |
| 48 | or file.endswith(".cmake") |
| 49 | ]: |
| 50 | print("find cmake_file: {}".format(cmake_file)) |
| 51 | assert os.path.isfile(cmake_file), "code issue happened!!" |
| 52 | handle_files.append(cmake_file) |
| 53 | |
| 54 | for cmake_file in handle_files: |
| 55 | handle_type = ["format", "--in-place"] |
| 56 | if args.check: |
| 57 | handle_type = ["check", "--check"] |
| 58 | cmd = "cmake-format -c script/cmake_format_config.json {} {}".format( |
| 59 | handle_type[1], cmake_file) |
| 60 | print("try {}: {} with command: {}".format(handle_type[0], cmake_file, |
| 61 | cmd)) |
| 62 | try: |
| 63 | subprocess.check_call(cmd, shell=True) |
| 64 | except Exception as exc: |
| 65 | print("run cmd {} failed".format(cmd)) |
| 66 | if args.check: |
| 67 | print( |
| 68 | 'please run: "python3 script/cmakeformat.py" to format cmake files' |
| 69 | ) |
| 70 | else: |
| 71 | print("code issue happened!!, please FIXME!!") |
| 72 | raise exc |
| 73 | |