def _get_or_download_image(self, templates, image, do_download=True): img_path = self.image_path(image) try: return get_image_descriptor(img_path) except CommandError: if not do_download: raise logger.info('Image %s missing, attempting to download...', image) image_downloader = ImageDownloader(templates) image_downloader.download_images([image], self.image_path()) return get_image_descriptor(img_path)
def handle(self, *args, **options): # Check the archive archive = options['archive'][0] if not os.path.isfile(archive): raise CommandError('%s is not a valid project archive' % archive) # Get the name of the project that we are importing project_name = options.get('project_name') if not project_name: project_name = _get_project_name(archive) logger.info('Importing project \'%s\' from %s', project_name, archive) # Check if a project with that name already exists project_path = self.projects_path(project_name) if os.path.isdir(project_path): if options['force']: logger.info('\'%s\' already exists - removing', project_name) shutil.rmtree(self.projects_path(project_name)) else: raise CommandError('\'%s\' already exists. Either remove this ' 'project or use the force option' % project_name) _decompress_archive(archive, project_path) # Rewrite all of the exported files to fix their S2E environment paths logger.info('Rewriting project files') rewrite_files(project_path, CONSTANTS['import_export']['project_files'], S2E_ENV_PLACEHOLDER, self.env_path()) with open(os.path.join(project_path, 'project.json'), 'r') as f: proj_desc = json.load(f) if 'image' not in proj_desc: logger.error('No image description found in project.json. Unable ' 'to determine the guest tools to symlink') return override_image = options.get('image', None) if override_image: dn = os.path.dirname(proj_desc['image']) old_image = os.path.basename(proj_desc['image']) proj_desc['image'] = os.path.join(dn, override_image) rewrite_files(project_path, CONSTANTS['import_export']['project_files'], old_image, override_image) image = get_image_descriptor(proj_desc['image']) # Create a symlink to the guest tools directory self._symlink_guest_tools(project_path, image) # Create a symlink to guestfs (if it exists) if proj_desc.get('has_guestfs'): self._symlink_guestfs(project_path, image) logger.success('Project successfully imported from %s to %s', archive, project_path)
def image(self): # Put import here to avoid circular dependency # https://github.com/PyCQA/pylint/issues/850 # pylint: disable=cyclic-import from s2e_env.utils import images if not self._image: self._image = images.get_image_descriptor(self.project_desc['image']) return self._image
def _select_guestfs(self, img_name): img_dir = os.path.join(self.image_path(), img_name) img_desc = get_image_descriptor(img_dir) return select_guestfs(self.image_path(), img_desc)