Skip to content

Commit

Permalink
Add --mtime flag to build_tar.py. Use this feature for pkg_tar.
Browse files Browse the repository at this point in the history
This effectively reverts part of #7276.
archive.py no longer makes TAR files with the mtime 2000-01-01.
Now, only tar files made with the pkg_tar() rule will get that mtime value by default.
This means other users of archive.py (e.g. rules_docker) will not experience a sudden change in tar content when updating Bazel.

#1299

@beasleyr-vmw: Can you test this in your process to see that it still solves issue 1299 for you?

Closes #7482.

PiperOrigin-RevId: 234991284
  • Loading branch information
aiuto authored and Copybara-Service committed Feb 21, 2019
1 parent d54b741 commit 25d202f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
19 changes: 19 additions & 0 deletions tools/build_defs/pkg/README.md
Expand Up @@ -173,6 +173,25 @@ Creates a tar file from a list of inputs.
</p>
</td>
</tr>
<tr>
<td><code>mtime</code></td>
<td>
<code>int, seconds since Jan 1, 1970, default to -1 (ignored)</code>
<p>
Set the mod time of files added by the <code>files</code> attribute.
</p>
</td>
</tr>
<tr>
<td><code>portale_mtime</code></td>
<td>
<code>bool, default True</code>
<p>
Set the mod time of files added by the <code>files</code> attribute
to a 2000-01-01.
</p>
</td>
</tr>
<tr>
<td><code>modes</code></td>
<td>
Expand Down
15 changes: 10 additions & 5 deletions tools/build_defs/pkg/archive.py
Expand Up @@ -22,7 +22,7 @@

# Use a deterministic mtime that doesn't confuse other programs.
# See: https://github.com/bazelbuild/bazel/issues/1299
MTIME = 946684800 # 2000-01-01 00:00:00.000 UTC
PORTABLE_MTIME = 946684800 # 2000-01-01 00:00:00.000 UTC


class SimpleArFile(object):
Expand Down Expand Up @@ -110,14 +110,16 @@ def __init__(self,
name,
compression='',
root_directory='./',
default_mtime=MTIME):
default_mtime=None):
"""TarFileWriter wraps tarfile.open().
Args:
name: the tar file name.
compression: compression type: bzip2, bz2, gz, tgz, xz, lzma.
root_directory: virtual root to prepend to elements in the archive.
default_mtime: default mtime to use for elements in the archive.
May be an integer or the value 'portable' to use the date
2000-01-01, which is compatible with non *nix OSes'.
"""
if compression in ['bzip2', 'bz2']:
mode = 'w:bz2'
Expand All @@ -128,7 +130,12 @@ def __init__(self,
self.xz = compression in ['xz', 'lzma']
self.name = name
self.root_directory = root_directory.rstrip('/')
self.default_mtime = default_mtime
if default_mtime is None:
self.default_mtime = 0
elif default_mtime == 'portable':
self.default_mtime = PORTABLE_MTIME
else:
self.default_mtime = int(default_mtime)

self.fileobj = None
if self.gz:
Expand Down Expand Up @@ -292,8 +299,6 @@ def add_file(self,
tarinfo.mode = 0o644 if kind == tarfile.REGTYPE else 0o755
else:
tarinfo.mode = mode
if mtime < 0:
mtime = self.default_mtime
if link:
tarinfo.linkname = link
if content:
Expand Down
20 changes: 15 additions & 5 deletions tools/build_defs/pkg/build_tar.py
Expand Up @@ -34,6 +34,11 @@
gflags.DEFINE_string('mode', None,
'Force the mode on the added files (in octal).')

gflags.DEFINE_string(
'mtime', None, 'Set mtime on tar file entries. May be an integer or the'
' value "portable", to get the value 2000-01-01, which is'
' is usable with non *nix OSes')

gflags.DEFINE_multistring('empty_file', [], 'An empty file to add to the layer')

gflags.DEFINE_multistring('empty_dir', [], 'An empty dir to add to the layer')
Expand Down Expand Up @@ -86,20 +91,25 @@


class TarFile(object):
"""A class to generates a Docker layer."""
"""A class to generates a TAR file."""

class DebError(Exception):
pass

def __init__(self, output, directory, compression, root_directory):
def __init__(self, output, directory, compression, root_directory,
default_mtime):
self.directory = directory
self.output = output
self.compression = compression
self.root_directory = root_directory
self.default_mtime = default_mtime

def __enter__(self):
self.tarfile = archive.TarFileWriter(self.output, self.compression,
self.root_directory)
self.tarfile = archive.TarFileWriter(
self.output,
self.compression,
self.root_directory,
default_mtime=self.default_mtime)
return self

def __exit__(self, t, v, traceback):
Expand Down Expand Up @@ -326,7 +336,7 @@ def main(unused_argv):

# Add objects to the tar file
with TarFile(FLAGS.output, FLAGS.directory, FLAGS.compression,
FLAGS.root_directory) as output:
FLAGS.root_directory, FLAGS.mtime) as output:

def file_attributes(filename):
if filename.startswith('/'):
Expand Down
8 changes: 8 additions & 0 deletions tools/build_defs/pkg/pkg.bzl
Expand Up @@ -47,6 +47,12 @@ def _pkg_tar_impl(ctx):
"--owner=" + ctx.attr.owner,
"--owner_name=" + ctx.attr.ownername,
]
if ctx.attr.mtime != -1: # Note: Must match default in rule def.
if ctx.attr.portable_mtime:
fail("You may not set both mtime and portable_mtime")
args.append("--mtime=" + ctx.attr.mtime)

This comment has been minimized.

Copy link
@ixdy

ixdy Mar 28, 2019

Contributor

this actually fails:

Traceback (most recent call last):
...
        File ".../external/bazel_tools/tools/build_defs/pkg/pkg.bzl", line 53, in _pkg_tar_impl
                args.append(("--mtime=" + ctx.attr.mtime))
        File ".../external/bazel_tools/tools/build_defs/pkg/pkg.bzl", line 53, in args.append
                "--mtime=" + ctx.attr.mtime
unsupported operand type(s) for +: 'string' and 'int'

This comment has been minimized.

Copy link
@aiuto

aiuto via email Apr 11, 2019

Author Contributor
if ctx.attr.portable_mtime:
args.append("--mtime=portable")

# Add runfiles if requested
file_inputs = []
Expand Down Expand Up @@ -218,6 +224,8 @@ _real_pkg_tar = rule(
"files": attr.label_keyed_string_dict(allow_files = True),
"mode": attr.string(default = "0555"),
"modes": attr.string_dict(),
"mtime": attr.int(default = -1),
"portable_mtime": attr.bool(default = True),
"owner": attr.string(default = "0.0"),
"ownername": attr.string(default = "."),
"owners": attr.string_dict(),
Expand Down

0 comments on commit 25d202f

Please sign in to comment.