File indexing completed on 2024-05-12 04:17:32
0001 #!/usr/bin/env python 0002 0003 # ============================================================ 0004 # 0005 # This file is a part of digiKam project 0006 # https://www.digikam.org 0007 # 0008 # Date : 2011-03-09 0009 # Description : a helper script for formatting the digiKam source code 0010 # prerequisites: Python 2.7 or higher: http://www.python.org 0011 # AStyle: http://astyle.sourceforge.net 0012 # 0013 # SPDX-FileCopyrightText: 2011 by Andi Clemens <andi dot clemens at gmail dot com> 0014 # 0015 # SPDX-License-Identifier: BSD-3-Clause 0016 # 0017 # ============================================================ */ 0018 0019 import os 0020 import sys 0021 import subprocess 0022 0023 # --------------------------------------------------- 0024 0025 # if necessary, enter the absolute path to the astyle executable 0026 astyle = "astyle" 0027 0028 # the file type that should be formatted (defined by its extension) 0029 file_srcs = [ 0030 ".cpp", 0031 ".c", 0032 ".hpp", 0033 ".h", 0034 ] 0035 0036 # the file type that defines a backup file (defined by its extension) 0037 file_backup = [ 0038 ".orig", 0039 ] 0040 0041 # --------------------------------------------------- 0042 0043 def format_file(f, verbose=False): 0044 args = list() 0045 args.append(astyle) 0046 args.append("--mode=c") 0047 args.append("--style=allman") 0048 args.append("--indent=spaces=4") 0049 args.append("--convert-tabs") 0050 args.append("--indent-switches") 0051 args.append("--break-blocks") 0052 args.append("--break-closing-brackets") 0053 args.append("--pad-oper") 0054 args.append("--pad-header") 0055 args.append("--unpad-paren") 0056 args.append("--align-pointer=type") 0057 args.append("--align-reference=type") 0058 args.append("--indent-col1-comments") 0059 args.append("--add-brackets") 0060 args.append("--min-conditional-indent=0") 0061 args.append("--max-instatement-indent=70") 0062 args.append(f) 0063 0064 if (verbose): 0065 process = subprocess.Popen(args) 0066 else: 0067 process = subprocess.Popen(args, stdout=subprocess.PIPE) 0068 process.communicate() 0069 return process.returncode 0070 0071 # --------------------------------------------------- 0072 0073 def get_files(path, file_ext): 0074 files2check = set() 0075 0076 if os.path.isfile(path): 0077 for ext in file_ext: 0078 if ext in file_backup: 0079 if os.path.isfile(path + ext): 0080 files2check.add(path + ext) 0081 elif os.path.isfile(path): 0082 files2check.add(path) 0083 elif os.path.isdir(path): 0084 files2check.update([os.path.join(r,f) for r,dirs,files in os.walk(path) for f in 0085 files if os.path.splitext(os.path.join(r,f))[1] in file_ext]) 0086 return files2check 0087 0088 # --------------------------------------------------- 0089 0090 def format_path(path, verbose=False): 0091 errors = 0 0092 0093 files2check = get_files(path, file_srcs) 0094 0095 for f in files2check: 0096 if format_file(f, verbose) != 0: 0097 print("An error occurred while formatting '%s'" % f) 0098 errors += 1 0099 0100 if (verbose): 0101 print("Formatted %d source files..." % (len(files2check) - errors)) 0102 0103 return errors 0104 0105 # --------------------------------------------------- 0106 0107 def cleanup_path(path, verbose): 0108 # TODO: error handling 0109 errors = 0 0110 0111 files2check = get_files(path, file_backup) 0112 0113 for f in files2check: 0114 os.remove(f) 0115 if (verbose): 0116 print("removing %s" % f) 0117 if (verbose): 0118 print("Removed %d backup files..." % (len(files2check) - errors)) 0119 0120 return errors 0121 0122 # --------------------------------------------------- 0123 0124 def main(): 0125 import argparse 0126 parser = argparse.ArgumentParser() 0127 parser.add_argument("path", help="the path where the source files are located") 0128 parser.add_argument("-v", "--verbose", help="verbose output", default=False, action="store_true") 0129 parser.add_argument("-b", "--backup", help="keep backup files", default=False, action="store_true") 0130 args = parser.parse_args() 0131 0132 errors = format_path(args.path, args.verbose) 0133 0134 if (args.backup == False): 0135 errors += cleanup_path(args.path, args.verbose) 0136 0137 return errors 0138 0139 # --------------------------------------------------- 0140 0141 if __name__ == "__main__": 0142 sys.exit(main()) 0143