mirror of
https://github.com/mandiant/capa.git
synced 2025-12-23 07:28:34 -08:00
lint: handle calls to print within pbar
This commit is contained in:
@@ -19,12 +19,15 @@ import time
|
|||||||
import string
|
import string
|
||||||
import difflib
|
import difflib
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
import argparse
|
import argparse
|
||||||
import itertools
|
import itertools
|
||||||
import posixpath
|
import posixpath
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
import tqdm
|
||||||
import termcolor
|
import termcolor
|
||||||
import ruamel.yaml
|
import ruamel.yaml
|
||||||
import tqdm.contrib.logging
|
import tqdm.contrib.logging
|
||||||
@@ -686,6 +689,41 @@ def lint_rule(ctx, rule):
|
|||||||
return (lints_failed, lints_warned)
|
return (lints_failed, lints_warned)
|
||||||
|
|
||||||
|
|
||||||
|
def width(s, count):
|
||||||
|
if len(s) > count:
|
||||||
|
return s[: count - 3] + "..."
|
||||||
|
else:
|
||||||
|
return s.ljust(count)
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def redirecting_print_to_tqdm():
|
||||||
|
"""
|
||||||
|
tqdm (progress bar) expects to have fairly tight control over console output.
|
||||||
|
so calls to `print()` will break the progress bar and make things look bad.
|
||||||
|
so, this context manager temporarily replaces the `print` implementation
|
||||||
|
with one that is compatible with tqdm.
|
||||||
|
|
||||||
|
via: https://stackoverflow.com/a/42424890/87207
|
||||||
|
"""
|
||||||
|
old_print = print
|
||||||
|
|
||||||
|
def new_print(*args, **kwargs):
|
||||||
|
|
||||||
|
# If tqdm.tqdm.write raises error, use builtin print
|
||||||
|
try:
|
||||||
|
tqdm.tqdm.write(*args, **kwargs)
|
||||||
|
except:
|
||||||
|
old_print(*args, **kwargs)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Globaly replace print with new_print
|
||||||
|
inspect.builtins.print = new_print
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
inspect.builtins.print = old_print
|
||||||
|
|
||||||
|
|
||||||
def lint(ctx, rules):
|
def lint(ctx, rules):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
@@ -701,11 +739,12 @@ def lint(ctx, rules):
|
|||||||
ret = {}
|
ret = {}
|
||||||
|
|
||||||
with tqdm.contrib.logging.tqdm_logging_redirect(rules.rules.items(), unit="rule") as pbar:
|
with tqdm.contrib.logging.tqdm_logging_redirect(rules.rules.items(), unit="rule") as pbar:
|
||||||
|
with redirecting_print_to_tqdm():
|
||||||
for name, rule in pbar:
|
for name, rule in pbar:
|
||||||
if rule.meta.get("capa/subscope-rule", False):
|
if rule.meta.get("capa/subscope-rule", False):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
pbar.set_description("linting rule: %s" % (name))
|
pbar.set_description(width("linting rule: %s" % (name), 48))
|
||||||
ret[name] = lint_rule(ctx, rule)
|
ret[name] = lint_rule(ctx, rule)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|||||||
Reference in New Issue
Block a user