def test_create_with_exclude(self): tmp_dir = temp_content.write_items_to_temp_dir([ 'file a/b/c/foo.txt "foo content" 755', 'file d/e/bar.txt "bar content" 644', 'dir baz "" 700', ]) tmp_archive = self.make_temp_file(suffix = '.zip') archiver.create(tmp_archive, tmp_dir, exclude = [ self.native_filename('d/e/bar.txt') ] ) self.assertEqual( [ 'a/b/c/foo.txt', ], archiver.members(tmp_archive) )
def test_filenames_with_brackets_tar(self): tmp_dir = temp_content.write_items_to_temp_dir([ 'file .foo/bar-[baz].ext-kiwi.apple.lemon "something" 644', ], delete = not self.DEBUG) tmp_archive = self.make_temp_file() archiver.create(tmp_archive, tmp_dir, extension = 'tar.gz') self.assertEqual( [ '.foo/bar-[baz].ext-kiwi.apple.lemon', ], archiver.members(tmp_archive) ) self.assertEqual( 'tgz', archiver.format_name(tmp_archive) ) self.assertEqual( '3fc9b689459d738f8c88a3a48aa9e33542016b7a4052e001aaa536fca74813cb', archiver.member_checksum(tmp_archive, '.foo/bar-[baz].ext-kiwi.apple.lemon') )
def test_create_force_tar_gz(self): tmp_dir = temp_content.write_items_to_temp_dir([ 'file a/b/c/foo.txt "foo content" 755', 'file d/e/bar.txt "bar content" 644', 'dir baz "" 700', ]) tmp_archive = self.make_temp_file() archiver.create(tmp_archive, tmp_dir, extension = 'tar.gz') self.assertEqual( [ 'a/b/c/foo.txt', 'd/e/bar.txt', ], archiver.members(tmp_archive) ) self.assertEqual( 'tgz', archiver.format_name(tmp_archive) )
def test_create_zip(self): tmp_dir = temp_content.write_items_to_temp_dir([ 'file a/b/c/foo.txt "foo content" 755', 'file d/e/bar.txt "bar content" 644', 'dir baz "" 700', ]) tmp_archive = self.make_temp_file(suffix = '.zip') archiver.create(tmp_archive, tmp_dir) self.assertEqual( [ 'a/b/c/foo.txt', 'd/e/bar.txt', ], archiver.members(tmp_archive) ) self.assertEqual( 'zip', archiver.format_name(tmp_archive) )
def archive_to_file(clazz, root, prefix, revision, output_filename, archive_format = None, short_hash = True): 'git archive to a archive file.' prefix = file_util.ensure_rsep(prefix) archive_format = archive_format or 'tgz' output_filename = path.abspath(output_filename) file_util.ensure_file_dir(output_filename) if short_hash: if clazz.is_long_hash(revision): revision = clazz.short_hash(root, revision) clazz.log.log_d('archive_to_file: revision={} output_filename={} archive_format={}'.format(revision, output_filename, archive_format)) tmp_dir = temp_file.make_temp_dir() clazz.archive_to_dir(root, revision, tmp_dir) archiver.create(output_filename, tmp_dir, base_dir = prefix, extension = archive_format)
def test_transform(self): tmp_dir = temp_content.write_items_to_temp_dir([ 'file files/foo.txt "this is foo.txt" 644', 'file files/bar.txt "this is bar.txt" 644', 'file files/baz.txt "this is baz.txt" 644', ], delete = not self.DEBUG) tmp_archive = self.make_temp_file(suffix = '.zip') archiver.create(tmp_archive, tmp_dir) from bes.archive.archive_operation_add_file import archive_operation_add_file from bes.archive.archive_operation_remove_files import archive_operation_remove_files operations = [ archive_operation_add_file('new/new_file.txt', 'this is new_file.txt', 0o0644), archive_operation_remove_files([ 'files/foo.txt', 'files/bar.txt' ]), ] archiver.transform(tmp_archive, operations) self.assertEqual( [ 'files/baz.txt', 'new/new_file.txt', ], archiver.members(tmp_archive) ) self.assertEqual( 'this is new_file.txt', archiver.extract_member_to_string(tmp_archive, 'new/new_file.txt', codec = 'utf8') )
def vm_run_package(self, vm_id, package_dir, entry_command, entry_command_args, run_program_options): check.check_string(vm_id) check.check_string(package_dir) check.check_string(entry_command) check.check_string_seq(entry_command_args) check.check_vmware_run_program_options(run_program_options) self._log.log_method_d() vm = self._resolve_vmx_to_local_vm(vm_id) if not path.isdir(package_dir): raise vmware_error( 'package_dir not found or not a dir: "{}"'.format(package_dir)) tmp_dir_local = temp_file.make_temp_dir(suffix='-run_package.dir', delete=not self._options.debug) if self._options.debug: print('tmp_dir_local={}'.format(tmp_dir_local)) tmp_remote_dir = path.join('/tmp', path.basename(tmp_dir_local)) self._log.log_d( 'vm_run_package: tmp_remote_dir={}'.format(tmp_remote_dir)) tmp_package = self._make_tmp_file_pair(tmp_dir_local, 'package.tar.gz') tmp_caller_script = self._make_tmp_file_pair(tmp_dir_local, 'caller_script.py') tmp_output_log = self._make_tmp_file_pair(tmp_dir_local, 'output.log') archiver.create(tmp_package.local, package_dir) file_util.save(tmp_caller_script.local, content=vmware_guest_scripts.RUN_PACKAGE_CALLER, mode=0o0755) self._log.log_d('vm_run_package: tmp_package={}'.format(tmp_package)) self._log.log_d( 'vm_run_package: tmp_caller_script={}'.format(tmp_caller_script)) self._log.log_d( 'vm_run_package: tmp_output_log={}'.format(tmp_output_log)) parsed_entry_command_args = command_line.parse_args(entry_command_args) debug_args = [] if self._options.debug: debug_args.append('--debug') if self._options.tty: debug_args.extend(['--tty', tty]) caller_args = debug_args + [ tmp_package.remote, entry_command, tmp_output_log.remote, ] + parsed_entry_command_args with vmware_restore_vm_running_state(self) as _: with vmware_run_operation(vm, run_program_options) as target_vm: target_vm.dir_create(tmp_remote_dir) target_vm.file_copy_to(tmp_package.local, tmp_package.remote) target_vm.file_copy_to(tmp_caller_script.local, tmp_caller_script.remote) rv = target_vm.run_program( tmp_caller_script.remote, caller_args, run_program_options=run_program_options) target_vm.file_copy_from(tmp_output_log.remote, tmp_output_log.local) target_vm.dir_delete(tmp_remote_dir) with file_util.open_with_default( filename=run_program_options.output_filename) as f: log_content = file_util.read(tmp_output_log.local, codec='utf-8') f.write(log_content) f.flush() return rv