MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / main

Function main

tools/format.py:68–137  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

66
67
68def main():
69 parser = argparse.ArgumentParser(
70 description="Format source files using clang-format, eg: `./tools/format.py src -w`. \
71 Require clang-format version == 12.0",
72 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
73 )
74
75 parser.add_argument(
76 "path", nargs="+", help="file name or path based on MegBrain root dir."
77 )
78 parser.add_argument(
79 "-w",
80 "--write",
81 action="store_true",
82 help="use formatted file to replace original file.",
83 )
84 parser.add_argument(
85 "--clang-format",
86 default=os.getenv("CLANG_FORMAT", "clang-format"),
87 help="clang-format executable name; it can also be "
88 "modified via the CLANG_FORMAT environment var",
89 )
90 args = parser.parse_args()
91
92 format_type = [".cpp", ".c", ".h", ".cu", ".cuh", ".inl"]
93
94 def getfiles(path):
95 rst = []
96 for p in os.listdir(path):
97 p = os.path.join(path, p)
98 if os.path.isdir(p):
99 rst += getfiles(p)
100 elif (
101 os.path.isfile(p)
102 and not os.path.islink(p)
103 and os.path.splitext(p)[1] in format_type
104 ):
105 rst.append(p)
106 return rst
107
108 files = []
109 for path in args.path:
110 if os.path.isdir(path):
111 files += getfiles(path)
112 elif os.path.isfile(path):
113 files.append(path)
114 else:
115 raise ValueError("Invalid path {}".format(path))
116
117 # check version, we only support 12.0.1 now
118 version = subprocess.check_output([args.clang_format, "--version",],)
119 version = version.decode("utf-8")
120
121 need_version = "12.0.1"
122 if version.find(need_version) < 0:
123 print(
124 "We only support {} now, please install {} version, find version: {}".format(
125 need_version, need_version, version

Callers 1

format.pyFile · 0.70

Calls 5

getfilesFunction · 0.85
printFunction · 0.85
appendMethod · 0.45
formatMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected