From 74f41973c25831412b3dfa1a72db47078db2b820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Wed, 1 May 2024 18:02:44 +0200 Subject: [PATCH] feat: use FAKETIME_FMT environment var instead of patching libfaketime --- .github/workflows/tests.yaml | 1 - README.md | 1 - libfaketime/__init__.py | 14 +++++++++++--- libfaketime/vendor/nanosecond.patch | 25 ------------------------- setup.py | 1 - 5 files changed, 11 insertions(+), 31 deletions(-) delete mode 100644 libfaketime/vendor/nanosecond.patch diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index d861ea7..cc06a01 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -36,7 +36,6 @@ jobs: key: pip|${{ hashFiles('setup.py') }}|${{ hashFiles('setup.cfg') }} - run: pip install tox - run: git submodule update --init --force - - run: git apply --directory libfaketime/vendor/libfaketime libfaketime/vendor/nanosecond.patch - run: env FAKETIME_COMPILE_CFLAGS="-UFAKE_STAT -UFAKE_UTIME -UFAKE_SLEEP" make -C libfaketime/vendor/libfaketime if: runner.os == 'Linux' - run: make -C libfaketime/vendor/libfaketime diff --git a/README.md b/README.md index d906ebb..53c87fc 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,6 @@ Contributions are welcome! You should compile libfaketime before running tests: ```bash git submodule init --update -git apply --directory libfaketime/vendor/libfaketime libfaketime/vendor/nanosecond.patch # For Linux: env FAKETIME_COMPILE_CFLAGS="-UFAKE_STAT -UFAKE_UTIME -UFAKE_SLEEP" make -C libfaketime/vendor/libfaketime # For macOS diff --git a/libfaketime/__init__.py b/libfaketime/__init__.py index 182ecc0..e59bdfb 100644 --- a/libfaketime/__init__.py +++ b/libfaketime/__init__.py @@ -26,7 +26,7 @@ # This env var is set by reexec to ensure we don't reload more than once. _DID_REEXEC_VAR = 'FAKETIME_DID_REEXEC' - +_FAKETIME_FMT = '%Y-%m-%d %T.%f' def _get_lib_path(): vendor_dir = 'libfaketime' @@ -162,7 +162,7 @@ def _should_patch_uuid(self): return None def _format_datetime(self, _datetime): - return _datetime.strftime('%Y-%m-%d %T %f') + return _datetime.strftime(_FAKETIME_FMT) def tick(self, delta=datetime.timedelta(seconds=1)): self.time_to_freeze += delta @@ -173,11 +173,13 @@ def __enter__(self): begin_callback(self) self._prev_spec = os.environ.get('FAKETIME') self._prev_tz = os.environ.get('TZ') + self._prev_fmt = os.environ.get('FAKETIME_FMT') os.environ['TZ'] = self.timezone_str time.tzset() os.environ['FAKETIME'] = self._format_datetime(self.time_to_freeze) + os.environ['FAKETIME_FMT'] = _FAKETIME_FMT func_name = self._should_patch_uuid() if func_name: @@ -202,7 +204,13 @@ def __exit__(self, *exc): os.environ['FAKETIME'] = self._prev_spec else: del os.environ['FAKETIME'] - end_callback(self) + + if self._prev_fmt is not None: + os.environ['FAKETIME_FMT'] = self._prev_spec + else: + del os.environ['FAKETIME_FMT'] + + end_callback(self) return False diff --git a/libfaketime/vendor/nanosecond.patch b/libfaketime/vendor/nanosecond.patch deleted file mode 100644 index 542bcf2..0000000 --- a/libfaketime/vendor/nanosecond.patch +++ /dev/null @@ -1,25 +0,0 @@ -diff --git a/src/libfaketime.c b/src/libfaketime.c -index e632395..09d9019 100644 ---- a/src/libfaketime.c -+++ b/src/libfaketime.c -@@ -2384,10 +2384,16 @@ static void parse_ft_string(const char *user_faked_time) - user_faked_time_tm.tm_isdst = -1; - nstime_str = strptime(user_faked_time, user_faked_time_fmt, &user_faked_time_tm); - -+ /* the actual format has a " %f" appended. Parse out the microseconds. */ -+ char nanosecond_str[7]; -+ memcpy(&nanosecond_str, user_faked_time + 20, 6); -+ nanosecond_str[6] = '\0'; -+ int nanoseconds = atoi(nanosecond_str) * 1000; -+ - if (NULL != nstime_str) - { - user_faked_time_timespec.tv_sec = mktime(&user_faked_time_tm); -- user_faked_time_timespec.tv_nsec = 0; -+ user_faked_time_timespec.tv_nsec = nanoseconds; - - if (nstime_str[0] == '.') - { --- -2.45.0 - diff --git a/setup.py b/setup.py index 69c85dc..6add73e 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,6 @@ class CustomInstall(install): def run(self): self.my_outputs = [] - subprocess.check_call(['patch', '-p1', '<', '../nanosecond.patch'], cwd=_vendor_path, shell=True) if sys.platform in ("linux", "linux2"): subprocess.check_call(['env', 'FAKETIME_COMPILE_CFLAGS=-UFAKE_STAT -UFAKE_UTIME -UFAKE_SLEEP', 'make', '-C', _vendor_path]) elif sys.platform == "darwin":