Backing up the sources of third-party dependencies

The sources backup feature transparently uploads the sources downloaded by recipes (from Github releases, project web pages, etc.) to a server of your own, so that future builds are reproducible even if the original URLs disappear. By default, those backups are uploaded together with the packages with conan upload. But what if we want to back up the sources of our dependencies without building and uploading any package? This is the case for example for populating the backup server from a CI job, or for dependencies that are already available in a remote as binaries, and that would be skipped during the upload.

There are two ways to do that, both ending with the conan cache backup-upload command:

  • Retrieving the sources of a full dependency graph with conan graph info and tools.build:download_source=True.

  • Retrieving the sources of a single recipe with conan source.

The backup-upload command uploads all the source backups that are in the cache, regardless of which package they belong to, if any.

Configuring the backup server

Let’s define the following global.conf, using the URL of a backup repository that accepts PUT and GET HTTP methods (see how to create one in Artifactory):

global.conf
core.sources:upload_url=https://myteam.myorg.com/artifactory/backup-sources/
core.sources:download_urls=["origin", "https://myteam.myorg.com/artifactory/backup-sources/"]

If the repository requires authentication, define it in the source_credentials.json file. Check the sources backup configuration for the rest of the available configuration items, like core.sources:exclude_urls.

Backing up the sources of a dependency graph

Recipes download their sources in the source() method, that is only executed when the package is built from source. The tools.build:download_source=True conf forces the execution of source() for all the packages in the graph, even if there are binaries available, without needing to build anything. This can be done with conan graph info (or conan install):

$ conan graph info --requires=libiconv/1.17 -c tools.build:download_source=True

Recall that the conan graph info can also work with a conanfile.txt/.py instead of a --requires argument. Once the sources are downloaded to the Conan cache (to the core.sources:download_cache folder), we can upload all of them to the backup server:

$ conan cache backup-upload

-------- Uploading backup sources --------
Uploading summary '8f74213b56238c85a50a5329f77e06198771e70dd9a739779f4c02f65d971313.json' to backup sources server
Uploading file '8f74213b56238c85a50a5329f77e06198771e70dd9a739779f4c02f65d971313' to backup sources server
Upload backup sources complete

Each source is stored in the backup server with the sha256 of the downloaded file as name, plus a .json file with the references and original URLs that the file belongs to:

8f74213b56238c85a50a5329f77e06198771e70dd9a739779f4c02f65d971313.json
{
  "references": {
    "libiconv/1.17": [
      "https://ftpmirror.gnu.org/gnu/libiconv/libiconv-1.17.tar.gz",
      "https://ftp.gnu.org/gnu/libiconv/libiconv-1.17.tar.gz"
    ]
  },
  "timestamp": 1794226585
}

Note

  • Only the sources downloaded with get() or download() providing a sha256 are backed up.

  • The tools.build:download_source=True conf applies to all the packages in the graph, including the tool_requires, so their sources will be backed up too. It can be restricted to some packages with the conf pattern syntax, like -c libiconv/*:tools.build:download_source=True.

  • Sources that were fetched from a URL in core.sources:exclude_urls are not uploaded.

Note

The source() method is not executed for the packages whose binaries are skipped from the graph, as they are not necessary in the current configuration (for example the dependencies of a shared library that are not needed by its consumers). The sources of those packages will not be downloaded, so they will not be backed up either. To make sure that the sources of all the packages in the graph are retrieved, disable the binaries skip with the tools.graph:skip_binaries=False conf:

# This will only work for packages in the "host" context, not build tools
# Use "-c:a" if you want to download the sources for "tool-requires" too
$ conan graph info --requires=libiconv/1.17 -c tools.build:download_source=True -c tools.graph:skip_binaries=False

Backing up the sources of a single recipe

If we have the recipe in a folder, for example in the Conan Center Index repository or in our own recipes repository, we can run the conan source command directly, which only executes the source() method without building anything:

conanfile.py
from conan import ConanFile
from conan.tools.files import get

class ZlibRecipe(ConanFile):
    name = "zlib"
    version = "1.3.1"

    def source(self):
        get(self, **self.conan_data["sources"][self.version], strip_root=True)
conandata.yml
sources:
  "1.3.1":
    url: "https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz"
    sha256: "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23"
$ conan source .
Getting sources for conanfile.py (zlib/1.3.1)
Calling source() in /home/user/zlib
Sources correctly downloaded from https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
Uncompressing zlib-1.3.1.tar.gz to .

$ conan cache backup-upload
-------- Uploading backup sources --------
Uploading summary '9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23.json' to backup sources server
Uploading file '9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23' to backup sources server
Upload backup sources complete

This approach can be automated for many recipes by simply looping over their folders, and then uploading all the backups at once with a single conan cache backup-upload.

Using the backup

Once the sources are in the backup server, any build that has the core.sources:download_urls conf above will be able to fetch the sources from it when the original URL is not available (or before trying it, if the backup server is listed first in core.sources:download_urls). See how to run Conan with the backup configured for more details.