Пример #1
0
 def load_verify_locations(self, cafile=None, capath=None, cadata=None):
     if cafile is not None:
         cafile = cafile.encode('utf-8')
     if capath is not None:
         capath = capath.encode('utf-8')
     self._ctx.load_verify_locations(cafile, capath)
     if cadata is not None:
         self._ctx.load_verify_locations(BytesIO(cadata))
Пример #2
0
 def _write_script(self, names, shebang, script_bytes, filenames, ext):
     use_launcher = self.add_launchers and self._is_nt
     linesep = os.linesep.encode('utf-8')
     if not shebang.endswith(linesep):
         shebang += linesep
     if not use_launcher:
         script_bytes = shebang + script_bytes
     else:  # pragma: no cover
         if ext == 'py':
             launcher = self._get_launcher('t')
         else:
             launcher = self._get_launcher('w')
         stream = BytesIO()
         with ZipFile(stream, 'w') as zf:
             zf.writestr('__main__.py', script_bytes)
         zip_data = stream.getvalue()
         script_bytes = launcher + shebang + zip_data
     for name in names:
         outname = os.path.join(self.target_dir, name)
         if use_launcher:  # pragma: no cover
             n, e = os.path.splitext(outname)
             if e.startswith('.py'):
                 outname = n
             outname = '%s.exe' % outname
             try:
                 self._fileop.write_binary_file(outname, script_bytes)
             except Exception:
                 # Failed writing an executable - it might be in use.
                 logger.warning('Failed to write executable - trying to '
                                'use .deleteme logic')
                 dfname = '%s.deleteme' % outname
                 if os.path.exists(dfname):
                     os.remove(dfname)  # Not allowed to fail here
                 os.rename(outname, dfname)  # nor here
                 self._fileop.write_binary_file(outname, script_bytes)
                 logger.debug('Able to replace executable using '
                              '.deleteme logic')
                 try:
                     os.remove(dfname)
                 except Exception:
                     pass  # still in use - ignore error
         else:
             if self._is_nt and not outname.endswith(
                     '.' + ext):  # pragma: no cover
                 outname = '%s.%s' % (outname, ext)
             if os.path.exists(outname) and not self.clobber:
                 logger.warning('Skipping existing file %s', outname)
                 continue
             self._fileop.write_binary_file(outname, script_bytes)
             if self.set_mode:
                 self._fileop.set_executable_mode([outname])
         filenames.append(outname)
Пример #3
0
    def openStream(self, source):
        """Produces a file object from source.

        source can be either a file object, local filename or a string.

        """
        # Already a file object
        if hasattr(source, 'read'):
            stream = source
        else:
            stream = BytesIO(source)

        try:
            stream.seek(stream.tell())
        except:  # pylint:disable=bare-except
            stream = BufferedStream(stream)

        return stream
Пример #4
0
def encode_multipart_formdata(fields, boundary=None):
    """
    Encode a dictionary of ``fields`` using the multipart/form-data MIME format.

    :param fields:
        Dictionary of fields or list of (key, :class:`~urllib3.fields.RequestField`).

    :param boundary:
        If not specified, then a random boundary will be generated using
        :func:`urllib3.filepost.choose_boundary`.
    """
    body = BytesIO()
    if boundary is None:
        boundary = choose_boundary()

    for field in iter_field_objects(fields):
        body.write(b('--%s\r\n' % (boundary)))

        writer(body).write(field.render_headers())
        data = field.data

        if isinstance(data, int):
            data = str(data)  # Backwards compatibility

        if isinstance(data, six.text_type):
            writer(body).write(data)
        else:
            body.write(data)

        body.write(b'\r\n')

    body.write(b('--%s--\r\n' % (boundary)))

    content_type = str('multipart/form-data; boundary=%s' % boundary)

    return body.getvalue(), content_type
Пример #5
0
 def __init__(self, fp, callback):
     self.__buf = BytesIO()
     self.__fp = fp
     self.__callback = callback