Skip to content

Commit

Permalink
Merge pull request #259 from kjsanger/bug/safe-remove-script-options
Browse files Browse the repository at this point in the history
Bug: add default for --output CLI option of safe-remove-script
  • Loading branch information
dkj authored Mar 2, 2024
2 parents c36960f + 387dce4 commit 7fbf826
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/npg_irods/cli/apply_ont_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -81,7 +81,7 @@
"--db-config",
"--db_config",
help="Configuration file for database connection.",
type=argparse.FileType("r"),
type=argparse.FileType("r", encoding="UTF-8"),
required=True,
)

Expand Down
4 changes: 2 additions & 2 deletions src/npg_irods/cli/check_checksums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2022, 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2022, 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -61,7 +61,7 @@
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions src/npg_irods/cli/check_common_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2022, 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2022, 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -60,7 +60,7 @@
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions src/npg_irods/cli/check_consent_withdrawn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -69,7 +69,7 @@
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions src/npg_irods/cli/check_replicas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -62,7 +62,7 @@
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
6 changes: 3 additions & 3 deletions src/npg_irods/cli/repair_checksums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2022, 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2022, 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -59,14 +59,14 @@
"-i",
"--input",
help="Input filename.",
type=argparse.FileType("r"),
type=argparse.FileType("r", encoding="UTF-8"),
default=sys.stdin,
)
parser.add_argument(
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions src/npg_irods/cli/repair_common_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2022, 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2022, 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -57,7 +57,7 @@
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
6 changes: 3 additions & 3 deletions src/npg_irods/cli/repair_replicas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -51,14 +51,14 @@
"-i",
"--input",
help="Input filename.",
type=argparse.FileType("r"),
type=argparse.FileType("r", encoding="UTF-8"),
default=sys.stdin,
)
parser.add_argument(
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
5 changes: 4 additions & 1 deletion src/npg_irods/cli/safe_remove_script.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2022, 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2022, 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -18,6 +18,7 @@
# @author Keith James <[email protected]>

import argparse
import sys

import structlog

Expand Down Expand Up @@ -57,6 +58,8 @@
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
"--echo-commands",
Expand Down
6 changes: 3 additions & 3 deletions src/npg_irods/cli/update_secondary_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -70,14 +70,14 @@ def main():
"-i",
"--input",
help="Input filename.",
type=argparse.FileType("r"),
type=argparse.FileType("r", encoding="UTF-8"),
default=sys.stdin,
)
parser.add_argument(
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
6 changes: 3 additions & 3 deletions src/npg_irods/cli/withdraw_consent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2023 Genome Research Ltd. All rights reserved.
# Copyright © 2023, 2024 Genome Research Ltd. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -54,14 +54,14 @@
"-i",
"--input",
help="Input filename.",
type=argparse.FileType("r"),
type=argparse.FileType("r", encoding="UTF-8"),
default=sys.stdin,
)
parser.add_argument(
"-o",
"--output",
help="Output filename.",
type=argparse.FileType("w"),
type=argparse.FileType("w", encoding="UTF-8"),
default=sys.stdout,
)
parser.add_argument(
Expand Down
27 changes: 16 additions & 11 deletions src/npg_irods/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io
import os
import shlex
import sys
import threading
from multiprocessing.pool import ThreadPool
from pathlib import PurePath
Expand Down Expand Up @@ -1020,7 +1021,7 @@ def _log_print(cmd, path):
_log_print("irmdir", target)


def write_safe_remove_script(path, root, stop_on_error=True, verbose=False):
def write_safe_remove_script(writer, root, stop_on_error=True, verbose=False):
"""Write a shell script that will safely and remove a collection and contents (or a
data object) from iRODS. It will generate irm commands for data objects and irmdir
commands for collections. None of the commands generated are themselves recursive.
Expand All @@ -1029,20 +1030,24 @@ def write_safe_remove_script(path, root, stop_on_error=True, verbose=False):
system. The script uses the interpreter "/bin/bash".
Args:
path: The path of the script to be generated. Any existing file at this path will
writer: Writer to which the script is written. Any existing file there will
be overwritten without warning.
root: A DataObject, Collection, PurePath or str path to remove.
stop_on_error: Add "set -e" to the script to stop on the first error.
verbose: Add "set -x" to the script to echo commands to STDERR as they are run.
"""
with open(path, "w", encoding="utf-8") as f:
print(f"#!/bin/bash", file=f)
print(f"# Generated by npg-irods {version()}", file=f)

if stop_on_error:
print("set -e", file=f)
if verbose:
print("set -x", file=f)
print(f"#!/bin/bash", file=writer)
print(f"# Generated by npg-irods {version()}", file=writer)

write_safe_remove_commands(root, f)
os.chmod(path, 0o755)
if stop_on_error:
print("set -e", file=writer)
if verbose:
print("set -x", file=writer)

write_safe_remove_commands(root, writer)

if writer is not sys.stdout:
writer.close()
os.chmod(writer.name, 0o755)
log.info(f"Script written to {writer.name}")
6 changes: 4 additions & 2 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ def test_write_safe_remove_commands(self, annotated_collection_tree):
@m.it("Removes the expected collections and data objects")
def test_write_safe_remove_script(self, tmp_path, annotated_collection_tree):
script = Path(tmp_path, "safe_rm.sh")
write_safe_remove_script(script, annotated_collection_tree)
with open(script, "w", encoding="UTF-8") as writer:
write_safe_remove_script(writer, annotated_collection_tree)
subprocess.run([script.as_posix()], check=True)

assert not Collection(annotated_collection_tree).exists()
Expand Down Expand Up @@ -626,7 +627,8 @@ def test_write_safe_remove_commands_special(self, challenging_paths_irods):
@m.it("Removes the expected collections and data objects")
def test_write_safe_remove_script_special(self, tmp_path, challenging_paths_irods):
script = Path(tmp_path, "safe_rm.sh")
write_safe_remove_script(script, challenging_paths_irods)
with open(script, "w", encoding="UTF-8") as writer:
write_safe_remove_script(writer, challenging_paths_irods)
subprocess.run([script.as_posix()], check=True)

assert not Collection(challenging_paths_irods).exists()

0 comments on commit 7fbf826

Please sign in to comment.