Introduce Black to automate code formatting

This commit is contained in:
Ivan Kravets
2019-09-23 23:13:48 +03:00
parent 5e144a2c98
commit 7c41c7c2f3
90 changed files with 4064 additions and 3367 deletions

View File

@@ -28,39 +28,50 @@ from platformio.commands.check.defect import DefectItem
from platformio.commands.check.tools import CheckToolFactory
from platformio.compat import dump_json_to_unicode
from platformio.project.config import ProjectConfig
from platformio.project.helpers import (find_project_dir_above,
get_project_dir,
get_project_include_dir,
get_project_src_dir)
from platformio.project.helpers import (
find_project_dir_above,
get_project_dir,
get_project_include_dir,
get_project_src_dir,
)
@click.command("check", short_help="Run a static analysis tool on code")
@click.option("-e", "--environment", multiple=True)
@click.option("-d",
"--project-dir",
default=os.getcwd,
type=click.Path(exists=True,
file_okay=True,
dir_okay=True,
writable=True,
resolve_path=True))
@click.option("-c",
"--project-conf",
type=click.Path(exists=True,
file_okay=True,
dir_okay=False,
readable=True,
resolve_path=True))
@click.option(
"-d",
"--project-dir",
default=os.getcwd,
type=click.Path(
exists=True, file_okay=True, dir_okay=True, writable=True, resolve_path=True
),
)
@click.option(
"-c",
"--project-conf",
type=click.Path(
exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True
),
)
@click.option("--filter", multiple=True, help="Pattern: +<include> -<exclude>")
@click.option("--flags", multiple=True)
@click.option("--severity",
multiple=True,
type=click.Choice(DefectItem.SEVERITY_LABELS.values()))
@click.option(
"--severity", multiple=True, type=click.Choice(DefectItem.SEVERITY_LABELS.values())
)
@click.option("-s", "--silent", is_flag=True)
@click.option("-v", "--verbose", is_flag=True)
@click.option("--json-output", is_flag=True)
def cli(environment, project_dir, project_conf, filter, flags, severity,
silent, verbose, json_output):
def cli(
environment,
project_dir,
project_conf,
filter,
flags,
severity,
silent,
verbose,
json_output,
):
# find project directory on upper level
if isfile(project_dir):
project_dir = find_project_dir_above(project_dir)
@@ -68,15 +79,18 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
results = []
with fs.cd(project_dir):
config = ProjectConfig.get_instance(
project_conf or join(project_dir, "platformio.ini"))
project_conf or join(project_dir, "platformio.ini")
)
config.validate(environment)
default_envs = config.default_envs()
for envname in config.envs():
skipenv = any([
environment and envname not in environment, not environment
and default_envs and envname not in default_envs
])
skipenv = any(
[
environment and envname not in environment,
not environment and default_envs and envname not in default_envs,
]
)
env_options = config.items(env=envname, as_dict=True)
env_dump = []
@@ -84,7 +98,8 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
if k not in ("platform", "framework", "board"):
continue
env_dump.append(
"%s: %s" % (k, ", ".join(v) if isinstance(v, list) else v))
"%s: %s" % (k, ", ".join(v) if isinstance(v, list) else v)
)
default_filter = [
"+<%s/>" % basename(d)
@@ -94,13 +109,12 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
tool_options = dict(
verbose=verbose,
silent=silent,
filter=filter
or env_options.get("check_filter", default_filter),
filter=filter or env_options.get("check_filter", default_filter),
flags=flags or env_options.get("check_flags"),
severity=[
DefectItem.SEVERITY_LABELS[DefectItem.SEVERITY_HIGH]
] if silent else
(severity or env_options.get("check_severity")))
severity=[DefectItem.SEVERITY_LABELS[DefectItem.SEVERITY_HIGH]]
if silent
else (severity or env_options.get("check_severity")),
)
for tool in env_options.get("check_tool", ["cppcheck"]):
if skipenv:
@@ -109,26 +123,29 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
if not silent and not json_output:
print_processing_header(tool, envname, env_dump)
ct = CheckToolFactory.new(tool, project_dir, config, envname,
tool_options)
ct = CheckToolFactory.new(
tool, project_dir, config, envname, tool_options
)
result = {"env": envname, "tool": tool, "duration": time()}
rc = ct.check(on_defect_callback=None if (
json_output or verbose
) else lambda defect: click.echo(repr(defect)))
rc = ct.check(
on_defect_callback=None
if (json_output or verbose)
else lambda defect: click.echo(repr(defect))
)
result['defects'] = ct.get_defects()
result['duration'] = time() - result['duration']
result['succeeded'] = (
rc == 0 and not any(d.severity == DefectItem.SEVERITY_HIGH
for d in result['defects']))
result["defects"] = ct.get_defects()
result["duration"] = time() - result["duration"]
result["succeeded"] = rc == 0 and not any(
d.severity == DefectItem.SEVERITY_HIGH for d in result["defects"]
)
results.append(result)
if verbose:
click.echo("\n".join(repr(d) for d in result['defects']))
click.echo("\n".join(repr(d) for d in result["defects"]))
if not json_output and not silent:
if not result['defects']:
if not result["defects"]:
click.echo("No defects found")
print_processing_footer(result)
@@ -145,11 +162,13 @@ def cli(environment, project_dir, project_conf, filter, flags, severity,
def results_to_json(raw):
results = []
for item in raw:
item.update({
"ignored": item.get("succeeded") is None,
"succeeded": bool(item.get("succeeded")),
"defects": [d.to_json() for d in item.get("defects", [])]
})
item.update(
{
"ignored": item.get("succeeded") is None,
"succeeded": bool(item.get("succeeded")),
"defects": [d.to_json() for d in item.get("defects", [])],
}
)
results.append(item)
return results
@@ -157,8 +176,9 @@ def results_to_json(raw):
def print_processing_header(tool, envname, envdump):
click.echo(
"Checking %s > %s (%s)" %
(click.style(envname, fg="cyan", bold=True), tool, "; ".join(envdump)))
"Checking %s > %s (%s)"
% (click.style(envname, fg="cyan", bold=True), tool, "; ".join(envdump))
)
terminal_width, _ = click.get_terminal_size()
click.secho("-" * terminal_width, bold=True)
@@ -166,10 +186,17 @@ def print_processing_header(tool, envname, envdump):
def print_processing_footer(result):
is_failed = not result.get("succeeded")
util.print_labeled_bar(
"[%s] Took %.2f seconds" %
((click.style("FAILED", fg="red", bold=True) if is_failed else
click.style("PASSED", fg="green", bold=True)), result['duration']),
is_error=is_failed)
"[%s] Took %.2f seconds"
% (
(
click.style("FAILED", fg="red", bold=True)
if is_failed
else click.style("PASSED", fg="green", bold=True)
),
result["duration"],
),
is_error=is_failed,
)
def print_defects_stats(results):
@@ -178,8 +205,7 @@ def print_defects_stats(results):
def _append_defect(component, defect):
if not components.get(component):
components[component] = Counter()
components[component].update(
{DefectItem.SEVERITY_LABELS[defect.severity]: 1})
components[component].update({DefectItem.SEVERITY_LABELS[defect.severity]: 1})
for result in results:
for defect in result.get("defects", []):
@@ -235,20 +261,32 @@ def print_check_summary(results):
status_str = click.style("PASSED", fg="green")
tabular_data.append(
(click.style(result['env'], fg="cyan"), result['tool'], status_str,
util.humanize_duration_time(result.get("duration"))))
(
click.style(result["env"], fg="cyan"),
result["tool"],
status_str,
util.humanize_duration_time(result.get("duration")),
)
)
click.echo(tabulate(tabular_data,
headers=[
click.style(s, bold=True)
for s in ("Environment", "Tool", "Status",
"Duration")
]),
err=failed_nums)
click.echo(
tabulate(
tabular_data,
headers=[
click.style(s, bold=True)
for s in ("Environment", "Tool", "Status", "Duration")
],
),
err=failed_nums,
)
util.print_labeled_bar(
"%s%d succeeded in %s" %
("%d failed, " % failed_nums if failed_nums else "", succeeded_nums,
util.humanize_duration_time(duration)),
"%s%d succeeded in %s"
% (
"%d failed, " % failed_nums if failed_nums else "",
succeeded_nums,
util.humanize_duration_time(duration),
),
is_error=failed_nums,
fg="red" if failed_nums else "green")
fg="red" if failed_nums else "green",
)

View File

@@ -29,18 +29,19 @@ class DefectItem(object):
SEVERITY_LOW = 4
SEVERITY_LABELS = {4: "low", 2: "medium", 1: "high"}
def __init__(self,
severity,
category,
message,
file="unknown",
line=0,
column=0,
id=None,
callstack=None,
cwe=None):
assert severity in (self.SEVERITY_HIGH, self.SEVERITY_MEDIUM,
self.SEVERITY_LOW)
def __init__(
self,
severity,
category,
message,
file="unknown",
line=0,
column=0,
id=None,
callstack=None,
cwe=None,
):
assert severity in (self.SEVERITY_HIGH, self.SEVERITY_MEDIUM, self.SEVERITY_LOW)
self.severity = severity
self.category = category
self.message = message
@@ -61,14 +62,14 @@ class DefectItem(object):
defect_color = "yellow"
format_str = "{file}:{line}: [{severity}:{category}] {message} {id}"
return format_str.format(severity=click.style(
self.SEVERITY_LABELS[self.severity], fg=defect_color),
category=click.style(self.category.lower(),
fg=defect_color),
file=click.style(self.file, bold=True),
message=self.message,
line=self.line,
id="%s" % "[%s]" % self.id if self.id else "")
return format_str.format(
severity=click.style(self.SEVERITY_LABELS[self.severity], fg=defect_color),
category=click.style(self.category.lower(), fg=defect_color),
file=click.style(self.file, bold=True),
message=self.message,
line=self.line,
id="%s" % "[%s]" % self.id if self.id else "",
)
def __or__(self, defect):
return self.severity | defect.severity
@@ -90,5 +91,5 @@ class DefectItem(object):
"column": self.column,
"callstack": self.callstack,
"id": self.id,
"cwe": self.cwe
"cwe": self.cwe,
}

View File

@@ -18,7 +18,6 @@ from platformio.commands.check.tools.cppcheck import CppcheckCheckTool
class CheckToolFactory(object):
@staticmethod
def new(tool, project_dir, config, envname, options):
cls = None
@@ -27,6 +26,5 @@ class CheckToolFactory(object):
elif tool == "clangtidy":
cls = ClangtidyCheckTool
else:
raise exception.PlatformioException("Unknown check tool `%s`" %
tool)
raise exception.PlatformioException("Unknown check tool `%s`" % tool)
return cls(project_dir, config, envname, options)

View File

@@ -20,7 +20,6 @@ from platformio.project.helpers import get_project_dir, load_project_ide_data
class CheckToolBase(object): # pylint: disable=too-many-instance-attributes
def __init__(self, project_dir, config, envname, options):
self.config = config
self.envname = envname
@@ -35,14 +34,15 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes
# detect all defects by default
if not self.options.get("severity"):
self.options['severity'] = [
DefectItem.SEVERITY_LOW, DefectItem.SEVERITY_MEDIUM,
DefectItem.SEVERITY_HIGH
self.options["severity"] = [
DefectItem.SEVERITY_LOW,
DefectItem.SEVERITY_MEDIUM,
DefectItem.SEVERITY_HIGH,
]
# cast to severity by ids
self.options['severity'] = [
self.options["severity"] = [
s if isinstance(s, int) else DefectItem.severity_to_int(s)
for s in self.options['severity']
for s in self.options["severity"]
]
def _load_cpp_data(self, project_dir, envname):
@@ -51,8 +51,7 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes
return
self.cpp_includes = data.get("includes", [])
self.cpp_defines = data.get("defines", [])
self.cpp_defines.extend(
self._get_toolchain_defines(data.get("cc_path")))
self.cpp_defines.extend(self._get_toolchain_defines(data.get("cc_path")))
def get_flags(self, tool):
result = []
@@ -61,18 +60,16 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes
if ":" not in flag:
result.extend([f for f in flag.split(" ") if f])
elif flag.startswith("%s:" % tool):
result.extend(
[f for f in flag.split(":", 1)[1].split(" ") if f])
result.extend([f for f in flag.split(":", 1)[1].split(" ") if f])
return result
@staticmethod
def _get_toolchain_defines(cc_path):
defines = []
result = proc.exec_command("echo | %s -dM -E -x c++ -" % cc_path,
shell=True)
result = proc.exec_command("echo | %s -dM -E -x c++ -" % cc_path, shell=True)
for line in result['out'].split("\n"):
for line in result["out"].split("\n"):
tokens = line.strip().split(" ", 2)
if not tokens or tokens[0] != "#define":
continue
@@ -105,7 +102,7 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes
click.echo(line)
return
if defect.severity not in self.options['severity']:
if defect.severity not in self.options["severity"]:
return
self._defects.append(defect)
@@ -125,8 +122,9 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes
def get_project_src_files(self):
file_extensions = ["h", "hpp", "c", "cc", "cpp", "ino"]
return fs.match_src_files(get_project_dir(),
self.options.get("filter"), file_extensions)
return fs.match_src_files(
get_project_dir(), self.options.get("filter"), file_extensions
)
def check(self, on_defect_callback=None):
self._on_defect_callback = on_defect_callback
@@ -137,7 +135,8 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes
proc.exec_command(
cmd,
stdout=proc.LineBufferedAsyncPipe(self.on_tool_output),
stderr=proc.LineBufferedAsyncPipe(self.on_tool_output))
stderr=proc.LineBufferedAsyncPipe(self.on_tool_output),
)
self.clean_up()

View File

@@ -21,10 +21,8 @@ from platformio.managers.core import get_core_package_dir
class ClangtidyCheckTool(CheckToolBase):
def tool_output_filter(self, line):
if not self.options.get(
"verbose") and "[clang-diagnostic-error]" in line:
if not self.options.get("verbose") and "[clang-diagnostic-error]" in line:
return ""
if "[CommonOptionsParser]" in line:
@@ -37,8 +35,7 @@ class ClangtidyCheckTool(CheckToolBase):
return ""
def parse_defect(self, raw_line):
match = re.match(r"^(.*):(\d+):(\d+):\s+([^:]+):\s(.+)\[([^]]+)\]$",
raw_line)
match = re.match(r"^(.*):(\d+):(\d+):\s+([^:]+):\s(.+)\[([^]]+)\]$", raw_line)
if not match:
return raw_line
@@ -50,8 +47,7 @@ class ClangtidyCheckTool(CheckToolBase):
elif category == "warning":
severity = DefectItem.SEVERITY_MEDIUM
return DefectItem(severity, category, message, file_, line, column,
defect_id)
return DefectItem(severity, category, message, file_, line, column, defect_id)
def configure_command(self):
tool_path = join(get_core_package_dir("tool-clangtidy"), "clang-tidy")

View File

@@ -23,29 +23,42 @@ from platformio.project.helpers import get_project_core_dir
class CppcheckCheckTool(CheckToolBase):
def __init__(self, *args, **kwargs):
self._tmp_files = []
self.defect_fields = [
"severity", "message", "file", "line", "column", "callstack",
"cwe", "id"
"severity",
"message",
"file",
"line",
"column",
"callstack",
"cwe",
"id",
]
super(CppcheckCheckTool, self).__init__(*args, **kwargs)
def tool_output_filter(self, line):
if not self.options.get(
"verbose") and "--suppress=unmatchedSuppression:" in line:
if (
not self.options.get("verbose")
and "--suppress=unmatchedSuppression:" in line
):
return ""
if any(msg in line for msg in ("No C or C++ source files found",
"unrecognized command line option")):
if any(
msg in line
for msg in (
"No C or C++ source files found",
"unrecognized command line option",
)
):
self._bad_input = True
return line
def parse_defect(self, raw_line):
if "<&PIO&>" not in raw_line or any(f not in raw_line
for f in self.defect_fields):
if "<&PIO&>" not in raw_line or any(
f not in raw_line for f in self.defect_fields
):
return None
args = dict()
@@ -54,13 +67,13 @@ class CppcheckCheckTool(CheckToolBase):
name, value = field.split("=", 1)
args[name] = value
args['category'] = args['severity']
if args['severity'] == "error":
args['severity'] = DefectItem.SEVERITY_HIGH
elif args['severity'] == "warning":
args['severity'] = DefectItem.SEVERITY_MEDIUM
args["category"] = args["severity"]
if args["severity"] == "error":
args["severity"] = DefectItem.SEVERITY_HIGH
elif args["severity"] == "warning":
args["severity"] = DefectItem.SEVERITY_MEDIUM
else:
args['severity'] = DefectItem.SEVERITY_LOW
args["severity"] = DefectItem.SEVERITY_LOW
return DefectItem(**args)
@@ -68,20 +81,26 @@ class CppcheckCheckTool(CheckToolBase):
tool_path = join(get_core_package_dir("tool-cppcheck"), "cppcheck")
cmd = [
tool_path, "--error-exitcode=1",
"--verbose" if self.options.get("verbose") else "--quiet"
tool_path,
"--error-exitcode=1",
"--verbose" if self.options.get("verbose") else "--quiet",
]
cmd.append('--template="%s"' % "<&PIO&>".join(
["{0}={{{0}}}".format(f) for f in self.defect_fields]))
cmd.append(
'--template="%s"'
% "<&PIO&>".join(["{0}={{{0}}}".format(f) for f in self.defect_fields])
)
flags = self.get_flags("cppcheck")
if not self.is_flag_set("--platform", flags):
cmd.append("--platform=unspecified")
if not self.is_flag_set("--enable", flags):
enabled_checks = [
"warning", "style", "performance", "portability",
"unusedFunction"
"warning",
"style",
"performance",
"portability",
"unusedFunction",
]
cmd.append("--enable=%s" % ",".join(enabled_checks))