104 lines
3.0 KiB
Python
Executable File
104 lines
3.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
import subprocess
|
|
import os
|
|
from os.path import join, splitext
|
|
|
|
from autopep8_clean_config import PATHS, BLACKLIST
|
|
|
|
# Useful to disable when debugging warnings.
|
|
USE_MULTIPROCESS = True
|
|
|
|
print(PATHS)
|
|
SOURCE_EXT = (
|
|
# Python
|
|
".py",
|
|
)
|
|
|
|
|
|
def is_source_and_included(filename):
|
|
return (
|
|
filename.endswith(SOURCE_EXT) and
|
|
filename not in BLACKLIST
|
|
)
|
|
|
|
|
|
def path_iter(path, filename_check=None):
|
|
for dirpath, dirnames, filenames in os.walk(path):
|
|
# skip ".git"
|
|
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
|
|
|
|
for filename in filenames:
|
|
if filename.startswith("."):
|
|
continue
|
|
filepath = join(dirpath, filename)
|
|
if filename_check is None or filename_check(filepath):
|
|
yield filepath
|
|
|
|
|
|
def path_expand(paths, filename_check=None):
|
|
for f in paths:
|
|
if not os.path.exists(f):
|
|
print("Missing:", f)
|
|
elif os.path.isdir(f):
|
|
yield from path_iter(f, filename_check)
|
|
else:
|
|
yield f
|
|
|
|
|
|
def autopep8_format_file(f):
|
|
print(f)
|
|
subprocess.call((
|
|
"autopep8",
|
|
"--ignore",
|
|
",".join((
|
|
# Info: Use `isinstance()` instead of comparing types directly.
|
|
# Why disable?: Changes code logic, in rare cases we want to compare exact types.
|
|
"E721",
|
|
# Info: Fix bare except.
|
|
# Why disable?: Disruptive, leave our exceptions alone.
|
|
"E722",
|
|
# Info: Fix module level import not at top of file.
|
|
# Why disable?: re-ordering imports is disruptive and breaks some scripts
|
|
# that need to check if a module has already been loaded in the case of reloading.
|
|
"E402",
|
|
# Info: Try to make lines fit within --max-line-length characters.
|
|
# Why disable? Causes lines to be wrapped, where long lines have the
|
|
# trailing bracket moved to the end of the line.
|
|
# If trailing commas were respected as they are by clang-format this might be acceptable.
|
|
# Note that this doesn't disable all line wrapping.
|
|
"E501",
|
|
# Info: Fix various deprecated code (via lib2to3)
|
|
# Why disable?: causes imports to be added/re-arranged.
|
|
"W690",
|
|
)),
|
|
"--aggressive",
|
|
"--in-place",
|
|
"--max-line-length", "120",
|
|
f,
|
|
))
|
|
|
|
|
|
def main():
|
|
import sys
|
|
import subprocess
|
|
|
|
if os.path.samefile(sys.argv[-1], __file__):
|
|
paths = path_expand(PATHS, is_source_and_included)
|
|
else:
|
|
paths = path_expand(sys.argv[1:], is_source_and_included)
|
|
|
|
if USE_MULTIPROCESS:
|
|
import multiprocessing
|
|
job_total = multiprocessing.cpu_count()
|
|
pool = multiprocessing.Pool(processes=job_total * 2)
|
|
pool.map(autopep8_format_file, paths)
|
|
else:
|
|
for f in paths:
|
|
autopep8_format_file(f)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|