Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- Prints a histogram of the modifications dates of all the files
- in the specified directory.
- This is useful for determining the overall last modified date.
- Author: David Foster (davidfstr)
- Created: 2012-02-02
- Environment: Python 2.7
- """
- from collections import defaultdict
- from datetime import datetime
- import os
- import os.path
- import sys
- HISTOGRAM_WIDTH = 80
- IGNORED_EXTENSIONS = [
- '.class',
- '.pyc',
- '.pyo',
- ]
- # Parse arguments
- args = sys.argv[1:]
- if len(args) != 1:
- exit('syntax: python carbon_date.py <dirpath>')
- root_dirpath = args[0]
- def is_ignored_file(filename):
- if filename == '.DS_Store':
- return True
- # Has an ignored extension?
- for ext in IGNORED_EXTENSIONS:
- if filename.endswith(ext):
- return True
- return False
- def quarter_create(dt):
- return str(dt.year) + 'Q' + str(((dt.month-1) // 3) + 1)
- def quarter_next(q):
- q_parts = map(int, q.split('Q'))
- q_parts[1] += 1
- if q_parts[1] == 5:
- q_parts = [q_parts[0]+1, 1]
- return 'Q'.join(map(str, q_parts))
- bins = defaultdict(lambda: 0)
- values = []
- for (dirpath, dirnames, filenames) in os.walk(root_dirpath):
- for filename in filenames:
- if is_ignored_file(filename):
- continue
- filepath = os.path.join(dirpath, filename)
- if not os.path.exists(filepath):
- continue
- modified_dt = datetime.fromtimestamp(os.path.getmtime(filepath))
- bins[quarter_create(modified_dt)] += 1
- values.append(modified_dt)
- values = sorted(values)
- cur_bin = min(bins.keys())
- max_bin = max(bins.keys())
- max_freq = max(bins.values())
- while cur_bin <= max_bin:
- cur_bar_length = bins[cur_bin] * HISTOGRAM_WIDTH // max_freq
- print '%s: %s%s|' % (
- cur_bin,
- '*' * cur_bar_length,
- ' ' * (HISTOGRAM_WIDTH - cur_bar_length),
- )
- cur_bin = quarter_next(cur_bin)
- print
- median_value = values[(len(values) - 1) // 2]
- percentile_80 = values[int((len(values) - 1) * .80)]
- percentile_90 = values[int((len(values) - 1) * .90)]
- percentile_95 = values[int((len(values) - 1) * .95)]
- print 'Median: %s' % median_value.ctime()
- print '80th Percentile: %s' % percentile_80.ctime()
- print '90th Percentile: %s' % percentile_90.ctime()
- print '95th Percentile: %s' % percentile_95.ctime()
Advertisement
Add Comment
Please, Sign In to add comment