Debian Codegraph @

docs/examples/recursive_ls.rst

raw
1 Recursive ``ls`` via TransferClient
2 -----------------------------------
3
4 The Globus Transfer API does not offer a recursive variant of the ``ls``
5 operation. There are several reasons for this, but most obviously: ``ls`` is
6 synchronous, and a recursive listing may be very slow.
7
8 This example demonstrates how to write a breadth-first traversal of a dir tree
9 using a local deque to implement recursive ``ls``. You will need a properly
10 authenticated :class:`TransferClient <globus_sdk.TransferClient>`.
11
12 .. code-block:: python
13
14 from collections import deque
15
16
17 def _recursive_ls_helper(tc, ep, queue, max_depth):
18 while queue:
19 abs_path, rel_path, depth = queue.pop()
20 path_prefix = rel_path + "/" if rel_path else ""
21
22 res = tc.operation_ls(ep, path=abs_path)
23
24 if depth < max_depth:
25 queue.extend(
26 (
27 res["path"] + item["name"],
28 path_prefix + item["name"],
29 depth + 1,
30 )
31 for item in res["DATA"]
32 if item["type"] == "dir"
33 )
34 for item in res["DATA"]:
35 item["name"] = path_prefix + item["name"]
36 yield item
37
38
39 # tc: a TransferClient
40 # ep: an endpoint ID
41 # path: the path to list recursively
42 def recursive_ls(tc, ep, path, max_depth=3):
43 queue = deque()
44 queue.append((path, "", 0))
45 yield from _recursive_ls_helper(tc, ep, queue, max_depth)
46
47
48 This acts as a generator function, which you can then use for iteration, or
49 evaluate with ``list()`` or other expressions which will iterate over values
50 from the generator.
51
52 adding sleep
53 ~~~~~~~~~~~~
54
55 One of the issues with the above recursive listing tooling is that it can
56 easily run into rate limits on very large dir trees with a fast filesystem.
57
58 To avoid issues, simply add a periodic sleep. For example, we could add a
59 ``sleep_frequency`` and ``sleep_duration``, then count the number of ``ls``
60 calls that have been made. Every ``sleep_frequency`` calls, sleep for
61 ``sleep_duration``.
62
63 The modifications in the helper would be something like so:
64
65 .. code-block:: python
66
67 import time
68
69
70 def _recursive_ls_helper(tc, ep, queue, max_depth, sleep_frequency, sleep_duration):
71 call_count = 0
72 while queue:
73 abs_path, rel_path, depth = queue.pop()
74 path_prefix = rel_path + "/" if rel_path else ""
75
76 res = tc.operation_ls(ep, path=abs_path)
77
78 call_count += 1
79 if call_count % sleep_frequency == 0:
80 time.sleep(sleep_duration)
81
82 # as above
83 ...
84
85
86 parameter passthrough
87 ~~~~~~~~~~~~~~~~~~~~~
88
89 What if you want to pass parameters to the ``ls`` calls? Accepting that some
90 behaviors -- like order-by -- might not behave as expected if passed to the
91 recursive calls, you can still do-so. Add ``ls_params``, a dictionary of
92 additional parameters to pass to the underlying ``operation_ls`` invocations.
93
94 The helper can assume that a dict is passed, and the wrapper would just
95 initialize it as ``{}`` if nothing is passed.
96
97 Something like so:
98
99 .. code-block:: python
100
101 def _recursive_ls_helper(tc, ep, queue, max_depth, ls_params):
102 call_count = 0
103 while queue:
104 abs_path, rel_path, depth = queue.pop()
105 path_prefix = rel_path + "/" if rel_path else ""
106
107 res = tc.operation_ls(ep, path=abs_path, **ls_params)
108
109 # as above
110 ...
111
112
113 # importantly, the params should default to `None` and be rewritten to a
114 # dict in the function body (parameter default bindings are modifiable)
115 def recursive_ls(tc, ep, path, max_depth=3, ls_params=None):
116 ls_params = ls_params or {}
117 queue = deque()
118 queue.append((path, "", 0))
119 yield from _recursive_ls_helper(
120 tc, ep, queue, max_depth, sleep_frequency, sleep_duration, ls_params
121 )
122
123 What if we want to have different parameters to the top-level ``ls`` call from
124 any of the recursive calls? For example, maybe we want to filter the items
125 found in the initial directory, but not in subdirectories.
126
127 In that case, we just add on another layer: ``top_level_ls_params``, and we
128 only use those parameters on the initial call.
129
130 .. code-block:: python
131
132 def _recursive_ls_helper(
133 tc,
134 ep,
135 queue,
136 max_depth,
137 ls_params,
138 top_level_ls_params,
139 ):
140 first_call = True
141 while queue:
142 abs_path, rel_path, depth = queue.pop()
143 path_prefix = rel_path + "/" if rel_path else ""
144
145 use_params = ls_params
146 if first_call:
147 # on modern pythons, dict expansion can be used to easily
148 # combine dicts
149 use_params = {**ls_params, **top_level_ls_params}
150 first_call = False
151 res = tc.operation_ls(ep, path=abs_path, **use_params)
152
153 # again, the rest of the loop is the same
154 ...
155
156
157 def recursive_ls(
158 tc,
159 ep,
160 path,
161 max_depth=3,
162 ls_params=None,
163 top_level_ls_params=None,
164 ):
165 ls_params = ls_params or {}
166 top_level_ls_params = top_level_ls_params or {}
167 ...
168
169
170 With Sleep and Parameter Passing
171 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172
173 We can combine sleeps and parameter passing into one final, complete example:
174
175 .. code-block:: python
176
177 import time
178 from collections import deque
179
180
181 def _recursive_ls_helper(
182 tc,
183 ep,
184 queue,
185 max_depth,
186 sleep_frequency,
187 sleep_duration,
188 ls_params,
189 top_level_ls_params,
190 ):
191 call_count = 0
192 while queue:
193 abs_path, rel_path, depth = queue.pop()
194 path_prefix = rel_path + "/" if rel_path else ""
195
196 use_params = ls_params
197 if call_count == 0:
198 use_params = {**ls_params, **top_level_ls_params}
199
200 res = tc.operation_ls(ep, path=abs_path, **use_params)
201
202 call_count += 1
203 if call_count % sleep_frequency == 0:
204 time.sleep(sleep_duration)
205
206 if depth < max_depth:
207 queue.extend(
208 (
209 res["path"] + item["name"],
210 path_prefix + item["name"],
211 depth + 1,
212 )
213 for item in res["DATA"]
214 if item["type"] == "dir"
215 )
216 for item in res["DATA"]:
217 item["name"] = path_prefix + item["name"]
218 yield item
219
220
221 def recursive_ls(
222 tc,
223 ep,
224 path,
225 max_depth=3,
226 sleep_frequency=10,
227 sleep_duration=0.5,
228 ls_params=None,
229 top_level_ls_params=None,
230 ):
231 ls_params = ls_params or {}
232 top_level_ls_params = top_level_ls_params or {}
233 queue = deque()
234 queue.append((path, "", 0))
235 yield from _recursive_ls_helper(
236 tc,
237 ep,
238 queue,
239 max_depth,
240 sleep_frequency,
241 sleep_duration,
242 ls_params,
243 top_level_ls_params,
244 )