Debian Codegraph @

tests/u/filetree.py

raw
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 """Utilities for testing workflow directory structure.
18
19 (There should be no tests in this module.)
20
21 A filetree is represented by a dict like so:
22 {
23 # Dirs are represented by dicts (which are also sub-filetrees):
24 'dir': {
25 'another-dir': {
26 # Files are represented by None:
27 'file.txt': None
28 }
29 },
30 # Symlinks are represented by the Symlink class, with the target
31 # represented by the relative path from the tmp_path directory:
32 'symlink': Symlink('dir/another-dir')
33 }
34 """
35
36 from pathlib import Path, PosixPath
37 from typing import Any, Dict, List
38
39
40 class Symlink(PosixPath):
41 """A class to represent a symlink target."""
42 ...
43
44
45 def create_filetree(
46 filetree: Dict[str, Any], location: Path, root: Path
47 ) -> None:
48 """Create the directory structure represented by the filetree dict.
49
50 Args:
51 filetree: The filetree to create.
52 location: The absolute path in which to create the filetree.
53 root: The top-level dir from which relative symlink targets are
54 located (typically tmp_path).
55 """
56 for name, entry in filetree.items():
57 path = location / name
58 if isinstance(entry, dict):
59 path.mkdir(exist_ok=True)
60 create_filetree(entry, path, root)
61 elif isinstance(entry, Symlink):
62 path.symlink_to(root / entry)
63 else:
64 path.touch()
65
66
67 def get_filetree_as_list(
68 filetree: Dict[str, Any], location: Path
69 ) -> List[str]:
70 """Return a list of the paths in a filetree.
71
72 Args:
73 filetree: The filetree to listify.
74 location: The absolute path to the filetree.
75 """
76 ret: List[str] = []
77 for name, entry in filetree.items():
78 path = location / name
79 ret.append(str(path))
80 if isinstance(entry, dict):
81 ret.extend(get_filetree_as_list(entry, path))
82 return ret
83
84
85 FILETREE_1 = {
86 'cylc-run': {
87 'foo': {
88 'bar': {
89 '.service': {
90 'db': None,
91 },
92 'flow.cylc': None,
93 'log': Symlink('sym/cylc-run/foo/bar/log'),
94 'mirkwood': Symlink('you-shall-not-pass/mirkwood'),
95 'rincewind.txt': Symlink('you-shall-not-pass/rincewind.txt'),
96 },
97 },
98 },
99 'sym': {
100 'cylc-run': {
101 'foo': {
102 'bar': {
103 'log': {
104 'darmok': Symlink('you-shall-not-pass/darmok'),
105 'temba.txt': Symlink('you-shall-not-pass/temba.txt'),
106 'bib': {
107 'fortuna.txt': None,
108 },
109 },
110 },
111 },
112 },
113 },
114 'you-shall-not-pass': { # Nothing in here should get deleted
115 'darmok': {
116 'jalad.txt': None,
117 },
118 'mirkwood': {
119 'spiders.txt': None,
120 },
121 'rincewind.txt': None,
122 'temba.txt': None,
123 },
124 }
125
126 FILETREE_2 = {
127 'cylc-run': {'foo': {'bar': Symlink('sym-run/cylc-run/foo/bar')}},
128 'sym-run': {
129 'cylc-run': {
130 'foo': {
131 'bar': {
132 '.service': {
133 'db': None,
134 },
135 'flow.cylc': None,
136 'share': Symlink('sym-share/cylc-run/foo/bar/share'),
137 },
138 },
139 },
140 },
141 'sym-share': {
142 'cylc-run': {
143 'foo': {
144 'bar': {
145 'share': {
146 'cycle': Symlink(
147 'sym-cycle/cylc-run/foo/bar/share/cycle'
148 ),
149 },
150 },
151 },
152 },
153 },
154 'sym-cycle': {
155 'cylc-run': {
156 'foo': {
157 'bar': {
158 'share': {
159 'cycle': {
160 'macklunkey.txt': None,
161 },
162 },
163 },
164 },
165 },
166 },
167 'you-shall-not-pass': {},
168 }
169
170 FILETREE_3 = {
171 'cylc-run': {
172 'foo': {
173 'bar': Symlink('sym-run/cylc-run/foo/bar'),
174 },
175 },
176 'sym-run': {
177 'cylc-run': {
178 'foo': {
179 'bar': {
180 '.service': {
181 'db': None,
182 },
183 'flow.cylc': None,
184 'share': {
185 'cycle': Symlink(
186 'sym-cycle/cylc-run/foo/bar/share/cycle'
187 ),
188 },
189 },
190 },
191 },
192 },
193 'sym-cycle': {
194 'cylc-run': {
195 'foo': {
196 'bar': {
197 'share': {
198 'cycle': {
199 'sokath.txt': None,
200 },
201 },
202 },
203 },
204 },
205 },
206 'you-shall-not-pass': {},
207 }
208
209 FILETREE_4 = {
210 'cylc-run': {
211 'foo': {
212 'bar': {
213 '.service': {
214 'db': None,
215 },
216 'flow.cylc': None,
217 'share': {
218 'cycle': Symlink('sym-cycle/cylc-run/foo/bar/share/cycle'),
219 },
220 },
221 },
222 },
223 'sym-cycle': {
224 'cylc-run': {
225 'foo': {
226 'bar': {
227 'share': {
228 'cycle': {
229 'kiazi.txt': None,
230 },
231 },
232 },
233 },
234 },
235 },
236 'you-shall-not-pass': {},
237 }