Source - Base source class

Abstract Methods

For loading and configuration purposes, Sources must implement the Plugin base class abstract methods.

Sources expose the following abstract methods. Unless explicitly mentioned, these methods are mandatory to implement.

SourceFetcher - Object for fetching individual URLs

Abstract Methods

SourceFetchers expose the following abstract methods. Unless explicitly mentioned, these methods are mandatory to implement.

  • SourceFetcher.fetch()

    Fetches the URL associated with this SourceFetcher, optionally taking an alias override.

class Consistency

Bases: object

INCONSISTENT = 0

Inconsistent

Inconsistent sources have no explicit reference set. They cannot produce a cache key, be fetched or staged. They can only be tracked.

RESOLVED = 1

Resolved

Resolved sources have a reference and can produce a cache key and be fetched, however they cannot be staged.

CACHED = 2

Cached

Cached sources have a reference which is present in the local source cache. Only cached sources can be staged.

exception SourceError(message, *, detail=None, reason=None, temporary=False)

Bases: buildstream._exceptions.BstError

This exception should be raised by Source implementations to report errors to the user.

Parameters:
  • message (str) – The breif error description to report to the user
  • detail (str) – A possibly multiline, more detailed error message
  • reason (str) – An optional machine readable reason string, used for test cases
  • temporary (bool) – An indicator to whether the error may occur if the operation was run again. (Since: 1.2)
class SourceFetcher

Bases: object

This interface exists so that a source that downloads from multiple places (e.g. a git source with submodules) has a consistent interface for fetching and substituting aliases.

Since: 1.2

fetch(alias_override=None)

Fetch remote sources and mirror them locally, ensuring at least that the specific reference is cached locally.

Parameters:alias_override (str) – The alias to use instead of the default one defined by the aliases field in the project’s config.
Raises:SourceError

Implementors should raise SourceError if the there is some network error or if the source reference could not be matched.

mark_download_url(url)

Identifies the URL that this SourceFetcher uses to download

This must be called during the fetcher’s initialization

Parameters:url (str) – The url used to download.
class Source

Bases: buildstream.plugin.Plugin

Base Source class.

All Sources derive from this class, this interface defines how the core will be interacting with Sources.

COMMON_CONFIG_KEYS = ['kind', 'directory']

Common source config keys

Source config keys that must not be accessed in configure(), and should be checked for using node_validate().

get_consistency()

Report whether the source has a resolved reference

Returns:The source consistency
Return type:(Consistency)
load_ref(node)

Loads the ref for this Source from the specified node.

Parameters:node (dict) – The YAML node to load the ref from

Note

The ref for the Source is expected to be read at Plugin.configure() time, this will only be used for loading refs from alternative locations than in the element.bst file where the given Source object has been declared.

Since: 1.2

get_ref()

Fetch the internal ref, however it is represented

Returns:The internal source reference, or None
Return type:(simple object)

Note

The reference is the user provided (or track resolved) value the plugin uses to represent a specific input, like a commit in a VCS or a tarball’s checksum. Usually the reference is a string, but the plugin may choose to represent it with a tuple or such.

Implementations must return a None value in the case that the ref was not loaded. E.g. a (None, None) tuple is not acceptable.

set_ref(ref, node)

Applies the internal ref, however it is represented

Parameters:
  • ref (simple object) – The internal source reference to set, or None
  • node (dict) – The same dictionary which was previously passed to configure()

See get_ref() for a discussion on the ref parameter.

Note

Implementors must support the special None value here to allow clearing any existing ref.

track()

Resolve a new ref from the plugin’s track option

Returns:A new internal source reference, or None
Return type:(simple object)

If the backend in question supports resolving references from a symbolic tracking branch or tag, then this should be implemented to perform this task on behalf of build-stream track commands.

This usually requires fetching new content from a remote origin to see if a new ref has appeared for your branch or tag. If the backend store allows one to query for a new ref from a symbolic tracking data without downloading then that is desirable.

See get_ref() for a discussion on the ref parameter.

fetch()

Fetch remote sources and mirror them locally, ensuring at least that the specific reference is cached locally.

Raises:SourceError

Implementors should raise SourceError if the there is some network error or if the source reference could not be matched.

stage(directory)

Stage the sources to a directory

Parameters:directory (str) – Path to stage the source
Raises:SourceError

Implementors should assume that directory already exists and stage already cached sources to the passed directory.

Implementors should raise SourceError when encountering some system error.

init_workspace(directory)

Initialises a new workspace

Parameters:directory (str) – Path of the workspace to init
Raises:SourceError

Default implementation is to call stage().

Implementors overriding this method should assume that directory already exists.

Implementors should raise SourceError when encountering some system error.

mark_download_url(url)

Identifies the URL that this Source uses to download

This must be called during configure() if translate_url() is not called.

Parameters:url (str) – The url used to download

Since: 1.2

get_source_fetchers()

Get the objects that are used for fetching

If this source doesn’t download from multiple URLs, returning None and falling back on the default behaviour is recommended.

Returns:
A list of SourceFetchers. If SourceFetchers are not supported,
this will be an empty list.
Return type:list

Since: 1.2

get_mirror_directory()

Fetches the directory where this source should store things

Returns:The directory belonging to this source
Return type:(str)
translate_url(url, *, alias_override=None)

Translates the given url which may be specified with an alias into a fully qualified url.

Parameters:
  • url (str) – A url, which may be using an alias
  • alias_override (str) – Optionally, an URI to override the alias with. (Since: 1.2)
Returns:

The fully qualified url, with aliases resolved

Return type:

str

get_project_directory()

Fetch the project base directory

This is useful for sources which need to load resources stored somewhere inside the project.

Returns:The project base directory
Return type:str
tempdir()

Context manager for working in a temporary directory

Yields:(str) – A path to a temporary directory

This should be used by source plugins directly instead of the tempfile module. This one will automatically cleanup in case of termination by catching the signal before os._exit(). It will also use the ‘mirror directory’ as expected for a source.