comparison tests/lib-stat.py @ 278:7ec353866f70

Extend the lib-stat.py script to be able to assert timestamp differences. This is used now for asserting **changed** timestamps of files.
author Franz Glasner <hg@dom66.de>
date Sat, 05 Jan 2019 12:12:42 +0100
parents 6abd41f7d879
children 379050873141
comparison
equal deleted inserted replaced
277:5e5d11417138 278:7ec353866f70
1 """Cross-platform file modification check.
2
3 This is because the stat(1) utility has incompatible cross-platform usage.
4
5 Usage: lib-stat.py [ OPTIONS ] FILE ...
6
7 Options:
8
9 -c TS, --check=TS Assert that the given timestamp TS matches the given
10 FILE's modtime
11 -d TS, --differs=TS Assert that the given timestamp TS differs from the
12 given FILE's modtime
13
14 If no option is given print the modification time for each FILE to stdout.
15
16 """
17
1 from __future__ import absolute_import, print_function 18 from __future__ import absolute_import, print_function
2 19
3 20
21 import datetime
22 import getopt
23 import os
4 import sys 24 import sys
5 import os
6 import datetime
7 25
8 26
9 class FloatTimesInStat(object): 27 class FloatTimesInStat(object):
10 """Context manager to ensure that stat returns float values. 28 """Context manager to ensure that stat returns float values.
11 29
44 else: 62 else:
45 return dt.isoformat() 63 return dt.isoformat()
46 64
47 65
48 def main(): 66 def main():
49 with FloatTimesInStat(): 67 opt_check = None
50 for f in sys.argv[1:]: 68 opt_differs = None
51 st = os.lstat(f) 69
52 print(to_isoformat(st.st_mtime)) 70 opts, args = getopt.getopt(sys.argv[1:],
71 "c:d:",
72 ["check=",
73 "differs="])
74 for opt, val in opts:
75 if opt in ("-c", "--check"):
76 opt_check = val
77 elif opt in ("-d", "--differs"):
78 opt_differs = val
79 else:
80 assert False
81
82 if opt_check:
83 with FloatTimesInStat():
84 for f in args:
85 st = os.lstat(f)
86 tss = to_isoformat(st.st_mtime)
87 if tss != opt_check:
88 print(
89 "mtime does not match for file `{filename}': expected {expected}, current: {current}".format(
90 filename=f,
91 expected=opt_check,
92 current=tss),
93 file=sys.stderr)
94 return 1
95 elif opt_differs:
96 with FloatTimesInStat():
97 for f in args:
98 st = os.lstat(f)
99 tss = to_isoformat(st.st_mtime)
100 if tss == opt_differs:
101 print(
102 "mtime does not differ for file `{filename}': {timestamp}".format(
103 filename=f,
104 timestamp=tss),
105 file=sys.stderr)
106 return 1
107 else:
108 with FloatTimesInStat():
109 for f in args:
110 st = os.lstat(f)
111 print(to_isoformat(st.st_mtime))
112 return 0
53 113
54 114
55 if __name__ == "__main__": 115 if __name__ == "__main__":
56 main() 116 sys.exit(main())