14 files = os.popen (
'hg st -nma')
15 return [filename.strip()
for filename
in files]
18 [tmp,pathname] = tempfile.mkstemp()
19 src = open(filename,
'r') 20 dst = open(pathname, 'w')
30 nl_collapse_empty_body=False 41 nl_namespace_brace=Remove 42 nl_after_brace_open=True 43 nl_class_leave_one_liners=False 44 nl_enum_leave_one_liners=False 45 nl_func_leave_one_liners=False 46 nl_if_leave_one_liners=False 48 nl_after_access_spec=1 49 nl_after_semicolon=True 53 nl_class_init_args=Add 56 # does not work very well 57 nl_func_type_name=Ignore 58 nl_func_scope_name=Ignore 59 nl_func_type_name_class=Ignore 60 nl_func_proto_type_name=Ignore 68 mod_full_brace_for=Add 70 mod_full_brace_while=Add 71 mod_full_brace_for=Add 72 mod_remove_extra_semicolon=True 75 #ls_for_split_full=True 76 #ls_func_split_full=True 79 # extra spaces here and there 87 sp_func_class_paren=Add 93 sp_func_proto_paren=Add 95 sp_func_call_paren=Add 96 sp_after_semi_for=Ignore 97 sp_before_sparen=Ignore 100 nl_class_leave_one_liners=True 101 nl_enum_leave_one_liners=True 102 nl_func_leave_one_liners=True 103 nl_assign_leave_one_liners=True 104 #nl_collapse_empty_body=False 105 nl_getset_leave_one_liners=True 106 nl_if_leave_one_liners=True 108 # finally, indentation configuration 110 indent_namespace=false 115 indent_class_colon=True 117 indent_align_assign=False 118 align_left_shift=True 119 # comment reformating disabled 120 cmt_reflow_mode=1 # do not touch comments at all 121 cmt_indent_multi=False # really, do not touch them 123 [tmp,pathname] = tempfile.mkstemp()
124 dst = open(pathname,
'w')
150 @param self The current class 157 @param self The current class 158 @param line source line 165 @param self The current class 166 @param line destination line 173 @param self The current class 181 @param self The current class 182 @param s line to append 188 @param self The current class 194 @param self The current class 195 @return true if type is source 200 @param self The current class 201 @return true if type is destination 206 @param self The current class 208 @return exception if invalid type 211 f.write(
'-%s\n' % self.
__line)
213 f.write(
'+%s\n' % self.
__line)
215 f.write(
' %s\n' % self.
__line)
217 raise Exception(
'invalid patch')
233 @param self: this object 234 @param src_pos: source position 235 @param dst_pos: destination position 242 """! Source start function 243 @param self this object 244 @return source position 248 """! Add line function 249 @param self The current class 250 @param line line to add 255 """! Get source lines 256 @param self The current class 257 @return the source lines 265 """! Get destination lines 266 @param self The current class 267 @return the destination lines 275 """! Get number of source lines 276 @param self The current class 277 @return number of source lines 279 return len(self.
src())
281 """! Get number of destinaton lines 282 @param self The current class 283 @return number of destination lines 285 return len(self.
dst())
287 """! Write lines to file 288 @param self The current class 289 @param f: file to write to 307 @param self The current class 315 @param self this object 322 @param self The current class 328 @param self this object 335 @param self this object 336 @param dst destintion 342 @param self The current class 343 @param filename file name 350 @param self The current class 354 f.write(
'--- %s\n' % self.__src )
355 f.write(
'+++ %s\n' % self.__dst )
356 for chunk
in self.__chunks:
360 src_file = re.compile(
'^--- (.*)$')
361 dst_file = re.compile(
'^\+\+\+ (.*)$')
362 chunk_start = re.compile(
'^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@')
363 src = re.compile(
'^-(.*)$')
364 dst = re.compile(
'^\+(.*)$')
365 both = re.compile(
'^ (.*)$')
368 for line
in generator:
369 m = src_file.search(line)
371 current_patch =
Patch()
372 patchset.append(current_patch)
373 current_patch.set_src(m.group(1))
375 m = dst_file.search(line)
377 current_patch.set_dst(m.group(1))
379 m = chunk_start.search(line)
381 current_chunk =
PatchChunk(m.group(1), m.group(3))
382 current_patch.add_chunk(current_chunk)
387 l.set_src(m.group(1))
388 current_chunk.add_line(l)
393 l.set_dst(m.group(1))
394 current_chunk.add_line(l)
396 m = both.search(line)
399 l.set_both(m.group(1))
400 current_chunk.add_line(l)
406 whitespace = re.compile(
'^(.*)([ \t]+)$')
408 for patch
in patchset:
409 for chunk
in patch.chunks():
413 for i
in range(0,len(src)):
416 m = whitespace.search(s.line())
417 if m
is not None and m.group(1) == d.line():
418 d.append_to_line(m.group(2))
425 output = tempfile.mkstemp()[1]
429 sys.stderr.write(
'original file=' + source +
'\n')
430 sys.stderr.write(
'uncrustify config file=' + cfg +
'\n')
431 sys.stderr.write(
'temporary file=' + output +
'\n')
433 uncrust = subprocess.Popen([
'uncrustify',
'-c', cfg,
'-f', source,
'-o', output],
434 stdin = subprocess.PIPE,
435 stdout = subprocess.PIPE,
436 stderr = subprocess.PIPE)
437 (out, err) = uncrust.communicate(
'')
439 sys.stderr.write(out)
440 sys.stderr.write(err)
442 raise Exception (
'uncrustify not installed')
444 src = open(source,
'r') 445 dst = open(output, 'r') 446 diff = difflib.unified_diff(src.readlines(), dst.readlines(), 447 fromfile=source, tofile=output) 451 initial_diff = tempfile.mkstemp()[1]
452 sys.stderr.write(
'initial diff file=' + initial_diff +
'\n')
453 tmp = open(initial_diff,
'w')
456 final_diff = tempfile.mkstemp()[1]
459 dst = open(final_diff,
'w')
460 if len(patchset) != 0:
461 patchset[0].write(dst)
464 dst = open(final_diff,
'w')
471 sys.stderr.write(
'final diff file=' + final_diff +
'\n')
472 shutil.copyfile(source,output)
473 patch = subprocess.Popen([
'patch',
'-p1',
'-i', final_diff, output],
474 stdin = subprocess.PIPE,
475 stdout = subprocess.PIPE,
476 stderr = subprocess.PIPE)
477 (out, err) = patch.communicate(
'')
479 sys.stderr.write(out)
480 sys.stderr.write(err)
485 def indent_files(files, diff=False, debug=False, level=0, inplace=False):
488 dst =
indent(f, debug=debug, level=level)
489 output.append([f,dst])
493 for src,dst
in output:
494 shutil.copyfile(dst,src)
499 for src,dst
in output:
500 if filecmp.cmp(src,dst) == 0:
501 failed.append([src, dst])
504 print(
'Found %u badly indented files:' % len(failed))
505 for src,dst
in failed:
508 for src,dst
in failed:
509 s = open(src,
'r').readlines() 510 d = open(dst, 'r').readlines() 511 for line
in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
512 sys.stdout.write(line)
518 from mercurial
import lock, error
519 lock.LockError = error.LockError
527 parser = optparse.OptionParser()
528 parser.add_option(
'--debug', action=
'store_true', dest=
'debug', default=
False,
529 help=
'Output some debugging information')
530 parser.add_option(
'-l',
'--level', type=
'int', dest=
'level', default=0,
531 help=
"Level of style conformance: higher levels include all lower levels. " 532 "level=0: re-indent only. level=1: add extra spaces. level=2: insert extra newlines and " 533 "extra braces around single-line statements. level=3: remove all trailing spaces")
534 parser.add_option(
'--check-hg-hook', action=
'store_true', dest=
'hg_hook', default=
False,
535 help=
'Get the list of files to check from mercurial\'s list of modified ' 536 'and added files and assume that the script runs as a pretxncommit mercurial hook')
537 parser.add_option(
'--check-hg', action=
'store_true', dest=
'hg', default=
False,
538 help=
"Get the list of files to check from mercurial\'s list of modified and added files")
539 parser.add_option(
'-f',
'--check-file', action=
'store', dest=
'file', default=
'',
540 help=
"Check a single file")
541 parser.add_option(
'--diff', action=
'store_true', dest=
'diff', default=
False,
542 help=
"Generate a diff on stdout of the indented files")
543 parser.add_option(
'-i',
'--in-place', action=
'store_true', dest=
'in_place', default=
False,
544 help=
"Indent the input files in-place")
545 (options,args) = parser.parse_args()
546 debug = options.debug
558 inplace=options.in_place)
559 elif options.file !=
'':
561 if not os.path.exists(file)
or \
562 not os.path.isfile(file):
563 print(
'file %s does not exist' % file)
568 inplace=options.in_place)
571 if __name__ ==
'__main__':
def append_to_line(self, s)
Append to line.
def set_src(self, src)
Set source.
def apply(self, filename)
Apply function.
def set_both(self, line)
Set both.
def chunks(self)
Get the chunks.
def dst(self)
Get destination lines.
def indent(source, debug, level)
def __init__(self, src_pos, dst_pos)
Initializer.
def add_line(self, line)
Add line function.
def set_src(self, line)
Set source.
def src(self)
Get source lines.
def write(self, f)
Write to file.
def run_as_hg_hook(ui, repo, kwargs)
__dst_pos
destination position
def is_src(self)
Is source.
def is_dst(self)
Is destination.
def set_dst(self, dst)
Set destination.
def remove_trailing_whitespace_changes(patch_generator)
def uncrustify_config_file(level)
def dst_len(self)
Get number of destinaton lines.
def __init__(self)
Initializer.
def write(self, f)
Write to file.
def src_start(self)
Source start function.
def set_dst(self, line)
Set destination.
def src_len(self)
Get number of source lines.
def parse_patchset(generator)
def write(self, f)
Write lines to file.
def add_chunk(self, chunk)
Add chunk.
def __init__(self)
Initializer.
def indent_files(files, diff=False, debug=False, level=0, inplace=False)