From 3289e84b210d6730b734100d6f861e942dd29e22 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 4 Nov 2019 18:22:28 +0200 Subject: [PATCH] Refactor PIO Check from "check_filters" to "check_patterns" --- platformio/commands/check/command.py | 18 ++++++--------- platformio/commands/check/tools/base.py | 24 ++++++++++++++++---- platformio/commands/check/tools/clangtidy.py | 2 +- platformio/commands/check/tools/cppcheck.py | 4 ++-- platformio/project/options.py | 6 ++--- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/platformio/commands/check/command.py b/platformio/commands/check/command.py index 0d0b6f27..24575436 100644 --- a/platformio/commands/check/command.py +++ b/platformio/commands/check/command.py @@ -17,7 +17,7 @@ import os from collections import Counter -from os.path import basename, dirname, isfile +from os.path import dirname, isfile from time import time import click @@ -48,7 +48,7 @@ from platformio.project.helpers import find_project_dir_above, get_project_dir exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True ), ) -@click.option("--filter", multiple=True, help="Pattern: + -") +@click.option("--pattern", multiple=True) @click.option("--flags", multiple=True) @click.option( "--severity", multiple=True, type=click.Choice(DefectItem.SEVERITY_LABELS.values()) @@ -65,7 +65,7 @@ def cli( environment, project_dir, project_conf, - filter, + pattern, flags, severity, silent, @@ -102,18 +102,14 @@ def cli( "%s: %s" % (k, ", ".join(v) if isinstance(v, list) else v) ) - default_filter = [ - "+<%s/>" % basename(d) - for d in ( - config.get_optional_dir("src"), - config.get_optional_dir("include"), - ) + default_patterns = [ + config.get_optional_dir("src"), + config.get_optional_dir("include"), ] - tool_options = dict( verbose=verbose, silent=silent, - filter=filter or env_options.get("check_filter", default_filter), + patterns=pattern or env_options.get("check_patterns", default_patterns), flags=flags or env_options.get("check_flags"), severity=[DefectItem.SEVERITY_LABELS[DefectItem.SEVERITY_HIGH]] if silent diff --git a/platformio/commands/check/tools/base.py b/platformio/commands/check/tools/base.py index ae4bf7b5..59951288 100644 --- a/platformio/commands/check/tools/base.py +++ b/platformio/commands/check/tools/base.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import glob import os import click @@ -124,11 +125,24 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes def clean_up(self): pass - 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 - ) + def get_project_target_files(self): + allowed_extensions = (".h", ".hpp", ".c", ".cc", ".cpp", ".ino") + result = [] + + def _add_file(path): + if not path.endswith(allowed_extensions): + return + result.append(os.path.abspath(path)) + + for pattern in self.options["patterns"]: + for item in glob.glob(pattern): + if not os.path.isdir(item): + _add_file(item) + for root, _, files in os.walk(item, followlinks=True): + for f in files: + _add_file(os.path.join(root, f)) + + return result def get_source_language(self): with fs.cd(get_project_dir()): diff --git a/platformio/commands/check/tools/clangtidy.py b/platformio/commands/check/tools/clangtidy.py index 20b4a5de..b7845f8b 100644 --- a/platformio/commands/check/tools/clangtidy.py +++ b/platformio/commands/check/tools/clangtidy.py @@ -58,7 +58,7 @@ class ClangtidyCheckTool(CheckToolBase): cmd.append("--checks=*") cmd.extend(flags) - cmd.extend(self.get_project_src_files()) + cmd.extend(self.get_project_target_files()) cmd.append("--") cmd.extend(["-D%s" % d for d in self.cpp_defines]) diff --git a/platformio/commands/check/tools/cppcheck.py b/platformio/commands/check/tools/cppcheck.py index 20a28ec5..3d92bc56 100644 --- a/platformio/commands/check/tools/cppcheck.py +++ b/platformio/commands/check/tools/cppcheck.py @@ -137,7 +137,7 @@ class CppcheckCheckTool(CheckToolBase): def _generate_src_file(self): src_files = [ - f for f in self.get_project_src_files() if not f.endswith((".h", ".hpp")) + f for f in self.get_project_target_files() if not f.endswith((".h", ".hpp")) ] return self._create_tmp_file("\n".join(src_files)) @@ -152,7 +152,7 @@ class CppcheckCheckTool(CheckToolBase): # delete temporary dump files generated by addons if not self.is_flag_set("--addon", self.get_flags("cppcheck")): return - for f in self.get_project_src_files(): + for f in self.get_project_target_files(): dump_file = f + ".dump" if isfile(dump_file): remove(dump_file) diff --git a/platformio/project/options.py b/platformio/project/options.py index 58149df9..b4c1814f 100644 --- a/platformio/project/options.py +++ b/platformio/project/options.py @@ -537,10 +537,10 @@ ProjectOptions = OrderedDict( ), ConfigEnvOption( group="check", - name="check_filter", + name="check_patterns", description=( - "Configure a list of source files which should be " - "included/excluded from a check process" + "Configure a list of target files or directories for checking " + "(Unix shell-style wildcards)" ), multiple=True, ),