export_sources()

等同于 exports_sources 属性,但以方法形式存在。此方法将在 export 时被调用,即在 conan exportconan export-pkgconan create 命令执行期间发生。其目的是允许将文件从用户文件夹复制到 Conan 缓存文件夹,这些文件将成为 recipe 源码的一部分。这些源码会与 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() later 应用。

from conan.tools.files import export_conandata_patches

class Pkg(ConanFile):

    def export_sources(self):
        export_conandata_patches(self)

注意

最佳实践

  • recipe 源码必须与配置无关。这些源码适用于所有配置,因此无法根据不同的设置、选项或平台进行条件性的 export_sources()。不要尝试进行任何形式的条件导出。如果需要,请一次性导出所有配置所需的所有文件。

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

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