req_file.py 18 KB

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