Skip to content

Commit 4c76644

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 4c76644

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

dockerfile_parse/parser.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import logging
1212
import os
1313
import re
14+
import warnings
1415
from contextlib import contextmanager
1516
from shlex import quote
1617

@@ -238,6 +239,7 @@ def structure(self):
238239
"value": "yum -y update && yum clean all"}
239240
]
240241
"""
242+
241243
def _rstrip_eol(text, line_continuation_char='\\'):
242244
text = text.rstrip()
243245
if text.endswith(line_continuation_char):
@@ -262,8 +264,8 @@ def _clean_comment_line(line):
262264
lineno = -1
263265
line_continuation_char = '\\'
264266
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?
267+
contre = re.compile(r'^.*\\\s*$') # line continues?
268+
commentre = re.compile(r'^\s*#') # line is a comment?
267269
directive_possible = True
268270
# escape directive regex
269271
escape_directive_re = re.compile(r'^\s*#\s*escape\s*=\s*(\\|`)\s*$', re.I)
@@ -354,7 +356,7 @@ def parent_images(self):
354356
top_args[key] = value
355357
elif instr['instruction'] == 'FROM':
356358
in_stage = True
357-
image, _ = image_from(instr['value'])
359+
image, _ = image_name_from(instr['value'])
358360
if image is not None:
359361
image = WordSplitter(image, args=top_args).dequote()
360362
parents.append(image)
@@ -376,7 +378,7 @@ def parent_images(self, parents):
376378
if instr['instruction'] != 'FROM':
377379
continue
378380

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

394396
lines = self.lines
395397
for instr in reversed(change_instrs):
396-
lines[instr['startline']:instr['endline']+1] = [instr['content']]
398+
lines[instr['startline']:instr['endline'] + 1] = [instr['content']]
397399

398400
self.lines = lines
399401

@@ -416,7 +418,7 @@ def baseimage(self, new_image):
416418
images = []
417419
for instr in self.structure:
418420
if instr['instruction'] == 'FROM':
419-
image, _ = image_from(instr['value'])
421+
image, _ = image_name_from(instr['value'])
420422
if image is not None:
421423
images.append(image)
422424
if not images:
@@ -762,7 +764,7 @@ def add_lines(self, *lines, **kwargs):
762764
for stage in range(len(froms)-2, -1, -1): # e.g. 0 for single or 2, 1, 0 for 3 stages
763765
start, finish = froms[stage], froms[stage+1]
764766
linenum = start['endline'] + 1 if at_start else finish['startline']
765-
image, _ = image_from(froms[stage].get('value') or '')
767+
image, _ = image_name_from(froms[stage].get('value') or '')
766768
if skip_scratch and image == 'scratch':
767769
continue
768770
df_lines[linenum:linenum] = lines
@@ -862,6 +864,16 @@ def context_structure(self):
862864

863865

864866
def image_from(from_value):
867+
"""
868+
:param from_value: string like "image:tag" or "image:tag AS name"
869+
:return: tuple of the image and stage name, e.g. ("image:tag", None)
870+
"""
871+
warnings.warn("Use image_name_from instead.", DeprecationWarning)
872+
image, name = image_name_from(from_value)
873+
return str(image) if image else None, name
874+
875+
876+
def image_name_from(from_value):
865877
"""
866878
:param from_value: string like "image:tag" or "image:tag AS name"
867879
: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)