Skip to content

Commit

Permalink
#337: don't create folders in case of dry-run, change input arg and u…
Browse files Browse the repository at this point in the history
…pdate README
  • Loading branch information
mortenwh committed Oct 2, 2024
1 parent ab47412 commit 11ac8e1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ module can be extended with other necessary data management tools. Moving
can be done with the `move_data` script, e.g.:

```
move_data /path/to/files-from-git/mmd-xml-<env> /path/to/new/storage "/path/to/data/files/*.nc" --dmci-update
move_data /path/to/files-from-git/mmd-xml-<env> /path/to/old/storage /path/to/new/storage "%Y/%m/%d/*.nc" --dmci-update
```

The two last arguments provide a search pattern in case the netCDF files are
stored in subfolders, and to directly updated the metadata catalog,
respectively. If --dmci-update is not provided, local MMD files will not be
pushed to the catalog.

# Installation

To avoid problems with conflicting versions, we recommend using the [Conda](
Expand Down
7 changes: 4 additions & 3 deletions py_mmd_tools/mmd_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def move_data(mmd_repository_path, old_file_location_base, new_file_location_bas
for nc_file in existing:
# Error message
emsg = ""
nfl = new_file_location(nc_file, new_file_location_base, old_file_location_base)
nfl = new_file_location(nc_file, new_file_location_base, old_file_location_base, dry_run)
mmd_orig = get_local_mmd_git_path(nc_file, mmd_repository_path)

# Check permissions before doing anything
Expand Down Expand Up @@ -216,13 +216,14 @@ def move_data(mmd_repository_path, old_file_location_base, new_file_location_bas
return not_updated, updated


def new_file_location(nc_file, new_base_loc, existing_base_loc):
def new_file_location(nc_file, new_base_loc, existing_base_loc, dry_run):
"""Return the name of the new folder where the netcdf file will be
stored. Subfolders of new_base_loc will be created.
"""
if not os.path.isdir(new_base_loc):
raise ValueError(f"Folder does not exist: {new_base_loc}")
file_path = nc_file.replace(existing_base_loc, new_base_loc)
new_folder = os.path.dirname(os.path.abspath(file_path))
os.makedirs(new_folder)
if not dry_run:
os.makedirs(new_folder)
return new_folder
8 changes: 4 additions & 4 deletions py_mmd_tools/script/move_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def create_parser():
"new_file_location_base", type=str,
help="Base or exact path to the folder to which the data file(s) will be moved.")
parser.add_argument(
"--ext_pattern", type=str, default=None,
"--ext-pattern", type=str, default=None,
help="Pathname pattern extending old_file_location_base, i.e., extending the "
"existing file *base* location(s). Allows parsing date/times from a path "
"given a glob pattern intertwined with date/time format akin to "
"strptime/strftime format.")
"existing file *base* location(s) with, e.g, the year and month as a "
"glob pattern intertwined with date/time format akin to "
"strptime/strftime format (e.g., '%Y/%m').")
parser.add_argument(
'--dmci-update', action='store_true',
help='Directly update the online catalog with the changed MMD files.'
Expand Down
4 changes: 2 additions & 2 deletions tests/test_mmd_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ def test_new_file_location(monkeypatch):
existing_base_loc = "/some/old/loc"

with pytest.raises(ValueError):
new_file_location(file, new_base, existing_base_loc)
new_file_location(file, new_base, existing_base_loc, True)

with monkeypatch.context() as mp:
mp.setattr("py_mmd_tools.mmd_operations.os.path.isdir", lambda *a, **k: True)
mp.setattr("py_mmd_tools.mmd_operations.os.makedirs", lambda *a, **k: None)
assert new_file_location(file, new_base, existing_base_loc) == \
assert new_file_location(file, new_base, existing_base_loc, False) == \
"/some/where/else/2024/06/19"


Expand Down

0 comments on commit 11ac8e1

Please sign in to comment.