Skip to content

Commit

Permalink
Check and split multistage dockerfile
Browse files Browse the repository at this point in the history
Implemented two functions:
1. check_mutistage_dockerfile(): Given a dockerfile object, return if
it is a multistage dockerfile.
2. split_multistage_dockerfile(): Given a multistage dockerfile object,
return the splited dockerfile object list.

Works towards tern-tools#767.
Super issue tern-tools#612.

Signed-off-by: WangJL <hazard15020@gmail.com>
  • Loading branch information
ForgetMe17 committed Jul 18, 2020
1 parent 044dc47 commit 98948d5
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tern/analyze/docker/dockerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dockerfile_parse import DockerfileParser
import re
import logging
import copy

from tern.utils import general
from tern.utils import constants
Expand Down Expand Up @@ -333,3 +334,32 @@ def write_locked_dockerfile(dfile, destination=None):
file_name = constants.locked_dockerfile
with open(file_name, 'w') as f:
f.write(dfile)


def check_mutistage_dockerfile(dfobj):
"""Given a dockerfile object, return if it is a multistage dockerfile."""
from_line = [1 for c in dfobj.structure if c['instruction'] == 'FROM']
return len(from_line) > 1


def split_multistage_dockerfile(dfobj):
"""Given a multistage dockerfile object, return the splited dockerfile
object list."""
dfobj_list = []
# Dockerfile object to record.
dfobj_temp = Dockerfile()
dfobj_temp.envs = dfobj.envs
dfobj_temp.prev_env = dfobj.prev_env
dfobj_temp.file_path = "Split_dockerfile"
dfobj_temp.structure = []
for command_dict in dfobj.structure:
if command_dict['instruction'] == 'FROM':
if dfobj_temp.structure:
# dfobj_temp not empty, append to the list
dfobj_list.append(copy.copy(dfobj_temp))
dfobj_temp.structure = []
dfobj_temp.structure.append(command_dict)
if dfobj_temp.structure:
# dfobj_temp not empty, append to the list
dfobj_list.append(copy.copy(dfobj_temp))
return dfobj_list

0 comments on commit 98948d5

Please sign in to comment.