]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """distutils.core\r |
2 | \r | |
3 | The only module that needs to be imported to use the Distutils; provides\r | |
4 | the 'setup' function (which is to be called from the setup script). Also\r | |
5 | indirectly provides the Distribution and Command classes, although they are\r | |
6 | really defined in distutils.dist and distutils.cmd.\r | |
7 | """\r | |
8 | \r | |
9 | __revision__ = "$Id$"\r | |
10 | \r | |
11 | import sys\r | |
12 | import os\r | |
13 | \r | |
14 | from distutils.debug import DEBUG\r | |
15 | from distutils.errors import (DistutilsSetupError, DistutilsArgError,\r | |
16 | DistutilsError, CCompilerError)\r | |
17 | from distutils.util import grok_environment_error\r | |
18 | \r | |
19 | # Mainly import these so setup scripts can "from distutils.core import" them.\r | |
20 | from distutils.dist import Distribution\r | |
21 | from distutils.cmd import Command\r | |
22 | from distutils.config import PyPIRCCommand\r | |
23 | from distutils.extension import Extension\r | |
24 | \r | |
25 | # This is a barebones help message generated displayed when the user\r | |
26 | # runs the setup script with no arguments at all. More useful help\r | |
27 | # is generated with various --help options: global help, list commands,\r | |
28 | # and per-command help.\r | |
29 | USAGE = """\\r | |
30 | usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]\r | |
31 | or: %(script)s --help [cmd1 cmd2 ...]\r | |
32 | or: %(script)s --help-commands\r | |
33 | or: %(script)s cmd --help\r | |
34 | """\r | |
35 | \r | |
36 | def gen_usage(script_name):\r | |
37 | script = os.path.basename(script_name)\r | |
38 | return USAGE % {'script': script}\r | |
39 | \r | |
40 | \r | |
41 | # Some mild magic to control the behaviour of 'setup()' from 'run_setup()'.\r | |
42 | _setup_stop_after = None\r | |
43 | _setup_distribution = None\r | |
44 | \r | |
45 | # Legal keyword arguments for the setup() function\r | |
46 | setup_keywords = ('distclass', 'script_name', 'script_args', 'options',\r | |
47 | 'name', 'version', 'author', 'author_email',\r | |
48 | 'maintainer', 'maintainer_email', 'url', 'license',\r | |
49 | 'description', 'long_description', 'keywords',\r | |
50 | 'platforms', 'classifiers', 'download_url',\r | |
51 | 'requires', 'provides', 'obsoletes',\r | |
52 | )\r | |
53 | \r | |
54 | # Legal keyword arguments for the Extension constructor\r | |
55 | extension_keywords = ('name', 'sources', 'include_dirs',\r | |
56 | 'define_macros', 'undef_macros',\r | |
57 | 'library_dirs', 'libraries', 'runtime_library_dirs',\r | |
58 | 'extra_objects', 'extra_compile_args', 'extra_link_args',\r | |
59 | 'swig_opts', 'export_symbols', 'depends', 'language')\r | |
60 | \r | |
61 | def setup(**attrs):\r | |
62 | """The gateway to the Distutils: do everything your setup script needs\r | |
63 | to do, in a highly flexible and user-driven way. Briefly: create a\r | |
64 | Distribution instance; find and parse config files; parse the command\r | |
65 | line; run each Distutils command found there, customized by the options\r | |
66 | supplied to 'setup()' (as keyword arguments), in config files, and on\r | |
67 | the command line.\r | |
68 | \r | |
69 | The Distribution instance might be an instance of a class supplied via\r | |
70 | the 'distclass' keyword argument to 'setup'; if no such class is\r | |
71 | supplied, then the Distribution class (in dist.py) is instantiated.\r | |
72 | All other arguments to 'setup' (except for 'cmdclass') are used to set\r | |
73 | attributes of the Distribution instance.\r | |
74 | \r | |
75 | The 'cmdclass' argument, if supplied, is a dictionary mapping command\r | |
76 | names to command classes. Each command encountered on the command line\r | |
77 | will be turned into a command class, which is in turn instantiated; any\r | |
78 | class found in 'cmdclass' is used in place of the default, which is\r | |
79 | (for command 'foo_bar') class 'foo_bar' in module\r | |
80 | 'distutils.command.foo_bar'. The command class must provide a\r | |
81 | 'user_options' attribute which is a list of option specifiers for\r | |
82 | 'distutils.fancy_getopt'. Any command-line options between the current\r | |
83 | and the next command are used to set attributes of the current command\r | |
84 | object.\r | |
85 | \r | |
86 | When the entire command-line has been successfully parsed, calls the\r | |
87 | 'run()' method on each command object in turn. This method will be\r | |
88 | driven entirely by the Distribution object (which each command object\r | |
89 | has a reference to, thanks to its constructor), and the\r | |
90 | command-specific options that became attributes of each command\r | |
91 | object.\r | |
92 | """\r | |
93 | \r | |
94 | global _setup_stop_after, _setup_distribution\r | |
95 | \r | |
96 | # Determine the distribution class -- either caller-supplied or\r | |
97 | # our Distribution (see below).\r | |
98 | klass = attrs.get('distclass')\r | |
99 | if klass:\r | |
100 | del attrs['distclass']\r | |
101 | else:\r | |
102 | klass = Distribution\r | |
103 | \r | |
104 | if 'script_name' not in attrs:\r | |
105 | attrs['script_name'] = os.path.basename(sys.argv[0])\r | |
106 | if 'script_args' not in attrs:\r | |
107 | attrs['script_args'] = sys.argv[1:]\r | |
108 | \r | |
109 | # Create the Distribution instance, using the remaining arguments\r | |
110 | # (ie. everything except distclass) to initialize it\r | |
111 | try:\r | |
112 | _setup_distribution = dist = klass(attrs)\r | |
113 | except DistutilsSetupError, msg:\r | |
114 | if 'name' in attrs:\r | |
115 | raise SystemExit, "error in %s setup command: %s" % \\r | |
116 | (attrs['name'], msg)\r | |
117 | else:\r | |
118 | raise SystemExit, "error in setup command: %s" % msg\r | |
119 | \r | |
120 | if _setup_stop_after == "init":\r | |
121 | return dist\r | |
122 | \r | |
123 | # Find and parse the config file(s): they will override options from\r | |
124 | # the setup script, but be overridden by the command line.\r | |
125 | dist.parse_config_files()\r | |
126 | \r | |
127 | if DEBUG:\r | |
128 | print "options (after parsing config files):"\r | |
129 | dist.dump_option_dicts()\r | |
130 | \r | |
131 | if _setup_stop_after == "config":\r | |
132 | return dist\r | |
133 | \r | |
134 | # Parse the command line and override config files; any\r | |
135 | # command-line errors are the end user's fault, so turn them into\r | |
136 | # SystemExit to suppress tracebacks.\r | |
137 | try:\r | |
138 | ok = dist.parse_command_line()\r | |
139 | except DistutilsArgError, msg:\r | |
140 | raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg\r | |
141 | \r | |
142 | if DEBUG:\r | |
143 | print "options (after parsing command line):"\r | |
144 | dist.dump_option_dicts()\r | |
145 | \r | |
146 | if _setup_stop_after == "commandline":\r | |
147 | return dist\r | |
148 | \r | |
149 | # And finally, run all the commands found on the command line.\r | |
150 | if ok:\r | |
151 | try:\r | |
152 | dist.run_commands()\r | |
153 | except KeyboardInterrupt:\r | |
154 | raise SystemExit, "interrupted"\r | |
155 | except (IOError, os.error), exc:\r | |
156 | error = grok_environment_error(exc)\r | |
157 | \r | |
158 | if DEBUG:\r | |
159 | sys.stderr.write(error + "\n")\r | |
160 | raise\r | |
161 | else:\r | |
162 | raise SystemExit, error\r | |
163 | \r | |
164 | except (DistutilsError,\r | |
165 | CCompilerError), msg:\r | |
166 | if DEBUG:\r | |
167 | raise\r | |
168 | else:\r | |
169 | raise SystemExit, "error: " + str(msg)\r | |
170 | \r | |
171 | return dist\r | |
172 | \r | |
173 | \r | |
174 | def run_setup(script_name, script_args=None, stop_after="run"):\r | |
175 | """Run a setup script in a somewhat controlled environment, and\r | |
176 | return the Distribution instance that drives things. This is useful\r | |
177 | if you need to find out the distribution meta-data (passed as\r | |
178 | keyword args from 'script' to 'setup()', or the contents of the\r | |
179 | config files or command-line.\r | |
180 | \r | |
181 | 'script_name' is a file that will be run with 'execfile()';\r | |
182 | 'sys.argv[0]' will be replaced with 'script' for the duration of the\r | |
183 | call. 'script_args' is a list of strings; if supplied,\r | |
184 | 'sys.argv[1:]' will be replaced by 'script_args' for the duration of\r | |
185 | the call.\r | |
186 | \r | |
187 | 'stop_after' tells 'setup()' when to stop processing; possible\r | |
188 | values:\r | |
189 | init\r | |
190 | stop after the Distribution instance has been created and\r | |
191 | populated with the keyword arguments to 'setup()'\r | |
192 | config\r | |
193 | stop after config files have been parsed (and their data\r | |
194 | stored in the Distribution instance)\r | |
195 | commandline\r | |
196 | stop after the command-line ('sys.argv[1:]' or 'script_args')\r | |
197 | have been parsed (and the data stored in the Distribution)\r | |
198 | run [default]\r | |
199 | stop after all commands have been run (the same as if 'setup()'\r | |
200 | had been called in the usual way\r | |
201 | \r | |
202 | Returns the Distribution instance, which provides all information\r | |
203 | used to drive the Distutils.\r | |
204 | """\r | |
205 | if stop_after not in ('init', 'config', 'commandline', 'run'):\r | |
206 | raise ValueError, "invalid value for 'stop_after': %r" % (stop_after,)\r | |
207 | \r | |
208 | global _setup_stop_after, _setup_distribution\r | |
209 | _setup_stop_after = stop_after\r | |
210 | \r | |
211 | save_argv = sys.argv\r | |
212 | g = {'__file__': script_name}\r | |
213 | l = {}\r | |
214 | try:\r | |
215 | try:\r | |
216 | sys.argv[0] = script_name\r | |
217 | if script_args is not None:\r | |
218 | sys.argv[1:] = script_args\r | |
219 | f = open(script_name)\r | |
220 | try:\r | |
221 | exec f.read() in g, l\r | |
222 | finally:\r | |
223 | f.close()\r | |
224 | finally:\r | |
225 | sys.argv = save_argv\r | |
226 | _setup_stop_after = None\r | |
227 | except SystemExit:\r | |
228 | # Hmm, should we do something if exiting with a non-zero code\r | |
229 | # (ie. error)?\r | |
230 | pass\r | |
231 | except:\r | |
232 | raise\r | |
233 | \r | |
234 | if _setup_distribution is None:\r | |
235 | raise RuntimeError, \\r | |
236 | ("'distutils.core.setup()' was never called -- "\r | |
237 | "perhaps '%s' is not a Distutils setup script?") % \\r | |
238 | script_name\r | |
239 | \r | |
240 | # I wonder if the setup script's namespace -- g and l -- would be of\r | |
241 | # any interest to callers?\r | |
242 | return _setup_distribution\r |