|
8 | 8 |
|
9 | 9 | importos |
10 | 10 | importsys |
| 11 | +importtokenize |
11 | 12 |
|
12 | 13 | fromdistutils.debugimportDEBUG |
13 | 14 | fromdistutils.errorsimport* |
@@ -144,29 +145,41 @@ class found in 'cmdclass' is used in place of the default, which is |
144 | 145 |
|
145 | 146 | # And finally, run all the commands found on the command line. |
146 | 147 | ifok: |
147 | | -try: |
148 | | -dist.run_commands() |
149 | | -exceptKeyboardInterrupt: |
150 | | -raiseSystemExit("interrupted") |
151 | | -exceptOSErrorasexc: |
152 | | -ifDEBUG: |
153 | | -sys.stderr.write("error: %s\n"% (exc,)) |
154 | | -raise |
155 | | -else: |
156 | | -raiseSystemExit("error: %s"% (exc,)) |
157 | | - |
158 | | -except (DistutilsError, |
159 | | -CCompilerError)asmsg: |
160 | | -ifDEBUG: |
161 | | -raise |
162 | | -else: |
163 | | -raiseSystemExit("error: "+str(msg)) |
| 148 | +returnrun_commands(dist) |
164 | 149 |
|
165 | 150 | returndist |
166 | 151 |
|
167 | 152 | # setup () |
168 | 153 |
|
169 | 154 |
|
| 155 | +defrun_commands (dist): |
| 156 | +"""Given a Distribution object run all the commands, |
| 157 | + raising ``SystemExit`` errors in the case of failure. |
| 158 | +
|
| 159 | + This function assumes that either ``sys.argv`` or ``dist.script_args`` |
| 160 | + is already set accordingly. |
| 161 | + """ |
| 162 | +try: |
| 163 | +dist.run_commands() |
| 164 | +exceptKeyboardInterrupt: |
| 165 | +raiseSystemExit("interrupted") |
| 166 | +exceptOSErrorasexc: |
| 167 | +ifDEBUG: |
| 168 | +sys.stderr.write("error: %s\n"% (exc,)) |
| 169 | +raise |
| 170 | +else: |
| 171 | +raiseSystemExit("error: %s"% (exc,)) |
| 172 | + |
| 173 | +except (DistutilsError, |
| 174 | +CCompilerError)asmsg: |
| 175 | +ifDEBUG: |
| 176 | +raise |
| 177 | +else: |
| 178 | +raiseSystemExit("error: "+str(msg)) |
| 179 | + |
| 180 | +returndist |
| 181 | + |
| 182 | + |
170 | 183 | defrun_setup (script_name,script_args=None,stop_after="run"): |
171 | 184 | """Run a setup script in a somewhat controlled environment, and |
172 | 185 | return the Distribution instance that drives things. This is useful |
@@ -205,14 +218,16 @@ def run_setup (script_name, script_args=None, stop_after="run"): |
205 | 218 | _setup_stop_after=stop_after |
206 | 219 |
|
207 | 220 | save_argv=sys.argv.copy() |
208 | | -g= {'__file__':script_name} |
| 221 | +g= {'__file__':script_name,'__name__':'__main__'} |
209 | 222 | try: |
210 | 223 | try: |
211 | 224 | sys.argv[0]=script_name |
212 | 225 | ifscript_argsisnotNone: |
213 | 226 | sys.argv[1:]=script_args |
214 | | -withopen(script_name,'rb')asf: |
215 | | -exec(f.read(),g) |
| 227 | +# tokenize.open supports automatic encoding detection |
| 228 | +withtokenize.open(script_name)asf: |
| 229 | +code=f.read().replace(r'\r\n',r'\n') |
| 230 | +exec(code,g) |
216 | 231 | finally: |
217 | 232 | sys.argv=save_argv |
218 | 233 | _setup_stop_after=None |
|