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