summaryrefslogtreecommitdiff
path: root/cad/src/tools/UpdateCopyright.py
blob: cd69131bd9d4437061f22e541dace1dd00cee716 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env python

# Copyright 2007 Nanorex, Inc.  See LICENSE file for details.

"""
UpdateCopyright.py

This is a standalone python script intended to maintain the copyright
notices in all NE1 code.  It's intended to be used before each release
in the following way:

First, make sure you have a clean copy of all source trees.  That
means up to date with no local changes.  The 'cvs update' command
should not report any modified files.

Next, run this script with the --check option.  This will print lists
of files that meet certain criteria.  In particular, you might want to
examine the files listed as containing other copyrights.  These may
need to be verified, and perhaps updated manually.  You can make any
necessary edits now or later.

Next, run the script with the --modify option.  This will take some
time, as it runs a 'cvs log' command on each file to determine the
years in which it was modified.  New copyright lines will be written
to the files, and the changes made will be printed as context diffs.
Examine the diffs to make sure the script has done the right thing.

Finally, commit the changes to CVS.

Other options available are:

--dry-run
 Produces the diff output, but does not modify the original files.

--no-cvs
 Speeds up the run by omitting the cvs log commands for each file.
 Files are assumed to be modified in the current year only.  Old
 copyright dates are still merged into the new copyright strings, so
 this option could be used once all copyrights have been generated by
 a run without --no-cvs.  Note, however, that this will add the
 current year to a copyright line even if no checkins have occurred on
 this file in the current year.

@copyright: 2007 Nanorex, Inc.
"""

import sys
import os
import os.path
import re
import time

# command line flags
OptionCheck = False
OptionModify = False
OptionDryRun = False
OptionNoCVS = False

pathsToExclude = [
    "./src/reindent.py",  # cad, public domain
    ]

# These files must not require special comment characters.
pathsToInclude = [
    "./src/MergingParameterFiles", # sim
    "./src/merge.parameters",
    "./src/branch",                # both
    "./src/INSTALL",               # cad
    "./src/README-Pyrex",
    "./src/README-cvs-branching",
    ]

directoriesToExclude = [
    "/CVS",
    "/bin", # in cad
#    "/doc", # in cad ######################################################## temp
    "/doc/old_doc", # in cad
    "/licenses-common", # in cad
    "/licenses-Linux", # in cad
    "/licenses-Mac", # in cad
    "/licenses-Windows", # in cad
    "/src/tests", # in sim.  including could impact test comparisons
    "/src/parameters", # in sim.  lots of strange files - do by hand
    ]

# List of filename extensions to process.  Each extension is followed
# by the strings which should start and end any comment lines inserted
# into the file.
commentStringMapping = [
    "sim-params.txt", "#", "",
    ".py",   "#",    "",    # python source
    ".pyx",  "#",    "",    # pyrex source
    ".desc", "#",    "",    # plugin parameter description
    ".htm",  "<!--", "-->", # html
    ".html", "<!--", "-->", # html
    ".dwt",  "<!--", "-->", # html
    ".c",    "//",   "",    # c source
    ".cpp",  "//",   "",    # c++ source
    ".h",    "//",   "",    # c source
    ".sh",   "#",    "",    # unix shell
    ".txt",  "",     "",    # text
    "BUGS",  "",     "",
    "NOTES", "",     "",
    "README", "",    "",
    "Doxyfile", "#", "",    # project description
    "Makefile", "#", "",
    "/makethemall", "#", "", # sh script
    "/jbranch", "#", "",    # sh script
    "/rungms", "#", "",     # sh script
    "/runmac", "#", "",     # sh script
    ]

# If we're printing a message for each file we DON'T process, then we
# don't want to print such a message for any of these files.
excludeQuietly = [
    ".bmp", # binary image
    ".db",  # binary database
    ".doc", # binary word document
    ".gif", # binary image
    ".gz",  # binary archive
    ".ico", # binary image
    ".inp", # gamess input file
    ".java",# java source file (original CoNTub source)
    ".jpg", # binary image
    ".js",  # javascript -- generated by epydoc
    ".mmp", ###############
    ".moi", # ascii fragment of coordinates, no known comment structure
    ".o",   # binary
    ".parms", # ascii sim parameters intermediate -- could probably use # comment
    ".pdb", # ascii atom format -- may have comment, not sure of format
    ".png", # binary image
    ".psp", # binary image -- paint shop pro
    ".pyc", # binary compiled python
    ".so",  # binary shared library
    ".sxc", # binary -- sun xml calc
    ".ui",  # generated xml file
    ".xls", # binary spreadsheet
    ".zip", # binary archive
    ".~",   # cvs backup file
    ]

# Files which start with any of these strings should have a copyright
# inserted at the beginning, rather than after line 1 or 2.
badFirstLineStrings = [
    '"""',
    "'''",
    "/*",
    "#define",
    "#if",
    "#include",
    "//",
    "from",
    "import",
    "cdef",
    ]

# Extract year of modification from the output of "cvs log file".
cvsLogDateRegexp = re.compile(r'^date: (\d\d\d\d)-\d\d-\d\d')

# The date list regex embedded in the following regexes looks like
# this: dddd ( ,|- dddd )* a list of groups of up to four digits,
# separated by commas or dashes.

# This regex recognizes existing copyright lines, including an
# optional (c), which will be removed.
copyrightRegex = re.compile(r'Copyright\s+(?:\(c\))?\s*(\d{1,4}(\s*(\,|\-)\s*\d{1,4})*)\s+Nanorex\,\s+Inc\.')

# This regex recognizes the @copyright: epydoc field.
epydocCopyrightRegex = re.compile(r'^(.*\@copyright\:\s*)(\d{1,4}(\s*(\,|\-)\s*\d{1,4})*)(\s+Nanorex\,\s+Inc\..*)')

# This is used to extract parts of the above date range lists.  Group
# 1 is the first date, group 2 is the separator, group 3 is the
# remainder.  If there's no separator, then the whole string should
# just be the year.
dateRangeRegex = re.compile(r'(\d{1,4})\s*(\,|\-)\s*(.*)')

blankLineRegex = re.compile(r'^\s*$')

anyCopyrightRegex = re.compile(r'copyright', re.IGNORECASE)

def printExcluding(path):
    for extension in excludeQuietly:
        if (path.endswith(extension)):
            return
    FileSetExcluded.add(path)

def visitFilesRecursively(path, functionToApply):
    extensionsToInclude = commentStringMapping[::3]
    for (path, dirs, files) in os.walk(path):
        skipDirectory = False
        for excludeDir in directoriesToExclude:
            if (path.endswith(excludeDir)):
                skipDirectory = True
                break
        if (skipDirectory):
            del dirs[:]
            continue
        for file in files:
            # ignore hidden files, which start with .
            if (file.startswith(".")):
                continue

            filename = "%s%s%s" % (path, os.sep, file)

            exclude = False
            for exclusion in pathsToExclude:
                if (filename == exclusion):
                    printExcluding(filename)
                    exclude = True
                    break
            if (exclude):
                continue

            exclude = True
            for extension in extensionsToInclude:
                if (filename.endswith(extension)):
                    functionToApply(filename)
                    exclude = False
                    break
            if (exclude):
                printExcluding(filename)

    for filename in pathsToInclude:
        if (os.path.exists(filename)):
            FileSetExcluded.remove(filename)
            functionToApply(filename)

