Skip to content

Commit

Permalink
Allow profile to be set from command line (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
berland authored Oct 21, 2021
1 parent b58e5f7 commit 32a10fb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/subscript/fmu_copy_revision/fmu_copy_revision.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
9. Make your own filter rules in a named file. For syntax, see e.g.
https://www.tutorialspoint.com/unix_commands/rsync.htm
"""

DEFAULT_PROFILE = 4

# FILTER* are per file filters, used in the second rsync command in the shell file below
# DIRFILTER* are per folder, used if DIRTREE is 1 or 2 in the shell script below.
Expand Down Expand Up @@ -272,9 +272,8 @@ def get_parser() -> argparse.ArgumentParser:
parser.add_argument(
"--profile",
dest="profile",
type=str,
default="4",
help="profile for copy profile to use, default is 3",
type=int,
help="profile for copy profile to use, default is 4",
)
parser.add_argument(
"--threads",
Expand Down Expand Up @@ -305,7 +304,7 @@ def __init__(self):
self.default_target = None
self.target = None
self.nthreads = None
self.profile = 3
self.profile = None
self.filter = ""
self.dirfilter = ""
self.batch = False
Expand Down Expand Up @@ -494,10 +493,13 @@ def _filesize(size: float) -> str:
def show_possible_profiles_copy(self):
"""Show a menu for possible profiles for copy/rsync."""

print(USERMENU)

default = "4"
self.profile = int(input(f"Choose (default is {default}): ") or default)
if self.args.profile is None:
print(USERMENU)
self.profile = int(
input(f"Choose (default is {DEFAULT_PROFILE}): ") or DEFAULT_PROFILE
)
else:
self.profile = int(self.args.profile)

if self.profile == 9:
ffile = input("Choose rsync filter file: ")
Expand Down Expand Up @@ -573,7 +575,10 @@ def do_rsyncing(self):
# as default, leave one CPU free for other use
self.nthreads = cpu_count() - 1 if cpu_count() > 1 else 1

print(f"Doing copy using {self.nthreads} CPU threads, please wait...")
print(
f"Doing copy with profile {self.profile} "
f"using {self.nthreads} CPU threads, please wait..."
)

# the -R (--relative) is crucial for making filter profiles work!
rsyncargs = "-a -R --delete"
Expand Down Expand Up @@ -649,7 +654,6 @@ def main(args=None) -> None:
logger.setLevel("DEBUG")

if not runner.args.source:
# interactive menues
runner.check_folders()
runner.menu_source_folder()
runner.menu_target_folder()
Expand Down
17 changes: 17 additions & 0 deletions tests/test_fmu_copy_revision.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,23 @@ def test_choice_profile3(datatree):
assert not (datatree / target / "backup").is_dir()


def test_profile_via_args(datatree):
"""Test interactive use but with profile specified on command line"""
os.chdir(datatree)
target = "users/jriv/xx_cmd_profile"
user_input = bytes(f"1\n{target}\n", encoding="ascii")
result = subprocess.run(
["fmu_copy_revision", "--profile", "3"],
check=True,
input=user_input,
stdout=subprocess.PIPE,
)
print(result.stdout.decode())

assert "Sync files using multiple threads" in result.stdout.decode()
assert (datatree / target / "rms" / "input" / "faults" / "f1.dat").exists()


def test_choice_profile3_double_target(datatree):
"""Test interactive mode, using profile 3 trying writing to same target twice."""
os.chdir(datatree)
Expand Down

0 comments on commit 32a10fb

Please sign in to comment.