(#11627) linter - check ConanFile import and cross_building

* linter - check ConanFile import and cross_building

* remove unused code

* Add docs

* improve docs

* example - abseil

* md syntax

* revert uneeded changes

* Update docs/v2_linter.md

Co-authored-by: Daniel <danimanzaneque@gmail.com>

Co-authored-by: Daniel <danimanzaneque@gmail.com>
This commit is contained in:
Javier G. Sogo
2022-07-13 18:30:09 +02:00
committed by GitHub
parent 1f5efc8656
commit 202e85f077
9 changed files with 109 additions and 4 deletions

44
docs/v2_linter.md Normal file
View File

@@ -0,0 +1,44 @@
Linter to help migration to Conan v2
====================================
On our [path to Conan v2](v2_roadmap.md) we are leveraging on custom Pylint rules. This
linter will run for every pull-request that is submitted to the repository and will
raise some warnings and errors that should be addressed in order to migrate the
recipes to Conan v2.
It is important to note that these rules are targetting Conan v2 compatibility layer, their
purpose is to fail for v1 syntax that will be no longer available in v2. Even if the syntax
if perfectly valid in Conan v1, the recipe might fail here because it is not v2-compliant.
> **Note.-** Some of the errored checks might be just plain Python syntax errors, while
> others might be related to the custom rules added by us.
Here you can find some examples of the extra rules we are adding:
## Import ConanFile from `conan`
The module `conans` is deprecated in Conan v2. Now all the imports should be done from
module `conan`:
```python
from conan import ConanFile
```
## Import tools from `conan`
All v2-compatible tools are available in module `conan.tools` under different submodules. Recipes
should start to import their tools from this new module. Some of the new tools accept new
argument, please, check the [Conan documentation](https://docs.conan.io/en/latest/reference/conanfile/tools.html).
For example, the `cross_building` tool now should be used like:
```python
from conan.tools.build import cross_building
...
class Recipe(ConanFile):
def test(self):
if not cross_building(self):
pass
```

View File

@@ -6,6 +6,8 @@
> will be taken in ConanCenter and this repository to start running
> Conan v2 in pull requests.
> ⚠️ Read about the [linter in pull requests](v2_linter.md).
It's time to start thinking seriously about Conan v2 and prepare recipes
for the incoming changes. Conan v2 comes with many
changes and improvements, you can read about them in the

View File

@@ -0,0 +1,28 @@
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from astroid import nodes, Const, AssignName
class ImportConanFile(BaseChecker):
"""
Import ConanFile from new 'conan' module
"""
__implements__ = IAstroidChecker
name = "conan-import-conanfile"
msgs = {
"W9006": (
"Import ConanFile from new module: `from conan import ConanFile`. Old import is deprecated in Conan v2.",
"conan-import-conanfile",
"Import ConanFile from new module: `from conan import ConanFile`. Old import is deprecated in Conan v2.",
),
}
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
basename = node.modname
if basename == 'conans':
names = [name for name, _ in node.names]
if 'ConanFile' in names:
self.add_message("conan-import-conanfile", node=node)

View File

@@ -1,7 +1,9 @@
"""Pylint plugin for Conan Center Index"""
from pylint.lint import PyLinter
from linter.package_name import PackageName
from linter.check_package_name import PackageName
from linter.check_import_conanfile import ImportConanFile
def register(linter: PyLinter) -> None:
linter.register_checker(PackageName(linter))
linter.register_checker(ImportConanFile(linter))

View File

@@ -1,6 +1,8 @@
[MASTER]
load-plugins=linter.conanv2_transition,
linter.conanfile_transform
linter.transform_conanfile,
linter.transform_imports
py-version=3.6
recursive=no
suggestion-mode=yes
@@ -17,7 +19,8 @@ disable=fixme,
import-outside-toplevel # TODO: Remove
enable=conan-bad-name,
conan-missing-name
conan-missing-name,
conan-import-conanfile
[REPORTS]
evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error) / statement) * 10))

View File

@@ -1,6 +1,7 @@
[MASTER]
load-plugins=linter.conanv2_transition,
linter.conanfile_transform
linter.transform_conanfile,
linter.transform_imports
py-version=3.6
recursive=no
suggestion-mode=yes

View File

@@ -0,0 +1,25 @@
import astroid
from pylint.lint import PyLinter
"""
Here we are transforming the imports to mimic future Conan v2 release. With
these changes, built-in checks in Pylint will raise with different errors, so
we are modifying the messages to point users in the right direction.
"""
def register(linter: PyLinter):
msge1101 = linter.msgs_store._messages_definitions["E1101"]
msge1101.msg += ". Please, check https://github.com/conan-io/conan-center-index/blob/master/docs/v2_linter.md"
linter.msgs_store.register_message(msge1101)
def transform_tools(module):
""" Transform import module """
if 'cross_building' in module.locals:
del module.locals['cross_building']
astroid.MANAGER.register_transform(
astroid.Module, transform_tools,
lambda node: node.qname() == "conans.tools")