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