| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import datetime |
| 4 |
import logging |
| 5 |
import typing as t |
| 6 |
import uuid |
| 7 |
|
| 8 |
from globus_sdk._internal.remarshal import stringify |
| 9 |
from globus_sdk._missing import MISSING, MissingType |
| 10 |
from globus_sdk._payload import GlobusPayload |
| 11 |
|
| 12 |
log = logging.getLogger(__name__) |
| 13 |
_sync_level_dict: dict[t.Literal["exists", "size", "mtime", "checksum"], int] = { |
| 14 |
"exists": 0, |
| 15 |
"size": 1, |
| 16 |
"mtime": 2, |
| 17 |
"checksum": 3, |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
def _parse_sync_level( |
| 22 |
sync_level: t.Literal["exists", "size", "mtime", "checksum"] | int | MissingType, |
| 23 |
) -> int | MissingType: |
| 24 |
""" |
| 25 |
Map sync_level strings to known int values |
| 26 |
|
| 27 |
Important: if more levels are added in the future you can always pass as an int |
| 28 |
""" |
| 29 |
if isinstance(sync_level, str): |
| 30 |
try: |
| 31 |
sync_level = _sync_level_dict[sync_level] |
| 32 |
except KeyError as err: |
| 33 |
raise ValueError(f"Unrecognized sync_level {sync_level}") from err |
| 34 |
return sync_level |
| 35 |
|
| 36 |
|
| 37 |
class TransferData(GlobusPayload): |
| 38 |
r""" |
| 39 |
Convenience class for constructing a transfer document, to use as the |
| 40 |
``data`` parameter to |
| 41 |
:meth:`submit_transfer <globus_sdk.TransferClient.submit_transfer>`. |
| 42 |
|
| 43 |
At least one item must be added using |
| 44 |
:meth:`add_item <globus_sdk.TransferData.add_item>`. |
| 45 |
|
| 46 |
:param source_endpoint: The endpoint ID of the source endpoint |
| 47 |
:param destination_endpoint: The endpoint ID of the destination endpoint |
| 48 |
:param label: A string label for the Task |
| 49 |
:param submission_id: A submission ID value fetched via :meth:`get_submission_id \ |
| 50 |
<globus_sdk.TransferClient.get_submission_id>`. By default, the SDK |
| 51 |
will fetch and populate this field when :meth:`submit_transfer \ |
| 52 |
<globus_sdk.TransferClient.submit_transfer>` is called. |
| 53 |
:param sync_level: The method used to compare items between the source and |
| 54 |
destination. One of ``"exists"``, ``"size"``, ``"mtime"``, or ``"checksum"`` |
| 55 |
See the section below on sync-level for an explanation of values. |
| 56 |
:param verify_checksum: When true, after transfer verify that the source and |
| 57 |
destination file checksums match. If they don't, re-transfer the entire file and |
| 58 |
keep trying until it succeeds. This will create CPU load on both the origin and |
| 59 |
destination of the transfer, and may even be a bottleneck if the network speed |
| 60 |
is high enough. |
| 61 |
[default: ``False``] |
| 62 |
:param preserve_timestamp: When true, Globus Transfer will attempt to set file |
| 63 |
timestamps on the destination to match those on the origin. [default: ``False``] |
| 64 |
:param encrypt_data: When true, all files will be TLS-protected during transfer. |
| 65 |
[default: ``False``] |
| 66 |
:param deadline: An ISO-8601 timestamp (as a string) or a datetime object which |
| 67 |
defines a deadline for the transfer. At the deadline, even if the data transfer |
| 68 |
is not complete, the job will be canceled. We recommend ensuring that the |
| 69 |
timestamp is in UTC to avoid confusion and ambiguity. Examples of ISO-8601 |
| 70 |
timestamps include ``2017-10-12 09:30Z``, ``2017-10-12 12:33:54+00:00``, and |
| 71 |
``2017-10-12`` |
| 72 |
:param skip_source_errors: When true, source permission denied and file |
| 73 |
not found errors from the source endpoint will cause the offending |
| 74 |
path to be skipped. |
| 75 |
[default: ``False``] |
| 76 |
:param fail_on_quota_errors: When true, quota exceeded errors will cause the |
| 77 |
task to fail. |
| 78 |
[default: ``False``] |
| 79 |
:param delete_destination_extra: Delete files and directories on the |
| 80 |
destination endpoint which don’t exist on the source endpoint or are a |
| 81 |
different type. Only applies for recursive directory transfers. |
| 82 |
[default: ``False``] |
| 83 |
:param notify_on_succeeded: Send a notification email when the transfer completes |
| 84 |
with a status of SUCCEEDED. |
| 85 |
[default: ``True``] |
| 86 |
:param notify_on_failed: Send a notification email when the transfer completes |
| 87 |
with a status of FAILED. |
| 88 |
[default: ``True``] |
| 89 |
:param notify_on_inactive: Send a notification email when the transfer changes |
| 90 |
status to INACTIVE. e.g. From credentials expiring. |
| 91 |
[default: ``True``] |
| 92 |
:param source_local_user: Optional value passed to the source's identity mapping |
| 93 |
specifying which local user account to map to. Only usable with Globus Connect |
| 94 |
Server v5 mapped collections. |
| 95 |
:param destination_local_user: Optional value passed to the destination's identity |
| 96 |
mapping specifying which local user account to map to. Only usable with Globus |
| 97 |
Connect Server v5 mapped collections. |
| 98 |
:param additional_fields: additional fields to be added to the transfer |
| 99 |
document. Mostly intended for internal use |
| 100 |
|
| 101 |
**Sync Levels** |
| 102 |
|
| 103 |
The values for ``sync_level`` are used to determine how comparisons are made between |
| 104 |
files found both on the source and the destination. When files match, no data |
| 105 |
transfer will occur. |
| 106 |
|
| 107 |
For compatibility, this can be an integer ``0``, ``1``, ``2``, or ``3`` in addition |
| 108 |
to the string values. |
| 109 |
|
| 110 |
The meanings are as follows: |
| 111 |
|
| 112 |
===================== ======== |
| 113 |
value behavior |
| 114 |
===================== ======== |
| 115 |
``0``, ``exists`` Determine whether or not to transfer based on file |
| 116 |
existence. If the destination file is absent, do the |
| 117 |
transfer. |
| 118 |
``1``, ``size`` Determine whether or not to transfer based on the size of |
| 119 |
the file. If destination file size does not match the |
| 120 |
source, do the transfer. |
| 121 |
``2``, ``mtime`` Determine whether or not to transfer based on modification |
| 122 |
times. If source has a newer modified time than the |
| 123 |
destination, do the transfer. |
| 124 |
``3``, ``checksum`` Determine whether or not to transfer based on checksums of |
| 125 |
file contents. If source and destination contents differ, as |
| 126 |
determined by a checksum of their contents, do the transfer. |
| 127 |
===================== ======== |
| 128 |
|
| 129 |
**Examples** |
| 130 |
|
| 131 |
See the |
| 132 |
:meth:`submit_transfer <globus_sdk.TransferClient.submit_transfer>` |
| 133 |
documentation for example usage. |
| 134 |
|
| 135 |
**External Documentation** |
| 136 |
|
| 137 |
See the |
| 138 |
`Task document definition \ |
| 139 |
<https://docs.globus.org/api/transfer/task_submit/#document_types>`_ |
| 140 |
and |
| 141 |
`Transfer specific fields \ |
| 142 |
<https://docs.globus.org/api/transfer/task_submit/#transfer_specific_fields>`_ |
| 143 |
in the REST documentation for more details on Transfer Task documents. |
| 144 |
|
| 145 |
.. automethodlist:: globus_sdk.TransferData |
| 146 |
""" |
| 147 |
|
| 148 |
def __init__( |
| 149 |
self, |
| 150 |
source_endpoint: uuid.UUID | str, |
| 151 |
destination_endpoint: uuid.UUID | str, |
| 152 |
*, |
| 153 |
label: str | MissingType = MISSING, |
| 154 |
submission_id: uuid.UUID | str | MissingType = MISSING, |
| 155 |
sync_level: ( |
| 156 |
int | t.Literal["exists", "size", "mtime", "checksum"] | MissingType |
| 157 |
) = MISSING, |
| 158 |
verify_checksum: bool | MissingType = MISSING, |
| 159 |
preserve_timestamp: bool | MissingType = MISSING, |
| 160 |
encrypt_data: bool | MissingType = MISSING, |
| 161 |
deadline: datetime.datetime | str | MissingType = MISSING, |
| 162 |
skip_source_errors: bool | MissingType = MISSING, |
| 163 |
fail_on_quota_errors: bool | MissingType = MISSING, |
| 164 |
delete_destination_extra: bool | MissingType = MISSING, |
| 165 |
notify_on_succeeded: bool | MissingType = MISSING, |
| 166 |
notify_on_failed: bool | MissingType = MISSING, |
| 167 |
notify_on_inactive: bool | MissingType = MISSING, |
| 168 |
source_local_user: str | MissingType = MISSING, |
| 169 |
destination_local_user: str | MissingType = MISSING, |
| 170 |
additional_fields: dict[str, t.Any] | None = None, |
| 171 |
) -> None: |
| 172 |
super().__init__() |
| 173 |
log.debug("Creating a new TransferData object") |
| 174 |
self["DATA_TYPE"] = "transfer" |
| 175 |
self["DATA"] = [] |
| 176 |
self["source_endpoint"] = source_endpoint |
| 177 |
self["destination_endpoint"] = destination_endpoint |
| 178 |
self["label"] = label |
| 179 |
self["submission_id"] = submission_id |
| 180 |
self["deadline"] = stringify(deadline) |
| 181 |
self["source_local_user"] = source_local_user |
| 182 |
self["destination_local_user"] = destination_local_user |
| 183 |
self["verify_checksum"] = verify_checksum |
| 184 |
self["preserve_timestamp"] = preserve_timestamp |
| 185 |
self["encrypt_data"] = encrypt_data |
| 186 |
self["skip_source_errors"] = skip_source_errors |
| 187 |
self["fail_on_quota_errors"] = fail_on_quota_errors |
| 188 |
self["delete_destination_extra"] = delete_destination_extra |
| 189 |
self["notify_on_succeeded"] = notify_on_succeeded |
| 190 |
self["notify_on_failed"] = notify_on_failed |
| 191 |
self["notify_on_inactive"] = notify_on_inactive |
| 192 |
self["sync_level"] = _parse_sync_level(sync_level) |
| 193 |
|
| 194 |
for k, v in self.items(): |
| 195 |
log.debug("TransferData.%s = %s", k, v) |
| 196 |
|
| 197 |
if additional_fields is not None: |
| 198 |
self.update(additional_fields) |
| 199 |
for option, value in additional_fields.items(): |
| 200 |
log.debug( |
| 201 |
f"TransferData.{option} = {value} (option passed " |
| 202 |
"in via additional_fields)" |
| 203 |
) |
| 204 |
|
| 205 |
def add_item( |
| 206 |
self, |
| 207 |
source_path: str, |
| 208 |
destination_path: str, |
| 209 |
*, |
| 210 |
recursive: bool | MissingType = MISSING, |
| 211 |
external_checksum: str | MissingType = MISSING, |
| 212 |
checksum_algorithm: str | MissingType = MISSING, |
| 213 |
additional_fields: dict[str, t.Any] | None = None, |
| 214 |
) -> None: |
| 215 |
""" |
| 216 |
Add a file or directory to be transferred. |
| 217 |
|
| 218 |
Appends a transfer_item document to the DATA key of the transfer |
| 219 |
document. |
| 220 |
|
| 221 |
.. note:: |
| 222 |
|
| 223 |
The full path to the destination file must be provided for file items. |
| 224 |
Parent directories of files are not allowed. See |
| 225 |
`task submission documentation |
| 226 |
<https://docs.globus.org/api/transfer/task_submit/#submit_transfer_task>`_ |
| 227 |
for more details. |
| 228 |
|
| 229 |
:param source_path: Path to the source directory or file to be transferred |
| 230 |
:param destination_path: Path to the destination directory or file will be |
| 231 |
transferred to |
| 232 |
:param recursive: Set to True if the target at source path is a directory |
| 233 |
:param external_checksum: A checksum to verify both source file and destination |
| 234 |
file integrity. The checksum will be verified after the data transfer and a |
| 235 |
failure will cause the entire task to fail. Cannot be used with directories. |
| 236 |
Assumed to be an MD5 checksum unless checksum_algorithm is also given. |
| 237 |
:param checksum_algorithm: Specifies the checksum algorithm to be used when |
| 238 |
verify_checksum is True, sync_level is "checksum" or 3, or an |
| 239 |
external_checksum is given. |
| 240 |
:param additional_fields: additional fields to be added to the transfer item |
| 241 |
""" |
| 242 |
item_data: dict[str, t.Any] = { |
| 243 |
"DATA_TYPE": "transfer_item", |
| 244 |
"source_path": source_path, |
| 245 |
"destination_path": destination_path, |
| 246 |
"recursive": recursive, |
| 247 |
"external_checksum": external_checksum, |
| 248 |
"checksum_algorithm": checksum_algorithm, |
| 249 |
**(additional_fields or {}), |
| 250 |
} |
| 251 |
log.debug( |
| 252 |
'TransferData[{}, {}].add_item: "{}"->"{}"'.format( |
| 253 |
self["source_endpoint"], |
| 254 |
self["destination_endpoint"], |
| 255 |
source_path, |
| 256 |
destination_path, |
| 257 |
) |
| 258 |
) |
| 259 |
self["DATA"].append(item_data) |
| 260 |
|
| 261 |
def add_filter_rule( |
| 262 |
self, |
| 263 |
name: str, |
| 264 |
*, |
| 265 |
method: t.Literal["include", "exclude"] = "exclude", |
| 266 |
type: ( # pylint: disable=redefined-builtin |
| 267 |
t.Literal["file", "dir"] | MissingType |
| 268 |
) = MISSING, |
| 269 |
) -> None: |
| 270 |
""" |
| 271 |
Add a filter rule to the transfer document. |
| 272 |
|
| 273 |
These rules specify which items are or are not included when recursively |
| 274 |
transferring directories. Each item that is found during recursive directory |
| 275 |
traversal is matched against these rules in the order they are listed. |
| 276 |
The method of the first filter rule that matches an item is applied (either |
| 277 |
"include" or "exclude"), and filter rule matching stops. If no rules match, |
| 278 |
the item is included in the transfer. Notably, this makes "include" filter |
| 279 |
rules only useful when overriding more general "exclude" filter rules later |
| 280 |
in the list. |
| 281 |
|
| 282 |
:param name: A pattern to match against item names. Wildcards are supported, as |
| 283 |
are character groups: ``*`` matches everything, ``?`` matches any single |
| 284 |
character, ``[]`` matches any single character within the brackets, and |
| 285 |
``[!]`` matches any single character not within the brackets. |
| 286 |
:param method: The method to use for filtering. If "exclude" (the default) |
| 287 |
items matching this rule will not be included in the transfer. If |
| 288 |
"include" items matching this rule will be included in the transfer. |
| 289 |
:param type: The types of items on which to apply this filter rule. Either |
| 290 |
``"file"`` or ``"dir"``. If unspecified, the rule applies to both. |
| 291 |
Note that if a ``"dir"`` is excluded then all items within it will |
| 292 |
also be excluded regardless if they would have matched any include rules. |
| 293 |
|
| 294 |
Example Usage: |
| 295 |
|
| 296 |
>>> tdata = TransferData(...) |
| 297 |
>>> tdata.add_filter_rule(method="exclude", "*.tgz", type="file") |
| 298 |
>>> tdata.add_filter_rule(method="exclude", "*.tar.gz", type="file") |
| 299 |
|
| 300 |
``tdata`` now describes a transfer which will skip any gzipped tar files with |
| 301 |
the extensions ``.tgz`` or ``.tar.gz`` |
| 302 |
|
| 303 |
>>> tdata = TransferData(...) |
| 304 |
>>> tdata.add_filter_rule(method="include", "*.txt", type="file") |
| 305 |
>>> tdata.add_filter_rule(method="exclude", "*", type="file") |
| 306 |
|
| 307 |
``tdata`` now describes a transfer which will only transfer files |
| 308 |
with the ``.txt`` extension. |
| 309 |
""" |
| 310 |
if self.get("filter_rules", MISSING) is MISSING: |
| 311 |
self["filter_rules"] = [] |
| 312 |
rule = { |
| 313 |
"DATA_TYPE": "filter_rule", |
| 314 |
"method": method, |
| 315 |
"name": name, |
| 316 |
"type": type, |
| 317 |
} |
| 318 |
self["filter_rules"].append(rule) |
| 319 |
|
| 320 |
def iter_items(self) -> t.Iterator[dict[str, t.Any]]: |
| 321 |
""" |
| 322 |
An iterator of items created by ``add_item``. |
| 323 |
|
| 324 |
Each item takes the form of a dictionary. |
| 325 |
""" |
| 326 |
yield from iter(self["DATA"]) |