File: license-header-replace.py

package info (click to toggle)
coot 1.1.18%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,004 kB
  • sloc: cpp: 495,934; python: 35,043; ansic: 26,143; lisp: 22,768; sh: 13,186; makefile: 2,746; awk: 441; xml: 245; csh: 14
file content (157 lines) | stat: -rw-r--r-- 5,585 bytes parent folder | download | duplicates (2)
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

import sys
import os

def write_lines(lines, file_name):

    f = open(file_name, "w")
    try:
        for l in lines:
            f.write(l)
    except TypeError as e:
        print(e, "with l", l)
    f.close()

def make_copyright_line(person, copyright_year):

    pp = person
    if person == "Martin": pp = "Martin Noble, University of Oxford"
    if person == "Kevin":  pp = "Kevin Cowtan & University York"

    line = " * Copyright " + copyright_year + " by " + pp + "\n"
    return line

def insert_new_header(person, file_name, copyright_year, lines):

    copyright_line = make_copyright_line(person, copyright_year)
    file_line = " * " + file_name + "\n"
    author_line = " * Author: " + person + "\n"
    if person == "Martin": author_line = " * Author: Martin Noble\n"

    new_lines = [ "/*\n",
                  file_line,
                  " *\n",
                  copyright_line,
                  author_line,
                  " *\n",
                  " * This file is part of Coot\n",
                  " *\n",
                  " * This program is free software; you can redistribute it and/or modify\n",
                  " * it under the terms of the GNU Lesser General Public License as published\n",
                  " * by the Free Software Foundation; either version 3 of the License, or (at\n",
                  " * your option) any later version.\n",
                  " *\n",
                  " * This program is distributed in the hope that it will be useful, but\n",
                  " * WITHOUT ANY WARRANTY; without even the implied warranty of\n",
                  " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n",
                  " * General Public License for more details.\n",
                  " *\n",
                  " * You should have received a copy of the GNU General Public License\n",
                  " * along with this program; if not, write to the Free Software\n",
                  " * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n",
                  " * 02110-1301, USA\n",
                  " */\n" ]

    el = enumerate(new_lines)
    for idx,new_line in el:
        lines.insert(idx, new_line)

def get_copyright_year(line):

    y = ""
    parts = line.split()
    # now find the first part after "Copyright" that contains "20"
    idx_copyright = -1
    for idx,part in enumerate(parts):
        if part == "Copyright":
            idx_copyright = idx
        if part == "(C)":
            idx_copyright = idx
    if idx_copyright != -1:
        for idx,part in enumerate(parts):
            if idx > idx_copyright:
                if "20" in part:
                    y = part
    if "Created on" in line: y = parts[-1]

    return y

def get_author(line):
    parts = line.split()
    author = parts[-2] + " " + parts[-1]
    return author

def replace_lines(file_name, lines):

    done = False

    # ---------------- Martin's code style

    if True:
        raw_top_do_it = False
        if '#include ' in lines[0]: raw_top_do_it = True
        if '#define '  in lines[1]: raw_top_do_it = True
        if '#ifndef '  in lines[1]: raw_top_do_it = True
        if '#ifndef '  in lines[0]: raw_top_do_it = True

        if raw_top_do_it:
            insert_new_header("Martin", file_name, "2009", lines)
            done = True

        for idx,line in enumerate(lines):
            if done: continue
            if idx < 10:
                if line == " */\n":
                    for i in range(idx+1):
                        lines.pop(0)
                    insert_new_header("Martin", file_name, "2009", lines)
                    done = True

        fparts = 0
        for idx,line in enumerate(lines):
            if done: continue
            if idx > 10: continue
            if "//  MoleculesToTriangles"    in line: fparts += 1
            if "//  Created by Martin Noble" in line: fparts += 1
            if "//  Copyright "              in line: fparts += 1
            if "Copyright" in line: copyright_year = get_copyright_year(line)
            if line == "//\n":
                if fparts == 3:
                    for i in range (idx+1):
                        lines.pop(0)
                    insert_new_header("Martin", file_name, copyright_year, lines)
                    done = True

    # ---------------- Kevin's code style

    if False:
        copyright_year = ""
        for idx,line in enumerate(lines):
            if done: continue
            if " (C) "        in line: copyright_year = get_copyright_year(line)
            if " Copyright "  in line: copyright_year = get_copyright_year(line)
            if " Created on:" in line: copyright_year = get_copyright_year(line)
            if " Author: "    in line: author = get_author(line)
            if "York all rights reserved" in line:
                lines.pop(idx)
                lines.pop(idx-1)
                insert_new_header("Kevin Cowtan", file_name, copyright_year, lines)
                done = True

            if idx < 10:
                if " */\n" in line:
                    if len(line) == 4:
                        for i in range(idx+1):
                            lines.pop(0)
                        insert_new_header(author, file_name, copyright_year, lines)
                        done = True

if len(sys.argv) > 1:
    fn = sys.argv[1]
    new_file_name = fn + ".new"
    print("Converting header:", fn)
    f = open(fn)
    lines = f.readlines()
    if len(lines) > 6:
        replace_lines(fn, lines)
        write_lines(lines, new_file_name)