source: cows/trunk/add_license.py @ 6576

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/add_license.py@6576
Revision 6576, 2.9 KB checked in by spascoe, 10 years ago (diff)

Added licence to all source files

Line 
1"""
2# BSD Licence
3# Copyright (c) 2009, Science & Technology Facilities Council (STFC)
4# All rights reserved.
5#
6# See the LICENSE file in the source distribution of this software for
7# the full license text.
8
9Insert a copywrite notice at the top of any python files given as arguments.
10
11"""
12
13import datetime
14import os, re, sys
15import optparse
16
17license_text = """
18# BSD Licence
19# Copyright (c) %(year)s, Science & Technology Facilities Council (STFC)
20# All rights reserved.
21#
22%(msg)s
23
24""".lstrip()
25
26detect_rexp = re.compile(r'''^[#] BSD Licence''', re.M)
27
28#[#] Copyright (c) \d+, Science & Technology Facilities Council (STFC)
29#[#] All rights reserved.
30#''', re.M)
31
32
33default_message = """
34See the LICENSE file in the source distribution of this software for
35the full license text.
36""".strip()
37
38def add_license(filename, year=None, msg=None):
39    """
40    Add a license to a file
41   
42    """
43
44    ext = os.path.splitext(filename)[1][1:]
45    #!TODO: Add xml when fixme below complete
46    if ext not in ['py']:
47        raise ValueError('Extension %s not supported' % ext)
48       
49    if year is None:
50        year = datetime.datetime.now().year
51    if msg is None:
52        msg = default_message
53
54    msg = '\n'.join('# %s'%x for x in msg.split('\n'))
55
56    license = license_text % dict(msg=msg, year=year)
57
58    fh_in = open(filename)
59    fh_out = open('%s.tmp' % filename, 'w')
60
61    if ext == 'py':
62        line = fh_in.readline()
63        if line[:2] == '#!':
64            fh_out.write(line)
65            line = fh_in.readline()
66            fh_out.write(license)
67        else:
68            fh_out.write(license)
69            fh_out.write(line)
70
71    #!FIXME: This bit is broken because it doesn't look for <!DOCTYPE!>
72    #    declarations
73    elif ext == 'xml':
74        line = fh_in.readline()
75        if re.match(r'<\?xml', line):
76            fh_out.write(line)
77            line = fh_in.readline()
78
79        fh_out.write('<!--\n')
80        fh_out.write(license)
81        fh_out.write('-->\n')
82
83
84    for line in fh_in:
85        fh_out.write(line)
86
87    fh_in.close()
88    fh_out.close()
89    os.system('mv %s %s.orig' % (filename, filename))
90    os.system('mv %s.tmp %s' % (filename, filename))
91    print 'Added license to %s, original is %s.orig' % (filename, filename)
92
93
94def detect_license(filename):
95    file_txt = open(filename).read()
96
97    if detect_rexp.search(file_txt):
98        return True
99    else:
100        return False
101
102def main(argv=sys.argv[1:]):
103
104    parser = optparse.OptionParser()
105    parser.add_option('-d', '--detect', dest='detect', action='store_true',
106                      default=False,
107                      help='Detect whether each file has a license')
108
109    options, args = parser.parse_args(argv)
110
111    if options.detect:
112        for filename in args:
113            if not detect_license(filename):
114                print filename
115    else:
116        for filename in args:
117            add_license(filename)
118   
119
120if __name__ == '__main__':
121    main()
Note: See TracBrowser for help on using the repository browser.