generated from datalad/datalad-extension-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
uncurl.py
544 lines (459 loc) · 21.8 KB
/
uncurl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
"""
*uncurl* git-annex external special remote
==========================================
This implementation is a git-annex accessible interface to datalad-next's URL
operations framework. It serves two main purposes:
1. Combine git-annex's capabilities of registering and accessing file content
via URLs with DataLad's access credential management and (additional or
alternative) transport protocol implementations.
2. Minimize the maintenance effort for datasets (primarily) composed from
content that is remotely accessible via URLs from systems other than
Datalad or git-annex in the event of an infrastructure transition
(e.g. moving to a different technical system or a different data
organization on a storage system).
Requirements
------------
This special remote implementation requires git-annex version 8.20210127 (or
later) to be available.
Download helper
---------------
The simplest way to use this remote is to initialize it without any particular
configuration::
$ git annex initremote uncurl type=external externaltype=uncurl encryption=none
initremote uncurl ok
(recording state in git...)
Once initialized, or later enabled in a clone, ``git-annex addurl`` will check
with the *uncurl* remote whether it can handle a particular URL, and will let
the remote perform the download in case of positive response. By default, the
remote will claim any URLs with a scheme that the local datalad-next
installation supports. This always includes ``file://``, ``http://``, and
``https://``, but is extensible, and a particular installation may also support
``ssh://`` (by default when openssh is installed), or other schemes.
This additional URL support is also available for other commands. Here is an
example how ``datalad addurls`` can be given any uncurl-supported URLs
(here an SSH-URL) directly, provided that the ``uncurl`` remote was initialized
for a dataset (as shown above)::
$ echo '[{"url":"ssh://my.server.org/home/me/file", "file":"dummy"}]' \\
| datalad addurls - '{url}' {'file'}
This makes legacy commands (e.g., ``datalad download-url``), unnecessary, and
facilitates the use of more advanced ``datalad addurls`` features (e.g.,
automatic creation of subdatasets) that are not provided by lower-level
commands like ``git annex addurl``.
Download helper with credential management support
--------------------------------------------------
With this setup, download requests now also use DataLad's credential system for
authentication. DataLad will automatically lookup matching credentials, prompt
for manual entry if none are found, and offer to store them securely for later
use after having used them successfully::
$ git annex addurl http://httpbin.org/basic-auth/myuser/mypassword
Credential needed for access to http://httpbin.org/basic-auth/myuser/mypassword
user: myuser
password:
password (repeat):
Enter a name to save the credential
(for accessing http://httpbin.org/basic-auth/myuser/mypassword) securely for future
re-use, or 'skip' to not save the credential
name: httpbin-dummy
addurl http://httpbin.org/basic-auth/myuser/mypassword (from uncurl) (to ...)
ok
(recording state in git...)
By adding files via downloads from URLs in this fashion, datasets can be built
that track information across a range of locations/services, using a possibly
heterogeneous set of access methods.
This feature is very similar to the ``datalad`` special remote implementation
included in the core DataLad package. The difference here is that alternative
implementations of downloaders are employed and the ``datalad-next`` credential
system is used instead of the "providers" mechanism from DataLad's core
package.
Transforming recorded URLs
--------------------------
The main benefit of using *uncurl* is, however, only revealed when the original
snapshot of where data used to be accessible becomes invalid, maybe because
data were moved to a different storage system, or simply a different host.
This would typically require an update of each, now broken, access URL. For
datasets with thousands or even millions of files this can be an expensive
operation. For data portal operators providing a large number of datasets it is
even more tedious.
*uncurl* enables programmatic, on-access URL rewriting. This is similar, in
spirit, to Git's ``url.<base>.insteadOf`` URL modification feature. However,
modification possibilities reach substantially beyond replacing a base URL.
This feature is based on two customizable settings: 1) a *URL template*; and
2) a *set of match expressions* that extract additional identifiers
from any recorded access URL for an annex key.
Here is an example: Let's say a file in a dataset has a recorded access URL
of::
https://data.example.org/c542/s7612_figure1.pdf
We can let *uncurl* know that ``c542`` is actually an identifier for a
particular collection of items in this data store. Likewise ``s7612`` is an
identifier of a particular item in that collection, and ``figure1.pdf`` is the
name of a component in that collection item. The following Python regular
expression can be used to "decompose" the above URL into these semantic
components::
(?P<site>https://[^/]+)/(?P<collection>c[^/]+)/(?P<item>s[^/]+)_(?P<component>.*)$
This expression is not the most readable, but it basically chunks the URL
into segments of ``(?P<name>...)``, so-called named groups (see a
`live demo of this expression <https://www.debuggex.com/r/Aa1yua-awXBuqZ39>`__).
This expression, and additional ones like it, can set as a configuration
parameter of an *uncurl* remote setup. Extending the configuration established
by the ``initremote`` call above::
$ git annex enableremote uncurl \\
'match=(?P<site>https://[^/]+)/(?P<collection>c[^/]+)/(?P<item>s[^/]+)_(?P<component>.*)$'
The last argument is quoted to prevent it from being processed by the shell.
With the match expression configured, URL rewriting can be enabled by declaring
a URL template as another configuration item. The URL template uses the `Python
Format String Syntax
<https://docs.python.org/3/library/string.html#format-string-syntax>`__. If the
new URL for the file above is now
``http://newsite.net/ex-archive/c542_s7612_figure1.pdf``, we can declare
the following URL template to have *uncurl* go to the new site::
http://newsite.net/ex-archive/{collection}_{item}_{component}
This template references the identifiers of the named groups we defined in the
match expression. Again, the URL template can be set via ``git annex
enableremote``::
$ git annex enableremote uncurl \\
'url=http://newsite.net/ex-archive/{collection}_{item}_{component}'
There is no need to separate the ``enableremote`` calls. Both configuration can
be given at the same time. In fact, they can also be given to ``initremote``
immediately.
The three identifiers ``site``, ``collection``, ``item``, and ``component`` are
actually a custom addition to a standard set of identifiers that are available
for composing URLs via a template.
- ``datalad_dsid`` - the DataLad dataset ID (UUID)
- ``annex_dirhash`` - "mixed" variant of the two level hash for a particular key
(uses POSIX directory separators, and included a trailing separator)
- ``annex_dirhash_lower`` - "lower case" variant of the two level hash for a
particular key (uses POSIX directory separators, and included a trailing
separator)
- ``annex_key`` - git-annex key name for a request
- ``annex_remoteuuid`` - UUID of the special remote (location) used by git-annex
- ``git_remotename`` - Name of the Git remote for the uncurl special remote
.. note::
The URL template must "resolve" to a complete and valid URL. This cannot
be verified at configuration time, because even the URL scheme could be a
dynamic setting.
Uploading content
-----------------
The *uncurl* special remote can upload file content or store annex keys
via supported URL schemes whenever a URL template is defined. At minimum,
storing at ``file://`` and ``ssh://`` URLs are supported. But other URL
scheme handlers with upload support may be available at a local DataLad
installation.
Deleting content
----------------
As for uploading, deleting content is only permitted with a configured
URL template. Moreover, it also depends on the delete operation being
supported for a particular URL scheme.
Configuration overrides
-----------------------
Both match expressions and the URL template can also be configured in a dataset's
configuration (committed branch configuration, or any Git configuration scope
(local, global, system) using the following configuration item names:
- ``remote.<remotename>.uncurl-url``
- ``remote.<remotename>.uncurl-match``
where ``<remotename>`` is the name of the special remote in the dataset.
A URL template provided via configuration *overrides* one defined in the special
remote setup via ``init/enableremote``.
Match expressions defined as configuration items *extend* the set of match
expressions that may be included in the special remote setup via
``init/enableremote``. The ``remote.<remotename>.uncurl-match`` configuration
item can be set as often as necessary (which one match expression each).
Tips
----
When multiple match expressions are defined, it is recommended to use unique
names for each match-group to avoid collisions.
"""
from __future__ import annotations
from functools import partial
from pathlib import Path
import re
# we intentionally limit ourselves to the most basic interface
# and even that we only need to get a `ConfigManager` instance.
# If that class would support a plain path argument, we could
# avoid it entirely
from datalad_next.datasets import LeanAnnexRepo
from datalad_next.exceptions import (
CapturedException,
UrlOperationsRemoteError,
UrlOperationsResourceUnknown,
)
from datalad_next.url_operations.any import AnyUrlOperations
from datalad_next.utils import ensure_list
from . import (
RemoteError,
SpecialRemote,
super_main
)
class UncurlRemote(SpecialRemote):
""" """
def __init__(self, annex):
super().__init__(annex)
self.configs.update(
url='Python format language template composing an access URL',
match='(whitespace-separated list of) regular expression(s) to match particular components in supported URL via named groups',
)
self.url_tmpl = None
self.match = None
self.url_handler = None
# cache of properties that do not vary within a session
# or across annex keys
self.persistent_tmpl_props = {}
def initremote(self):
# at present there is nothing that needs to be done on init/enable.
# the remote is designed to work without any specific setup too
pass
def prepare(self):
# we need the git remote name to be able to look up config about
# that remote
# check the config for a URL template setting
self.url_tmpl = self.get_remote_gitcfg('uncurl', 'url', '')
# only if we have no local, overriding, configuration ask git-annex
# for the committed special remote config on the URL template
if not self.url_tmpl:
# ask for the commit config, could still be empty
self.url_tmpl = self.annex.getconfig('url')
# TODO test the case of a fully absent URL template
# that would be fine and only verbatim recorded URLs could
# be sent to a downloader
# unconditionally ask git-annex for a match-url setting, any local
# config amends, and does not override
self.match = self.annex.getconfig('match')
if self.match:
self.match = self.match.split()
# TODO implement sanity checks, but running it through re.compile()
# might just be enough
self.match = [re.compile(m) for m in self.match]
# extend with additional matchers from local config
self.match = (self.match or []) + [
re.compile(m)
for m in ensure_list(self.get_remote_gitcfg(
'uncurl', 'match', default=[], get_all=True))
]
self.message(
f'URL rewriting template: {self.url_tmpl!r}', type='debug')
self.message(
f'Active URL match expressions: {[e.pattern for e in self.match]!r}',
type='debug')
# let the URL handler use the repo's config
self.url_handler = AnyUrlOperations(cfg=self.repo.cfg)
# cache template properties
# using function arg name syntax, we need the identifiers to be valid
# Python symbols to work in `format()`
self.persistent_tmpl_props.update(
datalad_dsid=self.repo.cfg.get('datalad.dataset.id', ''),
git_remotename=self.remotename,
annex_remoteuuid=self.annex.getuuid(),
)
def claimurl(self, url):
"""Needs to check if want to handle a given URL
If match expressions are configured, matches the URL against all known
URL expressions, and returns `True` if there is any match, or
`False` otherwise.
If no match expressions are configured, return `True` of the URL
scheme is supported, or `False` otherwise.
"""
if self.match:
return self.is_recognized_url(url)
else:
return self.url_handler.is_supported_url(url)
def checkurl(self, url):
"""
When running `git-annex addurl`, this is called after CLAIMURL
indicated that we could handle a URL. It can return information
on the URL target (e.g., size of the download, a target filename,
or a sequence thereof with additional URLs pointing to individual
components that would jointly make up the full download from the
given URL. However, all of that is optional, and a simple `True`
returned is sufficient to make git-annex call `TRANSFER RETRIEVE`.
"""
# try-except, because a URL template might need something
# that cannot be extracted from this very URL.
# we cannot consult a key that may already point to the same
# content as the URL, and may have other information --
# we simply dont have it at this point
try:
url = self.get_mangled_url(
url,
self.url_tmpl,
self.extract_tmpl_props(
tmpl=self.url_tmpl,
urls=[url],
),
)
except KeyError as e:
self.message(
'URL rewriting template requires unavailable component '
f'{e}, continuing with original URL',
type='debug',
)
# otherwise go ahead with the original URL. the template might
# just be here to aid structured uploads
try:
urlprops = self.url_handler.stat(url)
return True
except UrlOperationsRemoteError as e:
# leave a trace in the logs
CapturedException(e)
return False
# we could return a URL/size/filename triple instead of a bool.
# this would make git annex download from a URL different from the input,
# and to the given filename.
# it could be nice to report the (mangled) url, even if the handler reports
# a potentially deviating URL (redirects, etc.). Keeping external
# resolvers in the loop can be intentional, and users could provide
# resolved URL if they consider that desirable.
# however, going with the original URL kinda does that already, rewriting
# is happening anyways. And not reporting triplets avoids the issue
# of git-annex insisting to write into a dedicated directory for this
# download.
def transfer_retrieve(self, key, filename):
self._check_retrieve(
key,
partial(self.url_handler.download, to_path=Path(filename)),
('download', 'from'),
)
def checkpresent(self, key):
return self._check_retrieve(
key,
self.url_handler.stat,
('find', 'at'),
)
def transfer_store(self, key, filename):
return self._store_delete(
key,
partial(self.url_handler.upload, from_path=Path(filename)),
'cannot store',
)
def remove(self, key):
try:
return self._store_delete(
key,
# we have to map parameter names to be able to use a common
# helper with transfer_store(), because UrlOperations.upload()
# needs to get the URL as a second argument, hence we need
# to pass parameters as keyword-args
lambda to_url: self.url_handler.delete(url=to_url),
'refuses to delete',
)
except UrlOperationsResourceUnknown:
self.message(
'f{key} not found at the remote, skipping', type='debug')
#
# helpers
#
def is_recognized_url(self, url):
return any(m.match(url) for m in self.match or [])
def get_key_urls(self, key) -> list[str]:
# ask git-annex for the URLs it has on record for the key.
# this will also work within checkurl() for a temporary key
# generated by git-annex after claimurl()
urls = self.annex.geturls(key, prefix='')
self.message(f"Known urls for {key!r}: {urls}", type='debug')
if self.url_tmpl:
# we have a rewriting template. extract all properties
# from all known URLs and instantiate the template
# to get the ONE desired URL
props = self.extract_tmpl_props(
tmpl=self.url_tmpl,
urls=urls,
key=key,
)
url = self.get_mangled_url(
fallback_url=None,
tmpl=self.url_tmpl,
tmpl_props=props,
)
return [url]
# we have no rewriting template, and must return all URLs we know
# to let the caller sort it out
return urls
def get_mangled_url(self, fallback_url, tmpl, tmpl_props):
if not tmpl:
# the best thing we can do without a URL template is to
# return the URL itself
return fallback_url
url = tmpl.format(**tmpl_props)
return url
def extract_tmpl_props(self, tmpl, *, urls=None, key=None):
# look up all the standard
allprops = dict(self.persistent_tmpl_props)
if key:
allprops['annex_key'] = key
# if we are working on a specific key, check the template if it
# needs more key-specific properties. The conditionals below
# are intentionally imprecise to avoid false-negatives given the
# flexibility of the format-string-syntax
if 'annex_dirhash' in tmpl:
allprops['annex_dirhash'] = self.annex.dirhash(key)
if 'annex_dirhash_lower' in tmpl:
allprops['annex_dirhash_lower'] = self.annex.dirhash_lower(key)
# try all URLs against all matchers
for url in ensure_list(urls):
for matcher in (self.match or []):
match = matcher.match(url)
if not match:
# ignore any non-match
continue
# we only support named groups in expressions so this is sufficient
props = match.groupdict()
if any(p in allprops and allprops[p] != props[p] for p in props):
self.message(
'Partial URL property shadowing detected. '
'Avoid by using unique expression match group names.',
type='debug'
)
allprops.update(props)
return allprops
def _check_retrieve(self, key, handler, action: tuple):
urls = self.get_key_urls(key)
# depending on the configuration (rewriting template or not)
# we could have one or more URLs to try
for url in urls:
try:
handler(url)
# we succeeded, no need to try again
return True
except UrlOperationsResourceUnknown:
# general system access worked, but at the key location is nothing
# to be found
return False
except UrlOperationsRemoteError as e:
# return False only if we could be sure that the remote
# system works properly and just the key is not around
ce = CapturedException(e)
self.message(
f'Failed to {action[0]} key {key!r} {action[1]} {url!r}',
type='debug')
raise RemoteError(
f'Failed to {action[0]} {key!r} {action[1]} any of {urls!r}')
def _store_delete(self, key, handler, action: str):
if not self.url_tmpl:
raise RemoteError(
f'Remote {action} content without a configured URL template')
url = self.get_key_urls(key)
# we have a rewriting template, so we expect exactly one URL
assert len(url) == 1
url = url[0]
try:
handler(to_url=url)
except UrlOperationsResourceUnknown:
# pass-through, would happen when removing a non-existing key,
# which git-annex wants to be a OK thing to happen.
# handler in callers
raise
except Exception as e:
# we need to raise RemoteError whenever we could not perform
raise RemoteError from e
_stat2checkurl_map = {
'content-length': 'size',
}
"""Translate property names returned by AnyUrlOperations.stat()
to those expected from checkurl()"""
def main():
"""cmdline entry point"""
super_main(
cls=UncurlRemote,
remote_name='uncurl',
description=\
"flexible access data (in archive systems) "
"via a variety of identification schemes",
)