File: update_copyright.py

package info (click to toggle)
liquid-dsp 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,216 kB
  • sloc: ansic: 115,859; sh: 3,513; makefile: 1,350; python: 274; asm: 11
file content (168 lines) | stat: -rwxr-xr-x 5,102 bytes parent folder | download | duplicates (5)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python

'''
This script updates the copyright license lines for all files within a
particular directory.

usage:
  $ python scripts/update_copyright.py <dir>
'''

import os
import sys
import re

def usage():
    print __doc__

if len(sys.argv) != 2:
    sys.stderr.write("error: please provide an input directory\n")
    usage()
    sys.exit(-1)
elif sys.argv[1] in ('-h','--help'):
    usage()
    sys.exit(0)

# new copyright
newcopy = '''\
Copyright (c) 2007 - 2015 Joseph Gaeddert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.'''.splitlines()

# add new line character to end of each line
for i in xrange(len(newcopy)):
    newcopy[i] += '\n'

rootdir = sys.argv[1]

# specific files to ignore
ignore_files = ['update_copyright.py',]

# directories to ignore
ignore_directories = ['.git',]

# only look at these extensions
include_extensions = ['.h', '.c', '.md', '.tex', '.ac', '.in', '.m',]

# print alignment for status
align = 56

#
def update_copyright(filename=""):
    """
    Update copyright on file.
    """
    # try to open file
    contents = []
    with open( filename, 'r' ) as f:
        # read lines from file (removing newline character at end)
        for line in f:
            #contents.append(line.strip('\n\r'))
            contents.append(line)

    # parse contents
    index_start = -1
    index_stop  = -1

    # search for copyright; starts at top of file
    for i in range(min(10, len(contents))):
        if re.search(r'Copyright .* Joseph Gaeddert',contents[i]):
            index_start = i
            break

    if index_start == -1:
        print "  " + filename + ":" + " "*(align-len(filename)) + "no copyright found"
        return

    # look for end of copyright
    #for i in range(index_start+15,index_start+15+min(10, len(contents))):
    i = index_start + 15
    if re.search(r'along with liquid.  If not, see',contents[i]):
        index_stop = i
    elif re.search(r'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM',contents[i]):
        print "  " + filename + ":" + " "*(align-len(filename)) + "copyright already updated"
        return
    else:
        print "  " + filename + ":" + " "*(align-len(filename)) + "could not find end of copyright"
        return

    # check comment type
    m = re.search(r'^( \*|#+) +Copyright',contents[index_start])
    if m:
        comment = m.group(1)
    else:
        raise Exception('unexpected error')

    # delete items in list
    contents.__delslice__(index_start, index_stop+1)

    # insert new copyright
    for i in range(len(newcopy)):
        # only add space after comment characters if string is not empty
        # (e.g. print ' *' instead of ' * ')
        space = ' ' if len(newcopy[i].strip()) > 0 else ''

        # insert new comment
        contents.insert(index_start+i, comment + space + newcopy[i])

    # open original file for writing
    with open( filename, 'w' ) as f:
        for line in contents:
            f.write(line)
            
    print "  " + filename + ":" + " "*(align-len(filename)) + "updated"

#
for root, subFolders, files in os.walk(rootdir):

    # strip off leading './' if it exists
    if root.startswith('./'):
        root = root[2:]

    # print root directory
    print root
    
    # ignore certain directories
    if root in ignore_directories:
        print "(skipping directory)"
        continue

    # print subfolders
    #for folder in subFolders:
    #    print "%s has subdirectory %s" % (root, folder)

    # parse each filename in directory
    for filename in files:
        filePath = os.path.join(root, filename)

        # check filename
        if filePath in ignore_files:
            print "  " + filePath + ":" + " "*(align-len(filePath)) + "ignoring this specific file"
            continue;

        # check filename extension
        baseName, extension = os.path.splitext(filename)
        if extension not in include_extensions:
            print "  " + filePath + ":" + " "*(align-len(filePath)) + "improper extension; ignoring file"
            continue;

        # continue on with this file
        update_copyright(filePath)