test: add method to copy source data from cache

Add a new method that will copy the downloaded data for a specified
source to a directory, creating a directory structure so that the
target directory can then be used to initialize the osbuild cache,
i.e. the target directory be used for the `cache_from` parameter in
the `test.OSBuild` constructor.
The main reason why this is done per source, and not for all sources,
is that not all source can be copied via a plain `cp` operation or
easily added to, in case that the `target` directory already contains
data. The `org.osbuild.ostree` source is such an example.
This commit is contained in:
Christian Kellner 2020-10-09 13:58:51 +02:00 committed by David Rheinsberg
parent 0e6b4749f7
commit 9e5e179128

View file

@ -366,3 +366,25 @@ class OSBuild(contextlib.AbstractContextManager):
# as a context-manager so the caller does not retain the path for
# later access.
yield path
def copy_source_data(self, target, source):
"""Copy the cached sources for a `source` to `target`
This will copy all the downloaded data for a specified source
to the `target` directory, with a folder structure in a way so
it can be used to initialize the cache, via the constructor's
`cache_from` argument. Does nothing if there is no downloaded
data for the specified `source`.
"""
from_path = os.path.join(self._cachedir, "sources", source)
if not os.path.isdir(from_path):
return
to_path = os.path.join(target, "sources", source)
os.makedirs(to_path, exist_ok=True)
subprocess.run(["cp", "--reflink=auto", "-a",
os.path.join(from_path, "."),
to_path],
check=True)