dist.py 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. """distutils.dist
  2. Provides the Distribution class, which represents the module distribution
  3. being built/installed/distributed.
  4. """
  5. import sys
  6. import os
  7. import re
  8. import pathlib
  9. import contextlib
  10. import logging
  11. from email import message_from_file
  12. try:
  13. import warnings
  14. except ImportError:
  15. warnings = None
  16. from .errors import (
  17. DistutilsOptionError,
  18. DistutilsModuleError,
  19. DistutilsArgError,
  20. DistutilsClassError,
  21. )
  22. from .fancy_getopt import FancyGetopt, translate_longopt
  23. from .util import check_environ, strtobool, rfc822_escape
  24. from ._log import log
  25. from .debug import DEBUG
  26. # Regex to define acceptable Distutils command names. This is not *quite*
  27. # the same as a Python NAME -- I don't allow leading underscores. The fact
  28. # that they're very similar is no coincidence; the default naming scheme is
  29. # to look for a Python module named after the command.
  30. command_re = re.compile(r'^[a-zA-Z]([a-zA-Z0-9_]*)$')
  31. def _ensure_list(value, fieldname):
  32. if isinstance(value, str):
  33. # a string containing comma separated values is okay. It will
  34. # be converted to a list by Distribution.finalize_options().
  35. pass
  36. elif not isinstance(value, list):
  37. # passing a tuple or an iterator perhaps, warn and convert
  38. typename = type(value).__name__
  39. msg = "Warning: '{fieldname}' should be a list, got type '{typename}'"
  40. msg = msg.format(**locals())
  41. log.warning(msg)
  42. value = list(value)
  43. return value
  44. class Distribution:
  45. """The core of the Distutils. Most of the work hiding behind 'setup'
  46. is really done within a Distribution instance, which farms the work out
  47. to the Distutils commands specified on the command line.
  48. Setup scripts will almost never instantiate Distribution directly,
  49. unless the 'setup()' function is totally inadequate to their needs.
  50. However, it is conceivable that a setup script might wish to subclass
  51. Distribution for some specialized purpose, and then pass the subclass
  52. to 'setup()' as the 'distclass' keyword argument. If so, it is
  53. necessary to respect the expectations that 'setup' has of Distribution.
  54. See the code for 'setup()', in core.py, for details.
  55. """
  56. # 'global_options' describes the command-line options that may be
  57. # supplied to the setup script prior to any actual commands.
  58. # Eg. "./setup.py -n" or "./setup.py --quiet" both take advantage of
  59. # these global options. This list should be kept to a bare minimum,
  60. # since every global option is also valid as a command option -- and we
  61. # don't want to pollute the commands with too many options that they
  62. # have minimal control over.
  63. # The fourth entry for verbose means that it can be repeated.
  64. global_options = [
  65. ('verbose', 'v', "run verbosely (default)", 1),
  66. ('quiet', 'q', "run quietly (turns verbosity off)"),
  67. ('dry-run', 'n', "don't actually do anything"),
  68. ('help', 'h', "show detailed help message"),
  69. ('no-user-cfg', None, 'ignore pydistutils.cfg in your home directory'),
  70. ]
  71. # 'common_usage' is a short (2-3 line) string describing the common
  72. # usage of the setup script.
  73. common_usage = """\
  74. Common commands: (see '--help-commands' for more)
  75. setup.py build will build the package underneath 'build/'
  76. setup.py install will install the package
  77. """
  78. # options that are not propagated to the commands
  79. display_options = [
  80. ('help-commands', None, "list all available commands"),
  81. ('name', None, "print package name"),
  82. ('version', 'V', "print package version"),
  83. ('fullname', None, "print <package name>-<version>"),
  84. ('author', None, "print the author's name"),
  85. ('author-email', None, "print the author's email address"),
  86. ('maintainer', None, "print the maintainer's name"),
  87. ('maintainer-email', None, "print the maintainer's email address"),
  88. ('contact', None, "print the maintainer's name if known, else the author's"),
  89. (
  90. 'contact-email',
  91. None,
  92. "print the maintainer's email address if known, else the author's",
  93. ),
  94. ('url', None, "print the URL for this package"),
  95. ('license', None, "print the license of the package"),
  96. ('licence', None, "alias for --license"),
  97. ('description', None, "print the package description"),
  98. ('long-description', None, "print the long package description"),
  99. ('platforms', None, "print the list of platforms"),
  100. ('classifiers', None, "print the list of classifiers"),
  101. ('keywords', None, "print the list of keywords"),
  102. ('provides', None, "print the list of packages/modules provided"),
  103. ('requires', None, "print the list of packages/modules required"),
  104. ('obsoletes', None, "print the list of packages/modules made obsolete"),
  105. ]
  106. display_option_names = [translate_longopt(x[0]) for x in display_options]
  107. # negative options are options that exclude other options
  108. negative_opt = {'quiet': 'verbose'}
  109. # -- Creation/initialization methods -------------------------------
  110. def __init__(self, attrs=None): # noqa: C901
  111. """Construct a new Distribution instance: initialize all the
  112. attributes of a Distribution, and then use 'attrs' (a dictionary
  113. mapping attribute names to values) to assign some of those
  114. attributes their "real" values. (Any attributes not mentioned in
  115. 'attrs' will be assigned to some null value: 0, None, an empty list
  116. or dictionary, etc.) Most importantly, initialize the
  117. 'command_obj' attribute to the empty dictionary; this will be
  118. filled in with real command objects by 'parse_command_line()'.
  119. """
  120. # Default values for our command-line options
  121. self.verbose = 1
  122. self.dry_run = 0
  123. self.help = 0
  124. for attr in self.display_option_names:
  125. setattr(self, attr, 0)
  126. # Store the distribution meta-data (name, version, author, and so
  127. # forth) in a separate object -- we're getting to have enough
  128. # information here (and enough command-line options) that it's
  129. # worth it. Also delegate 'get_XXX()' methods to the 'metadata'
  130. # object in a sneaky and underhanded (but efficient!) way.
  131. self.metadata = DistributionMetadata()
  132. for basename in self.metadata._METHOD_BASENAMES:
  133. method_name = "get_" + basename
  134. setattr(self, method_name, getattr(self.metadata, method_name))
  135. # 'cmdclass' maps command names to class objects, so we
  136. # can 1) quickly figure out which class to instantiate when
  137. # we need to create a new command object, and 2) have a way
  138. # for the setup script to override command classes
  139. self.cmdclass = {}
  140. # 'command_packages' is a list of packages in which commands
  141. # are searched for. The factory for command 'foo' is expected
  142. # to be named 'foo' in the module 'foo' in one of the packages
  143. # named here. This list is searched from the left; an error
  144. # is raised if no named package provides the command being
  145. # searched for. (Always access using get_command_packages().)
  146. self.command_packages = None
  147. # 'script_name' and 'script_args' are usually set to sys.argv[0]
  148. # and sys.argv[1:], but they can be overridden when the caller is
  149. # not necessarily a setup script run from the command-line.
  150. self.script_name = None
  151. self.script_args = None
  152. # 'command_options' is where we store command options between
  153. # parsing them (from config files, the command-line, etc.) and when
  154. # they are actually needed -- ie. when the command in question is
  155. # instantiated. It is a dictionary of dictionaries of 2-tuples:
  156. # command_options = { command_name : { option : (source, value) } }
  157. self.command_options = {}
  158. # 'dist_files' is the list of (command, pyversion, file) that
  159. # have been created by any dist commands run so far. This is
  160. # filled regardless of whether the run is dry or not. pyversion
  161. # gives sysconfig.get_python_version() if the dist file is
  162. # specific to a Python version, 'any' if it is good for all
  163. # Python versions on the target platform, and '' for a source
  164. # file. pyversion should not be used to specify minimum or
  165. # maximum required Python versions; use the metainfo for that
  166. # instead.
  167. self.dist_files = []
  168. # These options are really the business of various commands, rather
  169. # than of the Distribution itself. We provide aliases for them in
  170. # Distribution as a convenience to the developer.
  171. self.packages = None
  172. self.package_data = {}
  173. self.package_dir = None
  174. self.py_modules = None
  175. self.libraries = None
  176. self.headers = None
  177. self.ext_modules = None
  178. self.ext_package = None
  179. self.include_dirs = None
  180. self.extra_path = None
  181. self.scripts = None
  182. self.data_files = None
  183. self.password = ''
  184. # And now initialize bookkeeping stuff that can't be supplied by
  185. # the caller at all. 'command_obj' maps command names to
  186. # Command instances -- that's how we enforce that every command
  187. # class is a singleton.
  188. self.command_obj = {}
  189. # 'have_run' maps command names to boolean values; it keeps track
  190. # of whether we have actually run a particular command, to make it
  191. # cheap to "run" a command whenever we think we might need to -- if
  192. # it's already been done, no need for expensive filesystem
  193. # operations, we just check the 'have_run' dictionary and carry on.
  194. # It's only safe to query 'have_run' for a command class that has
  195. # been instantiated -- a false value will be inserted when the
  196. # command object is created, and replaced with a true value when
  197. # the command is successfully run. Thus it's probably best to use
  198. # '.get()' rather than a straight lookup.
  199. self.have_run = {}
  200. # Now we'll use the attrs dictionary (ultimately, keyword args from
  201. # the setup script) to possibly override any or all of these
  202. # distribution options.
  203. if attrs:
  204. # Pull out the set of command options and work on them
  205. # specifically. Note that this order guarantees that aliased
  206. # command options will override any supplied redundantly
  207. # through the general options dictionary.
  208. options = attrs.get('options')
  209. if options is not None:
  210. del attrs['options']
  211. for command, cmd_options in options.items():
  212. opt_dict = self.get_option_dict(command)
  213. for opt, val in cmd_options.items():
  214. opt_dict[opt] = ("setup script", val)
  215. if 'licence' in attrs:
  216. attrs['license'] = attrs['licence']
  217. del attrs['licence']
  218. msg = "'licence' distribution option is deprecated; use 'license'"
  219. if warnings is not None:
  220. warnings.warn(msg)
  221. else:
  222. sys.stderr.write(msg + "\n")
  223. # Now work on the rest of the attributes. Any attribute that's
  224. # not already defined is invalid!
  225. for key, val in attrs.items():
  226. if hasattr(self.metadata, "set_" + key):
  227. getattr(self.metadata, "set_" + key)(val)
  228. elif hasattr(self.metadata, key):
  229. setattr(self.metadata, key, val)
  230. elif hasattr(self, key):
  231. setattr(self, key, val)
  232. else:
  233. msg = "Unknown distribution option: %s" % repr(key)
  234. warnings.warn(msg)
  235. # no-user-cfg is handled before other command line args
  236. # because other args override the config files, and this
  237. # one is needed before we can load the config files.
  238. # If attrs['script_args'] wasn't passed, assume false.
  239. #
  240. # This also make sure we just look at the global options
  241. self.want_user_cfg = True
  242. if self.script_args is not None:
  243. for arg in self.script_args:
  244. if not arg.startswith('-'):
  245. break
  246. if arg == '--no-user-cfg':
  247. self.want_user_cfg = False
  248. break
  249. self.finalize_options()
  250. def get_option_dict(self, command):
  251. """Get the option dictionary for a given command. If that
  252. command's option dictionary hasn't been created yet, then create it
  253. and return the new dictionary; otherwise, return the existing
  254. option dictionary.
  255. """
  256. dict = self.command_options.get(command)
  257. if dict is None:
  258. dict = self.command_options[command] = {}
  259. return dict
  260. def dump_option_dicts(self, header=None, commands=None, indent=""):
  261. from pprint import pformat
  262. if commands is None: # dump all command option dicts
  263. commands = sorted(self.command_options.keys())
  264. if header is not None:
  265. self.announce(indent + header)
  266. indent = indent + " "
  267. if not commands:
  268. self.announce(indent + "no commands known yet")
  269. return
  270. for cmd_name in commands:
  271. opt_dict = self.command_options.get(cmd_name)
  272. if opt_dict is None:
  273. self.announce(indent + "no option dict for '%s' command" % cmd_name)
  274. else:
  275. self.announce(indent + "option dict for '%s' command:" % cmd_name)
  276. out = pformat(opt_dict)
  277. for line in out.split('\n'):
  278. self.announce(indent + " " + line)
  279. # -- Config file finding/parsing methods ---------------------------
  280. def find_config_files(self):
  281. """Find as many configuration files as should be processed for this
  282. platform, and return a list of filenames in the order in which they
  283. should be parsed. The filenames returned are guaranteed to exist
  284. (modulo nasty race conditions).
  285. There are multiple possible config files:
  286. - distutils.cfg in the Distutils installation directory (i.e.
  287. where the top-level Distutils __inst__.py file lives)
  288. - a file in the user's home directory named .pydistutils.cfg
  289. on Unix and pydistutils.cfg on Windows/Mac; may be disabled
  290. with the ``--no-user-cfg`` option
  291. - setup.cfg in the current directory
  292. - a file named by an environment variable
  293. """
  294. check_environ()
  295. files = [str(path) for path in self._gen_paths() if os.path.isfile(path)]
  296. if DEBUG:
  297. self.announce("using config files: %s" % ', '.join(files))
  298. return files
  299. def _gen_paths(self):
  300. # The system-wide Distutils config file
  301. sys_dir = pathlib.Path(sys.modules['distutils'].__file__).parent
  302. yield sys_dir / "distutils.cfg"
  303. # The per-user config file
  304. prefix = '.' * (os.name == 'posix')
  305. filename = prefix + 'pydistutils.cfg'
  306. if self.want_user_cfg:
  307. yield pathlib.Path('~').expanduser() / filename
  308. # All platforms support local setup.cfg
  309. yield pathlib.Path('setup.cfg')
  310. # Additional config indicated in the environment
  311. with contextlib.suppress(TypeError):
  312. yield pathlib.Path(os.getenv("DIST_EXTRA_CONFIG"))
  313. def parse_config_files(self, filenames=None): # noqa: C901
  314. from configparser import ConfigParser
  315. # Ignore install directory options if we have a venv
  316. if sys.prefix != sys.base_prefix:
  317. ignore_options = [
  318. 'install-base',
  319. 'install-platbase',
  320. 'install-lib',
  321. 'install-platlib',
  322. 'install-purelib',
  323. 'install-headers',
  324. 'install-scripts',
  325. 'install-data',
  326. 'prefix',
  327. 'exec-prefix',
  328. 'home',
  329. 'user',
  330. 'root',
  331. ]
  332. else:
  333. ignore_options = []
  334. ignore_options = frozenset(ignore_options)
  335. if filenames is None:
  336. filenames = self.find_config_files()
  337. if DEBUG:
  338. self.announce("Distribution.parse_config_files():")
  339. parser = ConfigParser()
  340. for filename in filenames:
  341. if DEBUG:
  342. self.announce(" reading %s" % filename)
  343. parser.read(filename)
  344. for section in parser.sections():
  345. options = parser.options(section)
  346. opt_dict = self.get_option_dict(section)
  347. for opt in options:
  348. if opt != '__name__' and opt not in ignore_options:
  349. val = parser.get(section, opt)
  350. opt = opt.replace('-', '_')
  351. opt_dict[opt] = (filename, val)
  352. # Make the ConfigParser forget everything (so we retain
  353. # the original filenames that options come from)
  354. parser.__init__()
  355. # If there was a "global" section in the config file, use it
  356. # to set Distribution options.
  357. if 'global' in self.command_options:
  358. for opt, (src, val) in self.command_options['global'].items():
  359. alias = self.negative_opt.get(opt)
  360. try:
  361. if alias:
  362. setattr(self, alias, not strtobool(val))
  363. elif opt in ('verbose', 'dry_run'): # ugh!
  364. setattr(self, opt, strtobool(val))
  365. else:
  366. setattr(self, opt, val)
  367. except ValueError as msg:
  368. raise DistutilsOptionError(msg)
  369. # -- Command-line parsing methods ----------------------------------
  370. def parse_command_line(self):
  371. """Parse the setup script's command line, taken from the
  372. 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
  373. -- see 'setup()' in core.py). This list is first processed for
  374. "global options" -- options that set attributes of the Distribution
  375. instance. Then, it is alternately scanned for Distutils commands
  376. and options for that command. Each new command terminates the
  377. options for the previous command. The allowed options for a
  378. command are determined by the 'user_options' attribute of the
  379. command class -- thus, we have to be able to load command classes
  380. in order to parse the command line. Any error in that 'options'
  381. attribute raises DistutilsGetoptError; any error on the
  382. command-line raises DistutilsArgError. If no Distutils commands
  383. were found on the command line, raises DistutilsArgError. Return
  384. true if command-line was successfully parsed and we should carry
  385. on with executing commands; false if no errors but we shouldn't
  386. execute commands (currently, this only happens if user asks for
  387. help).
  388. """
  389. #
  390. # We now have enough information to show the Macintosh dialog
  391. # that allows the user to interactively specify the "command line".
  392. #
  393. toplevel_options = self._get_toplevel_options()
  394. # We have to parse the command line a bit at a time -- global
  395. # options, then the first command, then its options, and so on --
  396. # because each command will be handled by a different class, and
  397. # the options that are valid for a particular class aren't known
  398. # until we have loaded the command class, which doesn't happen
  399. # until we know what the command is.
  400. self.commands = []
  401. parser = FancyGetopt(toplevel_options + self.display_options)
  402. parser.set_negative_aliases(self.negative_opt)
  403. parser.set_aliases({'licence': 'license'})
  404. args = parser.getopt(args=self.script_args, object=self)
  405. option_order = parser.get_option_order()
  406. logging.getLogger().setLevel(logging.WARN - 10 * self.verbose)
  407. # for display options we return immediately
  408. if self.handle_display_options(option_order):
  409. return
  410. while args:
  411. args = self._parse_command_opts(parser, args)
  412. if args is None: # user asked for help (and got it)
  413. return
  414. # Handle the cases of --help as a "global" option, ie.
  415. # "setup.py --help" and "setup.py --help command ...". For the
  416. # former, we show global options (--verbose, --dry-run, etc.)
  417. # and display-only options (--name, --version, etc.); for the
  418. # latter, we omit the display-only options and show help for
  419. # each command listed on the command line.
  420. if self.help:
  421. self._show_help(
  422. parser, display_options=len(self.commands) == 0, commands=self.commands
  423. )
  424. return
  425. # Oops, no commands found -- an end-user error
  426. if not self.commands:
  427. raise DistutilsArgError("no commands supplied")
  428. # All is well: return true
  429. return True
  430. def _get_toplevel_options(self):
  431. """Return the non-display options recognized at the top level.
  432. This includes options that are recognized *only* at the top
  433. level as well as options recognized for commands.
  434. """
  435. return self.global_options + [
  436. (
  437. "command-packages=",
  438. None,
  439. "list of packages that provide distutils commands",
  440. ),
  441. ]
  442. def _parse_command_opts(self, parser, args): # noqa: C901
  443. """Parse the command-line options for a single command.
  444. 'parser' must be a FancyGetopt instance; 'args' must be the list
  445. of arguments, starting with the current command (whose options
  446. we are about to parse). Returns a new version of 'args' with
  447. the next command at the front of the list; will be the empty
  448. list if there are no more commands on the command line. Returns
  449. None if the user asked for help on this command.
  450. """
  451. # late import because of mutual dependence between these modules
  452. from distutils.cmd import Command
  453. # Pull the current command from the head of the command line
  454. command = args[0]
  455. if not command_re.match(command):
  456. raise SystemExit("invalid command name '%s'" % command)
  457. self.commands.append(command)
  458. # Dig up the command class that implements this command, so we
  459. # 1) know that it's a valid command, and 2) know which options
  460. # it takes.
  461. try:
  462. cmd_class = self.get_command_class(command)
  463. except DistutilsModuleError as msg:
  464. raise DistutilsArgError(msg)
  465. # Require that the command class be derived from Command -- want
  466. # to be sure that the basic "command" interface is implemented.
  467. if not issubclass(cmd_class, Command):
  468. raise DistutilsClassError(
  469. "command class %s must subclass Command" % cmd_class
  470. )
  471. # Also make sure that the command object provides a list of its
  472. # known options.
  473. if not (
  474. hasattr(cmd_class, 'user_options')
  475. and isinstance(cmd_class.user_options, list)
  476. ):
  477. msg = (
  478. "command class %s must provide "
  479. "'user_options' attribute (a list of tuples)"
  480. )
  481. raise DistutilsClassError(msg % cmd_class)
  482. # If the command class has a list of negative alias options,
  483. # merge it in with the global negative aliases.
  484. negative_opt = self.negative_opt
  485. if hasattr(cmd_class, 'negative_opt'):
  486. negative_opt = negative_opt.copy()
  487. negative_opt.update(cmd_class.negative_opt)
  488. # Check for help_options in command class. They have a different
  489. # format (tuple of four) so we need to preprocess them here.
  490. if hasattr(cmd_class, 'help_options') and isinstance(
  491. cmd_class.help_options, list
  492. ):
  493. help_options = fix_help_options(cmd_class.help_options)
  494. else:
  495. help_options = []
  496. # All commands support the global options too, just by adding
  497. # in 'global_options'.
  498. parser.set_option_table(
  499. self.global_options + cmd_class.user_options + help_options
  500. )
  501. parser.set_negative_aliases(negative_opt)
  502. (args, opts) = parser.getopt(args[1:])
  503. if hasattr(opts, 'help') and opts.help:
  504. self._show_help(parser, display_options=0, commands=[cmd_class])
  505. return
  506. if hasattr(cmd_class, 'help_options') and isinstance(
  507. cmd_class.help_options, list
  508. ):
  509. help_option_found = 0
  510. for help_option, short, desc, func in cmd_class.help_options:
  511. if hasattr(opts, parser.get_attr_name(help_option)):
  512. help_option_found = 1
  513. if callable(func):
  514. func()
  515. else:
  516. raise DistutilsClassError(
  517. "invalid help function %r for help option '%s': "
  518. "must be a callable object (function, etc.)"
  519. % (func, help_option)
  520. )
  521. if help_option_found:
  522. return
  523. # Put the options from the command-line into their official
  524. # holding pen, the 'command_options' dictionary.
  525. opt_dict = self.get_option_dict(command)
  526. for name, value in vars(opts).items():
  527. opt_dict[name] = ("command line", value)
  528. return args
  529. def finalize_options(self):
  530. """Set final values for all the options on the Distribution
  531. instance, analogous to the .finalize_options() method of Command
  532. objects.
  533. """
  534. for attr in ('keywords', 'platforms'):
  535. value = getattr(self.metadata, attr)
  536. if value is None:
  537. continue
  538. if isinstance(value, str):
  539. value = [elm.strip() for elm in value.split(',')]
  540. setattr(self.metadata, attr, value)
  541. def _show_help(self, parser, global_options=1, display_options=1, commands=[]):
  542. """Show help for the setup script command-line in the form of
  543. several lists of command-line options. 'parser' should be a
  544. FancyGetopt instance; do not expect it to be returned in the
  545. same state, as its option table will be reset to make it
  546. generate the correct help text.
  547. If 'global_options' is true, lists the global options:
  548. --verbose, --dry-run, etc. If 'display_options' is true, lists
  549. the "display-only" options: --name, --version, etc. Finally,
  550. lists per-command help for every command name or command class
  551. in 'commands'.
  552. """
  553. # late import because of mutual dependence between these modules
  554. from distutils.core import gen_usage
  555. from distutils.cmd import Command
  556. if global_options:
  557. if display_options:
  558. options = self._get_toplevel_options()
  559. else:
  560. options = self.global_options
  561. parser.set_option_table(options)
  562. parser.print_help(self.common_usage + "\nGlobal options:")
  563. print('')
  564. if display_options:
  565. parser.set_option_table(self.display_options)
  566. parser.print_help(
  567. "Information display options (just display "
  568. + "information, ignore any commands)"
  569. )
  570. print('')
  571. for command in self.commands:
  572. if isinstance(command, type) and issubclass(command, Command):
  573. klass = command
  574. else:
  575. klass = self.get_command_class(command)
  576. if hasattr(klass, 'help_options') and isinstance(klass.help_options, list):
  577. parser.set_option_table(
  578. klass.user_options + fix_help_options(klass.help_options)
  579. )
  580. else:
  581. parser.set_option_table(klass.user_options)
  582. parser.print_help("Options for '%s' command:" % klass.__name__)
  583. print('')
  584. print(gen_usage(self.script_name))
  585. def handle_display_options(self, option_order):
  586. """If there were any non-global "display-only" options
  587. (--help-commands or the metadata display options) on the command
  588. line, display the requested info and return true; else return
  589. false.
  590. """
  591. from distutils.core import gen_usage
  592. # User just wants a list of commands -- we'll print it out and stop
  593. # processing now (ie. if they ran "setup --help-commands foo bar",
  594. # we ignore "foo bar").
  595. if self.help_commands:
  596. self.print_commands()
  597. print('')
  598. print(gen_usage(self.script_name))
  599. return 1
  600. # If user supplied any of the "display metadata" options, then
  601. # display that metadata in the order in which the user supplied the
  602. # metadata options.
  603. any_display_options = 0
  604. is_display_option = {}
  605. for option in self.display_options:
  606. is_display_option[option[0]] = 1
  607. for opt, val in option_order:
  608. if val and is_display_option.get(opt):
  609. opt = translate_longopt(opt)
  610. value = getattr(self.metadata, "get_" + opt)()
  611. if opt in ('keywords', 'platforms'):
  612. print(','.join(value))
  613. elif opt in ('classifiers', 'provides', 'requires', 'obsoletes'):
  614. print('\n'.join(value))
  615. else:
  616. print(value)
  617. any_display_options = 1
  618. return any_display_options
  619. def print_command_list(self, commands, header, max_length):
  620. """Print a subset of the list of all commands -- used by
  621. 'print_commands()'.
  622. """
  623. print(header + ":")
  624. for cmd in commands:
  625. klass = self.cmdclass.get(cmd)
  626. if not klass:
  627. klass = self.get_command_class(cmd)
  628. try:
  629. description = klass.description
  630. except AttributeError:
  631. description = "(no description available)"
  632. print(" %-*s %s" % (max_length, cmd, description))
  633. def print_commands(self):
  634. """Print out a help message listing all available commands with a
  635. description of each. The list is divided into "standard commands"
  636. (listed in distutils.command.__all__) and "extra commands"
  637. (mentioned in self.cmdclass, but not a standard command). The
  638. descriptions come from the command class attribute
  639. 'description'.
  640. """
  641. import distutils.command
  642. std_commands = distutils.command.__all__
  643. is_std = {}
  644. for cmd in std_commands:
  645. is_std[cmd] = 1
  646. extra_commands = []
  647. for cmd in self.cmdclass.keys():
  648. if not is_std.get(cmd):
  649. extra_commands.append(cmd)
  650. max_length = 0
  651. for cmd in std_commands + extra_commands:
  652. if len(cmd) > max_length:
  653. max_length = len(cmd)
  654. self.print_command_list(std_commands, "Standard commands", max_length)
  655. if extra_commands:
  656. print()
  657. self.print_command_list(extra_commands, "Extra commands", max_length)
  658. def get_command_list(self):
  659. """Get a list of (command, description) tuples.
  660. The list is divided into "standard commands" (listed in
  661. distutils.command.__all__) and "extra commands" (mentioned in
  662. self.cmdclass, but not a standard command). The descriptions come
  663. from the command class attribute 'description'.
  664. """
  665. # Currently this is only used on Mac OS, for the Mac-only GUI
  666. # Distutils interface (by Jack Jansen)
  667. import distutils.command
  668. std_commands = distutils.command.__all__
  669. is_std = {}
  670. for cmd in std_commands:
  671. is_std[cmd] = 1
  672. extra_commands = []
  673. for cmd in self.cmdclass.keys():
  674. if not is_std.get(cmd):
  675. extra_commands.append(cmd)
  676. rv = []
  677. for cmd in std_commands + extra_commands:
  678. klass = self.cmdclass.get(cmd)
  679. if not klass:
  680. klass = self.get_command_class(cmd)
  681. try:
  682. description = klass.description
  683. except AttributeError:
  684. description = "(no description available)"
  685. rv.append((cmd, description))
  686. return rv
  687. # -- Command class/object methods ----------------------------------
  688. def get_command_packages(self):
  689. """Return a list of packages from which commands are loaded."""
  690. pkgs = self.command_packages
  691. if not isinstance(pkgs, list):
  692. if pkgs is None:
  693. pkgs = ''
  694. pkgs = [pkg.strip() for pkg in pkgs.split(',') if pkg != '']
  695. if "distutils.command" not in pkgs:
  696. pkgs.insert(0, "distutils.command")
  697. self.command_packages = pkgs
  698. return pkgs
  699. def get_command_class(self, command):
  700. """Return the class that implements the Distutils command named by
  701. 'command'. First we check the 'cmdclass' dictionary; if the
  702. command is mentioned there, we fetch the class object from the
  703. dictionary and return it. Otherwise we load the command module
  704. ("distutils.command." + command) and fetch the command class from
  705. the module. The loaded class is also stored in 'cmdclass'
  706. to speed future calls to 'get_command_class()'.
  707. Raises DistutilsModuleError if the expected module could not be
  708. found, or if that module does not define the expected class.
  709. """
  710. klass = self.cmdclass.get(command)
  711. if klass:
  712. return klass
  713. for pkgname in self.get_command_packages():
  714. module_name = "{}.{}".format(pkgname, command)
  715. klass_name = command
  716. try:
  717. __import__(module_name)
  718. module = sys.modules[module_name]
  719. except ImportError:
  720. continue
  721. try:
  722. klass = getattr(module, klass_name)
  723. except AttributeError:
  724. raise DistutilsModuleError(
  725. "invalid command '%s' (no class '%s' in module '%s')"
  726. % (command, klass_name, module_name)
  727. )
  728. self.cmdclass[command] = klass
  729. return klass
  730. raise DistutilsModuleError("invalid command '%s'" % command)
  731. def get_command_obj(self, command, create=1):
  732. """Return the command object for 'command'. Normally this object
  733. is cached on a previous call to 'get_command_obj()'; if no command
  734. object for 'command' is in the cache, then we either create and
  735. return it (if 'create' is true) or return None.
  736. """
  737. cmd_obj = self.command_obj.get(command)
  738. if not cmd_obj and create:
  739. if DEBUG:
  740. self.announce(
  741. "Distribution.get_command_obj(): "
  742. "creating '%s' command object" % command
  743. )
  744. klass = self.get_command_class(command)
  745. cmd_obj = self.command_obj[command] = klass(self)
  746. self.have_run[command] = 0
  747. # Set any options that were supplied in config files
  748. # or on the command line. (NB. support for error
  749. # reporting is lame here: any errors aren't reported
  750. # until 'finalize_options()' is called, which means
  751. # we won't report the source of the error.)
  752. options = self.command_options.get(command)
  753. if options:
  754. self._set_command_options(cmd_obj, options)
  755. return cmd_obj
  756. def _set_command_options(self, command_obj, option_dict=None): # noqa: C901
  757. """Set the options for 'command_obj' from 'option_dict'. Basically
  758. this means copying elements of a dictionary ('option_dict') to
  759. attributes of an instance ('command').
  760. 'command_obj' must be a Command instance. If 'option_dict' is not
  761. supplied, uses the standard option dictionary for this command
  762. (from 'self.command_options').
  763. """
  764. command_name = command_obj.get_command_name()
  765. if option_dict is None:
  766. option_dict = self.get_option_dict(command_name)
  767. if DEBUG:
  768. self.announce(" setting options for '%s' command:" % command_name)
  769. for option, (source, value) in option_dict.items():
  770. if DEBUG:
  771. self.announce(" {} = {} (from {})".format(option, value, source))
  772. try:
  773. bool_opts = [translate_longopt(o) for o in command_obj.boolean_options]
  774. except AttributeError:
  775. bool_opts = []
  776. try:
  777. neg_opt = command_obj.negative_opt
  778. except AttributeError:
  779. neg_opt = {}
  780. try:
  781. is_string = isinstance(value, str)
  782. if option in neg_opt and is_string:
  783. setattr(command_obj, neg_opt[option], not strtobool(value))
  784. elif option in bool_opts and is_string:
  785. setattr(command_obj, option, strtobool(value))
  786. elif hasattr(command_obj, option):
  787. setattr(command_obj, option, value)
  788. else:
  789. raise DistutilsOptionError(
  790. "error in %s: command '%s' has no such option '%s'"
  791. % (source, command_name, option)
  792. )
  793. except ValueError as msg:
  794. raise DistutilsOptionError(msg)
  795. def reinitialize_command(self, command, reinit_subcommands=0):
  796. """Reinitializes a command to the state it was in when first
  797. returned by 'get_command_obj()': ie., initialized but not yet
  798. finalized. This provides the opportunity to sneak option
  799. values in programmatically, overriding or supplementing
  800. user-supplied values from the config files and command line.
  801. You'll have to re-finalize the command object (by calling
  802. 'finalize_options()' or 'ensure_finalized()') before using it for
  803. real.
  804. 'command' should be a command name (string) or command object. If
  805. 'reinit_subcommands' is true, also reinitializes the command's
  806. sub-commands, as declared by the 'sub_commands' class attribute (if
  807. it has one). See the "install" command for an example. Only
  808. reinitializes the sub-commands that actually matter, ie. those
  809. whose test predicates return true.
  810. Returns the reinitialized command object.
  811. """
  812. from distutils.cmd import Command
  813. if not isinstance(command, Command):
  814. command_name = command
  815. command = self.get_command_obj(command_name)
  816. else:
  817. command_name = command.get_command_name()
  818. if not command.finalized:
  819. return command
  820. command.initialize_options()
  821. command.finalized = 0
  822. self.have_run[command_name] = 0
  823. self._set_command_options(command)
  824. if reinit_subcommands:
  825. for sub in command.get_sub_commands():
  826. self.reinitialize_command(sub, reinit_subcommands)
  827. return command
  828. # -- Methods that operate on the Distribution ----------------------
  829. def announce(self, msg, level=logging.INFO):
  830. log.log(level, msg)
  831. def run_commands(self):
  832. """Run each command that was seen on the setup script command line.
  833. Uses the list of commands found and cache of command objects
  834. created by 'get_command_obj()'.
  835. """
  836. for cmd in self.commands:
  837. self.run_command(cmd)
  838. # -- Methods that operate on its Commands --------------------------
  839. def run_command(self, command):
  840. """Do whatever it takes to run a command (including nothing at all,
  841. if the command has already been run). Specifically: if we have
  842. already created and run the command named by 'command', return
  843. silently without doing anything. If the command named by 'command'
  844. doesn't even have a command object yet, create one. Then invoke
  845. 'run()' on that command object (or an existing one).
  846. """
  847. # Already been here, done that? then return silently.
  848. if self.have_run.get(command):
  849. return
  850. log.info("running %s", command)
  851. cmd_obj = self.get_command_obj(command)
  852. cmd_obj.ensure_finalized()
  853. cmd_obj.run()
  854. self.have_run[command] = 1
  855. # -- Distribution query methods ------------------------------------
  856. def has_pure_modules(self):
  857. return len(self.packages or self.py_modules or []) > 0
  858. def has_ext_modules(self):
  859. return self.ext_modules and len(self.ext_modules) > 0
  860. def has_c_libraries(self):
  861. return self.libraries and len(self.libraries) > 0
  862. def has_modules(self):
  863. return self.has_pure_modules() or self.has_ext_modules()
  864. def has_headers(self):
  865. return self.headers and len(self.headers) > 0
  866. def has_scripts(self):
  867. return self.scripts and len(self.scripts) > 0
  868. def has_data_files(self):
  869. return self.data_files and len(self.data_files) > 0
  870. def is_pure(self):
  871. return (
  872. self.has_pure_modules()
  873. and not self.has_ext_modules()
  874. and not self.has_c_libraries()
  875. )
  876. # -- Metadata query methods ----------------------------------------
  877. # If you're looking for 'get_name()', 'get_version()', and so forth,
  878. # they are defined in a sneaky way: the constructor binds self.get_XXX
  879. # to self.metadata.get_XXX. The actual code is in the
  880. # DistributionMetadata class, below.
  881. class DistributionMetadata:
  882. """Dummy class to hold the distribution meta-data: name, version,
  883. author, and so forth.
  884. """
  885. _METHOD_BASENAMES = (
  886. "name",
  887. "version",
  888. "author",
  889. "author_email",
  890. "maintainer",
  891. "maintainer_email",
  892. "url",
  893. "license",
  894. "description",
  895. "long_description",
  896. "keywords",
  897. "platforms",
  898. "fullname",
  899. "contact",
  900. "contact_email",
  901. "classifiers",
  902. "download_url",
  903. # PEP 314
  904. "provides",
  905. "requires",
  906. "obsoletes",
  907. )
  908. def __init__(self, path=None):
  909. if path is not None:
  910. self.read_pkg_file(open(path))
  911. else:
  912. self.name = None
  913. self.version = None
  914. self.author = None
  915. self.author_email = None
  916. self.maintainer = None
  917. self.maintainer_email = None
  918. self.url = None
  919. self.license = None
  920. self.description = None
  921. self.long_description = None
  922. self.keywords = None
  923. self.platforms = None
  924. self.classifiers = None
  925. self.download_url = None
  926. # PEP 314
  927. self.provides = None
  928. self.requires = None
  929. self.obsoletes = None
  930. def read_pkg_file(self, file):
  931. """Reads the metadata values from a file object."""
  932. msg = message_from_file(file)
  933. def _read_field(name):
  934. value = msg[name]
  935. if value and value != "UNKNOWN":
  936. return value
  937. def _read_list(name):
  938. values = msg.get_all(name, None)
  939. if values == []:
  940. return None
  941. return values
  942. metadata_version = msg['metadata-version']
  943. self.name = _read_field('name')
  944. self.version = _read_field('version')
  945. self.description = _read_field('summary')
  946. # we are filling author only.
  947. self.author = _read_field('author')
  948. self.maintainer = None
  949. self.author_email = _read_field('author-email')
  950. self.maintainer_email = None
  951. self.url = _read_field('home-page')
  952. self.license = _read_field('license')
  953. if 'download-url' in msg:
  954. self.download_url = _read_field('download-url')
  955. else:
  956. self.download_url = None
  957. self.long_description = _read_field('description')
  958. self.description = _read_field('summary')
  959. if 'keywords' in msg:
  960. self.keywords = _read_field('keywords').split(',')
  961. self.platforms = _read_list('platform')
  962. self.classifiers = _read_list('classifier')
  963. # PEP 314 - these fields only exist in 1.1
  964. if metadata_version == '1.1':
  965. self.requires = _read_list('requires')
  966. self.provides = _read_list('provides')
  967. self.obsoletes = _read_list('obsoletes')
  968. else:
  969. self.requires = None
  970. self.provides = None
  971. self.obsoletes = None
  972. def write_pkg_info(self, base_dir):
  973. """Write the PKG-INFO file into the release tree."""
  974. with open(
  975. os.path.join(base_dir, 'PKG-INFO'), 'w', encoding='UTF-8'
  976. ) as pkg_info:
  977. self.write_pkg_file(pkg_info)
  978. def write_pkg_file(self, file):
  979. """Write the PKG-INFO format data to a file object."""
  980. version = '1.0'
  981. if (
  982. self.provides
  983. or self.requires
  984. or self.obsoletes
  985. or self.classifiers
  986. or self.download_url
  987. ):
  988. version = '1.1'
  989. # required fields
  990. file.write('Metadata-Version: %s\n' % version)
  991. file.write('Name: %s\n' % self.get_name())
  992. file.write('Version: %s\n' % self.get_version())
  993. def maybe_write(header, val):
  994. if val:
  995. file.write(f"{header}: {val}\n")
  996. # optional fields
  997. maybe_write("Summary", self.get_description())
  998. maybe_write("Home-page", self.get_url())
  999. maybe_write("Author", self.get_contact())
  1000. maybe_write("Author-email", self.get_contact_email())
  1001. maybe_write("License", self.get_license())
  1002. maybe_write("Download-URL", self.download_url)
  1003. maybe_write("Description", rfc822_escape(self.get_long_description() or ""))
  1004. maybe_write("Keywords", ",".join(self.get_keywords()))
  1005. self._write_list(file, 'Platform', self.get_platforms())
  1006. self._write_list(file, 'Classifier', self.get_classifiers())
  1007. # PEP 314
  1008. self._write_list(file, 'Requires', self.get_requires())
  1009. self._write_list(file, 'Provides', self.get_provides())
  1010. self._write_list(file, 'Obsoletes', self.get_obsoletes())
  1011. def _write_list(self, file, name, values):
  1012. values = values or []
  1013. for value in values:
  1014. file.write('{}: {}\n'.format(name, value))
  1015. # -- Metadata query methods ----------------------------------------
  1016. def get_name(self):
  1017. return self.name or "UNKNOWN"
  1018. def get_version(self):
  1019. return self.version or "0.0.0"
  1020. def get_fullname(self):
  1021. return "{}-{}".format(self.get_name(), self.get_version())
  1022. def get_author(self):
  1023. return self.author
  1024. def get_author_email(self):
  1025. return self.author_email
  1026. def get_maintainer(self):
  1027. return self.maintainer
  1028. def get_maintainer_email(self):
  1029. return self.maintainer_email
  1030. def get_contact(self):
  1031. return self.maintainer or self.author
  1032. def get_contact_email(self):
  1033. return self.maintainer_email or self.author_email
  1034. def get_url(self):
  1035. return self.url
  1036. def get_license(self):
  1037. return self.license
  1038. get_licence = get_license
  1039. def get_description(self):
  1040. return self.description
  1041. def get_long_description(self):
  1042. return self.long_description
  1043. def get_keywords(self):
  1044. return self.keywords or []
  1045. def set_keywords(self, value):
  1046. self.keywords = _ensure_list(value, 'keywords')
  1047. def get_platforms(self):
  1048. return self.platforms
  1049. def set_platforms(self, value):
  1050. self.platforms = _ensure_list(value, 'platforms')
  1051. def get_classifiers(self):
  1052. return self.classifiers or []
  1053. def set_classifiers(self, value):
  1054. self.classifiers = _ensure_list(value, 'classifiers')
  1055. def get_download_url(self):
  1056. return self.download_url
  1057. # PEP 314
  1058. def get_requires(self):
  1059. return self.requires or []
  1060. def set_requires(self, value):
  1061. import distutils.versionpredicate
  1062. for v in value:
  1063. distutils.versionpredicate.VersionPredicate(v)
  1064. self.requires = list(value)
  1065. def get_provides(self):
  1066. return self.provides or []
  1067. def set_provides(self, value):
  1068. value = [v.strip() for v in value]
  1069. for v in value:
  1070. import distutils.versionpredicate
  1071. distutils.versionpredicate.split_provision(v)
  1072. self.provides = value
  1073. def get_obsoletes(self):
  1074. return self.obsoletes or []
  1075. def set_obsoletes(self, value):
  1076. import distutils.versionpredicate
  1077. for v in value:
  1078. distutils.versionpredicate.VersionPredicate(v)
  1079. self.obsoletes = list(value)
  1080. def fix_help_options(options):
  1081. """Convert a 4-tuple 'help_options' list as found in various command
  1082. classes to the 3-tuple form required by FancyGetopt.
  1083. """
  1084. new_options = []
  1085. for help_tuple in options:
  1086. new_options.append(help_tuple[0:3])
  1087. return new_options