Skip to content

Commit 545ed0a

Browse files
committed
add image_name_from function and add deprecation warning to image_name
Signed-off-by: Tim van Katwijk <timvankatwijk@hotmail.com>
1 parent 5fe6ebb commit 545ed0a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

dockerfile_parse/parser.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def structure(self):
238238
"value": "yum -y update && yum clean all"}
239239
]
240240
"""
241+
241242
def _rstrip_eol(text, line_continuation_char='\\'):
242243
text = text.rstrip()
243244
if text.endswith(line_continuation_char):
@@ -262,8 +263,8 @@ def _clean_comment_line(line):
262263
lineno = -1
263264
line_continuation_char = '\\'
264265
insnre = re.compile(r'^\s*(\S+)\s+(.*)$') # matched group is insn
265-
contre = re.compile(r'^.*\\\s*$') # line continues?
266-
commentre = re.compile(r'^\s*#') # line is a comment?
266+
contre = re.compile(r'^.*\\\s*$') # line continues?
267+
commentre = re.compile(r'^\s*#') # line is a comment?
267268
directive_possible = True
268269
# escape directive regex
269270
escape_directive_re = re.compile(r'^\s*#\s*escape\s*=\s*(\\|`)\s*$', re.I)
@@ -354,7 +355,7 @@ def parent_images(self):
354355
top_args[key] = value
355356
elif instr['instruction'] == 'FROM':
356357
in_stage = True
357-
image, _ = image_from(instr['value'])
358+
image, _ = image_name_from(instr['value'])
358359
if image is not None:
359360
image = WordSplitter(image, args=top_args).dequote()
360361
parents.append(image)
@@ -376,7 +377,7 @@ def parent_images(self, parents):
376377
if instr['instruction'] != 'FROM':
377378
continue
378379

379-
old_image, stage = image_from(instr['value'])
380+
old_image, stage = image_name_from(instr['value'])
380381
if old_image is None:
381382
continue # broken FROM, fixing would just confuse things
382383
if not parents:
@@ -393,7 +394,7 @@ def parent_images(self, parents):
393394

394395
lines = self.lines
395396
for instr in reversed(change_instrs):
396-
lines[instr['startline']:instr['endline']+1] = [instr['content']]
397+
lines[instr['startline']:instr['endline'] + 1] = [instr['content']]
397398

398399
self.lines = lines
399400

@@ -416,7 +417,7 @@ def baseimage(self, new_image):
416417
images = []
417418
for instr in self.structure:
418419
if instr['instruction'] == 'FROM':
419-
image, _ = image_from(instr['value'])
420+
image, _ = image_name_from(instr['value'])
420421
if image is not None:
421422
images.append(image)
422423
if not images:
@@ -762,7 +763,7 @@ def add_lines(self, *lines, **kwargs):
762763
for stage in range(len(froms)-2, -1, -1): # e.g. 0 for single or 2, 1, 0 for 3 stages
763764
start, finish = froms[stage], froms[stage+1]
764765
linenum = start['endline'] + 1 if at_start else finish['startline']
765-
image, _ = image_from(froms[stage].get('value') or '')
766+
image, _ = image_name_from(froms[stage].get('value') or '')
766767
if skip_scratch and image == 'scratch':
767768
continue
768769
df_lines[linenum:linenum] = lines
@@ -862,6 +863,16 @@ def context_structure(self):
862863

863864

864865
def image_from(from_value):
866+
"""
867+
:param from_value: string like "image:tag" or "image:tag AS name"
868+
:return: tuple of the image and stage name, e.g. ("image:tag", None)
869+
"""
870+
DeprecationWarning("Use image_name_from instead.")
871+
image, name = image_name_from(from_value)
872+
return str(image) if image else None, name
873+
874+
875+
def image_name_from(from_value):
865876
"""
866877
:param from_value: string like "image:tag" or "image:tag AS name"
867878
:return: tuple of the image and stage name, e.g. ("image:tag", None)

tests/test_parser.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from textwrap import dedent
1818

1919
from dockerfile_parse import DockerfileParser
20-
from dockerfile_parse.parser import image_from
20+
from dockerfile_parse.parser import image_from, image_name_from
2121
from dockerfile_parse.constants import COMMENT_INSTRUCTION
2222
from dockerfile_parse.util import b2u, u2b, Context, ImageName
2323

@@ -573,9 +573,12 @@ def test_get_instructions_from_df(self, dfparser, instruction, instr_value,
573573
('registry.example.com:5000/foo/bar:baz', None),
574574
)
575575
])
576-
def test_image_from(self, from_value, expect):
577-
result = image_from(from_value)
576+
def test_image_name_from(self, from_value, expect):
577+
result = image_name_from(from_value)
578+
# image_from is deprecated. But we still want to test it.
579+
deprecated_result = image_from(from_value)
578580
assert result == expect
581+
assert deprecated_result == expect
579582

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

0 commit comments

Comments
 (0)