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