req_file.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. """
  2. Requirements file parsing
  3. """
  4. from __future__ import annotations
  5. import codecs
  6. import locale
  7. import logging
  8. import optparse
  9. import os
  10. import re
  11. import shlex
  12. import sys
  13. import urllib.parse
  14. from collections.abc import Generator, Iterable
  15. from dataclasses import dataclass
  16. from optparse import Values
  17. from typing import (
  18. TYPE_CHECKING,
  19. Any,
  20. Callable,
  21. NoReturn,
  22. )
  23. from pip._internal.cli import cmdoptions
  24. from pip._internal.exceptions import InstallationError, RequirementsFileParseError
  25. from pip._internal.models.release_control import ReleaseControl
  26. from pip._internal.models.search_scope import SearchScope
  27. if TYPE_CHECKING:
  28. from pip._internal.index.package_finder import PackageFinder
  29. from pip._internal.network.session import PipSession
  30. __all__ = ["parse_requirements"]
  31. ReqFileLines = Iterable[tuple[int, str]]
  32. LineParser = Callable[[str], tuple[str, Values]]
  33. SCHEME_RE = re.compile(r"^(http|https|file):", re.I)
  34. COMMENT_RE = re.compile(r"(^|\s+)#.*$")
  35. # Matches environment variable-style values in '${MY_VARIABLE_1}' with the
  36. # variable name consisting of only uppercase letters, digits or the '_'
  37. # (underscore). This follows the POSIX standard defined in IEEE Std 1003.1,
  38. # 2013 Edition.
  39. ENV_VAR_RE = re.compile(r"(?P<var>\$\{(?P<name>[A-Z0-9_]+)\})")
  40. SUPPORTED_OPTIONS: list[Callable[..., optparse.Option]] = [
  41. cmdoptions.index_url,
  42. cmdoptions.extra_index_url,
  43. cmdoptions.no_index,
  44. cmdoptions.constraints,
  45. cmdoptions.requirements,
  46. cmdoptions.editable,
  47. cmdoptions.find_links,
  48. cmdoptions.no_binary,
  49. cmdoptions.only_binary,
  50. cmdoptions.prefer_binary,
  51. cmdoptions.require_hashes,
  52. cmdoptions.pre,
  53. cmdoptions.all_releases,
  54. cmdoptions.only_final,
  55. cmdoptions.trusted_host,
  56. cmdoptions.use_new_feature,
  57. ]
  58. # options to be passed to requirements
  59. SUPPORTED_OPTIONS_REQ: list[Callable[..., optparse.Option]] = [
  60. cmdoptions.hash,
  61. cmdoptions.config_settings,
  62. ]
  63. SUPPORTED_OPTIONS_EDITABLE_REQ: list[Callable[..., optparse.Option]] = [
  64. cmdoptions.config_settings,
  65. ]
  66. # the 'dest' string values
  67. SUPPORTED_OPTIONS_REQ_DEST = [str(o().dest) for o in SUPPORTED_OPTIONS_REQ]
  68. SUPPORTED_OPTIONS_EDITABLE_REQ_DEST = [
  69. str(o().dest) for o in SUPPORTED_OPTIONS_EDITABLE_REQ
  70. ]
  71. # order of BOMS is important: codecs.BOM_UTF16_LE is a prefix of codecs.BOM_UTF32_LE
  72. # so data.startswith(BOM_UTF16_LE) would be true for UTF32_LE data
  73. BOMS: list[tuple[bytes, str]] = [
  74. (codecs.BOM_UTF8, "utf-8"),
  75. (codecs.BOM_UTF32, "utf-32"),
  76. (codecs.BOM_UTF32_BE, "utf-32-be"),
  77. (codecs.BOM_UTF32_LE, "utf-32-le"),
  78. (codecs.BOM_UTF16, "utf-16"),
  79. (codecs.BOM_UTF16_BE, "utf-16-be"),
  80. (codecs.BOM_UTF16_LE, "utf-16-le"),
  81. ]
  82. PEP263_ENCODING_RE = re.compile(rb"coding[:=]\s*([-\w.]+)")
  83. DEFAULT_ENCODING = "utf-8"
  84. logger = logging.getLogger(__name__)
  85. @dataclass(frozen=True)
  86. class ParsedRequirement:
  87. # TODO: replace this with slots=True when dropping Python 3.9 support.
  88. __slots__ = (
  89. "requirement",
  90. "is_editable",
  91. "comes_from",
  92. "constraint",
  93. "options",
  94. "line_source",
  95. )
  96. requirement: str
  97. is_editable: bool
  98. comes_from: str
  99. constraint: bool
  100. options: dict[str, Any] | None
  101. line_source: str | None
  102. @dataclass(frozen=True)
  103. class ParsedLine:
  104. __slots__ = ("filename", "lineno", "args", "opts", "constraint")
  105. filename: str
  106. lineno: int
  107. args: str
  108. opts: Values
  109. constraint: bool
  110. @property
  111. def is_editable(self) -> bool:
  112. return bool(self.opts.editables)
  113. @property
  114. def requirement(self) -> str | None:
  115. if self.args:
  116. return self.args
  117. elif self.is_editable:
  118. # We don't support multiple -e on one line
  119. return self.opts.editables[0]
  120. return None
  121. def parse_requirements(
  122. filename: str,
  123. session: PipSession,
  124. finder: PackageFinder | None = None,
  125. options: optparse.Values | None = None,
  126. constraint: bool = False,
  127. ) -> Generator[ParsedRequirement, None, None]:
  128. """Parse a requirements file and yield ParsedRequirement instances.
  129. :param filename: Path or url of requirements file.
  130. :param session: PipSession instance.
  131. :param finder: Instance of pip.index.PackageFinder.
  132. :param options: cli options.
  133. :param constraint: If true, parsing a constraint file rather than
  134. requirements file.
  135. """
  136. line_parser = get_line_parser(finder)
  137. parser = RequirementsFileParser(session, line_parser)
  138. for parsed_line in parser.parse(filename, constraint):
  139. parsed_req = handle_line(
  140. parsed_line, options=options, finder=finder, session=session
  141. )
  142. if parsed_req is not None:
  143. yield parsed_req
  144. def preprocess(content: str) -> ReqFileLines:
  145. """Split, filter, and join lines, and return a line iterator
  146. :param content: the content of the requirements file
  147. """
  148. lines_enum: ReqFileLines = enumerate(content.splitlines(), start=1)
  149. lines_enum = join_lines(lines_enum)
  150. lines_enum = ignore_comments(lines_enum)
  151. lines_enum = expand_env_variables(lines_enum)
  152. return lines_enum
  153. def handle_requirement_line(
  154. line: ParsedLine,
  155. options: optparse.Values | None = None,
  156. ) -> ParsedRequirement:
  157. # preserve for the nested code path
  158. line_comes_from = "{} {} (line {})".format(
  159. "-c" if line.constraint else "-r",
  160. line.filename,
  161. line.lineno,
  162. )
  163. assert line.requirement is not None
  164. # get the options that apply to requirements
  165. if line.is_editable:
  166. supported_dest = SUPPORTED_OPTIONS_EDITABLE_REQ_DEST
  167. else:
  168. supported_dest = SUPPORTED_OPTIONS_REQ_DEST
  169. req_options = {}
  170. for dest in supported_dest:
  171. if dest in line.opts.__dict__ and line.opts.__dict__[dest]:
  172. req_options[dest] = line.opts.__dict__[dest]
  173. line_source = f"line {line.lineno} of {line.filename}"
  174. return ParsedRequirement(
  175. requirement=line.requirement,
  176. is_editable=line.is_editable,
  177. comes_from=line_comes_from,
  178. constraint=line.constraint,
  179. options=req_options,
  180. line_source=line_source,
  181. )
  182. def handle_option_line(
  183. opts: Values,
  184. filename: str,
  185. lineno: int,
  186. finder: PackageFinder | None = None,
  187. options: optparse.Values | None = None,
  188. session: PipSession | None = None,
  189. ) -> None:
  190. if opts.hashes:
  191. logger.warning(
  192. "%s line %s has --hash but no requirement, and will be ignored.",
  193. filename,
  194. lineno,
  195. )
  196. if options:
  197. # percolate options upward
  198. if opts.require_hashes:
  199. options.require_hashes = opts.require_hashes
  200. if opts.features_enabled:
  201. options.features_enabled.extend(
  202. f for f in opts.features_enabled if f not in options.features_enabled
  203. )
  204. # set finder options
  205. if finder:
  206. find_links = finder.find_links
  207. index_urls = finder.index_urls
  208. no_index = finder.search_scope.no_index
  209. if opts.no_index is True:
  210. no_index = True
  211. index_urls = []
  212. if opts.index_url and not no_index:
  213. index_urls = [opts.index_url]
  214. if opts.extra_index_urls and not no_index:
  215. index_urls.extend(opts.extra_index_urls)
  216. if opts.find_links:
  217. # FIXME: it would be nice to keep track of the source
  218. # of the find_links: support a find-links local path
  219. # relative to a requirements file.
  220. value = opts.find_links[0]
  221. req_dir = os.path.dirname(os.path.abspath(filename))
  222. relative_to_reqs_file = os.path.join(req_dir, value)
  223. if os.path.exists(relative_to_reqs_file):
  224. value = relative_to_reqs_file
  225. find_links.append(value)
  226. if session:
  227. # We need to update the auth urls in session
  228. session.update_index_urls(index_urls)
  229. search_scope = SearchScope(
  230. find_links=find_links,
  231. index_urls=index_urls,
  232. no_index=no_index,
  233. )
  234. finder.search_scope = search_scope
  235. # Transform --pre into --all-releases :all:
  236. if opts.pre:
  237. if not opts.release_control:
  238. opts.release_control = ReleaseControl()
  239. opts.release_control.all_releases.add(":all:")
  240. if opts.release_control:
  241. if not finder.release_control:
  242. # First time seeing release_control, set it on finder
  243. finder.set_release_control(opts.release_control)
  244. if opts.prefer_binary:
  245. finder.set_prefer_binary()
  246. if session:
  247. for host in opts.trusted_hosts or []:
  248. source = f"line {lineno} of {filename}"
  249. session.add_trusted_host(host, source=source)
  250. def handle_line(
  251. line: ParsedLine,
  252. options: optparse.Values | None = None,
  253. finder: PackageFinder | None = None,
  254. session: PipSession | None = None,
  255. ) -> ParsedRequirement | None:
  256. """Handle a single parsed requirements line; This can result in
  257. creating/yielding requirements, or updating the finder.
  258. :param line: The parsed line to be processed.
  259. :param options: CLI options.
  260. :param finder: The finder - updated by non-requirement lines.
  261. :param session: The session - updated by non-requirement lines.
  262. Returns a ParsedRequirement object if the line is a requirement line,
  263. otherwise returns None.
  264. For lines that contain requirements, the only options that have an effect
  265. are from SUPPORTED_OPTIONS_REQ, and they are scoped to the
  266. requirement. Other options from SUPPORTED_OPTIONS may be present, but are
  267. ignored.
  268. For lines that do not contain requirements, the only options that have an
  269. effect are from SUPPORTED_OPTIONS. Options from SUPPORTED_OPTIONS_REQ may
  270. be present, but are ignored. These lines may contain multiple options
  271. (although our docs imply only one is supported), and all our parsed and
  272. affect the finder.
  273. """
  274. if line.requirement is not None:
  275. parsed_req = handle_requirement_line(line, options)
  276. return parsed_req
  277. else:
  278. handle_option_line(
  279. line.opts,
  280. line.filename,
  281. line.lineno,
  282. finder,
  283. options,
  284. session,
  285. )
  286. return None
  287. class RequirementsFileParser:
  288. def __init__(
  289. self,
  290. session: PipSession,
  291. line_parser: LineParser,
  292. ) -> None:
  293. self._session = session
  294. self._line_parser = line_parser
  295. def parse(
  296. self, filename: str, constraint: bool
  297. ) -> Generator[ParsedLine, None, None]:
  298. """Parse a given file, yielding parsed lines."""
  299. yield from self._parse_and_recurse(
  300. filename, constraint, [{os.path.abspath(filename): None}]
  301. )
  302. def _parse_and_recurse(
  303. self,
  304. filename: str,
  305. constraint: bool,
  306. parsed_files_stack: list[dict[str, str | None]],
  307. ) -> Generator[ParsedLine, None, None]:
  308. for line in self._parse_file(filename, constraint):
  309. if line.requirement is None and (
  310. line.opts.requirements or line.opts.constraints
  311. ):
  312. # parse a nested requirements file
  313. if line.opts.requirements:
  314. req_path = line.opts.requirements[0]
  315. nested_constraint = False
  316. else:
  317. req_path = line.opts.constraints[0]
  318. nested_constraint = True
  319. # original file is over http
  320. if SCHEME_RE.search(filename):
  321. # do a url join so relative paths work
  322. req_path = urllib.parse.urljoin(filename, req_path)
  323. # original file and nested file are paths
  324. elif not SCHEME_RE.search(req_path):
  325. # do a join so relative paths work
  326. # and then abspath so that we can identify recursive references
  327. req_path = os.path.abspath(
  328. os.path.join(
  329. os.path.dirname(filename),
  330. req_path,
  331. )
  332. )
  333. parsed_files = parsed_files_stack[0]
  334. if req_path in parsed_files:
  335. initial_file = parsed_files[req_path]
  336. tail = (
  337. f" and again in {initial_file}"
  338. if initial_file is not None
  339. else ""
  340. )
  341. raise RequirementsFileParseError(
  342. f"{req_path} recursively references itself in {filename}{tail}"
  343. )
  344. # Keeping a track where was each file first included in
  345. new_parsed_files = parsed_files.copy()
  346. new_parsed_files[req_path] = filename
  347. yield from self._parse_and_recurse(
  348. req_path, nested_constraint, [new_parsed_files, *parsed_files_stack]
  349. )
  350. else:
  351. yield line
  352. def _parse_file(
  353. self, filename: str, constraint: bool
  354. ) -> Generator[ParsedLine, None, None]:
  355. _, content = get_file_content(filename, self._session)
  356. lines_enum = preprocess(content)
  357. for line_number, line in lines_enum:
  358. try:
  359. args_str, opts = self._line_parser(line)
  360. except OptionParsingError as e:
  361. # add offending line
  362. msg = f"Invalid requirement: {line}\n{e.msg}"
  363. raise RequirementsFileParseError(msg)
  364. yield ParsedLine(
  365. filename,
  366. line_number,
  367. args_str,
  368. opts,
  369. constraint,
  370. )
  371. def get_line_parser(finder: PackageFinder | None) -> LineParser:
  372. def parse_line(line: str) -> tuple[str, Values]:
  373. # Build new parser for each line since it accumulates appendable
  374. # options.
  375. parser = build_parser()
  376. defaults = parser.get_default_values()
  377. defaults.index_url = None
  378. if finder:
  379. defaults.format_control = finder.format_control
  380. defaults.release_control = finder.release_control
  381. args_str, options_str = break_args_options(line)
  382. try:
  383. options = shlex.split(options_str)
  384. except ValueError as e:
  385. raise OptionParsingError(f"Could not split options: {options_str}") from e
  386. opts, _ = parser.parse_args(options, defaults)
  387. return args_str, opts
  388. return parse_line
  389. def break_args_options(line: str) -> tuple[str, str]:
  390. """Break up the line into an args and options string. We only want to shlex
  391. (and then optparse) the options, not the args. args can contain markers
  392. which are corrupted by shlex.
  393. """
  394. tokens = line.split(" ")
  395. args = []
  396. options = tokens[:]
  397. for token in tokens:
  398. if token.startswith(("-", "--")):
  399. break
  400. else:
  401. args.append(token)
  402. options.pop(0)
  403. return " ".join(args), " ".join(options)
  404. class OptionParsingError(Exception):
  405. def __init__(self, msg: str) -> None:
  406. self.msg = msg
  407. def build_parser() -> optparse.OptionParser:
  408. """
  409. Return a parser for parsing requirement lines
  410. """
  411. parser = optparse.OptionParser(add_help_option=False)
  412. option_factories = SUPPORTED_OPTIONS + SUPPORTED_OPTIONS_REQ
  413. for option_factory in option_factories:
  414. option = option_factory()
  415. parser.add_option(option)
  416. # By default optparse sys.exits on parsing errors. We want to wrap
  417. # that in our own exception.
  418. def parser_exit(self: Any, msg: str) -> NoReturn:
  419. raise OptionParsingError(msg)
  420. # NOTE: mypy disallows assigning to a method
  421. # https://github.com/python/mypy/issues/2427
  422. parser.exit = parser_exit # type: ignore
  423. return parser
  424. def join_lines(lines_enum: ReqFileLines) -> ReqFileLines:
  425. """Joins a line ending in '\' with the previous line (except when following
  426. comments). The joined line takes on the index of the first line.
  427. """
  428. primary_line_number = None
  429. new_line: list[str] = []
  430. for line_number, line in lines_enum:
  431. if not line.endswith("\\") or COMMENT_RE.match(line):
  432. if COMMENT_RE.match(line):
  433. # this ensures comments are always matched later
  434. line = " " + line
  435. if new_line:
  436. new_line.append(line)
  437. assert primary_line_number is not None
  438. yield primary_line_number, "".join(new_line)
  439. new_line = []
  440. else:
  441. yield line_number, line
  442. else:
  443. if not new_line:
  444. primary_line_number = line_number
  445. new_line.append(line.strip("\\"))
  446. # last line contains \
  447. if new_line:
  448. assert primary_line_number is not None
  449. yield primary_line_number, "".join(new_line)
  450. # TODO: handle space after '\'.
  451. def ignore_comments(lines_enum: ReqFileLines) -> ReqFileLines:
  452. """
  453. Strips comments and filter empty lines.
  454. """
  455. for line_number, line in lines_enum:
  456. line = COMMENT_RE.sub("", line)
  457. line = line.strip()
  458. if line:
  459. yield line_number, line
  460. def expand_env_variables(lines_enum: ReqFileLines) -> ReqFileLines:
  461. """Replace all environment variables that can be retrieved via `os.getenv`.
  462. The only allowed format for environment variables defined in the
  463. requirement file is `${MY_VARIABLE_1}` to ensure two things:
  464. 1. Strings that contain a `$` aren't accidentally (partially) expanded.
  465. 2. Ensure consistency across platforms for requirement files.
  466. These points are the result of a discussion on the `github pull
  467. request #3514 <https://github.com/pypa/pip/pull/3514>`_.
  468. Valid characters in variable names follow the `POSIX standard
  469. <http://pubs.opengroup.org/onlinepubs/9699919799/>`_ and are limited
  470. to uppercase letter, digits and the `_` (underscore).
  471. """
  472. for line_number, line in lines_enum:
  473. for env_var, var_name in ENV_VAR_RE.findall(line):
  474. value = os.getenv(var_name)
  475. if not value:
  476. continue
  477. line = line.replace(env_var, value)
  478. yield line_number, line
  479. def get_file_content(url: str, session: PipSession) -> tuple[str, str]:
  480. """Gets the content of a file; it may be a filename, file: URL, or
  481. http: URL. Returns (location, content). Content is unicode.
  482. Respects # -*- coding: declarations on the retrieved files.
  483. :param url: File path or url.
  484. :param session: PipSession instance.
  485. """
  486. scheme = urllib.parse.urlsplit(url).scheme
  487. # Pip has special support for file:// URLs (LocalFSAdapter).
  488. if scheme in ["http", "https", "file"]:
  489. # Delay importing heavy network modules until absolutely necessary.
  490. from pip._internal.network.utils import raise_for_status
  491. resp = session.get(url)
  492. raise_for_status(resp)
  493. return resp.url, resp.text
  494. # Assume this is a bare path.
  495. try:
  496. with open(url, "rb") as f:
  497. raw_content = f.read()
  498. except OSError as exc:
  499. raise InstallationError(f"Could not open requirements file: {exc}")
  500. content = _decode_req_file(raw_content, url)
  501. return url, content
  502. def _decode_req_file(data: bytes, url: str) -> str:
  503. for bom, encoding in BOMS:
  504. if data.startswith(bom):
  505. return data[len(bom) :].decode(encoding)
  506. for line in data.split(b"\n")[:2]:
  507. if line[0:1] == b"#":
  508. result = PEP263_ENCODING_RE.search(line)
  509. if result is not None:
  510. encoding = result.groups()[0].decode("ascii")
  511. return data.decode(encoding)
  512. try:
  513. return data.decode(DEFAULT_ENCODING)
  514. except UnicodeDecodeError:
  515. locale_encoding = locale.getpreferredencoding(False) or sys.getdefaultencoding()
  516. logging.warning(
  517. "unable to decode data from %s with default encoding %s, "
  518. "falling back to encoding from locale: %s. "
  519. "If this is intentional you should specify the encoding with a "
  520. "PEP-263 style comment, e.g. '# -*- coding: %s -*-'",
  521. url,
  522. DEFAULT_ENCODING,
  523. locale_encoding,
  524. locale_encoding,
  525. )
  526. return data.decode(locale_encoding)