Skip to content

Commit

Permalink
add image_name_from function and add deprecation warning to image_name
Browse files Browse the repository at this point in the history
Signed-off-by: Tim van Katwijk <[email protected]>
  • Loading branch information
tim-vk committed Jan 6, 2023
1 parent 5fe6ebb commit 4c76644
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
26 changes: 19 additions & 7 deletions dockerfile_parse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import os
import re
import warnings
from contextlib import contextmanager
from shlex import quote

Expand Down Expand Up @@ -238,6 +239,7 @@ def structure(self):
"value": "yum -y update && yum clean all"}
]
"""

def _rstrip_eol(text, line_continuation_char='\\'):
text = text.rstrip()
if text.endswith(line_continuation_char):
Expand All @@ -262,8 +264,8 @@ def _clean_comment_line(line):
lineno = -1
line_continuation_char = '\\'
insnre = re.compile(r'^\s*(\S+)\s+(.*)$') # matched group is insn
contre = re.compile(r'^.*\\\s*$') # line continues?
commentre = re.compile(r'^\s*#') # line is a comment?
contre = re.compile(r'^.*\\\s*$') # line continues?
commentre = re.compile(r'^\s*#') # line is a comment?
directive_possible = True
# escape directive regex
escape_directive_re = re.compile(r'^\s*#\s*escape\s*=\s*(\\|`)\s*$', re.I)
Expand Down Expand Up @@ -354,7 +356,7 @@ def parent_images(self):
top_args[key] = value
elif instr['instruction'] == 'FROM':
in_stage = True
image, _ = image_from(instr['value'])
image, _ = image_name_from(instr['value'])
if image is not None:
image = WordSplitter(image, args=top_args).dequote()
parents.append(image)
Expand All @@ -376,7 +378,7 @@ def parent_images(self, parents):
if instr['instruction'] != 'FROM':
continue

old_image, stage = image_from(instr['value'])
old_image, stage = image_name_from(instr['value'])
if old_image is None:
continue # broken FROM, fixing would just confuse things
if not parents:
Expand All @@ -393,7 +395,7 @@ def parent_images(self, parents):

lines = self.lines
for instr in reversed(change_instrs):
lines[instr['startline']:instr['endline']+1] = [instr['content']]
lines[instr['startline']:instr['endline'] + 1] = [instr['content']]

self.lines = lines

Expand All @@ -416,7 +418,7 @@ def baseimage(self, new_image):
images = []
for instr in self.structure:
if instr['instruction'] == 'FROM':
image, _ = image_from(instr['value'])
image, _ = image_name_from(instr['value'])
if image is not None:
images.append(image)
if not images:
Expand Down Expand Up @@ -762,7 +764,7 @@ def add_lines(self, *lines, **kwargs):
for stage in range(len(froms)-2, -1, -1): # e.g. 0 for single or 2, 1, 0 for 3 stages
start, finish = froms[stage], froms[stage+1]
linenum = start['endline'] + 1 if at_start else finish['startline']
image, _ = image_from(froms[stage].get('value') or '')
image, _ = image_name_from(froms[stage].get('value') or '')
if skip_scratch and image == 'scratch':
continue
df_lines[linenum:linenum] = lines
Expand Down Expand Up @@ -862,6 +864,16 @@ def context_structure(self):


def image_from(from_value):
"""
:param from_value: string like "image:tag" or "image:tag AS name"
:return: tuple of the image and stage name, e.g. ("image:tag", None)
"""
warnings.warn("Use image_name_from instead.", DeprecationWarning)
image, name = image_name_from(from_value)
return str(image) if image else None, name


def image_name_from(from_value):
"""
:param from_value: string like "image:tag" or "image:tag AS name"
:return: tuple of the image and stage name, e.g. ("image:tag", None)
Expand Down
9 changes: 6 additions & 3 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from textwrap import dedent

from dockerfile_parse import DockerfileParser
from dockerfile_parse.parser import image_from
from dockerfile_parse.parser import image_from, image_name_from
from dockerfile_parse.constants import COMMENT_INSTRUCTION
from dockerfile_parse.util import b2u, u2b, Context, ImageName

Expand Down Expand Up @@ -573,9 +573,12 @@ def test_get_instructions_from_df(self, dfparser, instruction, instr_value,
('registry.example.com:5000/foo/bar:baz', None),
)
])
def test_image_from(self, from_value, expect):
result = image_from(from_value)
def test_image_name_from(self, from_value, expect):
result = image_name_from(from_value)
# image_from is deprecated. But we still want to test it.
deprecated_result = image_from(from_value)
assert result == expect
assert deprecated_result == expect

def test_parent_images(self, dfparser):
FROM = ('my-builder:latest', 'rhel7:7.5')
Expand Down

0 comments on commit 4c76644

Please sign in to comment.