summaryrefslogtreecommitdiffstats
path: root/update_headers.py
blob: 15c549b448318131afb3c0205f8085e21f227080 (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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
'Checks for standard headers and update version and copyright info in python files'
__url__ = 'http://github.com/silnrsi/pysilfont'
__copyright__ = 'Copyright (c) 2016 SIL International (http://www.sil.org)'
__license__ = 'Released under the MIT License (http://opensource.org/licenses/MIT)'
__author__ = 'David Raymond'

cyear = "2016" # Year to use if no other copyright year present

from silfont.core import execute
import os,sys

argspec = [
    ('action',{'help': 'Action - report or update', 'nargs': '?', 'default': 'report', 'choices': ('report','update')},{}),
    ('-l','--log',{'help': 'Log file'}, {'type': 'outfile', 'def': 'local/update_headers.log'})]

def doit(args) :
    global file
    action = args.action
    params = args.paramsobj
    logger=params.logger

    varlist = ['url', 'copyright', 'license', 'author', 'version']

    copyrightpre = 'Copyright (c) '
    copyrightpost =  ' SIL International (http://www.sil.org)'

    standards = {
        'copyright': copyrightpre + cyear + copyrightpost,
        'version': params.sets['default']['version'],
        'url': 'http://github.com/silnrsi/pysilfont',
        'license': 'Released under the MIT License (http://opensource.org/licenses/MIT)'}

    pythonfiles = {}
    otherfiles = []

    for subdir, dirs, files in os.walk("."):
        if not (subdir=="." or subdir[0:5] in ("./lib","./scr")) : continue
        if subdir[0:] == "./lib/pysilfont.egg-info" : continue

        for filen in files:
            if filen[-1:]=="~" : continue
            if filen[-3:]=="pyc" : continue
            if filen in ("__init__.py", "ez_setup.py") : continue
            needver = (True if filen in ('setup.py', 'param.py') else False)
            fulln = os.path.join(subdir,filen)
            file = open(fulln,"r")
            line1 = nextline()
            pyline1 = (True if line1 in ("#!/usr/bin/env python", "#!/usr/bin/python") else False)
            if pyline1 or filen[-3:] == ".py" :
                # Look for standard headers
                headererror = []
                headers = "#!/usr/bin/env python"
                if pyline1 :
                    # Read description which may be single or multiline
                    line = nextline()
                    headers = headers + "\n"+line
                    if line[0:3] == "'''" :
                        while line[-3:] != "'''" :
                            line = nextline()
                            if line =="EOF" : break # Must be EOF
                            headers = headers + "\n"+line
                        if line =="EOF" : headererror.append("No closing ''' to description")
                    elif line[0:1] != "'" : headererror.append("No description")
                    if headererror :
                        for line in headererror : logger.log(fulln + ": "+line,"E")
                        continue
                    # Read header variables
                    headvar={}
                    line = nextline()
                    while line[0:2] == "__" :
                        endn = line.find("__ = '")
                        if endn == -1 : std = headererror.append("Invalid variable line: " + line)
                        varn = line[2:endn]
                        val = line[endn+6:-1]
                        headvar[varn] = val
                        line = nextline()
                    # Check header variables
                    updatevars = {}
                    reportvars = {}
                    author = None
                    for varn in varlist :
                        if varn in headvar:
                            headval = headvar[varn]
                            if varn == 'author' : # Simply use existing author
                                author = headval
                            elif varn == "version" and not needver :
                                updatevars[varn] = "deleted"
                            elif varn == "copyright" : # Need to check dates and use oldest
                                # Find existing dates, assuming format 20nn and one or two dates
                                cdate = cyear
                                valid = True
                                datpos = headval.find("20")
                                if datpos != -1 :
                                    # read any more digits
                                    cdate='20'
                                    nextpos = datpos+2
                                    while headval[nextpos] in '0123456789' and nextpos < len(headval) :
                                        cdate = cdate + headval[nextpos]
                                        nextpos += 1
                                    # Look for second date
                                    rest = headval[nextpos:]
                                    datpos = rest.find("20")
                                    date2 = ""
                                    if datpos != -1 :
                                        date2 = '20'
                                        nextpos = datpos+2
                                        while rest[nextpos] in '0123456789' and nextpos < len(rest) :
                                            date2 = date2 + rest[nextpos]
                                            nextpos += 1
                                    cval=int(cdate)
                                    if cval < 2000 or cval > int(cyear) : valid = False
                                    if date2 != "" :
                                        val2 = int(date2)
                                        if val2 < cval or val2 > int(cyear) : valid = False
                                    if not valid : cdate = cyear
                                copyright = copyrightpre + cdate + copyrightpost
                                if headval != copyright :
                                    updatevars[varn] = ("updated" if valid else "update (invalid dates)")
                            else :
                                if headval != standards[varn] :
                                    updatevars[varn] = "updated"
                        else :
                            if varn == 'author' :
                                reportvars[varn] = "no author"
                            elif varn == "version" and not needver :
                                pass
                            else:
                                updatevars[varn] ="added"
                    for varn in headvar:
                        if varn not in varlist: reportvars[varn] = "non-standard"
                else :
                    logger.log( fulln + ": " + "No python header - first line is " + line1, "E")
                    continue
            else :
                otherfiles.append(fulln)
                continue

            # Now have python file with no errors, so can update headers
            if action == 'update' and updatevars :
                logger.log("Updating "+fulln,"P")
                outfile = open("update_headers_temp.txt", "w")
                outfile.write(headers + "\n")
                for varn in varlist :
                    if varn == "version" and not needver :
                        pass
                    elif varn == "author" :
                        if author : outfile.write("__author__ = '" + author + "'\n")
                    elif varn == "copyright" :
                        outfile.write("__copyright__ = '" + copyright + "'\n")
                    else:
                        outfile.write("__" + varn + "__ = '" + standards[varn] + "'\n")
                    if varn in updatevars :
                        reason = updatevars[varn]
                        if reason == "no author" :
                            logger.log("No author header variable ", "I")
                        else :
                            logger.log("Header variable " + varn + " " + reason, "I")
                for varn in reportvars :
                    reason = reportvars[varn]
                    if reason == "non-standard" :
                        outfile.write("__" + varn + "__ = '" + headvar[varn] + "'\n")
                        logger.log("Non-standard header variable " + varn + " retained", "W")
                    else:
                        logger.log("No author header variable", "I")
                # Write the rest of the file
                outfile.write(line + "\n") # last line read checking headers
                for line in file: outfile.write(line)
                outfile.close()
                file.close()
                os.rename(fulln, fulln+"~")
                os.rename("update_headers_temp.txt",fulln)
            else :
                for varn in updatevars :
                    logger.log(fulln + ": Header variable " + varn + " will be " + updatevars[varn], "I")
                for varn in reportvars :
                    reason = reportvars[varn]
                    if reason == "non-standard" :
                        logger.log(fulln + ": Non-standard header variable " + varn + " present", "W")
                    else:
                        logger.log(fulln + ": No author header variable", "I")

    print "\n"+"Non-python files"+"\n"
    for filen in otherfiles:
        print filen

    return

def nextline() :
    global file
    line = file.readline()
    line = ("EOF" if line == "" else line.strip())
    return line

execute(None,doit, argspec)