2
# Copyright (C) 2009 Sun Microsystems
3
# Copyright (C) 2009 Robert Collins
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; version 2 of the License.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
srcdir = os.environ.get('srcdir','.')
20
path = os.path.join(srcdir, 'config', 'lint-rules.am')
22
# relpath import (available in Python 2.6 and above)
24
relpath = os.path.relpath
25
except AttributeError:
27
from os.path import curdir, sep, pardir, join
29
def relpath(path, start=curdir):
30
"""Return a relative version of a path"""
33
raise ValueError("no path specified")
35
start_list = os.path.abspath(start).split(sep)
36
path_list = os.path.abspath(path).split(sep)
38
# Work out how much of the filepath is shared by start and path.
39
i = len(os.path.commonprefix([start_list, path_list]))
41
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
44
return join(*rel_list)
46
class ChangeProtectedFile(object):
47
# from register_plugins.py. XXX: duplication. fix.
49
def __init__(self, fname):
50
self.bogus_file= False
51
self.real_fname= fname
52
self.new_fname= "%s.new" % fname
54
self.new_file= open(self.new_fname,'w+')
58
def write(self, text):
59
if not self.bogus_file:
60
self.new_file.write(text)
62
# We've written all of this out into .new files, now we only copy them
63
# over the old ones if they are different, so that we don't cause
64
# unnecessary recompiles
66
"""Return True if the file had changed."""
70
new_content = self.new_file.read()
73
old_file = file(self.real_fname, 'r')
74
old_content = old_file.read()
78
if new_content != old_content:
79
if old_content != None:
80
os.unlink(self.real_fname)
81
os.rename(self.new_fname, self.real_fname)
85
os.unlink(self.new_fname)
88
output = ChangeProtectedFile(path)
90
# We write a makefile that causes:
91
# linted to depend on linting all the source files we find
92
# linting a source file to depend on the output dep file for that linted file.
96
# linted depends on linting this:
97
output.write('linted: %s.linted\n' % path)
98
output.write('%s.linted: %s\n' % (path, path))
99
# the thing being linted depends on the dependencies included in
101
#output.write('@am__include@ @am__quote@%s.linted@am__quote@\n' % path)
102
# If the lint file doesn't exist, we'll make one, or else we have to do
103
# a full lint run on every fresh bzr checkout, which is sort of silly
104
#if not os.path.exists("%s.linted" % path):
105
# lint_file = open("%s.linted" % path,"w")
106
# lint_file.write("# Placeholder to make empty file")
110
def clean_lints(paths):
111
output.write('cleanlints:\n')
113
for pos in range(len(paths)/50 + 1):
114
path_str = ' '.join((path + '.linted') for path in paths[pos *50:(pos + 1)*50])
117
output.write('\trm -f %s\n' % path_str)
120
def should_lint(path):
121
if not (path.endswith('.cc') or path.endswith('.h')):
123
if not (path.startswith('plugin/') or path.startswith('drizzled/') or
124
path.startswith('client/')):
126
for exclude in ['innobase', 'gnulib', '.pb.', 'bak-header', 'm4',
127
'sql_yacc', 'gperf', 'drizzled/probes.h',
128
'drizzled/function_hash.h', 'drizzled/symbol_hash.h',
129
'util/dummy.cc', 'drizzled/sql_yacc.h', 'drizzled/configmake.h',
130
'drizzled/plugin/config.h']:
135
def accumulate_sources(arg, dirname, fnames):
137
path = os.path.join(dirname, fname)
138
path = relpath(path, srcdir)
139
if not should_lint(path):
144
os.path.walk(srcdir,accumulate_sources,sources_list)
145
for path in sources_list:
147
clean_lints(sources_list)