From 863699f5aa0d611b1afccbcec9dda6fdb4bbcdc7 Mon Sep 17 00:00:00 2001 From: yadavchiragg Date: Wed, 25 Nov 2026 08:52:26 +0530 Subject: [PATCH] build: fail when the output directory exists and add --force to replace a generated study --- concore_cli/README.md | 1 + concore_cli/cli.py | 8 ++- concore_cli/commands/build.py | 55 ++++++++++++++++-- tests/test_build_force.py | 105 ++++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+), 6 deletions(-) create mode 100644 tests/test_build_force.py diff --git a/concore_cli/README.md b/concore_cli/README.md index 78f98b1a..0f470a7f 100644 --- a/concore_cli/README.md +++ b/concore_cli/README.md @@ -63,6 +63,7 @@ Compiles a concore workflow GraphML file into executable scripts (POSIX, Windows - `-t, --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 diff --git a/concore_cli/cli.py b/concore_cli/cli.py index dc8fca34..2f6e92fe 100644 --- a/concore_cli/cli.py +++ b/concore_cli/cli.py @@ -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") @@ -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)}") diff --git a/concore_cli/commands/build.py b/concore_cli/commands/build.py index aeb2e50b..4d117af8 100644 --- a/concore_cli/commands/build.py +++ b/concore_cli/commands/build.py @@ -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, @@ -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() @@ -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}") diff --git a/tests/test_build_force.py b/tests/test_build_force.py new file mode 100644 index 00000000..3164aa34 --- /dev/null +++ b/tests/test_build_force.py @@ -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()