1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#!/usr/bin/env python
#
# Copyright (C) 2009 Anders Logg
#
# This file is part of DOLFIN.
#
# DOLFIN is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DOLFIN is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#
# Check and fix coding style in all files (currently very limited)
import os, sys, re
# Camelcaps exceptions
camelcaps_exceptions = ["cGq", "dGq", # ODE solver
"addListener", "addTest", "getRegistry", "makeTest", "wasSuccessful", # cppunit
"tagName", "tagNames", "documentElement", "childNodes", # doscstrings
"nodeType", "firstChild", "parentNode", # docstrings
"doubleArray", "intArray", # swig
"fatalError", "xmlChar", "xmlNode", "xmlStrcasecmp", # libxml2
"startDocument", "endDocument", "startElement", "endElement"] # libxml2
def camelcaps2underscore(word):
"Convert fooBar to foo_bar."
new_word = ""
for c in word:
if c.isupper() and len(new_word) > 0:
new_word += "_" + c.lower()
else:
new_word += c
return new_word
def check_camelcaps(code, replacements):
"Replace fooBar by foo_bar."
words = re.findall(r"\b([a-z]+[A-Z][a-z]+)\b", code)
for word in words:
if word in camelcaps_exceptions:
continue
new_word = camelcaps2underscore(word)
code = code.replace(word, new_word)
r = (word, new_word)
if r in replacements:
replacements[r] += 1
else:
replacements[r] = 1
print " Replacing: %s --> %s" % r
return code
def check_trailing_whitespace(code, replacements):
"Remove trailing spaces at end of lines."
lines = []
for line in code.split("\n"):
new_line = line.rstrip()
lines.append(new_line)
if new_line == line:
continue
r = ("trailing whitespace", "")
if r in replacements:
replacements[r] += 1
else:
replacements[r] = 1
print " Removing trailing whitespace"
new_code = "\n".join(lines)
if len(new_code) > 0 and not new_code[-1] == "\n":
new_code += "\n"
return new_code
def replace(filename, replacements):
"Make replacements for given filename."
print "Checking %s..." % filename
# Open file
f = open(filename, "r")
code = f.read()
f.close()
# Checks
new_code = check_camelcaps(code, replacements)
new_code = check_trailing_whitespace(code, replacements)
# Write file
f = open(filename, "w")
f.write(new_code)
f.close()
# Iterate over all source files
replacements = {}
for root, dirs, files in os.walk("dolfin"):
for file in files:
if not file.endswith((".cpp", ".h", ".py", ".i")):
continue
filename = os.path.join(root, file)
replace(filename, replacements)
for root, dirs, files in os.walk("demo"):
for file in files:
if not file.endswith((".cpp", ".h", ".py")):
continue
filename = os.path.join(root, file)
replace(filename, replacements)
# Report replacements
if len(replacements) == 0:
print "No replacements were made, unchanged."
else:
print ""
print "The following replacements were made:"
lines = []
for r in replacements:
lines.append(" %s --> %s (%d occurences)" % (r[0], r[1], replacements[r]))
lines.sort()
print "\n".join(lines)
|