Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create the output directory if its missing and force is used, otherwise raise #295

Merged
merged 6 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions conda_pack/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ def _parcel_output(self, parcel_root, parcel_name, parcel_version, parcel_distro
dest_prefix = os.path.join(parcel_root, arcroot)
return dest_prefix, arcroot, triple

def _check_output_dir(self, output, force):
directory = os.path.dirname(output)
if (
not directory or # when passed just a filename, `directory` will be an empty string
os.path.exists(directory)
):
return
if force:
os.makedirs(directory)
return
raise CondaPackException(f"The target output diretory {directory} does not exist")

def pack(
self,
output=None,
Expand Down Expand Up @@ -386,6 +398,8 @@ def pack(
# Ensure the prefix is a relative path
arcroot = arcroot.strip(os.path.sep) if arcroot else ""

self._check_output_dir(output, force)

if os.path.exists(output) and not force:
raise CondaPackException("File %r already exists" % output)

Expand Down
20 changes: 20 additions & 0 deletions conda_pack/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ def test_ignore_errors_editable_packages():
CondaEnv.from_prefix(py37_editable_path, ignore_editable_packages=True)


def test_errors_when_target_directory_not_exists_and_not_force(py37_env):
with pytest.raises(CondaPackException) as exc:
py37_env.pack(output="not_a_real_directory/environment.tar.gz", force=False)

assert "not_a_real_directory" in str(exc.value)


def test_creates_directories_if_missing_and_force(tmpdir, py37_env):

target_directory = os.path.join(tmpdir, "not_a_real_directory/")

assert not os.path.exists(target_directory)

target_file = os.path.join(target_directory, "env.tar.gz")

py37_env.pack(output=target_file, force=True)

assert os.path.exists(target_directory)


def test_errors_pip_overwrites():
with pytest.raises(CondaPackException) as exc:
CondaEnv.from_prefix(py37_broken_path)
Expand Down