req_file.py 17 KB

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