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
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
def is_url(name: str) -> bool: """ Return true if the name looks like a URL. """ scheme = get_url_scheme(name) if scheme is None: return False return scheme in ["http", "https", "file", "ftp"] + vcs.all_schemes
def is_url(name): # type: (str) -> bool """ Return true if the name looks like a URL. """ scheme = get_url_scheme(name) if scheme is None: return False return scheme in ['http', 'https', 'file', 'ftp'] + vcs.all_schemes
def is_url(name): # type: (Union[str, Text]) -> bool """ Return true if the name looks like a URL. """ scheme = get_url_scheme(name) if scheme is None: return False return scheme in ["http", "https", "file", "ftp"] + vcs.all_schemes
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. Respects # -*- coding: declarations on the retrieved files. :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
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
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
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
line = line.replace(env_var, value) yield line_number, line 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) <<<<<<< HEAD raise_for_status(resp) ======= resp.raise_for_status() >>>>>>> b66a76afa15ab74019740676a52a071b85ed8f71 return resp.url, resp.text elif scheme == 'file': if comes_from and comes_from.startswith('http'): raise InstallationError( 'Requirements file {} references URL {}, '
def test_get_url_scheme(url, expected): assert get_url_scheme(url) == expected
def test_get_url_scheme(url: str, expected: Optional[str]) -> None: assert get_url_scheme(url) == expected