diff options
author | Ed Baunton <ebaunton1@bloomberg.net> | 2018-07-12 18:11:08 -0400 |
---|---|---|
committer | Ed Baunton <ebaunton1@bloomberg.net> | 2018-07-25 20:43:51 +0100 |
commit | c049d021f91ba5b25806dc9066b622a411d78766 (patch) | |
tree | cd7128d54349a6c45bd106c8d9c143a0080c2a8e | |
parent | f62b6cb7420c91c2f87b3ebba4dcfcf16b4d6f0a (diff) | |
download | buildstream-c049d021f91ba5b25806dc9066b622a411d78766.tar.gz |
Add remote source plugin
Add a plugin that supports downloading files verbatim from a source with
an optional overridable filename and destination directory. Bumps bst
format version to 10.
Fixes #163
-rw-r--r-- | buildstream/_versions.py | 2 | ||||
-rw-r--r-- | buildstream/plugins/sources/remote.py | 80 | ||||
-rw-r--r-- | doc/source/core_plugins.rst | 1 | ||||
-rw-r--r-- | tests/sources/remote.py | 133 | ||||
-rw-r--r-- | tests/sources/remote/missing-file/target.bst | 7 | ||||
-rw-r--r-- | tests/sources/remote/no-ref/file | 1 | ||||
-rw-r--r-- | tests/sources/remote/no-ref/target.bst | 5 | ||||
-rw-r--r-- | tests/sources/remote/path-in-filename/dir/file | 0 | ||||
-rw-r--r-- | tests/sources/remote/path-in-filename/target.bst | 7 | ||||
-rw-r--r-- | tests/sources/remote/single-file-custom-name/dir/file | 0 | ||||
-rw-r--r-- | tests/sources/remote/single-file-custom-name/target.bst | 7 | ||||
-rw-r--r-- | tests/sources/remote/single-file/dir/file | 0 | ||||
-rw-r--r-- | tests/sources/remote/single-file/target.bst | 6 | ||||
-rw-r--r-- | tests/sources/remote/unique-keys/dir/file | 0 | ||||
-rw-r--r-- | tests/sources/remote/unique-keys/target-custom.bst | 7 | ||||
-rw-r--r-- | tests/sources/remote/unique-keys/target.bst | 6 |
16 files changed, 261 insertions, 1 deletions
diff --git a/buildstream/_versions.py b/buildstream/_versions.py index 28f00f8ca..19699e125 100644 --- a/buildstream/_versions.py +++ b/buildstream/_versions.py @@ -23,7 +23,7 @@ # This version is bumped whenever enhancements are made # to the `project.conf` format or the core element format. # -BST_FORMAT_VERSION = 9 +BST_FORMAT_VERSION = 10 # The base BuildStream artifact version diff --git a/buildstream/plugins/sources/remote.py b/buildstream/plugins/sources/remote.py new file mode 100644 index 000000000..77c4ce17d --- /dev/null +++ b/buildstream/plugins/sources/remote.py @@ -0,0 +1,80 @@ +# +# Copyright Bloomberg Finance LP +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see <http://www.gnu.org/licenses/>. +# +# Authors: +# Ed Baunton <ebaunton1@bloomberg.net> + +""" +remote - stage files from remote urls +===================================== + +**Usage:** + +.. code:: yaml + + # Specify the remote source kind + kind: remote + + # Optionally specify a relative staging directory + # directory: path/to/stage + + # Optionally specify a relative staging filename. + # If not specified, the basename of the url will be used. + # filename: customfilename + + # Specify the url. Using an alias defined in your project + # configuration is encouraged. 'bst track' will update the + # sha256sum in 'ref' to the downloaded file's sha256sum. + url: upstream:foo + + # Specify the ref. It's a sha256sum of the file you download. + ref: 6c9f6f68a131ec6381da82f2bff978083ed7f4f7991d931bfa767b7965ebc94b + +.. note:: + + The ``remote`` plugin is available since :ref:`format version 10 <project_format_version>` + +""" +import os +from buildstream import SourceError, utils +from ._downloadablefilesource import DownloadableFileSource + + +class RemoteSource(DownloadableFileSource): + # pylint: disable=attribute-defined-outside-init + + def configure(self, node): + super().configure(node) + + self.filename = self.node_get_member(node, str, 'filename', os.path.basename(self.url)) + + if os.sep in self.filename: + raise SourceError('{}: filename parameter cannot contain directories'.format(self)) + self.node_validate(node, DownloadableFileSource.COMMON_CONFIG_KEYS + ['filename']) + + def get_unique_key(self): + return super().get_unique_key() + [self.filename] + + def stage(self, directory): + # Same as in local plugin, don't use hardlinks to stage sources, they + # are not write protected in the sandbox. + dest = os.path.join(directory, self.filename) + with self.timed_activity("Staging remote file to {}".format(dest)): + utils.safe_copy(self._get_mirror_file(), dest) + + +def setup(): + return RemoteSource diff --git a/doc/source/core_plugins.rst b/doc/source/core_plugins.rst index 2951e00cf..da7c607c8 100644 --- a/doc/source/core_plugins.rst +++ b/doc/source/core_plugins.rst @@ -50,6 +50,7 @@ Sources :maxdepth: 1 sources/local + sources/remote sources/tar sources/zip sources/git diff --git a/tests/sources/remote.py b/tests/sources/remote.py new file mode 100644 index 000000000..f0f695c4f --- /dev/null +++ b/tests/sources/remote.py @@ -0,0 +1,133 @@ +import os +import pytest + +from buildstream._exceptions import ErrorDomain +from buildstream import _yaml +from tests.testutils import cli + +DATA_DIR = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + 'remote', +) + + +def generate_project(project_dir, tmpdir): + project_file = os.path.join(project_dir, "project.conf") + _yaml.dump({ + 'name': 'foo', + 'aliases': { + 'tmpdir': "file:///" + str(tmpdir) + } + }, project_file) + + +# Test that without ref, consistency is set appropriately. +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'no-ref')) +def test_no_ref(cli, tmpdir, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename) + generate_project(project, tmpdir) + assert cli.get_element_state(project, 'target.bst') == 'no reference' + + +# Here we are doing a fetch on a file that doesn't exist. target.bst +# refers to 'file' but that file is not present. +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'missing-file')) +def test_missing_file(cli, tmpdir, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename) + generate_project(project, tmpdir) + + # Try to fetch it + result = cli.run(project=project, args=[ + 'fetch', 'target.bst' + ]) + + result.assert_main_error(ErrorDomain.STREAM, None) + result.assert_task_error(ErrorDomain.SOURCE, None) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'path-in-filename')) +def test_path_in_filename(cli, tmpdir, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename) + generate_project(project, tmpdir) + + # Try to fetch it + result = cli.run(project=project, args=[ + 'fetch', 'target.bst' + ]) + + # The bst file has a / in the filename param + result.assert_main_error(ErrorDomain.SOURCE, None) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'single-file')) +def test_simple_file_build(cli, tmpdir, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename) + generate_project(project, tmpdir) + checkoutdir = os.path.join(str(tmpdir), "checkout") + + # Try to fetch it + result = cli.run(project=project, args=[ + 'fetch', 'target.bst' + ]) + result.assert_success() + + result = cli.run(project=project, args=[ + 'build', 'target.bst' + ]) + result.assert_success() + + result = cli.run(project=project, args=[ + 'checkout', 'target.bst', checkoutdir + ]) + result.assert_success() + # Note that the url of the file in target.bst is actually /dir/file + # but this tests confirms we take the basename + assert(os.path.exists(os.path.join(checkoutdir, 'file'))) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'single-file-custom-name')) +def test_simple_file_custom_name_build(cli, tmpdir, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename) + generate_project(project, tmpdir) + checkoutdir = os.path.join(str(tmpdir), "checkout") + + # Try to fetch it + result = cli.run(project=project, args=[ + 'fetch', 'target.bst' + ]) + result.assert_success() + + result = cli.run(project=project, args=[ + 'build', 'target.bst' + ]) + result.assert_success() + + result = cli.run(project=project, args=[ + 'checkout', 'target.bst', checkoutdir + ]) + result.assert_success() + assert(not os.path.exists(os.path.join(checkoutdir, 'file'))) + assert(os.path.exists(os.path.join(checkoutdir, 'custom-file'))) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'unique-keys')) +def test_unique_key(cli, tmpdir, datafiles): + '''This test confirms that the 'filename' parameter is honoured when it comes + to generating a cache key for the source. + ''' + project = os.path.join(datafiles.dirname, datafiles.basename) + generate_project(project, tmpdir) + assert cli.get_element_state(project, 'target.bst') == "fetch needed" + assert cli.get_element_state(project, 'target-custom.bst') == "fetch needed" + # Try to fetch it + result = cli.run(project=project, args=[ + 'fetch', 'target.bst' + ]) + + # We should download the file only once + assert cli.get_element_state(project, 'target.bst') == 'buildable' + assert cli.get_element_state(project, 'target-custom.bst') == 'buildable' + + # But the cache key is different because the 'filename' is different. + assert cli.get_element_key(project, 'target.bst') != \ + cli.get_element_key(project, 'target-custom.bst') diff --git a/tests/sources/remote/missing-file/target.bst b/tests/sources/remote/missing-file/target.bst new file mode 100644 index 000000000..53aeb3522 --- /dev/null +++ b/tests/sources/remote/missing-file/target.bst @@ -0,0 +1,7 @@ +kind: autotools +description: The kind of this element is irrelevant. +sources: +- kind: remote + url: tmpdir:/file + ref: abcdef + filename: filename diff --git a/tests/sources/remote/no-ref/file b/tests/sources/remote/no-ref/file new file mode 100644 index 000000000..7b4a53b4b --- /dev/null +++ b/tests/sources/remote/no-ref/file @@ -0,0 +1 @@ +filecontent diff --git a/tests/sources/remote/no-ref/target.bst b/tests/sources/remote/no-ref/target.bst new file mode 100644 index 000000000..b4f5f58af --- /dev/null +++ b/tests/sources/remote/no-ref/target.bst @@ -0,0 +1,5 @@ +kind: autotools +description: The kind of this element is irrelevant. +sources: +- kind: remote + url: tmpdir:/file diff --git a/tests/sources/remote/path-in-filename/dir/file b/tests/sources/remote/path-in-filename/dir/file new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/sources/remote/path-in-filename/dir/file diff --git a/tests/sources/remote/path-in-filename/target.bst b/tests/sources/remote/path-in-filename/target.bst new file mode 100644 index 000000000..ea0c96b44 --- /dev/null +++ b/tests/sources/remote/path-in-filename/target.bst @@ -0,0 +1,7 @@ +kind: import +description: test +sources: +- kind: remote + url: tmpdir:/dir/file + ref: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 + filename: path/to/file diff --git a/tests/sources/remote/single-file-custom-name/dir/file b/tests/sources/remote/single-file-custom-name/dir/file new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/sources/remote/single-file-custom-name/dir/file diff --git a/tests/sources/remote/single-file-custom-name/target.bst b/tests/sources/remote/single-file-custom-name/target.bst new file mode 100644 index 000000000..e8f95ad1a --- /dev/null +++ b/tests/sources/remote/single-file-custom-name/target.bst @@ -0,0 +1,7 @@ +kind: import +description: test +sources: +- kind: remote + url: tmpdir:/dir/file + ref: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 + filename: custom-file diff --git a/tests/sources/remote/single-file/dir/file b/tests/sources/remote/single-file/dir/file new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/sources/remote/single-file/dir/file diff --git a/tests/sources/remote/single-file/target.bst b/tests/sources/remote/single-file/target.bst new file mode 100644 index 000000000..f4642e3aa --- /dev/null +++ b/tests/sources/remote/single-file/target.bst @@ -0,0 +1,6 @@ +kind: import +description: test +sources: +- kind: remote + url: tmpdir:/dir/file + ref: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 diff --git a/tests/sources/remote/unique-keys/dir/file b/tests/sources/remote/unique-keys/dir/file new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/sources/remote/unique-keys/dir/file diff --git a/tests/sources/remote/unique-keys/target-custom.bst b/tests/sources/remote/unique-keys/target-custom.bst new file mode 100644 index 000000000..cd00ab611 --- /dev/null +++ b/tests/sources/remote/unique-keys/target-custom.bst @@ -0,0 +1,7 @@ +kind: import +description: test +sources: +- kind: remote + url: tmpdir:/dir/file + ref: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 + filename: some-custom-file diff --git a/tests/sources/remote/unique-keys/target.bst b/tests/sources/remote/unique-keys/target.bst new file mode 100644 index 000000000..f4642e3aa --- /dev/null +++ b/tests/sources/remote/unique-keys/target.bst @@ -0,0 +1,6 @@ +kind: import +description: test +sources: +- kind: remote + url: tmpdir:/dir/file + ref: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 |