Commit e4a3380b authored by Florent Xicluna's avatar Florent Xicluna

Clean up syntax for some scripts.

parent aba74bdd
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
import os import os
import sys import sys
class Stats: class Stats:
def __init__(self): def __init__(self):
...@@ -28,12 +29,11 @@ class Stats: ...@@ -28,12 +29,11 @@ class Stats:
sys.stderr.write("Can't list %s: %s\n" % (dir, err)) sys.stderr.write("Can't list %s: %s\n" % (dir, err))
self.addstats("<dir>", "unlistable", 1) self.addstats("<dir>", "unlistable", 1)
return return
names.sort() for name in sorted(names):
for name in names:
if name.startswith(".#"): if name.startswith(".#"):
continue # Skip CVS temp files continue # Skip CVS temp files
if name.endswith("~"): if name.endswith("~"):
continue# Skip Emacs backup files continue # Skip Emacs backup files
full = os.path.join(dir, name) full = os.path.join(dir, name)
if os.path.islink(full): if os.path.islink(full):
self.addstats("<lnk>", "links", 1) self.addstats("<lnk>", "links", 1)
...@@ -52,20 +52,19 @@ class Stats: ...@@ -52,20 +52,19 @@ class Stats:
ext = "<none>" ext = "<none>"
self.addstats(ext, "files", 1) self.addstats(ext, "files", 1)
try: try:
f = open(filename, "rb") with open(filename, "rb") as f:
data = f.read()
except IOError as err: except IOError as err:
sys.stderr.write("Can't open %s: %s\n" % (filename, err)) sys.stderr.write("Can't open %s: %s\n" % (filename, err))
self.addstats(ext, "unopenable", 1) self.addstats(ext, "unopenable", 1)
return return
data = f.read()
f.close()
self.addstats(ext, "bytes", len(data)) self.addstats(ext, "bytes", len(data))
if b'\0' in data: if b'\0' in data:
self.addstats(ext, "binary", 1) self.addstats(ext, "binary", 1)
return return
if not data: if not data:
self.addstats(ext, "empty", 1) self.addstats(ext, "empty", 1)
#self.addstats(ext, "chars", len(data)) # self.addstats(ext, "chars", len(data))
lines = str(data, "latin-1").splitlines() lines = str(data, "latin-1").splitlines()
self.addstats(ext, "lines", len(lines)) self.addstats(ext, "lines", len(lines))
del lines del lines
...@@ -105,10 +104,12 @@ class Stats: ...@@ -105,10 +104,12 @@ class Stats:
for ext in exts: for ext in exts:
self.stats[ext]["ext"] = ext self.stats[ext]["ext"] = ext
cols.insert(0, "ext") cols.insert(0, "ext")
def printheader(): def printheader():
for col in cols: for col in cols:
print("%*s" % (colwidth[col], col), end=' ') print("%*s" % (colwidth[col], col), end=' ')
print() print()
printheader() printheader()
for ext in exts: for ext in exts:
for col in cols: for col in cols:
...@@ -117,6 +118,7 @@ class Stats: ...@@ -117,6 +118,7 @@ class Stats:
print() print()
printheader() # Another header at the bottom printheader() # Another header at the bottom
def main(): def main():
args = sys.argv[1:] args = sys.argv[1:]
if not args: if not args:
...@@ -125,5 +127,6 @@ def main(): ...@@ -125,5 +127,6 @@ def main():
s.statargs(args) s.statargs(args)
s.report() s.report()
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -9,6 +9,7 @@ def n_files_str(count): ...@@ -9,6 +9,7 @@ def n_files_str(count):
"""Return 'N file(s)' with the proper plurality on 'file'.""" """Return 'N file(s)' with the proper plurality on 'file'."""
return "{} file{}".format(count, "s" if count != 1 else "") return "{} file{}".format(count, "s" if count != 1 else "")
def status(message, modal=False, info=None): def status(message, modal=False, info=None):
"""Decorator to output status info to stdout.""" """Decorator to output status info to stdout."""
def decorated_fxn(fxn): def decorated_fxn(fxn):
...@@ -21,32 +22,25 @@ def status(message, modal=False, info=None): ...@@ -21,32 +22,25 @@ def status(message, modal=False, info=None):
elif info: elif info:
print(info(result)) print(info(result))
else: else:
if result: print("yes" if result else "NO")
print("yes")
else:
print("NO")
return result return result
return call_fxn return call_fxn
return decorated_fxn return decorated_fxn
@status("Getting the list of files that have been added/changed", @status("Getting the list of files that have been added/changed",
info=lambda x: n_files_str(len(x))) info=lambda x: n_files_str(len(x)))
def changed_files(): def changed_files():
"""Run ``svn status`` and return a set of files that have been """Run ``svn status`` and return a set of files that have been
changed/added.""" changed/added.
"""
cmd = 'svn status --quiet --non-interactive --ignore-externals' cmd = 'svn status --quiet --non-interactive --ignore-externals'
svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) svn_st = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
svn_st.wait() svn_st.wait()
output = [x.decode().rstrip() for x in svn_st.stdout.readlines()] output = (x.decode().rstrip().rsplit(None, 1)[-1]
files = set() for x in svn_st.stdout if x[0] in b'AM')
for line in output: return set(path for path in output if os.path.isfile(path))
if not line[0] in ('A', 'M'):
continue
line_parts = line.split()
path = line_parts[-1]
if os.path.isfile(path):
files.add(path)
return files
def report_modified_files(file_paths): def report_modified_files(file_paths):
count = len(file_paths) count = len(file_paths)
...@@ -58,6 +52,7 @@ def report_modified_files(file_paths): ...@@ -58,6 +52,7 @@ def report_modified_files(file_paths):
lines.append(" {}".format(path)) lines.append(" {}".format(path))
return "\n".join(lines) return "\n".join(lines)
@status("Fixing whitespace", info=report_modified_files) @status("Fixing whitespace", info=report_modified_files)
def normalize_whitespace(file_paths): def normalize_whitespace(file_paths):
"""Make sure that the whitespace for .py files have been normalized.""" """Make sure that the whitespace for .py files have been normalized."""
...@@ -68,16 +63,19 @@ def normalize_whitespace(file_paths): ...@@ -68,16 +63,19 @@ def normalize_whitespace(file_paths):
fixed.append(path) fixed.append(path)
return fixed return fixed
@status("Docs modified", modal=True) @status("Docs modified", modal=True)
def docs_modified(file_paths): def docs_modified(file_paths):
"""Report if any file in the Doc directory has been changed.""" """Report if any file in the Doc directory has been changed."""
return bool(file_paths) return bool(file_paths)
@status("Misc/ACKS updated", modal=True) @status("Misc/ACKS updated", modal=True)
def credit_given(file_paths): def credit_given(file_paths):
"""Check if Misc/ACKS has been changed.""" """Check if Misc/ACKS has been changed."""
return 'Misc/ACKS' in file_paths return 'Misc/ACKS' in file_paths
@status("Misc/NEWS updated", modal=True) @status("Misc/NEWS updated", modal=True)
def reported_news(file_paths): def reported_news(file_paths):
"""Check if Misc/NEWS has been changed.""" """Check if Misc/NEWS has been changed."""
......
...@@ -42,26 +42,27 @@ you'd prefer. You can always use the --nobackup option to prevent this. ...@@ -42,26 +42,27 @@ you'd prefer. You can always use the --nobackup option to prevent this.
__version__ = "1" __version__ = "1"
import tokenize import tokenize
import os, shutil import os
import shutil
import sys import sys
verbose = 0 verbose = False
recurse = 0 recurse = False
dryrun = 0 dryrun = False
makebackup = True makebackup = True
def usage(msg=None): def usage(msg=None):
if msg is not None: if msg is None:
msg = __doc__
print(msg, file=sys.stderr) print(msg, file=sys.stderr)
print(__doc__, file=sys.stderr)
def errprint(*args): def errprint(*args):
sep = "" sys.stderr.write(" ".join(str(arg) for arg in args))
for arg in args:
sys.stderr.write(sep + str(arg))
sep = " "
sys.stderr.write("\n") sys.stderr.write("\n")
def main(): def main():
import getopt import getopt
global verbose, recurse, dryrun, makebackup global verbose, recurse, dryrun, makebackup
...@@ -73,13 +74,13 @@ def main(): ...@@ -73,13 +74,13 @@ def main():
return return
for o, a in opts: for o, a in opts:
if o in ('-d', '--dryrun'): if o in ('-d', '--dryrun'):
dryrun += 1 dryrun = True
elif o in ('-r', '--recurse'): elif o in ('-r', '--recurse'):
recurse += 1 recurse = True
elif o in ('-n', '--nobackup'): elif o in ('-n', '--nobackup'):
makebackup = False makebackup = False
elif o in ('-v', '--verbose'): elif o in ('-v', '--verbose'):
verbose += 1 verbose = True
elif o in ('-h', '--help'): elif o in ('-h', '--help'):
usage() usage()
return return
...@@ -91,6 +92,7 @@ def main(): ...@@ -91,6 +92,7 @@ def main():
for arg in args: for arg in args:
check(arg) check(arg)
def check(file): def check(file):
if os.path.isdir(file) and not os.path.islink(file): if os.path.isdir(file) and not os.path.islink(file):
if verbose: if verbose:
...@@ -108,13 +110,12 @@ def check(file): ...@@ -108,13 +110,12 @@ def check(file):
if verbose: if verbose:
print("checking", file, "...", end=' ') print("checking", file, "...", end=' ')
try: try:
f = open(file) with open(file) as f:
r = Reindenter(f)
except IOError as msg: except IOError as msg:
errprint("%s: I/O Error: %s" % (file, str(msg))) errprint("%s: I/O Error: %s" % (file, str(msg)))
return return
r = Reindenter(f)
f.close()
if r.run(): if r.run():
if verbose: if verbose:
print("changed.") print("changed.")
...@@ -126,9 +127,8 @@ def check(file): ...@@ -126,9 +127,8 @@ def check(file):
shutil.copyfile(file, bak) shutil.copyfile(file, bak)
if verbose: if verbose:
print("backed up", file, "to", bak) print("backed up", file, "to", bak)
f = open(file, "w") with open(file, "w") as f:
r.write(f) r.write(f)
f.close()
if verbose: if verbose:
print("wrote new", file) print("wrote new", file)
return True return True
...@@ -137,6 +137,7 @@ def check(file): ...@@ -137,6 +137,7 @@ def check(file):
print("unchanged.") print("unchanged.")
return False return False
def _rstrip(line, JUNK='\n \t'): def _rstrip(line, JUNK='\n \t'):
"""Return line stripped of trailing spaces, tabs, newlines. """Return line stripped of trailing spaces, tabs, newlines.
...@@ -146,10 +147,11 @@ def _rstrip(line, JUNK='\n \t'): ...@@ -146,10 +147,11 @@ def _rstrip(line, JUNK='\n \t'):
""" """
i = len(line) i = len(line)
while i > 0 and line[i-1] in JUNK: while i > 0 and line[i - 1] in JUNK:
i -= 1 i -= 1
return line[:i] return line[:i]
class Reindenter: class Reindenter:
def __init__(self, f): def __init__(self, f):
...@@ -192,9 +194,9 @@ class Reindenter: ...@@ -192,9 +194,9 @@ class Reindenter:
# we see a line with *something* on it. # we see a line with *something* on it.
i = stats[0][0] i = stats[0][0]
after.extend(lines[1:i]) after.extend(lines[1:i])
for i in range(len(stats)-1): for i in range(len(stats) - 1):
thisstmt, thislevel = stats[i] thisstmt, thislevel = stats[i]
nextstmt = stats[i+1][0] nextstmt = stats[i + 1][0]
have = getlspace(lines[thisstmt]) have = getlspace(lines[thisstmt])
want = thislevel * 4 want = thislevel * 4
if want < 0: if want < 0:
...@@ -206,7 +208,7 @@ class Reindenter: ...@@ -206,7 +208,7 @@ class Reindenter:
want = have2want.get(have, -1) want = have2want.get(have, -1)
if want < 0: if want < 0:
# Then it probably belongs to the next real stmt. # Then it probably belongs to the next real stmt.
for j in range(i+1, len(stats)-1): for j in range(i + 1, len(stats) - 1):
jline, jlevel = stats[j] jline, jlevel = stats[j]
if jlevel >= 0: if jlevel >= 0:
if have == getlspace(lines[jline]): if have == getlspace(lines[jline]):
...@@ -216,11 +218,11 @@ class Reindenter: ...@@ -216,11 +218,11 @@ class Reindenter:
# comment like this one, # comment like this one,
# in which case we should shift it like its base # in which case we should shift it like its base
# line got shifted. # line got shifted.
for j in range(i-1, -1, -1): for j in range(i - 1, -1, -1):
jline, jlevel = stats[j] jline, jlevel = stats[j]
if jlevel >= 0: if jlevel >= 0:
want = have + getlspace(after[jline-1]) - \ want = have + (getlspace(after[jline - 1]) -
getlspace(lines[jline]) getlspace(lines[jline]))
break break
if want < 0: if want < 0:
# Still no luck -- leave it alone. # Still no luck -- leave it alone.
...@@ -295,6 +297,7 @@ class Reindenter: ...@@ -295,6 +297,7 @@ class Reindenter:
if line: # not endmarker if line: # not endmarker
self.stats.append((slinecol[0], self.level)) self.stats.append((slinecol[0], self.level))
# Count number of leading blanks. # Count number of leading blanks.
def getlspace(line): def getlspace(line):
i, n = 0, len(line) i, n = 0, len(line)
...@@ -302,5 +305,6 @@ def getlspace(line): ...@@ -302,5 +305,6 @@ def getlspace(line):
i += 1 i += 1
return i return i
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -9,8 +9,9 @@ import sys ...@@ -9,8 +9,9 @@ import sys
import re import re
import getopt import getopt
def main(): def main():
bufsize = 64*1024 bufsize = 64 * 1024
reflags = 0 reflags = 0
opts, args = getopt.getopt(sys.argv[1:], "i") opts, args = getopt.getopt(sys.argv[1:], "i")
for o, a in opts: for o, a in opts:
...@@ -24,11 +25,11 @@ def main(): ...@@ -24,11 +25,11 @@ def main():
try: try:
prog = re.compile(pattern, reflags) prog = re.compile(pattern, reflags)
except re.error as msg: except re.error as msg:
usage("error in regular expression: %s" % str(msg)) usage("error in regular expression: %s" % msg)
try: try:
f = open(filename) f = open(filename)
except IOError as msg: except IOError as msg:
usage("can't open %s: %s" % (repr(filename), str(msg)), 1) usage("can't open %r: %s" % (filename, msg), 1)
f.seek(0, 2) f.seek(0, 2)
pos = f.tell() pos = f.tell()
leftover = None leftover = None
...@@ -49,16 +50,17 @@ def main(): ...@@ -49,16 +50,17 @@ def main():
del lines[0] del lines[0]
else: else:
leftover = None leftover = None
lines.reverse() for line in reversed(lines):
for line in lines:
if prog.search(line): if prog.search(line):
print(line) print(line)
def usage(msg, code=2): def usage(msg, code=2):
sys.stdout = sys.stderr sys.stdout = sys.stderr
print(msg) print(msg)
print(__doc__) print(__doc__)
sys.exit(code) sys.exit(code)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -6,24 +6,21 @@ ...@@ -6,24 +6,21 @@
import sys import sys
def main(): def main():
files = sys.argv[1:] files = sys.argv[1:]
suffixes = {} suffixes = {}
for filename in files: for filename in files:
suff = getsuffix(filename) suff = getsuffix(filename)
if suff not in suffixes: suffixes.setdefault(suff, []).append(filename)
suffixes[suff] = [] for suff, filenames in sorted(suffixes.items()):
suffixes[suff].append(filename) print(repr(suff), len(filenames))
keys = sorted(suffixes.keys())
for suff in keys:
print(repr(suff), len(suffixes[suff]))
def getsuffix(filename): def getsuffix(filename):
suff = '' name, sep, suff = filename.rpartition('.')
for i in range(len(filename)): return sep + suff if sep else ''
if filename[i] == '.':
suff = filename[i:]
return suff
if __name__ == '__main__': if __name__ == '__main__':
main() main()
...@@ -32,9 +32,12 @@ and for a file with a binary mime-type property: ...@@ -32,9 +32,12 @@ and for a file with a binary mime-type property:
import re import re
import os import os
import sys
import subprocess
def propfiles(root, fn): def propfiles(root, fn):
default = os.path.join(root, ".svn", "props", fn+".svn-work") default = os.path.join(root, ".svn", "props", fn + ".svn-work")
try: try:
format = int(open(os.path.join(root, ".svn", "format")).read().strip()) format = int(open(os.path.join(root, ".svn", "format")).read().strip())
except IOError: except IOError:
...@@ -42,12 +45,13 @@ def propfiles(root, fn): ...@@ -42,12 +45,13 @@ def propfiles(root, fn):
if format in (8, 9): if format in (8, 9):
# In version 8 and 9, committed props are stored in prop-base, local # In version 8 and 9, committed props are stored in prop-base, local
# modifications in props # modifications in props
return [os.path.join(root, ".svn", "prop-base", fn+".svn-base"), return [os.path.join(root, ".svn", "prop-base", fn + ".svn-base"),
os.path.join(root, ".svn", "props", fn+".svn-work")] os.path.join(root, ".svn", "props", fn + ".svn-work")]
raise ValueError, "Unknown repository format" raise ValueError("Unknown repository format")
def proplist(root, fn): def proplist(root, fn):
"Return a list of property names for file fn in directory root" """Return a list of property names for file fn in directory root."""
result = [] result = []
for path in propfiles(root, fn): for path in propfiles(root, fn):
try: try:
...@@ -56,7 +60,7 @@ def proplist(root, fn): ...@@ -56,7 +60,7 @@ def proplist(root, fn):
# no properties file: not under version control, # no properties file: not under version control,
# or no properties set # or no properties set
continue continue
while 1: while True:
# key-value pairs, of the form # key-value pairs, of the form
# K <length> # K <length>
# <keyname>NL # <keyname>NL
...@@ -79,13 +83,32 @@ def proplist(root, fn): ...@@ -79,13 +83,32 @@ def proplist(root, fn):
f.close() f.close()
return result return result
def set_eol_native(path):
cmd = 'svn propset svn:eol-style native "{}"'.format(path)
propset = subprocess.Popen(cmd, shell=True)
propset.wait()
possible_text_file = re.compile(r"\.([hc]|py|txt|sln|vcproj)$").search possible_text_file = re.compile(r"\.([hc]|py|txt|sln|vcproj)$").search
for root, dirs, files in os.walk('.'):
def main():
for arg in sys.argv[1:] or [os.curdir]:
if os.path.isfile(arg):
root, fn = os.path.split(arg)
if 'svn:eol-style' not in proplist(root, fn):
set_eol_native(arg)
elif os.path.isdir(arg):
for root, dirs, files in os.walk(arg):
if '.svn' in dirs: if '.svn' in dirs:
dirs.remove('.svn') dirs.remove('.svn')
for fn in files: for fn in files:
if possible_text_file(fn): if possible_text_file(fn):
if 'svn:eol-style' not in proplist(root, fn): if 'svn:eol-style' not in proplist(root, fn):
path = os.path.join(root, fn) path = os.path.join(root, fn)
os.system('svn propset svn:eol-style native "%s"' % path) set_eol_native(path)
if __name__ == '__main__':
main()
...@@ -6,6 +6,7 @@ import os ...@@ -6,6 +6,7 @@ import os
import sys import sys
import getopt import getopt
def main(): def main():
tabsize = 8 tabsize = 8
try: try:
...@@ -23,11 +24,11 @@ def main(): ...@@ -23,11 +24,11 @@ def main():
for filename in args: for filename in args:
process(filename, tabsize) process(filename, tabsize)
def process(filename, tabsize): def process(filename, tabsize):
try: try:
f = open(filename) with open(filename) as f:
text = f.read() text = f.read()
f.close()
except IOError as msg: except IOError as msg:
print("%r: I/O error: %s" % (filename, msg)) print("%r: I/O error: %s" % (filename, msg))
return return
...@@ -43,10 +44,10 @@ def process(filename, tabsize): ...@@ -43,10 +44,10 @@ def process(filename, tabsize):
os.rename(filename, backup) os.rename(filename, backup)
except os.error: except os.error:
pass pass
f = open(filename, "w") with open(filename, "w") as f:
f.write(newtext) f.write(newtext)
f.close()
print(filename) print(filename)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment