From 46c1ff9d144dae3da0010920019333bec73befa6 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 6 Oct 2021 16:08:08 +0200 Subject: [PATCH 01/28] imlement yaml linter hook --- hooks/yaml_linter.py | 41 +++++++++++++++ tests/test_hooks/test_yaml_linter.py | 74 ++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 hooks/yaml_linter.py create mode 100644 tests/test_hooks/test_yaml_linter.py diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py new file mode 100644 index 00000000..e3fafe49 --- /dev/null +++ b/hooks/yaml_linter.py @@ -0,0 +1,41 @@ +# coding=utf-8 + +import os +import platform +import subprocess + +from conans.errors import ConanException +from conans.tools import logger + + +CONAN_HOOK_YAMLLINT_WERR = "CONAN_YAMLLINT_WERR" + + +def pre_export(output, conanfile_path, *args, **kwargs): + output.info("Lint yaml '{}'".format(conanfile_path)) + conanfile_dirname = os.path.dirname(conanfile_path) + conandata_file = os.path.join(conanfile_dirname, 'conadata.yaml') + + lint_args = ['-f', 'parsable', + '-d', '"{extends: default, rules: {document-start: disable}}"'] + + try: + command = ['yamllint'] + lint_args + [conandata_file.replace('\\', '/')] + command = " ".join(command) + shell = bool(platform.system() != "Windows") + p = subprocess.Popen(command, shell=shell, bufsize=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + yamllint_stdout, yamllint_stderr = p.communicate() + yamllint_stdout = yamllint_stdout.decode('utf-8') + except Exception as exc: + output.error("Unexpected error running linter: {}".format(exc)) + return + errors = 0 + for line in messages.splitlines(): + output.info(line) + parts = line.split(' ') + errors += int(parts[1] == "[error]") + + output.info("YAML Linter detected '{}' errors".format(errors)) + if os.getenv(CONAN_HOOK_YAMLLINT_WERR) and errors: + raise ConanException("Package recipe has YAML linter errors. Please fix them.") \ No newline at end of file diff --git a/tests/test_hooks/test_yaml_linter.py b/tests/test_hooks/test_yaml_linter.py new file mode 100644 index 00000000..c3fe2c39 --- /dev/null +++ b/tests/test_hooks/test_yaml_linter.py @@ -0,0 +1,74 @@ +# coding=utf-8 + +import os +import textwrap +import unittest + +from parameterized import parameterized + +from conans import tools +from conans.client.command import ERROR_GENERAL, SUCCESS +from conans.tools import environment_append +from tests.utils.test_cases.conan_client import ConanClientTestCase + + +class YAMLLinterTests(ConanClientTestCase): + conanfile = textwrap.dedent(r""" + from conans import ConanFile, tools + + class TestConan(ConanFile): + name = "name" + version = "version" + """) + + def _get_environ(self, **kwargs): + kwargs = super(YAMLLinterTests, self)._get_environ(**kwargs) + kwargs.update({'CONAN_HOOKS': os.path.join(os.path.dirname( + __file__), '..', '..', 'hooks', 'yaml_linter')}) + return kwargs + + @parameterized.expand([(False, ), (True, )]) + def test_basic(self, yamllint_werr): + conandatafile = textwrap.dedent(r""" + sources: + "version": + url: "https://url.to/name/version.tar.xz" + sha256: "3a530d1b243b5dec00bc54937455471aaa3e56849d2593edb8ded07228202240" + patches: + "version": + - patch_file: "patches/abcdef.diff" + base_path: "source" + patches: + """) + tools.save('conanfile.py', content=self.conanfile) + tools.save('conandata.yaml', content=self.conandatafile) + yamllint_werr_value = "1" if yamllint_werr else None + with environment_append({"CONAN_YAMLLINT_WERR": yamllint_werr_value}): + return_code = ERROR_GENERAL if yamllint_werr else SUCCESS + output = self.conan(['export', '.', 'name/version@'], expected_return_code=return_code) + + if yamllint_werr: + self.assertIn("pre_export(): Package recipe has linter errors." + " Please fix them.", output) + + self.assertIn("pre_export(): conandata.yaml:9:1:" + " [error] duplication of key \"patches\" in mapping (key-duplicates)", + output) + + def test_path_with_spaces(self): + conandatafile = textwrap.dedent(r""" + sources: + "version": + url: "https://url.to/name/version.tar.xz" + sha256: "3a530d1b243b5dec00bc54937455471aaa3e56849d2593edb8ded07228202240" + patches: + "version": + - patch_file: "patches/abcdef.diff" + base_path: "source" + """) + tools.save(os.path.join("path spaces", "conanfile.py"), content=conanfile) + tools.save(os.path.join("path spaces", "conandata.py"), content=conandatafile) + output = self.conan(['export', 'path spaces/conanfile.py', 'name/version@']) + recipe_path = os.path.join(os.getcwd(), "path spaces", "conanfile.py") + self.assertIn("pre_export(): Lint recipe '{}'".format(recipe_path), output) + self.assertIn("pre_export(): Linter detected '0' errors", output) From c02f3c4475ed6df3b75635529948ae44a2e09f65 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 6 Oct 2021 16:21:34 +0200 Subject: [PATCH 02/28] CI: install yamllint --- .ci/generate_env_windows.bat | 2 +- .ci/requirements_linux.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.ci/generate_env_windows.bat b/.ci/generate_env_windows.bat index be3ba420..22ac131a 100644 --- a/.ci/generate_env_windows.bat +++ b/.ci/generate_env_windows.bat @@ -1,5 +1,5 @@ set PATH=%PATH%;C:/Python36/Scripts/ -pip install tox==3.7.0 tox-venv==0.3.1 requests virtualenv +pip install tox==3.7.0 tox-venv==0.3.1 requests virtualenv yamllint python .ci/last_conan_version.py IF "%PYVER%"=="py39" ( diff --git a/.ci/requirements_linux.txt b/.ci/requirements_linux.txt index de45dc7a..b558102f 100644 --- a/.ci/requirements_linux.txt +++ b/.ci/requirements_linux.txt @@ -2,3 +2,4 @@ tox tox-venv virtualenv<=16.7.9 # TODO: Remove when dropping Python 2 requests +yamllint From 1e914e6cfe65a7519287ed48a9489ce02a03ddb1 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 6 Oct 2021 17:12:25 +0200 Subject: [PATCH 03/28] Update yaml_linter.py --- hooks/yaml_linter.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index e3fafe49..5b1b72d9 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -15,9 +15,20 @@ def pre_export(output, conanfile_path, *args, **kwargs): output.info("Lint yaml '{}'".format(conanfile_path)) conanfile_dirname = os.path.dirname(conanfile_path) conandata_file = os.path.join(conanfile_dirname, 'conadata.yaml') + + rules = { + "document-start": "disable", + "line-length": "disable", + "new-lines": "level: warning", + "empty-lines": "level: warning", + "indentation": "level: warning" + "comments": "level: warning", + "trailing-spaces": "level: warning", + } lint_args = ['-f', 'parsable', - '-d', '"{extends: default, rules: {document-start: disable}}"'] + '-d', '"{extends: default, rules: {%s}}"' % + ", ".join("%s: %s" % (r, rules[r]) for r in rules)] try: command = ['yamllint'] + lint_args + [conandata_file.replace('\\', '/')] @@ -38,4 +49,4 @@ def pre_export(output, conanfile_path, *args, **kwargs): output.info("YAML Linter detected '{}' errors".format(errors)) if os.getenv(CONAN_HOOK_YAMLLINT_WERR) and errors: - raise ConanException("Package recipe has YAML linter errors. Please fix them.") \ No newline at end of file + raise ConanException("Package recipe has YAML linter errors. Please fix them.") From f84670135a174945bde306dd02a77c73a08507ea Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 6 Oct 2021 17:15:20 +0200 Subject: [PATCH 04/28] Update yaml_linter.py --- hooks/yaml_linter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index 5b1b72d9..f4a5f85f 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -22,7 +22,6 @@ def pre_export(output, conanfile_path, *args, **kwargs): "new-lines": "level: warning", "empty-lines": "level: warning", "indentation": "level: warning" - "comments": "level: warning", "trailing-spaces": "level: warning", } From 3cfc6cdddae99f63e912e197fc272ddbefe5b8bf Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 6 Oct 2021 20:25:56 +0200 Subject: [PATCH 05/28] Update yaml_linter.py --- hooks/yaml_linter.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index f4a5f85f..30f2dc86 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -14,15 +14,15 @@ def pre_export(output, conanfile_path, *args, **kwargs): output.info("Lint yaml '{}'".format(conanfile_path)) conanfile_dirname = os.path.dirname(conanfile_path) - conandata_file = os.path.join(conanfile_dirname, 'conadata.yaml') + conandata_file = os.path.join(conanfile_dirname, 'conandata.yaml') rules = { "document-start": "disable", "line-length": "disable", - "new-lines": "level: warning", - "empty-lines": "level: warning", - "indentation": "level: warning" - "trailing-spaces": "level: warning", + "new-lines": "{level: warning}", + "empty-lines": "{level: warning}", + "indentation": "{level: warning}", + "trailing-spaces": "{level: warning}", } lint_args = ['-f', 'parsable', @@ -30,7 +30,7 @@ def pre_export(output, conanfile_path, *args, **kwargs): ", ".join("%s: %s" % (r, rules[r]) for r in rules)] try: - command = ['yamllint'] + lint_args + [conandata_file.replace('\\', '/')] + command = ['yamllint'] + lint_args + ['"%s"' % conandata_file.replace('\\', '/')] command = " ".join(command) shell = bool(platform.system() != "Windows") p = subprocess.Popen(command, shell=shell, bufsize=10, @@ -41,8 +41,12 @@ def pre_export(output, conanfile_path, *args, **kwargs): output.error("Unexpected error running linter: {}".format(exc)) return errors = 0 - for line in messages.splitlines(): + for line in yamllint_stdout.splitlines(): output.info(line) + i = line.find(":") + line = line[i:] + i = line.find(":") + line = line[i:] parts = line.split(' ') errors += int(parts[1] == "[error]") From a470d0d734c53737fa61fbc5b93110f980b672ce Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 6 Oct 2021 23:04:53 +0200 Subject: [PATCH 06/28] Update yaml_linter.py --- hooks/yaml_linter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index 30f2dc86..809302b4 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -14,7 +14,7 @@ def pre_export(output, conanfile_path, *args, **kwargs): output.info("Lint yaml '{}'".format(conanfile_path)) conanfile_dirname = os.path.dirname(conanfile_path) - conandata_file = os.path.join(conanfile_dirname, 'conandata.yaml') + conandata_file = os.path.join(conanfile_dirname, 'conandata.yml') rules = { "document-start": "disable", From cedc7586940fb4b696ee0444d69ce52bc600e405 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Wed, 6 Oct 2021 23:19:32 +0200 Subject: [PATCH 07/28] Update recipe_linter.py --- hooks/recipe_linter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hooks/recipe_linter.py b/hooks/recipe_linter.py index 5a24abe3..1dba76ff 100644 --- a/hooks/recipe_linter.py +++ b/hooks/recipe_linter.py @@ -28,7 +28,6 @@ def pre_export(output, conanfile_path, *args, **kwargs): conanfile_dirname = os.path.dirname(conanfile_path) lint_args = ['--output-format=json', # JSON output fails in Windows (parsing) - '--py3k', '--enable=all', '--reports=no', '--disable=no-absolute-import', From 39320640db76902731e788e728ee038ecb261801 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Thu, 7 Oct 2021 10:27:02 +0200 Subject: [PATCH 08/28] be explicit when yamllint is not installed --- hooks/yaml_linter.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index 809302b4..f128f4f5 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -3,11 +3,19 @@ import os import platform import subprocess +import sys from conans.errors import ConanException from conans.tools import logger +try: + import yamllint +except ImportError as e: + sys.stderr.write("Install yamllint to use 'yaml_linter' hook: 'pip install yamllint'") + sys.exit(1) + + CONAN_HOOK_YAMLLINT_WERR = "CONAN_YAMLLINT_WERR" From 2a9a9727773859a7417b05d7f5065695a8888298 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Thu, 7 Oct 2021 18:54:58 +0200 Subject: [PATCH 09/28] fix test --- tests/test_hooks/test_yaml_linter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_hooks/test_yaml_linter.py b/tests/test_hooks/test_yaml_linter.py index c3fe2c39..70362420 100644 --- a/tests/test_hooks/test_yaml_linter.py +++ b/tests/test_hooks/test_yaml_linter.py @@ -41,7 +41,7 @@ def test_basic(self, yamllint_werr): patches: """) tools.save('conanfile.py', content=self.conanfile) - tools.save('conandata.yaml', content=self.conandatafile) + tools.save('conandata.yml', content=self.conandatafile) yamllint_werr_value = "1" if yamllint_werr else None with environment_append({"CONAN_YAMLLINT_WERR": yamllint_werr_value}): return_code = ERROR_GENERAL if yamllint_werr else SUCCESS @@ -51,7 +51,7 @@ def test_basic(self, yamllint_werr): self.assertIn("pre_export(): Package recipe has linter errors." " Please fix them.", output) - self.assertIn("pre_export(): conandata.yaml:9:1:" + self.assertIn("pre_export(): conandata.yml:9:1:" " [error] duplication of key \"patches\" in mapping (key-duplicates)", output) From 08766472e703ed64892076df2daf26b127eb9ce3 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 11:57:20 +0200 Subject: [PATCH 10/28] lint all yml files --- hooks/yaml_linter.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index f128f4f5..8369161c 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -22,7 +22,6 @@ def pre_export(output, conanfile_path, *args, **kwargs): output.info("Lint yaml '{}'".format(conanfile_path)) conanfile_dirname = os.path.dirname(conanfile_path) - conandata_file = os.path.join(conanfile_dirname, 'conandata.yml') rules = { "document-start": "disable", @@ -38,7 +37,7 @@ def pre_export(output, conanfile_path, *args, **kwargs): ", ".join("%s: %s" % (r, rules[r]) for r in rules)] try: - command = ['yamllint'] + lint_args + ['"%s"' % conandata_file.replace('\\', '/')] + command = ['yamllint'] + lint_args + ['"%s"' % conanfile_dirname.replace('\\', '/')] command = " ".join(command) shell = bool(platform.system() != "Windows") p = subprocess.Popen(command, shell=shell, bufsize=10, From 5dbb4d1d5c76a1350df087a71de563289aec6869 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:00:40 +0200 Subject: [PATCH 11/28] Update test_yaml_linter.py --- tests/test_hooks/test_yaml_linter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hooks/test_yaml_linter.py b/tests/test_hooks/test_yaml_linter.py index 70362420..e6120c45 100644 --- a/tests/test_hooks/test_yaml_linter.py +++ b/tests/test_hooks/test_yaml_linter.py @@ -41,7 +41,7 @@ def test_basic(self, yamllint_werr): patches: """) tools.save('conanfile.py', content=self.conanfile) - tools.save('conandata.yml', content=self.conandatafile) + tools.save('conandata.yml', content=conandatafile) yamllint_werr_value = "1" if yamllint_werr else None with environment_append({"CONAN_YAMLLINT_WERR": yamllint_werr_value}): return_code = ERROR_GENERAL if yamllint_werr else SUCCESS From 65b0f5de4e1274c863a52a2f354e5547ff23d378 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:12:45 +0200 Subject: [PATCH 12/28] Update test_yaml_linter.py --- tests/test_hooks/test_yaml_linter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_hooks/test_yaml_linter.py b/tests/test_hooks/test_yaml_linter.py index e6120c45..f802ce22 100644 --- a/tests/test_hooks/test_yaml_linter.py +++ b/tests/test_hooks/test_yaml_linter.py @@ -37,7 +37,7 @@ def test_basic(self, yamllint_werr): patches: "version": - patch_file: "patches/abcdef.diff" - base_path: "source" + base_path: "source" patches: """) tools.save('conanfile.py', content=self.conanfile) @@ -64,9 +64,9 @@ def test_path_with_spaces(self): patches: "version": - patch_file: "patches/abcdef.diff" - base_path: "source" + base_path: "source" """) - tools.save(os.path.join("path spaces", "conanfile.py"), content=conanfile) + tools.save(os.path.join("path spaces", "conanfile.py"), content=self.conanfile) tools.save(os.path.join("path spaces", "conandata.py"), content=conandatafile) output = self.conan(['export', 'path spaces/conanfile.py', 'name/version@']) recipe_path = os.path.join(os.getcwd(), "path spaces", "conanfile.py") From 58f9019bcfbdbffdb5c84465c0d93f95afd587ee Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:22:31 +0200 Subject: [PATCH 13/28] Update requirements_test.txt --- tests/requirements_test.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/requirements_test.txt b/tests/requirements_test.txt index 2a69b3b4..da4f3e5e 100644 --- a/tests/requirements_test.txt +++ b/tests/requirements_test.txt @@ -5,4 +5,5 @@ pluggy==0.11.0 pylint==2.10.2; python_version >= '3.0' pylint==1.9.5; python_version == '2.7' astroid -spdx_lookup \ No newline at end of file +spdx_lookup +yamllint From 9a403d4e262b72c087df146fbd452507c55310f8 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:22:48 +0200 Subject: [PATCH 14/28] Update generate_env_windows.bat --- .ci/generate_env_windows.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/generate_env_windows.bat b/.ci/generate_env_windows.bat index 22ac131a..be3ba420 100644 --- a/.ci/generate_env_windows.bat +++ b/.ci/generate_env_windows.bat @@ -1,5 +1,5 @@ set PATH=%PATH%;C:/Python36/Scripts/ -pip install tox==3.7.0 tox-venv==0.3.1 requests virtualenv yamllint +pip install tox==3.7.0 tox-venv==0.3.1 requests virtualenv python .ci/last_conan_version.py IF "%PYVER%"=="py39" ( From bc02165cd9217befcab341460ef2d8c642b8cc9b Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:22:57 +0200 Subject: [PATCH 15/28] Update requirements_linux.txt --- .ci/requirements_linux.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/.ci/requirements_linux.txt b/.ci/requirements_linux.txt index b558102f..de45dc7a 100644 --- a/.ci/requirements_linux.txt +++ b/.ci/requirements_linux.txt @@ -2,4 +2,3 @@ tox tox-venv virtualenv<=16.7.9 # TODO: Remove when dropping Python 2 requests -yamllint From d9a5a3cd574735b0d62757cb4d6d5f6e2282b91a Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:34:12 +0200 Subject: [PATCH 16/28] Update test_yaml_linter.py --- tests/test_hooks/test_yaml_linter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_hooks/test_yaml_linter.py b/tests/test_hooks/test_yaml_linter.py index f802ce22..065d7e0a 100644 --- a/tests/test_hooks/test_yaml_linter.py +++ b/tests/test_hooks/test_yaml_linter.py @@ -48,10 +48,10 @@ def test_basic(self, yamllint_werr): output = self.conan(['export', '.', 'name/version@'], expected_return_code=return_code) if yamllint_werr: - self.assertIn("pre_export(): Package recipe has linter errors." + self.assertIn("pre_export(): Package recipe has YAML linter errors." " Please fix them.", output) - self.assertIn("pre_export(): conandata.yml:9:1:" + self.assertIn("conandata.yml:10:1:" " [error] duplication of key \"patches\" in mapping (key-duplicates)", output) From a088850b84f111d1562209d0fad8f34ed1e1cf13 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:39:39 +0200 Subject: [PATCH 17/28] Update recipe_linter.py --- hooks/recipe_linter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hooks/recipe_linter.py b/hooks/recipe_linter.py index 1dba76ff..5a24abe3 100644 --- a/hooks/recipe_linter.py +++ b/hooks/recipe_linter.py @@ -28,6 +28,7 @@ def pre_export(output, conanfile_path, *args, **kwargs): conanfile_dirname = os.path.dirname(conanfile_path) lint_args = ['--output-format=json', # JSON output fails in Windows (parsing) + '--py3k', '--enable=all', '--reports=no', '--disable=no-absolute-import', From d99c7189d7ddab9234f35541c46a484eb6ebf3ae Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 12:57:11 +0200 Subject: [PATCH 18/28] force recreation of tox environment --- tox.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tox.ini b/tox.ini index e6c9a9f8..4fbfc5ec 100644 --- a/tox.ini +++ b/tox.ini @@ -33,3 +33,5 @@ commands = coverage: coverage combine coverage: coverage report coverage: coverage xml + +recreate = true From a33d237fa7a11b2daf4dbbd7946ca8f21a99c31d Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 13:05:28 +0200 Subject: [PATCH 19/28] Update test_yaml_linter.py --- tests/test_hooks/test_yaml_linter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_hooks/test_yaml_linter.py b/tests/test_hooks/test_yaml_linter.py index 065d7e0a..33892dad 100644 --- a/tests/test_hooks/test_yaml_linter.py +++ b/tests/test_hooks/test_yaml_linter.py @@ -70,5 +70,5 @@ def test_path_with_spaces(self): tools.save(os.path.join("path spaces", "conandata.py"), content=conandatafile) output = self.conan(['export', 'path spaces/conanfile.py', 'name/version@']) recipe_path = os.path.join(os.getcwd(), "path spaces", "conanfile.py") - self.assertIn("pre_export(): Lint recipe '{}'".format(recipe_path), output) - self.assertIn("pre_export(): Linter detected '0' errors", output) + self.assertIn("pre_export(): Lint yaml '{}'".format(recipe_path), output) + self.assertIn("pre_export(): YAML Linter detected '0' errors", output) From b7f967194d6477ce218d7f2836b46ba4b9f66591 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 20:02:19 +0200 Subject: [PATCH 20/28] Scan also ../config.yml --- hooks/yaml_linter.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index 8369161c..9c27c354 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -35,9 +35,13 @@ def pre_export(output, conanfile_path, *args, **kwargs): lint_args = ['-f', 'parsable', '-d', '"{extends: default, rules: {%s}}"' % ", ".join("%s: %s" % (r, rules[r]) for r in rules)] + lint_args.append('"%s"' % conanfile_dirname.replace('\\', '/')) + configfile = os.path.join(conanfile_dirname), "..", "config.yml") + if os.path.isfile(configfile): + lint_args.append('"%s"' % configfile.replace('\\', '/')) try: - command = ['yamllint'] + lint_args + ['"%s"' % conanfile_dirname.replace('\\', '/')] + command = ['yamllint'] + lint_args command = " ".join(command) shell = bool(platform.system() != "Windows") p = subprocess.Popen(command, shell=shell, bufsize=10, From 3b1ab55541d0de07f26d635107271eddd9cc6ee4 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 8 Oct 2021 22:22:37 +0200 Subject: [PATCH 21/28] Update hooks/yaml_linter.py Co-authored-by: Uilian Ries --- hooks/yaml_linter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index 9c27c354..a145f7e1 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -36,7 +36,7 @@ def pre_export(output, conanfile_path, *args, **kwargs): '-d', '"{extends: default, rules: {%s}}"' % ", ".join("%s: %s" % (r, rules[r]) for r in rules)] lint_args.append('"%s"' % conanfile_dirname.replace('\\', '/')) - configfile = os.path.join(conanfile_dirname), "..", "config.yml") + configfile = os.path.join(conanfile_dirname, "..", "config.yml") if os.path.isfile(configfile): lint_args.append('"%s"' % configfile.replace('\\', '/')) From 67f855a33a83dcf2f23523182ea770a138cccb25 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Thu, 14 Oct 2021 17:49:51 +0200 Subject: [PATCH 22/28] don't exit if yamllint is not available --- hooks/yaml_linter.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index a145f7e1..8bb997ec 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -9,17 +9,17 @@ from conans.tools import logger -try: - import yamllint -except ImportError as e: - sys.stderr.write("Install yamllint to use 'yaml_linter' hook: 'pip install yamllint'") - sys.exit(1) CONAN_HOOK_YAMLLINT_WERR = "CONAN_YAMLLINT_WERR" def pre_export(output, conanfile_path, *args, **kwargs): + try: + import yamllint + except ImportError as e: + output.error("Install yamllint to use 'yaml_linter' hook: 'pip install yamllint'") + return output.info("Lint yaml '{}'".format(conanfile_path)) conanfile_dirname = os.path.dirname(conanfile_path) From f4fdc61470ac31816f0ad9844babe0caea6a0747 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Thu, 14 Oct 2021 18:11:04 +0200 Subject: [PATCH 23/28] Update yaml_linter.py --- hooks/yaml_linter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index 8bb997ec..5e719bc9 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -3,7 +3,6 @@ import os import platform import subprocess -import sys from conans.errors import ConanException from conans.tools import logger From 6d7cc69ca2ec49c177873756bbfe3e669079b5ec Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Thu, 14 Oct 2021 18:16:56 +0200 Subject: [PATCH 24/28] Update yaml_linter.py --- hooks/yaml_linter.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/hooks/yaml_linter.py b/hooks/yaml_linter.py index 5e719bc9..efca1e43 100644 --- a/hooks/yaml_linter.py +++ b/hooks/yaml_linter.py @@ -8,8 +8,6 @@ from conans.tools import logger - - CONAN_HOOK_YAMLLINT_WERR = "CONAN_YAMLLINT_WERR" From 77059c5de06200465eecfa3a67938fc59cb66571 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Thu, 14 Oct 2021 18:22:20 +0200 Subject: [PATCH 25/28] Update tox.ini --- tox.ini | 2 -- 1 file changed, 2 deletions(-) diff --git a/tox.ini b/tox.ini index 4fbfc5ec..e6c9a9f8 100644 --- a/tox.ini +++ b/tox.ini @@ -33,5 +33,3 @@ commands = coverage: coverage combine coverage: coverage report coverage: coverage xml - -recreate = true From 4df32e64ac25df7caa1012c2a6aa40ccaa417dc6 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 15 Oct 2021 10:18:49 +0200 Subject: [PATCH 26/28] Update README.md --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 605bbf9d..d240e791 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Repository to develop **experimental** [Conan](https://conan.io) hooks for Conan * [Member typo checker](#members-typo-checker) * [SPDX checker](#spdx-checker) * [Recipe linter](#recipe-linter) + * [YAML linter](#yaml-linter) ## Hook setup @@ -29,7 +30,7 @@ Only copying hook files will not activate them. To install all hooks from Conan repository in Github: -``$ conan config install https://github.com/conan-io/hooks.git`` +``$ conan config install https://github.com/conan*io/hooks.git`` If you are using Conan >=1.14 you can specify the source and destination folder to avoid copying undesired files to your local cache: @@ -170,6 +171,14 @@ There several environment variables you can use to configure it: This hook requires additional dependencies to work: `pip install pylint astroid`. +### [YAML linter](hooks/yaml_linter.py) + +This hooks runs [yamllint ](https://yamllint.readthedocs.io/) over the yaml files +in a recipe before exporting them (it runs in the `pre_export` hook), it can be +really useful to check for typos. + +This hook requires additional dependencies to work: `pip install yamllint`. + ## License [MIT License](LICENSE) From 9ba6953d319f70c3238476a7cc967272d52aa9e4 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 15 Oct 2021 10:19:45 +0200 Subject: [PATCH 27/28] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d240e791..0a2d482b 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,7 @@ This hook requires additional dependencies to work: `pip install pylint astroid` ### [YAML linter](hooks/yaml_linter.py) -This hooks runs [yamllint ](https://yamllint.readthedocs.io/) over the yaml files +This hook runs [yamllint](https://yamllint.readthedocs.io/) over the yaml files in a recipe before exporting them (it runs in the `pre_export` hook), it can be really useful to check for typos. From 197247b7ac03ac84315e16e28d495d5a2fed9fc1 Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Fri, 15 Oct 2021 10:29:46 +0200 Subject: [PATCH 28/28] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index af393eaa..87ee642d 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Only copying hook files will not activate them. To install all hooks from Conan repository in Github: -``$ conan config install https://github.com/conan*io/hooks.git`` +``$ conan config install https://github.com/conan-io/hooks.git`` If you are using Conan >=1.14 you can specify the source and destination folder to avoid copying undesired files to your local cache: