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