本文整理汇总了Python中utils.Utils.replaceinfile方法的典型用法代码示例。如果您正苦于以下问题:Python Utils.replaceinfile方法的具体用法?Python Utils.replaceinfile怎么用?Python Utils.replaceinfile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.Utils
的用法示例。
在下文中一共展示了Utils.replaceinfile方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: writefstabandgrub
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import replaceinfile [as 别名]
def writefstabandgrub(mount_path, uuidval, partuuidval):
os.remove(mount_path + "/etc/fstab")
f = open(mount_path + "/etc/fstab", "w")
if uuidval != '':
f.write("UUID={} / ext4 defaults 1 1\n".format(uuidval))
else:
f.write("PARTUUID={} / ext4 defaults 1 1\n".format(partuuidval))
f.close()
Utils.replaceinfile(mount_path + "/boot/grub/grub.cfg",
"rootpartition=PARTUUID=.*$",
"rootpartition=PARTUUID={}".format(partuuidval))
示例2: generateImage
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import replaceinfile [as 别名]
def generateImage(raw_image_path, additional_rpms_path, tools_bin_path, src_root, config):
working_directory = os.path.dirname(raw_image_path)
mount_path = os.path.splitext(raw_image_path)[0]
build_scripts_path = os.path.dirname(os.path.abspath(__file__))
if os.path.exists(mount_path) and os.path.isdir(mount_path):
shutil.rmtree(mount_path)
os.mkdir(mount_path)
disk_device = (Utils.runshellcommand(
"losetup --show -f {}".format(raw_image_path))).rstrip('\n')
disk_partitions = Utils.runshellcommand("kpartx -as {}".format(disk_device))
device_name = disk_device.split('/')[2]
if not device_name:
raise Exception("Could not create loop device and partition")
loop_device_path = "/dev/mapper/{}p2".format(device_name)
print(loop_device_path)
try:
(uuidval, partuuidval) = generateUuid(loop_device_path)
# Prep the loop device
prepLoopDevice(loop_device_path, mount_path)
# Clear the root password if not set explicitly from the config file
if config['passwordtext'] == 'PASSWORD':
Utils.replaceinfile(mount_path + "/etc/shadow",
'root:.*?:', 'root:*:')
# Clear machine-id so it gets regenerated on boot
open(mount_path + "/etc/machine-id", "w").close()
# Write fstab
writefstabandgrub(mount_path, uuidval, partuuidval)
if additional_rpms_path and os.path.exists(additional_rpms_path):
installAdditionalRpms(mount_path, additional_rpms_path)
# Perform additional steps defined in installer config
customizeImage(config, mount_path)
except Exception as e:
print(e)
finally:
cleanupMountPoints(mount_path)
Utils.runshellcommand("kpartx -d {}".format(disk_device))
Utils.runshellcommand("losetup -d {}".format(disk_device))
shutil.rmtree(mount_path)
createOutputArtifact(raw_image_path, config, src_root, tools_bin_path)
示例3: create_ova_image
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import replaceinfile [as 别名]
def create_ova_image(raw_image_name, tools_path, build_scripts_path, config):
output_path = os.path.dirname(os.path.realpath(raw_image_name))
utils = Utils()
# Remove older artifacts
files = os.listdir(output_path)
for file in files:
if file.endswith(".vmdk"):
os.remove(os.path.join(output_path, file))
vmx_path = output_path + '/photon-ova.vmx'
utils.replaceandsaveasnewfile(build_scripts_path + '/vmx-template',
vmx_path, 'VMDK_IMAGE',
output_path + '/photon-ova.vmdk')
vixdiskutil_path = tools_path + 'vixdiskutil'
vmdk_path = output_path + '/photon-ova.vmdk'
ovf_path = output_path + '/photon-ova.ovf'
mf_path = output_path + '/photon-ova.mf'
ovfinfo_path = build_scripts_path + '/ovfinfo.txt'
vmdk_capacity = (int(config['size']['root']) +
int(config['size']['swap'])) * 1024
utils.runshellcommand(
"{} -convert {} -cap {} {}".format(vixdiskutil_path,
raw_image_name,
vmdk_capacity,
vmdk_path))
utils.runshellcommand(
"{} -wmeta toolsVersion 2147483647 {}".format(vixdiskutil_path, vmdk_path))
utils.runshellcommand("ovftool {} {}".format(vmx_path, ovf_path))
utils.replaceinfile(ovf_path, 'otherGuest', 'other3xLinux64Guest')
#Add product info
if os.path.exists(ovfinfo_path):
with open(ovfinfo_path) as f:
lines = f.readlines()
for line in fileinput.input(ovf_path, inplace=True):
if line.strip() == '</VirtualHardwareSection>':
for ovfinfoline in lines:
print(ovfinfoline)
else:
print(line)
if os.path.exists(mf_path):
os.remove(mf_path)
cwd = os.getcwd()
os.chdir(output_path)
out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk photon-ova.ovf")
with open(mf_path, "w") as source:
source.write(out)
rawsplit = os.path.splitext(raw_image_name)
ova_name = rawsplit[0] + '.ova'
ovatar = tarfile.open(ova_name, "w", format=tarfile.USTAR_FORMAT)
for name in ["photon-ova.ovf", "photon-ova.mf", "photon-ova-disk1.vmdk"]:
ovatar.add(name, arcname=os.path.basename(name))
ovatar.close()
os.remove(vmx_path)
os.remove(mf_path)
if 'additionalhwversion' in config:
for addlversion in config['additionalhwversion']:
new_ovf_path = output_path + "/photon-ova-hw{}.ovf".format(addlversion)
mf_path = output_path + "/photon-ova-hw{}.mf".format(addlversion)
utils.replaceandsaveasnewfile(
ovf_path, new_ovf_path, "vmx-.*<", "vmx-{}<".format(addlversion))
out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk "
"photon-ova-hw{}.ovf".format(addlversion))
with open(mf_path, "w") as source:
source.write(out)
temp_name_list = os.path.basename(ova_name).split('-')
temp_name_list = temp_name_list[:2] + ["hw{}".format(addlversion)] + temp_name_list[2:]
new_ova_name = '-'.join(temp_name_list)
new_ova_path = output_path + '/' + new_ova_name
ovatar = tarfile.open(new_ova_path, "w", format=tarfile.USTAR_FORMAT)
for name in [new_ovf_path, mf_path, "photon-ova-disk1.vmdk"]:
ovatar.add(name, arcname=os.path.basename(name))
ovatar.close()
os.remove(new_ovf_path)
os.remove(mf_path)
os.chdir(cwd)
os.remove(ovf_path)
os.remove(vmdk_path)
files = os.listdir(output_path)
for file in files:
if file.endswith(".vmdk"):
os.remove(os.path.join(output_path, file))
示例4: RuntimeError
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import replaceinfile [as 别名]
"sgdisk -i 2 {} ".format(disk_device))
partuuidval = (re.findall(r'Partition unique GUID.*',
sgdiskout))[0].split(':')[1].strip(' ').lower()
if partuuidval == '':
raise RuntimeError("Cannot generate partuuid")
# Mount the loop device
print("Mounting the loop device for customization ...")
utils.runshellcommand(
"mount -t ext4 {} {}".format(loop_device_path, options.mount_path))
shutil.rmtree(options.mount_path + "/installer", ignore_errors=True)
shutil.rmtree(options.mount_path + "/LOGS", ignore_errors=True)
# Clear the root password if not set explicitly from the config file
if config['password']['text'] != 'PASSWORD':
utils.replaceinfile(options.mount_path + "/etc/shadow",
'root:.*?:', 'root:*:')
# Clear machine-id so it gets regenerated on boot
open(options.mount_path + "/etc/machine-id", "w").close()
os.remove(options.mount_path + "/etc/fstab")
f = open(options.mount_path + "/etc/fstab", "w")
if uuidval != '':
f.write("UUID={} / ext4 defaults 1 1\n".format(uuidval))
else:
f.write("PARTUUID={} / ext4 defaults 1 1\n".format(partuuidval))
f.close()
utils.replaceinfile(options.mount_path + "/boot/grub/grub.cfg",
"rootpartition=PARTUUID=.*$",
"rootpartition=PARTUUID={}".format(partuuidval))
if os.path.exists(options.additional_rpms_path):
示例5: create_ova_image
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import replaceinfile [as 别名]
def create_ova_image(raw_image_name, tools_path, build_scripts_path, config):
output_path = os.path.dirname(os.path.realpath(raw_image_name))
utils = Utils()
# Remove older artifacts
files = os.listdir(output_path)
for file in files:
if file.endswith(".vmdk"):
os.remove(os.path.join(output_path, file))
vmx_path = output_path + '/photon-ova.vmx'
utils.replaceandsaveasnewfile(build_scripts_path + '/vmx-template', vmx_path, 'VMDK_IMAGE', output_path + '/photon-ova.vmdk')
vixdiskutil_path = tools_path + 'vixdiskutil'
vmdk_path = output_path + '/photon-ova.vmdk'
ovf_path = output_path + '/photon-ova.ovf'
mf_path = output_path + '/photon-ova.mf'
utils.runshellcommand("{} -convert {} -cap 16000 {}".format(vixdiskutil_path, raw_image_name, vmdk_path))
utils.runshellcommand("{} -wmeta toolsVersion 2147483647 {}".format(vixdiskutil_path, vmdk_path))
utils.runshellcommand("ovftool {} {}".format(vmx_path, ovf_path))
utils.replaceinfile(ovf_path, 'otherGuest', 'other3xLinux64Guest')
#Add product info
for line in fileinput.input(ovf_path, inplace=True):
if line.strip() == '</VirtualSystem>':
print ' \t<ProductSection> \n \t\t<Info>Information about the installed software</Info> \n \t\t<Product>Photon</Product> \n \t\t<Vendor>VMware Inc.</Vendor> \n \t\t<Version>1.0</Version> \n \t\t<FullVersion>1.0</FullVersion> \n \t</ProductSection> '
print line,
if os.path.exists(mf_path):
os.remove(mf_path)
cwd = os.getcwd()
os.chdir(output_path)
out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk photon-ova.ovf")
with open(mf_path, "w") as source:
source.write(out)
rawsplit = os.path.splitext(raw_image_name)
ova_name = rawsplit[0] + '.ova'
ovatar = tarfile.open(ova_name, "w", format = tarfile.USTAR_FORMAT)
for name in ["photon-ova.ovf", "photon-ova.mf", "photon-ova-disk1.vmdk"]:
ovatar.add(name, arcname=os.path.basename(name))
ovatar.close()
os.remove(vmx_path)
os.remove(mf_path)
if 'additionalhwversion' in config:
for addlversion in config['additionalhwversion']:
new_ovf_path = output_path + "/photon-ova-hw{}.ovf".format(addlversion)
mf_path = output_path + "/photon-ova-hw{}.mf".format(addlversion)
utils.replaceandsaveasnewfile(ovf_path, new_ovf_path, "vmx-.*<", "vmx-{}<".format(addlversion))
out = utils.runshellcommand("openssl sha1 photon-ova-disk1.vmdk photon-ova-hw{}.ovf".format(addlversion))
with open(mf_path, "w") as source:
source.write(out)
temp_name_list = os.path.basename(ova_name).split('-')
temp_name_list = temp_name_list[:2] + ["hw{}".format(addlversion)] + temp_name_list[2:]
new_ova_name = '-'.join(temp_name_list)
new_ova_path = output_path + '/' + new_ova_name
ovatar = tarfile.open(new_ova_path, "w", format = tarfile.USTAR_FORMAT)
for name in [new_ovf_path, mf_path, "photon-ova-disk1.vmdk"]:
ovatar.add(name, arcname=os.path.basename(name))
ovatar.close()
os.remove(new_ovf_path)
os.remove(mf_path)
os.chdir(cwd)
os.remove(ovf_path)
os.remove(vmdk_path)
files = os.listdir(output_path)
for file in files:
if file.endswith(".vmdk"):
os.remove(os.path.join(output_path, file))