You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

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