]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Scripts/FormatDosFiles.py
BaseTools:"--exclude" don't apply if parameter ends with separator
[mirror_edk2.git] / BaseTools / Scripts / FormatDosFiles.py
CommitLineData
c30084fb
LG
1# @file FormatDosFiles.py\r
2# This script format the source files to follow dos style.\r
3# It supports Python2.x and Python3.x both.\r
4#\r
5# Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>\r
6#\r
2e351cbe 7# SPDX-License-Identifier: BSD-2-Clause-Patent\r
c30084fb
LG
8#\r
9\r
10#\r
11# Import Modules\r
12#\r
72443dd2 13from __future__ import print_function\r
c30084fb
LG
14import argparse\r
15import os\r
16import os.path\r
17import re\r
18import sys\r
19import copy\r
20\r
21__prog__ = 'FormatDosFiles'\r
22__version__ = '%s Version %s' % (__prog__, '0.10 ')\r
35ec4180 23__copyright__ = 'Copyright (c) 2018-2019, Intel Corporation. All rights reserved.'\r
c30084fb
LG
24__description__ = 'Convert source files to meet the EDKII C Coding Standards Specification.\n'\r
25DEFAULT_EXT_LIST = ['.h', '.c', '.nasm', '.nasmb', '.asm', '.S', '.inf', '.dec', '.dsc', '.fdf', '.uni', '.asl', '.aslc', '.vfr', '.idf', '.txt', '.bat', '.py']\r
26\r
27#For working in python2 and python3 environment, re pattern should use binary string, which is bytes type in python3.\r
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.\r
29def FormatFile(FilePath, Args):\r
30 with open(FilePath, 'rb') as Fd:\r
31 Content = Fd.read()\r
32 # Convert the line endings to CRLF\r
33 Content = re.sub(br'([^\r])\n', br'\1\r\n', Content)\r
34 Content = re.sub(br'^\n', br'\r\n', Content, flags=re.MULTILINE)\r
35 # Add a new empty line if the file is not end with one\r
36 Content = re.sub(br'([^\r\n])$', br'\1\r\n', Content)\r
37 # Remove trailing white spaces\r
38 Content = re.sub(br'[ \t]+(\r\n)', br'\1', Content, flags=re.MULTILINE)\r
39 # Replace '\t' with two spaces\r
40 Content = re.sub(b'\t', b' ', Content)\r
41 with open(FilePath, 'wb') as Fd:\r
42 Fd.write(Content)\r
43 if not Args.Quiet:\r
44 print(FilePath)\r
45\r
46def FormatFilesInDir(DirPath, ExtList, Args):\r
47\r
48 FileList = []\r
35ec4180 49 ExcludeDir = DirPath\r
c30084fb
LG
50 for DirPath, DirNames, FileNames in os.walk(DirPath):\r
51 if Args.Exclude:\r
52 DirNames[:] = [d for d in DirNames if d not in Args.Exclude]\r
53 FileNames[:] = [f for f in FileNames if f not in Args.Exclude]\r
35ec4180
FZ
54 Continue = False\r
55 for Path in Args.Exclude:\r
6da405eb 56 Path = Path.strip('\\').strip('/')\r
35ec4180
FZ
57 if not os.path.isdir(Path) and not os.path.isfile(Path):\r
58 Path = os.path.join(ExcludeDir, Path)\r
59 if os.path.isdir(Path) and Path.endswith(DirPath):\r
60 DirNames[:] = []\r
61 Continue = True\r
62 elif os.path.isfile(Path):\r
63 FilePaths = FileNames\r
64 for ItemPath in FilePaths:\r
65 FilePath = os.path.join(DirPath, ItemPath)\r
66 if Path.endswith(FilePath):\r
67 FileNames.remove(ItemPath)\r
68 if Continue:\r
69 continue\r
c30084fb
LG
70 for FileName in [f for f in FileNames if any(f.endswith(ext) for ext in ExtList)]:\r
71 FileList.append(os.path.join(DirPath, FileName))\r
72 for File in FileList:\r
73 FormatFile(File, Args)\r
74\r
75if __name__ == "__main__":\r
ccaa7754 76 parser = argparse.ArgumentParser(prog=__prog__, description=__description__ + __copyright__, conflict_handler = 'resolve')\r
c30084fb
LG
77\r
78 parser.add_argument('Path', nargs='+',\r
79 help='the path for files to be converted.It could be directory or file path.')\r
80 parser.add_argument('--version', action='version', version=__version__)\r
81 parser.add_argument('--append-extensions', dest='AppendExt', nargs='+',\r
82 help='append file extensions filter to default extensions. (Example: .txt .c .h)')\r
83 parser.add_argument('--override-extensions', dest='OverrideExt', nargs='+',\r
84 help='override file extensions filter on default extensions. (Example: .txt .c .h)')\r
85 parser.add_argument('-v', '--verbose', dest='Verbose', action='store_true',\r
86 help='increase output messages')\r
87 parser.add_argument('-q', '--quiet', dest='Quiet', action='store_true',\r
88 help='reduce output messages')\r
89 parser.add_argument('--debug', dest='Debug', type=int, metavar='[0-9]', choices=range(0, 10), default=0,\r
90 help='set debug level')\r
91 parser.add_argument('--exclude', dest='Exclude', nargs='+', help="directory name or file name which will be excluded")\r
92 args = parser.parse_args()\r
93 DefaultExt = copy.copy(DEFAULT_EXT_LIST)\r
94\r
95 if args.OverrideExt is not None:\r
96 DefaultExt = args.OverrideExt\r
97 if args.AppendExt is not None:\r
98 DefaultExt = list(set(DefaultExt + args.AppendExt))\r
99\r
100 for Path in args.Path:\r
101 if not os.path.exists(Path):\r
102 print("not exists path: {0}".format(Path))\r
103 sys.exit(1)\r
104 if os.path.isdir(Path):\r
105 FormatFilesInDir(Path, DefaultExt, args)\r
106 elif os.path.isfile(Path):\r
107 FormatFile(Path, args)\r