2019-08-12 16:55:34 -07:00
|
|
|
#!/usr/bin/env python3
|
2010-02-01 08:04:41 +01:00
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
|
import tempfile
|
|
|
|
|
import sys
|
|
|
|
|
import filecmp
|
|
|
|
|
import optparse
|
|
|
|
|
import shutil
|
|
|
|
|
import difflib
|
|
|
|
|
import re
|
|
|
|
|
|
2020-03-05 23:26:52 +05:30
|
|
|
def git_modified_files():
|
|
|
|
|
files = os.popen('git diff --name-only')
|
|
|
|
|
process = subprocess.Popen(["git","rev-parse","--show-toplevel"],
|
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
|
stderr = subprocess.PIPE)
|
2021-08-21 10:59:53 -07:00
|
|
|
root_dir, _ = process.communicate()
|
2020-03-05 23:26:52 +05:30
|
|
|
if isinstance(root_dir, bytes):
|
|
|
|
|
root_dir=root_dir.decode("utf-8")
|
|
|
|
|
files_changed = [item.strip() for item in files.readlines()]
|
|
|
|
|
files_changed = [item for item in files_changed if item.endswith('.h') or item.endswith('.cc')]
|
|
|
|
|
return [root_dir[: -1] + "/" + filename.strip () for filename in files_changed]
|
2010-02-01 08:04:41 +01:00
|
|
|
|
|
|
|
|
def copy_file(filename):
|
2021-08-21 10:59:53 -07:00
|
|
|
_, pathname = tempfile.mkstemp()
|
2020-05-13 18:45:06 +01:00
|
|
|
with open(filename, 'r') as src, open(pathname, 'w') as dst:
|
|
|
|
|
for line in src:
|
|
|
|
|
dst.write(line)
|
2010-02-01 08:04:41 +01:00
|
|
|
return pathname
|
|
|
|
|
|
|
|
|
|
# generate a temporary configuration file
|
|
|
|
|
def uncrustify_config_file(level):
|
|
|
|
|
level2 = """
|
|
|
|
|
nl_if_brace=Add
|
|
|
|
|
nl_brace_else=Add
|
|
|
|
|
nl_elseif_brace=Add
|
|
|
|
|
nl_else_brace=Add
|
|
|
|
|
nl_while_brace=Add
|
|
|
|
|
nl_do_brace=Add
|
|
|
|
|
nl_for_brace=Add
|
|
|
|
|
nl_brace_while=Add
|
|
|
|
|
nl_switch_brace=Add
|
|
|
|
|
nl_after_case=True
|
2020-04-07 17:10:07 -07:00
|
|
|
nl_namespace_brace=ignore
|
2010-02-01 08:04:41 +01:00
|
|
|
nl_after_brace_open=True
|
|
|
|
|
nl_class_leave_one_liners=False
|
|
|
|
|
nl_enum_leave_one_liners=False
|
|
|
|
|
nl_func_leave_one_liners=False
|
|
|
|
|
nl_if_leave_one_liners=False
|
|
|
|
|
nl_class_colon=Ignore
|
2020-03-25 15:12:18 -07:00
|
|
|
nl_before_access_spec=2
|
|
|
|
|
nl_after_access_spec=0
|
2020-04-07 17:10:07 -07:00
|
|
|
indent_access_spec=-indent_columns
|
2010-02-01 08:04:41 +01:00
|
|
|
nl_after_semicolon=True
|
|
|
|
|
pos_class_colon=Lead
|
|
|
|
|
pos_class_comma=Trail
|
2020-03-25 15:12:18 -07:00
|
|
|
indent_constr_colon=true
|
2010-02-01 08:04:41 +01:00
|
|
|
pos_bool=Lead
|
|
|
|
|
nl_class_init_args=Add
|
|
|
|
|
nl_template_class=Add
|
|
|
|
|
nl_class_brace=Add
|
|
|
|
|
# does not work very well
|
|
|
|
|
nl_func_type_name=Ignore
|
|
|
|
|
nl_func_scope_name=Ignore
|
|
|
|
|
nl_func_type_name_class=Ignore
|
|
|
|
|
nl_func_proto_type_name=Ignore
|
|
|
|
|
# function\\n(
|
|
|
|
|
nl_func_paren=Remove
|
|
|
|
|
nl_fdef_brace=Add
|
|
|
|
|
nl_struct_brace=Add
|
|
|
|
|
nl_enum_brace=Add
|
|
|
|
|
nl_union_brace=Add
|
|
|
|
|
mod_full_brace_do=Add
|
|
|
|
|
mod_full_brace_for=Add
|
|
|
|
|
mod_full_brace_if=Add
|
|
|
|
|
mod_full_brace_while=Add
|
|
|
|
|
mod_full_brace_for=Add
|
|
|
|
|
mod_remove_extra_semicolon=True
|
|
|
|
|
# max code width
|
|
|
|
|
#code_width=128
|
|
|
|
|
#ls_for_split_full=True
|
|
|
|
|
#ls_func_split_full=True
|
2020-03-25 15:12:18 -07:00
|
|
|
nl_cpp_lambda_leave_one_liners=True
|
2010-02-01 08:04:41 +01:00
|
|
|
"""
|
|
|
|
|
level1 = """
|
|
|
|
|
# extra spaces here and there
|
|
|
|
|
sp_brace_typedef=Add
|
|
|
|
|
sp_enum_assign=Add
|
|
|
|
|
sp_before_sparen=Add
|
|
|
|
|
sp_after_semi_for=Add
|
|
|
|
|
sp_arith=Add
|
|
|
|
|
sp_assign=Add
|
|
|
|
|
sp_compare=Add
|
|
|
|
|
sp_func_class_paren=Add
|
|
|
|
|
sp_after_type=Add
|
|
|
|
|
sp_type_func=Add
|
|
|
|
|
sp_angle_paren=Add
|
|
|
|
|
"""
|
|
|
|
|
level0 = """
|
2011-05-22 23:17:41 -07:00
|
|
|
sp_func_proto_paren=Add
|
|
|
|
|
sp_func_def_paren=Add
|
|
|
|
|
sp_func_call_paren=Add
|
2010-02-01 08:04:41 +01:00
|
|
|
sp_after_semi_for=Ignore
|
|
|
|
|
sp_before_sparen=Ignore
|
2020-08-24 18:50:07 +00:00
|
|
|
sp_before_ellipsis=Remove
|
2010-02-01 08:04:41 +01:00
|
|
|
sp_type_func=Ignore
|
|
|
|
|
sp_after_type=Ignore
|
|
|
|
|
nl_class_leave_one_liners=True
|
|
|
|
|
nl_enum_leave_one_liners=True
|
|
|
|
|
nl_func_leave_one_liners=True
|
|
|
|
|
nl_assign_leave_one_liners=True
|
2020-03-25 15:12:18 -07:00
|
|
|
nl_collapse_empty_body=True
|
2010-02-01 08:04:41 +01:00
|
|
|
nl_getset_leave_one_liners=True
|
|
|
|
|
nl_if_leave_one_liners=True
|
|
|
|
|
nl_fdef_brace=Ignore
|
|
|
|
|
# finally, indentation configuration
|
|
|
|
|
indent_with_tabs=0
|
|
|
|
|
indent_namespace=false
|
|
|
|
|
indent_columns=2
|
|
|
|
|
indent_brace=2
|
2020-04-07 17:10:07 -07:00
|
|
|
indent_case_brace=indent_columns
|
2010-02-01 08:04:41 +01:00
|
|
|
indent_class=true
|
|
|
|
|
indent_class_colon=True
|
2020-04-07 17:10:07 -07:00
|
|
|
indent_switch_case=indent_columns
|
2010-02-01 08:04:41 +01:00
|
|
|
# alignment
|
|
|
|
|
indent_align_assign=False
|
|
|
|
|
align_left_shift=True
|
|
|
|
|
# comment reformating disabled
|
|
|
|
|
cmt_reflow_mode=1 # do not touch comments at all
|
|
|
|
|
cmt_indent_multi=False # really, do not touch them
|
2020-03-25 15:12:18 -07:00
|
|
|
disable_processing_cmt= " *NS_CHECK_STYLE_OFF*"
|
|
|
|
|
enable_processing_cmt= " *NS_CHECK_STYLE_ON*"
|
2010-02-01 08:04:41 +01:00
|
|
|
"""
|
2021-08-21 10:59:53 -07:00
|
|
|
_, pathname = tempfile.mkstemp()
|
2020-05-13 18:45:06 +01:00
|
|
|
with open(pathname, 'w') as dst:
|
|
|
|
|
dst.write(level0)
|
|
|
|
|
if level >= 1:
|
|
|
|
|
dst.write(level1)
|
|
|
|
|
if level >= 2:
|
|
|
|
|
dst.write(level2)
|
2010-02-01 08:04:41 +01:00
|
|
|
return pathname
|
|
|
|
|
|
2017-02-24 09:23:49 -08:00
|
|
|
## PatchChunkLine class
|
2010-02-01 08:04:41 +01:00
|
|
|
class PatchChunkLine:
|
2017-02-24 09:23:49 -08:00
|
|
|
## @var __type
|
|
|
|
|
# type
|
|
|
|
|
## @var __line
|
|
|
|
|
# line
|
|
|
|
|
## @var SRC
|
|
|
|
|
# Source
|
2010-02-01 08:04:41 +01:00
|
|
|
SRC = 1
|
2017-02-24 09:23:49 -08:00
|
|
|
## @var DST
|
|
|
|
|
# Destination
|
2010-02-01 08:04:41 +01:00
|
|
|
DST = 2
|
2017-02-24 09:23:49 -08:00
|
|
|
## @var BOTH
|
|
|
|
|
# Both
|
2010-02-01 08:04:41 +01:00
|
|
|
BOTH = 3
|
|
|
|
|
def __init__(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Initializer
|
|
|
|
|
@param self The current class
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__type = 0
|
|
|
|
|
self.__line = ''
|
|
|
|
|
def set_src(self,line):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Set source
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param line source line
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__type = self.SRC
|
|
|
|
|
self.__line = line
|
|
|
|
|
def set_dst(self,line):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Set destination
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param line destination line
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__type = self.DST
|
|
|
|
|
self.__line = line
|
|
|
|
|
def set_both(self,line):
|
2020-05-13 18:45:06 +01:00
|
|
|
"""! Set both
|
2017-02-24 09:23:49 -08:00
|
|
|
@param self The current class
|
|
|
|
|
@param line
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__type = self.BOTH
|
|
|
|
|
self.__line = line
|
|
|
|
|
def append_to_line(self, s):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Append to line
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param s line to append
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__line = self.__line + s
|
|
|
|
|
def line(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Get line
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return line
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
return self.__line
|
|
|
|
|
def is_src(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Is source
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return true if type is source
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
return self.__type == self.SRC or self.__type == self.BOTH
|
|
|
|
|
def is_dst(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Is destination
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return true if type is destination
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
return self.__type == self.DST or self.__type == self.BOTH
|
|
|
|
|
def write(self, f):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Write to file
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param f file
|
2020-05-13 18:45:06 +01:00
|
|
|
@return exception if invalid type
|
2017-02-24 09:23:49 -08:00
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
if self.__type == self.SRC:
|
|
|
|
|
f.write('-%s\n' % self.__line)
|
|
|
|
|
elif self.__type == self.DST:
|
|
|
|
|
f.write('+%s\n' % self.__line)
|
|
|
|
|
elif self.__type == self.BOTH:
|
|
|
|
|
f.write(' %s\n' % self.__line)
|
|
|
|
|
else:
|
|
|
|
|
raise Exception('invalid patch')
|
2020-05-13 18:45:06 +01:00
|
|
|
|
2017-02-24 09:23:49 -08:00
|
|
|
## PatchChunk class
|
2010-02-01 08:04:41 +01:00
|
|
|
class PatchChunk:
|
2017-02-24 09:23:49 -08:00
|
|
|
## @var __lines
|
|
|
|
|
# list of lines
|
|
|
|
|
## @var __src_pos
|
|
|
|
|
# source position
|
|
|
|
|
## @var __dst_pos
|
|
|
|
|
# destination position
|
|
|
|
|
## @var src
|
|
|
|
|
# source
|
|
|
|
|
## @var dst
|
|
|
|
|
# destination
|
2010-02-01 08:04:41 +01:00
|
|
|
def __init__(self, src_pos, dst_pos):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Initializer
|
|
|
|
|
@param self: this object
|
|
|
|
|
@param src_pos: source position
|
|
|
|
|
@param dst_pos: destination position
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__lines = []
|
|
|
|
|
self.__src_pos = int(src_pos)
|
|
|
|
|
self.__dst_pos = int(dst_pos)
|
|
|
|
|
def src_start(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Source start function
|
|
|
|
|
@param self this object
|
|
|
|
|
@return source position
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
return self.__src_pos
|
|
|
|
|
def add_line(self,line):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Add line function
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param line line to add
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__lines.append(line)
|
|
|
|
|
def src(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Get source lines
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return the source lines
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
src = []
|
|
|
|
|
for line in self.__lines:
|
|
|
|
|
if line.is_src():
|
|
|
|
|
src.append(line)
|
|
|
|
|
return src
|
|
|
|
|
def dst(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Get destination lines
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return the destination lines
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
dst = []
|
|
|
|
|
for line in self.__lines:
|
|
|
|
|
if line.is_dst():
|
|
|
|
|
dst.append(line)
|
|
|
|
|
return dst
|
|
|
|
|
def src_len(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Get number of source lines
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return number of source lines
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
return len(self.src())
|
|
|
|
|
def dst_len(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Get number of destinaton lines
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return number of destination lines
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
return len(self.dst())
|
|
|
|
|
def write(self,f):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Write lines to file
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param f: file to write to
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
f.write('@@ -%d,%d +%d,%d @@\n' % (self.__src_pos, self.src_len(),
|
|
|
|
|
self.__dst_pos, self.dst_len()))
|
|
|
|
|
for line in self.__lines:
|
|
|
|
|
line.write(f)
|
|
|
|
|
|
2017-02-24 09:23:49 -08:00
|
|
|
## Patch class
|
2010-02-01 08:04:41 +01:00
|
|
|
class Patch:
|
2017-02-24 09:23:49 -08:00
|
|
|
## @var __src
|
|
|
|
|
# source
|
|
|
|
|
## @var __dst
|
|
|
|
|
# destination
|
|
|
|
|
## @var __chunks
|
|
|
|
|
# chunks
|
2010-02-01 08:04:41 +01:00
|
|
|
def __init__(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Initializer
|
|
|
|
|
@param self The current class
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__src = ''
|
|
|
|
|
self.__dst = ''
|
|
|
|
|
self.__chunks = []
|
|
|
|
|
def add_chunk(self, chunk):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Add chunk
|
|
|
|
|
@param self this object
|
|
|
|
|
@param chunk chunk
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__chunks.append(chunk)
|
|
|
|
|
def chunks(self):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Get the chunks
|
|
|
|
|
@param self The current class
|
|
|
|
|
@return the chunks
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
return self.__chunks
|
|
|
|
|
def set_src(self,src):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Set source
|
|
|
|
|
@param self this object
|
|
|
|
|
@param src source
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__src = src
|
|
|
|
|
def set_dst(self,dst):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Set destination
|
|
|
|
|
@param self this object
|
|
|
|
|
@param dst destintion
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
self.__dst = dst
|
|
|
|
|
def apply(self,filename):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Apply function
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param filename file name
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
# XXX: not implemented
|
|
|
|
|
return
|
|
|
|
|
def write(self,f):
|
2017-02-24 09:23:49 -08:00
|
|
|
"""! Write to file
|
|
|
|
|
@param self The current class
|
|
|
|
|
@param f the file
|
|
|
|
|
@return none
|
|
|
|
|
"""
|
2010-02-01 08:04:41 +01:00
|
|
|
f.write('--- %s\n' % self.__src )
|
|
|
|
|
f.write('+++ %s\n' % self.__dst )
|
|
|
|
|
for chunk in self.__chunks:
|
|
|
|
|
chunk.write(f)
|
|
|
|
|
|
|
|
|
|
def parse_patchset(generator):
|
|
|
|
|
src_file = re.compile('^--- (.*)$')
|
|
|
|
|
dst_file = re.compile('^\+\+\+ (.*)$')
|
|
|
|
|
chunk_start = re.compile('^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@')
|
|
|
|
|
src = re.compile('^-(.*)$')
|
|
|
|
|
dst = re.compile('^\+(.*)$')
|
|
|
|
|
both = re.compile('^ (.*)$')
|
|
|
|
|
patchset = []
|
|
|
|
|
current_patch = None
|
|
|
|
|
for line in generator:
|
|
|
|
|
m = src_file.search(line)
|
|
|
|
|
if m is not None:
|
|
|
|
|
current_patch = Patch()
|
|
|
|
|
patchset.append(current_patch)
|
|
|
|
|
current_patch.set_src(m.group(1))
|
|
|
|
|
continue
|
|
|
|
|
m = dst_file.search(line)
|
|
|
|
|
if m is not None:
|
|
|
|
|
current_patch.set_dst(m.group(1))
|
|
|
|
|
continue
|
|
|
|
|
m = chunk_start.search(line)
|
|
|
|
|
if m is not None:
|
|
|
|
|
current_chunk = PatchChunk(m.group(1), m.group(3))
|
|
|
|
|
current_patch.add_chunk(current_chunk)
|
|
|
|
|
continue
|
|
|
|
|
m = src.search(line)
|
|
|
|
|
if m is not None:
|
|
|
|
|
l = PatchChunkLine()
|
|
|
|
|
l.set_src(m.group(1))
|
|
|
|
|
current_chunk.add_line(l)
|
|
|
|
|
continue
|
|
|
|
|
m = dst.search(line)
|
|
|
|
|
if m is not None:
|
|
|
|
|
l = PatchChunkLine()
|
|
|
|
|
l.set_dst(m.group(1))
|
|
|
|
|
current_chunk.add_line(l)
|
|
|
|
|
continue
|
|
|
|
|
m = both.search(line)
|
|
|
|
|
if m is not None:
|
|
|
|
|
l = PatchChunkLine()
|
|
|
|
|
l.set_both(m.group(1))
|
|
|
|
|
current_chunk.add_line(l)
|
|
|
|
|
continue
|
|
|
|
|
raise Exception()
|
|
|
|
|
return patchset
|
|
|
|
|
|
|
|
|
|
def remove_trailing_whitespace_changes(patch_generator):
|
|
|
|
|
whitespace = re.compile('^(.*)([ \t]+)$')
|
|
|
|
|
patchset = parse_patchset(patch_generator)
|
|
|
|
|
for patch in patchset:
|
|
|
|
|
for chunk in patch.chunks():
|
|
|
|
|
src = chunk.src()
|
|
|
|
|
dst = chunk.dst()
|
|
|
|
|
try:
|
|
|
|
|
for i in range(0,len(src)):
|
|
|
|
|
s = src[i]
|
|
|
|
|
d = dst[i]
|
|
|
|
|
m = whitespace.search(s.line())
|
|
|
|
|
if m is not None and m.group(1) == d.line():
|
|
|
|
|
d.append_to_line(m.group(2))
|
|
|
|
|
except:
|
|
|
|
|
return patchset
|
|
|
|
|
return patchset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2020-03-25 15:12:18 -07:00
|
|
|
stderr = subprocess.PIPE,
|
2020-06-27 11:00:32 -07:00
|
|
|
universal_newlines = True)
|
2010-02-01 08:04:41 +01:00
|
|
|
(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
|
2020-05-13 18:45:06 +01:00
|
|
|
with open(source, 'r') as src, open(output, 'r') as dst:
|
|
|
|
|
diff = difflib.unified_diff(src.readlines(), dst.readlines(),
|
|
|
|
|
fromfile=source, tofile=output)
|
2010-02-01 08:04:41 +01:00
|
|
|
if debug:
|
|
|
|
|
initial_diff = tempfile.mkstemp()[1]
|
|
|
|
|
sys.stderr.write('initial diff file=' + initial_diff + '\n')
|
2020-05-13 18:45:06 +01:00
|
|
|
with open(initial_diff, 'w') as tmp:
|
|
|
|
|
tmp.writelines(diff)
|
2010-02-01 08:04:41 +01:00
|
|
|
final_diff = tempfile.mkstemp()[1]
|
|
|
|
|
if level < 3:
|
2020-05-13 18:45:06 +01:00
|
|
|
patchset = remove_trailing_whitespace_changes(diff)
|
2010-02-01 08:04:41 +01:00
|
|
|
if len(patchset) != 0:
|
2020-05-13 18:45:06 +01:00
|
|
|
with open(final_diff, 'w') as dst:
|
|
|
|
|
patchset[0].write(dst)
|
2010-02-01 08:04:41 +01:00
|
|
|
else:
|
2020-05-13 18:45:06 +01:00
|
|
|
with open(final_diff, 'w') as dst:
|
|
|
|
|
dst.writelines(diff)
|
|
|
|
|
|
|
|
|
|
|
2010-02-01 08:04:41 +01:00
|
|
|
# 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,
|
2020-03-25 15:12:18 -07:00
|
|
|
stderr = subprocess.PIPE,
|
2020-06-27 11:00:32 -07:00
|
|
|
universal_newlines = True)
|
2010-02-01 08:04:41 +01:00
|
|
|
(out, err) = patch.communicate('')
|
|
|
|
|
if debug:
|
|
|
|
|
sys.stderr.write(out)
|
|
|
|
|
sys.stderr.write(err)
|
|
|
|
|
return output
|
2020-05-13 18:45:06 +01:00
|
|
|
|
2010-02-01 08:04:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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 src,dst in output:
|
|
|
|
|
if filecmp.cmp(src,dst) == 0:
|
|
|
|
|
failed.append([src, dst])
|
|
|
|
|
if len(failed) > 0:
|
|
|
|
|
if not diff:
|
2016-02-01 15:27:31 -08:00
|
|
|
print('Found %u badly indented files:' % len(failed))
|
2010-02-01 08:04:41 +01:00
|
|
|
for src,dst in failed:
|
2016-02-01 15:27:31 -08:00
|
|
|
print(' ' + src)
|
2010-02-01 08:04:41 +01:00
|
|
|
else:
|
|
|
|
|
for src,dst in failed:
|
2020-05-13 18:45:06 +01:00
|
|
|
with open(src, 'r') as f_src, open(dst, 'r') as f_dst:
|
|
|
|
|
s = f_src.readlines()
|
|
|
|
|
d = f_dst.readlines()
|
|
|
|
|
for line in difflib.unified_diff(s, d, fromfile=src, tofile=dst):
|
|
|
|
|
sys.stdout.write(line)
|
2010-02-01 08:04:41 +01:00
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def run_as_main():
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
|
parser.add_option('--debug', action='store_true', dest='debug', default=False,
|
|
|
|
|
help='Output some debugging information')
|
|
|
|
|
parser.add_option('-l', '--level', type='int', dest='level', default=0,
|
|
|
|
|
help="Level of style conformance: higher levels include all lower levels. "
|
|
|
|
|
"level=0: re-indent only. level=1: add extra spaces. level=2: insert extra newlines and "
|
|
|
|
|
"extra braces around single-line statements. level=3: remove all trailing spaces")
|
2020-03-05 23:26:52 +05:30
|
|
|
parser.add_option('--check-git', action='store_true', dest='git', default=False,
|
|
|
|
|
help="Get the list of files to check from Git\'s list of modified and added files")
|
2010-02-01 08:04:41 +01:00
|
|
|
parser.add_option('-f', '--check-file', action='store', dest='file', default='',
|
|
|
|
|
help="Check a single file")
|
|
|
|
|
parser.add_option('--diff', action='store_true', dest='diff', default=False,
|
|
|
|
|
help="Generate a diff on stdout of the indented files")
|
|
|
|
|
parser.add_option('-i', '--in-place', action='store_true', dest='in_place', default=False,
|
|
|
|
|
help="Indent the input files in-place")
|
2021-08-21 10:59:53 -07:00
|
|
|
options, _ = parser.parse_args()
|
|
|
|
|
style_is_correct = False
|
2020-03-05 23:26:52 +05:30
|
|
|
|
|
|
|
|
if options.git:
|
|
|
|
|
files = git_modified_files()
|
|
|
|
|
style_is_correct = indent_files(files,
|
|
|
|
|
diff=options.diff,
|
|
|
|
|
debug=options.debug,
|
|
|
|
|
level=options.level,
|
|
|
|
|
inplace=options.in_place)
|
2010-02-01 08:04:41 +01:00
|
|
|
elif options.file != '':
|
|
|
|
|
file = options.file
|
|
|
|
|
if not os.path.exists(file) or \
|
|
|
|
|
not os.path.isfile(file):
|
2016-02-01 15:27:31 -08:00
|
|
|
print('file %s does not exist' % file)
|
2010-02-01 08:04:41 +01:00
|
|
|
sys.exit(1)
|
2020-03-05 23:26:52 +05:30
|
|
|
style_is_correct = indent_files([file],
|
|
|
|
|
diff=options.diff,
|
|
|
|
|
debug=options.debug,
|
|
|
|
|
level=options.level,
|
|
|
|
|
inplace=options.in_place)
|
|
|
|
|
|
|
|
|
|
if not style_is_correct:
|
|
|
|
|
sys.exit(1)
|
2010-02-01 08:04:41 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-03-05 23:26:52 +05:30
|
|
|
try:
|
2010-02-01 08:04:41 +01:00
|
|
|
run_as_main()
|
2020-03-05 23:26:52 +05:30
|
|
|
except Exception as e:
|
|
|
|
|
sys.stderr.write(str(e) + '\n')
|
|
|
|
|
sys.exit(1)
|