Skip to content

Commit 44214bb

Browse files
jianyuhfacebook-github-bot
authored andcommitted
add lint to oss (pytorch#1427)
Summary: Pull Request resolved: pytorch#1427 Pull Request resolved: pytorch#1417 Enforce format check to fbgemm OSS. Leave this to Airidas Korolkovas as the starter rampup task. Differential Revision: https://internalfb.com/D40620055 fbshipit-source-id: a611d68936427a4c08d4cbc625847cddf6c7b0d9
1 parent 79f2073 commit 44214bb

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

.github/workflows/pylint.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
branches:
10+
- main
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
python-version: ["3.8"]
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v2
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
- name: Install dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install ufmt
27+
pip install click
28+
pip install flake8
29+
- name: Analyzing the code with flake8
30+
run: |
31+
echo "::add-matcher::fbgemm_gpu/test/lint/flake8_problem_matcher.json"
32+
flake8 --ignore=E501 . # Ignore line too long issue
33+
- name: Analyzing the code with ufmt
34+
run: |
35+
ufmt diff fbgemm_gpu/fbgemm_gpu
36+
ufmt diff fbgemm_gpu/test
37+
ufmt diff fbgemm_gpu/bench
38+
- name: Check Meta copyright header
39+
run: |
40+
python fbgemm_gpu/test/lint/check_meta_header.py --path=./fbgemm_gpu/fbgemm_gpu --fixit=False
41+
python fbgemm_gpu/test/lint/check_meta_header.py --path=./fbgemm_gpu/test --fixit=False
42+
python fbgemm_gpu/test/lint/check_meta_header.py --path=./fbgemm_gpu/bench --fixit=False
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) Meta Platforms, Inc. and affiliates.
4+
# All rights reserved.
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) Meta Platforms, Inc. and affiliates.
4+
# All rights reserved.
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
# pyre-unsafe
9+
10+
"""Check Python source code contains Meta copyright header
11+
"""
12+
13+
from __future__ import annotations
14+
15+
import os
16+
import sys
17+
18+
import click
19+
20+
21+
def process_header(header, comment):
22+
lines = header.split("\n")
23+
new_lines = []
24+
for line in lines:
25+
if line is None or line == "":
26+
new_lines.append(comment)
27+
else:
28+
new_lines.append(comment + " " + line)
29+
return "\n".join(new_lines) + "\n"
30+
31+
32+
HEADER = """Copyright (c) Meta Platforms, Inc. and affiliates.
33+
All rights reserved.
34+
This source code is licensed under the BSD-style license found in the
35+
LICENSE file in the root directory of this source tree.
36+
"""
37+
HEADER_lines = HEADER.splitlines()[1:]
38+
PY_HEADER = process_header(HEADER, "#")
39+
CPP_HEADER = process_header(HEADER, "//")
40+
41+
42+
def dfs(root_path: str) -> list[str]:
43+
"""DFS source code tree to find python files missing header
44+
45+
Parameters
46+
----------
47+
root_path : str
48+
root source directory path
49+
50+
Returns
51+
-------
52+
list[str]
53+
file list missing header
54+
"""
55+
ret = []
56+
for root, _, files in os.walk(root_path, topdown=False):
57+
for name in files:
58+
path = os.path.join(root, name)
59+
if path.endswith(".py"):
60+
with open(path) as fi:
61+
src = fi.read()
62+
flag = True
63+
for line in HEADER_lines:
64+
if line not in src:
65+
flag = False
66+
break
67+
if not flag:
68+
ret.append(path)
69+
return ret
70+
71+
72+
def fix_header(file_list: list[str]) -> None:
73+
"""Adding Meta header to to source files
74+
75+
Parameters
76+
----------
77+
file_list : list[str]
78+
file list missing header
79+
"""
80+
for path in file_list:
81+
src = ""
82+
with open(path) as fi:
83+
src = fi.read()
84+
with open(path, "w") as fo:
85+
fo.write(PY_HEADER)
86+
fo.write(src)
87+
88+
89+
@click.command()
90+
@click.option(
91+
"--path", help="Root directory of source to be checked", required=True, type=str
92+
)
93+
@click.option(
94+
"--fixit", default=False, help="Fix missing header", required=False, type=bool
95+
)
96+
def check_header(path, fixit):
97+
ret = dfs(path)
98+
if len(ret) == 0:
99+
sys.exit(0)
100+
print("Need to add Meta header to the following files.")
101+
print("----------------File List----------------")
102+
for line in ret:
103+
print(line)
104+
print("-----------------------------------------")
105+
if fixit:
106+
fix_header(ret)
107+
sys.exit(1)
108+
109+
110+
if __name__ == "__main__":
111+
check_header()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "flake8",
5+
"severity": "error",
6+
"pattern": [
7+
{
8+
"regexp": "^([^:]+):(\\d+):(\\d+):\\s+(.*)$",
9+
"file": 1,
10+
"line": 2,
11+
"column": 3,
12+
"message": 4
13+
}
14+
]
15+
}
16+
]
17+
}

0 commit comments

Comments
 (0)