From e2156ec23ddfdf6c6f91e025c50ded2409c460d7 Mon Sep 17 00:00:00 2001 From: un-pogaz <46523284+un-pogaz@users.noreply.github.com> Date: Sun, 15 Dec 2024 13:17:48 +0100 Subject: [PATCH] add option --fix to 'setup.py check' Try to automatically fix some of the smallest errors --- setup/check.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/setup/check.py b/setup/check.py index eb660591b1..3b75c790bb 100644 --- a/setup/check.py +++ b/setup/check.py @@ -41,6 +41,10 @@ class Check(Command): CACHE = 'check.json' + def add_options(self, parser): + parser.add_option('--fix', '--auto-fix', default=False, action='store_true', + help='Try to automatically fix some of the smallest errors') + def get_files(self): yield from checkable_python_files(self.SRC) @@ -91,6 +95,10 @@ class Check(Command): p = subprocess.Popen(['python', self.j(self.wn_path, 'whats_new.py'), f]) return p.wait() != 0 + def perform_auto_fix(self): + p = subprocess.Popen(['ruff', 'check', '--fix-only'], text=True, stdout=subprocess.PIPE) + return p.stdout.read() + def run(self, opts): self.fhash_cache = {} cache = {} @@ -102,6 +110,10 @@ class Check(Command): except OSError as err: if err.errno != errno.ENOENT: raise + if opts.fix: + self.info('\tAuto-fixing') + msg = self.perform_auto_fix() + self.info(msg) dirty_files = tuple(f for f in self.get_files() if not self.is_cache_valid(f, cache)) try: for i, f in enumerate(dirty_files):