]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Convert Split tool to python
authorBob Feng <bob.c.feng@intel.com>
Wed, 13 Jan 2021 03:12:24 +0000 (11:12 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thu, 21 Jan 2021 10:19:09 +0000 (10:19 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3165

There are 2 reasons to convert Split tool from C to Python.
1. We are in the process of moving the Basetools Python code
to a separate repository. But there still are many C tools under
edk2/BaseTools. To make all Basetools be in the separate repo,
we can convert the C tools to Python tools.
2. The original Split tool is very slow. This python tool can reduce
90% time.

Signed-off-by: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Yuwei Chen <yuwei.chen@intel.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
Reviewed-by: Yuwei Chen <yuwei.chen@intel.com>
BaseTools/BinWrappers/PosixLike/Split
BaseTools/BinWrappers/WindowsLike/Split.bat [new file with mode: 0644]
BaseTools/Source/C/GNUmakefile
BaseTools/Source/C/Makefile
BaseTools/Source/C/Split/GNUmakefile [deleted file]
BaseTools/Source/C/Split/Makefile [deleted file]
BaseTools/Source/C/Split/Split.c [deleted file]
BaseTools/Source/Python/Split/Split.py [new file with mode: 0644]
BaseTools/Source/Python/Split/__init__.py [new file with mode: 0644]

index 0945d86d9209fe4cf438f44a38bf43142162856d..f3770eed42b4dfec0dfa971294dea6124a52ee67 100755 (executable)
@@ -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 python
+if command -v ${PYTHON_COMMAND} >/dev/null 2>&1; then
+    python_exe=${PYTHON_COMMAND}
+fi
 
 full_cmd=${BASH_SOURCE:-$0} # see http://mywiki.wooledge.org/BashFAQ/028 for a discussion of why $0 is not a good choice here
 dir=$(dirname "$full_cmd")
 cmd=${full_cmd##*/}
 
-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="$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/BinWrappers/WindowsLike/Split.bat
new file mode 100644 (file)
index 0000000..9616cd8
--- /dev/null
@@ -0,0 +1,3 @@
+@setlocal\r
+@set ToolName=%~n0%\r
+@%PYTHON_COMMAND% %BASE_TOOLS_PATH%\Source\Python\%ToolName%\%ToolName%.py %*\r
index 464f43277455128a1207554dc28f286578447f5d..8c191e0c38176cded43f7c5a6349ff95615c94a0 100644 (file)
@@ -57,7 +57,6 @@ APPLICATIONS = \
   GenSec \\r
   GenCrc32 \\r
   LzmaCompress \\r
-  Split \\r
   TianoCompress \\r
   VolInfo \\r
   DevicePath\r
index e8f8abe59a794521c54063eadf8e787240129ea7..a376d32e220e5fb7bc35c395b398f701d4f52d6e 100644 (file)
@@ -19,7 +19,6 @@ APPLICATIONS = \
   GenFw \\r
   GenSec \\r
   LzmaCompress \\r
-  Split \\r
   TianoCompress \\r
   VolInfo \\r
   DevicePath\r
diff --git a/BaseTools/Source/C/Split/GNUmakefile b/BaseTools/Source/C/Split/GNUmakefile
deleted file mode 100644 (file)
index b3d4dff..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-## @file\r
-# GNU/Linux makefile for 'Split' module build.\r
-#\r
-# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
-# SPDX-License-Identifier: BSD-2-Clause-Patent\r
-#\r
-MAKEROOT ?= ..\r
-\r
-APPNAME = Split\r
-\r
-OBJECTS = Split.o\r
-\r
-include $(MAKEROOT)/Makefiles/app.makefile\r
-\r
-LIBS = -lCommon\r
-\r
-\r
diff --git a/BaseTools/Source/C/Split/Makefile b/BaseTools/Source/C/Split/Makefile
deleted file mode 100644 (file)
index 19d3e31..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-## @file\r
-# Windows makefile for 'Split' module build.\r
-#\r
-# Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
-# SPDX-License-Identifier: BSD-2-Clause-Patent\r
-#\r
-!INCLUDE ..\Makefiles\ms.common\r
-\r
-APPNAME = Split\r
-\r
-LIBS = $(LIB_PATH)\Common.lib\r
-\r
-OBJECTS = Split.obj\r
-\r
-!INCLUDE ..\Makefiles\ms.app\r
-\r
diff --git a/BaseTools/Source/C/Split/Split.c b/BaseTools/Source/C/Split/Split.c
deleted file mode 100644 (file)
index be0ee12..0000000
+++ /dev/null
@@ -1,466 +0,0 @@
-/** @file\r
-\r
-  Split a file into two pieces at the request offset.\r
-\r
-Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.<BR>\r
-SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r
-**/\r
-\r
-// GC_TODO: fix comment to start with /*++\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-#ifdef __GNUC__\r
-#include <unistd.h>\r
-#else\r
-#include <direct.h>\r
-#endif\r
-#include <ctype.h>\r
-#include "ParseInf.h"\r
-#include "CommonLib.h"\r
-#include "EfiUtilityMsgs.h"\r
-//\r
-// Utility Name\r
-//\r
-#define UTILITY_NAME  "Split"\r
-\r
-//\r
-// Utility version information\r
-//\r
-#define UTILITY_MAJOR_VERSION 1\r
-#define UTILITY_MINOR_VERSION 0\r
-\r
-void\r
-Version (\r
-  void\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Displays the standard utility information to SDTOUT\r
-\r
-Arguments:\r
-\r
-  None\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-{\r
-  printf ("%s Version %d.%d Build %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
-}\r
-\r
-void\r
-Usage (\r
-  void\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  GC_TODO: Add function description\r
-\r
-Arguments:\r
-\r
-\r
-Returns:\r
-\r
-  GC_TODO: add return values\r
-\r
---*/\r
-{\r
-  Version();\r
-  printf ("Copyright (c) 1999-2017 Intel Corporation. All rights reserved.\n");\r
-  printf ("\n  SplitFile creates two Binary files either in the same directory as the current working\n");\r
-  printf ("  directory or in the specified directory.\n");\r
-  printf ("\nUsage: \n\\r
-   Split\n\\r
-     -f, --filename inputFile to split\n\\r
-     -s, --split VALUE the number of bytes in the first file\n\\r
-     [-p, --prefix OutputDir]\n\\r
-     [-o, --firstfile Filename1]\n\\r
-     [-t, --secondfile Filename2]\n\\r
-     [-v, --verbose]\n\\r
-     [--version]\n\\r
-     [-q, --quiet disable all messages except fatal errors]\n\\r
-     [-d, --debug[#]\n\\r
-     [-h, --help]\n");\r
-}\r
-\r
-EFI_STATUS\r
-GetSplitValue (\r
-  IN CONST CHAR8* SplitValueString,\r
-  OUT UINT64 *ReturnValue\r
-)\r
-{\r
-  UINT64 len = 0;\r
-  UINT64 base = 1;\r
-  UINT64 index = 0;\r
-  UINT64 number = 0;\r
-  CHAR8 lastCHAR = 0;\r
-  EFI_STATUS Status = EFI_SUCCESS;\r
-\r
-  if (SplitValueString != NULL){\r
-    len = strlen(SplitValueString);\r
-  }\r
-\r
-  if (len == 0) {\r
-    return EFI_ABORTED;\r
-  }\r
-\r
-  Status = AsciiStringToUint64 (SplitValueString, FALSE, ReturnValue);\r
-  if (!EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  if (SplitValueString[0] == '0' && (SplitValueString[1] == 'x' || SplitValueString[1] == 'X')) {\r
-    Status = AsciiStringToUint64 (SplitValueString, TRUE, ReturnValue);\r
-    if (!EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-  }\r
-\r
-  lastCHAR = (CHAR8)toupper((int)SplitValueString[len - 1]);\r
-\r
-  if (lastCHAR != 'K' && lastCHAR != 'M' && lastCHAR != 'G') {\r
-    return STATUS_ERROR;\r
-  }\r
-\r
-  for (;index < len - 1; ++index) {\r
-    if (!isdigit((int)SplitValueString[index])) {\r
-      return EFI_ABORTED;\r
-    }\r
-  }\r
-\r
-  number = atol (SplitValueString);\r
-  if (lastCHAR == 'K')\r
-    base = 1024;\r
-  else if (lastCHAR == 'M')\r
-    base = 1024*1024;\r
-  else\r
-    base = 1024*1024*1024;\r
-\r
-  *ReturnValue = number*base;\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-CountVerboseLevel (\r
-  IN CONST CHAR8* VerboseLevelString,\r
-  IN CONST UINT64 Length,\r
-  OUT UINT64 *ReturnValue\r
-)\r
-{\r
-  UINT64 i = 0;\r
-  for (;i < Length; ++i) {\r
-    if (VerboseLevelString[i] != 'v' && VerboseLevelString[i] != 'V') {\r
-      return EFI_ABORTED;\r
-    }\r
-    ++(*ReturnValue);\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-CreateDir (\r
-  IN OUT CHAR8** FullFileName\r
-)\r
-{\r
-  CHAR8* temp = *FullFileName;\r
-  CHAR8* start = temp;\r
-  CHAR8  tempchar;\r
-  UINT64 index = 0;\r
-\r
-  for (;index < strlen(temp); ++index) {\r
-    if (temp[index] == '\\' || temp[index] == '/') {\r
-      if (temp[index + 1] != '\0') {\r
-        tempchar = temp[index + 1];\r
-        temp[index + 1] = 0;\r
-        if (chdir(start)) {\r
-          if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {\r
-            return EFI_ABORTED;\r
-          }\r
-          chdir(start);\r
-        }\r
-        start = temp + index + 1;\r
-        temp[index] = '/';\r
-        temp[index + 1] = tempchar;\r
-      }\r
-    }\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
-int\r
-main (\r
-  int argc,\r
-  char*argv[]\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  GC_TODO: Add function description\r
-\r
-Arguments:\r
-\r
-  argc  - GC_TODO: add argument description\r
-  ]     - GC_TODO: add argument description\r
-\r
-Returns:\r
-\r
-  GC_TODO: add return values\r
-\r
---*/\r
-{\r
-  EFI_STATUS    Status = EFI_SUCCESS;\r
-  INTN          ReturnStatus = STATUS_SUCCESS;\r
-  FILE          *In;\r
-  CHAR8         *InputFileName = NULL;\r
-  CHAR8         *OutputDir = NULL;\r
-  CHAR8         *OutFileName1 = NULL;\r
-  CHAR8         *OutFileName2 = NULL;\r
-  UINT64        SplitValue = (UINT64) -1;\r
-  FILE          *Out1 = NULL;\r
-  FILE          *Out2 = NULL;\r
-  CHAR8         *OutName1 = NULL;\r
-  CHAR8         *OutName2 = NULL;\r
-  CHAR8         *CurrentDir = NULL;\r
-  UINT64        Index;\r
-  CHAR8         CharC;\r
-  UINT64        DebugLevel = 0;\r
-  UINT64        VerboseLevel = 0;\r
-\r
-  SetUtilityName(UTILITY_NAME);\r
-  if (argc == 1) {\r
-    Usage();\r
-    return STATUS_ERROR;\r
-  }\r
-\r
-  argc --;\r
-  argv ++;\r
-\r
-  if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {\r
-    Usage();\r
-    return STATUS_SUCCESS;\r
-  }\r
-\r
-  if (stricmp (argv[0], "--version") == 0) {\r
-    Version();\r
-    return STATUS_SUCCESS;\r
-  }\r
-\r
-  while (argc > 0) {\r
-    if ((stricmp (argv[0], "-p") == 0) || (stricmp (argv[0], "--prefix") == 0)) {\r
-      OutputDir = argv[1];\r
-      if (OutputDir == NULL) {\r
-        Warning (NULL, 0, 0, "NO output directory specified.", NULL);\r
-        return STATUS_ERROR;\r
-      }\r
-      argc -= 2;\r
-      argv += 2;\r
-      continue;\r
-    }\r
-\r
-    if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--filename") == 0)) {\r
-      InputFileName = argv[1];\r
-      if (InputFileName == NULL) {\r
-        Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);\r
-        return STATUS_ERROR;\r
-      }\r
-      argc -= 2;\r
-      argv += 2;\r
-      continue;\r
-    }\r
-\r
-    if ((stricmp (argv[0], "-s") == 0) || (stricmp (argv[0], "--split") == 0)) {\r
-      Status = GetSplitValue(argv[1], &SplitValue);\r
-      if (EFI_ERROR (Status)) {\r
-        Error (NULL, 0, 0x1003, "Input split value is not one valid integer.", NULL);\r
-        return STATUS_ERROR;\r
-      }\r
-      argc -= 2;\r
-      argv += 2;\r
-      continue;\r
-    }\r
-\r
-    if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--firstfile") == 0)) {\r
-      OutFileName1 = argv[1];\r
-      if (OutFileName1 == NULL) {\r
-        Warning (NULL, 0, 0, NULL, "No output file1 specified.");\r
-      }\r
-      argc -= 2;\r
-      argv += 2;\r
-      continue;\r
-    }\r
-\r
-    if ((stricmp (argv[0], "-t") == 0) || (stricmp (argv[0], "--secondfile") == 0)) {\r
-      OutFileName2 = argv[1];\r
-      if (OutFileName2 == NULL) {\r
-        Warning (NULL, 0, 0, NULL, "No output file2 specified.");\r
-      }\r
-      argc -= 2;\r
-      argv += 2;\r
-      continue;\r
-    }\r
-\r
-    if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {\r
-      argc --;\r
-      argv ++;\r
-      continue;\r
-    }\r
-\r
-    if ((strlen(argv[0]) >= 2 && argv[0][0] == '-' && (argv[0][1] == 'v' || argv[0][1] == 'V')) || (stricmp (argv[0], "--verbose") == 0)) {\r
-      VerboseLevel = 1;\r
-      if (strlen(argv[0]) > 2) {\r
-        Status = CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &VerboseLevel);\r
-        if (EFI_ERROR (Status)) {\r
-          Error (NULL, 0, 0x1003, NULL, "%s is invalid parameter!", argv[0]);\r
-          return STATUS_ERROR;\r
-        }\r
-      }\r
-\r
-      argc --;\r
-      argv ++;\r
-      continue;\r
-    }\r
-\r
-    if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) {\r
-      Status = AsciiStringToUint64 (argv[1], FALSE, &DebugLevel);\r
-      if (EFI_ERROR (Status)) {\r
-        Error (NULL, 0, 0x1003, "Input debug level is not one valid integrator.", NULL);\r
-        return STATUS_ERROR;\r
-      }\r
-      argc -= 2;\r
-      argv += 2;\r
-      continue;\r
-    }\r
-    //\r
-    // Don't recognize the parameter.\r
-    //\r
-    Error (NULL, 0, 0x1003, NULL, "%s is invalid parameter!", argv[0]);\r
-    return STATUS_ERROR;\r
-  }\r
-\r
-  if (InputFileName == NULL) {\r
-    Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);\r
-    return STATUS_ERROR;\r
-  }\r
-\r
-  In = fopen (LongFilePath (InputFileName), "rb");\r
-  if (In == NULL) {\r
-    // ("Unable to open file \"%s\"\n", InputFileName);\r
-    Error (InputFileName, 0, 1, "File open failure", NULL);\r
-    return STATUS_ERROR;\r
-  }\r
-\r
-  if (OutFileName1 == NULL) {\r
-    OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16);\r
-    if (OutName1 == NULL) {\r
-      Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");\r
-      ReturnStatus = STATUS_ERROR;\r
-      goto Finish;\r
-    }\r
-    strcpy (OutName1, InputFileName);\r
-    strcat (OutName1, "1");\r
-    OutFileName1 = OutName1;\r
-\r
-  }\r
-  if (OutFileName2 == NULL) {\r
-    OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16);\r
-    if (OutName2 == NULL) {\r
-      Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");\r
-      ReturnStatus = STATUS_ERROR;\r
-      goto Finish;\r
-    }\r
-    strcpy (OutName2, InputFileName);\r
-    strcat (OutName2, "2");\r
-    OutFileName2 = OutName2;\r
-\r
-  }\r
-\r
-  if (OutputDir != NULL) {\r
-    //OutputDirSpecified = TRUE;\r
-    if (chdir(OutputDir) != 0) {\r
-      Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");\r
-      ReturnStatus = STATUS_ERROR;\r
-      goto Finish;\r
-    }\r
-  }\r
-\r
-  CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0);\r
-  if (EFI_ERROR(CreateDir(&OutFileName1))) {\r
-      Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);\r
-      ReturnStatus = STATUS_ERROR;\r
-      goto Finish;\r
-  }\r
-  chdir(CurrentDir);\r
-\r
-  if (EFI_ERROR(CreateDir(&OutFileName2))) {\r
-      Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);\r
-      ReturnStatus = STATUS_ERROR;\r
-      goto Finish;\r
-  }\r
-  chdir(CurrentDir);\r
-  free(CurrentDir);\r
-\r
-  Out1 = fopen (LongFilePath (OutFileName1), "wb");\r
-  if (Out1 == NULL) {\r
-    // ("Unable to open file \"%s\"\n", OutFileName1);\r
-    Error (OutFileName1, 0, 1, "File open failure", NULL);\r
-    ReturnStatus = STATUS_ERROR;\r
-    goto Finish;\r
-  }\r
-\r
-  Out2 = fopen (LongFilePath (OutFileName2), "wb");\r
-  if (Out2 == NULL) {\r
-    // ("Unable to open file \"%s\"\n", OutFileName2);\r
-    Error (OutFileName2, 0, 1, "File open failure", NULL);\r
-    ReturnStatus = STATUS_ERROR;\r
-    goto Finish;\r
-  }\r
-\r
-  for (Index = 0; Index < SplitValue; Index++) {\r
-    CharC = (CHAR8) fgetc (In);\r
-    if (feof (In)) {\r
-      break;\r
-    }\r
-\r
-    fputc (CharC, Out1);\r
-  }\r
-\r
-  for (;;) {\r
-    CharC = (CHAR8) fgetc (In);\r
-    if (feof (In)) {\r
-      break;\r
-    }\r
-\r
-    fputc (CharC, Out2);\r
-  }\r
-\r
-Finish:\r
-  if (OutName1 != NULL) {\r
-    free(OutName1);\r
-  }\r
-  if (OutName2 != NULL) {\r
-    free(OutName2);\r
-  }\r
-  if (In != NULL) {\r
-    fclose (In);\r
-  }\r
-  if (Out1 != NULL) {\r
-    fclose (Out1);\r
-  }\r
-  if (Out2 != NULL) {\r
-    fclose (Out2);\r
-  }\r
-\r
-  return ReturnStatus;\r
-}\r
diff --git a/BaseTools/Source/Python/Split/Split.py b/BaseTools/Source/Python/Split/Split.py
new file mode 100644 (file)
index 0000000..45a5a06
--- /dev/null
@@ -0,0 +1,202 @@
+# @file\r
+#  Split a file into two pieces at the request offset.\r
+#\r
+#  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+# Import Modules\r
+#\r
+import argparse\r
+import os\r
+import io\r
+import shutil\r
+import logging\r
+import sys\r
+import tempfile\r
+\r
+parser = argparse.ArgumentParser(description='''\r
+SplitFile creates two Binary files either in the same directory as the current working directory or in the specified directory.\r
+''')\r
+parser.add_argument("-f", "--filename", dest="inputfile",\r
+                    required=True, help="The input file to split tool.")\r
+parser.add_argument("-s", "--split", dest="position",\r
+                    required=True, help="The number of bytes in the first file. The valid format are HEX, Decimal and Decimal[KMG].")\r
+parser.add_argument("-p", "--prefix",  dest="output",\r
+                    help="The output folder.")\r
+parser.add_argument("-o", "--firstfile",  help="The first file name")\r
+parser.add_argument("-t", "--secondfile",  help="The second file name")\r
+parser.add_argument("--version", action="version", version='%(prog)s Version 2.0',\r
+                    help="Print debug information.")\r
+\r
+group = parser.add_mutually_exclusive_group()\r
+group.add_argument("-v", "--verbose", action="store_true",\r
+                   help="Print debug information.")\r
+group.add_argument("-q", "--quiet", action="store_true",\r
+                   help="Disable all messages except fatal errors")\r
+\r
+SizeDict = {\r
+    "K": 1024,\r
+    "M": 1024*1024,\r
+    "G": 1024*1024*1024\r
+}\r
+\r
+\r
+def GetPositionValue(position):\r
+    '''\r
+    Parse the string of the argument position and return a decimal number.\r
+    The valid position formats are\r
+    1. HEX\r
+    e.g. 0x1000 or 0X1000\r
+    2. Decimal\r
+    e.g. 100\r
+    3. Decimal[KMG]\r
+    e.g. 100K or 100M or 100G or 100k or 100m or 100g\r
+    '''\r
+    logger = logging.getLogger('Split')\r
+    PosVal = 0\r
+    header = position[:2].upper()\r
+    tailer = position[-1].upper()\r
+\r
+    try:\r
+        if tailer in SizeDict:\r
+            PosVal = int(position[:-1]) * SizeDict[tailer]\r
+        else:\r
+            if header == "0X":\r
+                PosVal = int(position, 16)\r
+            else:\r
+                PosVal = int(position)\r
+    except Exception as e:\r
+        logger.error(\r
+            "The parameter %s format is incorrect. The valid format is HEX, Decimal and Decimal[KMG]." % position)\r
+        raise(e)\r
+\r
+    return PosVal\r
+\r
+\r
+def getFileSize(filename):\r
+    '''\r
+    Read the input file and return the file size.\r
+    '''\r
+    logger = logging.getLogger('Split')\r
+    length = 0\r
+    try:\r
+        with open(filename, "rb") as fin:\r
+            fin.seek(0, io.SEEK_END)\r
+            length = fin.tell()\r
+    except Exception as e:\r
+        logger.error("Access file failed: %s", filename)\r
+        raise(e)\r
+\r
+    return length\r
+\r
+\r
+def splitFile(inputfile, position, outputdir=None, outputfile1=None, outputfile2=None):\r
+    '''\r
+    Split the inputfile into outputfile1 and outputfile2 from the position.\r
+    '''\r
+    logger = logging.getLogger('Split')\r
+\r
+    inputfile = os.path.abspath(inputfile)\r
+    workspace = os.path.dirname(inputfile)\r
+    if not os.path.exists(inputfile):\r
+        logger.error("File Not Found: %s" % inputfile)\r
+        raise(Exception)\r
+\r
+    if outputfile1 and outputfile2 and outputfile1 == outputfile2:\r
+        logger.error(\r
+            "The firstfile and the secondfile can't be the same: %s" % outputfile1)\r
+        raise(Exception)\r
+\r
+    if not outputdir:\r
+        outputdir = workspace\r
+    elif not os.path.isabs(outputdir):\r
+        outputdir = os.path.join(workspace, outputdir)\r
+\r
+    # Create dir for the output files\r
+    try:\r
+        if not outputfile1:\r
+            outputfile1 = os.path.abspath(os.path.join(\r
+                outputdir, "{}1".format(os.path.basename(inputfile))))\r
+        else:\r
+            outputfile1 = os.path.abspath(os.path.join(outputdir, outputfile1))\r
+        outputdir = os.path.dirname(outputfile1)\r
+        if not os.path.exists(outputdir):\r
+            os.makedirs(outputdir)\r
+\r
+        if not outputfile2:\r
+            outputfile2 = os.path.abspath(os.path.join(\r
+                outputdir, "{}2".format(os.path.basename(inputfile))))\r
+        else:\r
+            outputfile2 = os.path.abspath(os.path.join(outputdir, outputfile2))\r
+        outputdir = os.path.dirname(outputfile2)\r
+        if not os.path.exists(outputdir):\r
+            os.makedirs(outputdir)\r
+    except Exception as e:\r
+        logger.error("Can't make dir: %s" % outputdir)\r
+        raise(e)\r
+\r
+    if position <= 0:\r
+        if outputfile2 != inputfile:\r
+            shutil.copy2(inputfile, outputfile2)\r
+        with open(outputfile1, "wb") as fout:\r
+            fout.write(b'')\r
+    else:\r
+        inputfilesize = getFileSize(inputfile)\r
+        if position >= inputfilesize:\r
+            if outputfile1 != inputfile:\r
+                shutil.copy2(inputfile, outputfile1)\r
+            with open(outputfile2, "wb") as fout:\r
+                fout.write(b'')\r
+        else:\r
+            try:\r
+                tempdir = tempfile.mkdtemp()\r
+                tempfile1 = os.path.join(tempdir, "file1.bin")\r
+                tempfile2 = os.path.join(tempdir, "file2.bin")\r
+                with open(inputfile, "rb") as fin:\r
+                    content1 = fin.read(position)\r
+                    with open(tempfile1, "wb") as fout1:\r
+                        fout1.write(content1)\r
+\r
+                    content2 = fin.read(inputfilesize - position)\r
+                    with open(tempfile2, "wb") as fout2:\r
+                        fout2.write(content2)\r
+                shutil.copy2(tempfile1, outputfile1)\r
+                shutil.copy2(tempfile2, outputfile2)\r
+            except Exception as e:\r
+                logger.error("Split file failed")\r
+                raise(e)\r
+            finally:\r
+                if os.path.exists(tempdir):\r
+                    shutil.rmtree(tempdir)\r
+\r
+\r
+def main():\r
+    args = parser.parse_args()\r
+    status = 0\r
+\r
+    logger = logging.getLogger('Split')\r
+    if args.quiet:\r
+        logger.setLevel(logging.CRITICAL)\r
+    if args.verbose:\r
+        logger.setLevel(logging.DEBUG)\r
+\r
+    lh = logging.StreamHandler(sys.stdout)\r
+    lf = logging.Formatter("%(levelname)-8s: %(message)s")\r
+    lh.setFormatter(lf)\r
+    logger.addHandler(lh)\r
+\r
+    try:\r
+        position = GetPositionValue(args.position)\r
+        splitFile(args.inputfile, position, args.output,\r
+                  args.firstfile, args.secondfile)\r
+    except Exception as e:\r
+        status = 1\r
+\r
+    return status\r
+\r
+\r
+if __name__ == "__main__":\r
+    exit(main())\r
diff --git a/BaseTools/Source/Python/Split/__init__.py b/BaseTools/Source/Python/Split/__init__.py
new file mode 100644 (file)
index 0000000..8f4daf8
--- /dev/null
@@ -0,0 +1,10 @@
+# @file\r
+#  Split a file into two pieces at the request offset.\r
+#\r
+#  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+# Import Modules\r