ยปCore Development>Code coverage>Tools/scripts/byteyears.py

Python code coverage for Tools/scripts/byteyears.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# Print the product of age and size of each file, in suitable units.
4n/a#
5n/a# Usage: byteyears [ -a | -m | -c ] file ...
6n/a#
7n/a# Options -[amc] select atime, mtime (default) or ctime as age.
8n/a
9n/aimport sys, os, time
10n/afrom stat import *
11n/a
12n/adef main():
13n/a
14n/a # Use lstat() to stat files if it exists, else stat()
15n/a try:
16n/a statfunc = os.lstat
17n/a except AttributeError:
18n/a statfunc = os.stat
19n/a
20n/a # Parse options
21n/a if sys.argv[1] == '-m':
22n/a itime = ST_MTIME
23n/a del sys.argv[1]
24n/a elif sys.argv[1] == '-c':
25n/a itime = ST_CTIME
26n/a del sys.argv[1]
27n/a elif sys.argv[1] == '-a':
28n/a itime = ST_CTIME
29n/a del sys.argv[1]
30n/a else:
31n/a itime = ST_MTIME
32n/a
33n/a secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
34n/a now = time.time() # Current time, for age computations
35n/a status = 0 # Exit status, set to 1 on errors
36n/a
37n/a # Compute max file name length
38n/a maxlen = 1
39n/a for filename in sys.argv[1:]:
40n/a maxlen = max(maxlen, len(filename))
41n/a
42n/a # Process each argument in turn
43n/a for filename in sys.argv[1:]:
44n/a try:
45n/a st = statfunc(filename)
46n/a except OSError as msg:
47n/a sys.stderr.write("can't stat %r: %r\n" % (filename, msg))
48n/a status = 1
49n/a st = ()
50n/a if st:
51n/a anytime = st[itime]
52n/a size = st[ST_SIZE]
53n/a age = now - anytime
54n/a byteyears = float(size) * float(age) / secs_per_year
55n/a print(filename.ljust(maxlen), end=' ')
56n/a print(repr(int(byteyears)).rjust(8))
57n/a
58n/a sys.exit(status)
59n/a
60n/aif __name__ == '__main__':
61n/a main()