def parse_requirements(requirement_file): requirements = [] for req in pip_parse_requirements(requirement_file, session=PipSession()): install_req = install_req_from_parsed_requirement(req) if install_req.markers and not pkg_resources.evaluate_marker(str(install_req.markers)): continue requirements.append(pkg_resources.Requirement.parse(str(install_req.req))) return requirements
def parse_requirements(filename): requirements = pip_parse_requirements( filename, session=pip._internal.download.PipSession()) for requirement in requirements: if not requirement.name: raise ValueError('A requirement lacks a name (e.g. no `#egg` url)') elif requirement.editable: raise ValueError('Cannot parse %s: editable projects unsupported' % requirement.name) else: yield requirement
def parse_requirements(requirement_file): requirements = [] for req in pip_parse_requirements(requirement_file, session=PipSession()): install_req = install_req_from_parsed_requirement(req) if install_req.markers and not pkg_resources.evaluate_marker( str(install_req.markers)): # req should not installed due to env markers continue elif install_req.editable: # skip editable req as they are failing in the resolve phase continue requirements.append( pkg_resources.Requirement.parse(str(install_req.req))) return requirements
def test_parse_requirements(request, tmpdir): """ Ensures `_parse_requirements` returns the same result as `pip._internal.req.parse_requirements` """ from pip._internal.req import parse_requirements as pip_parse_requirements from pip._internal.network.session import PipSession root_req_src = """ # No version specifier noverspec no-ver-spec # Version specifiers verspec<1.0 ver-spec == 2.0 # Environment marker env-marker; python_version < "3.8" inline-comm # Inline comment inlinecomm # Inline comment # Git URIs git+https://github.com/git/uri git+https://github.com/sub/dir#subdirectory=subdir # Requirements files -r {relative_req} --requirement {absolute_req} # Constraints files -c {relative_con} --constraint {absolute_con} # Line continuation line-cont\ ==\ 1.0 # Line continuation with spaces line-cont-space \ == \ 1.0 # Line continuation with a blank line line-cont-blank\ # Line continuation at EOF line-cont-eof\ """.strip() try: os.chdir(tmpdir) root_req = tmpdir.join("requirements.txt") # Requirements files rel_req = tmpdir.join("relative_req.txt") abs_req = tmpdir.join("absolute_req.txt") # Constraints files rel_con = tmpdir.join("relative_con.txt") abs_con = tmpdir.join("absolute_con.txt") # pip's requirements parser collapses an absolute requirements file path: # https://github.com/pypa/pip/issues/10121 # As a workaround, use a relative path on Windows. absolute_req = abs_req.basename if os.name == "nt" else abs_req.strpath absolute_con = abs_con.basename if os.name == "nt" else abs_con.strpath root_req.write( root_req_src.format( relative_req=rel_req.basename, absolute_req=absolute_req, relative_con=rel_con.basename, absolute_con=absolute_con, )) rel_req.write("rel-req-xxx\nrel-req-yyy") abs_req.write("abs-req-zzz") rel_con.write("rel-con-xxx\nrel-con-yyy") abs_con.write("abs-con-zzz") expected_cons = [ "rel-con-xxx", "rel-con-yyy", "abs-con-zzz", ] expected_reqs = [ "noverspec", "no-ver-spec", "verspec<1.0", "ver-spec == 2.0", 'env-marker; python_version < "3.8"', "inline-comm", "inlinecomm", "git+https://github.com/git/uri", "git+https://github.com/sub/dir#subdirectory=subdir", "rel-req-xxx", "rel-req-yyy", "abs-req-zzz", "line-cont==1.0", "line-cont-space == 1.0", "line-cont-blank", "line-cont-eof", ] parsed_reqs = list( _parse_requirements(root_req.basename, is_constraint=False)) pip_reqs = list( pip_parse_requirements(root_req.basename, session=PipSession())) # Requirements assert [r.req_str for r in parsed_reqs if not r.is_constraint] == expected_reqs assert [r.requirement for r in pip_reqs if not r.constraint] == expected_reqs # Constraints assert [r.req_str for r in parsed_reqs if r.is_constraint] == expected_cons assert [r.requirement for r in pip_reqs if r.constraint] == expected_cons finally: os.chdir(request.config.invocation_dir)