Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 1 | """distutils.core |
| 2 | |
| 3 | The only module that needs to be imported to use the Distutils; provides |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 4 | the 'setup' function (which is to be called from the setup script). Also |
| 5 | indirectly provides the Distribution and Command classes, although they are |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 6 | really defined in distutils.dist and distutils.cmd. |
| 7 | """ |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 8 | |
| 9 | # created 1999/03/01, Greg Ward |
| 10 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 11 | __revision__ = "$Id$" |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 12 | |
Greg Ward | d80506c | 2000-04-22 03:20:49 +0000 | [diff] [blame] | 13 | import sys, os |
Greg Ward | 1ea8af2 | 1999-08-29 18:20:32 +0000 | [diff] [blame] | 14 | from types import * |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 15 | from distutils.errors import * |
Greg Ward | a76bbd4 | 2000-05-31 01:11:20 +0000 | [diff] [blame] | 16 | |
| 17 | # Mainly import these so setup scripts can "from distutils.core import" them. |
Greg Ward | fe6462c | 2000-04-04 01:40:52 +0000 | [diff] [blame] | 18 | from distutils.dist import Distribution |
| 19 | from distutils.cmd import Command |
Greg Ward | a76bbd4 | 2000-05-31 01:11:20 +0000 | [diff] [blame] | 20 | from distutils.extension import Extension |
| 21 | |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 22 | |
Greg Ward | 4c96db1 | 2000-02-18 00:26:23 +0000 | [diff] [blame] | 23 | # 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 Ward | c9c37b1 | 1999-12-12 16:51:44 +0000 | [diff] [blame] | 27 | usage = """\ |
| 28 | usage: %s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] |
Greg Ward | 732745b | 2000-05-23 01:43:08 +0000 | [diff] [blame] | 29 | or: %s --help [cmd1 cmd2 ...] |
Greg Ward | f0fd617 | 2000-01-30 18:30:32 +0000 | [diff] [blame] | 30 | or: %s --help-commands |
Greg Ward | c9c37b1 | 1999-12-12 16:51:44 +0000 | [diff] [blame] | 31 | or: %s cmd --help |
Greg Ward | 732745b | 2000-05-23 01:43:08 +0000 | [diff] [blame] | 32 | """ % ((os.path.basename(sys.argv[0]),) * 4) |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 33 | |
| 34 | |
Greg Ward | 37af1c3 | 2000-05-26 00:54:52 +0000 | [diff] [blame] | 35 | # If DISTUTILS_DEBUG is anything other than the empty string, we run in |
| 36 | # debug mode. |
| 37 | DEBUG = os.environ.get('DISTUTILS_DEBUG') |
| 38 | |
| 39 | |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 40 | def setup (**attrs): |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 41 | """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 Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 47 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 48 | 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 Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 53 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 54 | 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 Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 64 | |
Greg Ward | 8ff5a3f | 2000-06-02 00:44:53 +0000 | [diff] [blame] | 65 | 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 Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 72 | |
| 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 Ward | 3985151 | 2000-06-03 01:02:06 +0000 | [diff] [blame] | 83 | try: |
| 84 | dist = klass (attrs) |
| 85 | except DistutilsSetupError, msg: |
| 86 | raise SystemExit, "error in setup script: %s" % msg |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 87 | |
Gregory P. Smith | bb8c71d | 2000-05-12 00:42:19 +0000 | [diff] [blame] | 88 | # 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 Ward | f7a5507 | 2000-06-02 01:55:36 +0000 | [diff] [blame] | 92 | if DEBUG: |
| 93 | print "options (after parsing config files):" |
| 94 | dist.dump_option_dicts() |
Greg Ward | 77751c0 | 2000-05-23 03:54:16 +0000 | [diff] [blame] | 95 | |
Gregory P. Smith | bb8c71d | 2000-05-12 00:42:19 +0000 | [diff] [blame] | 96 | # Parse the command line; any command-line errors are the end user's |
Greg Ward | 42926dd | 1999-09-08 02:41:09 +0000 | [diff] [blame] | 97 | # fault, so turn them into SystemExit to suppress tracebacks. |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 98 | try: |
Greg Ward | c9c37b1 | 1999-12-12 16:51:44 +0000 | [diff] [blame] | 99 | ok = dist.parse_command_line (sys.argv[1:]) |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 100 | except DistutilsArgError, msg: |
Greg Ward | c9c37b1 | 1999-12-12 16:51:44 +0000 | [diff] [blame] | 101 | sys.stderr.write (usage + "\n") |
Greg Ward | 9d46b9c | 1999-12-16 01:19:05 +0000 | [diff] [blame] | 102 | raise SystemExit, "error: %s" % msg |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 103 | |
Greg Ward | f7a5507 | 2000-06-02 01:55:36 +0000 | [diff] [blame] | 104 | if DEBUG: |
| 105 | print "options (after parsing command line):" |
| 106 | dist.dump_option_dicts() |
Greg Ward | 77751c0 | 2000-05-23 03:54:16 +0000 | [diff] [blame] | 107 | |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 108 | # And finally, run all the commands found on the command line. |
Greg Ward | c9c37b1 | 1999-12-12 16:51:44 +0000 | [diff] [blame] | 109 | if ok: |
| 110 | try: |
| 111 | dist.run_commands () |
| 112 | except KeyboardInterrupt: |
| 113 | raise SystemExit, "interrupted" |
Greg Ward | d80506c | 2000-04-22 03:20:49 +0000 | [diff] [blame] | 114 | except (IOError, os.error), exc: |
Greg Ward | cf0e2dd | 2000-06-17 02:17:45 +0000 | [diff] [blame^] | 115 | error = grok_environment_error(exc) |
Greg Ward | 37af1c3 | 2000-05-26 00:54:52 +0000 | [diff] [blame] | 116 | |
| 117 | if DEBUG: |
| 118 | sys.stderr.write(error + "\n") |
| 119 | raise |
| 120 | else: |
| 121 | raise SystemExit, error |
| 122 | |
Greg Ward | ddad73b | 2000-04-22 03:11:17 +0000 | [diff] [blame] | 123 | except (DistutilsExecError, |
| 124 | DistutilsFileError, |
Greg Ward | 66ac93e | 2000-05-30 02:04:29 +0000 | [diff] [blame] | 125 | DistutilsOptionError, |
| 126 | CCompilerError), msg: |
Greg Ward | 37af1c3 | 2000-05-26 00:54:52 +0000 | [diff] [blame] | 127 | if DEBUG: |
| 128 | raise |
| 129 | else: |
| 130 | raise SystemExit, "error: " + str(msg) |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 131 | |
| 132 | # setup () |