From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga14.intel.com (mga14.intel.com []) by mx.groups.io with SMTP id smtpd.web11.5943.1610531774284313300 for ; Wed, 13 Jan 2021 01:56:17 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=fail (domain: intel.com, ip: , mailfrom: bob.c.feng@intel.com) IronPort-SDR: vBwy18wBYto/XGWMZuaKnKu9zyNOM7UAbYy/Qg7ArgPXJp6KC4OxocEDXFiRBKC11bRcBU7D4x ZdRknLFmbBfg== X-IronPort-AV: E=McAfee;i="6000,8403,9862"; a="177403469" X-IronPort-AV: E=Sophos;i="5.79,344,1602572400"; d="scan'208";a="177403469" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jan 2021 01:56:17 -0800 IronPort-SDR: sCXN/MYHlUuZOWAOoInyv9OJIYAzTa8mMaeDLbDarS76H23tR7CE8kVaWY7PqX4G4gjpMgJu4Y mIzvgCT4Oh+w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.79,344,1602572400"; d="scan'208";a="464845012" Received: from shwdepsi1121.ccr.corp.intel.com ([10.239.158.32]) by fmsmga001.fm.intel.com with ESMTP; 13 Jan 2021 01:56:15 -0800 From: "Bob Feng" To: devel@edk2.groups.io Cc: Liming Gao , Yuwei Chen Subject: [Patch 1/2] BaseTools: Convert Split tool to python Date: Wed, 13 Jan 2021 17:56:08 +0800 Message-Id: <20210113095609.1398-2-bob.c.feng@intel.com> X-Mailer: git-send-email 2.29.1.windows.1 In-Reply-To: <20210113095609.1398-1-bob.c.feng@intel.com> References: <20210113095609.1398-1-bob.c.feng@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable There are 2 reasons to convert Split tool from C to Python.=0D 1. We are in the process of moving the Basetools Python code=0D to a separate repository. But there still are many C tools under=0D edk2/BaseTools. To make all Basetools be in the separate repo,=0D we can convert the C tools to Python tools.=0D 2. The original Split tool is very slow. This python tool can reduce=0D 90% time. Signed-off-by: Bob Feng =0D Cc: Liming Gao =0D Cc: Yuwei Chen =0D --- BaseTools/Source/C/Split/Split.c | 466 -------------------- BaseTools/BinWrappers/PosixLike/Split | 31 +- BaseTools/BinWrappers/WindowsLike/Split.bat | 3 + BaseTools/Source/C/GNUmakefile | 1 - BaseTools/Source/C/Makefile | 1 - BaseTools/Source/C/Split/GNUmakefile | 17 - BaseTools/Source/C/Split/Makefile | 16 - BaseTools/Source/Python/Split/Split.py | 203 +++++++++ BaseTools/Source/Python/Split/__init__.py | 10 + 9 files changed, 224 insertions(+), 524 deletions(-) delete mode 100644 BaseTools/Source/C/Split/Split.c create mode 100644 BaseTools/BinWrappers/WindowsLike/Split.bat delete mode 100644 BaseTools/Source/C/Split/GNUmakefile delete mode 100644 BaseTools/Source/C/Split/Makefile create mode 100644 BaseTools/Source/Python/Split/Split.py create mode 100644 BaseTools/Source/Python/Split/__init__.py diff --git a/BaseTools/Source/C/Split/Split.c b/BaseTools/Source/C/Split/Sp= lit.c deleted file mode 100644 index be0ee124bfe0..000000000000 --- a/BaseTools/Source/C/Split/Split.c +++ /dev/null @@ -1,466 +0,0 @@ -/** @file=0D -=0D - Split a file into two pieces at the request offset.=0D -=0D -Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.
=0D -SPDX-License-Identifier: BSD-2-Clause-Patent=0D -=0D -**/=0D -=0D -// GC_TODO: fix comment to start with /*++=0D -#include =0D -#include =0D -#include =0D -#ifdef __GNUC__=0D -#include =0D -#else=0D -#include =0D -#endif=0D -#include =0D -#include "ParseInf.h"=0D -#include "CommonLib.h"=0D -#include "EfiUtilityMsgs.h"=0D -//=0D -// Utility Name=0D -//=0D -#define UTILITY_NAME "Split"=0D -=0D -//=0D -// Utility version information=0D -//=0D -#define UTILITY_MAJOR_VERSION 1=0D -#define UTILITY_MINOR_VERSION 0=0D -=0D -void=0D -Version (=0D - void=0D - )=0D -/*++=0D -=0D -Routine Description:=0D -=0D - Displays the standard utility information to SDTOUT=0D -=0D -Arguments:=0D -=0D - None=0D -=0D -Returns:=0D -=0D - None=0D -=0D ---*/=0D -{=0D - printf ("%s Version %d.%d Build %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSI= ON, UTILITY_MINOR_VERSION, __BUILD_VERSION);=0D -}=0D -=0D -void=0D -Usage (=0D - void=0D - )=0D -/*++=0D -=0D -Routine Description:=0D -=0D - GC_TODO: Add function description=0D -=0D -Arguments:=0D -=0D -=0D -Returns:=0D -=0D - GC_TODO: add return values=0D -=0D ---*/=0D -{=0D - Version();=0D - printf ("Copyright (c) 1999-2017 Intel Corporation. All rights reserved.= \n");=0D - printf ("\n SplitFile creates two Binary files either in the same direc= tory as the current working\n");=0D - printf (" directory or in the specified directory.\n");=0D - printf ("\nUsage: \n\=0D - Split\n\=0D - -f, --filename inputFile to split\n\=0D - -s, --split VALUE the number of bytes in the first file\n\=0D - [-p, --prefix OutputDir]\n\=0D - [-o, --firstfile Filename1]\n\=0D - [-t, --secondfile Filename2]\n\=0D - [-v, --verbose]\n\=0D - [--version]\n\=0D - [-q, --quiet disable all messages except fatal errors]\n\=0D - [-d, --debug[#]\n\=0D - [-h, --help]\n");=0D -}=0D -=0D -EFI_STATUS=0D -GetSplitValue (=0D - IN CONST CHAR8* SplitValueString,=0D - OUT UINT64 *ReturnValue=0D -)=0D -{=0D - UINT64 len =3D 0;=0D - UINT64 base =3D 1;=0D - UINT64 index =3D 0;=0D - UINT64 number =3D 0;=0D - CHAR8 lastCHAR =3D 0;=0D - EFI_STATUS Status =3D EFI_SUCCESS;=0D -=0D - if (SplitValueString !=3D NULL){=0D - len =3D strlen(SplitValueString);=0D - }=0D -=0D - if (len =3D=3D 0) {=0D - return EFI_ABORTED;=0D - }=0D -=0D - Status =3D AsciiStringToUint64 (SplitValueString, FALSE, ReturnValue);=0D - if (!EFI_ERROR (Status)) {=0D - return Status;=0D - }=0D -=0D - if (SplitValueString[0] =3D=3D '0' && (SplitValueString[1] =3D=3D 'x' ||= SplitValueString[1] =3D=3D 'X')) {=0D - Status =3D AsciiStringToUint64 (SplitValueString, TRUE, ReturnValue);= =0D - if (!EFI_ERROR (Status)) {=0D - return Status;=0D - }=0D - }=0D -=0D - lastCHAR =3D (CHAR8)toupper((int)SplitValueString[len - 1]);=0D -=0D - if (lastCHAR !=3D 'K' && lastCHAR !=3D 'M' && lastCHAR !=3D 'G') {=0D - return STATUS_ERROR;=0D - }=0D -=0D - for (;index < len - 1; ++index) {=0D - if (!isdigit((int)SplitValueString[index])) {=0D - return EFI_ABORTED;=0D - }=0D - }=0D -=0D - number =3D atol (SplitValueString);=0D - if (lastCHAR =3D=3D 'K')=0D - base =3D 1024;=0D - else if (lastCHAR =3D=3D 'M')=0D - base =3D 1024*1024;=0D - else=0D - base =3D 1024*1024*1024;=0D -=0D - *ReturnValue =3D number*base;=0D -=0D - return EFI_SUCCESS;=0D -}=0D -=0D -EFI_STATUS=0D -CountVerboseLevel (=0D - IN CONST CHAR8* VerboseLevelString,=0D - IN CONST UINT64 Length,=0D - OUT UINT64 *ReturnValue=0D -)=0D -{=0D - UINT64 i =3D 0;=0D - for (;i < Length; ++i) {=0D - if (VerboseLevelString[i] !=3D 'v' && VerboseLevelString[i] !=3D 'V') = {=0D - return EFI_ABORTED;=0D - }=0D - ++(*ReturnValue);=0D - }=0D -=0D - return EFI_SUCCESS;=0D -}=0D -=0D -EFI_STATUS=0D -CreateDir (=0D - IN OUT CHAR8** FullFileName=0D -)=0D -{=0D - CHAR8* temp =3D *FullFileName;=0D - CHAR8* start =3D temp;=0D - CHAR8 tempchar;=0D - UINT64 index =3D 0;=0D -=0D - for (;index < strlen(temp); ++index) {=0D - if (temp[index] =3D=3D '\\' || temp[index] =3D=3D '/') {=0D - if (temp[index + 1] !=3D '\0') {=0D - tempchar =3D temp[index + 1];=0D - temp[index + 1] =3D 0;=0D - if (chdir(start)) {=0D - if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) !=3D 0) {=0D - return EFI_ABORTED;=0D - }=0D - chdir(start);=0D - }=0D - start =3D temp + index + 1;=0D - temp[index] =3D '/';=0D - temp[index + 1] =3D tempchar;=0D - }=0D - }=0D - }=0D -=0D - return EFI_SUCCESS;=0D -}=0D -=0D -int=0D -main (=0D - int argc,=0D - char*argv[]=0D - )=0D -/*++=0D -=0D -Routine Description:=0D -=0D - GC_TODO: Add function description=0D -=0D -Arguments:=0D -=0D - argc - GC_TODO: add argument description=0D - ] - GC_TODO: add argument description=0D -=0D -Returns:=0D -=0D - GC_TODO: add return values=0D -=0D ---*/=0D -{=0D - EFI_STATUS Status =3D EFI_SUCCESS;=0D - INTN ReturnStatus =3D STATUS_SUCCESS;=0D - FILE *In;=0D - CHAR8 *InputFileName =3D NULL;=0D - CHAR8 *OutputDir =3D NULL;=0D - CHAR8 *OutFileName1 =3D NULL;=0D - CHAR8 *OutFileName2 =3D NULL;=0D - UINT64 SplitValue =3D (UINT64) -1;=0D - FILE *Out1 =3D NULL;=0D - FILE *Out2 =3D NULL;=0D - CHAR8 *OutName1 =3D NULL;=0D - CHAR8 *OutName2 =3D NULL;=0D - CHAR8 *CurrentDir =3D NULL;=0D - UINT64 Index;=0D - CHAR8 CharC;=0D - UINT64 DebugLevel =3D 0;=0D - UINT64 VerboseLevel =3D 0;=0D -=0D - SetUtilityName(UTILITY_NAME);=0D - if (argc =3D=3D 1) {=0D - Usage();=0D - return STATUS_ERROR;=0D - }=0D -=0D - argc --;=0D - argv ++;=0D -=0D - if ((stricmp (argv[0], "-h") =3D=3D 0) || (stricmp (argv[0], "--help") = =3D=3D 0)) {=0D - Usage();=0D - return STATUS_SUCCESS;=0D - }=0D -=0D - if (stricmp (argv[0], "--version") =3D=3D 0) {=0D - Version();=0D - return STATUS_SUCCESS;=0D - }=0D -=0D - while (argc > 0) {=0D - if ((stricmp (argv[0], "-p") =3D=3D 0) || (stricmp (argv[0], "--prefix= ") =3D=3D 0)) {=0D - OutputDir =3D argv[1];=0D - if (OutputDir =3D=3D NULL) {=0D - Warning (NULL, 0, 0, "NO output directory specified.", NULL);=0D - return STATUS_ERROR;=0D - }=0D - argc -=3D 2;=0D - argv +=3D 2;=0D - continue;=0D - }=0D -=0D - if ((stricmp (argv[0], "-f") =3D=3D 0) || (stricmp (argv[0], "--filena= me") =3D=3D 0)) {=0D - InputFileName =3D argv[1];=0D - if (InputFileName =3D=3D NULL) {=0D - Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);=0D - return STATUS_ERROR;=0D - }=0D - argc -=3D 2;=0D - argv +=3D 2;=0D - continue;=0D - }=0D -=0D - if ((stricmp (argv[0], "-s") =3D=3D 0) || (stricmp (argv[0], "--split"= ) =3D=3D 0)) {=0D - Status =3D GetSplitValue(argv[1], &SplitValue);=0D - if (EFI_ERROR (Status)) {=0D - Error (NULL, 0, 0x1003, "Input split value is not one valid intege= r.", NULL);=0D - return STATUS_ERROR;=0D - }=0D - argc -=3D 2;=0D - argv +=3D 2;=0D - continue;=0D - }=0D -=0D - if ((stricmp (argv[0], "-o") =3D=3D 0) || (stricmp (argv[0], "--firstf= ile") =3D=3D 0)) {=0D - OutFileName1 =3D argv[1];=0D - if (OutFileName1 =3D=3D NULL) {=0D - Warning (NULL, 0, 0, NULL, "No output file1 specified.");=0D - }=0D - argc -=3D 2;=0D - argv +=3D 2;=0D - continue;=0D - }=0D -=0D - if ((stricmp (argv[0], "-t") =3D=3D 0) || (stricmp (argv[0], "--second= file") =3D=3D 0)) {=0D - OutFileName2 =3D argv[1];=0D - if (OutFileName2 =3D=3D NULL) {=0D - Warning (NULL, 0, 0, NULL, "No output file2 specified.");=0D - }=0D - argc -=3D 2;=0D - argv +=3D 2;=0D - continue;=0D - }=0D -=0D - if ((stricmp (argv[0], "-q") =3D=3D 0) || (stricmp (argv[0], "--quiet"= ) =3D=3D 0)) {=0D - argc --;=0D - argv ++;=0D - continue;=0D - }=0D -=0D - if ((strlen(argv[0]) >=3D 2 && argv[0][0] =3D=3D '-' && (argv[0][1] = =3D=3D 'v' || argv[0][1] =3D=3D 'V')) || (stricmp (argv[0], "--verbose") = =3D=3D 0)) {=0D - VerboseLevel =3D 1;=0D - if (strlen(argv[0]) > 2) {=0D - Status =3D CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &V= erboseLevel);=0D - if (EFI_ERROR (Status)) {=0D - Error (NULL, 0, 0x1003, NULL, "%s is invalid parameter!", argv[0= ]);=0D - return STATUS_ERROR;=0D - }=0D - }=0D -=0D - argc --;=0D - argv ++;=0D - continue;=0D - }=0D -=0D - if ((stricmp (argv[0], "-d") =3D=3D 0) || (stricmp (argv[0], "--debug"= ) =3D=3D 0)) {=0D - Status =3D AsciiStringToUint64 (argv[1], FALSE, &DebugLevel);=0D - if (EFI_ERROR (Status)) {=0D - Error (NULL, 0, 0x1003, "Input debug level is not one valid integr= ator.", NULL);=0D - return STATUS_ERROR;=0D - }=0D - argc -=3D 2;=0D - argv +=3D 2;=0D - continue;=0D - }=0D - //=0D - // Don't recognize the parameter.=0D - //=0D - Error (NULL, 0, 0x1003, NULL, "%s is invalid parameter!", argv[0]);=0D - return STATUS_ERROR;=0D - }=0D -=0D - if (InputFileName =3D=3D NULL) {=0D - Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);=0D - return STATUS_ERROR;=0D - }=0D -=0D - In =3D fopen (LongFilePath (InputFileName), "rb");=0D - if (In =3D=3D NULL) {=0D - // ("Unable to open file \"%s\"\n", InputFileName);=0D - Error (InputFileName, 0, 1, "File open failure", NULL);=0D - return STATUS_ERROR;=0D - }=0D -=0D - if (OutFileName1 =3D=3D NULL) {=0D - OutName1 =3D (CHAR8*)malloc(strlen(InputFileName) + 16);=0D - if (OutName1 =3D=3D NULL) {=0D - Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");=0D - ReturnStatus =3D STATUS_ERROR;=0D - goto Finish;=0D - }=0D - strcpy (OutName1, InputFileName);=0D - strcat (OutName1, "1");=0D - OutFileName1 =3D OutName1;=0D -=0D - }=0D - if (OutFileName2 =3D=3D NULL) {=0D - OutName2 =3D (CHAR8*)malloc(strlen(InputFileName) + 16);=0D - if (OutName2 =3D=3D NULL) {=0D - Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");=0D - ReturnStatus =3D STATUS_ERROR;=0D - goto Finish;=0D - }=0D - strcpy (OutName2, InputFileName);=0D - strcat (OutName2, "2");=0D - OutFileName2 =3D OutName2;=0D -=0D - }=0D -=0D - if (OutputDir !=3D NULL) {=0D - //OutputDirSpecified =3D TRUE;=0D - if (chdir(OutputDir) !=3D 0) {=0D - Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");=0D - ReturnStatus =3D STATUS_ERROR;=0D - goto Finish;=0D - }=0D - }=0D -=0D - CurrentDir =3D (CHAR8*)getcwd((CHAR8*)0, 0);=0D - if (EFI_ERROR(CreateDir(&OutFileName1))) {=0D - Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);=0D - ReturnStatus =3D STATUS_ERROR;=0D - goto Finish;=0D - }=0D - chdir(CurrentDir);=0D -=0D - if (EFI_ERROR(CreateDir(&OutFileName2))) {=0D - Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);=0D - ReturnStatus =3D STATUS_ERROR;=0D - goto Finish;=0D - }=0D - chdir(CurrentDir);=0D - free(CurrentDir);=0D -=0D - Out1 =3D fopen (LongFilePath (OutFileName1), "wb");=0D - if (Out1 =3D=3D NULL) {=0D - // ("Unable to open file \"%s\"\n", OutFileName1);=0D - Error (OutFileName1, 0, 1, "File open failure", NULL);=0D - ReturnStatus =3D STATUS_ERROR;=0D - goto Finish;=0D - }=0D -=0D - Out2 =3D fopen (LongFilePath (OutFileName2), "wb");=0D - if (Out2 =3D=3D NULL) {=0D - // ("Unable to open file \"%s\"\n", OutFileName2);=0D - Error (OutFileName2, 0, 1, "File open failure", NULL);=0D - ReturnStatus =3D STATUS_ERROR;=0D - goto Finish;=0D - }=0D -=0D - for (Index =3D 0; Index < SplitValue; Index++) {=0D - CharC =3D (CHAR8) fgetc (In);=0D - if (feof (In)) {=0D - break;=0D - }=0D -=0D - fputc (CharC, Out1);=0D - }=0D -=0D - for (;;) {=0D - CharC =3D (CHAR8) fgetc (In);=0D - if (feof (In)) {=0D - break;=0D - }=0D -=0D - fputc (CharC, Out2);=0D - }=0D -=0D -Finish:=0D - if (OutName1 !=3D NULL) {=0D - free(OutName1);=0D - }=0D - if (OutName2 !=3D NULL) {=0D - free(OutName2);=0D - }=0D - if (In !=3D NULL) {=0D - fclose (In);=0D - }=0D - if (Out1 !=3D NULL) {=0D - fclose (Out1);=0D - }=0D - if (Out2 !=3D NULL) {=0D - fclose (Out2);=0D - }=0D -=0D - return ReturnStatus;=0D -}=0D diff --git a/BaseTools/BinWrappers/PosixLike/Split b/BaseTools/BinWrappers/= PosixLike/Split index 0945d86d9209..f3770eed42b4 100755 --- a/BaseTools/BinWrappers/PosixLike/Split +++ b/BaseTools/BinWrappers/PosixLike/Split @@ -1,29 +1,14 @@ #!/usr/bin/env bash +#python `dirname $0`/RunToolFromSource.py `basename $0` $* + +# If a ${PYTHON_COMMAND} command is available, use it in preference to pyt= hon +if command -v ${PYTHON_COMMAND} >/dev/null 2>&1; then + python_exe=3D${PYTHON_COMMAND} +fi =20 full_cmd=3D${BASH_SOURCE:-$0} # see http://mywiki.wooledge.org/BashFAQ/028= for a discussion of why $0 is not a good choice here dir=3D$(dirname "$full_cmd") cmd=3D${full_cmd##*/} =20 -if [ -n "$WORKSPACE" ] && [ -e "$WORKSPACE/Conf/BaseToolsCBinaries" ] -then - exec "$WORKSPACE/Conf/BaseToolsCBinaries/$cmd" -elif [ -n "$WORKSPACE" ] && [ -e "$EDK_TOOLS_PATH/Source/C" ] -then - if [ ! -e "$EDK_TOOLS_PATH/Source/C/bin/$cmd" ] - then - echo "BaseTools C Tool binary was not found ($cmd)" - echo "You may need to run:" - echo " make -C $EDK_TOOLS_PATH/Source/C" - else - exec "$EDK_TOOLS_PATH/Source/C/bin/$cmd" "$@" - fi -elif [ -e "$dir/../../Source/C/bin/$cmd" ] -then - exec "$dir/../../Source/C/bin/$cmd" "$@" -else - echo "Unable to find the real '$cmd' to run" - echo "This message was printed by" - echo " $0" - exit 127 -fi - +export PYTHONPATH=3D"$dir/../../Source/Python${PYTHONPATH:+:"$PYTHONPATH"}" +exec "${python_exe:-python}" "$dir/../../Source/Python/$cmd/$cmd.py" "$@" diff --git a/BaseTools/BinWrappers/WindowsLike/Split.bat b/BaseTools/BinWra= ppers/WindowsLike/Split.bat new file mode 100644 index 000000000000..9616cd893bec --- /dev/null +++ b/BaseTools/BinWrappers/WindowsLike/Split.bat @@ -0,0 +1,3 @@ +@setlocal=0D +@set ToolName=3D%~n0%=0D +@%PYTHON_COMMAND% %BASE_TOOLS_PATH%\Source\Python\%ToolName%\%ToolName%.py= %*=0D diff --git a/BaseTools/Source/C/GNUmakefile b/BaseTools/Source/C/GNUmakefile index 464f43277455..8c191e0c3817 100644 --- a/BaseTools/Source/C/GNUmakefile +++ b/BaseTools/Source/C/GNUmakefile @@ -55,11 +55,10 @@ APPLICATIONS =3D \ GenFv \=0D GenFw \=0D GenSec \=0D GenCrc32 \=0D LzmaCompress \=0D - Split \=0D TianoCompress \=0D VolInfo \=0D DevicePath=0D =0D SUBDIRS :=3D $(LIBRARIES) $(APPLICATIONS)=0D diff --git a/BaseTools/Source/C/Makefile b/BaseTools/Source/C/Makefile index e8f8abe59a79..a376d32e220e 100644 --- a/BaseTools/Source/C/Makefile +++ b/BaseTools/Source/C/Makefile @@ -17,11 +17,10 @@ APPLICATIONS =3D \ GenFfs \=0D GenFv \=0D GenFw \=0D GenSec \=0D LzmaCompress \=0D - Split \=0D TianoCompress \=0D VolInfo \=0D DevicePath=0D =0D all: libs apps install=0D diff --git a/BaseTools/Source/C/Split/GNUmakefile b/BaseTools/Source/C/Spli= t/GNUmakefile deleted file mode 100644 index b3d4dff51ac1..000000000000 --- a/BaseTools/Source/C/Split/GNUmakefile +++ /dev/null @@ -1,17 +0,0 @@ -## @file=0D -# GNU/Linux makefile for 'Split' module build.=0D -#=0D -# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
=0D -# SPDX-License-Identifier: BSD-2-Clause-Patent=0D -#=0D -MAKEROOT ?=3D ..=0D -=0D -APPNAME =3D Split=0D -=0D -OBJECTS =3D Split.o=0D -=0D -include $(MAKEROOT)/Makefiles/app.makefile=0D -=0D -LIBS =3D -lCommon=0D -=0D -=0D diff --git a/BaseTools/Source/C/Split/Makefile b/BaseTools/Source/C/Split/M= akefile deleted file mode 100644 index 19d3e31a7624..000000000000 --- a/BaseTools/Source/C/Split/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -## @file=0D -# Windows makefile for 'Split' module build.=0D -#=0D -# Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.
=0D -# SPDX-License-Identifier: BSD-2-Clause-Patent=0D -#=0D -!INCLUDE ..\Makefiles\ms.common=0D -=0D -APPNAME =3D Split=0D -=0D -LIBS =3D $(LIB_PATH)\Common.lib=0D -=0D -OBJECTS =3D Split.obj=0D -=0D -!INCLUDE ..\Makefiles\ms.app=0D -=0D diff --git a/BaseTools/Source/Python/Split/Split.py b/BaseTools/Source/Pyth= on/Split/Split.py new file mode 100644 index 000000000000..39d4ee7689d6 --- /dev/null +++ b/BaseTools/Source/Python/Split/Split.py @@ -0,0 +1,203 @@ +=0D +# @file=0D +# Split a file into two pieces at the request offset.=0D +#=0D +# Copyright (c) 2021, Intel Corporation. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +# Import Modules=0D +#=0D +import argparse=0D +import os=0D +import io=0D +import shutil=0D +import logging=0D +import sys=0D +import tempfile=0D +=0D +parser =3D argparse.ArgumentParser(description=3D'''=0D +SplitFile creates two Binary files either in the same directory as the cur= rent working directory or in the specified directory.=0D +''')=0D +parser.add_argument("-f", "--filename", dest=3D"inputfile",=0D + required=3DTrue, help=3D"The input file to split tool.= ")=0D +parser.add_argument("-s", "--split", dest=3D"position",=0D + required=3DTrue, help=3D"The number of bytes in the fi= rst file. The valid format are HEX, Decimal and Decimal[KMG].")=0D +parser.add_argument("-p", "--prefix", dest=3D"output",=0D + help=3D"The output folder.")=0D +parser.add_argument("-o", "--firstfile", help=3D"The first file name")=0D +parser.add_argument("-t", "--secondfile", help=3D"The second file name")= =0D +parser.add_argument("--version", action=3D"version", version=3D'%(prog)s V= ersion 1.0',=0D + help=3D"Print debug information.")=0D +=0D +group =3D parser.add_mutually_exclusive_group()=0D +group.add_argument("-v", "--verbose", action=3D"store_true",=0D + help=3D"Print debug information.")=0D +group.add_argument("-q", "--quiet", action=3D"store_true",=0D + help=3D"Disable all messages except fatal errors")=0D +=0D +SizeDict =3D {=0D + "K": 1024,=0D + "M": 1024*1024,=0D + "G": 1024*1024*1024=0D +}=0D +=0D +=0D +def GetPositionValue(position):=0D + '''=0D + Parse the string of the argument position and return a decimal number.= =0D + The valid position formats are=0D + 1. HEX=0D + e.g. 0x1000 or 0X1000=0D + 2. Decimal=0D + e.g. 100=0D + 3. Decimal[KMG]=0D + e.g. 100K or 100M or 100G or 100k or 100m or 100g=0D + '''=0D + logger =3D logging.getLogger('Split')=0D + PosVal =3D 0=0D + header =3D position[:2].upper()=0D + tailer =3D position[-1].upper()=0D +=0D + try:=0D + if tailer in SizeDict:=0D + PosVal =3D int(position[:-1]) * SizeDict[tailer]=0D + else:=0D + if header =3D=3D "0X":=0D + PosVal =3D int(position, 16)=0D + else:=0D + PosVal =3D int(position)=0D + except Exception as e:=0D + logger.error(=0D + "The parameter %s format is incorrect. The valid format is HEX= , Decimal and Decimal[KMG]." % position)=0D + raise(e)=0D +=0D + return PosVal=0D +=0D +=0D +def getFileSize(filename):=0D + '''=0D + Read the input file and return the file size.=0D + '''=0D + logger =3D logging.getLogger('Split')=0D + length =3D 0=0D + try:=0D + with open(filename, "rb") as fin:=0D + fin.seek(0, io.SEEK_END)=0D + length =3D fin.tell()=0D + except Exception as e:=0D + logger.error("Access file failed: %s", filename)=0D + raise(e)=0D +=0D + return length=0D +=0D +=0D +def splitFile(inputfile, position, outputdir=3DNone, outputfile1=3DNone, o= utputfile2=3DNone):=0D + '''=0D + Split the inputfile into outputfile1 and outputfile2 from the position= .=0D + '''=0D + logger =3D logging.getLogger('Split')=0D +=0D + inputfile =3D os.path.abspath(inputfile)=0D + workspace =3D os.path.dirname(inputfile)=0D + if not os.path.exists(inputfile):=0D + logger.error("File Not Found: %s" % inputfile)=0D + raise(Exception)=0D +=0D + if outputfile1 and outputfile2 and outputfile1 =3D=3D outputfile2:=0D + logger.error(=0D + "The firstfile and the secondfile can't be the same: %s" % out= putfile1)=0D + raise(Exception)=0D +=0D + if not outputdir:=0D + outputdir =3D workspace=0D + elif not os.path.isabs(outputdir):=0D + outputdir =3D os.path.join(workspace, outputdir)=0D +=0D + # Create dir for the output files=0D + try:=0D + if not outputfile1:=0D + outputfile1 =3D os.path.abspath(os.path.join(=0D + outputdir, "{}1".format(os.path.basename(inputfile))))=0D + else:=0D + outputfile1 =3D os.path.abspath(os.path.join(outputdir, output= file1))=0D + outputdir =3D os.path.dirname(outputfile1)=0D + if not os.path.exists(outputdir):=0D + os.makedirs(outputdir)=0D +=0D + if not outputfile2:=0D + outputfile2 =3D os.path.abspath(os.path.join(=0D + outputdir, "{}2".format(os.path.basename(inputfile))))=0D + else:=0D + outputfile2 =3D os.path.abspath(os.path.join(outputdir, output= file2))=0D + outputdir =3D os.path.dirname(outputfile2)=0D + if not os.path.exists(outputdir):=0D + os.makedirs(outputdir)=0D + except Exception as e:=0D + logger.error("Can't make dir: %s" % outputdir)=0D + raise(e)=0D +=0D + if position <=3D 0:=0D + if outputfile2 !=3D inputfile:=0D + shutil.copy2(inputfile, outputfile2)=0D + with open(outputfile1, "wb") as fout:=0D + fout.write(b'')=0D + else:=0D + inputfilesize =3D getFileSize(inputfile)=0D + if position >=3D inputfilesize:=0D + if outputfile1 !=3D inputfile:=0D + shutil.copy2(inputfile, outputfile1)=0D + with open(outputfile2, "wb") as fout:=0D + fout.write(b'')=0D + else:=0D + try:=0D + tempdir =3D tempfile.mkdtemp()=0D + tempfile1 =3D os.path.join(tempdir, "file1.bin")=0D + tempfile2 =3D os.path.join(tempdir, "file2.bin")=0D + with open(inputfile, "rb") as fin:=0D + content1 =3D fin.read(position)=0D + with open(tempfile1, "wb") as fout1:=0D + fout1.write(content1)=0D +=0D + content2 =3D fin.read(inputfilesize - position)=0D + with open(tempfile2, "wb") as fout2:=0D + fout2.write(content2)=0D + shutil.copy2(tempfile1, outputfile1)=0D + shutil.copy2(tempfile2, outputfile2)=0D + except Exception as e:=0D + logger.error("Split file failed")=0D + raise(e)=0D + finally:=0D + if os.path.exists(tempdir):=0D + shutil.rmtree(tempdir)=0D +=0D +=0D +def main():=0D + args =3D parser.parse_args()=0D + status =3D 0=0D +=0D + logger =3D logging.getLogger('Split')=0D + if args.quiet:=0D + logger.setLevel(logging.CRITICAL)=0D + if args.verbose:=0D + logger.setLevel(logging.DEBUG)=0D +=0D + lh =3D logging.StreamHandler(sys.stdout)=0D + lf =3D logging.Formatter("%(levelname)-8s: %(message)s")=0D + lh.setFormatter(lf)=0D + logger.addHandler(lh)=0D +=0D + try:=0D + position =3D GetPositionValue(args.position)=0D + splitFile(args.inputfile, position, args.output,=0D + args.firstfile, args.secondfile)=0D + except Exception as e:=0D + status =3D 1=0D +=0D + return status=0D +=0D +=0D +if __name__ =3D=3D "__main__":=0D + exit(main())=0D diff --git a/BaseTools/Source/Python/Split/__init__.py b/BaseTools/Source/P= ython/Split/__init__.py new file mode 100644 index 000000000000..8f4daf86e84a --- /dev/null +++ b/BaseTools/Source/Python/Split/__init__.py @@ -0,0 +1,10 @@ +# @file=0D +# Split a file into two pieces at the request offset.=0D +#=0D +# Copyright (c) 2021, Intel Corporation. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +# Import Modules=0D --=20 2.29.1.windows.1