Skip to content

Commit 61d3fd0

Browse files
authored
Merge pull request codecov#215 from nmoinvaz/improvements/gzip-reports
Added support for gzipping reports.
2 parents 4d87117 + 6a8d722 commit 61d3fd0

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

codecov/__init__.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import requests
88
import argparse
99
import fnmatch
10+
import zlib
1011
from time import sleep
1112
from json import loads
1213

@@ -1021,6 +1022,16 @@ def main(*argv, **kwargs):
10211022
write(reports)
10221023
write("-------------------------------------------------")
10231024

1025+
# Handle reports encoding for Python 2 and 3
1026+
if not isinstance(reports, bytes):
1027+
reports = reports.encode("utf-8")
1028+
1029+
# Compress reports using zlib and output with gzip header
1030+
write(" Gzipping contents..")
1031+
gzip_worker = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
1032+
reports_gzip = gzip_worker.compress(reports) + gzip_worker.flush()
1033+
write(" Compressed contents to {0} bytes".format(len(reports_gzip)))
1034+
10241035
s3 = None
10251036
trys = 0
10261037
while trys < 3:
@@ -1034,7 +1045,8 @@ def main(*argv, **kwargs):
10341045
headers={
10351046
"Accept": "text/plain",
10361047
"X-Reduced-Redundancy": "false",
1037-
},
1048+
"X-Content-Type": "application/x-gzip"
1049+
}
10381050
)
10391051
if res.status_code in (400, 406):
10401052
raise Exception(res.text)
@@ -1044,15 +1056,14 @@ def main(*argv, **kwargs):
10441056
res = res.text.strip().split()
10451057
result, upload_url = res[0], res[1]
10461058

1047-
# Handle reports encoding for Python 2 and 3
1048-
if not isinstance(reports, bytes):
1049-
reports = reports.encode("utf-8")
1050-
10511059
write(" Uploading to S3...")
10521060
s3 = requests.put(
10531061
upload_url,
1054-
data=reports,
1055-
headers={"Content-Type": "text/plain",},
1062+
data=reports_gzip,
1063+
headers={
1064+
"Content-Type": "application/x-gzip",
1065+
"Content-Encoding": "gzip"
1066+
}
10561067
)
10571068
s3.raise_for_status()
10581069
assert s3.status_code == 200
@@ -1070,10 +1081,12 @@ def main(*argv, **kwargs):
10701081
res = requests.post(
10711082
"%s/upload/v2?%s" % (codecov.url, urlargs),
10721083
verify=codecov.cacert,
1073-
data="\n".join(
1074-
(reports, s3.reason if s3 else "", s3.text if s3 else "")
1075-
),
1076-
headers={"Accept": "text/plain"},
1084+
data=reports_gzip,
1085+
headers={
1086+
"Accept": "text/plain",
1087+
"Content-Type": "application/x-gzip",
1088+
"Content-Encoding": "gzip"
1089+
}
10771090
)
10781091
if res.status_code < 500:
10791092
write(" " + res.text)

tests/test.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import pickle
44
import itertools
5+
import zlib
56
from ddt import ddt, data
67
from mock import patch, Mock
78
import unittest
@@ -267,7 +268,9 @@ def test_send(self):
267268
)
268269
assert "token=%3Ctoken%3E" in post.call_args[0][0]
269270
assert "branch=master" in post.call_args[0][0]
270-
assert u"tests/test.py".encode("utf-8") in put.call_args[1]["data"]
271+
gzip_worker = zlib.decompressobj(zlib.MAX_WBITS | 16)
272+
reports = gzip_worker.decompress(put.call_args[1]["data"]) + gzip_worker.flush()
273+
assert u"tests/test.py".encode("utf-8") in reports
271274

272275
def test_send_error(self):
273276
with patch("requests.post") as post:

0 commit comments

Comments
 (0)