]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/PyUtility/PyUtility.c
License header updated to match correct format.
[mirror_edk2.git] / BaseTools / Source / C / PyUtility / PyUtility.c
CommitLineData
52302d4d 1/** @file\r
97fa0ee9 2Python Utility\r
52302d4d 3\r
97fa0ee9 4Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
52302d4d
LG
5This program and the accompanying materials are licensed and made available \r
6under the terms and conditions of the BSD License which accompanies this \r
7distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
30fdf114
LG
15#include <Python.h>\r
16#include <Windows.h>\r
17#include <Common/UefiBaseTypes.h>\r
18\r
19/*\r
20 SaveFileToDisk(FilePath, Content)\r
21*/\r
22STATIC\r
23PyObject*\r
24SaveFileToDisk (\r
25 PyObject *Self,\r
26 PyObject *Args\r
27 )\r
28{\r
29 CHAR8 *File;\r
30 UINT8 *Data;\r
31 UINTN DataLength;\r
32 UINTN WriteBytes;\r
33 UINTN Status;\r
34 HANDLE FileHandle;\r
35 PyObject *ReturnValue = Py_False;\r
36\r
37 Status = PyArg_ParseTuple(\r
38 Args,\r
39 "ss#",\r
40 &File,\r
41 &Data,\r
42 &DataLength\r
43 );\r
44 if (Status == 0) {\r
45 return NULL;\r
46 }\r
47\r
48 FileHandle = CreateFile(\r
49 File,\r
50 GENERIC_WRITE,\r
51 FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,\r
52 NULL,\r
53 CREATE_ALWAYS,\r
54 FILE_ATTRIBUTE_NORMAL,\r
55 NULL\r
56 );\r
57 if (FileHandle == INVALID_HANDLE_VALUE) {\r
58 PyErr_SetString(PyExc_Exception, "File creation failure");\r
59 return NULL;\r
60 }\r
61\r
62 while (WriteFile(FileHandle, Data, DataLength, &WriteBytes, NULL)) {\r
63 if (DataLength <= WriteBytes) {\r
64 DataLength = 0;\r
65 break;\r
66 }\r
67\r
68 Data += WriteBytes;\r
69 DataLength -= WriteBytes;\r
70 }\r
71\r
72 if (DataLength != 0) {\r
73 // file saved unsuccessfully\r
74 PyErr_SetString(PyExc_Exception, "File write failure");\r
75 goto Done;\r
76 }\r
77\r
78 // \r
79 // Flush buffer may slow down the whole build performance (average 10s slower)\r
80 // \r
81 //if (!FlushFileBuffers(FileHandle)) {\r
82 // PyErr_SetString(PyExc_Exception, "File flush failure");\r
83 // goto Done;\r
84 //}\r
85\r
86 // success!\r
87 ReturnValue = Py_True;\r
88\r
89Done:\r
90 CloseHandle(FileHandle);\r
91 return ReturnValue;\r
92}\r
93\r
94STATIC INT8 SaveFileToDiskDocs[] = "SaveFileToDisk(): Make sure the file is saved to disk\n";\r
95\r
96STATIC PyMethodDef PyUtility_Funcs[] = {\r
97 {"SaveFileToDisk", (PyCFunction)SaveFileToDisk, METH_VARARGS, SaveFileToDiskDocs},\r
98 {NULL, NULL, 0, NULL}\r
99};\r
100\r
101PyMODINIT_FUNC\r
102initPyUtility(VOID) {\r
103 Py_InitModule3("PyUtility", PyUtility_Funcs, "Utilties Module Implemented C Language");\r
104}\r
105\r
106\r