def install_vbox_additions(): vbox_version = conf.get('vbox-version') iso_prefix = conf.get('vbox-additions.iso-prefix') additions_mount_point = conf.get('vbox-additions.mount-point') url_pattern = conf.get('vbox-additions.url-pattern') executable_path = conf.get('vbox-additions.executable-path') uname = os.uname()[2] aptgettools.install( ['build-essential', 'linux-headers-%s' % uname, 'dkms']) additions_iso_file = filetools.temp_file(iso_prefix, '.iso') nettools.download(url_pattern % (vbox_version, vbox_version), additions_iso_file) os.mkdir(additions_mount_point) try: exectools.check_call_command([ 'mount', '-o', 'loop', '-t', 'iso9660', additions_iso_file, additions_mount_point ]) try: exectools.call_command( [os.path.join(additions_mount_point, executable_path)]) finally: exectools.check_call_command(['umount', additions_mount_point]) finally: os.rmdir(additions_mount_point)
def install_vbox_additions(): vbox_version = conf.get('vbox-version') iso_prefix = conf.get('vbox-additions.iso-prefix') additions_mount_point = conf.get('vbox-additions.mount-point') url_pattern = conf.get('vbox-additions.url-pattern') executable_path = conf.get('vbox-additions.executable-path') uname = os.uname()[2] aptgettools.install(['build-essential', 'linux-headers-%s' % uname, 'dkms']) additions_iso_file = filetools.temp_file(iso_prefix, '.iso') nettools.download(url_pattern % (vbox_version, vbox_version), additions_iso_file) os.mkdir(additions_mount_point) try: exectools.check_call_command(['mount', '-o', 'loop', '-t', 'iso9660', additions_iso_file, additions_mount_point]) try: exectools.call_command([os.path.join(additions_mount_point, executable_path)]) finally: exectools.check_call_command(['umount', additions_mount_point]) finally: os.rmdir(additions_mount_point)
def replace_tokens(tokens, source_file, destination_file=None): if not destination_file: destination_file = source_file with open(source_file, 'r') as fd: text = fd.read() for key in tokens.keys(): text = text.replace('${%s}' % key, tokens[key]) with open(destination_file, 'w') as fd: fd.write(text) if __name__ == '__main__': from net.sandbox.install.util import filetools original_file = filetools.temp_file() modified_file = filetools.temp_file() patched_file = filetools.temp_file() reversed_patch_file = filetools.temp_file() patch_file = filetools.temp_file() with open(original_file, 'w') as fd1: fd1.write('line 1\n') fd1.write('line 2\n') fd1.write('line 3\n') with open(modified_file, 'w') as fd2: fd2.write('line 1\n') fd2.write('line 1.5\n') create_patch(original_file, modified_file, patch_file)
p = subprocess.Popen(['patch', '-Rfu', '--input=%s' % patch_file, '--output=-', original_file], stdout=subprocess.PIPE, stderr = subprocess.PIPE) content, _ = p.communicate() assert p.returncode == 0 with open(destination_file, 'w') as fd: fd.write(content) def replace_tokens(tokens, source_file, destination_file = None): if not destination_file: destination_file = source_file with open(source_file, 'r') as fd: text = fd.read() for key in tokens.keys(): text = text.replace('${%s}' % key, tokens[key]) with open(destination_file, 'w') as fd: fd.write(text) if __name__ == '__main__': from net.sandbox.install.util import filetools original_file = filetools.temp_file() modified_file = filetools.temp_file() patched_file = filetools.temp_file() reversed_patch_file = filetools.temp_file() patch_file = filetools.temp_file() with open(original_file, 'w') as fd1: fd1.write('line 1\n') fd1.write('line 2\n') fd1.write('line 3\n') with open(modified_file, 'w') as fd2: fd2.write('line 1\n') fd2.write('line 1.5\n') create_patch(original_file, modified_file, patch_file)