From nobody Fri Mar 29 08:05:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1528448680331381.60041540376596; Fri, 8 Jun 2018 02:04:40 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 63AED210D438D; Fri, 8 Jun 2018 02:04:39 -0700 (PDT) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id AA4612098C211 for ; Fri, 8 Jun 2018 02:04:38 -0700 (PDT) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Jun 2018 02:04:38 -0700 Received: from dongaogu-mobl.ccr.corp.intel.com ([10.239.196.172]) by orsmga001.jf.intel.com with ESMTP; 08 Jun 2018 02:04:37 -0700 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=dongao.guo@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,490,1520924400"; d="scan'208";a="62869938" From: Dongao Guo To: edk2-devel@lists.01.org Date: Fri, 8 Jun 2018 17:03:47 +0800 Message-Id: <1528448627-18648-1-git-send-email-dongao.guo@intel.com> X-Mailer: git-send-email 2.6.1.windows.1 Subject: [edk2] [patch v3] Formalize source files to follow DOS format X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Liming Gao V3: support exclude dir and file by name while traversing the directory. remove close in with statement. V2: add version,description,copyright. add flag -v,-q,--append-extensions,--override-extensions,--debug. -q will omit default output,-v and --debug are not implemented. add default file extensions. support input of file path. support muliple input path. simplify comment. change 'pattern'.encode() to b'pattern',I think this will be better. change naming of variable and function to keep the same with BinToPcd.py V1: FormatDosFiles.py is added to clean up dos source files. It bases on the rules defined in EDKII C Coding Standards Specification. 5.1.2 Do not use tab characters 5.1.6 Only use CRLF (Carriage Return Line Feed) line endings. 5.1.7 All files must end with CRLF No trailing white space in one line. (To be added in spec) The source files in edk2 project with the below postfix are dos format. .h .c .nasm .nasmb .asm .S .inf .dec .dsc .fdf .uni .asl .aslc .vfr .idf .txt .bat .py The package maintainer can use this script to clean up all files in his package. The prefer way is to create one patch per one package. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Liming Gao Signed-off-by: Dongao Guo --- BaseTools/Scripts/FormatDosFiles.py | 95 +++++++++++++++++++++++++++++++++= ++++ 1 file changed, 95 insertions(+) create mode 100644 BaseTools/Scripts/FormatDosFiles.py diff --git a/BaseTools/Scripts/FormatDosFiles.py b/BaseTools/Scripts/Format= DosFiles.py new file mode 100644 index 0000000..4c75876 --- /dev/null +++ b/BaseTools/Scripts/FormatDosFiles.py @@ -0,0 +1,95 @@ +# @file FormatDosFiles.py +# This script format the source files to follow dos style. +# It supports Python2.x and Python3.x both. +# +# Copyright (c) 2018, Intel Corporation. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may = be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +# + +# +# Import Modules +# +import argparse +import os +import os.path +import re +import sys +import copy + +__prog__ =3D 'FormatDosFiles' +__version__ =3D '%s Version %s' % (__prog__, '0.12 ') +__copyright__ =3D 'Copyright (c) 2018 3, Intel Corporation. All rights r= eserved.' +__description__ =3D 'Convert source files to meet the EDKII C Coding Stand= ards Specification.\n' +DEFAULT_EXT_LIST =3D ['.h', '.c', '.nasm', '.nasmb', '.asm', '.S', '.inf',= '.dec', '.dsc', '.fdf', '.uni', '.asl', '.aslc', '.vfr', '.idf', '.txt', '= .bat', '.py'] + +#For working in python2 and python3 environment, re pattern should use bin= ary string, which is bytes type in python3. +#Because in python3,read from file in binary mode will return bytes type,a= nd in python3 bytes type can not be mixed with str type. +def FormatFile(FilePath, Args): + with open(FilePath, 'rb') as Fd: + Content =3D Fd.read() + # Convert the line endings to CRLF + Content =3D re.sub(br'([^\r])\n', br'\1\r\n', Content) + Content =3D re.sub(br'^\n', br'\r\n', Content, flags=3Dre.MULTILIN= E) + # Add a new empty line if the file is not end with one + Content =3D re.sub(br'([^\r\n])$', br'\1\r\n', Content) + # Remove trailing white spaces + Content =3D re.sub(br'[ \t]+(\r\n)', br'\1', Content, flags=3Dre.M= ULTILINE) + # Replace '\t' with two spaces + Content =3D re.sub(b'\t', b' ', Content) + with open(FilePath, 'wb') as Fd: + Fd.write(Content) + if not Args.Quiet: + print(FilePath) + +def FormatFilesInDir(DirPath, ExtList, Args): + + FileList =3D [] + for DirPath, DirNames, FileNames in os.walk(DirPath): + if Args.Exclude: + DirNames[:] =3D [d for d in DirNames if d not in Args.Exclude] + FileNames[:] =3D [f for f in FileNames if f not in Args.Exclud= e] + for FileName in [f for f in FileNames if any(f.endswith(ext) for e= xt in ExtList)]: + FileList.append(os.path.join(DirPath, FileName)) + for File in FileList: + FormatFile(File, Args) + +if __name__ =3D=3D "__main__": + parser =3D argparse.ArgumentParser(prog=3D__prog__,description=3D__des= cription__ + __copyright__, conflict_handler =3D 'resolve') + + parser.add_argument('Path', nargs=3D'+', + help=3D'the path for files to be converted.It coul= d be directory or file path.') + parser.add_argument('--version', action=3D'version', version=3D__versi= on__) + parser.add_argument('--append-extensions', dest=3D'AppendExt', nargs= =3D'+', + help=3D'append file extensions filter to default e= xtensions. (Example: .txt .c .h)') + parser.add_argument('--override-extensions', dest=3D'OverrideExt', nar= gs=3D'+', + help=3D'override file extensions filter on default= extensions. (Example: .txt .c .h)') + parser.add_argument('-v', '--verbose', dest=3D'Verbose', action=3D'sto= re_true', + help=3D'increase output messages') + parser.add_argument('-q', '--quiet', dest=3D'Quiet', action=3D'store_t= rue', + help=3D'reduce output messages') + parser.add_argument('--debug', dest=3D'Debug', type=3Dint, metavar=3D'= [0-9]', choices=3Drange(0, 10), default=3D0, + help=3D'set debug level') + parser.add_argument('--exclude', dest=3D'Exclude', nargs=3D'+', help= =3D"directory name or file name which will be excluded") + args =3D parser.parse_args() + DefaultExt =3D copy.copy(DEFAULT_EXT_LIST) + + if args.OverrideExt is not None: + DefaultExt =3D args.OverrideExt + if args.AppendExt is not None: + DefaultExt =3D list(set(DefaultExt + args.AppendExt)) + + for Path in args.Path: + if not os.path.exists(Path): + print("not exists path: {0}".format(Path)) + sys.exit(1) + if os.path.isdir(Path): + FormatFilesInDir(Path, DefaultExt, args) + elif os.path.isfile(Path): + FormatFile(Path, args) --=20 2.6.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel