export_sources()

等同于 exports_sources 属性,但采用方法形式。此方法将在 export 时间被调用,这发生在 conan exportconan export-pkgconan create 命令中,其目的是允许将文件从用户文件夹复制到 Conan 缓存文件夹,这些文件将成为 recipe 的一部分。这些 sources 将与 recipe 一起上传到服务器,但通常只有在从源码构建包时才会被下载。

当前工作目录将是 self.recipe_folder,并且可以使用 self.export_sources_folder 作为使用 copy() 或自定义复制的目的文件夹。

from conan import ConanFile
from conan.tools.files import copy

class Pkg(ConanFile):
    def export_sources(self):
        # This LICENSE.md is a source file intended to be part of the final package
        # it is not the license of the current recipe
        copy(self, "LICENSE.md", self.recipe_folder, self.export_sources_folder)

该方法可能能够读取 recipe 文件夹中的文件并对其进行处理。

import os
from conan import ConanFile
from conan.tools.files import load, save

class Pkg(ConanFile):

    def export_sources(self):
        content = load(self, os.path.join(self.recipe_folder, "data.txt"))
        save(self, os.path.join(self.export_sources_folder, "myfile.txt"), content)

export_conandata_patches() 是一个高级辅助函数,它负责导出 conandata.yml 文件中定义的补丁,这些补丁稍后可以在 source() 方法中使用 apply_conandata_patches() 进行应用。

from conan.tools.files import export_conandata_patches

class Pkg(ConanFile):

    def export_sources(self):
        export_conandata_patches(self)

注意

最佳实践

  • Recipe sources 必须与配置无关。这些 sources 对所有配置都是通用的,因此不可能为不同的设置、选项或平台进行条件式 export_sources()。请勿尝试任何形式的条件式导出。如有必要,请一次性导出所有配置所需的文件。

  • export_sources() 方法不接收任何来自 profile 的信息,甚至不接收 conf。只有 global.conf 可用,并且无论如何都无法使用该 conf 来定义条件。

  • 请保持 export_source() 方法简单。其目的是将文件从用户文件夹复制到缓存,以便将这些文件与 recipe 一起存储。