Skip to content
Snippets Groups Projects
Commit b37d79c3 authored by Phil Dawson's avatar Phil Dawson
Browse files

Add --force / -f option to source-checkout command

parent c4718b60
No related branches found
No related tags found
No related merge requests found
......@@ -684,6 +684,8 @@ def checkout(app, element, location, force, deps, integrate, hardlinks, tar):
# Source Checkout Command #
##################################################################
@cli.command(name='source-checkout', short_help='Checkout sources for an element')
@click.option('--force', '-f', default=False, is_flag=True,
help="Allow files to be overwritten")
@click.option('--except', 'except_', multiple=True,
type=click.Path(readable=False),
help="Except certain dependencies")
......@@ -699,12 +701,13 @@ def checkout(app, element, location, force, deps, integrate, hardlinks, tar):
type=click.Path(readable=False))
@click.argument('location', type=click.Path())
@click.pass_obj
def source_checkout(app, element, location, deps, fetch_, except_, tar):
def source_checkout(app, element, location, force, deps, fetch_, except_, tar):
"""Checkout sources of an element to the specified location
"""
with app.initialized():
app.stream.source_checkout(element,
location=location,
force=force,
deps=deps,
fetch=fetch_,
except_targets=except_,
......
......@@ -449,12 +449,13 @@ class Stream():
#
def source_checkout(self, target, *,
location=None,
force=False,
deps='none',
fetch=False,
except_targets=(),
tar=False):
self._check_location_writable(location, tar=tar)
self._check_location_writable(location, force=force, tar=tar)
elements, _ = self._load((target,), (),
selection=deps,
......@@ -468,7 +469,7 @@ class Stream():
# Stage all sources determined by scope
try:
self._source_checkout(elements, location, deps, fetch, tar)
self._source_checkout(elements, location, force, deps, fetch, tar)
except BstError as e:
raise StreamError("Error while writing sources"
": '{}'".format(e), detail=e.detail, reason=e.reason) from e
......@@ -1189,6 +1190,7 @@ class Stream():
# Helper function for source_checkout()
def _source_checkout(self, elements,
location=None,
force=False,
deps='none',
fetch=False,
tar=False):
......@@ -1204,7 +1206,7 @@ class Stream():
if tar:
self._create_tarball(temp_source_dir.name, location)
else:
self._move_directory(temp_source_dir.name, location)
self._move_directory(temp_source_dir.name, location, force)
except OSError as e:
raise StreamError("Failed to checkout sources to {}: {}"
.format(location, e)) from e
......@@ -1212,10 +1214,9 @@ class Stream():
with suppress(FileNotFoundError):
temp_source_dir.cleanup()
# Move a directory src to dest. This will work across devices and
# may optionaly overwrite existing files.
def _move_directory(self, src, dest):
def _move_directory(self, src, dest, force=False):
def is_empty_dir(path):
return os.path.isdir(dest) and not os.listdir(dest)
......@@ -1225,7 +1226,7 @@ class Stream():
except OSError:
pass
if is_empty_dir(dest):
if force or is_empty_dir(dest):
try:
utils.link_files(src, dest)
except utils.UtilError as e:
......
import os
import pytest
import tarfile
from pathlib import Path
from tests.testutils import cli
......@@ -47,6 +48,22 @@ def test_source_checkout(datafiles, tmpdir_factory, cli, with_workspace):
assert os.path.exists(os.path.join(checkout, 'checkout-deps', 'etc', 'buildstream', 'config'))
@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize('force_flag', ['--force', '-f'])
def test_source_checkout_force(datafiles, cli, force_flag):
project = os.path.join(datafiles.dirname, datafiles.basename)
checkout = os.path.join(cli.directory, 'source-checkout')
target = 'checkout-deps.bst'
os.makedirs(os.path.join(checkout, 'some-thing'))
# Path(os.path.join(checkout, 'some-file')).touch()
result = cli.run(project=project, args=['source-checkout', force_flag, target, '--deps', 'none', checkout])
result.assert_success()
assert os.path.exists(os.path.join(checkout, 'checkout-deps', 'etc', 'buildstream', 'config'))
@pytest.mark.datafiles(DATA_DIR)
def test_source_checkout_tar(datafiles, cli):
project = os.path.join(datafiles.dirname, datafiles.basename)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment