summaryrefslogtreecommitdiff
path: root/dtools/bin/fix-copyright-years
blob: bb0f3009d9bb6aabd1a5b3cf5501fb18cbe2ebae (plain)
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
#!/usr/bin/env python

import re
import datetime
import subprocess
import multiprocessing

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

def fix_co_years(files):
    for file in files:
        print file
        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', 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
        try:
            co_years_str = re.match('^.*Copyright (.*) Free Software Foundation.*$', line).groups()[0]
            co_years = set(map(int, co_years_str.split(',')))
        except: print '    format error on line %d: "%s"'%(num, line); continue

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

if __name__ == "__main__":
    #get recursive list of files in the repo
    files = command('git', 'ls-tree', '--name-only', 'HEAD', '-r').splitlines()

    #start n+1 processes to handle the files
    num_procs = multiprocessing.cpu_count()
    procs = [multiprocessing.Process(
        target=lambda *files: fix_co_years(files),
        args=files[num::num_procs],
    ) for num in range(num_procs)]
    map(multiprocessing.Process.start, procs)
    map(multiprocessing.Process.join, procs)