diff options
author | Tristan van Berkom <tristan@codethink.co.uk> | 2020-10-25 18:10:18 +0900 |
---|---|---|
committer | Tristan van Berkom <tristan@codethink.co.uk> | 2020-10-25 18:10:18 +0900 |
commit | d164ae319fced1e41ce5978a90c2bd64983cb54e (patch) | |
tree | cbe8fc2c82284dba82782b9b92d0481c98c4616d | |
parent | 1d98d140e4d60e5f6dbe3f3da7e0be3bc1743f00 (diff) | |
download | buildstream-d164ae319fced1e41ce5978a90c2bd64983cb54e.tar.gz |
src/buildstream/testing/_utils/site.py: Adding have_subsecond_mtime()
A utility for tests to check if subsecond precision mtime is supported
at a given filesystem path (it will depend on underlying filesystem, so
we cannot have a simple HAVE_SUBSECOND_MTIME variable to check, because
we don't know where tests will operate at this stage).
-rw-r--r-- | src/buildstream/testing/_utils/site.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/buildstream/testing/_utils/site.py b/src/buildstream/testing/_utils/site.py index 705419f58..727fe015b 100644 --- a/src/buildstream/testing/_utils/site.py +++ b/src/buildstream/testing/_utils/site.py @@ -5,6 +5,7 @@ import os import stat import subprocess import sys +import tempfile from typing import Optional # pylint: disable=unused-import from buildstream import utils, ProgramNotFoundError @@ -73,3 +74,34 @@ try: HAVE_SANDBOX = "buildbox-run" except (ProgramNotFoundError, OSError, subprocess.CalledProcessError): pass + + +# Check if we have subsecond mtime support on the +# filesystem where @directory is located. +# +def have_subsecond_mtime(directory): + + try: + test_file, test_filename = tempfile.mkstemp(dir=directory) + os.close(test_file) + except OSError: + # If we can't create a temp file, lets just say this is False + return False + + try: + os.utime(test_filename, times=None, ns=(int(12345), int(12345))) + except OSError: + # If we can't set the mtime, lets just say this is False + os.unlink(test_filename) + return False + + try: + stat_result = os.stat(test_filename) + except OSError: + # If we can't stat the file, lets just say this is False + os.unlink(test_filename) + return False + + os.unlink(test_filename) + + return stat_result.st_mtime_ns == 12345 |