davidfstr

carbon_date.py

Feb 3rd, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1.  
  2. #!/usr/bin/env python
  3. """
  4. Prints a histogram of the modifications dates of all the files
  5. in the specified directory.
  6.  
  7. This is useful for determining the overall last modified date.
  8.  
  9. Author: David Foster (davidfstr)
  10. Created: 2012-02-02
  11. Environment: Python 2.7
  12. """
  13.  
  14. from collections import defaultdict
  15. from datetime import datetime
  16. import os
  17. import os.path
  18. import sys
  19.  
  20. HISTOGRAM_WIDTH = 80
  21. IGNORED_EXTENSIONS = [
  22.     '.class',
  23.     '.pyc',
  24.     '.pyo',
  25. ]
  26.  
  27. # Parse arguments
  28. args = sys.argv[1:]
  29. if len(args) != 1:
  30.     exit('syntax: python carbon_date.py <dirpath>')
  31. root_dirpath = args[0]
  32.  
  33. def is_ignored_file(filename):
  34.     if filename == '.DS_Store':
  35.         return True
  36.    
  37.     # Has an ignored extension?
  38.     for ext in IGNORED_EXTENSIONS:
  39.         if filename.endswith(ext):
  40.             return True
  41.    
  42.     return False
  43.  
  44. def quarter_create(dt):
  45.     return str(dt.year) + 'Q' + str(((dt.month-1) // 3) + 1)
  46.  
  47. def quarter_next(q):
  48.     q_parts = map(int, q.split('Q'))
  49.     q_parts[1] += 1
  50.     if q_parts[1] == 5:
  51.         q_parts = [q_parts[0]+1, 1]
  52.     return 'Q'.join(map(str, q_parts))
  53.  
  54. bins = defaultdict(lambda: 0)
  55. values = []
  56. for (dirpath, dirnames, filenames) in os.walk(root_dirpath):
  57.     for filename in filenames:
  58.         if is_ignored_file(filename):
  59.             continue
  60.         filepath = os.path.join(dirpath, filename)
  61.         if not os.path.exists(filepath):
  62.             continue
  63.         modified_dt = datetime.fromtimestamp(os.path.getmtime(filepath))
  64.        
  65.         bins[quarter_create(modified_dt)] += 1
  66.         values.append(modified_dt)
  67. values = sorted(values)
  68.  
  69. cur_bin = min(bins.keys())
  70. max_bin = max(bins.keys())
  71. max_freq = max(bins.values())
  72. while cur_bin <= max_bin:
  73.     cur_bar_length = bins[cur_bin] * HISTOGRAM_WIDTH // max_freq
  74.     print '%s: %s%s|' % (
  75.         cur_bin,
  76.         '*' * cur_bar_length,
  77.         ' ' * (HISTOGRAM_WIDTH - cur_bar_length),
  78.     )
  79.    
  80.     cur_bin = quarter_next(cur_bin)
  81. print
  82.  
  83. median_value = values[(len(values) - 1) // 2]
  84. percentile_80 = values[int((len(values) - 1) * .80)]
  85. percentile_90 = values[int((len(values) - 1) * .90)]
  86. percentile_95 = values[int((len(values) - 1) * .95)]
  87. print 'Median: %s' % median_value.ctime()
  88. print '80th Percentile: %s' % percentile_80.ctime()
  89. print '90th Percentile: %s' % percentile_90.ctime()
  90. print '95th Percentile: %s' % percentile_95.ctime()
Advertisement
Add Comment
Please, Sign In to add comment