diff options
author | bst-marge-bot <marge-bot@buildstream.build> | 2019-04-17 03:18:29 +0000 |
---|---|---|
committer | bst-marge-bot <marge-bot@buildstream.build> | 2019-04-17 03:18:29 +0000 |
commit | 316a0e493dc661f2f22d3f1277ee4319cfe652d9 (patch) | |
tree | 109707eaf268fbf104e3ec2cdad337f2fde81c3d /buildstream/testing/repo.py | |
parent | 9d7d803bbefed364ad58b4b5b5305f4e210bb5e1 (diff) | |
parent | 97bbedbef15fc9edea5b04f66a30ec788da7f130 (diff) | |
download | buildstream-316a0e493dc661f2f22d3f1277ee4319cfe652d9.tar.gz |
Merge branch 'phil/rename-plugintestutils' into 'master'
Rename 'plugintestutils' package to 'testing'
See merge request BuildStream/buildstream!1291
Diffstat (limited to 'buildstream/testing/repo.py')
-rw-r--r-- | buildstream/testing/repo.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/buildstream/testing/repo.py b/buildstream/testing/repo.py new file mode 100644 index 000000000..c1538685d --- /dev/null +++ b/buildstream/testing/repo.py @@ -0,0 +1,109 @@ +# +# Copyright (C) 2016-2018 Codethink Limited +# Copyright (C) 2019 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/>. + +""" +Repo - Utility class for testing source plugins +=============================================== + + +""" +import os +import shutil + + +class Repo(): + """Repo() + + Abstract class providing scaffolding for generating data to be + used with various sources. Subclasses of Repo may be registered to + run through the suite of generic source plugin tests provided in + buildstream.testing. + + Args: + directory (str): The base temp directory for the test + subdir (str): The subdir for the repo, in case there is more than one + + """ + def __init__(self, directory, subdir='repo'): + + # The working directory for the repo object + # + self.directory = os.path.abspath(directory) + + # The directory the actual repo will be stored in + self.repo = os.path.join(self.directory, subdir) + + os.makedirs(self.repo, exist_ok=True) + + def create(self, directory): + """Create a repository in self.directory and add the initial content + + Args: + directory: A directory with content to commit + + Returns: + (smth): A new ref corresponding to this commit, which can + be passed as the ref in the Repo.source_config() API. + """ + raise NotImplementedError("create method has not been implemeted") + + def source_config(self, ref=None): + """ + Args: + ref (smth): An optional abstract ref object, usually a string. + + Returns: + (dict): A configuration which can be serialized as a + source when generating an element file on the fly + + """ + raise NotImplementedError("source_config method has not been implemeted") + + def copy_directory(self, src, dest): + """ Copies the content of src to the directory dest + + Like shutil.copytree(), except dest is expected + to exist. + + Args: + src (str): The source directory + dest (str): The destination directory + """ + for filename in os.listdir(src): + src_path = os.path.join(src, filename) + dest_path = os.path.join(dest, filename) + if os.path.isdir(src_path): + shutil.copytree(src_path, dest_path) + else: + shutil.copy2(src_path, dest_path) + + def copy(self, dest): + """Creates a copy of this repository in the specified destination. + + Args: + dest (str): The destination directory + + Returns: + (Repo): A Repo object for the new repository. + """ + subdir = self.repo[len(self.directory):].lstrip(os.sep) + new_dir = os.path.join(dest, subdir) + os.makedirs(new_dir, exist_ok=True) + self.copy_directory(self.repo, new_dir) + repo_type = type(self) + new_repo = repo_type(dest, subdir) + return new_repo |