From 898b7eb6f0802fb766e3f06ea6486e53f7d9a3fe Mon Sep 17 00:00:00 2001 From: Tom Swirly Date: Sun, 13 Apr 2014 20:59:27 -0400 Subject: [PATCH] Add CleanCCPFiles.py, a utility for tidying up whitespace --- src/beast/python/beast/util/CleanCPPFiles.py | 77 ++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/beast/python/beast/util/CleanCPPFiles.py diff --git a/src/beast/python/beast/util/CleanCPPFiles.py b/src/beast/python/beast/util/CleanCPPFiles.py new file mode 100644 index 000000000..de11b0234 --- /dev/null +++ b/src/beast/python/beast/util/CleanCPPFiles.py @@ -0,0 +1,77 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + +#------------------------------------------------------------------------------ +# +# This file is part of Beast: https://github.com/vinniefalco/Beast +# Copyright 2014, Tom Ritchford +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# +#------------------------------------------------------------------------------ + +""" +Cleans C++ files according to a consistent standard. + +Trims trailing whitespace; converts files encoded in Latin-1 to UTF-8. + +""" + +import os +import sys +import tempfile + +def process_file(filename, process): + with open(filename) as infile: + lines = infile.read().strip().splitlines() + outfd, outname = tempfile.mkstemp(dir=os.path.dirname(filename)) + with os.fdopen(outfd, 'w') as outfile: + for number, line in enumerate(lines): + try: + result = process(line, filename, number).encode('utf-8') + outfile.write(result) + except Exception as e: + if True: raise + raise Exception('%s on line %d in file %s' % + (e.message, number + 1, filename)) + os.rename(outname, filename) + +def walk_and_process(root, process, condition, message=None): + for root, dirs, files in os.walk(root): + for f in files: + filename = os.path.join(root, f) + if condition(filename): + if message: + print(message % filename) + process_file(filename, process) + +def clean_line_endings(root, endings=None, message=None): + if endings == None: + endings = '.h', '.cpp', '.sh' + def condition(filename): + return os.path.splitext(filename)[1] in endings + + def process(line, filename, number): + try: + return line.decode('utf-8').rstrip() + '\n' + except: + result = line.decode('latin-1').rstrip() + '\n' + print('Found a non UTF-8 line at %s:%d' % (filename, number + 1)) + return result + + walk_and_process(root, process, condition, message) + +if __name__ == "__main__": + path = '.' + if len(sys.argv) > 1: + path = sys.argv[1] + clean_line_endings(os.path.abspath(path)) #, message='processing %s')