Monday, May 31, 2010

[Discuss-gnuradio] if only there was a way to fix those damn copyright dates on the tops of files...

#!/usr/bin/env python

import re
import subprocess

def command(*args): return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]

def is_gnuradio_co_source(lines):
for line in lines[:20]:
if 'GNU Radio is free software' in line: return True
return False

def get_gnuradio_co_line(lines):
for i, line in enumerate(lines[:5]):
if 'Copyright' in line and 'Free Software Foundation' in line: return line, i
return None

if __name__ == "__main__":
#get recursive list of files in the repo
for file in command('git', 'ls-tree', '--name-only', 'HEAD', '-r').splitlines():
lines = open(file).readlines()
if not is_gnuradio_co_source(lines): continue

#extract the years from the git history
years = set(map(
lambda l: int(l.split()[-2]),
filter(
lambda l: l.startswith('Date'),
command('git', 'log', '--sparse', '--date=default', file).splitlines(),
),
))

#extract line and line number for co line
try: line, num = get_gnuradio_co_line(lines)
except: continue

#extract years from co string
co_years_str = re.match('^.*Copyright (.*) Free Software Foundation.*$', line).groups()[0]
co_years = set(map(int, co_years_str.split(',')))

#update the years if missing any
all_years = co_years.union(years)
if all_years != years:
print '%s missing %s'%(file, all_years - years)
new_text = ''.join(lines[:num] + [line.replace(co_years_str, ', '.join(map(str, all_years)))] + lines[num+1:])
open(file, 'w').write(new_text)

-Josh

No comments:

Post a Comment