Run a setup script in a somewhat controlled environment, and return the Distribution instance that drives things. This is useful if you need to find out the distribution meta-data (passed as keyword args from 'script' to 'setup()', or the contents of the config files or command-
(script_name, script_args=None, stop_after="run")
| 168 | |
| 169 | |
| 170 | def run_setup (script_name, script_args=None, stop_after="run"): |
| 171 | """Run a setup script in a somewhat controlled environment, and |
| 172 | return the Distribution instance that drives things. This is useful |
| 173 | if you need to find out the distribution meta-data (passed as |
| 174 | keyword args from 'script' to 'setup()', or the contents of the |
| 175 | config files or command-line. |
| 176 | |
| 177 | 'script_name' is a file that will be read and run with 'exec()'; |
| 178 | 'sys.argv[0]' will be replaced with 'script' for the duration of the |
| 179 | call. 'script_args' is a list of strings; if supplied, |
| 180 | 'sys.argv[1:]' will be replaced by 'script_args' for the duration of |
| 181 | the call. |
| 182 | |
| 183 | 'stop_after' tells 'setup()' when to stop processing; possible |
| 184 | values: |
| 185 | init |
| 186 | stop after the Distribution instance has been created and |
| 187 | populated with the keyword arguments to 'setup()' |
| 188 | config |
| 189 | stop after config files have been parsed (and their data |
| 190 | stored in the Distribution instance) |
| 191 | commandline |
| 192 | stop after the command-line ('sys.argv[1:]' or 'script_args') |
| 193 | have been parsed (and the data stored in the Distribution) |
| 194 | run [default] |
| 195 | stop after all commands have been run (the same as if 'setup()' |
| 196 | had been called in the usual way |
| 197 | |
| 198 | Returns the Distribution instance, which provides all information |
| 199 | used to drive the Distutils. |
| 200 | """ |
| 201 | if stop_after not in ('init', 'config', 'commandline', 'run'): |
| 202 | raise ValueError("invalid value for 'stop_after': %r" % (stop_after,)) |
| 203 | |
| 204 | global _setup_stop_after, _setup_distribution |
| 205 | _setup_stop_after = stop_after |
| 206 | |
| 207 | save_argv = sys.argv.copy() |
| 208 | g = {'__file__': script_name} |
| 209 | try: |
| 210 | try: |
| 211 | sys.argv[0] = script_name |
| 212 | if script_args is not None: |
| 213 | sys.argv[1:] = script_args |
| 214 | with open(script_name, 'rb') as f: |
| 215 | exec(f.read(), g) |
| 216 | finally: |
| 217 | sys.argv = save_argv |
| 218 | _setup_stop_after = None |
| 219 | except SystemExit: |
| 220 | # Hmm, should we do something if exiting with a non-zero code |
| 221 | # (ie. error)? |
| 222 | pass |
| 223 | |
| 224 | if _setup_distribution is None: |
| 225 | raise RuntimeError(("'distutils.core.setup()' was never called -- " |
| 226 | "perhaps '%s' is not a Distutils setup script?") % \ |
| 227 | script_name) |