import re

from semantic_version import Version

_USER_AGENT_SEARCH_REGEX = re.compile(r'docker\/([0-9]+(?:\.[0-9]+){1,2})')
_EXACT_1_5_USER_AGENT = re.compile(r'^Go 1\.1 package http$')
_ONE_FIVE_ZERO = '1.5.0'

def docker_version(user_agent_string):
  """ Extract the Docker version from the user agent, taking special care to
      handle the case of a 1.5 client requesting an auth token, which sends
      a broken user agent. If we can not positively identify a version, return
      None.
      """

  # First search for a well defined semver portion in the UA header.
  found_semver = _USER_AGENT_SEARCH_REGEX.search(user_agent_string)
  if found_semver:
    return Version(found_semver.group(1), partial=True)

  # Check if we received the very specific header which represents a 1.5 request
  # to the auth endpoints.
  elif _EXACT_1_5_USER_AGENT.match(user_agent_string):
    return Version(_ONE_FIVE_ZERO)

  else:
    return None