Skip to content

Commit 406e151

Browse files
committed
add basetag property
Signed-off-by: Tim van Katwijk <timvankatwijk@hotmail.com>
1 parent 7052fe5 commit 406e151

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

dockerfile_parse/parser.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def parent_images(self, parents):
401401
lines[instr['startline']:instr['endline']+1] = [instr['content']]
402402

403403
self.lines = lines
404-
404+
405405
@property
406406
def is_multistage(self):
407407
return len(self.parent_images) > 1
@@ -428,6 +428,21 @@ def baseimage(self, new_image):
428428
raise RuntimeError('No stage defined to set base image on')
429429
images[-1] = new_image
430430
self.parent_images = images
431+
432+
@property
433+
def basetag(self):
434+
"""
435+
:return: tag of base image, i.e. tag of base image
436+
"""
437+
_, tag = tag_from(self.baseimage)
438+
return tag
439+
440+
@basetag.setter
441+
def basetag(self, new_tag):
442+
"""
443+
only change the tag of the final stage FROM instruction
444+
"""
445+
self.baseimage = tag_to(self.basetag, new_tag)
431446

432447
@property
433448
def cmd(self):
@@ -917,7 +932,7 @@ def tag_to(image, new_tag):
917932

918933
bare, _ = tag_from(image)
919934
return ":".join(filter(None, [bare.strip() if bare else None, new_tag.strip() if new_tag else None]))
920-
935+
921936
def _endline(line):
922937
"""
923938
Make sure the line ends with a single newline.

tests/test_parser.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,22 @@ def test_get_baseimg_from_df(self, dfparser):
315315
"LABEL a b\n"]
316316
assert dfparser.baseimage == 'fedora:latest'
317317

318+
def test_get_basetag_from_df(self,dfparser):
319+
dfparser.lines = ["From fedora:latest\n",
320+
"LABEL a b\n"]
321+
assert dfparser.basetag == 'latest'
322+
318323
def test_get_baseimg_from_arg(self, dfparser):
319324
dfparser.lines = ["ARG BASE=fedora:latest\n",
320325
"FROM $BASE\n",
321326
"LABEL a b\n"]
322327
assert dfparser.baseimage == 'fedora:latest'
328+
329+
def test_get_basetag_from_arg(self, dfparser):
330+
dfparser.lines = ["ARG BASE=fedora:latest\n",
331+
"FROM $BASE\n",
332+
"LABEL a b\n"]
333+
assert dfparser.basetag == 'latest'
323334

324335
def test_get_baseimg_from_build_arg(self, tmpdir):
325336
tmpdir_path = str(tmpdir.realpath())
@@ -331,6 +342,16 @@ def test_get_baseimg_from_build_arg(self, tmpdir):
331342
assert dfp.baseimage == 'fedora:latest'
332343
assert not dfp.args
333344

345+
def test_get_basetag_from_build_arg(self, tmpdir):
346+
tmpdir_path = str(tmpdir.realpath())
347+
b_args = {"BASE": "fedora:latest"}
348+
dfp = DockerfileParser(tmpdir_path, env_replace=True, build_args=b_args)
349+
dfp.lines = ["ARG BASE=centos:latest\n",
350+
"FROM $BASE\n",
351+
"LABEL a b\n"]
352+
assert dfp.basetag == 'latest'
353+
assert not dfp.args
354+
334355
def test_set_no_baseimage(self, dfparser):
335356
dfparser.lines = []
336357
with pytest.raises(RuntimeError):
@@ -556,6 +577,7 @@ def test_tag_to(self, from_image, from_tag, expect):
556577
result = tag_to(from_image, from_tag)
557578
assert result == expect
558579

580+
559581
@pytest.mark.parametrize(('tag', 'expect'), [
560582
(
561583
"Tag",
@@ -617,8 +639,9 @@ def test_parent_images_missing_from(self, dfparser):
617639
assert dfparser.content.count('FROM') == 4
618640

619641
def test_modify_instruction(self, dfparser):
620-
FROM = ('ubuntu', 'fedora:')
642+
FROM = ('ubuntu', 'fedora:theBest')
621643
CMD = ('old❤cmd', 'new❤command')
644+
TAG = ('theBest', 'newtag')
622645
df_content = dedent("""\
623646
FROM {0}
624647
CMD {1}""").format(FROM[0], CMD[0])
@@ -628,6 +651,10 @@ def test_modify_instruction(self, dfparser):
628651
assert dfparser.baseimage == FROM[0]
629652
dfparser.baseimage = FROM[1]
630653
assert dfparser.baseimage == FROM[1]
654+
655+
assert dfparser.basetag == TAG[0]
656+
dfparser.basetag = TAG[1]
657+
assert dfparser.basetag == TAG[1]
631658

632659
assert dfparser.cmd == CMD[0]
633660
dfparser.cmd = CMD[1]

0 commit comments

Comments
 (0)