|
| 1 | +# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +"""Utilities relating to the listing and management of source workflows.""" |
| 17 | + |
| 18 | +from contextlib import suppress |
| 19 | +from pathlib import Path |
| 20 | +from typing import Optional, List, Dict |
| 21 | + |
| 22 | +from cylc.flow.cfgspec.glbl_cfg import glbl_cfg |
| 23 | +from cylc.flow.id import Tokens |
| 24 | +from cylc.flow.network.scan import scan_multi |
| 25 | +from cylc.flow.pathutil import get_workflow_run_dir |
| 26 | +from cylc.flow.workflow_files import get_workflow_source_dir |
| 27 | + |
| 28 | + |
| 29 | +# the user's configured workflow source directories |
| 30 | +SOURCE_DIRS: List[Path] = [ |
| 31 | + Path(source_dir).expanduser() |
| 32 | + for source_dir in glbl_cfg().get(['install', 'source dirs']) |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +SourceWorkflow = Dict |
| 37 | + |
| 38 | + |
| 39 | +def _source_workflow(source_path: Path) -> SourceWorkflow: |
| 40 | + """Return the fields required to resolve a SourceWorkflow. |
| 41 | +
|
| 42 | + Args: |
| 43 | + source_path: |
| 44 | + Path to the source workflow directory. |
| 45 | +
|
| 46 | + """ |
| 47 | + return { |
| 48 | + 'name': _get_source_workflow_name(source_path), |
| 49 | + 'path': source_path, |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +def _blank_source_workflow() -> SourceWorkflow: |
| 54 | + """Return a blank source workflow. |
| 55 | +
|
| 56 | + This will be used for workflows which were not installed by "cylc install". |
| 57 | + """ |
| 58 | + |
| 59 | + return {'name': None, 'path': None} |
| 60 | + |
| 61 | + |
| 62 | +def _get_source_workflow_name(source_path: Path) -> Optional[str]: |
| 63 | + """Return the "name" of the source workflow. |
| 64 | +
|
| 65 | + This is the "name" that can be provided to the "cylc install" command. |
| 66 | +
|
| 67 | + Args: |
| 68 | + source_path: |
| 69 | + Path to the source workflow directory. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + The source workflow name if the source workflow is located within |
| 73 | + a configured source directory, else None. |
| 74 | +
|
| 75 | + """ |
| 76 | + for source_dir in SOURCE_DIRS: |
| 77 | + with suppress(ValueError): |
| 78 | + return str(source_path.relative_to(source_dir)) |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def _get_workflow_source(workflow_id): |
| 83 | + """Return the source workflow for the given workflow ID.""" |
| 84 | + run_dir = get_workflow_run_dir(workflow_id) |
| 85 | + source_dir, _symlink = get_workflow_source_dir(run_dir) |
| 86 | + if source_dir: |
| 87 | + return _source_workflow(Path(source_dir)) |
| 88 | + return _blank_source_workflow() |
| 89 | + |
| 90 | + |
| 91 | +async def list_source_workflows(*_) -> List[SourceWorkflow]: |
| 92 | + """List source workflows located in the configured source directories.""" |
| 93 | + ret = [] |
| 94 | + async for flow in scan_multi(SOURCE_DIRS): |
| 95 | + ret.append(_source_workflow(flow['path'])) |
| 96 | + return ret |
| 97 | + |
| 98 | + |
| 99 | +def get_workflow_source(data, _, **kwargs) -> Optional[SourceWorkflow]: |
| 100 | + """Resolve the source for an installed workflow. |
| 101 | +
|
| 102 | + If the source cannot be resolved, e.g. if the workflow was not installed by |
| 103 | + "cylc install", then this will return None. |
| 104 | + """ |
| 105 | + workflow_id = Tokens(data.id)['workflow'] |
| 106 | + return _get_workflow_source(workflow_id) |
0 commit comments