diff --git a/.ci/requirements.txt b/.ci/requirements.txt index d2d06d446ee338e2332147b19a76dd1124706355..2955c65e20383cae48c371c1a67b2c4009c94a6c 100644 --- a/.ci/requirements.txt +++ b/.ci/requirements.txt @@ -10,3 +10,4 @@ Sphinx==3.0.3 xunitparser==1.3.3 asttokens virtualenv +flake8-import-order diff --git a/tools/hooks/run-flake8.py b/tools/hooks/run-flake8.py index bdb08fbdaf15889397a3002b10a1537cfda12b7c..54bf4ab228ca11a8deb5a1a0987f27829dee1b39 100755 --- a/tools/hooks/run-flake8.py +++ b/tools/hooks/run-flake8.py @@ -2,12 +2,39 @@ from pathlib import Path import runpy -import subprocess import sys +from types import ModuleType + +from flake8.main.cli import main +from flake8_import_order.styles import PEP8 +import pkg_resources mod = runpy.run_path(str(Path(__file__).with_name('checkerlib.py'))) +args = mod['parser'].parse_args() + + +# flake8-import-order's PEP8 style merges app imports and relative imports +class AllSeparateStyle(PEP8): + @staticmethod + def same_section(previous, current): + return current.type == previous.type + + +# flake8-import-order is almost only configurable through entry_points +# ugly hack to avoid having to create a separate package with entry_point, setup, etc. +style_mod = ModuleType('fake_module_all_separate') +style_mod.AllSeparateStyle = AllSeparateStyle +sys.modules['fake_module_all_separate'] = style_mod + +d = pkg_resources.Distribution('/lol.py') +ep = pkg_resources.EntryPoint.parse('fake_module_all_separate = fake_module_all_separate:AllSeparateStyle') +ep.dist = d +d._ep_map = {'flake8_import_order.styles': {'fake_module_all_separate': ep}} +pkg_resources.working_set.add(d, 'fake_module_all_separate') + + # E501: Line too long # Disabled because it doesn't allow exceptions, for example URLs or log # messages shouldn't be split, less readable or searchable. @@ -15,11 +42,9 @@ # Disabling it follows pep8 (see W504). # E266: Too many leading '#' for block comment # But it's a nice visual separator sometimes. - -args = mod['parser'].parse_args() - -execution = subprocess.run([ - 'flake8', '--ignore=E501,W503,E266', +main([ + '--ignore=E501,W503,E266', + '--application-import-names=weboob', + '--import-order-style=fake_module_all_separate', *map(str, mod['files_to_check'](args)), ]) -sys.exit(execution.returncode) diff --git a/tools/pyflakes-strict.sh b/tools/pyflakes-strict.sh index 13390910166eb7858758154944fbb6147ab32c52..9f0628fce98ca7a394ff5cf9cedaf5edd63ea7aa 100755 --- a/tools/pyflakes-strict.sh +++ b/tools/pyflakes-strict.sh @@ -10,6 +10,7 @@ die () { } $PYTHON3 -c 'import flake8' || die "Please install flake8 (e.g. apt install flake8)" +$PYTHON3 -c 'import flake8_import_order' || die "Please install flake8-import-order (e.g. pip3 install flake8-import-order)" $PYTHON3 -c 'import bugbear' || die "Please install flake8-bugbear (e.g. pip3 install flake8-bugbear)" $PYTHON3 -c 'import asttokens' || die "Please install asttokens (e.g. apt install python3-asttokens)"