]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Scripts/FormatDosFiles.py
BaseTools/BinToPcd: Follow PEP-8 indent of 4 spaces
[mirror_edk2.git] / BaseTools / Scripts / FormatDosFiles.py
1 # @file FormatDosFiles.py
2 # This script format the source files to follow dos style.
3 # It supports Python2.x and Python3.x both.
4 #
5 # Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
6 #
7 # This program and the accompanying materials
8 # are licensed and made available under the terms and conditions of the BSD License
9 # which accompanies this distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
11 #
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #
15
16 #
17 # Import Modules
18 #
19 import argparse
20 import os
21 import os.path
22 import re
23 import sys
24 import copy
25
26 __prog__ = 'FormatDosFiles'
27 __version__ = '%s Version %s' % (__prog__, '0.10 ')
28 __copyright__ = 'Copyright (c) 2018, Intel Corporation. All rights reserved.'
29 __description__ = 'Convert source files to meet the EDKII C Coding Standards Specification.\n'
30 DEFAULT_EXT_LIST = ['.h', '.c', '.nasm', '.nasmb', '.asm', '.S', '.inf', '.dec', '.dsc', '.fdf', '.uni', '.asl', '.aslc', '.vfr', '.idf', '.txt', '.bat', '.py']
31
32 #For working in python2 and python3 environment, re pattern should use binary string, which is bytes type in python3.
33 #Because in python3,read from file in binary mode will return bytes type,and in python3 bytes type can not be mixed with str type.
34 def FormatFile(FilePath, Args):
35 with open(FilePath, 'rb') as Fd:
36 Content = Fd.read()
37 # Convert the line endings to CRLF
38 Content = re.sub(br'([^\r])\n', br'\1\r\n', Content)
39 Content = re.sub(br'^\n', br'\r\n', Content, flags=re.MULTILINE)
40 # Add a new empty line if the file is not end with one
41 Content = re.sub(br'([^\r\n])$', br'\1\r\n', Content)
42 # Remove trailing white spaces
43 Content = re.sub(br'[ \t]+(\r\n)', br'\1', Content, flags=re.MULTILINE)
44 # Replace '\t' with two spaces
45 Content = re.sub(b'\t', b' ', Content)
46 with open(FilePath, 'wb') as Fd:
47 Fd.write(Content)
48 if not Args.Quiet:
49 print(FilePath)
50
51 def FormatFilesInDir(DirPath, ExtList, Args):
52
53 FileList = []
54 for DirPath, DirNames, FileNames in os.walk(DirPath):
55 if Args.Exclude:
56 DirNames[:] = [d for d in DirNames if d not in Args.Exclude]
57 FileNames[:] = [f for f in FileNames if f not in Args.Exclude]
58 for FileName in [f for f in FileNames if any(f.endswith(ext) for ext in ExtList)]:
59 FileList.append(os.path.join(DirPath, FileName))
60 for File in FileList:
61 FormatFile(File, Args)
62
63 if __name__ == "__main__":
64 parser = argparse.ArgumentParser(prog=__prog__,description=__description__ + __copyright__, conflict_handler = 'resolve')
65
66 parser.add_argument('Path', nargs='+',
67 help='the path for files to be converted.It could be directory or file path.')
68 parser.add_argument('--version', action='version', version=__version__)
69 parser.add_argument('--append-extensions', dest='AppendExt', nargs='+',
70 help='append file extensions filter to default extensions. (Example: .txt .c .h)')
71 parser.add_argument('--override-extensions', dest='OverrideExt', nargs='+',
72 help='override file extensions filter on default extensions. (Example: .txt .c .h)')
73 parser.add_argument('-v', '--verbose', dest='Verbose', action='store_true',
74 help='increase output messages')
75 parser.add_argument('-q', '--quiet', dest='Quiet', action='store_true',
76 help='reduce output messages')
77 parser.add_argument('--debug', dest='Debug', type=int, metavar='[0-9]', choices=range(0, 10), default=0,
78 help='set debug level')
79 parser.add_argument('--exclude', dest='Exclude', nargs='+', help="directory name or file name which will be excluded")
80 args = parser.parse_args()
81 DefaultExt = copy.copy(DEFAULT_EXT_LIST)
82
83 if args.OverrideExt is not None:
84 DefaultExt = args.OverrideExt
85 if args.AppendExt is not None:
86 DefaultExt = list(set(DefaultExt + args.AppendExt))
87
88 for Path in args.Path:
89 if not os.path.exists(Path):
90 print("not exists path: {0}".format(Path))
91 sys.exit(1)
92 if os.path.isdir(Path):
93 FormatFilesInDir(Path, DefaultExt, args)
94 elif os.path.isfile(Path):
95 FormatFile(Path, args)