2014-04-03 03:33:58 +00:00
|
|
|
import re
|
|
|
|
|
2014-09-12 18:01:10 +00:00
|
|
|
LINE_CONTINUATION_REGEX = re.compile(r'(\s)*\\(\s)*\n')
|
2014-04-17 20:49:04 +00:00
|
|
|
COMMAND_REGEX = re.compile('([A-Za-z]+)\s(.*)')
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
COMMENT_CHARACTER = '#'
|
2014-04-30 22:48:36 +00:00
|
|
|
LATEST_TAG = 'latest'
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
class ParsedDockerfile(object):
|
|
|
|
def __init__(self, commands):
|
|
|
|
self.commands = commands
|
|
|
|
|
2014-04-30 22:48:36 +00:00
|
|
|
def _get_commands_of_kind(self, kind):
|
2014-04-03 03:33:58 +00:00
|
|
|
return [command for command in self.commands if command['command'] == kind]
|
|
|
|
|
2014-04-30 22:48:36 +00:00
|
|
|
def _get_from_image_identifier(self):
|
|
|
|
from_commands = self._get_commands_of_kind('FROM')
|
|
|
|
if not from_commands:
|
2014-04-03 03:33:58 +00:00
|
|
|
return None
|
|
|
|
|
2014-04-30 22:48:36 +00:00
|
|
|
return from_commands[-1]['parameters']
|
2014-04-14 22:54:39 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2014-04-30 22:48:36 +00:00
|
|
|
def parse_image_identifier(image_identifier):
|
|
|
|
""" Parses a docker image identifier, and returns a tuple of image name and tag, where the tag
|
|
|
|
is filled in with "latest" if left unspecified.
|
|
|
|
"""
|
2014-04-03 03:33:58 +00:00
|
|
|
# Note:
|
|
|
|
# Dockerfile images references can be of multiple forms:
|
|
|
|
# server:port/some/path
|
|
|
|
# somepath
|
|
|
|
# server/some/path
|
|
|
|
# server/some/path:tag
|
|
|
|
# server:port/some/path:tag
|
2014-04-30 22:48:36 +00:00
|
|
|
parts = image_identifier.strip().split(':')
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
if len(parts) == 1:
|
2014-04-14 22:54:39 +00:00
|
|
|
# somepath
|
2014-04-30 22:48:36 +00:00
|
|
|
return (parts[0], LATEST_TAG)
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
# Otherwise, determine if the last part is a port
|
|
|
|
# or a tag.
|
|
|
|
if parts[-1].find('/') >= 0:
|
|
|
|
# Last part is part of the hostname.
|
2014-04-30 22:48:36 +00:00
|
|
|
return (image_identifier, LATEST_TAG)
|
2014-04-03 03:33:58 +00:00
|
|
|
|
2014-04-14 22:54:39 +00:00
|
|
|
# Remaining cases:
|
|
|
|
# server/some/path:tag
|
2014-11-24 21:07:38 +00:00
|
|
|
# server:port/some/path:tag
|
|
|
|
return (':'.join(parts[0:-1]), parts[-1])
|
2014-04-03 03:33:58 +00:00
|
|
|
|
2014-04-30 22:48:36 +00:00
|
|
|
def get_base_image(self):
|
|
|
|
""" Return the base image without the tag name. """
|
|
|
|
return self.get_image_and_tag()[0]
|
2014-04-03 03:33:58 +00:00
|
|
|
|
2014-04-30 22:48:36 +00:00
|
|
|
def get_image_and_tag(self):
|
|
|
|
""" Returns the image and tag from the FROM line of the dockerfile. """
|
|
|
|
image_identifier = self._get_from_image_identifier()
|
|
|
|
if image_identifier is None:
|
|
|
|
return (None, None)
|
|
|
|
|
|
|
|
return self.parse_image_identifier(image_identifier)
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def strip_comments(contents):
|
2015-08-31 17:58:28 +00:00
|
|
|
lines = []
|
|
|
|
for line in contents.split('\n'):
|
|
|
|
index = line.find(COMMENT_CHARACTER)
|
|
|
|
if index < 0:
|
|
|
|
lines.append(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
line = line[:index]
|
|
|
|
lines.append(line)
|
|
|
|
|
2014-04-03 03:33:58 +00:00
|
|
|
return '\n'.join(lines)
|
|
|
|
|
2014-04-14 22:54:39 +00:00
|
|
|
|
2014-04-03 03:33:58 +00:00
|
|
|
def join_continued_lines(contents):
|
|
|
|
return LINE_CONTINUATION_REGEX.sub('', contents)
|
|
|
|
|
2014-04-14 22:54:39 +00:00
|
|
|
|
2014-04-03 03:33:58 +00:00
|
|
|
def parse_dockerfile(contents):
|
2015-08-31 17:58:28 +00:00
|
|
|
# If we receive ASCII, translate into unicode.
|
|
|
|
try:
|
|
|
|
contents = contents.decode('utf-8')
|
|
|
|
except ValueError:
|
|
|
|
# Already unicode or unable to convert.
|
|
|
|
pass
|
|
|
|
|
2014-04-03 03:33:58 +00:00
|
|
|
contents = join_continued_lines(strip_comments(contents))
|
2015-08-31 17:58:28 +00:00
|
|
|
lines = [line.strip() for line in contents.split('\n') if len(line) > 0]
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
commands = []
|
|
|
|
for line in lines:
|
2014-04-14 22:54:39 +00:00
|
|
|
match_command = COMMAND_REGEX.match(line)
|
|
|
|
if match_command:
|
2015-08-31 17:58:28 +00:00
|
|
|
command = match_command.group(1).upper()
|
|
|
|
parameters = match_command.group(2)
|
2014-04-03 03:33:58 +00:00
|
|
|
|
|
|
|
commands.append({
|
|
|
|
'command': command,
|
|
|
|
'parameters': parameters
|
|
|
|
})
|
|
|
|
|
|
|
|
return ParsedDockerfile(commands)
|