Skip to content

Instantly share code, notes, and snippets.

@soffes
Created February 8, 2009 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soffes/60567 to your computer and use it in GitHub Desktop.
Save soffes/60567 to your computer and use it in GitHub Desktop.
#!/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