2020from fig .packages import six
2121
2222from ..utils import utils
23+ from .. import errors
2324
2425INDEX_URL = 'https://index.docker.io/v1/'
2526DOCKER_CONFIG_FILENAME = '.dockercfg'
@@ -45,18 +46,19 @@ def expand_registry_url(hostname):
4546
4647def resolve_repository_name (repo_name ):
4748 if '://' in repo_name :
48- raise ValueError ( 'Repository name cannot contain a '
49- ' scheme ({0})' .format (repo_name ))
49+ raise errors . InvalidRepository (
50+ 'Repository name cannot contain a scheme ({0})' .format (repo_name ))
5051 parts = repo_name .split ('/' , 1 )
51- if not '.' in parts [0 ] and not ':' in parts [0 ] and parts [0 ] != 'localhost' :
52+ if '.' not in parts [0 ] and ':' not in parts [0 ] and parts [0 ] != 'localhost' :
5253 # This is a docker index repo (ex: foo/bar or ubuntu)
5354 return INDEX_URL , repo_name
5455 if len (parts ) < 2 :
55- raise ValueError ('Invalid repository name ({0})' .format (repo_name ))
56+ raise errors .InvalidRepository (
57+ 'Invalid repository name ({0})' .format (repo_name ))
5658
5759 if 'index.docker.io' in parts [0 ]:
58- raise ValueError ( 'Invalid repository name,'
59- ' try "{0}" instead' .format (parts [1 ]))
60+ raise errors . InvalidRepository (
61+ 'Invalid repository name, try "{0}" instead' .format (parts [1 ]))
6062
6163 return expand_registry_url (parts [0 ]), parts [1 ]
6264
@@ -87,6 +89,11 @@ def resolve_authconfig(authconfig, registry=None):
8789 return authconfig .get (swap_protocol (registry ), None )
8890
8991
92+ def encode_auth (auth_info ):
93+ return base64 .b64encode (auth_info .get ('username' , '' ) + b':' +
94+ auth_info .get ('password' , '' ))
95+
96+
9097def decode_auth (auth ):
9198 if isinstance (auth , six .string_types ):
9299 auth = auth .encode ('ascii' )
@@ -100,6 +107,12 @@ def encode_header(auth):
100107 return base64 .b64encode (auth_json )
101108
102109
110+ def encode_full_header (auth ):
111+ """ Returns the given auth block encoded for the X-Registry-Config header.
112+ """
113+ return encode_header ({'configs' : auth })
114+
115+
103116def load_config (root = None ):
104117 """Loads authentication data from a Docker configuration file in the given
105118 root directory."""
@@ -136,7 +149,8 @@ def load_config(root=None):
136149 data .append (line .strip ().split (' = ' )[1 ])
137150 if len (data ) < 2 :
138151 # Not enough data
139- raise Exception ('Invalid or empty configuration file!' )
152+ raise errors .InvalidConfigFile (
153+ 'Invalid or empty configuration file!' )
140154
141155 username , password = decode_auth (data [0 ])
142156 conf [INDEX_URL ] = {
0 commit comments