def _update_dockerfile(self, dockerfile: DockerfileParser, stage: DockerfileStage, start_lines: List[str], end_lines: List[str]) -> None: """Add the specified lines at the start and end of the specified stage in the Dockerfile.""" from_structure, end_structure, stage_user = stage self.log.debug("updating stage starting line %d, ending at %d", from_structure['startline'], end_structure['endline']) add_start_lines = [] add_end_lines = [] parent_image_id = from_structure['value'].split(' ', 1)[0] if parent_image_id == SCRATCH_FROM: return # inspect any platform, the parent user should be the same for all platforms inspect = self.workflow.imageutil.get_inspect_for_image( parent_image_id) inherited_user = inspect.get(INSPECT_CONFIG, {}).get('User', '') if inherited_user: add_start_lines.append('USER root') add_start_lines.extend(start_lines) if inherited_user: add_start_lines.append('USER {}'.format(inherited_user)) final_user_line = "USER " + inherited_user if inherited_user else None if stage_user: final_user_line = stage_user if final_user_line: add_end_lines.append('USER root') add_end_lines.extend(end_lines) if final_user_line: add_end_lines.append(final_user_line) self.log.debug("append after: %r", add_end_lines) self.log.debug("insert before: %r", add_start_lines) dockerfile.add_lines_at(end_structure, *add_end_lines, after=True) dockerfile.add_lines_at(from_structure, *add_start_lines, after=True)