VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/BaseTools/Scripts/FormatDosFiles.py@ 80924

Last change on this file since 80924 was 80721, checked in by vboxsync, 6 years ago

Devices/EFI/FirmwareNew: Start upgrade process to edk2-stable201908 (compiles on Windows and works to some extent), bugref:4643

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
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#
13from __future__ import print_function
14import argparse
15import os
16import os.path
17import re
18import sys
19import 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'
25DEFAULT_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.
29def 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
46def 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 if not os.path.isdir(Path) and not os.path.isfile(Path):
57 Path = os.path.join(ExcludeDir, Path)
58 if os.path.isdir(Path) and Path.endswith(DirPath):
59 DirNames[:] = []
60 Continue = True
61 elif os.path.isfile(Path):
62 FilePaths = FileNames
63 for ItemPath in FilePaths:
64 FilePath = os.path.join(DirPath, ItemPath)
65 if Path.endswith(FilePath):
66 FileNames.remove(ItemPath)
67 if Continue:
68 continue
69 for FileName in [f for f in FileNames if any(f.endswith(ext) for ext in ExtList)]:
70 FileList.append(os.path.join(DirPath, FileName))
71 for File in FileList:
72 FormatFile(File, Args)
73
74if __name__ == "__main__":
75 parser = argparse.ArgumentParser(prog=__prog__, description=__description__ + __copyright__, conflict_handler = 'resolve')
76
77 parser.add_argument('Path', nargs='+',
78 help='the path for files to be converted.It could be directory or file path.')
79 parser.add_argument('--version', action='version', version=__version__)
80 parser.add_argument('--append-extensions', dest='AppendExt', nargs='+',
81 help='append file extensions filter to default extensions. (Example: .txt .c .h)')
82 parser.add_argument('--override-extensions', dest='OverrideExt', nargs='+',
83 help='override file extensions filter on default extensions. (Example: .txt .c .h)')
84 parser.add_argument('-v', '--verbose', dest='Verbose', action='store_true',
85 help='increase output messages')
86 parser.add_argument('-q', '--quiet', dest='Quiet', action='store_true',
87 help='reduce output messages')
88 parser.add_argument('--debug', dest='Debug', type=int, metavar='[0-9]', choices=range(0, 10), default=0,
89 help='set debug level')
90 parser.add_argument('--exclude', dest='Exclude', nargs='+', help="directory name or file name which will be excluded")
91 args = parser.parse_args()
92 DefaultExt = copy.copy(DEFAULT_EXT_LIST)
93
94 if args.OverrideExt is not None:
95 DefaultExt = args.OverrideExt
96 if args.AppendExt is not None:
97 DefaultExt = list(set(DefaultExt + args.AppendExt))
98
99 for Path in args.Path:
100 if not os.path.exists(Path):
101 print("not exists path: {0}".format(Path))
102 sys.exit(1)
103 if os.path.isdir(Path):
104 FormatFilesInDir(Path, DefaultExt, args)
105 elif os.path.isfile(Path):
106 FormatFile(Path, args)
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette