blob: 35afa84b7e577305745f1fff723b6222e25a308c [file] [log] [blame]
Greg Ward2689e3d1999-03-22 14:52:19 +00001"""distutils.core
2
3The only module that needs to be imported to use the Distutils; provides
Greg Wardfe6462c2000-04-04 01:40:52 +00004the 'setup' function (which is to be called from the setup script). Also
5indirectly provides the Distribution and Command classes, although they are
Greg Ward8ff5a3f2000-06-02 00:44:53 +00006really defined in distutils.dist and distutils.cmd.
7"""
Greg Ward2689e3d1999-03-22 14:52:19 +00008
9# created 1999/03/01, Greg Ward
10
Greg Ward3ce77fd2000-03-02 01:49:45 +000011__revision__ = "$Id$"
Greg Ward2689e3d1999-03-22 14:52:19 +000012
Greg Wardd80506c2000-04-22 03:20:49 +000013import sys, os
Greg Ward1ea8af21999-08-29 18:20:32 +000014from types import *
Greg Ward2689e3d1999-03-22 14:52:19 +000015from distutils.errors import *
Greg Warda76bbd42000-05-31 01:11:20 +000016
17# Mainly import these so setup scripts can "from distutils.core import" them.
Greg Wardfe6462c2000-04-04 01:40:52 +000018from distutils.dist import Distribution
19from distutils.cmd import Command
Greg Warda76bbd42000-05-31 01:11:20 +000020from distutils.extension import Extension
21
Greg Ward2689e3d1999-03-22 14:52:19 +000022
Greg Ward4c96db12000-02-18 00:26:23 +000023# This is a barebones help message generated displayed when the user
24# runs the setup script with no arguments at all. More useful help
25# is generated with various --help options: global help, list commands,
26# and per-command help.
Greg Wardc9c37b11999-12-12 16:51:44 +000027usage = """\
28usage: %s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
Greg Ward732745b2000-05-23 01:43:08 +000029 or: %s --help [cmd1 cmd2 ...]
Greg Wardf0fd6172000-01-30 18:30:32 +000030 or: %s --help-commands
Greg Wardc9c37b11999-12-12 16:51:44 +000031 or: %s cmd --help
Greg Ward732745b2000-05-23 01:43:08 +000032""" % ((os.path.basename(sys.argv[0]),) * 4)
Greg Ward2689e3d1999-03-22 14:52:19 +000033
34
Greg Ward37af1c32000-05-26 00:54:52 +000035# If DISTUTILS_DEBUG is anything other than the empty string, we run in
36# debug mode.
37DEBUG = os.environ.get('DISTUTILS_DEBUG')
38
39
Greg Ward2689e3d1999-03-22 14:52:19 +000040def setup (**attrs):
Greg Ward8ff5a3f2000-06-02 00:44:53 +000041 """The gateway to the Distutils: do everything your setup script needs
42 to do, in a highly flexible and user-driven way. Briefly: create a
43 Distribution instance; find and parse config files; parse the command
44 line; run each of those commands using the options supplied to
45 'setup()' (as keyword arguments), in config files, and on the command
46 line.
Greg Ward2689e3d1999-03-22 14:52:19 +000047
Greg Ward8ff5a3f2000-06-02 00:44:53 +000048 The Distribution instance might be an instance of a class supplied via
49 the 'distclass' keyword argument to 'setup'; if no such class is
50 supplied, then the Distribution class (in dist.py) is instantiated.
51 All other arguments to 'setup' (except for 'cmdclass') are used to set
52 attributes of the Distribution instance.
Greg Ward2689e3d1999-03-22 14:52:19 +000053
Greg Ward8ff5a3f2000-06-02 00:44:53 +000054 The 'cmdclass' argument, if supplied, is a dictionary mapping command
55 names to command classes. Each command encountered on the command line
56 will be turned into a command class, which is in turn instantiated; any
57 class found in 'cmdclass' is used in place of the default, which is
58 (for command 'foo_bar') class 'foo_bar' in module
59 'distutils.command.foo_bar'. The command class must provide a
60 'user_options' attribute which is a list of option specifiers for
61 'distutils.fancy_getopt'. Any command-line options between the current
62 and the next command are used to set attributes of the current command
63 object.
Greg Ward2689e3d1999-03-22 14:52:19 +000064
Greg Ward8ff5a3f2000-06-02 00:44:53 +000065 When the entire command-line has been successfully parsed, calls the
66 'run()' method on each command object in turn. This method will be
67 driven entirely by the Distribution object (which each command object
68 has a reference to, thanks to its constructor), and the
69 command-specific options that became attributes of each command
70 object.
71 """
Greg Ward2689e3d1999-03-22 14:52:19 +000072
73 # Determine the distribution class -- either caller-supplied or
74 # our Distribution (see below).
75 klass = attrs.get ('distclass')
76 if klass:
77 del attrs['distclass']
78 else:
79 klass = Distribution
80
81 # Create the Distribution instance, using the remaining arguments
82 # (ie. everything except distclass) to initialize it
Greg Ward39851512000-06-03 01:02:06 +000083 try:
84 dist = klass (attrs)
85 except DistutilsSetupError, msg:
86 raise SystemExit, "error in setup script: %s" % msg
Greg Ward2689e3d1999-03-22 14:52:19 +000087
Gregory P. Smithbb8c71d2000-05-12 00:42:19 +000088 # Find and parse the config file(s): they will override options from
89 # the setup script, but be overridden by the command line.
90 dist.parse_config_files()
91
Greg Wardf7a55072000-06-02 01:55:36 +000092 if DEBUG:
93 print "options (after parsing config files):"
94 dist.dump_option_dicts()
Greg Ward77751c02000-05-23 03:54:16 +000095
Gregory P. Smithbb8c71d2000-05-12 00:42:19 +000096 # Parse the command line; any command-line errors are the end user's
Greg Ward42926dd1999-09-08 02:41:09 +000097 # fault, so turn them into SystemExit to suppress tracebacks.
Greg Ward2689e3d1999-03-22 14:52:19 +000098 try:
Greg Wardc9c37b11999-12-12 16:51:44 +000099 ok = dist.parse_command_line (sys.argv[1:])
Greg Ward2689e3d1999-03-22 14:52:19 +0000100 except DistutilsArgError, msg:
Greg Wardc9c37b11999-12-12 16:51:44 +0000101 sys.stderr.write (usage + "\n")
Greg Ward9d46b9c1999-12-16 01:19:05 +0000102 raise SystemExit, "error: %s" % msg
Greg Ward2689e3d1999-03-22 14:52:19 +0000103
Greg Wardf7a55072000-06-02 01:55:36 +0000104 if DEBUG:
105 print "options (after parsing command line):"
106 dist.dump_option_dicts()
Greg Ward77751c02000-05-23 03:54:16 +0000107
Greg Ward2689e3d1999-03-22 14:52:19 +0000108 # And finally, run all the commands found on the command line.
Greg Wardc9c37b11999-12-12 16:51:44 +0000109 if ok:
110 try:
111 dist.run_commands ()
112 except KeyboardInterrupt:
113 raise SystemExit, "interrupted"
Greg Wardd80506c2000-04-22 03:20:49 +0000114 except (IOError, os.error), exc:
Greg Wardcf0e2dd2000-06-17 02:17:45 +0000115 error = grok_environment_error(exc)
Greg Ward37af1c32000-05-26 00:54:52 +0000116
117 if DEBUG:
118 sys.stderr.write(error + "\n")
119 raise
120 else:
121 raise SystemExit, error
122
Greg Wardddad73b2000-04-22 03:11:17 +0000123 except (DistutilsExecError,
124 DistutilsFileError,
Greg Ward66ac93e2000-05-30 02:04:29 +0000125 DistutilsOptionError,
126 CCompilerError), msg:
Greg Ward37af1c32000-05-26 00:54:52 +0000127 if DEBUG:
128 raise
129 else:
130 raise SystemExit, "error: " + str(msg)
Greg Ward2689e3d1999-03-22 14:52:19 +0000131
132# setup ()