dist.py 49 KB

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