# Returns a set of years in which modification to the given file were
# performed, according to the output of "cvs log path".
def determineModificationYears(path):
    if (OptionNoCVS):
        return set([ThisYear])

    dir = os.path.dirname(path)
    file = os.path.basename(path)
    cvsCommand = "cvs log %s" % file

    os.chdir(dir)
    results = os.popen(cvsCommand)
    os.chdir(WorkingDirectory)

    # If the file has not been checked in, cvs will complain, and this
    # routine will return an empty set.  When yearSetToYearRanges is
    # passed this empty set, it will add ThisYear, which should be
    # correct.

    years = set()
    for line in results:
        match = re.match(cvsLogDateRegexp, line)
        if (match):
            year = match.group(1)
            years.add(year)
    results.close()
    return years

# helper function factored out of yearSetToYearRanges()
def appendDate(s, start, previous, current):
    # We've seen a range of dates from start to previous, then a gap,
    # followed by current (which will become start on the next call).
    # If start == previous, there was only one date before the first
    # gap, so we don't generate the range.  If current == previous,
    # the list ends with a range, so we don't generate the gap.
    if (previous > start):
        s = s + "-"
        s = s + str(previous)
    if (current > previous):
        s = s + ", "
        s = s + str(current)
    return s

# Given a set of years, returns a string representing that set.  Years
# in the result are sorted, ranges are indicated by "YYYY-ZZZZ", gaps
# between ranges are indicated by "YYYY, ZZZZ".  Typical output might
# be "2001-2003, 2005, 2007".
def yearSetToYearRanges(yearSet):
    yearList = list(yearSet)
    yearList.sort()
    if (len(yearList) == 0):
        return ThisYear
    result = yearList[0]
    start = int(yearList[0])
    previous = start
    current = start
    for i in range(1, len(yearList)):
        current = int(yearList[i])
        if (current == previous + 1):
            previous = current
            # keep accumulating more of this range
            continue
        result = appendDate(result, start, previous, current)
        start = current
        previous = current
    result = appendDate(result, start, previous, current)
    return result

def daterangeToSet(dateRange):
    years = set()
    lastSeparator = ','
    lastYear = 2007
    while (True):
        match = re.search(dateRangeRegex, dateRange)
        if (match):
            nextYear = int(match.group(1))
            nextSeparator = match.group(2)
            remainder = match.group(3)
        else:
            nextYear = int(dateRange)


        if (nextYear < 100):
            if (nextYear > 50):
                nextYear = nextYear + 1900
            else:
                nextYear = nextYear + 2000

        if (lastSeparator == ','):
            years.add(str(nextYear))
        else:
            for year in range(lastYear, nextYear):
                years.add(str(year+1))

        if (match):
            lastYear = nextYear
            lastSeparator = nextSeparator
            dateRange = remainder
        else:
            return years

# Given a path name for a file, looks up the extension in
# commentStringMapping, and returns the beginning and end of line
# comment strings for that file type.
def getCommentStrings(path):
    for i in range(0, len(commentStringMapping), 3):
        if (path.endswith(commentStringMapping[i])):
            return (commentStringMapping[i+1], commentStringMapping[i+2])
    return ("", "")

def writeCopyrightLine(f, path, dateString):
    (commentStart, commentEnd) = getCommentStrings(path)
    f.write("%s Copyright %s Nanorex, Inc.  See LICENSE file for details. %s\n" % (commentStart, dateString, commentEnd))

def processFile(path):
    newFilename = path + ".new"

    years = determineModificationYears(path)
    finalDateRange = yearSetToYearRanges(years)

    file = open(path, 'r')

    line = file.readline()

    if (not line):
        # empty file
        return

    newFile = open(newFilename, 'w')

    if (not re.search(copyrightRegex, line)):
        firstLineOK = True
        for bad in badFirstLineStrings:
            if (line.startswith(bad)):
                firstLineOK = False
                break
        if (firstLineOK):
            newFile.write(line)
            line = file.readline()

    if (not re.search(copyrightRegex, line)):
        if (re.search(blankLineRegex, line)):
            newFile.write(line)
            line = file.readline()

    match = re.search(copyrightRegex, line)
    if (match):
        daterange = match.group(1)
        years = years | daterangeToSet(daterange)
        finalDateRange = yearSetToYearRanges(years)
        writeCopyrightLine(newFile, path, finalDateRange)
    else:
        writeCopyrightLine(newFile, path, finalDateRange)
        newFile.write(line)
    line = file.readline()
    while (line):
        match = re.search(epydocCopyrightRegex, line)
        if (match):
            line = match.group(1) + finalDateRange + match.group(match.lastindex) + "\n"
        newFile.write(line)
        line = file.readline()

    file.close()
    newFile.close()
    os.system("diff -c %s %s" % (path, newFilename))
    if (OptionDryRun):
        os.remove(newFilename)
    else:
        os.rename(newFilename, path)

