Skip to content

Commit 188b279

Browse files
Code formatting and checks are moved to a single module. (#548)
* Code formatting and checks are moved to a single module. * Added missing options in setup.cfg flake8. * Typo correction. * Excluding `scripts/` on setup.py. * setup.cfg restored. - Typo correction in README.md.
1 parent d392987 commit 188b279

File tree

7 files changed

+67
-11
lines changed

7 files changed

+67
-11
lines changed

.github/workflows/ci.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ jobs:
5454
run: |
5555
pip install -e .[dev]
5656
# stop the build if there are Python syntax errors or undefined names
57-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
58-
black . --check --config pyproject.toml
59-
isort -c .
57+
python scripts/run_code_style.py check
58+
6059
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
61-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
60+
flake8 . --exit-zero --max-complexity=10 --max-line-length=127
6261
6362
- name: Install core dependencies
6463
run: >

.github/workflows/ci_torch1.10.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ jobs:
5555
run: |
5656
pip install -e .[dev]
5757
# stop the build if there are Python syntax errors or undefined names
58-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
59-
black . --check --config pyproject.toml
60-
isort -c .
58+
python scripts/run_code_style.py check
59+
6160
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
62-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
61+
flake8 . --exit-zero --max-complexity=10 --max-line-length=127
6362
6463
- name: Install core dependencies
6564
run: >

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ pip install -e ."[dev]"
210210
- Reformat with black and isort:
211211

212212
```bash
213-
black . --config pyproject.toml
214-
isort .
213+
python scripts/run_code_style.py format
215214
```
216215

217216
## <div align="center">Contributors</div>

scripts/run_code_style.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
3+
from scripts.utils import shell, validate_and_exit
4+
5+
if __name__ == "__main__":
6+
arg = sys.argv[1]
7+
8+
if arg == "check":
9+
sts_flake = shell("flake8 . --config setup.cfg --select=E9,F63,F7,F82")
10+
sts_isort = shell("isort . --check --settings pyproject.toml")
11+
sts_black = shell("black . --check --config pyproject.toml")
12+
validate_and_exit(flake8=sts_flake, isort=sts_isort, black=sts_black)
13+
elif arg == "format":
14+
sts_isort = shell("isort . --settings pyproject.toml")
15+
sts_black = shell("black . --config pyproject.toml")
16+
validate_and_exit(isort=sts_isort, black=sts_black)

scripts/utils.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import shutil
3+
import sys
4+
5+
6+
def shell(command, exit_status=0):
7+
"""
8+
Run command through shell and return exit status if exit status of command run match with given exit status.
9+
10+
Args:
11+
command: (str) Command string which runs through system shell.
12+
exit_status: (int) Expected exit status of given command run.
13+
14+
Returns: actual_exit_status
15+
16+
"""
17+
actual_exit_status = os.system(command)
18+
if actual_exit_status == exit_status:
19+
return 0
20+
return actual_exit_status
21+
22+
23+
def validate_and_exit(expected_out_status=0, **kwargs):
24+
if all([arg == expected_out_status for arg in kwargs.values()]):
25+
# Expected status, OK
26+
sys.exit(0)
27+
else:
28+
# Failure
29+
print_console_centered("Summary Results")
30+
fail_count = 0
31+
for component, exit_status in kwargs.items():
32+
if exit_status != expected_out_status:
33+
print(f"{component} failed.")
34+
fail_count += 1
35+
print_console_centered(f"{len(kwargs)-fail_count} success, {fail_count} failure")
36+
sys.exit(1)
37+
38+
39+
def print_console_centered(text: str, fill_char="="):
40+
w, _ = shutil.get_terminal_size((80, 20))
41+
print(f" {text} ".center(w, fill_char))

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ max-complexity = 18
44
exclude =.git,__pycache__,docs/source/conf.py,build,dist
55
ignore = I101,I201,F401,F403,S001,D100,D101,D102,D103,D104,D105,D106,D107,D200,D205,D400,W504,D202,E203,E501,E722,W503,B006
66
inline-quotes = "
7+
statistics = true
8+
count = true
79
810
[mypy]
911
ignore_missing_imports = True

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_version():
3232
long_description=get_long_description(),
3333
long_description_content_type="text/markdown",
3434
url="https://github.com/obss/sahi",
35-
packages=setuptools.find_packages(exclude=["demo", "docs", "resources", "tests"]),
35+
packages=setuptools.find_packages(exclude=["demo", "docs", "resources", "tests", "scripts"]),
3636
python_requires=">=3.6",
3737
install_requires=get_requirements(),
3838
extras_require={

0 commit comments

Comments
 (0)