Skip to content

Commit 4008d32

Browse files
committed
relink now indents like rimport.
1 parent 3571bb4 commit 4008d32

7 files changed

Lines changed: 40 additions & 29 deletions

File tree

relink.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
validate_directory,
2222
configure_logging,
2323
logger,
24+
INDENT,
2425
)
2526

2627
# Define a custom log level that always prints
@@ -237,7 +238,7 @@ def replace_one_file_with_symlink(inputdata_root, target_dir, file_path, dry_run
237238
file_path (str): The path of the file to be replaced.
238239
dry_run (bool): If True, only show what would be done without making changes.
239240
"""
240-
logger.info("Found owned file: %s", file_path)
241+
logger.info("'%s':", file_path)
241242

242243
# Determine the relative path and the new link's destination
243244
relative_path = os.path.relpath(file_path, inputdata_root)
@@ -246,9 +247,9 @@ def replace_one_file_with_symlink(inputdata_root, target_dir, file_path, dry_run
246247
# Check if the target file actually exists
247248
if not os.path.exists(link_target):
248249
logger.warning(
249-
"Warning: Corresponding file '%s' not found for '%s'. Skipping.",
250+
"%sWarning: Corresponding file '%s' not found. Skipping.",
251+
INDENT,
250252
link_target,
251-
file_path,
252253
)
253254
return
254255

@@ -257,7 +258,8 @@ def replace_one_file_with_symlink(inputdata_root, target_dir, file_path, dry_run
257258

258259
if dry_run:
259260
logger.info(
260-
"[DRY RUN] Would create symbolic link: %s -> %s",
261+
"%s[DRY RUN] Would create symbolic link: %s -> %s",
262+
INDENT,
261263
link_name,
262264
link_target,
263265
)
@@ -266,9 +268,9 @@ def replace_one_file_with_symlink(inputdata_root, target_dir, file_path, dry_run
266268
# Remove the original file
267269
try:
268270
os.rename(link_name, link_name + ".tmp")
269-
logger.info("Deleted original file: %s", link_name)
271+
logger.info("%sDeleted original file: %s", INDENT, link_name)
270272
except OSError as e:
271-
logger.error("Error deleting file %s: %s. Skipping.", link_name, e)
273+
logger.error("%sError deleting file %s: %s. Skipping.", INDENT, link_name, e)
272274
return
273275

274276
# Create the symbolic link, handling necessary parent directories
@@ -277,10 +279,10 @@ def replace_one_file_with_symlink(inputdata_root, target_dir, file_path, dry_run
277279
os.makedirs(os.path.dirname(link_name), exist_ok=True)
278280
os.symlink(link_target, link_name)
279281
os.remove(link_name + ".tmp")
280-
logger.info("Created symbolic link: %s -> %s", link_name, link_target)
282+
logger.info("%sCreated symbolic link: %s -> %s", INDENT, link_name, link_target)
281283
except OSError as e:
282284
os.rename(link_name + ".tmp", link_name)
283-
logger.error("Error creating symlink for %s: %s. Skipping.", link_name, e)
285+
logger.error("%sError creating symlink for %s: %s. Skipping.", INDENT, link_name, e)
284286

285287

286288
def parse_arguments():

rimport

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ from urllib.request import Request, urlopen
2020
from urllib.error import HTTPError
2121

2222
import shared
23+
INDENT = shared.INDENT
2324

2425
DEFAULT_INPUTDATA_ROOT = Path(shared.DEFAULT_INPUTDATA_ROOT)
2526
DEFAULT_STAGING_ROOT = Path(shared.DEFAULT_STAGING_ROOT)
2627
STAGE_OWNER = "cesmdata"
27-
INDENT = " "
2828
INPUTDATA_URL = "https://osdf-data.gdex.ucar.edu/ncar/gdex/d651077/cesmdata/inputdata"
2929

3030
# Configure logging

shared.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
DEFAULT_STAGING_ROOT = (
1212
"/glade/campaign/collections/gdex/data/d651077/cesmdata/inputdata/"
1313
)
14+
INDENT = " "
1415

1516
logger = logging.getLogger("rimport_relink")
1617

tests/relink/test_cmdline.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import pytest
1111

12+
from shared import INDENT
13+
1214

1315
@pytest.fixture(name="mock_dirs")
1416
def fixture_mock_dirs(tmp_path):
@@ -57,7 +59,7 @@ def test_command_line_execution_dry_run(mock_dirs):
5759

5860
# Verify dry-run messages in output
5961
assert "DRY RUN MODE" in result.stdout
60-
assert "[DRY RUN] Would create symbolic link:" in result.stdout
62+
assert f"{INDENT}[DRY RUN] Would create symbolic link:" in result.stdout
6163

6264
# Verify no actual changes were made
6365
assert source_file.is_file()
@@ -96,7 +98,7 @@ def test_command_line_execution_given_dir(mock_dirs):
9698
assert os.readlink(str(source_file)) == str(target_file)
9799

98100
# Verify success messages in output
99-
assert "Created symbolic link:" in result.stdout
101+
assert f"{INDENT}Created symbolic link:" in result.stdout
100102

101103

102104
def test_command_line_execution_given_file(mock_dirs):
@@ -131,7 +133,7 @@ def test_command_line_execution_given_file(mock_dirs):
131133
assert os.readlink(str(source_file)) == str(target_file)
132134

133135
# Verify success messages in output
134-
assert "Created symbolic link:" in result.stdout
136+
assert f"{INDENT}Created symbolic link:" in result.stdout
135137

136138

137139
def test_command_line_multiple_source_dirs(temp_dirs):

tests/relink/test_dryrun.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# pylint: disable=wrong-import-position
1717
import relink # noqa: E402
1818

19+
from shared import INDENT
20+
1921

2022
@pytest.fixture(name="dry_run_setup")
2123
def fixture_dry_run_setup(temp_dirs):
@@ -70,7 +72,7 @@ def test_dry_run_shows_message(dry_run_setup, caplog):
7072

7173
# Check that dry-run messages were logged
7274
assert "DRY RUN MODE" in caplog.text
73-
assert "[DRY RUN] Would create symbolic link:" in caplog.text
75+
assert f"{INDENT}[DRY RUN] Would create symbolic link:" in caplog.text
7476
assert f"{source_file} -> {target_file}" in caplog.text
7577

7678

@@ -85,7 +87,7 @@ def test_dry_run_no_delete_or_create_messages(dry_run_setup, caplog):
8587
)
8688

8789
# Verify actual operation messages are NOT logged
88-
assert "Deleted original file:" not in caplog.text
89-
assert "Created symbolic link:" not in caplog.text
90+
assert f"{INDENT}Deleted original file:" not in caplog.text
91+
assert f"{INDENT}Created symbolic link:" not in caplog.text
9092
# But the dry-run message should be there
91-
assert "[DRY RUN] Would create symbolic link: " in caplog.text
93+
assert f"{INDENT}[DRY RUN] Would create symbolic link: " in caplog.text

tests/relink/test_replace_one_file_with_symlink.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# pylint: disable=wrong-import-position
1515
import relink # noqa: E402
1616

17+
from shared import INDENT
18+
1719

1820
def test_basic_file_replacement(temp_dirs):
1921
"""Test basic functionality: replace owned file with symlink."""
@@ -83,7 +85,7 @@ def test_missing_target_file(temp_dirs, caplog):
8385
assert os.path.isfile(source_file), "Original file should still exist"
8486

8587
# Check warning message
86-
assert "Warning: Corresponding file " in caplog.text
88+
assert f"{INDENT}Warning: Corresponding file " in caplog.text
8789
assert " not found" in caplog.text
8890

8991

@@ -117,7 +119,7 @@ def test_absolute_paths(temp_dirs):
117119

118120

119121
def test_print_found_owned_file(temp_dirs, caplog):
120-
"""Test that 'Found owned file' message is printed."""
122+
"""Test that message with filename is printed."""
121123
source_dir, target_dir = temp_dirs
122124

123125
# Create a file owned by current user
@@ -133,8 +135,8 @@ def test_print_found_owned_file(temp_dirs, caplog):
133135
with caplog.at_level(logging.INFO):
134136
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
135137

136-
# Check that "Found owned file" message was logged
137-
assert "Found owned file:" in caplog.text
138+
# Check that message was logged
139+
assert f"'{source_file}':" in caplog.text
138140
assert source_file in caplog.text
139141

140142

@@ -156,8 +158,8 @@ def test_print_deleted_and_created_messages(temp_dirs, caplog):
156158
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
157159

158160
# Check messages
159-
assert "Deleted original file:" in caplog.text
160-
assert "Created symbolic link:" in caplog.text
161+
assert f"{INDENT}Deleted original file:" in caplog.text
162+
assert f"{INDENT}Created symbolic link:" in caplog.text
161163
assert f"{source_file} -> {target_file}" in caplog.text
162164

163165

@@ -184,7 +186,7 @@ def mock_symlink(src, dst):
184186
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
185187

186188
# Check error message
187-
assert "Error creating symlink" in caplog.text
189+
assert f"{INDENT}Error creating symlink" in caplog.text
188190
assert source_file in caplog.text
189191

190192

@@ -254,5 +256,5 @@ def mock_rename(src, dst):
254256
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
255257

256258
# Check error message
257-
assert "Error deleting file" in caplog.text
259+
assert f"{INDENT}Error deleting file" in caplog.text
258260
assert source_file in caplog.text

tests/relink/test_verbosity.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# pylint: disable=wrong-import-position
1616
import relink # noqa: E402
1717

18+
from shared import INDENT
19+
1820

1921
def test_quiet_mode_suppresses_info_messages(temp_dirs, caplog):
2022
"""Test that quiet mode suppresses INFO level messages."""
@@ -45,8 +47,8 @@ def test_quiet_mode_suppresses_info_messages(temp_dirs, caplog):
4547
assert "Searching for files owned by" not in caplog.text
4648
assert "Skipping symlink:" not in caplog.text
4749
assert "Found owned file:" not in caplog.text
48-
assert "Deleted original file:" not in caplog.text
49-
assert "Created symbolic link:" not in caplog.text
50+
assert f"{INDENT}Deleted original file:" not in caplog.text
51+
assert f"{INDENT}Created symbolic link:" not in caplog.text
5052

5153

5254
def test_quiet_mode_shows_warnings(temp_dirs, caplog):
@@ -66,7 +68,7 @@ def test_quiet_mode_shows_warnings(temp_dirs, caplog):
6668
)
6769

6870
# Verify WARNING message IS in the log
69-
assert "Warning: Corresponding file" in caplog.text
71+
assert f"{INDENT}Warning: Corresponding file" in caplog.text
7072
assert "not found" in caplog.text
7173

7274

@@ -104,7 +106,7 @@ def mock_rename(src, dst):
104106
relink.replace_files_with_symlinks(
105107
source_dir, target_dir, username, inputdata_root=source_dir
106108
)
107-
assert "Error deleting file" in caplog.text
109+
assert f"{INDENT}Error deleting file" in caplog.text
108110

109111
# Clear the log for next test
110112
caplog.clear()
@@ -126,4 +128,4 @@ def mock_symlink(src, dst):
126128
relink.replace_files_with_symlinks(
127129
source_dir, target_dir, username, inputdata_root=source_dir
128130
)
129-
assert "Error creating symlink" in caplog.text
131+
assert f"{INDENT}Error creating symlink" in caplog.text

0 commit comments

Comments
 (0)