root / Mulberry / trunk / license-change.py

Revision 63, 3.2 kB (checked in by daboo, 1 year ago)

tabs->spaces. Add option to undo license change.

  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2
3import getopt
4import os
5import sys
6
7newlicense = """/*
8    Copyright (c) 2007 Cyrus Daboo. All rights reserved.
9   
10    Licensed under the Apache License, Version 2.0 (the "License");
11    you may not use this file except in compliance with the License.
12    You may obtain a copy of the License at
13   
14        http://www.apache.org/licenses/LICENSE-2.0
15   
16    Unless required by applicable law or agreed to in writing, software
17    distributed under the License is distributed on an "AS IS" BASIS,
18    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19    See the License for the specific language governing permissions and
20    limitations under the License.
21*/
22
23"""
24
25def process_directory(path, delete_unlicensed, undo):
26    for file in os.listdir(path):
27        fpath = os.path.join(path, file)
28        if os.path.isfile(fpath):
29            if undo:
30                undo_file(fpath)
31            else:
32                process_file(fpath, delete_unlicensed)
33        elif os.path.isdir(fpath):
34            process_directory(fpath, delete_unlicensed, undo)
35
36def undo_file(fpath):
37    if (fpath.endswith(".cp.unlicensed") or
38        fpath.endswith(".cc.unlicensed") or
39        fpath.endswith(".cpp.unlicensed") or
40        fpath.endswith(".c.unlicensed") or
41        fpath.endswith(".h.unlicensed")):
42        fpath = fpath[:-11]
43        print fpath
44        os.rename(fpath, "%s.licensed" % fpath)
45        os.rename("%s.unlicensed" % fpath, fpath)
46        os.remove("%s.licensed" % fpath)
47
48def process_file(fpath, delete_unlicensed):
49    if delete_unlicensed and \
50       (fpath.endswith(".cp.unlicensed") or
51        fpath.endswith(".cc.unlicensed") or
52        fpath.endswith(".cpp.unlicensed") or
53        fpath.endswith(".c.unlicensed") or
54        fpath.endswith(".h.unlicensed")):
55        os.remove(fpath)
56    elif not delete_unlicensed and \
57         (fpath.endswith(".cp") or
58          fpath.endswith(".cc") or
59          fpath.endswith(".cpp") or
60          fpath.endswith(".c") or
61          fpath.endswith(".h")):
62        replace_license(fpath)
63
64def replace_license(fpath):
65    print fpath
66    fin = open(fpath, "rU")
67    fout = open("%s.licensed" % fpath, "w")
68
69    first = True
70    ignore = False
71    reallyignore = False
72    ignored = []
73    for line in fin:
74        if first:
75            fout.write(newlicense)
76            if line.startswith("/*"):
77                ignore = True
78        first = False
79        if not ignore:
80            fout.write(line)
81        else:
82            ignored.append(line)
83        if ignore:
84            if line.find("Copyright") != -1:
85                reallyignore = True
86            if line.find("*/") != -1:
87                ignore = False
88                if not reallyignore:
89                    fout.write("".join(ignored))
90   
91    fin.close()
92    fout.close()
93    os.rename(fpath, "%s.unlicensed" % fpath)
94    os.rename("%s.licensed" % fpath, fpath)
95
96if __name__ == "__main__":
97
98    delete_unlicensed = False
99    undo = False
100
101    options, args = getopt.getopt(sys.argv[1:], "ru")
102
103    for option, value in options:
104        if option == "-r":
105            delete_unlicensed = True
106        elif option == "-r":
107            undo = True
108
109    paths = args
110
111    for path in paths:
112        process_directory(path, delete_unlicensed, undo)
113           
Note: See TracBrowser for help on using the browser.