copyrightme.py

Alex Zirbel, 01/03/2012 11:44 pm

Download (5.08 KB)

 
1
#!/usr/bin/python
2

    
3
# A simple script to add or delete the copyright notice on Colony files.
4
#  - del: Replaces the copyright info with a notice in all files
5
#  - add: Replaces the notice with copyright info
6
#
7
# Works recursively from the current directory.
8
#
9
# Don't commit files with the notice by mistake! I plan to use a git hook.
10
#
11
# Author: Alex Zirbel, a picky person.
12

    
13
import sys
14
import os
15
import string
16

    
17
copyright1 = """/**
18
 * Copyright (c) 2011 Colony Project
19
 * 
20
 * Permission is hereby granted, free of charge, to any person
21
 * obtaining a copy of this software and associated documentation
22
 * files (the "Software"), to deal in the Software without
23
 * restriction, including without limitation the rights to use,
24
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
25
 * copies of the Software, and to permit persons to whom the
26
 * Software is furnished to do so, subject to the following
27
 * conditions:
28
 * 
29
 * The above copyright notice and this permission notice shall be
30
 * included in all copies or substantial portions of the Software.
31
 * 
32
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
34
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
36
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
37
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
38
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
39
 * OTHER DEALINGS IN THE SOFTWARE.
40
 */"""
41

    
42
# GIT_NOCOMMIT doesn't do anything unless you add a pre-commit hook.
43
notice1 = "/* Copyright 1 here. GIT_NOCOMMIT */"
44

    
45
copyright2 = """/**
46
 * The code in this package was developed using the structure of Willow
47
 * Garage's turtlesim package.  It was modified by the CMU Robotics Club
48
 * to be used as a simulator for the Colony Scout robot.
49
 *
50
 * All redistribution of this code is limited to the terms of Willow Garage's
51
 * licensing terms, as well as under permission from the CMU Robotics Club.
52
 * 
53
 * Copyright (c) 2011 Colony Project
54
 * 
55
 * Permission is hereby granted, free of charge, to any person
56
 * obtaining a copy of this software and associated documentation
57
 * files (the "Software"), to deal in the Software without
58
 * restriction, including without limitation the rights to use,
59
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
60
 * copies of the Software, and to permit persons to whom the
61
 * Software is furnished to do so, subject to the following
62
 * conditions:
63
 * 
64
 * The above copyright notice and this permission notice shall be
65
 * included in all copies or substantial portions of the Software.
66
 * 
67
 * Copyright (c) 2009, Willow Garage, Inc.
68
 * All rights reserved.
69
 * 
70
 * Redistribution and use in source and binary forms, with or without
71
 * modification, are permitted provided that the following conditions are met:
72
 * 
73
 *    Redistributions of source code must retain the above copyright
74
 *       notice, this list of conditions and the following disclaimer.
75
 *    Redistributions in binary form must reproduce the above copyright
76
 *       notice, this list of conditions and the following disclaimer in the
77
 *       documentation and/or other materials provided with the distribution.
78
 *    Neither the name of the Willow Garage, Inc. nor the names of its
79
 *       contributors may be used to endorse or promote products derived from
80
 *       this software without specific prior written permission.
81
 * 
82
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
83
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
84
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
85
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
86
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
87
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
88
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
89
 * OTHER DEALINGS IN THE SOFTWARE.
90
 */"""
91

    
92
# GIT_NOCOMMIT doesn't do anything unless you add a pre-commit hook.
93
notice2 = "/* Copyright 2 here. GIT_NOCOMMIT */"
94

    
95
# Check usage
96
if len(sys.argv) < 2 or (sys.argv[1] != "add" and sys.argv[1] != "del"):
97
    print "Usage:", sys.argv[0], "<add/del>"
98
    exit()
99

    
100
# All files recursively, from the current directory
101
for dirname, _, files in os.walk(os.getcwd()):
102
    for filename in files:
103

    
104
        # Don't edit the script itself!
105
        if filename == "copyrightme.py":
106
            continue
107

    
108
        # Edit in place by joining the path with the filename
109
        filename = os.path.join(dirname, filename)
110

    
111
        print filename
112

    
113
        new_contents = ""
114
        with open(filename, "r") as f:
115

    
116
            contents = "".join(f.readlines())
117

    
118
            if sys.argv[1] == "add":
119
                new_contents = string.replace(contents, notice1, copyright1)
120
                new_contents = string.replace(new_contents, notice2, copyright2)
121
            else:
122
                new_contents = string.replace(contents, copyright1, notice1)
123
                new_contents = string.replace(new_contents, copyright2, notice2)
124
        
125
        with open(filename, "w") as f:
126
            f.write(new_contents)