Skip to content

Commit a9bd96e

Browse files
adamshapiro0bhers4
authored andcommitted
Added tar file format control support.
1 parent 1ef300e commit a9bd96e

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

pkg/private/tar/build_tar.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class DebError(Exception):
4343
pass
4444

4545
def __init__(self, output, directory, compression, compressor, create_parents,
46-
allow_dups_from_deps, default_mtime, compression_level):
46+
allow_dups_from_deps, default_mtime, compression_level,
47+
tar_format=None):
4748
# Directory prefix on all output paths
4849
d = directory.strip('/')
4950
self.directory = (d + '/') if d else None
@@ -54,6 +55,7 @@ def __init__(self, output, directory, compression, compressor, create_parents,
5455
self.create_parents = create_parents
5556
self.allow_dups_from_deps = allow_dups_from_deps
5657
self.compression_level = compression_level
58+
self.tar_format = tar_format
5759

5860
def __enter__(self):
5961
self.tarfile = tar_writer.TarFileWriter(
@@ -63,7 +65,8 @@ def __enter__(self):
6365
self.create_parents,
6466
self.allow_dups_from_deps,
6567
default_mtime=self.default_mtime,
66-
compression_level=self.compression_level)
68+
compression_level=self.compression_level,
69+
format=self.tar_format)
6770
return self
6871

6972
def __exit__(self, t, v, traceback):
@@ -375,7 +378,9 @@ def main():
375378
compression.add_argument('--compressor',
376379
help='Compressor program and arguments, '
377380
'e.g. `pigz -p 4`')
378-
381+
parser.add_argument(
382+
'--format', default='DEFAULT', choices=('DEFAULT', 'USTAR', 'GNU', 'PAX'),
383+
help='Specify the tar format to use: USTAR, GNU, PAX (default)')
379384
parser.add_argument(
380385
'--modes', action='append',
381386
help='Specific mode to apply to specific file (from the file argument),'
@@ -449,6 +454,15 @@ def main():
449454
f = f[1:]
450455
ids_map[f] = (int(user), int(group))
451456

457+
if options.format == 'DEFAULT':
458+
tar_format = tarfile.DEFAULT_FORMAT
459+
elif options.format == 'USTAR':
460+
tar_format = tarfile.USTAR_FORMAT
461+
elif options.format == 'GNU':
462+
tar_format = tarfile.GNU_FORMAT
463+
elif options.format == 'PAX':
464+
tar_format = tarfile.PAX_FORMAT
465+
452466
default_mtime = options.mtime
453467
if options.stamp_from:
454468
default_mtime = build_info.get_timestamp(options.stamp_from)
@@ -466,7 +480,8 @@ def main():
466480
default_mtime=default_mtime,
467481
create_parents=options.create_parents,
468482
allow_dups_from_deps=options.allow_dups_from_deps,
469-
compression_level = compression_level) as output:
483+
compression_level = compression_level,
484+
tar_format=tar_format) as output:
470485

471486
def file_attributes(filename):
472487
if filename.startswith('/'):

pkg/private/tar/tar.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def _pkg_tar_impl(ctx):
6363
args.add("--output", output_file.path)
6464
args.add("--owner", ctx.attr.owner)
6565
args.add("--owner_name", ctx.attr.ownername)
66+
args.add("--format", ctx.attr.format)
6667
if ctx.attr.mode != "":
6768
args.add("--mode", ctx.attr.mode)
6869
# Package dir can be specified by a file or inlined.
@@ -246,6 +247,7 @@ pkg_tar_impl = rule(
246247
doc = """Obsolete. Do not use.""",
247248
allow_files = True,
248249
),
250+
"format": attr.string(default = "DEFAULT"),
249251
"mode": attr.string(),
250252
"modes": attr.string_dict(),
251253
"mtime": attr.int(default = _DEFAULT_MTIME),

pkg/private/tar/tar_writer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self,
4949
create_parents=False,
5050
allow_dups_from_deps=True,
5151
default_mtime=None,
52+
format=None,
5253
preserve_tar_mtimes=True,
5354
compression_level=-1):
5455
"""TarFileWriter wraps tarfile.open().
@@ -70,6 +71,9 @@ def __init__(self,
7071
else:
7172
self.default_mtime = int(default_mtime)
7273

74+
if format is None:
75+
format = tarfile.DEFAULT_FORMAT
76+
7377
self.fileobj = None
7478
self.compressor_cmd = (compressor or '').strip()
7579
if self.compressor_cmd:
@@ -102,7 +106,7 @@ def __init__(self,
102106
self.name = name
103107

104108
self.tar = tarfile.open(name=name, mode=mode, fileobj=self.fileobj,
105-
format=tarfile.GNU_FORMAT)
109+
format=format)
106110
self.members = set()
107111
self.directories = set()
108112
# Preseed the added directory list with things we should not add. If we

0 commit comments

Comments
 (0)