BZ: https://bugzilla.tianocore.org/process_bug.cgi
On Windows OS, sometime the generated file is not immediate saved to disk.
When run nmake, prompt AutoGen.h not found, and stop build.
Below blog shows Write-Replace to fix it. This patch uses this way to write
temp file, then rename the temp file to the real file.
https://blog.gocept.com/2013/07/15/reliable-file-updates-with-python/
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
from struct import pack\r
import uuid\r
import subprocess\r
from struct import pack\r
import uuid\r
import subprocess\r
from collections import OrderedDict\r
\r
import Common.LongFilePathOs as os\r
from collections import OrderedDict\r
\r
import Common.LongFilePathOs as os\r
if not os.access(DirName, os.W_OK):\r
EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)\r
\r
if not os.access(DirName, os.W_OK):\r
EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)\r
\r
+ OpenMode = "wb"\r
+\r
+ if GlobalData.gIsWindows and not os.path.exists(File):\r
+ # write temp file, then rename the temp file to the real file\r
+ # to make sure the file be immediate saved to disk\r
+ with tempfile.NamedTemporaryFile(OpenMode, dir=os.path.dirname(File), delete=False) as tf:\r
+ tf.write(Content)\r
+ tempname = tf.name\r
- with open(File, "wb") as Fd:\r
- Fd.write(Content)\r
- except IOError as X:\r
+ os.rename(tempname, File)\r
+ except:\r
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
else:\r
try:\r
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
else:\r
try:\r
- with open(File, 'w') as Fd:\r
+ with open(File, OpenMode) as Fd:\r
Fd.write(Content)\r
except IOError as X:\r
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
Fd.write(Content)\r
except IOError as X:\r
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r