]>
git.proxmox.com Git - mirror_edk2.git/blob - 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.
5 # Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
13 from __future__
import print_function
21 __prog__
= 'FormatDosFiles'
22 __version__
= '%s Version %s' % (__prog__
, '0.10 ')
23 __copyright__
= 'Copyright (c) 2018, Intel Corporation. All rights reserved.'
24 __description__
= 'Convert source files to meet the EDKII C Coding Standards Specification.\n'
25 DEFAULT_EXT_LIST
= ['.h', '.c', '.nasm', '.nasmb', '.asm', '.S', '.inf', '.dec', '.dsc', '.fdf', '.uni', '.asl', '.aslc', '.vfr', '.idf', '.txt', '.bat', '.py']
27 #For working in python2 and python3 environment, re pattern should use binary string, which is bytes type in python3.
28 #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.
29 def FormatFile(FilePath
, Args
):
30 with
open(FilePath
, 'rb') as Fd
:
32 # Convert the line endings to CRLF
33 Content
= re
.sub(br
'([^\r])\n', br
'\1\r\n', Content
)
34 Content
= re
.sub(br
'^\n', br
'\r\n', Content
, flags
=re
.MULTILINE
)
35 # Add a new empty line if the file is not end with one
36 Content
= re
.sub(br
'([^\r\n])$', br
'\1\r\n', Content
)
37 # Remove trailing white spaces
38 Content
= re
.sub(br
'[ \t]+(\r\n)', br
'\1', Content
, flags
=re
.MULTILINE
)
39 # Replace '\t' with two spaces
40 Content
= re
.sub(b
'\t', b
' ', Content
)
41 with
open(FilePath
, 'wb') as Fd
:
46 def FormatFilesInDir(DirPath
, ExtList
, Args
):
49 for DirPath
, DirNames
, FileNames
in os
.walk(DirPath
):
51 DirNames
[:] = [d
for d
in DirNames
if d
not in Args
.Exclude
]
52 FileNames
[:] = [f
for f
in FileNames
if f
not in Args
.Exclude
]
53 for FileName
in [f
for f
in FileNames
if any(f
.endswith(ext
) for ext
in ExtList
)]:
54 FileList
.append(os
.path
.join(DirPath
, FileName
))
56 FormatFile(File
, Args
)
58 if __name__
== "__main__":
59 parser
= argparse
.ArgumentParser(prog
=__prog__
, description
=__description__
+ __copyright__
, conflict_handler
= 'resolve')
61 parser
.add_argument('Path', nargs
='+',
62 help='the path for files to be converted.It could be directory or file path.')
63 parser
.add_argument('--version', action
='version', version
=__version__
)
64 parser
.add_argument('--append-extensions', dest
='AppendExt', nargs
='+',
65 help='append file extensions filter to default extensions. (Example: .txt .c .h)')
66 parser
.add_argument('--override-extensions', dest
='OverrideExt', nargs
='+',
67 help='override file extensions filter on default extensions. (Example: .txt .c .h)')
68 parser
.add_argument('-v', '--verbose', dest
='Verbose', action
='store_true',
69 help='increase output messages')
70 parser
.add_argument('-q', '--quiet', dest
='Quiet', action
='store_true',
71 help='reduce output messages')
72 parser
.add_argument('--debug', dest
='Debug', type=int, metavar
='[0-9]', choices
=range(0, 10), default
=0,
73 help='set debug level')
74 parser
.add_argument('--exclude', dest
='Exclude', nargs
='+', help="directory name or file name which will be excluded")
75 args
= parser
.parse_args()
76 DefaultExt
= copy
.copy(DEFAULT_EXT_LIST
)
78 if args
.OverrideExt
is not None:
79 DefaultExt
= args
.OverrideExt
80 if args
.AppendExt
is not None:
81 DefaultExt
= list(set(DefaultExt
+ args
.AppendExt
))
83 for Path
in args
.Path
:
84 if not os
.path
.exists(Path
):
85 print("not exists path: {0}".format(Path
))
87 if os
.path
.isdir(Path
):
88 FormatFilesInDir(Path
, DefaultExt
, args
)
89 elif os
.path
.isfile(Path
):
90 FormatFile(Path
, args
)