| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 | # Copyright 2013 dotCloud inc.#    Licensed under the Apache License, Version 2.0 (the "License");#    you may not use this file except in compliance with the License.#    You may obtain a copy of the License at#        http://www.apache.org/licenses/LICENSE-2.0#    Unless required by applicable law or agreed to in writing, software#    distributed under the License is distributed on an "AS IS" BASIS,#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#    See the License for the specific language governing permissions and#    limitations under the License.import ioimport tarfileimport tempfileimport requestsimport sixdef mkbuildcontext(dockerfile):    f = tempfile.NamedTemporaryFile()    t = tarfile.open(mode='w', fileobj=f)    if isinstance(dockerfile, io.StringIO):        dfinfo = tarfile.TarInfo('Dockerfile')        if six.PY3:            raise TypeError('Please use io.BytesIO to create in-memory '                            'Dockerfiles with Python 3')        else:            dfinfo.size = len(dockerfile.getvalue())    elif isinstance(dockerfile, io.BytesIO):        dfinfo = tarfile.TarInfo('Dockerfile')        dfinfo.size = len(dockerfile.getvalue())    else:        dfinfo = t.gettarinfo(fileobj=dockerfile, arcname='Dockerfile')    t.addfile(dfinfo, dockerfile)    t.close()    f.seek(0)    return fdef tar(path):    f = tempfile.NamedTemporaryFile()    t = tarfile.open(mode='w', fileobj=f)    t.add(path, arcname='.')    t.close()    f.seek(0)    return fdef compare_version(v1, v2):    return float(v2) - float(v1)def ping(url):    try:        res = requests.get(url)        return res.status >= 400    except Exception:        return Falsedef _convert_port_binding(binding):    result = {'HostIp': '', 'HostPort': ''}    if isinstance(binding, tuple):        if len(binding) == 2:            result['HostPort'] = binding[1]            result['HostIp'] = binding[0]        elif isinstance(binding[0], six.string_types):            result['HostIp'] = binding[0]        else:            result['HostPort'] = binding[0]    else:        result['HostPort'] = binding    if result['HostPort'] is None:        result['HostPort'] = ''    else:        result['HostPort'] = str(result['HostPort'])    return resultdef convert_port_bindings(port_bindings):    result = {}    for k, v in six.iteritems(port_bindings):        key = str(k)        if '/' not in key:            key = key + '/tcp'        if isinstance(v, list):            result[key] = [_convert_port_binding(binding) for binding in v]        else:            result[key] = [_convert_port_binding(v)]    return result
 |