]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/PyUtility/PyUtility.c
Revert BaseTools: PYTHON3 migration
[mirror_edk2.git] / BaseTools / Source / C / PyUtility / PyUtility.c
diff --git a/BaseTools/Source/C/PyUtility/PyUtility.c b/BaseTools/Source/C/PyUtility/PyUtility.c
new file mode 100644 (file)
index 0000000..d14b872
--- /dev/null
@@ -0,0 +1,106 @@
+/** @file\r
+Python Utility\r
+\r
+Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials are licensed and made available\r
+under the terms and conditions of the BSD License which accompanies this\r
+distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Python.h>\r
+#include <Windows.h>\r
+#include <Common/UefiBaseTypes.h>\r
+\r
+/*\r
+ SaveFileToDisk(FilePath, Content)\r
+*/\r
+STATIC\r
+PyObject*\r
+SaveFileToDisk (\r
+  PyObject    *Self,\r
+  PyObject    *Args\r
+  )\r
+{\r
+  CHAR8         *File;\r
+  UINT8         *Data;\r
+  UINTN         DataLength;\r
+  UINTN         WriteBytes;\r
+  UINTN         Status;\r
+  HANDLE        FileHandle;\r
+  PyObject      *ReturnValue = Py_False;\r
+\r
+  Status = PyArg_ParseTuple(\r
+            Args,\r
+            "ss#",\r
+            &File,\r
+            &Data,\r
+            &DataLength\r
+            );\r
+  if (Status == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  FileHandle = CreateFile(\r
+                File,\r
+                GENERIC_WRITE,\r
+                FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,\r
+                NULL,\r
+                CREATE_ALWAYS,\r
+                FILE_ATTRIBUTE_NORMAL,\r
+                NULL\r
+                );\r
+  if (FileHandle == INVALID_HANDLE_VALUE) {\r
+    PyErr_SetString(PyExc_Exception, "File creation failure");\r
+    return NULL;\r
+  }\r
+\r
+  while (WriteFile(FileHandle, Data, DataLength, &WriteBytes, NULL)) {\r
+    if (DataLength <= WriteBytes) {\r
+      DataLength = 0;\r
+      break;\r
+    }\r
+\r
+    Data += WriteBytes;\r
+    DataLength -= WriteBytes;\r
+  }\r
+\r
+  if (DataLength != 0) {\r
+    // file saved unsuccessfully\r
+    PyErr_SetString(PyExc_Exception, "File write failure");\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // Flush buffer may slow down the whole build performance (average 10s slower)\r
+  //\r
+  //if (!FlushFileBuffers(FileHandle)) {\r
+  //  PyErr_SetString(PyExc_Exception, "File flush failure");\r
+  //  goto Done;\r
+  //}\r
+\r
+  // success!\r
+  ReturnValue = Py_True;\r
+\r
+Done:\r
+  CloseHandle(FileHandle);\r
+  return ReturnValue;\r
+}\r
+\r
+STATIC INT8 SaveFileToDiskDocs[] = "SaveFileToDisk(): Make sure the file is saved to disk\n";\r
+\r
+STATIC PyMethodDef PyUtility_Funcs[] = {\r
+  {"SaveFileToDisk", (PyCFunction)SaveFileToDisk, METH_VARARGS, SaveFileToDiskDocs},\r
+  {NULL, NULL, 0, NULL}\r
+};\r
+\r
+PyMODINIT_FUNC\r
+initPyUtility(VOID) {\r
+  Py_InitModule3("PyUtility", PyUtility_Funcs, "Utilties Module Implemented C Language");\r
+}\r
+\r
+\r