Skip to content

Commit

Permalink
feat: use FAKETIME_FMT environment var instead of patching libfaketime
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed May 1, 2024
1 parent 4ea08c5 commit 74f4197
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions libfaketime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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

Expand Down
25 changes: 0 additions & 25 deletions libfaketime/vendor/nanosecond.patch

This file was deleted.

1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 74f4197

Please sign in to comment.