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
167 @param self The current class
168 @param line source line
175 @param self The current class
176 @param line destination line
183 @param self The current class
191 @param self The current class
192 @param s line to append
198 @param self The current class
204 @param self The current class
205 @return true if type is source
210 @param self The current class
211 @return true if type is destination
216 @param self The current class
218 @return exception if invalid type
221 f.write(
'-%s\n' % self.
__line__line)
223 f.write(
'+%s\n' % self.
__line__line)
225 f.write(
' %s\n' % self.
__line__line)
227 raise Exception(
'invalid patch')
243 @param self: this object
244 @param src_pos: source position
245 @param dst_pos: destination position
251 """! Source start function
252 @param self this object
253 @return source position
257 """! Add line function
258 @param self The current class
259 @param line line to add
262 self.
__lines__lines.append(line)
264 """! Get source lines
265 @param self The current class
266 @return the source lines
269 for line
in self.
__lines__lines:
274 """! Get destination lines
275 @param self The current class
276 @return the destination lines
279 for line
in self.
__lines__lines:
284 """! Get number of source lines
285 @param self The current class
286 @return number of source lines
288 return len(self.
srcsrc())
290 """! Get number of destinaton lines
291 @param self The current class
292 @return number of destination lines
294 return len(self.
dstdst())
296 """! Write lines to file
297 @param self The current class
298 @param f: file to write to
301 f.write(
'@@ -%d,%d +%d,%d @@\n' % (self.
__src_pos__src_pos, self.
src_lensrc_len(),
303 for line
in self.
__lines__lines:
316 @param self The current class
323 @param self this object
330 @param self The current class
336 @param self this object
340 self.
__src__src = src
343 @param self this object
344 @param dst destintion
347 self.
__dst__dst = dst
350 @param self The current class
351 @param filename file name
358 @param self The current class
362 f.write(
'--- %s\n' % self.__src )
363 f.write(
'+++ %s\n' % self.__dst )
364 for chunk
in self.__chunks:
368 src_file = re.compile(
'^--- (.*)$')
369 dst_file = re.compile(
'^\+\+\+ (.*)$')
370 chunk_start = re.compile(
'^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@')
371 src = re.compile(
'^-(.*)$')
372 dst = re.compile(
'^\+(.*)$')
373 both = re.compile(
'^ (.*)$')
376 for line
in generator:
377 m = src_file.search(line)
379 current_patch =
Patch()
380 patchset.append(current_patch)
381 current_patch.set_src(m.group(1))
383 m = dst_file.search(line)
385 current_patch.set_dst(m.group(1))
387 m = chunk_start.search(line)
389 current_chunk =
PatchChunk(m.group(1), m.group(3))
390 current_patch.add_chunk(current_chunk)
395 l.set_src(m.group(1))
396 current_chunk.add_line(l)
401 l.set_dst(m.group(1))
402 current_chunk.add_line(l)
404 m = both.search(line)
407 l.set_both(m.group(1))
408 current_chunk.add_line(l)
414 whitespace = re.compile(
'^(.*)([ \t]+)$')
416 for patch
in patchset:
417 for chunk
in patch.chunks():
421 for i
in range(0,len(src)):
424 m = whitespace.search(s.line())
425 if m
is not None and m.group(1) == d.line():
426 d.append_to_line(m.group(2))
433 output = tempfile.mkstemp()[1]
437 sys.stderr.write(
'original file=' + source +
'\n')
438 sys.stderr.write(
'uncrustify config file=' + cfg +
'\n')
439 sys.stderr.write(
'temporary file=' + output +
'\n')
441 uncrust = subprocess.Popen([
'uncrustify',
'-c', cfg,
'-f', source,
'-o', output],
442 stdin = subprocess.PIPE,
443 stdout = subprocess.PIPE,
444 stderr = subprocess.PIPE,
445 universal_newlines =
True)
446 (out, err) = uncrust.communicate(
'')
448 sys.stderr.write(out)
449 sys.stderr.write(err)
451 raise Exception (
'uncrustify not installed')
453 with open(source,
'r')
as src, open(output,
'r')
as dst:
454 diff = difflib.unified_diff(src.readlines(), dst.readlines(),
455 fromfile=source, tofile=output)
457 initial_diff = tempfile.mkstemp()[1]
458 sys.stderr.write(
'initial diff file=' + initial_diff +
'\n')
459 with open(initial_diff,
'w')
as tmp:
461 final_diff = tempfile.mkstemp()[1]
464 if len(patchset) != 0:
465 with open(final_diff,
'w')
as dst:
466 patchset[0].write(dst)
468 with open(final_diff,
'w')
as dst:
474 sys.stderr.write(
'final diff file=' + final_diff +
'\n')
475 shutil.copyfile(source,output)
476 patch = subprocess.Popen([
'patch',
'-p1',
'-i', final_diff, output],
477 stdin = subprocess.PIPE,
478 stdout = subprocess.PIPE,
479 stderr = subprocess.PIPE,
480 universal_newlines =
True)
481 (out, err) = patch.communicate(
'')
483 sys.stderr.write(out)
484 sys.stderr.write(err)
489 def indent_files(files, diff=False, debug=False, level=0, inplace=False):
492 dst =
indent(f, debug=debug, level=level)
493 output.append([f,dst])
497 for src,dst
in output:
498 shutil.copyfile(dst,src)
503 for src,dst
in output:
504 if filecmp.cmp(src,dst) == 0:
505 failed.append([src, dst])
508 print(
'Found %u badly indented files:' % len(failed))
509 for src,dst
in failed:
512 for src,dst
in failed:
513 with open(src,
'r')
as f_src, open(dst,
'r')
as f_dst:
514 s = f_src.readlines()
515 d = f_dst.readlines()
516 for line
in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
517 sys.stdout.write(line)
522 parser = optparse.OptionParser()
523 parser.add_option(
'--debug', action=
'store_true', dest=
'debug', default=
False,
524 help=
'Output some debugging information')
525 parser.add_option(
'-l',
'--level', type=
'int', dest=
'level', default=0,
526 help=
"Level of style conformance: higher levels include all lower levels. "
527 "level=0: re-indent only. level=1: add extra spaces. level=2: insert extra newlines and "
528 "extra braces around single-line statements. level=3: remove all trailing spaces")
529 parser.add_option(
'--check-git', action=
'store_true', dest=
'git', default=
False,
530 help=
"Get the list of files to check from Git\'s list of modified and added files")
531 parser.add_option(
'-f',
'--check-file', action=
'store', dest=
'file', default=
'',
532 help=
"Check a single file")
533 parser.add_option(
'--diff', action=
'store_true', dest=
'diff', default=
False,
534 help=
"Generate a diff on stdout of the indented files")
535 parser.add_option(
'-i',
'--in-place', action=
'store_true', dest=
'in_place', default=
False,
536 help=
"Indent the input files in-place")
537 options, _ = parser.parse_args()
538 style_is_correct =
False
546 inplace=options.in_place)
547 elif options.file !=
'':
549 if not os.path.exists(file)
or \
550 not os.path.isfile(file):
551 print(
'file %s does not exist' % file)
557 inplace=options.in_place)
559 if not style_is_correct:
563 if __name__ ==
'__main__':
566 except Exception
as e:
567 sys.stderr.write(str(e) +
'\n')
def dst(self)
Get destination lines.
def src(self)
Get source lines.
def dst_len(self)
Get number of destinaton lines.
def src_start(self)
Source start function.
def write(self, f)
Write lines to file.
def __init__(self, src_pos, dst_pos)
Initializer.
def add_line(self, line)
Add line function.
def src_len(self)
Get number of source lines.
__dst_pos
destination position
def __init__(self)
Initializer.
def set_both(self, line)
Set both.
def write(self, f)
Write to file.
def is_src(self)
Is source.
def set_dst(self, line)
Set destination.
def set_src(self, line)
Set source.
def is_dst(self)
Is destination.
def append_to_line(self, s)
Append to line.
def add_chunk(self, chunk)
Add chunk.
def chunks(self)
Get the chunks.
def set_src(self, src)
Set source.
def set_dst(self, dst)
Set destination.
def apply(self, filename)
Apply function.
def write(self, f)
Write to file.
def __init__(self)
Initializer.
def parse_patchset(generator)
def uncrustify_config_file(level)
def remove_trailing_whitespace_changes(patch_generator)
def indent(source, debug, level)
def indent_files(files, diff=False, debug=False, level=0, inplace=False)