| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Extract named resources from the cylc.flow package.""" |
| 18 |
|
| 19 |
from ansimarkup import parse |
| 20 |
from contextlib import suppress |
| 21 |
from pathlib import Path |
| 22 |
import re |
| 23 |
import shutil |
| 24 |
import sys |
| 25 |
from typing import Optional |
| 26 |
|
| 27 |
import cylc.flow |
| 28 |
from cylc.flow import LOG |
| 29 |
from cylc.flow.cfgspec.glbl_cfg import glbl_cfg |
| 30 |
from cylc.flow.exceptions import InputError |
| 31 |
from cylc.flow.wallclock import get_current_time_string |
| 32 |
|
| 33 |
|
| 34 |
RESOURCE_DIR = Path(cylc.flow.__file__).parent / 'etc' |
| 35 |
TUTORIAL_DIR = RESOURCE_DIR / 'tutorial' |
| 36 |
EXAMPLE_DIR = RESOURCE_DIR / 'examples' |
| 37 |
|
| 38 |
|
| 39 |
# {resource: brief description} |
| 40 |
RESOURCE_NAMES = { |
| 41 |
'syntax/cylc-mode.el': 'Emacs syntax highlighting.', |
| 42 |
'syntax/cylc.lang': 'Gedit (gtksourceview) syntax highlighting.', |
| 43 |
'syntax/cylc.xml': 'Kate syntax highlighting.', |
| 44 |
'cylc-completion.bash': 'Bash auto-completion for Cylc commands.', |
| 45 |
'cylc': 'Cylc wrapper script.', |
| 46 |
'!syntax/cylc.vim': 'Obsolete- use https://github.com/cylc/cylc.vim', |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
def list_resources(write=print, headers=True): |
| 51 |
"""Print resource names to stdout.""" |
| 52 |
tutorials = [ |
| 53 |
path.relative_to(RESOURCE_DIR) |
| 54 |
for path in TUTORIAL_DIR.iterdir() |
| 55 |
if path.is_dir() |
| 56 |
] |
| 57 |
examples = [ |
| 58 |
path.relative_to(RESOURCE_DIR) |
| 59 |
for path in EXAMPLE_DIR.iterdir() |
| 60 |
if path.is_dir() |
| 61 |
] |
| 62 |
if headers: |
| 63 |
write('Resources:') |
| 64 |
max_len = max(len(res) for res in RESOURCE_NAMES) |
| 65 |
for resource, desc in RESOURCE_NAMES.items(): |
| 66 |
if resource[0] == '!': |
| 67 |
# Use ! to indicated that resource is deprecated: |
| 68 |
resource = resource[1:] |
| 69 |
write(parse( |
| 70 |
f'<yellow> {resource} {" " * (max_len - len(resource))}' |
| 71 |
f' # {desc}</yellow>' |
| 72 |
)) |
| 73 |
else: |
| 74 |
write(f' {resource} {" " * (max_len - len(resource))} # {desc}') |
| 75 |
if headers: |
| 76 |
write('\nTutorials:') |
| 77 |
for tutorial in tutorials: |
| 78 |
write(f' {tutorial}') |
| 79 |
if headers: |
| 80 |
write('\nExamples:') |
| 81 |
for example in examples: |
| 82 |
write(f' {example}') |
| 83 |
|
| 84 |
|
| 85 |
def path_is_source_workflow(src: Path) -> bool: |
| 86 |
"""Returns True if the src path is a Cylc workflow.""" |
| 87 |
with suppress(ValueError): |
| 88 |
src.relative_to(TUTORIAL_DIR) |
| 89 |
return True |
| 90 |
with suppress(ValueError): |
| 91 |
src.relative_to(EXAMPLE_DIR) |
| 92 |
return True |
| 93 |
return False |
| 94 |
|
| 95 |
|
| 96 |
def get_resources(resource: str, tgt_dir: Optional[str]): |
| 97 |
"""Extract cylc.flow resources and write them to a target directory. |
| 98 |
|
| 99 |
Arguments: |
| 100 |
resource: path relative to RESOURCE_DIR. |
| 101 |
target_dir: Where to put extracted resources, created if necessary. |
| 102 |
|
| 103 |
""" |
| 104 |
# get the resource path |
| 105 |
resource_path = Path(resource) |
| 106 |
|
| 107 |
src = RESOURCE_DIR / resource_path |
| 108 |
if not src.exists(): |
| 109 |
raise InputError( |
| 110 |
f'No such resources {resource}.' |
| 111 |
'\nRun `cylc get-resources --list` for resource names.' |
| 112 |
) |
| 113 |
|
| 114 |
is_source_workflow = path_is_source_workflow(src) |
| 115 |
|
| 116 |
# get the target path |
| 117 |
name = resource_path.name |
| 118 |
if not tgt_dir: |
| 119 |
if is_source_workflow: |
| 120 |
# this is a tutorial => use the primary source dir |
| 121 |
_tgt_dir = Path(glbl_cfg().get(['install', 'source dirs'])[0]) |
| 122 |
# remove any numerical prefix (used in the examples to ensure they |
| 123 |
# appear in order in the docs) as these are not valid workflow IDs |
| 124 |
name = re.sub(r'^[\d]+-', '', name) |
| 125 |
else: |
| 126 |
# this is a regular resource => use $PWD |
| 127 |
_tgt_dir = Path.cwd() |
| 128 |
else: |
| 129 |
_tgt_dir = Path(tgt_dir).resolve() |
| 130 |
tgt = _tgt_dir / name |
| 131 |
|
| 132 |
tgt = tgt.expanduser() |
| 133 |
tgt = tgt.resolve() |
| 134 |
|
| 135 |
# extract resources |
| 136 |
extract_resource(src, tgt, is_source_workflow) |
| 137 |
|
| 138 |
|
| 139 |
def _backup(tgt: Path) -> None: |
| 140 |
"""Make a timestamped backup of a dir or file.""" |
| 141 |
tstamp = get_current_time_string(use_basic_format=True) |
| 142 |
backup = Path(tgt).parent / (tgt.name + f'.{tstamp}') |
| 143 |
LOG.warning( |
| 144 |
'Replacing an existing cylc-tutorials folder which will' |
| 145 |
f' be copied to {backup}' |
| 146 |
) |
| 147 |
# NOTE: shutil interfaces don't fully support Path objects at all |
| 148 |
# python versions |
| 149 |
shutil.move(str(tgt), str(backup)) |
| 150 |
|
| 151 |
|
| 152 |
def extract_resource( |
| 153 |
src: Path, |
| 154 |
tgt: Path, |
| 155 |
is_source_workflow: bool = False, |
| 156 |
) -> None: |
| 157 |
"""Extract src into tgt. |
| 158 |
|
| 159 |
NOTE: src can be a dir or a file. |
| 160 |
""" |
| 161 |
LOG.info(f"Extracting {src.relative_to(RESOURCE_DIR)} to {tgt}") |
| 162 |
if is_source_workflow and tgt.exists(): |
| 163 |
# target exists, back up the old copy |
| 164 |
_backup(tgt) |
| 165 |
|
| 166 |
# files to exclude |
| 167 |
if is_source_workflow: |
| 168 |
excludes = [ |
| 169 |
# test files |
| 170 |
'.validate', |
| 171 |
'reftest', |
| 172 |
# documentation files |
| 173 |
'index.rst', |
| 174 |
] |
| 175 |
else: |
| 176 |
excludes = [] |
| 177 |
|
| 178 |
# create the target directory |
| 179 |
try: |
| 180 |
tgt.parent.mkdir(parents=True, exist_ok=True) |
| 181 |
|
| 182 |
# NOTE: shutil interfaces don't fully support Path objects at all |
| 183 |
# python versions |
| 184 |
if src.is_dir(): |
| 185 |
shutil.copytree(str(src), str(tgt)) |
| 186 |
else: |
| 187 |
shutil.copyfile(str(src), str(tgt)) |
| 188 |
for exclude in excludes: |
| 189 |
path = tgt / exclude |
| 190 |
if path.exists(): |
| 191 |
path.unlink() |
| 192 |
except IsADirectoryError as exc: |
| 193 |
LOG.error( |
| 194 |
f'Cannot extract file {exc.filename} as there is an ' |
| 195 |
'existing directory with the same name' |
| 196 |
) |
| 197 |
sys.exit(1) |
| 198 |
except FileExistsError as exc: |
| 199 |
LOG.error( |
| 200 |
f'Cannot extract directory {exc.filename} as there is an ' |
| 201 |
'existing file with the same name' |
| 202 |
) |
| 203 |
sys.exit(1) |