Skip to content

Commit 7ea49fa

Browse files
committed
Make compresslevel configurable for .tar.gz
1 parent 447fb8e commit 7ea49fa

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

pkg/private/tar/build_tar.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class TarFile(object):
4242
class DebError(Exception):
4343
pass
4444

45-
def __init__(self, output, directory, compression, compressor, create_parents, allow_dups_from_deps, default_mtime):
45+
def __init__(self, output, directory, compression, compressor, create_parents,
46+
allow_dups_from_deps, default_mtime, compresslevel=None):
4647
# Directory prefix on all output paths
4748
d = directory.strip('/')
4849
self.directory = (d + '/') if d else None
@@ -52,6 +53,7 @@ def __init__(self, output, directory, compression, compressor, create_parents, a
5253
self.default_mtime = default_mtime
5354
self.create_parents = create_parents
5455
self.allow_dups_from_deps = allow_dups_from_deps
56+
self.compresslevel = compresslevel
5557

5658
def __enter__(self):
5759
self.tarfile = tar_writer.TarFileWriter(
@@ -60,7 +62,8 @@ def __enter__(self):
6062
self.compressor,
6163
self.create_parents,
6264
self.allow_dups_from_deps,
63-
default_mtime=self.default_mtime)
65+
default_mtime=self.default_mtime,
66+
compresslevel=self.compresslevel)
6467
return self
6568

6669
def __exit__(self, t, v, traceback):
@@ -397,6 +400,9 @@ def main():
397400
parser.add_argument('--allow_dups_from_deps',
398401
action='store_true',
399402
help='')
403+
parser.add_argument(
404+
'--compresslevel', default='',
405+
help='Specify the numeric compress level in gzip mode; may be 0-9 or empty(6).')
400406
options = parser.parse_args()
401407

402408
# Parse modes arguments
@@ -448,7 +454,8 @@ def main():
448454
compressor = options.compressor,
449455
default_mtime=default_mtime,
450456
create_parents=options.create_parents,
451-
allow_dups_from_deps=options.allow_dups_from_deps) as output:
457+
allow_dups_from_deps=options.allow_dups_from_deps,
458+
compresslevel = options.compresslevel) as output:
452459

453460
def file_attributes(filename):
454461
if filename.startswith('/'):

pkg/private/tar/tar.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def _pkg_tar_impl(ctx):
114114
"--owner_names",
115115
"%s=%s" % (_quote(key), ctx.attr.ownernames[key]),
116116
)
117+
if ctx.attr.compresslevel:
118+
args.add("--compresslevel", ctx.attr.compresslevel)
117119

118120
# Now we begin processing the files.
119121
path_mapper = None
@@ -272,6 +274,10 @@ pkg_tar_impl = rule(
272274
),
273275
"create_parents": attr.bool(default = True),
274276
"allow_duplicates_from_deps": attr.bool(default = False),
277+
"compresslevel": attr.string(
278+
doc = """Specify the numeric compress level in gzip mode; may be 0-9 or empty (6).""",
279+
default = "",
280+
),
275281

276282
# Common attributes
277283
"out": attr.output(mandatory = True),

pkg/private/tar/tar_writer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def __init__(self,
4949
create_parents=False,
5050
allow_dups_from_deps=True,
5151
default_mtime=None,
52-
preserve_tar_mtimes=True):
52+
preserve_tar_mtimes=True,
53+
compresslevel=None):
5354
"""TarFileWriter wraps tarfile.open().
5455
5556
Args:
@@ -86,10 +87,11 @@ def __init__(self,
8687
else:
8788
mode = 'w:'
8889
if compression in ['tgz', 'gz']:
90+
compresslevel = int(compresslevel) if compresslevel or compresslevel == 0 else 6
8991
# The Tarfile class doesn't allow us to specify gzip's mtime attribute.
9092
# Instead, we manually reimplement gzopen from tarfile.py and set mtime.
9193
self.fileobj = gzip.GzipFile(
92-
filename=name, mode='w', compresslevel=6, mtime=self.default_mtime)
94+
filename=name, mode='w', compresslevel=compresslevel, mtime=self.default_mtime)
9395
self.compressor_proc = None
9496
if self.compressor_cmd:
9597
mode = 'w|'

0 commit comments

Comments
 (0)