Skip to content

Commit 2d686fd

Browse files
committed
rimport: Print msg if file copied to staging but not relinked.
1 parent decf50a commit 2d686fd

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

rimport

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,9 @@ def stage_data(
178178
f"({staging_root})"
179179
)
180180
print(f"{INDENT}File is already published and linked.")
181-
if can_file_be_downloaded(src.resolve(), staging_root):
182-
print(f"{INDENT}File is available for download.")
183-
else:
184-
print(f"{INDENT}File is not (yet) available for download.")
185-
return
186-
187-
# TODO: Check whether file is published but not relinked.
188-
if check:
189-
print(f"{INDENT}File is not already published")
181+
print_can_file_be_downloaded(
182+
can_file_be_downloaded(src.resolve(), staging_root)
183+
)
190184
return
191185

192186
if not src.exists():
@@ -204,6 +198,20 @@ def stage_data(
204198
) from exc
205199

206200
dst = staging_root / rel
201+
202+
if dst.exists():
203+
print(f"{INDENT}File is already published but NOT linked; do")
204+
print(f"{2*INDENT}relink.py {rel}")
205+
print(f"{INDENT}to resolve.")
206+
print_can_file_be_downloaded(
207+
can_file_be_downloaded(rel, staging_root)
208+
)
209+
return
210+
211+
if check:
212+
print(f"{INDENT}File is not already published")
213+
return
214+
207215
dst.parent.mkdir(parents=True, exist_ok=True)
208216
shutil.copy2(src, dst)
209217
print(f"{INDENT}[rimport] staged {src} -> {dst}")
@@ -295,6 +303,18 @@ def can_file_be_downloaded(file_relpath: Path, staging_root: Path, timeout: floa
295303
return False
296304

297305

306+
def print_can_file_be_downloaded(file_can_be_downloaded: bool):
307+
"""Print a message indicating whether a file is available for download.
308+
309+
Args:
310+
file_can_be_downloaded: Boolean indicating if the file can be downloaded.
311+
"""
312+
if file_can_be_downloaded:
313+
print(f"{INDENT}File is available for download.")
314+
else:
315+
print(f"{INDENT}File is not (yet) available for download.")
316+
317+
298318
def main(argv: List[str] | None = None) -> int:
299319
"""Main entry point for the rimport tool.
300320

tests/rimport/test_stage_data.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,39 @@ def test_prints_live_symlink_already_published_is_downloadable(
154154
msg = "is already under staging directory"
155155
assert msg not in stdout
156156

157+
def test_prints_published_but_not_linked(
158+
self, inputdata_root, staging_root, capsys
159+
):
160+
"""
161+
Tests printed message for when a file has been published (copied to staging root) but not
162+
yet linked (inputdata version replaced with symlink to staging version).
163+
"""
164+
# Create a real file in staging AND in inputdata
165+
filename = "real_file.nc"
166+
staged = staging_root / filename
167+
staged.write_text("data")
168+
inputdata = inputdata_root / filename
169+
inputdata.write_text("data")
170+
171+
# Mock shutil.copy2 to verify it's never called
172+
with patch("shutil.copy2") as mock_copy:
173+
# Mock can_file_be_downloaded to return True
174+
with patch("rimport.can_file_be_downloaded", return_value=True):
175+
# Should print message for live symlink and return early
176+
rimport.stage_data(inputdata, inputdata_root, staging_root)
177+
178+
# Verify that shutil.copy2 was never called (function returned early)
179+
mock_copy.assert_not_called()
180+
181+
# Verify the right messages were printed or not
182+
stdout = capsys.readouterr().out.strip()
183+
msg = "File is already published and linked"
184+
assert msg not in stdout
185+
msg = "File is already published but NOT linked; do"
186+
assert msg in stdout
187+
msg = "File is available for download"
188+
assert msg in stdout
189+
157190
def test_raises_error_for_live_symlink_pointing_somewhere_other_than_staging(
158191
self, tmp_path, inputdata_root, staging_root
159192
):

0 commit comments

Comments
 (0)