Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions concore_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Compiles a concore workflow GraphML file into executable scripts (POSIX, Windows
- `-t, --type <type>` - Execution type: windows, posix, or docker (default: windows)
- `--auto-build` - Automatically run build script after generation
- `--compose` - Generate `docker-compose.yml` (only valid with `--type docker`)
- `--force` - Replace the output directory if it already holds a study generated by concore (refuses any other directory)

**Example:**
```bash
Expand Down
8 changes: 7 additions & 1 deletion concore_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ def init(name, template, interactive):
is_flag=True,
help="Configure compose for ZMQ networking mode (requires --compose)",
)
def build(workflow_file, source, output, type, auto_build, compose, zmq):
@click.option(
"--force",
is_flag=True,
help="Replace the output directory if it holds a previously generated study",
)
def build(workflow_file, source, output, type, auto_build, compose, zmq, force):
"""Compile a concore workflow into executable scripts"""
if zmq and not compose:
console.print("[red]Error:[/red] --zmq requires --compose")
Expand All @@ -95,6 +100,7 @@ def build(workflow_file, source, output, type, auto_build, compose, zmq):
console,
compose=compose,
zmq_mode=zmq,
force=force,
)
except Exception as e:
console.print(f"[red]Error:[/red] {str(e)}")
Expand Down
55 changes: 50 additions & 5 deletions concore_cli/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,47 @@ def _write_docker_compose(output_path, console, zmq_mode=False):
return compose_path


def _is_generated_study(path):
"""A study made by mkconcore has a build script and a run script."""
has_build = (path / "build").is_file() or (path / "build.bat").is_file()
has_run = (path / "run").is_file() or (path / "run.bat").is_file()
return has_build and has_run


def _remove_generated_study(output_path, workflow_path, source_path):
"""Delete a previously generated study so it can be rebuilt (--force).

Refuses anything that does not look like a generated study, and any
directory that holds the user's own files (current directory, home,
the workflow or the sources).
"""
if not output_path.is_dir():
raise FileExistsError(
f"'{output_path}' exists and is not a directory; refusing to replace it"
)

cwd = Path.cwd().resolve()
protected = [Path.home().resolve(), Path(output_path.anchor)]
for keep in (cwd, workflow_path, source_path):
protected.append(keep)
if output_path in keep.parents:
raise FileExistsError(
f"Refusing to replace '{output_path}': it contains '{keep}'"
)
if output_path in protected:
raise FileExistsError(
f"Refusing to replace '{output_path}': it is not a generated study directory"
)

if not _is_generated_study(output_path):
raise FileExistsError(
f"Refusing to replace '{output_path}': it does not look like a study "
"generated by concore (no build and run scripts)"
)

shutil.rmtree(output_path)


def build_workflow(
workflow_file,
source,
Expand All @@ -161,6 +202,7 @@ def build_workflow(
console,
compose=False,
zmq_mode=False,
force=False,
):
workflow_path = Path(workflow_file).resolve()
source_path = resolve_source_dir(workflow_path, source).resolve()
Expand All @@ -170,11 +212,14 @@ def build_workflow(
raise FileNotFoundError(f"Source directory '{source}' not found")

if output_path.exists():
console.print(
f"[yellow]Warning:[/yellow] Output directory '{output}' already exists"
)
console.print("Remove it first or choose a different output directory")
return
if not force:
raise FileExistsError(
f"Output directory '{output}' already exists. Remove it, choose a "
"different output directory, or use --force to replace a "
"previously generated study"
)
_remove_generated_study(output_path, workflow_path, source_path)
console.print(f"[yellow]Replaced existing study:[/yellow] {output_path}")

console.print(f"[cyan]Workflow:[/cyan] {workflow_path.name}")
console.print(f"[cyan]Source:[/cyan] {source_path}")
Expand Down
105 changes: 105 additions & 0 deletions tests/test_build_force.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import shutil
import tempfile
import unittest
from pathlib import Path

from click.testing import CliRunner

from concore_cli.cli import cli


class TestBuildForce(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()
self.temp_dir = tempfile.mkdtemp()

def tearDown(self):
shutil.rmtree(self.temp_dir, ignore_errors=True)

def _project(self):
result = self.runner.invoke(cli, ["init", "proj"])
self.assertEqual(result.exit_code, 0, result.output)
return [
"build",
"proj/workflow.graphml",
"--source",
"proj/src",
"--type",
"posix",
]

def test_existing_output_is_an_error_without_force(self):
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
args = self._project()
Path("out").mkdir()
(Path("out") / "keep.txt").write_text("x")

result = self.runner.invoke(cli, args + ["--output", "out"])

self.assertNotEqual(result.exit_code, 0)
self.assertIn("already exists", result.output)
self.assertIn("--force", result.output)
self.assertTrue((Path("out") / "keep.txt").exists())

def test_force_replaces_a_generated_study(self):
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
args = self._project()
first = self.runner.invoke(cli, args + ["--output", "out"])
self.assertEqual(first.exit_code, 0, first.output)
(Path("out") / "stale.txt").write_text("old")

second = self.runner.invoke(cli, args + ["--output", "out", "--force"])

self.assertEqual(second.exit_code, 0, second.output)
self.assertFalse((Path("out") / "stale.txt").exists())
self.assertTrue((Path("out") / "run").exists())

def test_force_refuses_a_directory_that_is_not_a_study(self):
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
args = self._project()
Path("mydata").mkdir()
(Path("mydata") / "important.txt").write_text("keep me")

result = self.runner.invoke(cli, args + ["--output", "mydata", "--force"])

self.assertNotEqual(result.exit_code, 0)
self.assertIn("does not look like a study", result.output)
self.assertTrue((Path("mydata") / "important.txt").exists())

def test_force_refuses_the_current_directory(self):
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
args = self._project()
# Even if it looks like a study, the current directory is never deleted
(Path("build")).write_text("")
(Path("run")).write_text("")

result = self.runner.invoke(cli, args + ["--output", ".", "--force"])

self.assertNotEqual(result.exit_code, 0)
self.assertTrue((Path("proj") / "workflow.graphml").exists())

def test_force_refuses_a_directory_containing_the_sources(self):
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
args = self._project()
(Path("proj") / "build").write_text("")
(Path("proj") / "run").write_text("")

result = self.runner.invoke(cli, args + ["--output", "proj", "--force"])

self.assertNotEqual(result.exit_code, 0)
self.assertIn("contains", result.output)
self.assertTrue((Path("proj") / "workflow.graphml").exists())

def test_force_refuses_a_regular_file(self):
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
args = self._project()
Path("out").write_text("not a directory")

result = self.runner.invoke(cli, args + ["--output", "out", "--force"])

self.assertNotEqual(result.exit_code, 0)
self.assertEqual(Path("out").read_text(), "not a directory")


if __name__ == "__main__":
unittest.main()