1 # Copyright 2013 dotCloud inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
23 def mkbuildcontext(dockerfile
):
24 f
= tempfile
.NamedTemporaryFile()
25 t
= tarfile
.open(mode
='w', fileobj
=f
)
26 if isinstance(dockerfile
, io
.StringIO
):
27 dfinfo
= tarfile
.TarInfo('Dockerfile')
29 raise TypeError('Please use io.BytesIO to create in-memory '
30 'Dockerfiles with Python 3')
32 dfinfo
.size
= len(dockerfile
.getvalue())
33 elif isinstance(dockerfile
, io
.BytesIO
):
34 dfinfo
= tarfile
.TarInfo('Dockerfile')
35 dfinfo
.size
= len(dockerfile
.getvalue())
37 dfinfo
= t
.gettarinfo(fileobj
=dockerfile
, arcname
='Dockerfile')
38 t
.addfile(dfinfo
, dockerfile
)
45 f
= tempfile
.NamedTemporaryFile()
46 t
= tarfile
.open(mode
='w', fileobj
=f
)
47 t
.add(path
, arcname
='.')
53 def compare_version(v1
, v2
):
54 return float(v2
) - float(v1
)
59 res
= requests
.get(url
)
63 return res
.status_code
< 400
66 def _convert_port_binding(binding
):
67 result
= {'HostIp': '', 'HostPort': ''}
68 if isinstance(binding
, tuple):
70 result
['HostPort'] = binding
[1]
71 result
['HostIp'] = binding
[0]
72 elif isinstance(binding
[0], six
.string_types
):
73 result
['HostIp'] = binding
[0]
75 result
['HostPort'] = binding
[0]
77 result
['HostPort'] = binding
79 if result
['HostPort'] is None:
80 result
['HostPort'] = ''
82 result
['HostPort'] = str(result
['HostPort'])
87 def convert_port_bindings(port_bindings
):
89 for k
, v
in six
.iteritems(port_bindings
):
93 if isinstance(v
, list):
94 result
[key
] = [_convert_port_binding(binding
) for binding
in v
]
96 result
[key
] = [_convert_port_binding(v
)]