Skip to content

Commit

Permalink
Disable compression by default when using get_archive method
Browse files Browse the repository at this point in the history
Signed-off-by: Niklas Saari <niklas.saari@tutanota.com>
  • Loading branch information
Nicceboy committed Feb 26, 2020
1 parent 030af62 commit 51fd6dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
12 changes: 10 additions & 2 deletions docker/api/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,8 @@ def export(self, container, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
return self._stream_raw_result(res, chunk_size, False)

@utils.check_resource('container')
def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE,
encode_stream=False):
"""
Retrieve a file or folder from a container in the form of a tar
archive.
Expand All @@ -705,6 +706,8 @@ def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
chunk_size (int): The number of bytes returned by each iteration
of the generator. If ``None``, data will be streamed as it is
received. Default: 2 MB
encode_stream (bool): Determines if data should be encoded
(gzip-compressed) during transmission. Default: False
Returns:
(tuple): First element is a raw tar data stream. Second element is
Expand All @@ -729,8 +732,13 @@ def get_archive(self, container, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
params = {
'path': path
}
headers = {
"Accept-Encoding": "gzip, deflate"
} if encode_stream else {
"Accept-Encoding": "identity"
}
url = self._url('/containers/{0}/archive', container)
res = self._get(url, params=params, stream=True)
res = self._get(url, params=params, stream=True, headers=headers)
self._raise_for_status(res)
encoded_stat = res.headers.get('x-docker-container-path-stat')
return (
Expand Down
8 changes: 6 additions & 2 deletions docker/models/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def export(self, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
"""
return self.client.api.export(self.id, chunk_size)

def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE,
encode_stream=False):
"""
Retrieve a file or folder from the container in the form of a tar
archive.
Expand All @@ -235,6 +236,8 @@ def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
chunk_size (int): The number of bytes returned by each iteration
of the generator. If ``None``, data will be streamed as it is
received. Default: 2 MB
encode_stream (bool): Determines if data should be encoded
(gzip-compressed) during transmission. Default: False
Returns:
(tuple): First element is a raw tar data stream. Second element is
Expand All @@ -255,7 +258,8 @@ def get_archive(self, path, chunk_size=DEFAULT_DATA_CHUNK_SIZE):
... f.write(chunk)
>>> f.close()
"""
return self.client.api.get_archive(self.id, path, chunk_size)
return self.client.api.get_archive(self.id, path,
chunk_size, encode_stream)

def kill(self, signal=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def test_get_archive(self):
container = client.containers.get(FAKE_CONTAINER_ID)
container.get_archive('foo')
client.api.get_archive.assert_called_with(
FAKE_CONTAINER_ID, 'foo', DEFAULT_DATA_CHUNK_SIZE
FAKE_CONTAINER_ID, 'foo', DEFAULT_DATA_CHUNK_SIZE, False
)

def test_image(self):
Expand Down

0 comments on commit 51fd6dd

Please sign in to comment.