diff --git a/dockerfile_parse/parser.py b/dockerfile_parse/parser.py index 9f740c1..1274be1 100644 --- a/dockerfile_parse/parser.py +++ b/dockerfile_parse/parser.py @@ -238,6 +238,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): @@ -262,8 +263,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) @@ -354,7 +355,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) @@ -376,7 +377,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: @@ -393,7 +394,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 @@ -416,7 +417,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: @@ -762,7 +763,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 @@ -862,6 +863,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) + """ + DeprecationWarning("Use image_name_from instead.") + 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) diff --git a/tests/test_parser.py b/tests/test_parser.py index c5db795..5ac6569 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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 @@ -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')