def preprocess(content, options): """Split, filter, and join lines, and return a line iterator :param content: the content of the requirements file :param options: cli options """ lines_enum = enumerate(content.splitlines(), start=1) lines_enum = join_lines(lines_enum) lines_enum = ignore_comments(lines_enum) lines_enum = skip_regex(lines_enum, options) return lines_enum
def parse_requirements(filename, finder=None, comes_from=None, options=None, session=None, constraint=False, wheel_cache=None): """Parse a requirements file and yield InstallRequirement instances. :param filename: Path or url of requirements file. :param finder: Instance of pip.index.PackageFinder. :param comes_from: Origin description of requirements. :param options: Global options. :param session: Instance of pip.download.PipSession. :param constraint: If true, parsing a constraint file rather than requirements file. :param wheel_cache: Instance of pip.wheel.WheelCache """ if session is None: raise TypeError( "parse_requirements() missing 1 required keyword argument: " "'session'" ) _, content = get_file_content( filename, comes_from=comes_from, session=session ) lines = content.splitlines() lines = ignore_comments(lines) lines = join_lines(lines) lines = skip_regex(lines, options) lines = expand_env_variables(lines) kwargs = {} # The constraint keyword was only added in 7.1+ if tuple(pip.__version__.split('.')[:2]) > ('7', '0'): kwargs['constraint'] = constraint for line_number, line in enumerate(lines, 1): req_iter = process_line(line, filename, line_number, finder, comes_from, options, session, wheel_cache, **kwargs) for req in req_iter: yield req
def test_ignore_line(self): lines = [(1, ''), (2, 'req1'), (3, 'req2')] result = ignore_comments(lines) assert list(result) == [(2, 'req1'), (3, 'req2')]
def test_ignore_comment(self): lines = [(1, 'req1'), (2, '# comment'), (3, 'req2')] result = ignore_comments(lines) assert list(result) == [(1, 'req1'), (3, 'req2')]
def test_strip_comment(self): lines = [(1, 'req1'), (2, 'req # comment'), (3, 'req2')] result = ignore_comments(lines) assert list(result) == [(1, 'req1'), (2, 'req'), (3, 'req2')]
def test_strip_comment(self): lines = ['req1', '# comment', 'req2'] result = ignore_comments(lines) assert list(result) == ['req1', 'req2']
def test_strip_empty_line(self): lines = ['req1', '', 'req2'] result = ignore_comments(lines) assert list(result) == ['req1', 'req2']