Created
February 8, 2009 23:40
-
-
Save soffes/60567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/python2.6 | |
# This script fixes multiple spaces (that Git has a problem with) quickly. | |
# I know this sucks. It is a quick solution. | |
# Please fork this, fix it, and email me at sam@samsoff.es | |
import string | |
import glob | |
import sys | |
import os | |
import re | |
import subprocess | |
arg=sys.argv[1] | |
exts=[".h", ".m", ".php", ".css", ".html", ".js", ".java", ".xml", ".json", ".py", ".rrc", ".rrh", ".jad", ".jdp", ".rapc", ".cso", ".csl", ".alx", "LICENSE", "README"] | |
def go(): | |
if os.path.isfile(arg): | |
fix(arg) | |
elif os.path.isdir(arg): | |
os.path.walk(arg, visit, None) | |
else: | |
exit('arg is not file or folder') | |
def visit(arg, dirname, names): | |
for fname in names: | |
fname = os.path.join(dirname, fname) | |
if not os.path.isfile(fname): | |
continue | |
#inefficient | |
for ext in exts: | |
if fname.endswith(ext): | |
fix(fname) | |
break | |
def fix(fname): | |
print fname | |
f=open(fname, "r") | |
contents="" | |
for line in f.readlines(): | |
line=re.sub("\n\s?\n","\n\n",line) | |
line=re.sub("\s+$", endspace,line) | |
#line=re.sub("(\s+)\t+","$1", line) # doesn't work | |
contents+=line+"\n" | |
f.close() | |
f=open(fname, "w") | |
f.write(contents) | |
f.close() | |
# Fix line endings since Python jacks them up | |
subprocess.call('tr "\r" "\n" < %s > %s.tmp;tr "\r\n" "\n" < %s > %s.tmp;mv %s.tmp %s;echo %s' % (fname, fname, fname, fname, fname, fname, fname), shell=True) | |
def endspace(str): | |
return "" | |
go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment