From 8e4fd46464f6043dd29a8a4862842a54e19e8821 Mon Sep 17 00:00:00 2001 From: Shahwat Dalal Date: Mon, 3 Jun 2019 14:32:54 +0100 Subject: utils.py: Make `magic_timestamp` a public variable Some plugin elements, such as docker-element and tar-element, require to set `created` and/or `modified` meta-data fields. It would be nice to use `magic_timestamp` to ensure these fields are being set in a consistent and deterministic way. --- src/buildstream/storage/_casbaseddirectory.py | 4 ++-- src/buildstream/storage/_filebaseddirectory.py | 6 +++--- src/buildstream/storage/directory.py | 4 ++-- src/buildstream/utils.py | 7 +++---- 4 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src/buildstream') diff --git a/src/buildstream/storage/_casbaseddirectory.py b/src/buildstream/storage/_casbaseddirectory.py index 29aee7ad3..a93f16d90 100644 --- a/src/buildstream/storage/_casbaseddirectory.py +++ b/src/buildstream/storage/_casbaseddirectory.py @@ -32,7 +32,7 @@ import os from .._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 from .directory import Directory, VirtualDirectoryError, _FileType from ._filebaseddirectory import FileBasedDirectory -from ..utils import FileListResult, _magic_timestamp +from ..utils import FileListResult, BST_ARBITRARY_TIMESTAMP class IndexEntry(): @@ -436,7 +436,7 @@ class CasBasedDirectory(Directory): self.cas_cache.checkout(to_directory, self._get_digest(), can_link=can_link) - def export_to_tar(self, tarfile, destination_dir, mtime=_magic_timestamp): + def export_to_tar(self, tarfile, destination_dir, mtime=BST_ARBITRARY_TIMESTAMP): raise NotImplementedError() def mark_changed(self): diff --git a/src/buildstream/storage/_filebaseddirectory.py b/src/buildstream/storage/_filebaseddirectory.py index 233b56a09..8c55819c9 100644 --- a/src/buildstream/storage/_filebaseddirectory.py +++ b/src/buildstream/storage/_filebaseddirectory.py @@ -34,7 +34,7 @@ import time from .directory import Directory, VirtualDirectoryError, _FileType from .. import utils -from ..utils import link_files, copy_files, list_relative_paths, _get_link_mtime, _magic_timestamp +from ..utils import link_files, copy_files, list_relative_paths, _get_link_mtime, BST_ARBITRARY_TIMESTAMP from ..utils import _set_deterministic_user, _set_deterministic_mtime from ..utils import FileListResult @@ -153,7 +153,7 @@ class FileBasedDirectory(Directory): # First, it sorts the results of os.listdir() to ensure the ordering of # the files in the archive is the same. Second, it sets a fixed # timestamp for each entry. See also https://bugs.python.org/issue24465. - def export_to_tar(self, tarfile, destination_dir, mtime=_magic_timestamp): + def export_to_tar(self, tarfile, destination_dir, mtime=BST_ARBITRARY_TIMESTAMP): # We need directories here, including non-empty ones, # so list_relative_paths is not used. for filename in sorted(os.listdir(self.external_directory)): @@ -187,7 +187,7 @@ class FileBasedDirectory(Directory): Return value: List(str) - list of modified paths """ return [f for f in list_relative_paths(self.external_directory) - if _get_link_mtime(os.path.join(self.external_directory, f)) != _magic_timestamp] + if _get_link_mtime(os.path.join(self.external_directory, f)) != BST_ARBITRARY_TIMESTAMP] def list_relative_paths(self): """Provide a list of all relative paths. diff --git a/src/buildstream/storage/directory.py b/src/buildstream/storage/directory.py index c40e1bdb9..c9906b058 100644 --- a/src/buildstream/storage/directory.py +++ b/src/buildstream/storage/directory.py @@ -34,7 +34,7 @@ See also: :ref:`sandboxing`. from enum import Enum from .._exceptions import BstError, ErrorDomain -from ..utils import _magic_timestamp +from ..utils import BST_ARBITRARY_TIMESTAMP class VirtualDirectoryError(BstError): @@ -122,7 +122,7 @@ class Directory(): raise NotImplementedError() - def export_to_tar(self, tarfile, destination_dir, mtime=_magic_timestamp): + def export_to_tar(self, tarfile, destination_dir, mtime=BST_ARBITRARY_TIMESTAMP): """ Exports this directory into the given tar file. Args: diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index b5b554ee5..0e2cdeb43 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -43,8 +43,7 @@ from ._exceptions import BstError, ErrorDomain from ._protos.build.bazel.remote.execution.v2 import remote_execution_pb2 # The magic number for timestamps: 2011-11-11 11:11:11 -_magic_timestamp = calendar.timegm([2011, 11, 11, 11, 11, 11]) - +BST_ARBITRARY_TIMESTAMP = calendar.timegm([2011, 11, 11, 11, 11, 11]) # The separator we use for user specified aliases _ALIAS_SEPARATOR = ':' @@ -952,9 +951,9 @@ def _set_deterministic_mtime(directory): # However, nowadays it is possible at least on gnuish systems # with with the lutimes glibc function. if not os.path.islink(pathname): - os.utime(pathname, (_magic_timestamp, _magic_timestamp)) + os.utime(pathname, (BST_ARBITRARY_TIMESTAMP, BST_ARBITRARY_TIMESTAMP)) - os.utime(dirname, (_magic_timestamp, _magic_timestamp)) + os.utime(dirname, (BST_ARBITRARY_TIMESTAMP, BST_ARBITRARY_TIMESTAMP)) # _tempdir() -- cgit v1.2.1