Example #1
0
 def test_auto_decode_utf_16_be(self):
     data = (
         b'\xfe\xff\x00D\x00j\x00a\x00n\x00g\x00o\x00='
         b'\x00=\x001\x00.\x004\x00.\x002'
     )
     assert data.startswith(codecs.BOM_UTF16_BE)
     assert auto_decode(data) == "Django==1.4.2"
Example #2
0
def get_file_content(url, session, comes_from=None):
    # type: (str, PipSession, Optional[str]) -> Tuple[str, Text]
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.
    Respects # -*- coding: declarations on the retrieved files.

    :param url:         File path or url.
    :param session:     PipSession instance.
    :param comes_from:  Origin description of requirements.
    """
    scheme = get_url_scheme(url)

    if scheme in ['http', 'https']:
        # FIXME: catch some errors
        resp = session.get(url)
        raise_for_status(resp)
        return resp.url, resp.text

    elif scheme == 'file':
        if comes_from and comes_from.startswith('http'):
            raise InstallationError(
                'Requirements file {} references URL {}, '
                'which is local'.format(comes_from, url)
            )
        url = url_to_path(url)

    try:
        with open(url, 'rb') as f:
            content = auto_decode(f.read())
    except IOError as exc:
        raise InstallationError(
            'Could not open requirements file: {}'.format(exc)
        )
    return url, content
Example #3
0
 def test_auto_decode_utf_16_be(self):
     data = (
         b'\xfe\xff\x00D\x00j\x00a\x00n\x00g\x00o\x00='
         b'\x00=\x001\x00.\x004\x00.\x002'
     )
     assert data.startswith(codecs.BOM_UTF16_BE)
     assert auto_decode(data) == "Django==1.4.2"
Example #4
0
def get_file_content(url, session):
    # type: (str, PipSession) -> Tuple[str, str]
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.
    Respects # -*- coding: declarations on the retrieved files.

    :param url:         File path or url.
    :param session:     PipSession instance.
    """
    scheme = get_url_scheme(url)

    if scheme in ['http', 'https']:
        # FIXME: catch some errors
        resp = session.get(url)
        raise_for_status(resp)
        return resp.url, resp.text

    elif scheme == 'file':
        url = url_to_path(url)

    try:
        with open(url, 'rb') as f:
            content = auto_decode(f.read())
    except OSError as exc:
        raise InstallationError(f'Could not open requirements file: {exc}')
    return url, content
Example #5
0
 def test_auto_decode_no_preferred_encoding(self) -> None:
     om, em = Mock(), Mock()
     om.return_value = "ascii"
     em.return_value = None
     data = "data"
     with patch("sys.getdefaultencoding", om):
         with patch("locale.getpreferredencoding", em):
             ret = auto_decode(data.encode(sys.getdefaultencoding()))
     assert ret == data
Example #6
0
 def test_auto_decode_no_preferred_encoding(self):
     om, em = Mock(), Mock()
     om.return_value = 'ascii'
     em.return_value = None
     data = u'data'
     with patch('sys.getdefaultencoding', om):
         with patch('locale.getpreferredencoding', em):
             ret = auto_decode(data.encode(sys.getdefaultencoding()))
     assert ret == data
Example #7
0
 def test_auto_decode_no_preferred_encoding(self):
     om, em = Mock(), Mock()
     om.return_value = 'ascii'
     em.return_value = None
     data = u'data'
     with patch('sys.getdefaultencoding', om):
         with patch('locale.getpreferredencoding', em):
             ret = auto_decode(data.encode(sys.getdefaultencoding()))
     assert ret == data
