Skip to content

Commit

Permalink
Tweak reinstall to partially use centralised reg parse
Browse files Browse the repository at this point in the history
  • Loading branch information
MetRonnie committed Aug 13, 2021
1 parent 8fdec12 commit 5e0d240
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 116 deletions.
99 changes: 34 additions & 65 deletions cylc/flow/scripts/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
Examples:
# Having previously installed:
$ cylc install myflow
# To reinstall this workflow run:
$ cylc reinstall myflow/run1
# To reinstall the latest run:
$ cylc reinstall myflow
# Or, to reinstall a specific run:
$ cylc reinstall myflow/run2
# Having previously installed:
$ cylc install myflow --no-run-name
Expand All @@ -34,7 +36,6 @@
# To reinstall a workflow from within the cylc-run directory of a previously
# installed workflow:
$ cylc reinstall
"""

from pathlib import Path
Expand All @@ -43,11 +44,12 @@
from cylc.flow import iter_entry_points
from cylc.flow.exceptions import PluginError, WorkflowFilesError
from cylc.flow.option_parsers import CylcOptionParser as COP
from cylc.flow.pathutil import get_workflow_run_dir
from cylc.flow.pathutil import get_cylc_run_dir, get_workflow_run_dir
from cylc.flow.workflow_files import (
get_workflow_source_dir,
parse_reg,
reinstall_workflow,
WorkflowFiles)
)
from cylc.flow.terminal import cli_function

if TYPE_CHECKING:
Expand All @@ -56,86 +58,53 @@

def get_option_parser():
parser = COP(
__doc__, comms=True, prep=True,
argdoc=[("[NAMED_RUN]", "Named run. e.g. my-flow/run1")]
__doc__, comms=True, argdoc=[('[REG]', 'Workflow name')]
)

# If cylc-rose plugin is available ad the --option/-O config
parser.add_cylc_rose_options()
try:
# If cylc-rose plugin is available
__import__('cylc.rose')
parser.add_option(
"--opt-conf-key", "-O",
help=(
"Use optional Rose Config Setting "
"(if cylc-rose is installed)"
),
action="append",
default=[],
dest="opt_conf_keys"
)
parser.add_option(
"--define", '-D',
help=(
"Each of these overrides the `[SECTION]KEY` setting in a "
"`rose-suite.conf` file. "
"Can be used to disable a setting using the syntax "
"`--define=[SECTION]!KEY` or even `--define=[!SECTION]`."
),
action="append",
default=[],
dest="defines"
)
parser.add_option(
"--rose-template-variable", '-S',
help=(
"As `--define`, but with an implicit `[SECTION]` for "
"workflow variables."
),
action="append",
default=[],
dest="rose_template_vars"
)
except ImportError:
pass
else:
parser.add_option(
"--clear-rose-install-options",
help=(
"Clear options previously set by cylc-rose."
),
help="Clear options previously set by cylc-rose.",
action='store_true',
default=False,
dest="clear_rose_install_opts"
)
except ImportError:
pass

return parser


@cli_function(get_option_parser)
def main(
parser: COP, opts: 'Values', named_run: Optional[str] = None
) -> None:
if not named_run:
source, _ = get_workflow_source_dir(Path.cwd())
if source is None:
def main(parser: COP, opts: 'Values', reg: Optional[str] = None) -> None:
run_dir: Optional[Path]
if reg is None:
try:
reg = str(Path.cwd().relative_to(
Path(get_cylc_run_dir()).resolve()
))
except ValueError:
raise WorkflowFilesError(
f'"{Path.cwd()}" is not a workflow run directory.')
base_run_dir = Path(get_workflow_run_dir(''))
named_run = str(Path.cwd().relative_to(base_run_dir.resolve()))
run_dir = Path(get_workflow_run_dir(named_run))
if not run_dir.exists():
"The current working directory is not a workflow run directory"
)
else:
reg = parse_reg(reg)
run_dir = Path(get_workflow_run_dir(reg))
if not run_dir.is_dir():
raise WorkflowFilesError(
f'"{named_run}" is not an installed workflow.')
if run_dir.name in [WorkflowFiles.FLOW_FILE, WorkflowFiles.SUITE_RC]:
run_dir = run_dir.parent
named_run = named_run.rsplit('/', 1)[0]
source, source_path = get_workflow_source_dir(run_dir)
f'"{reg}" is not an installed workflow.')
source, source_symlink = get_workflow_source_dir(run_dir)
if not source:
raise WorkflowFilesError(
f'"{named_run}" was not installed with cylc install.')
if not Path(source).exists():
f'"{reg}" was not installed with cylc install.')
if not Path(source).is_dir():
raise WorkflowFilesError(
f'Workflow source dir is not accessible: "{source}".\n'
f'Restore the source or modify the "{source_path}"'
f'Restore the source or modify the "{source_symlink}"'
' symlink to continue.'
)
for entry_point in iter_entry_points(
Expand All @@ -153,7 +122,7 @@ def main(
) from None

reinstall_workflow(
named_run=named_run,
named_run=reg,
rundir=run_dir,
source=source,
dry_run=False # TODO: ready for dry run implementation
Expand Down
44 changes: 7 additions & 37 deletions tests/functional/cylc-reinstall/00-simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#------------------------------------------------------------------------------
# Test workflow re-installation
. "$(dirname "$0")/test_header"
set_test_number 36
set_test_number 28

# Test basic cylc reinstall, named run given
TEST_NAME="${TEST_NAME_BASE}-basic-named-run"
make_rnd_workflow
pushd "${RND_WORKFLOW_SOURCE}" || exit 1
run_ok "${TEST_NAME}" cylc install
contains_ok "${TEST_NAME}.stdout" <<__OUT__
cmp_ok "${TEST_NAME}.stdout" <<__OUT__
INSTALLED $RND_WORKFLOW_NAME/run1 from ${RND_WORKFLOW_SOURCE}
__OUT__
run_ok "basic-reinstall" cylc reinstall "${RND_WORKFLOW_NAME}/run1"
Expand All @@ -35,46 +35,16 @@ grep_ok "REINSTALLED ${RND_WORKFLOW_NAME}/run1 from ${RND_WORKFLOW_SOURCE}" "${R
popd || exit 1
purge_rnd_workflow

# Test basic cylc reinstall, named run (including ``flow.cylc``) given
TEST_NAME="${TEST_NAME_BASE}-flow-as-arg"
make_rnd_workflow
pushd "${RND_WORKFLOW_SOURCE}" || exit 1
run_ok "${TEST_NAME}" cylc install
run_ok "${TEST_NAME}-reinstall" cylc reinstall "${RND_WORKFLOW_NAME}/run1/flow.cylc"
contains_ok "${TEST_NAME}.stdout" <<__OUT__
INSTALLED $RND_WORKFLOW_NAME/run1 from ${RND_WORKFLOW_SOURCE}
__OUT__
REINSTALL_LOG="$(find "${RND_WORKFLOW_RUNDIR}/run1/log/install" -type f -name '*reinstall.log')"
grep_ok "REINSTALLED ${RND_WORKFLOW_NAME}/run1 from ${RND_WORKFLOW_SOURCE}" "${REINSTALL_LOG}"
popd || exit 1
purge_rnd_workflow

# Test basic cylc reinstall, named run (including suite.rc) given
TEST_NAME="${TEST_NAME_BASE}-suite.rc-as-arg"
make_rnd_workflow
pushd "${RND_WORKFLOW_SOURCE}" || exit 1
rm -rf flow.cylc
touch suite.rc
run_ok "${TEST_NAME}" cylc install
run_ok "${TEST_NAME}-reinstall-suite.rc" cylc reinstall "${RND_WORKFLOW_NAME}/run1/suite.rc"
contains_ok "${TEST_NAME}.stdout" <<__OUT__
INSTALLED $RND_WORKFLOW_NAME/run1 from ${RND_WORKFLOW_SOURCE}
__OUT__
REINSTALL_LOG="$(find "${RND_WORKFLOW_RUNDIR}/run1/log/install" -type f -name '*reinstall.log')"
grep_ok "REINSTALLED ${RND_WORKFLOW_NAME}/run1 from ${RND_WORKFLOW_SOURCE}" "${REINSTALL_LOG}"
popd || exit 1
purge_rnd_workflow

# Test install/reinstall executed from elsewhere in filesystem
TEST_NAME="${TEST_NAME_BASE}-named-flow"
make_rnd_workflow
pushd "${TMPDIR}" || exit 1
run_ok "${TEST_NAME}-install" cylc install -C "${RND_WORKFLOW_SOURCE}" --flow-name="${RND_WORKFLOW_NAME}"
contains_ok "${TEST_NAME}-install.stdout" <<__OUT__
cmp_ok "${TEST_NAME}-install.stdout" <<__OUT__
INSTALLED ${RND_WORKFLOW_NAME}/run1 from ${RND_WORKFLOW_SOURCE}
__OUT__
run_ok "${TEST_NAME}-reinstall" cylc reinstall "${RND_WORKFLOW_NAME}/run1"
contains_ok "${TEST_NAME}-reinstall.stdout" <<__OUT__
cmp_ok "${TEST_NAME}-reinstall.stdout" <<__OUT__
REINSTALLED $RND_WORKFLOW_NAME/run1 from ${RND_WORKFLOW_SOURCE}
__OUT__
popd || exit 1
Expand All @@ -87,7 +57,7 @@ make_rnd_workflow
rm -f "${RND_WORKFLOW_SOURCE}/flow.cylc"
touch "${RND_WORKFLOW_SOURCE}/suite.rc"
run_ok "${TEST_NAME}" cylc install --flow-name="${RND_WORKFLOW_NAME}" -C "${RND_WORKFLOW_SOURCE}"
contains_ok "${TEST_NAME}.stdout" <<__OUT__
cmp_ok "${TEST_NAME}.stdout" <<__OUT__
INSTALLED $RND_WORKFLOW_NAME/run1 from ${RND_WORKFLOW_SOURCE}
__OUT__
# test symlink not made in source dir
Expand Down Expand Up @@ -120,7 +90,7 @@ purge_rnd_workflow
TEST_NAME="${TEST_NAME_BASE}-no-args"
make_rnd_workflow
run_ok "${TEST_NAME}-install" cylc install --flow-name="${RND_WORKFLOW_NAME}" -C "${RND_WORKFLOW_SOURCE}"
contains_ok "${TEST_NAME}-install.stdout" <<__OUT__
cmp_ok "${TEST_NAME}-install.stdout" <<__OUT__
INSTALLED ${RND_WORKFLOW_NAME}/run1 from ${RND_WORKFLOW_SOURCE}
__OUT__
pushd "${RND_WORKFLOW_RUNDIR}/run1" || exit 1
Expand All @@ -137,7 +107,7 @@ TEST_NAME="${TEST_NAME_BASE}-no-args-no-run-name"
make_rnd_workflow
pushd "${RND_WORKFLOW_SOURCE}" || exit 1
run_ok "${TEST_NAME}-install" cylc install --no-run-name -C "${RND_WORKFLOW_SOURCE}"
contains_ok "${TEST_NAME}-install.stdout" <<__OUT__
cmp_ok "${TEST_NAME}-install.stdout" <<__OUT__
INSTALLED ${RND_WORKFLOW_NAME} from ${RND_WORKFLOW_SOURCE}
__OUT__
pushd "${RND_WORKFLOW_RUNDIR}" || exit 1
Expand Down
26 changes: 12 additions & 14 deletions tests/functional/cylc-reinstall/02-failures.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ set_test_number 26
TEST_NAME="${TEST_NAME_BASE}-nested-rundir-forbidden-reinstall"
make_rnd_workflow
run_ok "${TEST_NAME}-install" cylc install -C "${RND_WORKFLOW_SOURCE}" --flow-name="${RND_WORKFLOW_NAME}"
mkdir "${RND_WORKFLOW_RUNDIR}/run1/nested_run_dir"
touch "${RND_WORKFLOW_RUNDIR}/run1/nested_run_dir/flow.cylc"
mkdir -p "${RND_WORKFLOW_RUNDIR}/run1/nested/.service"
run_fail "${TEST_NAME}-reinstall-nested-run-dir" cylc reinstall "${RND_WORKFLOW_NAME}"
contains_ok "${TEST_NAME}-reinstall-nested-run-dir.stderr" <<__ERR__
WorkflowFilesError: Nested run directories not allowed - cannot install workflow name "${RND_WORKFLOW_NAME}" as "${RND_WORKFLOW_RUNDIR}/run1" is already a valid run directory.
cmp_ok "${TEST_NAME}-reinstall-nested-run-dir.stderr" <<__ERR__
WorkflowFilesError: Nested run directories not allowed - cannot install workflow name "${RND_WORKFLOW_NAME}/run1" as "${RND_WORKFLOW_RUNDIR}/run1/nested" is already a valid run directory.
__ERR__
purge_rnd_workflow

Expand All @@ -39,8 +38,8 @@ TEST_NAME="${TEST_NAME_BASE}-reinstall-no-run-dir"
make_rnd_workflow
run_ok "${TEST_NAME}-install" cylc install -C "${RND_WORKFLOW_SOURCE}" --flow-name="${RND_WORKFLOW_NAME}" --no-run-name
rm -rf "${RND_WORKFLOW_RUNDIR}"
run_fail "${TEST_NAME}-reinstall" cylc reinstall "${RND_WORKFLOW_NAME}"
contains_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
run_fail "${TEST_NAME}-reinstall" cylc reinstall "${RND_WORKFLOW_NAME}"
cmp_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
WorkflowFilesError: "${RND_WORKFLOW_NAME}" is not an installed workflow.
__ERR__
purge_rnd_workflow
Expand All @@ -51,8 +50,8 @@ TEST_NAME="${TEST_NAME_BASE}-reinstall-no-source-dir"
make_rnd_workflow
run_ok "${TEST_NAME}-install" cylc install -C "${RND_WORKFLOW_SOURCE}" --flow-name="${RND_WORKFLOW_NAME}" --no-run-name
rm -rf "${RND_WORKFLOW_SOURCE}"
run_fail "${TEST_NAME}-reinstall" cylc reinstall "${RND_WORKFLOW_NAME}"
contains_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
run_fail "${TEST_NAME}-reinstall" cylc reinstall "${RND_WORKFLOW_NAME}"
cmp_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
WorkflowFilesError: Workflow source dir is not accessible: "${RND_WORKFLOW_SOURCE}".
Restore the source or modify the "${RND_WORKFLOW_RUNDIR}/_cylc-install/source" symlink to continue.
__ERR__
Expand All @@ -65,7 +64,7 @@ make_rnd_workflow
run_ok "${TEST_NAME}-install" cylc install -C "${RND_WORKFLOW_SOURCE}" --flow-name="${RND_WORKFLOW_NAME}" --no-run-name
rm -f "${RND_WORKFLOW_SOURCE}/flow.cylc"
run_fail "${TEST_NAME}" cylc reinstall "${RND_WORKFLOW_NAME}"
contains_ok "${TEST_NAME}.stderr" <<__ERR__
cmp_ok "${TEST_NAME}.stderr" <<__ERR__
WorkflowFilesError: no flow.cylc or suite.rc in ${RND_WORKFLOW_SOURCE}
__ERR__
purge_rnd_workflow
Expand All @@ -79,7 +78,7 @@ for DIR in 'work' 'share' 'log' '_cylc-install'; do
cylc install --no-run-name --flow-name="${RND_WORKFLOW_NAME}"
mkdir ${DIR}
run_fail "${TEST_NAME}" cylc reinstall "${RND_WORKFLOW_NAME}"
contains_ok "${TEST_NAME}.stderr" <<__ERR__
cmp_ok "${TEST_NAME}.stderr" <<__ERR__
WorkflowFilesError: ${RND_WORKFLOW_NAME} installation failed. - ${DIR} exists in source directory.
__ERR__
purge_rnd_workflow
Expand All @@ -94,9 +93,8 @@ run_ok "${TEST_NAME}-install" cylc install --no-run-name --flow-name="${RND_WORK
pushd "${RND_WORKFLOW_RUNDIR}" || exit 1
rm -rf "_cylc-install"
run_fail "${TEST_NAME}-reinstall" cylc reinstall
CWD=$(pwd -P)
contains_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
WorkflowFilesError: "${CWD}" is not a workflow run directory.
cmp_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
WorkflowFilesError: "${RND_WORKFLOW_NAME}" was not installed with cylc install.
__ERR__
popd || exit 1
popd || exit 1
Expand All @@ -110,7 +108,7 @@ run_ok "${TEST_NAME}-install" cylc install --no-run-name --flow-name="${RND_WORK
pushd "${RND_WORKFLOW_RUNDIR}" || exit 1
rm -rf "_cylc-install"
run_fail "${TEST_NAME}-reinstall" cylc reinstall "$RND_WORKFLOW_NAME"
contains_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
cmp_ok "${TEST_NAME}-reinstall.stderr" <<__ERR__
WorkflowFilesError: "${RND_WORKFLOW_NAME}" was not installed with cylc install.
__ERR__
popd || exit 1
Expand Down

0 comments on commit 5e0d240

Please sign in to comment.