Skip to content

Commit 385ef89

Browse files
authored
Merge pull request #718 from yadavchiragg/feat/build-force
build: fail when the output directory exists; add --force to replace a generated study
2 parents e968065 + 863699f commit 385ef89

4 files changed

Lines changed: 163 additions & 6 deletions

File tree

‎concore_cli/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Compiles a concore workflow GraphML file into executable scripts (POSIX, Windows
7272
- `-t, --type <type>` - Execution type: windows, posix, or docker (default: windows)
7373
- `--auto-build` - Automatically run build script after generation
7474
- `--compose` - Generate `docker-compose.yml` (only valid with `--type docker`)
75+
- `--force` - Replace the output directory if it already holds a study generated by concore (refuses any other directory)
7576

7677
**Example:**
7778
```bash

‎concore_cli/cli.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ def init(name, template, interactive):
8686
is_flag=True,
8787
help="Configure compose for ZMQ networking mode (requires --compose)",
8888
)
89-
def build(workflow_file, source, output, type, auto_build, compose, zmq):
89+
@click.option(
90+
"--force",
91+
is_flag=True,
92+
help="Replace the output directory if it holds a previously generated study",
93+
)
94+
def build(workflow_file, source, output, type, auto_build, compose, zmq, force):
9095
"""Compile a concore workflow into executable scripts"""
9196
if zmq and not compose:
9297
console.print("[red]Error:[/red] --zmq requires --compose")
@@ -101,6 +106,7 @@ def build(workflow_file, source, output, type, auto_build, compose, zmq):
101106
console,
102107
compose=compose,
103108
zmq_mode=zmq,
109+
force=force,
104110
)
105111
except Exception as e:
106112
console.print(f"[red]Error:[/red] {str(e)}")

‎concore_cli/commands/build.py‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,47 @@ def _write_docker_compose(output_path, console, zmq_mode=False):
179179
return compose_path
180180

181181

182+
def _is_generated_study(path):
183+
"""A study made by mkconcore has a build script and a run script."""
184+
has_build = (path / "build").is_file() or (path / "build.bat").is_file()
185+
has_run = (path / "run").is_file() or (path / "run.bat").is_file()
186+
return has_build and has_run
187+
188+
189+
def _remove_generated_study(output_path, workflow_path, source_path):
190+
"""Delete a previously generated study so it can be rebuilt (--force).
191+
192+
Refuses anything that does not look like a generated study, and any
193+
directory that holds the user's own files (current directory, home,
194+
the workflow or the sources).
195+
"""
196+
if not output_path.is_dir():
197+
raise FileExistsError(
198+
f"'{output_path}' exists and is not a directory; refusing to replace it"
199+
)
200+
201+
cwd = Path.cwd().resolve()
202+
protected = [Path.home().resolve(), Path(output_path.anchor)]
203+
for keep in (cwd, workflow_path, source_path):
204+
protected.append(keep)
205+
if output_path in keep.parents:
206+
raise FileExistsError(
207+
f"Refusing to replace '{output_path}': it contains '{keep}'"
208+
)
209+
if output_path in protected:
210+
raise FileExistsError(
211+
f"Refusing to replace '{output_path}': it is not a generated study directory"
212+
)
213+
214+
if not _is_generated_study(output_path):
215+
raise FileExistsError(
216+
f"Refusing to replace '{output_path}': it does not look like a study "
217+
"generated by concore (no build and run scripts)"
218+
)
219+
220+
shutil.rmtree(output_path)
221+
222+
182223
def build_workflow(
183224
workflow_file,
184225
source,
@@ -188,6 +229,7 @@ def build_workflow(
188229
console,
189230
compose=False,
190231
zmq_mode=False,
232+
force=False,
191233
):
192234
workflow_path = Path(workflow_file).resolve()
193235
source_path = resolve_source_dir(workflow_path, source).resolve()
@@ -197,11 +239,14 @@ def build_workflow(
197239
raise FileNotFoundError(f"Source directory '{source}' not found")
198240

199241
if output_path.exists():
200-
console.print(
201-
f"[yellow]Warning:[/yellow] Output directory '{output}' already exists"
202-
)
203-
console.print("Remove it first or choose a different output directory")
204-
return
242+
if not force:
243+
raise FileExistsError(
244+
f"Output directory '{output}' already exists. Remove it, choose a "
245+
"different output directory, or use --force to replace a "
246+
"previously generated study"
247+
)
248+
_remove_generated_study(output_path, workflow_path, source_path)
249+
console.print(f"[yellow]Replaced existing study:[/yellow] {output_path}")
205250

206251
console.print(f"[cyan]Workflow:[/cyan] {workflow_path.name}")
207252
console.print(f"[cyan]Source:[/cyan] {source_path}")

‎tests/test_build_force.py‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import shutil
2+
import tempfile
3+
import unittest
4+
from pathlib import Path
5+
6+
from click.testing import CliRunner
7+
8+
from concore_cli.cli import cli
9+
10+
11+
class TestBuildForce(unittest.TestCase):
12+
def setUp(self):
13+
self.runner = CliRunner()
14+
self.temp_dir = tempfile.mkdtemp()
15+
16+
def tearDown(self):
17+
shutil.rmtree(self.temp_dir, ignore_errors=True)
18+
19+
def _project(self):
20+
result = self.runner.invoke(cli, ["init", "proj"])
21+
self.assertEqual(result.exit_code, 0, result.output)
22+
return [
23+
"build",
24+
"proj/workflow.graphml",
25+
"--source",
26+
"proj/src",
27+
"--type",
28+
"posix",
29+
]
30+
31+
def test_existing_output_is_an_error_without_force(self):
32+
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
33+
args = self._project()
34+
Path("out").mkdir()
35+
(Path("out") / "keep.txt").write_text("x")
36+
37+
result = self.runner.invoke(cli, args + ["--output", "out"])
38+
39+
self.assertNotEqual(result.exit_code, 0)
40+
self.assertIn("already exists", result.output)
41+
self.assertIn("--force", result.output)
42+
self.assertTrue((Path("out") / "keep.txt").exists())
43+
44+
def test_force_replaces_a_generated_study(self):
45+
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
46+
args = self._project()
47+
first = self.runner.invoke(cli, args + ["--output", "out"])
48+
self.assertEqual(first.exit_code, 0, first.output)
49+
(Path("out") / "stale.txt").write_text("old")
50+
51+
second = self.runner.invoke(cli, args + ["--output", "out", "--force"])
52+
53+
self.assertEqual(second.exit_code, 0, second.output)
54+
self.assertFalse((Path("out") / "stale.txt").exists())
55+
self.assertTrue((Path("out") / "run").exists())
56+
57+
def test_force_refuses_a_directory_that_is_not_a_study(self):
58+
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
59+
args = self._project()
60+
Path("mydata").mkdir()
61+
(Path("mydata") / "important.txt").write_text("keep me")
62+
63+
result = self.runner.invoke(cli, args + ["--output", "mydata", "--force"])
64+
65+
self.assertNotEqual(result.exit_code, 0)
66+
self.assertIn("does not look like a study", result.output)
67+
self.assertTrue((Path("mydata") / "important.txt").exists())
68+
69+
def test_force_refuses_the_current_directory(self):
70+
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
71+
args = self._project()
72+
# Even if it looks like a study, the current directory is never deleted
73+
(Path("build")).write_text("")
74+
(Path("run")).write_text("")
75+
76+
result = self.runner.invoke(cli, args + ["--output", ".", "--force"])
77+
78+
self.assertNotEqual(result.exit_code, 0)
79+
self.assertTrue((Path("proj") / "workflow.graphml").exists())
80+
81+
def test_force_refuses_a_directory_containing_the_sources(self):
82+
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
83+
args = self._project()
84+
(Path("proj") / "build").write_text("")
85+
(Path("proj") / "run").write_text("")
86+
87+
result = self.runner.invoke(cli, args + ["--output", "proj", "--force"])
88+
89+
self.assertNotEqual(result.exit_code, 0)
90+
self.assertIn("contains", result.output)
91+
self.assertTrue((Path("proj") / "workflow.graphml").exists())
92+
93+
def test_force_refuses_a_regular_file(self):
94+
with self.runner.isolated_filesystem(temp_dir=self.temp_dir):
95+
args = self._project()
96+
Path("out").write_text("not a directory")
97+
98+
result = self.runner.invoke(cli, args + ["--output", "out", "--force"])
99+
100+
self.assertNotEqual(result.exit_code, 0)
101+
self.assertEqual(Path("out").read_text(), "not a directory")
102+
103+
104+
if __name__ == "__main__":
105+
unittest.main()

0 commit comments

Comments
 (0)