From 7c51623627689cf0d32d9466c14e4acb7fbdc243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89loi=20Rivard?= Date: Tue, 30 Apr 2024 20:59:51 +0200 Subject: [PATCH] refactor: directly pass libfaketime CFLAGS instead of changing the Makefile C flags in the patch --- .github/workflows/tests.yaml | 10 ++++++---- README.md | 7 +++++-- libfaketime/vendor/nanosecond.patch | 25 +++++++++++++++++++++++++ setup.py | 7 +++++-- 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 libfaketime/vendor/nanosecond.patch diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index d114212..d861ea7 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -36,7 +36,9 @@ 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/libfaketime.patch - make -C libfaketime/vendor/libfaketime - - run: tox -e ${{ matrix.python }}-${{ matrix.tz }} + - 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 + if: runner.os == 'macOS' + - run: tox -e ${{ matrix.python }}-${{ matrix.tz }} --recreate diff --git a/README.md b/README.md index 6b08c38..d906ebb 100644 --- a/README.md +++ b/README.md @@ -165,8 +165,11 @@ Contributions are welcome! You should compile libfaketime before running tests: ```bash git submodule init --update -git apply --directory libfaketime/vendor/libfaketime libfaketime/vendor/libfaketime.patch -make -C libfaketime/vendor/libfaketime +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 +env make -C libfaketime/vendor/libfaketime ``` Then you can install requirements with ``pip install -r requirements.txt`` and use ``pytest`` and ``tox`` to run the tests. diff --git a/libfaketime/vendor/nanosecond.patch b/libfaketime/vendor/nanosecond.patch new file mode 100644 index 0000000..542bcf2 --- /dev/null +++ b/libfaketime/vendor/nanosecond.patch @@ -0,0 +1,25 @@ +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 b479ab8..69c85dc 100644 --- a/setup.py +++ b/setup.py @@ -38,8 +38,11 @@ class CustomInstall(install): def run(self): self.my_outputs = [] - subprocess.check_call(['patch', '-p1', '<', '../libfaketime.patch'], cwd=_vendor_path, shell=True) - subprocess.check_call(['make', '-C', _vendor_path]) + 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": + subprocess.check_call(['make', '-C', _vendor_path]) dest = os.path.join(self.install_purelib, os.path.dirname(faketime_lib)) try: