~drizzle-trunk/drizzle/development

1192.6.2 by Robert Collins
Merged in lifeless' fix for parallel lint and only linting changed files.
1
#!/usr/bin/env python
2
#  Copyright (C) 2009 Sun Microsystems
3
#  Copyright (C) 2009 Robert Collins
4
#
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.
8
#
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.
13
#
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
17
18
import os.path
19
srcdir = os.environ.get('srcdir','.')
20
path = os.path.join(srcdir, 'config', 'lint-rules.am')
21
22
# relpath import (available in Python 2.6 and above)
23
try:
24
    relpath = os.path.relpath
25
except AttributeError:
26
27
    from os.path import curdir, sep, pardir, join
28
29
    def relpath(path, start=curdir):
30
        """Return a relative version of a path"""
31
32
        if not path:
33
            raise ValueError("no path specified")
34
35
        start_list = os.path.abspath(start).split(sep)
36
        path_list = os.path.abspath(path).split(sep)
37
38
        # Work out how much of the filepath is shared by start and path.
39
        i = len(os.path.commonprefix([start_list, path_list]))
40
41
        rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
42
        if not rel_list:
43
            return curdir
44
        return join(*rel_list)
45
46
class ChangeProtectedFile(object):
47
  # from register_plugins.py. XXX: duplication. fix.
48
49
  def __init__(self, fname):
50
    self.bogus_file= False
51
    self.real_fname= fname
52
    self.new_fname= "%s.new" % fname
53
    try:
54
      self.new_file= open(self.new_fname,'w+')
55
    except IOError:
56
      self.bogus_file= True
57
58
  def write(self, text):
59
    if not self.bogus_file:
60
      self.new_file.write(text)
61
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
65
  def close(self):
66
    """Return True if the file had changed."""
67
    if self.bogus_file:
68
      return
69
    self.new_file.seek(0)
70
    new_content = self.new_file.read()
71
    self.new_file.close()
72
    try:
73
        old_file = file(self.real_fname, 'r')
74
        old_content = old_file.read()
75
        old_file.close()
76
    except IOError:
77
        old_content = None
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)
82
      return True
83
    else:
84
        try:
85
          os.unlink(self.new_fname)
86
        except:
87
          pass
88
output = ChangeProtectedFile(path)
89
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.
93
94
95
def lint_path(path):
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
100
    # the lint output
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")
107
    #    lint_file.close()
108
109
110
def clean_lints(paths):
111
    output.write('cleanlints:\n')
112
    # batch in 50
113
    for pos in range(len(paths)/50 + 1):
114
        path_str = ' '.join((path + '.linted') for path in paths[pos *50:(pos + 1)*50])
115
        if not path_str:
116
            continue
117
        output.write('\trm -f %s\n' % path_str)
118
119
120
def should_lint(path):
121
    if not (path.endswith('.cc') or path.endswith('.h')):
122
        return False
123
    if not (path.startswith('plugin/') or path.startswith('drizzled/') or
124
        path.startswith('client/')):
125
        return False
1497.3.23 by Monty Taylor
Fixes stewart's uncleanliness.
126
    # Let's not lint emacs autosave files
1497.3.25 by Monty Taylor
typtypoo
127
    if (os.path.split(path)[-1].startswith('.#')):
1497.3.23 by Monty Taylor
Fixes stewart's uncleanliness.
128
        return False
1548.3.1 by Monty Taylor
Added pbms to lint exclusion list. (We need to find a better way to deal
129
    # We need to figure out how to better express this
130
    for exclude in ['innobase', 'pbxt', 'pbms', 'gnulib', '.pb.', 'bak-header', 'm4',
1192.6.2 by Robert Collins
Merged in lifeless' fix for parallel lint and only linting changed files.
131
        'sql_yacc', 'gperf', 'drizzled/probes.h',
132
        'drizzled/function_hash.h', 'drizzled/symbol_hash.h',
133
        'util/dummy.cc', 'drizzled/sql_yacc.h', 'drizzled/configmake.h',
1253.2.4 by Monty Taylor
Some GCC warnings fixes.
134
	'drizzled/plugin/version.h',
1497.3.7 by Monty Taylor
We don't want to lint generated_probes.h.
135
        'drizzled/generated_probes.h',
1530.2.5 by Monty Taylor
Renamed classes that were in drizzled::plugin but which were not meant
136
        'drizzled/module/load_list.h']:
1192.6.2 by Robert Collins
Merged in lifeless' fix for parallel lint and only linting changed files.
137
        if exclude in path:
138
            return False
139
    return True
140
141
def accumulate_sources(arg, dirname, fnames):
142
    for fname in fnames:
143
        path = os.path.join(dirname, fname)
144
        path = relpath(path, srcdir)
145
        if not should_lint(path):
146
            continue
147
        arg.append(path)
148
149
sources_list = []
150
os.path.walk(srcdir,accumulate_sources,sources_list)
1241.9.11 by Robert Collins
Fixed a sort order problem which was causing spurious extra rebuilds.
151
sources_list.sort()
1192.6.2 by Robert Collins
Merged in lifeless' fix for parallel lint and only linting changed files.
152
for path in sources_list:
153
    lint_path(path)
154
clean_lints(sources_list)
155
156
output.close()