summaryrefslogtreecommitdiffstats
path: root/third_party/python/taskcluster/taskcluster/download.py
blob: 5584398ea87247ec77dc7f9913d3d8390175be95 (plain)
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
"""
Support for downloading objects from the object service, following best
practices for that service.

Downloaded data is written to a "writer" provided by a "writer factory".  A
writer has a `write` method which writes the entire passed buffer to storage.
A writer factory is a callable which returns a fresh writer, ready to write the
first byte of the object.  When downloads are retried, the writer factory may
be called more than once.

This module provides several pre-defined writers and writer factories for
common cases.
"""
import functools
import six

if six.PY2:
    raise ImportError("download is only supported in Python 3")

from .aio import download as aio_download
from .aio.asyncutils import ensureCoro, runAsync


def downloadToBuf(**kwargs):
    """
    Convenience method to download data to an in-memory buffer and return the
    downloaded data.  Arguments are the same as `download`, except that
    `writerFactory` should not be supplied.  Returns a tuple (buffer, contentType).
    """
    return runAsync(aio_download.downloadToBuf(**kwargs))


def downloadToFile(file, **kwargs):
    """
    Convenience method to download data to a file object.  The file must be
    writeable, in binary mode, seekable (`f.seek`), and truncatable
    (`f.truncate`) to support retries.  Arguments are the same as `download`,
    except that `writerFactory` should not be supplied.  Returns the content-type.
    """
    return runAsync(aio_download.downloadToFile(file=file, **kwargs))


def download(*, writerFactory, **kwargs):
    """
    Download the named object from the object service, using a writer returned
    from `writerFactory` to write the data.  The `maxRetries` parameter has
    the same meaning as for service clients.  The `objectService` parameter is
    an instance of the Object class, configured with credentials for the
    upload.  Returns the content-type.
    """
    wrappedWriterFactory = _wrapSyncWriterFactory(writerFactory)
    return runAsync(aio_download.download(writerFactory=wrappedWriterFactory, **kwargs))


def downloadArtifactToBuf(**kwargs):
    """
    Convenience method to download an artifact to an in-memory buffer and return the
    downloaded data.  Arguments are the same as `downloadArtifact`, except that
    `writerFactory` should not be supplied.  Returns a tuple (buffer, contentType).
    """
    return runAsync(aio_download.downloadArtifactToBuf(**kwargs))


def downloadArtifactToFile(file, **kwargs):
    """
    Convenience method to download an artifact to a file object.  The file must be
    writeable, in binary mode, seekable (`f.seek`), and truncatable
    (`f.truncate`) to support retries.  Arguments are the same as `downloadArtifac`,
    except that `writerFactory` should not be supplied.  Returns the content-type.
    """
    return runAsync(aio_download.downloadArtifactToFile(file=file, **kwargs))


def downloadArtifact(*, writerFactory, **kwargs):
    """
    Download the named artifact with the appropriate storageType, using a writer returned
    from `writerFactory` to write the data.  The `maxRetries` parameter has
    the same meaning as for service clients.  The `queueService` parameter is
    an instance of the Queue class, configured with credentials for the
    download.  Returns the content-type.
    """
    wrappedWriterFactory = _wrapSyncWriterFactory(writerFactory)
    return runAsync(aio_download.downloadArtifact(writerFactory=wrappedWriterFactory, **kwargs))


def _wrapSyncWriterFactory(writerFactory):
    """Modify the reader returned by readerFactory to have an async read."""
    @functools.wraps(writerFactory)
    async def wrappedFactory():
        writer = writerFactory()
        writer.write = ensureCoro(writer.write)
        return writer

    return wrappedFactory