def checkFile(path):
    newFilename = path + ".new"

    file = open(path, 'r')

    line = file.readline()

    if (not line):
        FileSetEmpty.add(path)
        return

    if (not re.search(copyrightRegex, line)):
        firstLineOK = True
        for bad in badFirstLineStrings:
            if (line.startswith(bad)):
                firstLineOK = False
                break
        if (firstLineOK):
            line = file.readline()

    if (not re.search(copyrightRegex, line)):
        if (re.search(blankLineRegex, line)):
            line = file.readline()

    match = re.search(copyrightRegex, line)
    if (match):
        FileSetHasInitialCopyright.add(path)
    else:
        FileSetNeedsInitialCopyright.add(path)

    line = file.readline()
    while (line):
        match = re.search(epydocCopyrightRegex, line)
        if (match):
            FileSetHasEpydocCopyright.add(path)
        else:
            match = re.search(anyCopyrightRegex, line)
            if (match):
                FileSetHasOtherCopyright.add(path)

        line = file.readline()

    file.close()

def printFileSet(headerString, fileSet):
    fileList = list(fileSet)
    if (len(fileList) == 0):
        return
    fileList.sort()
    print headerString
    for file in fileList:
        print " " + file + " \\"
    print ""

if (__name__ == '__main__'):

    ThisYear = time.strftime("%Y")

    for i in range(1, len(sys.argv)):
        if (sys.argv[i] == "--check"):
            OptionCheck = True
        elif (sys.argv[i] == "--modify"):
            OptionModify = True
        elif (sys.argv[i] == "--dry-run"):
            OptionDryRun = True
        elif (sys.argv[i] == "--no-cvs"):
            OptionNoCVS = True
        else:
            print >>sys.stderr, "UpdateCopyright.py: unrecognized option: " + sys.argv[i]
            sys.exit(1)

    optionCount = 0
    if (OptionCheck):
        optionCount += 1
    if (OptionModify):
        optionCount += 1
    if (OptionDryRun):
        optionCount += 1
    if (optionCount != 1):
        print >>sys.stderr, "UpdateCopyright.py:"
        print >>sys.stderr, " you must specify exactly one of:"
        print >>sys.stderr, "  --check, --modify, --dry-run"
        sys.exit(1)

    WorkingDirectory = os.getcwd()
    FileSetExcluded = set()
    if (OptionCheck):
        FileSetEmpty = set()
        FileSetHasInitialCopyright = set()
        FileSetNeedsInitialCopyright = set()
        FileSetHasEpydocCopyright = set()
        FileSetHasOtherCopyright = set()

        visitFilesRecursively(".", checkFile)

        printFileSet("Excluded files:", FileSetExcluded)
        printFileSet("Empty files:", FileSetEmpty)
        printFileSet("Has initial copyright:", FileSetHasInitialCopyright)
        printFileSet("Needs initial copyright:", FileSetNeedsInitialCopyright)
        printFileSet("Has Epydoc copyright:", FileSetHasEpydocCopyright)
        printFileSet("Has other copyright:", FileSetHasOtherCopyright)

    if (OptionModify or OptionDryRun):
        visitFilesRecursively(".", processFile)