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 | # SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 | #
|
---|
9 |
|
---|
10 | #
|
---|
11 | # Import Modules
|
---|
12 | #
|
---|
13 | from __future__ import print_function
|
---|
14 | import argparse
|
---|
15 | import os
|
---|
16 | import os.path
|
---|
17 | import re
|
---|
18 | import sys
|
---|
19 | import copy
|
---|
20 |
|
---|
21 | __prog__ = 'FormatDosFiles'
|
---|
22 | __version__ = '%s Version %s' % (__prog__, '0.10 ')
|
---|
23 | __copyright__ = 'Copyright (c) 2018-2019, 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']
|
---|
26 |
|
---|
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:
|
---|
31 | Content = Fd.read()
|
---|
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:
|
---|
42 | Fd.write(Content)
|
---|
43 | if not Args.Quiet:
|
---|
44 | print(FilePath)
|
---|
45 |
|
---|
46 | def FormatFilesInDir(DirPath, ExtList, Args):
|
---|
47 |
|
---|
48 | FileList = []
|
---|
49 | ExcludeDir = DirPath
|
---|
50 | for DirPath, DirNames, FileNames in os.walk(DirPath):
|
---|
51 | if Args.Exclude:
|
---|
52 | DirNames[:] = [d for d in DirNames if d not in Args.Exclude]
|
---|
53 | FileNames[:] = [f for f in FileNames if f not in Args.Exclude]
|
---|
54 | Continue = False
|
---|
55 | for Path in Args.Exclude:
|
---|
56 | Path = Path.strip('\\').strip('/')
|
---|
57 | if not os.path.isdir(Path) and not os.path.isfile(Path):
|
---|
58 | Path = os.path.join(ExcludeDir, Path)
|
---|
59 | if os.path.isdir(Path) and Path.endswith(DirPath):
|
---|
60 | DirNames[:] = []
|
---|
61 | Continue = True
|
---|
62 | elif os.path.isfile(Path):
|
---|
63 | FilePaths = FileNames
|
---|
64 | for ItemPath in FilePaths:
|
---|
65 | FilePath = os.path.join(DirPath, ItemPath)
|
---|
66 | if Path.endswith(FilePath):
|
---|
67 | FileNames.remove(ItemPath)
|
---|
68 | if Continue:
|
---|
69 | continue
|
---|
70 | for FileName in [f for f in FileNames if any(f.endswith(ext) for ext in ExtList)]:
|
---|
71 | FileList.append(os.path.join(DirPath, FileName))
|
---|
72 | for File in FileList:
|
---|
73 | FormatFile(File, Args)
|
---|
74 |
|
---|
75 | if __name__ == "__main__":
|
---|
76 | parser = argparse.ArgumentParser(prog=__prog__, description=__description__ + __copyright__, conflict_handler = 'resolve')
|
---|
77 |
|
---|
78 | parser.add_argument('Path', nargs='+',
|
---|
79 | help='the path for files to be converted.It could be directory or file path.')
|
---|
80 | parser.add_argument('--version', action='version', version=__version__)
|
---|
81 | parser.add_argument('--append-extensions', dest='AppendExt', nargs='+',
|
---|
82 | help='append file extensions filter to default extensions. (Example: .txt .c .h)')
|
---|
83 | parser.add_argument('--override-extensions', dest='OverrideExt', nargs='+',
|
---|
84 | help='override file extensions filter on default extensions. (Example: .txt .c .h)')
|
---|
85 | parser.add_argument('-v', '--verbose', dest='Verbose', action='store_true',
|
---|
86 | help='increase output messages')
|
---|
87 | parser.add_argument('-q', '--quiet', dest='Quiet', action='store_true',
|
---|
88 | help='reduce output messages')
|
---|
89 | parser.add_argument('--debug', dest='Debug', type=int, metavar='[0-9]', choices=range(0, 10), default=0,
|
---|
90 | help='set debug level')
|
---|
91 | parser.add_argument('--exclude', dest='Exclude', nargs='+', help="directory name or file name which will be excluded")
|
---|
92 | args = parser.parse_args()
|
---|
93 | DefaultExt = copy.copy(DEFAULT_EXT_LIST)
|
---|
94 |
|
---|
95 | if args.OverrideExt is not None:
|
---|
96 | DefaultExt = args.OverrideExt
|
---|
97 | if args.AppendExt is not None:
|
---|
98 | DefaultExt = list(set(DefaultExt + args.AppendExt))
|
---|
99 |
|
---|
100 | for Path in args.Path:
|
---|
101 | if not os.path.exists(Path):
|
---|
102 | print("not exists path: {0}".format(Path))
|
---|
103 | sys.exit(1)
|
---|
104 | if os.path.isdir(Path):
|
---|
105 | FormatFilesInDir(Path, DefaultExt, args)
|
---|
106 | elif os.path.isfile(Path):
|
---|
107 | FormatFile(Path, args)
|
---|