Example #8
0
def get_file_content(url, comes_from=None, session=None):
    # type: (str, Optional[str], Optional[PipSession]) -> Tuple[str, Text]
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.

    :param url:         File path or url.
    :param comes_from:  Origin description of requirements.
    :param session:     Instance of pip.download.PipSession.
    """
    if session is None:
        raise TypeError(
            "get_file_content() missing 1 required keyword argument: 'session'"
        )

    scheme = _get_url_scheme(url)

    if scheme in ['http', 'https']:
        # FIXME: catch some errors
        resp = session.get(url)
        resp.raise_for_status()
        return resp.url, resp.text

    elif scheme == 'file':
        if comes_from and comes_from.startswith('http'):
            raise InstallationError(
                'Requirements file %s references URL %s, which is local'
                % (comes_from, url))

        path = url.split(':', 1)[1]
        path = path.replace('\\', '/')
        match = _url_slash_drive_re.match(path)
        if match:
            path = match.group(1) + ':' + path.split('|', 1)[1]
        path = urllib_parse.unquote(path)
        if path.startswith('/'):
            path = '/' + path.lstrip('/')
        url = path

    try:
        with open(url, 'rb') as f:
            content = auto_decode(f.read())
    except IOError as exc:
        raise InstallationError(
            'Could not open requirements file: %s' % str(exc)
        )
    return url, content
Example #9
0
def get_file_content(url, comes_from=None, session=None):
    # type: (str, Optional[str], Optional[PipSession]) -> Tuple[str, Text]
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.

    :param url:         File path or url.
    :param comes_from:  Origin description of requirements.
    :param session:     Instance of pip.download.PipSession.
    """
    if session is None:
        raise TypeError(
            "get_file_content() missing 1 required keyword argument: 'session'"
        )

    match = _scheme_re.search(url)
    if match:
        scheme = match.group(1).lower()
        if (scheme == 'file' and comes_from and
                comes_from.startswith('http')):
            raise InstallationError(
                'Requirements file %s references URL %s, which is local'
                % (comes_from, url))
        if scheme == 'file':
            path = url.split(':', 1)[1]
            path = path.replace('\\', '/')
            match = _url_slash_drive_re.match(path)
            if match:
                path = match.group(1) + ':' + path.split('|', 1)[1]
            path = urllib_parse.unquote(path)
            if path.startswith('/'):
                path = '/' + path.lstrip('/')
            url = path
        else:
            # FIXME: catch some errors
            resp = session.get(url)
            resp.raise_for_status()
            return resp.url, resp.text
    try:
        with open(url, 'rb') as f:
            content = auto_decode(f.read())
    except IOError as exc:
        raise InstallationError(
            'Could not open requirements file: %s' % str(exc)
        )
    return url, content
def get_file_content(url, comes_from=None, session=None):
    # type: (str, Optional[str], Optional[PipSession]) -> Tuple[str, Text]
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.

    :param url:         File path or url.
    :param comes_from:  Origin description of requirements.
    :param session:     Instance of pip.download.PipSession.
    """
    if session is None:
        raise TypeError(
            "get_file_content() missing 1 required keyword argument: 'session'"
        )

    match = _scheme_re.search(url)
    if match:
        scheme = match.group(1).lower()
        if scheme == "file" and comes_from and comes_from.startswith("http"):
            raise InstallationError(
                "Requirements file %s references URL %s, which is local" %
                (comes_from, url))
        if scheme == "file":
            path = url.split(":", 1)[1]
            path = path.replace("\\", "/")
            match = _url_slash_drive_re.match(path)
            if match:
                path = match.group(1) + ":" + path.split("|", 1)[1]
            path = urllib_parse.unquote(path)
            if path.startswith("/"):
                path = "/" + path.lstrip("/")
            url = path
        else:
            # FIXME: catch some errors
            resp = session.get(url)
            resp.raise_for_status()
            return resp.url, resp.text
    try:
        with open(url, "rb") as f:
            content = auto_decode(f.read())
    except IOError as exc:
        raise InstallationError("Could not open requirements file: %s" %
                                str(exc))
    return url, content
Example #11
0
def get_file_content(url, session, comes_from=None):
    # type: (str, PipSession, Optional[str]) -> Tuple[str, Text]
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.
    Respects # -*- coding: declarations on the retrieved files.

    :param url:         File path or url.
    :param session:     PipSession instance.
    :param comes_from:  Origin description of requirements.
    """
    scheme = get_url_scheme(url)

    if scheme in ['http', 'https']:
        # FIXME: catch some errors
        resp = session.get(url)
        resp.raise_for_status()
        return resp.url, resp.text

    elif scheme == 'file':
        if comes_from and comes_from.startswith('http'):
            raise InstallationError(
                'Requirements file {} references URL {}, '
                'which is local'.format(comes_from, url)
            )

        path = url.split(':', 1)[1]
        path = path.replace('\\', '/')
        match = _url_slash_drive_re.match(path)
        if match:
            path = match.group(1) + ':' + path.split('|', 1)[1]
        path = urllib_parse.unquote(path)
        if path.startswith('/'):
            path = '/' + path.lstrip('/')
        url = path

    try:
        with open(url, 'rb') as f:
            content = auto_decode(f.read())
    except IOError as exc:
        raise InstallationError(
            'Could not open requirements file: {}'.format(exc)
        )
    return url, content
Example #12
0
def get_file_content(url, session, comes_from=None):
    # type: (str, PipSession, Optional[str]) -> Tuple[str, Text]
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.
    Respects # -*- coding: declarations on the retrieved files.

    :param url:         File path or url.
    :param session:     PipSession instance.
    :param comes_from:  Origin description of requirements.
    """
    scheme = get_url_scheme(url)

    if scheme in ["http", "https"]:
        # FIXME: catch some errors
        resp = session.get(url)
        resp.raise_for_status()
        return resp.url, resp.text

    elif scheme == "file":
        if comes_from and comes_from.startswith("http"):
            raise InstallationError(
                "Requirements file %s references URL %s, which is local" %
                (comes_from, url))

        path = url.split(":", 1)[1]
        path = path.replace("\\", "/")
        match = _url_slash_drive_re.match(path)
        if match:
            path = match.group(1) + ":" + path.split("|", 1)[1]
        path = urllib_parse.unquote(path)
        if path.startswith("/"):
            path = "/" + path.lstrip("/")
        url = path

    try:
        with open(url, "rb") as f:
            content = auto_decode(f.read())
    except IOError as exc:
        raise InstallationError("Could not open requirements file: %s" %
                                str(exc))
    return url, content
Example #13
0
def get_file_content(url: str, session: PipSession) -> Tuple[str, str]:
    """Gets the content of a file; it may be a filename, file: URL, or
    http: URL.  Returns (location, content).  Content is unicode.
    Respects # -*- coding: declarations on the retrieved files.

    :param url:         File path or url.
    :param session:     PipSession instance.
    """
    scheme = get_url_scheme(url)

    # Pip has special support for file:// URLs (LocalFSAdapter).
    if scheme in ["http", "https", "file"]:
        resp = session.get(url)
        raise_for_status(resp)
        return resp.url, resp.text

    # Assume this is a bare path.
    try:
        with open(url, "rb") as f:
            content = auto_decode(f.read())
    except OSError as exc:
        raise InstallationError(f"Could not open requirements file: {exc}")
    return url, content
Example #14
0
 def test_auto_decode_pep263_headers(self):
     latin1_req = u'# coding=latin1\n# Pas trop de café'
     assert auto_decode(latin1_req.encode('latin1')) == latin1_req
Example #15
0
 def test_auto_decode_no_bom(self):
     assert auto_decode(b'foobar') == u'foobar'
Example #16
0
 def test_auto_decode_no_bom(self) -> None:
     assert auto_decode(b"foobar") == "foobar"
Example #17
0
 def test_auto_decode_utf16_le(self):
     data = (b'\xff\xfeD\x00j\x00a\x00n\x00g\x00o\x00=\x00'
             b'=\x001\x00.\x004\x00.\x002\x00')
     assert auto_decode(data) == "Django==1.4.2"
Example #18
0
 def test_auto_decode_pep263_headers(self) -> None:
     latin1_req = "# coding=latin1\n# Pas trop de café"
     assert auto_decode(latin1_req.encode("latin1")) == latin1_req
Example #19
0
 def test_auto_decode_utf16_le(self):
     data = (
         b'\xff\xfeD\x00j\x00a\x00n\x00g\x00o\x00=\x00'
         b'=\x001\x00.\x004\x00.\x002\x00'
     )
     assert auto_decode(data) == "Django==1.4.2"
Example #20
0
        return resp.url, resp.text

    elif scheme == 'file':
        if comes_from and comes_from.startswith('http'):
            raise InstallationError(
                'Requirements file {} references URL {}, '
                'which is local'.format(comes_from, url)
            )

        path = url.split(':', 1)[1]
        path = path.replace('\\', '/')
        match = _url_slash_drive_re.match(path)
        if match:
            path = match.group(1) + ':' + path.split('|', 1)[1]
        path = urllib_parse.unquote(path)
        if path.startswith('/'):
            path = '/' + path.lstrip('/')
        url = path

    try:
        with open(url, 'rb') as f:
            content = auto_decode(f.read())
    except IOError as exc:
        raise InstallationError(
            'Could not open requirements file: {}'.format(exc)
        )
    return url, content


_url_slash_drive_re = re.compile(r'/*([a-z])\|', re.I)
Example #21
0
 def test_auto_decode_utf_16_le(self) -> None:
     data = (b"\xff\xfeD\x00j\x00a\x00n\x00g\x00o\x00=\x00"
             b"=\x001\x00.\x004\x00.\x002\x00")
     assert data.startswith(codecs.BOM_UTF16_LE)
     assert auto_decode(data) == "Django==1.4.2"
Example #22
0
 def test_auto_decode_no_bom(self):
     assert auto_decode(b'foobar') == u'foobar'
Example #23
0
 def test_auto_decode_pep263_headers(self):
     latin1_req = u'# coding=latin1\n# Pas trop de café'
     assert auto_decode(latin1_req.encode('latin1')) == latin1_req