Created
December 18, 2011 20:55
-
-
Save sevennineteen/1494429 to your computer and use it in GitHub Desktop.
Produce high-level summary of file system changes since a given date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs, os, glob | |
from datetime import datetime | |
import time | |
from stat import * | |
#---------------------------------------------------------- | |
BASE_DIR = r'C:\some_folder' | |
FILE_DIRS = [ | |
'some_subfolder_a', | |
'some_subfolder_b', | |
] | |
MATCH_PATTERN = '*.xml' | |
FROM_DATE = datetime(2011, 12, 1) # year, month, day | |
#---------------------------------------------------------- | |
def check_delta(): | |
from_date_epoch = time.mktime(FROM_DATE.timetuple()) | |
folder_delta = {} | |
for dir in ['\\'.join([BASE_DIR, fd]) for fd in FILE_DIRS]: | |
print 'Checking %s' % dir.replace(BASE_DIR, '') | |
folder_delta.update({dir: {'total': 0, 'modified': 0}}) | |
for filename in glob.iglob(os.path.join(dir, MATCH_PATTERN)): | |
folder_delta[dir]['total'] += 1 | |
try: | |
if from_date_epoch < os.stat(os.path.join(dir, filename))[ST_MTIME]: | |
folder_delta[dir]['modified'] += 1 | |
except Exception, e: | |
print e | |
return dict([(k, folder_delta[k]) for k in folder_delta.keys() if folder_delta[k]['total'] > 0]) | |
def summarize(delta): | |
REPORT = codecs.open('delta_report-%s-%s.txt' % (FROM_DATE.strftime('%m%d%y'), datetime.now().strftime('%m%d%y')), 'w', 'utf-8') | |
REPORT.write('Delta Report:\t%s to %s\r\n' % (FROM_DATE.strftime('%x'), datetime.now().strftime('%x'))) | |
REPORT.write('Prepared at:\t%s\r\n' % datetime.now().strftime('%X')) | |
REPORT.write('=' * 42 + '\r\n') | |
REPORT.write('Folder\tTotal\tChanged\tPercentage\r\n') | |
for dir in sorted(delta.keys()): | |
REPORT.write('%s\t\t%s\t%s\t%s%%\r\n' % ( | |
dir.replace(BASE_DIR, ''), | |
delta[dir]['total'], | |
delta[dir]['modified'], | |
delta[dir]['modified'] / delta[dir]['total'] * 100 | |
)) | |
# Calculate totals | |
sum_total = reduce(lambda x, y: x + y, [delta[dir]['total'] for dir in delta.keys()]) | |
sum_modified = reduce(lambda x, y: x + y, [delta[dir]['modified'] for dir in delta.keys()]) | |
sum_percentage = sum_modified / sum_total * 100 | |
REPORT.write('=' * 42 + '\r\n') | |
REPORT.write('Summary:\t\t%s\t%s\t%s%%\r\n' % (sum_total, sum_modified, sum_percentage)) | |
REPORT.close() | |
print 'Preparing delta report: %s to %s' % (FROM_DATE.strftime('%x'), datetime.now().strftime('%x')) | |
summarize(check_delta()) | |
print 'Report complete.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment