2012-05-27 07:22:27 +00:00
|
|
|
import sys
|
|
|
|
import os, os.path, shutil
|
|
|
|
import fnmatch
|
|
|
|
import re
|
|
|
|
|
|
|
|
def main():
|
|
|
|
sys.stdout.flush()
|
2012-05-27 08:24:45 +00:00
|
|
|
for x in range(1, len(sys.argv)):
|
2012-05-27 07:22:27 +00:00
|
|
|
cleanup_source(sys.argv[x])
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
def cleanup_source(path):
|
|
|
|
path = os.path.normpath(path)
|
2012-05-27 08:24:45 +00:00
|
|
|
regex_cases_before = re.compile(r'((case|default).+\r?\n)\r?\n', re.MULTILINE) #Fixes newline after case before case body
|
|
|
|
regex_cases_after = re.compile(r'\r?\n(\r?\n[ \t]+(case|default))', re.MULTILINE) #Fixes newline after case body before new case
|
2012-05-27 07:22:27 +00:00
|
|
|
|
|
|
|
def updatefile(src_file):
|
|
|
|
global count
|
|
|
|
tmp_file = src_file + '.tmp'
|
|
|
|
count = 0
|
|
|
|
with open(src_file, 'r') as fh:
|
|
|
|
buf = fh.read()
|
|
|
|
|
|
|
|
def fix_cases(match):
|
|
|
|
global count
|
|
|
|
count += 1
|
|
|
|
return match.group(1)
|
|
|
|
|
2012-05-27 08:40:22 +00:00
|
|
|
buf = regex_cases_before.sub(fix_cases, buf)
|
|
|
|
buf = regex_cases_after.sub(fix_cases, buf)
|
2012-05-27 07:22:27 +00:00
|
|
|
if count > 0:
|
|
|
|
with open(tmp_file, 'w') as fh:
|
|
|
|
fh.write(buf)
|
|
|
|
shutil.move(tmp_file, src_file)
|
|
|
|
|
|
|
|
for path, _, filelist in os.walk(path, followlinks=True):
|
|
|
|
sub_dir = os.path.relpath(path, path)
|
|
|
|
for cur_file in fnmatch.filter(filelist, '*.java'):
|
|
|
|
src_file = os.path.normpath(os.path.join(path, cur_file))
|
|
|
|
updatefile(src_file)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|