init()

这是一个可选的用于初始化 conanfile 值的方法,设计用于从 python_requires 继承。假设我们有一个 base/1.1 配方

base/conanfile.py
from conan import ConanFile

class MyConanfileBase:
    license = "MyLicense"
    settings = "os", # tuple!


class PyReq(ConanFile):
    name = "base"
    version = "1.1"
    package_type = "python-require"

我们可以使用以下方式重用和继承它

pkg/conanfile.py
from conan import ConanFile

class Pkg(ConanFile):
    license = "MIT"
    settings = "arch", # tuple!
    python_requires = "base/1.1"
    python_requires_extend = "base.MyConanfileBase"

    def init(self):
        base = self.python_requires["base"].module.MyConanfileBase
        self.settings = base.settings + self.settings  # Note, adding 2 tuples = tuple
        self.license = base.license  # License is overwritten

最终的 Pkg conanfile 将同时拥有 osarch 作为设置 (settings),以及 MyLicense 作为许可证 (license)。

要扩展基类的 options,必须调用 self.options.update() 方法

base/conanfile.py
from conan import ConanFile

class BaseConan:
    options = {"base": [True, False]}
    default_options = {"base": True}

class PyReq(ConanFile):
    name = "base"
    version = "1.0.0"
    package_type = "python-require"

当调用 init() 时,self.options 对象已经初始化。此时,更新 self.default_options 是没有用的,必须同时使用基类选项和基类默认选项值来更新 self.options

pkg/conanfile.py
from conan import ConanFile

class DerivedConan(ConanFile):
    name = "derived"
    python_requires = "base/1.0.0"
    python_requires_extend = "base.BaseConan"
    options = {"derived": [True, False]}
    default_options = {"derived": False}

    def init(self):
        base = self.python_requires["base"].module.BaseConan
        # Note we pass the base options and default_options
        self.options.update(base.options, base.default_options)

如果你需要无条件地初始化类属性,如 licensedescription,或者任何其他来自 conandata.yml 以外的数据文件的信息,这个方法也很有用。例如,你可以有一个包含有关库的 licensedescriptionauthor 信息的 json 文件。

data.json
{"license": "MIT", "description": "This is my awesome library.", "author": "Me"}

然后,你可以从 init() 方法中加载这些信息。

import os
import json
from conan import ConanFile
from conan.tools.files import load


class Pkg(ConanFile):
    exports = "data.json" # Important that it is exported with the recipe

    def init(self):
        data = load(self, os.path.join(self.recipe_folder, "data.json"))
        d = json.loads(data)
        self.license = d["license"]
        self.description = d["description"]
        self.author = d["author"]

注意

最佳实践

  • 尽量保持你的 python_requires 尽可能简单,并且不要重用其中的属性(init() 方法的主要需求),尽量避免此 init() 方法的复杂性。总的来说,继承可能比组合 (composition) 带来更多问题(或者换句话说,“优先使用组合而非继承”作为通用的编程良好实践),所以如果可能,尽量避免继承。

  • 不要滥用 init() 来处理此处未列出的其他目的,也不要使用 Python 私有的 ConanFile.__init__ 构造函数。

  • init() 方法在配方加载时执行。它不能包含对设置 (settings)、选项 (options)、配置 (conf) 的条件判断,也不能使用除上述 python_requires 之外的任何依赖信息。