Skip to content

Commit 4be0351

Browse files
committed
tools(//tools/linter): Adding linters for python and C++
Signed-off-by: Naren Dasan <[email protected]> Signed-off-by: Naren Dasan <[email protected]>
1 parent aa3b830 commit 4be0351

12 files changed

+390
-0
lines changed

.clang-format

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
AccessModifierOffset: -1
3+
AlignAfterOpenBracket: AlwaysBreak
4+
AlignConsecutiveAssignments: false
5+
AlignConsecutiveDeclarations: false
6+
AlignEscapedNewlinesLeft: true
7+
AlignOperands: false
8+
AlignTrailingComments: false
9+
AllowAllParametersOfDeclarationOnNextLine: false
10+
AllowShortBlocksOnASingleLine: false
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortFunctionsOnASingleLine: Empty
13+
AllowShortIfStatementsOnASingleLine: false
14+
AllowShortLoopsOnASingleLine: false
15+
AlwaysBreakAfterReturnType: None
16+
AlwaysBreakBeforeMultilineStrings: true
17+
AlwaysBreakTemplateDeclarations: true
18+
BinPackArguments: false
19+
BinPackParameters: false
20+
BraceWrapping:
21+
AfterClass: false
22+
AfterControlStatement: false
23+
AfterEnum: false
24+
AfterFunction: false
25+
AfterNamespace: false
26+
AfterObjCDeclaration: false
27+
AfterStruct: false
28+
AfterUnion: false
29+
BeforeCatch: false
30+
BeforeElse: false
31+
IndentBraces: false
32+
BreakBeforeBinaryOperators: None
33+
BreakBeforeBraces: Attach
34+
BreakBeforeTernaryOperators: true
35+
BreakConstructorInitializersBeforeComma: false
36+
BreakAfterJavaFieldAnnotations: false
37+
BreakStringLiterals: false
38+
ColumnLimit: 120
39+
CommentPragmas: '^ IWYU pragma:'
40+
CompactNamespaces: false
41+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
42+
ConstructorInitializerIndentWidth: 4
43+
ContinuationIndentWidth: 4
44+
Cpp11BracedListStyle: true
45+
DerivePointerAlignment: false
46+
DisableFormat: false
47+
ForEachMacros: [ FOR_EACH_RANGE, FOR_EACH, ]
48+
IncludeCategories:
49+
- Regex: '^<.*\.h(pp)?>'
50+
Priority: 1
51+
- Regex: '^<.*'
52+
Priority: 2
53+
- Regex: '.*'
54+
Priority: 3
55+
IndentCaseLabels: true
56+
IndentWidth: 2
57+
IndentWrappedFunctionNames: false
58+
KeepEmptyLinesAtTheStartOfBlocks: false
59+
MacroBlockBegin: ''
60+
MacroBlockEnd: ''
61+
MaxEmptyLinesToKeep: 1
62+
NamespaceIndentation: None
63+
ObjCBlockIndentWidth: 2
64+
ObjCSpaceAfterProperty: false
65+
ObjCSpaceBeforeProtocolList: false
66+
PenaltyBreakBeforeFirstCallParameter: 1
67+
PenaltyBreakComment: 300
68+
PenaltyBreakFirstLessLess: 120
69+
PenaltyBreakString: 1000
70+
PenaltyExcessCharacter: 1000000
71+
PenaltyReturnTypeOnItsOwnLine: 2000000
72+
PointerAlignment: Left
73+
ReflowComments: true
74+
SortIncludes: true
75+
SpaceAfterCStyleCast: false
76+
SpaceBeforeAssignmentOperators: true
77+
SpaceBeforeParens: ControlStatements
78+
SpaceInEmptyParentheses: false
79+
SpacesBeforeTrailingComments: 1
80+
SpacesInAngles: false
81+
SpacesInContainerLiterals: true
82+
SpacesInCStyleCastParentheses: false
83+
SpacesInParentheses: false
84+
SpacesInSquareBrackets: false
85+
Standard: Cpp11
86+
TabWidth: 8
87+
UseTab: Never

.clang-tidy

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# NOTE there must be no spaces before the '-', so put the comma last.
3+
InheritParentConfig: true
4+
Checks: '
5+
bugprone-*,
6+
-bugprone-forward-declaration-namespace,
7+
-bugprone-macro-parentheses,
8+
-bugprone-lambda-function-name,
9+
cppcoreguidelines-*,
10+
-cppcoreguidelines-interfaces-global-init,
11+
-cppcoreguidelines-owning-memory,
12+
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
13+
-cppcoreguidelines-pro-bounds-constant-array-index,
14+
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
15+
-cppcoreguidelines-pro-type-cstyle-cast,
16+
-cppcoreguidelines-pro-type-reinterpret-cast,
17+
-cppcoreguidelines-pro-type-static-cast-downcast,
18+
-cppcoreguidelines-pro-type-union-access,
19+
-cppcoreguidelines-pro-type-vararg,
20+
-cppcoreguidelines-special-member-functions,
21+
-facebook-hte-RelativeInclude,
22+
hicpp-exception-baseclass,
23+
hicpp-avoid-goto,
24+
modernize-*,
25+
-modernize-return-braced-init-list,
26+
-modernize-use-auto,
27+
-modernize-use-default-member-init,
28+
-modernize-use-using,
29+
-modernize-use-trailing-return-type,
30+
performance-*,
31+
-performance-noexcept-move-constructor,
32+
'
33+
HeaderFilterRegex: 'torch/csrc/.*'
34+
AnalyzeTemporaryDtors: false
35+
CheckOptions:
36+
...

.style.yapf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[style]
2+
based_on_style = google
3+
indent_width = 4
4+
COLUMN_LIMIT = 120
5+
SPACES_BEFORE_COMMENT = 2
6+
EACH_DICT_ENTRY_ON_SEPARATE_LINE = 1
7+
SPLIT_BEFORE_FIRST_ARGUMENT = false
8+
SPLIT_BEFORE_NAMED_ASSIGNS = true

WORKSPACE

+9
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,12 @@ pip3_import(
139139
load("@py_test_deps//:requirements.bzl", "pip_install")
140140
pip_install()
141141

142+
pip3_import(
143+
name = "pylinter_deps",
144+
requirements = "//tools/linter:requirements.txt",
145+
)
146+
147+
load("@pylinter_deps//:requirements.bzl", "pip_install")
148+
pip_install()
149+
150+

tools/BUILD

Whitespace-only changes.

tools/linter/BUILD

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
load("@pylinter_deps//:requirements.bzl", "requirement")
2+
3+
py_binary(
4+
name = "cpplint",
5+
srcs = ["cpplint.py"],
6+
python_version = "PY3",
7+
srcs_version = "PY3",
8+
visibility = ["//visibility:public"],
9+
deps = [":utils"],
10+
)
11+
12+
py_binary(
13+
name = "cpplint_diff",
14+
srcs = ["cpplint_diff.py"],
15+
python_version = "PY3",
16+
srcs_version = "PY3",
17+
visibility = ["//visibility:public"],
18+
deps = [":utils"],
19+
)
20+
21+
py_binary(
22+
name = "pylint",
23+
srcs = ["pylint.py"],
24+
python_version = "PY3",
25+
srcs_version = "PY3",
26+
visibility = ["//visibility:public"],
27+
deps = [
28+
":utils",
29+
requirement("yapf"),
30+
],
31+
)
32+
33+
py_binary(
34+
name = "pylint_diff",
35+
srcs = ["pylint_diff.py"],
36+
python_version = "PY3",
37+
srcs_version = "PY3",
38+
visibility = ["//visibility:public"],
39+
deps = [
40+
":utils",
41+
requirement("yapf"),
42+
],
43+
)
44+
45+
py_library(
46+
name = "utils",
47+
srcs = ["utils.py"],
48+
srcs_version = "PY3",
49+
)

tools/linter/cpplint.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import sys
3+
import glob
4+
import subprocess
5+
import utils
6+
7+
VALID_CPP_FILE_TYPES = [".cpp", ".cc", ".c", ".cu", ".hpp", ".h", ".cuh"]
8+
9+
10+
def lint(user, target_files, change_file=True):
11+
cmd = ['clang-format']
12+
if change_file:
13+
cmd.append("-i")
14+
print(
15+
"\033[93mWARNING:\033[0m This command is modifying your files with the recommended linting, you should review the changes before committing"
16+
)
17+
for f in target_files:
18+
cmd.append(f)
19+
subprocess.run(cmd)
20+
subprocess.run(["chown", user + ":" + user, f])
21+
subprocess.run(["chmod", "u+rw,g+rw", f])
22+
23+
24+
if __name__ == "__main__":
25+
BAZEL_ROOT = utils.find_bazel_root()
26+
USER = BAZEL_ROOT.split('/')[2]
27+
projects = utils.CHECK_PROJECTS(sys.argv[1:])
28+
if "//..." in projects:
29+
projects = [p.replace(BAZEL_ROOT, "/")[:-1] for p in glob.glob(BAZEL_ROOT + '/*/')]
30+
projects = [p for p in projects if p not in utils.BLACKLISTED_BAZEL_TARGETS]
31+
32+
for p in projects:
33+
if p.endswith("/..."):
34+
p = p[:-4]
35+
path = BAZEL_ROOT + '/' + p[2:]
36+
files = utils.glob_files(path, VALID_CPP_FILE_TYPES)
37+
if files != []:
38+
lint(USER, files)

tools/linter/cpplint_diff.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import sys
3+
import glob
4+
import subprocess
5+
import utils
6+
7+
VALID_CPP_FILE_TYPES = [".cpp", ".cc", ".c", ".cu", ".hpp", ".h", ".cuh"]
8+
9+
10+
def lint(target_files):
11+
failure = False
12+
for f in target_files:
13+
with open("/tmp/changes.txt", "w") as changes:
14+
subprocess.run(['clang-format', f], stdout=changes)
15+
output = subprocess.run(["git", "diff", "-u", "--color", f, "/tmp/changes.txt"])
16+
if output.returncode != 0:
17+
failure = True
18+
return failure
19+
20+
21+
if __name__ == "__main__":
22+
BAZEL_ROOT = utils.find_bazel_root()
23+
projects = utils.CHECK_PROJECTS(sys.argv[1:])
24+
if "//..." in projects:
25+
projects = [p.replace(BAZEL_ROOT, "/")[:-1] for p in glob.glob(BAZEL_ROOT + '/*/')]
26+
projects = [p for p in projects if p not in utils.BLACKLISTED_BAZEL_TARGETS]
27+
28+
failure = False
29+
for p in projects:
30+
if p.endswith("/..."):
31+
p = p[:-4]
32+
path = BAZEL_ROOT + '/' + p[2:]
33+
files = utils.glob_files(path, VALID_CPP_FILE_TYPES)
34+
if files != []:
35+
failure = lint(files)
36+
if failure:
37+
print("\033[91mERROR:\033[0m Some files do not conform to style guidelines")
38+
sys.exit(1)

tools/linter/pylint.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import sys
3+
import glob
4+
import utils
5+
import subprocess
6+
import yapf
7+
8+
VALID_PY_FILE_TYPES = [".py"]
9+
10+
11+
def lint(user, target_files, conf, change_file=True):
12+
return yapf.FormatFiles(
13+
filenames=target_files,
14+
lines=None,
15+
style_config=conf,
16+
no_local_style=None,
17+
in_place=change_file,
18+
print_diff=False,
19+
verify=True,
20+
parallel=True,
21+
verbose=True)
22+
23+
24+
if __name__ == "__main__":
25+
BAZEL_ROOT = utils.find_bazel_root()
26+
STYLE_CONF_PATH = BAZEL_ROOT + "/.style.yapf"
27+
USER = BAZEL_ROOT.split('/')[2]
28+
projects = utils.CHECK_PROJECTS(sys.argv[1:])
29+
if "//..." in projects:
30+
projects = [p.replace(BAZEL_ROOT, "/")[:-1] for p in glob.glob(BAZEL_ROOT + '/*/')]
31+
projects = [p for p in projects if p not in utils.BLACKLISTED_BAZEL_TARGETS]
32+
33+
for p in projects:
34+
if p.endswith("/..."):
35+
p = p[:-4]
36+
path = BAZEL_ROOT + '/' + p[2:]
37+
files = utils.glob_files(path, VALID_PY_FILE_TYPES)
38+
if files != []:
39+
changed = lint(USER, files, STYLE_CONF_PATH)
40+
if changed:
41+
print(
42+
"\033[93mWARNING:\033[0m This command modified your files with the recommended linting, you should review the changes before committing"
43+
)
44+
sys.exit(1)

tools/linter/pylint_diff.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import sys
3+
import glob
4+
import utils
5+
import subprocess
6+
import yapf
7+
8+
VALID_PY_FILE_TYPES = [".py"]
9+
10+
11+
def lint(user, target_files, conf, change_file=True):
12+
return yapf.FormatFiles(
13+
filenames=target_files,
14+
lines=None,
15+
style_config=conf,
16+
no_local_style=None,
17+
in_place=change_file,
18+
print_diff=False,
19+
verify=True,
20+
parallel=True,
21+
verbose=True)
22+
23+
24+
if __name__ == "__main__":
25+
BAZEL_ROOT = utils.find_bazel_root()
26+
STYLE_CONF_PATH = BAZEL_ROOT + "/.style.yapf"
27+
USER = BAZEL_ROOT.split('/')[2]
28+
projects = utils.CHECK_PROJECTS(sys.argv[1:])
29+
if "//..." in projects:
30+
projects = [p.replace(BAZEL_ROOT, "/")[:-1] for p in glob.glob(BAZEL_ROOT + '/*/')]
31+
projects = [p for p in projects if p not in utils.BLACKLISTED_BAZEL_TARGETS]
32+
33+
for p in projects:
34+
if p.endswith("/..."):
35+
p = p[:-4]
36+
path = BAZEL_ROOT + '/' + p[2:]
37+
files = utils.glob_files(path, VALID_PY_FILE_TYPES)
38+
if files != []:
39+
changed = lint(USER, files, STYLE_CONF_PATH)
40+
if changed:
41+
print(
42+
"\033[93mWARNING:\033[0m This command modified your files with the recommended linting, you should review the changes before committing"
43+
)
44+
sys.exit(1)

tools/linter/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yapf

0 commit comments

Comments
 (0)