updated update_file_content as per review comment

This commit is contained in:
Jenny Tam 2017-08-15 16:15:59 -07:00
parent d07c454dc9
commit ad970b4b3a

View file

@ -21,6 +21,7 @@ import stat
import datetime import datetime
import urllib.request import urllib.request
import zipfile import zipfile
import fileinput
class BuildUtil(object): class BuildUtil(object):
"""Build sqlsrv and/or pdo_sqlsrv drivers with PHP source with the following properties: """Build sqlsrv and/or pdo_sqlsrv drivers with PHP source with the following properties:
@ -141,12 +142,9 @@ class BuildUtil(object):
def update_file_content(file, search_str, new_str): def update_file_content(file, search_str, new_str):
"""Find *search_str* and replace it by *new_str* in a *file*""" """Find *search_str* and replace it by *new_str* in a *file*"""
os.chmod(file, stat.S_IWRITE) os.chmod(file, stat.S_IWRITE)
f = open(file, 'r') with fileinput.FileInput(file, inplace=True) as f:
filedata = f.read() for line in f:
updatedata = filedata.replace(search_str, new_str) print(line.replace(search_str, new_str), end='')
f = open(file, 'w')
f.write(updatedata)
f.close()
@staticmethod @staticmethod
def generateMMDD(): def generateMMDD():
@ -390,6 +388,12 @@ class BuildUtil(object):
# Copy the generated batch file to phpSDK for the php starter script # Copy the generated batch file to phpSDK for the php starter script
shutil.copy(os.path.join(work_dir, batch_file), phpSDK) shutil.copy(os.path.join(work_dir, batch_file), phpSDK)
sdk_source = os.path.join(phpSDK, 'Source')
# Sometimes, for various reasons, the Source folder from previous build
# might exist in phpSDK. If so, remove it first
if os.path.exists(sdk_source):
os.chmod(sdk_source, stat.S_IWRITE)
shutil.rmtree(sdk_source, ignore_errors=True)
shutil.move(source_dir, phpSDK) shutil.move(source_dir, phpSDK)
# Invoke phpsdk-<vc>-<arch>.bat # Invoke phpsdk-<vc>-<arch>.bat