--- a/check-style.py Fri Jan 08 12:48:18 2010 +0100
+++ b/check-style.py Fri Jan 08 13:09:42 2010 +0100
@@ -283,85 +283,94 @@
return patchset
-def indent(files, debug, level):
- for f in files:
- copy = tempfile.mkstemp()[1]
- # apply uncrustify
- cfg = uncrustify_config_file(level)
- if debug:
- sys.stderr.write('original file=' + f + '\n')
- sys.stderr.write('uncrustify config file=' + cfg + '\n')
- sys.stderr.write('temporary file=' + copy + '\n')
- try:
- uncrust = subprocess.Popen(['uncrustify', '-c', cfg, '-f', f, '-o', copy],
- stdin = subprocess.PIPE,
- stdout = subprocess.PIPE,
- stderr = subprocess.PIPE)
- (out, err) = uncrust.communicate('')
- if debug:
- sys.stderr.write(out)
- sys.stderr.write(err)
- except OSError:
- raise Exception ('uncrustify not installed')
- # generate a diff file
- src = open(f, 'r')
- dst = open(copy, 'r')
- diff = difflib.unified_diff(src.readlines(), dst.readlines(),
- fromfile=f, tofile=copy)
- src.close()
- dst.close()
- if debug:
- initial_diff = tempfile.mkstemp()[1]
- sys.stderr.write('initial diff file=' + initial_diff + '\n')
- tmp = open(initial_diff, 'w')
- tmp.writelines(diff)
- tmp.close()
- final_diff = tempfile.mkstemp()[1]
- if level < 3:
- patchset = remove_trailing_whitespace_changes(diff);
- dst = open(final_diff, 'w')
- if len(patchset) != 0:
- patchset[0].write(dst)
- dst.close()
- else:
- dst = open(final_diff, 'w')
- dst.writelines(diff)
- dst.close()
-
-
- # apply diff file
- if debug:
- sys.stderr.write('final diff file=' + final_diff + '\n')
- patch = subprocess.Popen(['patch', '-p1', '-i', final_diff, f],
- stdin = subprocess.PIPE,
- stdout = subprocess.PIPE,
- stderr = subprocess.PIPE)
- (out, err) = patch.communicate('')
+def indent(source, debug, level):
+ output = tempfile.mkstemp()[1]
+ # apply uncrustify
+ cfg = uncrustify_config_file(level)
+ if debug:
+ sys.stderr.write('original file=' + source + '\n')
+ sys.stderr.write('uncrustify config file=' + cfg + '\n')
+ sys.stderr.write('temporary file=' + output + '\n')
+ try:
+ uncrust = subprocess.Popen(['uncrustify', '-c', cfg, '-f', source, '-o', output],
+ stdin = subprocess.PIPE,
+ stdout = subprocess.PIPE,
+ stderr = subprocess.PIPE)
+ (out, err) = uncrust.communicate('')
if debug:
sys.stderr.write(out)
sys.stderr.write(err)
+ except OSError:
+ raise Exception ('uncrustify not installed')
+ # generate a diff file
+ src = open(source, 'r')
+ dst = open(output, 'r')
+ diff = difflib.unified_diff(src.readlines(), dst.readlines(),
+ fromfile=source, tofile=output)
+ src.close()
+ dst.close()
+ if debug:
+ initial_diff = tempfile.mkstemp()[1]
+ sys.stderr.write('initial diff file=' + initial_diff + '\n')
+ tmp = open(initial_diff, 'w')
+ tmp.writelines(diff)
+ tmp.close()
+ final_diff = tempfile.mkstemp()[1]
+ if level < 3:
+ patchset = remove_trailing_whitespace_changes(diff);
+ dst = open(final_diff, 'w')
+ if len(patchset) != 0:
+ patchset[0].write(dst)
+ dst.close()
+ else:
+ dst = open(final_diff, 'w')
+ dst.writelines(diff)
+ dst.close()
+
+
+ # apply diff file
+ if debug:
+ sys.stderr.write('final diff file=' + final_diff + '\n')
+ shutil.copyfile(source,output)
+ patch = subprocess.Popen(['patch', '-p1', '-i', final_diff, output],
+ stdin = subprocess.PIPE,
+ stdout = subprocess.PIPE,
+ stderr = subprocess.PIPE)
+ (out, err) = patch.communicate('')
+ if debug:
+ sys.stderr.write(out)
+ sys.stderr.write(err)
+ return output
-def check_indent(tmp, diff=False, debug=False, level=0):
- to_indent = [indented for f,org,indented in tmp]
- indent (to_indent, debug, level)
+def indent_files(files, diff=False, debug=False, level=0, inplace=False):
+ output = []
+ for f in files:
+ dst = indent(f, debug=debug, level=level)
+ output.append([f,dst])
+
+ # First, copy to inplace
+ if inplace:
+ for src,dst in output:
+ shutil.copyfile(dst,src)
+ return True
+
+ # now, compare
failed = []
- for f,org,indented in tmp:
- if filecmp.cmp(org,indented) == 0:
- failed.append(f)
+ for src,dst in output:
+ if filecmp.cmp(src,dst) == 0:
+ failed.append([src, dst])
if len(failed) > 0:
if not diff:
print 'Found %u badly indented files:' % len(failed)
- for f in failed:
- print ' ' + f
+ for src,dst in failed:
+ print ' ' + src
else:
- for f,org,indented in tmp:
- if not f in failed:
- continue
- src = open(org, 'r').readlines()
- dst = open(indented, 'r').readlines()
- for line in difflib.unified_diff(src, dst, fromfile=f, tofile=indented):
+ for src,dst in failed:
+ s = open(src, 'r').readlines()
+ d = open(dst, 'r').readlines()
+ for line in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
sys.stdout.write(line)
return False
return True
@@ -372,8 +381,7 @@
lock.LockError = error.LockError
# actually do the work
files = hg_modified_files()
- tmp = [(file,copy_file(file),copy_file(file)) for file in files]
- if not check_indent(tmp):
+ if not indent_files(files, inplace=False):
return True
return False
@@ -400,38 +408,31 @@
debug = options.debug
if options.hg_hook:
files = hg_modified_files()
- tmp = [(file,copy_file(file),copy_file(file)) for file in files]
- if not check_indent(tmp, debug=options.debug,
- level=options.level):
+ if not indent_files(files, debug=options.debug,
+ level=options.level,
+ inplace=False):
sys.exit(1)
elif options.hg:
files = hg_modified_files()
- if not options.modify_in_place:
- tmp = [(file,file,copy_file(file)) for file in files]
- else:
- tmp = [(file,file,file) for file in files]
- check_indent(tmp, diff=options.diff,
+ indent_files(files, diff=options.diff,
debug=options.debug,
- level=options.level)
+ level=options.level,
+ inplace=options.modify_in_place)
elif options.file != '':
file = options.file
if not os.path.exists(file) or \
not os.path.isfile(file):
print 'file %s does not exist' % file
sys.exit(1)
- if not options.modify_in_place:
- copy = copy_file(file)
- else:
- copy = file
- tmp = (file,file,copy)
- check_indent([tmp], diff=options.diff,
+ indent_files([file], diff=options.diff,
debug=options.debug,
- level=options.level)
+ level=options.level,
+ inplace=options.modify_in_place)
sys.exit(0)
if __name__ == '__main__':
- try:
+# try:
run_as_main()
- except Exception, e:
- sys.stderr.write(str(e) + '\n')
- sys.exit(1)
+# except Exception, e:
+# sys.stderr.write(str(e) + '\n')
+# sys.exit(1)