Mercurial > hgrepos > DevTools > mercurial-extensions
comparison tests/lib-stat.py @ 273:6abd41f7d879
Unit-tests for almost all currently implemented features of the timestamps extension.
BUGS: Missing testing commits with explicit files given.
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Fri, 04 Jan 2019 22:09:06 +0100 |
| parents | |
| children | 7ec353866f70 |
comparison
equal
deleted
inserted
replaced
| 272:1c78d1cbe91a | 273:6abd41f7d879 |
|---|---|
| 1 from __future__ import absolute_import, print_function | |
| 2 | |
| 3 | |
| 4 import sys | |
| 5 import os | |
| 6 import datetime | |
| 7 | |
| 8 | |
| 9 class FloatTimesInStat(object): | |
| 10 """Context manager to ensure that stat returns float values. | |
| 11 | |
| 12 For 3.7 <= Mercurial < 4.6: Mercurial calls :func:`os.stat_float_times` | |
| 13 for `stat` and friends to return :class:`int` values. | |
| 14 | |
| 15 Temporarily fix this. | |
| 16 | |
| 17 """ | |
| 18 | |
| 19 __slots__ = ("_do_reset",) | |
| 20 | |
| 21 def __init__(self): | |
| 22 self._do_reset = False | |
| 23 | |
| 24 def __enter__(self): | |
| 25 if not os.stat_float_times(): | |
| 26 os.stat_float_times(True) | |
| 27 self._do_reset = True | |
| 28 return self | |
| 29 | |
| 30 def __exit__(self, *args): | |
| 31 if self._do_reset: | |
| 32 os.stat_float_times(False) | |
| 33 | |
| 34 | |
| 35 def to_isoformat(t): | |
| 36 """Return the POSIX timestamp `t` formatted in full ISO format. | |
| 37 | |
| 38 `t` is expected to be in the UTC timezone. | |
| 39 | |
| 40 """ | |
| 41 dt = datetime.datetime.utcfromtimestamp(t) | |
| 42 if dt.utcoffset() is None: | |
| 43 return dt.isoformat() + "Z" | |
| 44 else: | |
| 45 return dt.isoformat() | |
| 46 | |
| 47 | |
| 48 def main(): | |
| 49 with FloatTimesInStat(): | |
| 50 for f in sys.argv[1:]: | |
| 51 st = os.lstat(f) | |
| 52 print(to_isoformat(st.st_mtime)) | |
| 53 | |
| 54 | |
| 55 if __name__ == "__main__": | |
| 56 main() |
