14 files = os.popen(
'git diff --name-only')
15 process = subprocess.Popen([
"git",
"rev-parse",
"--show-toplevel"],
16 stdout = subprocess.PIPE,
17 stderr = subprocess.PIPE)
18 root_dir, _ = process.communicate()
19 if isinstance(root_dir, bytes):
20 root_dir=root_dir.decode(
"utf-8")
21 files_changed = [item.strip()
for item
in files.readlines()]
22 files_changed = [item
for item
in files_changed
if item.endswith(
'.h')
or item.endswith(
'.cc')]
23 return [root_dir[: -1] +
"/" + filename.strip ()
for filename
in files_changed]
26 _, pathname = tempfile.mkstemp()
27 with open(filename,
'r')
as src, open(pathname,
'w')
as dst:
45 nl_namespace_brace=ignore
46 nl_after_brace_open=True
47 nl_class_leave_one_liners=False
48 nl_enum_leave_one_liners=False
49 nl_func_leave_one_liners=False
50 nl_if_leave_one_liners=False
52 nl_before_access_spec=2
53 nl_after_access_spec=0
54 indent_access_spec=-indent_columns
55 nl_after_semicolon=True
58 indent_constr_colon=true
60 nl_class_init_args=Add
63 # does not work very well
64 nl_func_type_name=Ignore
65 nl_func_scope_name=Ignore
66 nl_func_type_name_class=Ignore
67 nl_func_proto_type_name=Ignore
75 mod_full_brace_for=Add
77 mod_full_brace_while=Add
78 mod_full_brace_for=Add
79 mod_remove_extra_semicolon=True
82 #ls_for_split_full=True
83 #ls_func_split_full=True
84 nl_cpp_lambda_leave_one_liners=True
87 # extra spaces here and there
95 sp_func_class_paren=Add
101 sp_func_proto_paren=Add
102 sp_func_def_paren=Add
103 sp_func_call_paren=Add
104 sp_after_semi_for=Ignore
105 sp_before_sparen=Ignore
106 sp_before_ellipsis=Remove
109 nl_class_leave_one_liners=True
110 nl_enum_leave_one_liners=True
111 nl_func_leave_one_liners=True
112 nl_assign_leave_one_liners=True
113 nl_collapse_empty_body=True
114 nl_getset_leave_one_liners=True
115 nl_if_leave_one_liners=True
117 # finally, indentation configuration
119 indent_namespace=false
122 indent_case_brace=indent_columns
124 indent_class_colon=True
125 indent_switch_case=indent_columns
127 indent_align_assign=False
128 align_left_shift=True
129 # comment reformating disabled
130 cmt_reflow_mode=1 # do not touch comments at all
131 cmt_indent_multi=False # really, do not touch them
132 disable_processing_cmt= " *NS_CHECK_STYLE_OFF*"
133 enable_processing_cmt= " *NS_CHECK_STYLE_ON*"
135 _, pathname = tempfile.mkstemp()
136 with open(pathname,
'w')
as dst:
161 @param self The current class
168 @param self The current class
169 @param line source line
176 @param self The current class
177 @param line destination line
184 @param self The current class
192 @param self The current class
193 @param s line to append
199 @param self The current class
205 @param self The current class
206 @return true if type is source
211 @param self The current class
212 @return true if type is destination
217 @param self The current class
219 @return exception if invalid type
222 f.write(
'-%s\n' % self.
__line)
224 f.write(
'+%s\n' % self.
__line)
226 f.write(
' %s\n' % self.
__line)
228 raise Exception(
'invalid patch')
244 @param self: this object
245 @param src_pos: source position
246 @param dst_pos: destination position
253 """! Source start function
254 @param self this object
255 @return source position
259 """! Add line function
260 @param self The current class
261 @param line line to add
266 """! Get source lines
267 @param self The current class
268 @return the source lines
276 """! Get destination lines
277 @param self The current class
278 @return the destination lines
286 """! Get number of source lines
287 @param self The current class
288 @return number of source lines
290 return len(self.
src())
292 """! Get number of destinaton lines
293 @param self The current class
294 @return number of destination lines
296 return len(self.
dst())
298 """! Write lines to file
299 @param self The current class
300 @param f: file to write to
318 @param self The current class
326 @param self this object
333 @param self The current class
339 @param self this object
346 @param self this object
347 @param dst destintion
353 @param self The current class
354 @param filename file name
361 @param self The current class
365 f.write(
'--- %s\n' % self.__src )
366 f.write(
'+++ %s\n' % self.__dst )
367 for chunk
in self.__chunks:
371 src_file = re.compile(
'^--- (.*)$')
372 dst_file = re.compile(
'^\+\+\+ (.*)$')
373 chunk_start = re.compile(
'^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@')
374 src = re.compile(
'^-(.*)$')
375 dst = re.compile(
'^\+(.*)$')
376 both = re.compile(
'^ (.*)$')
379 for line
in generator:
380 m = src_file.search(line)
382 current_patch =
Patch()
383 patchset.append(current_patch)
384 current_patch.set_src(m.group(1))
386 m = dst_file.search(line)
388 current_patch.set_dst(m.group(1))
390 m = chunk_start.search(line)
392 current_chunk =
PatchChunk(m.group(1), m.group(3))
393 current_patch.add_chunk(current_chunk)
398 l.set_src(m.group(1))
399 current_chunk.add_line(l)
404 l.set_dst(m.group(1))
405 current_chunk.add_line(l)
407 m = both.search(line)
410 l.set_both(m.group(1))
411 current_chunk.add_line(l)
417 whitespace = re.compile(
'^(.*)([ \t]+)$')
419 for patch
in patchset:
420 for chunk
in patch.chunks():
424 for i
in range(0,len(src)):
427 m = whitespace.search(s.line())
428 if m
is not None and m.group(1) == d.line():
429 d.append_to_line(m.group(2))
436 output = tempfile.mkstemp()[1]
440 sys.stderr.write(
'original file=' + source +
'\n')
441 sys.stderr.write(
'uncrustify config file=' + cfg +
'\n')
442 sys.stderr.write(
'temporary file=' + output +
'\n')
444 uncrust = subprocess.Popen([
'uncrustify',
'-c', cfg,
'-f', source,
'-o', output],
445 stdin = subprocess.PIPE,
446 stdout = subprocess.PIPE,
447 stderr = subprocess.PIPE,
448 universal_newlines =
True)
449 (out, err) = uncrust.communicate(
'')
451 sys.stderr.write(out)
452 sys.stderr.write(err)
454 raise Exception (
'uncrustify not installed')
456 with open(source,
'r')
as src, open(output,
'r')
as dst:
457 diff = difflib.unified_diff(src.readlines(), dst.readlines(),
458 fromfile=source, tofile=output)
460 initial_diff = tempfile.mkstemp()[1]
461 sys.stderr.write(
'initial diff file=' + initial_diff +
'\n')
462 with open(initial_diff,
'w')
as tmp:
464 final_diff = tempfile.mkstemp()[1]
467 if len(patchset) != 0:
468 with open(final_diff,
'w')
as dst:
469 patchset[0].write(dst)
471 with open(final_diff,
'w')
as dst:
477 sys.stderr.write(
'final diff file=' + final_diff +
'\n')
478 shutil.copyfile(source,output)
479 patch = subprocess.Popen([
'patch',
'-p1',
'-i', final_diff, output],
480 stdin = subprocess.PIPE,
481 stdout = subprocess.PIPE,
482 stderr = subprocess.PIPE,
483 universal_newlines =
True)
484 (out, err) = patch.communicate(
'')
486 sys.stderr.write(out)
487 sys.stderr.write(err)
492 def indent_files(files, diff=False, debug=False, level=0, inplace=False):
495 dst =
indent(f, debug=debug, level=level)
496 output.append([f,dst])
500 for src,dst
in output:
501 shutil.copyfile(dst,src)
506 for src,dst
in output:
507 if filecmp.cmp(src,dst) == 0:
508 failed.append([src, dst])
511 print(
'Found %u badly indented files:' % len(failed))
512 for src,dst
in failed:
515 for src,dst
in failed:
516 with open(src,
'r')
as f_src, open(dst,
'r')
as f_dst:
517 s = f_src.readlines()
518 d = f_dst.readlines()
519 for line
in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
520 sys.stdout.write(line)
525 parser = optparse.OptionParser()
526 parser.add_option(
'--debug', action=
'store_true', dest=
'debug', default=
False,
527 help=
'Output some debugging information')
528 parser.add_option(
'-l',
'--level', type=
'int', dest=
'level', default=0,
529 help=
"Level of style conformance: higher levels include all lower levels. "
530 "level=0: re-indent only. level=1: add extra spaces. level=2: insert extra newlines and "
531 "extra braces around single-line statements. level=3: remove all trailing spaces")
532 parser.add_option(
'--check-git', action=
'store_true', dest=
'git', default=
False,
533 help=
"Get the list of files to check from Git\'s list of modified and added files")
534 parser.add_option(
'-f',
'--check-file', action=
'store', dest=
'file', default=
'',
535 help=
"Check a single file")
536 parser.add_option(
'--diff', action=
'store_true', dest=
'diff', default=
False,
537 help=
"Generate a diff on stdout of the indented files")
538 parser.add_option(
'-i',
'--in-place', action=
'store_true', dest=
'in_place', default=
False,
539 help=
"Indent the input files in-place")
540 options, _ = parser.parse_args()
541 style_is_correct =
False
549 inplace=options.in_place)
550 elif options.file !=
'':
552 if not os.path.exists(file)
or \
553 not os.path.isfile(file):
554 print(
'file %s does not exist' % file)
560 inplace=options.in_place)
562 if not style_is_correct:
566 if __name__ ==
'__main__':
569 except Exception
as e:
570 sys.stderr.write(str(e) +
'\n')