|
1
|
|
|
# Copyright (c) 2014, Salesforce.com, Inc. All rights reserved. |
|
2
|
|
|
# Copyright (c) 2015, Gamelan Labs, Inc. |
|
3
|
|
|
# Copyright (c) 2016, Google, Inc. |
|
4
|
|
|
# |
|
5
|
|
|
# Redistribution and use in source and binary forms, with or without |
|
6
|
|
|
# modification, are permitted provided that the following conditions |
|
7
|
|
|
# are met: |
|
8
|
|
|
# |
|
9
|
|
|
# - Redistributions of source code must retain the above copyright |
|
10
|
|
|
# notice, this list of conditions and the following disclaimer. |
|
11
|
|
|
# - Redistributions in binary form must reproduce the above copyright |
|
12
|
|
|
# notice, this list of conditions and the following disclaimer in the |
|
13
|
|
|
# documentation and/or other materials provided with the distribution. |
|
14
|
|
|
# - Neither the name of Salesforce.com nor the names of its contributors |
|
15
|
|
|
# may be used to endorse or promote products derived from this |
|
16
|
|
|
# software without specific prior written permission. |
|
17
|
|
|
# |
|
18
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
21
|
|
|
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
22
|
|
|
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
23
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24
|
|
|
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
|
25
|
|
|
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
26
|
|
|
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
|
27
|
|
|
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
|
28
|
|
|
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29
|
|
|
|
|
30
|
|
|
from __future__ import print_function |
|
31
|
|
|
import fnmatch |
|
32
|
|
|
import os |
|
33
|
|
|
import re |
|
34
|
|
|
import parsable |
|
35
|
|
|
|
|
36
|
|
|
dir_blacklist = [ |
|
37
|
|
|
'.git', |
|
38
|
|
|
] |
|
39
|
|
|
|
|
40
|
|
|
FILES = sorted( |
|
41
|
|
|
os.path.join(root, filename) |
|
42
|
|
|
for root, dirnames, filenames in os.walk('.') |
|
43
|
|
|
if not any(d in root.split('/') for d in dir_blacklist) |
|
44
|
|
|
for filename in fnmatch.filter(filenames, '*.py') |
|
45
|
|
|
) |
|
46
|
|
|
|
|
47
|
|
|
LICENSE = [] |
|
48
|
|
|
with open('LICENSE.txt') as f: |
|
49
|
|
|
for line in f: |
|
50
|
|
|
LICENSE.append('# {}'.format(line.rstrip()).strip()) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
@parsable.command |
|
54
|
|
|
def show(): |
|
55
|
|
|
''' |
|
56
|
|
|
List all files that should have a license. |
|
57
|
|
|
''' |
|
58
|
|
|
for filename in FILES: |
|
59
|
|
|
print(filename) |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def read_and_strip_lines(filename): |
|
63
|
|
|
lines = [] |
|
64
|
|
|
with open(filename) as i: |
|
65
|
|
|
writing = False |
|
66
|
|
|
for line in i: |
|
67
|
|
|
line = line.rstrip() |
|
68
|
|
|
if not writing and line and not line.startswith('#'): |
|
69
|
|
|
writing = True |
|
70
|
|
|
if writing: |
|
71
|
|
|
lines.append(line) |
|
72
|
|
|
return lines |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
def write_lines(lines, filename): |
|
76
|
|
|
with open(filename, 'w') as f: |
|
77
|
|
|
for line in lines: |
|
78
|
|
|
print(line, file=f) |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
@parsable.command |
|
82
|
|
|
def strip(): |
|
83
|
|
|
''' |
|
84
|
|
|
Strip headers from all files. |
|
85
|
|
|
''' |
|
86
|
|
|
for filename in FILES: |
|
87
|
|
|
lines = read_and_strip_lines(filename) |
|
88
|
|
|
write_lines(lines, filename) |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
@parsable.command |
|
92
|
|
|
def update(): |
|
93
|
|
|
''' |
|
94
|
|
|
Update headers on all files to match LICNESE.txt. |
|
95
|
|
|
''' |
|
96
|
|
|
for filename in FILES: |
|
97
|
|
|
extension = re.search('\.[^.]*$', filename).group() |
|
98
|
|
|
lines = read_and_strip_lines(filename) |
|
99
|
|
|
if lines and lines[0]: |
|
100
|
|
|
if extension == '.py' and lines[0].startswith('class '): |
|
101
|
|
|
lines = [''] + lines # pep8 compliance |
|
102
|
|
|
write_lines(LICENSE + [''] + lines, filename) |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
if __name__ == '__main__': |
|
106
|
|
|
parsable.dispatch() |
|
107
|
|
|
|