home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / BYTEYEARS.PY < prev    next >
Encoding:
Python Source  |  2003-05-13  |  1.5 KB  |  57 lines

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