mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-09-29 15:31:08 -04:00
support passing a file as argument for check
and quiet ruff
This commit is contained in:
parent
5fed12a0ca
commit
a589785ad2
@ -47,6 +47,10 @@ class Check(Command):
|
|||||||
def add_options(self, parser):
|
def add_options(self, parser):
|
||||||
parser.add_option('--fix', '--auto-fix', default=False, action='store_true',
|
parser.add_option('--fix', '--auto-fix', default=False, action='store_true',
|
||||||
help='Try to automatically fix some of the smallest errors instead of opening an editor for bad files.')
|
help='Try to automatically fix some of the smallest errors instead of opening an editor for bad files.')
|
||||||
|
parser.add_option('-f', '--file', dest='files', type='string', action='append',
|
||||||
|
help='Specific file to be check. Can be repeat to check severals.')
|
||||||
|
parser.add_option('--no-editor', default=False, action='store_true',
|
||||||
|
help="Don't open the editor when a bad file is found.")
|
||||||
|
|
||||||
def get_files(self):
|
def get_files(self):
|
||||||
yield from checkable_python_files(self.SRC)
|
yield from checkable_python_files(self.SRC)
|
||||||
@ -83,9 +87,9 @@ class Check(Command):
|
|||||||
ext = os.path.splitext(f)[1]
|
ext = os.path.splitext(f)[1]
|
||||||
if ext in {'.py', '.recipe'}:
|
if ext in {'.py', '.recipe'}:
|
||||||
if self.auto_fix:
|
if self.auto_fix:
|
||||||
p = subprocess.Popen(['ruff', 'check', '--fix', f])
|
p = subprocess.Popen(['ruff', 'check', '-q', '--fix', f])
|
||||||
else:
|
else:
|
||||||
p = subprocess.Popen(['ruff', 'check', f])
|
p = subprocess.Popen(['ruff', 'check', '-q', f])
|
||||||
return p.wait() != 0
|
return p.wait() != 0
|
||||||
if ext == '.pyj':
|
if ext == '.pyj':
|
||||||
p = subprocess.Popen(['rapydscript', 'lint', f])
|
p = subprocess.Popen(['rapydscript', 'lint', f])
|
||||||
@ -99,6 +103,8 @@ class Check(Command):
|
|||||||
self.wn_path = os.path.expanduser('~/work/srv/main/static')
|
self.wn_path = os.path.expanduser('~/work/srv/main/static')
|
||||||
self.has_changelog_check = os.path.exists(self.wn_path)
|
self.has_changelog_check = os.path.exists(self.wn_path)
|
||||||
self.auto_fix = opts.fix
|
self.auto_fix = opts.fix
|
||||||
|
self.files = opts.files
|
||||||
|
self.no_editor = opts.no_editor
|
||||||
self.run_check_files()
|
self.run_check_files()
|
||||||
|
|
||||||
def run_check_files(self):
|
def run_check_files(self):
|
||||||
@ -109,18 +115,24 @@ class Check(Command):
|
|||||||
except OSError as err:
|
except OSError as err:
|
||||||
if err.errno != errno.ENOENT:
|
if err.errno != errno.ENOENT:
|
||||||
raise
|
raise
|
||||||
|
if self.files:
|
||||||
|
dirty_files = tuple(self.files)
|
||||||
|
else:
|
||||||
dirty_files = tuple(f for f in self.get_files() if not self.is_cache_valid(f, cache))
|
dirty_files = tuple(f for f in self.get_files() if not self.is_cache_valid(f, cache))
|
||||||
try:
|
try:
|
||||||
for i, f in enumerate(dirty_files):
|
for i, f in enumerate(dirty_files):
|
||||||
self.info('\tChecking', f)
|
self.info('\tChecking', f)
|
||||||
if self.file_has_errors(f):
|
if self.file_has_errors(f):
|
||||||
self.info(f'{len(dirty_files) - i - 1} files left to check')
|
self.info(f'{len(dirty_files) - i - 1} files left to check')
|
||||||
|
e = SystemExit(1)
|
||||||
|
if self.no_editor:
|
||||||
|
raise e
|
||||||
try:
|
try:
|
||||||
edit_file(f)
|
edit_file(f)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass # continue if the configured editor fail to be open
|
raise e # raise immediately to skip second check
|
||||||
if self.file_has_errors(f):
|
if self.file_has_errors(f):
|
||||||
raise SystemExit(1)
|
raise e
|
||||||
cache[f] = self.file_hash(f)
|
cache[f] = self.file_hash(f)
|
||||||
finally:
|
finally:
|
||||||
self.save_cache(cache)
|
self.save_cache(cache)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user