Iterates through a directory, reading the data from svn info that looks like:
$Id: svn_redater.py 747 2010-07-30 09:56:08Z mn $
from source files.
Parses the datetime from svn info and if it differs from file modification datetime then changes file datetime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #!/usr/bin/env python
# -*- coding: utf8 -*-
__version__ = '$Id: svn_redater.py 748 2010-07-30 09:59:56Z mn $'
USAGE = "svn_redater.py\n\tredate source files according to svn data"
"""
Iterates through a directory, reading the data from svn info that looks like:
$Id: svn_redater.py 748 2010-07-30 09:59:56Z mn $
from source files.
Parses the datetime from svn info and if it differs from file
modification datetime then changes file datetime
author: Michal Niklas
"""
import re
import os
import os.path
import sys
import time
ALL_CNT = 0
CHANGED_CNT = 0
DEBUG = 0
# which file should be checked
EXTENSIONS = ('.py', '.pas', '.inc', '.java', '.c', '.cpp', '.h',)
def log_error(s):
sys.stderr.write('%s\n' % (s))
def show_fdt(fdt):
"""human readable format of file modification datetime"""
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(fdt))
def svndtinfo2time(ts):
"""changes svn date ('2009-10-30 13:17:45') to number of seconds since 1970-01-01"""
tpl = time.strptime(ts+'UTC', '%Y-%m-%d %H:%M:%S%Z')
return time.mktime(tpl)
dt_re = re.compile(r'\d+ (\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)Z ')
def extract_svn_dt(line):
"""change svn info:
'$Id: svn_redater.py 748 2010-07-30 09:59:56Z mn $'
into
'2009-10-30 13:17:45'"""
result = None
rx = dt_re.search(line)
if rx:
result = rx.group(1)
return result
def redate_file(fn):
"""redates file if file modification datetime differs from svn info"""
global ALL_CNT, CHANGED_CNT
ALL_CNT += 1
svn_dt = None
s = os.stat(fn)
file_time = s[8]
if DEBUG:
print(fn)
f = open(fn, 'rb')
try:
for line in f.readlines():
if '$Id:' in line:
svn_dt = extract_svn_dt(line)
break
finally:
f.close()
if svn_dt:
svn_time = svndtinfo2time(svn_dt)
secs_diff = file_time - svn_time
MAX_DIFF = 130.0
if secs_diff > MAX_DIFF or secs_diff < -MAX_DIFF:
print "%s %s -> %s" % (fn, show_fdt(file_time), show_fdt(svn_time))
os.utime(fn, (svn_time, svn_time))
CHANGED_CNT += 1
def process_dir(_, dir_name, files):
"""looks for source files in dir"""
sys.stdout.write('%-70s\r' % (dir_name))
for fname in files:
can_change = False
fnl = fname.lower()
for ext in EXTENSIONS:
if fnl.endswith(ext):
can_change = True
break
if can_change:
fname = os.path.join(dir_name, fname)
if os.path.isfile(fname):
redate_file(fname)
def main():
os.path.walk('.', process_dir, None)
print '\nChecked: %d\nChanged %d\n' % (ALL_CNT, CHANGED_CNT)
if '--version' in sys.argv:
print __version__
else:
if __name__ == '__main__':
if '--help' in sys.argv:
print USAGE
else:
main()
|