Show More
@@ -0,0 +1,51 b'' | |||||
|
1 | """Base utilities support for IPython. | |||
|
2 | ||||
|
3 | Warning: this is a module that other utilities modules will import from, so it | |||
|
4 | can ONLY depend on the standard library, and NOTHING ELSE. In particular, this | |||
|
5 | module can NOT import anything from IPython, or circular dependencies will arise. | |||
|
6 | """ | |||
|
7 | ||||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | # Imports | |||
|
10 | #----------------------------------------------------------------------------- | |||
|
11 | ||||
|
12 | import subprocess | |||
|
13 | ||||
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | # Functions | |||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | ||||
|
18 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): | |||
|
19 | """Return (standard output,standard error) of executing cmd in a shell. | |||
|
20 | ||||
|
21 | Accepts the same arguments as system(), plus: | |||
|
22 | ||||
|
23 | - split(0): if true, each of stdout/err is returned as a list split on | |||
|
24 | newlines. | |||
|
25 | ||||
|
26 | Note: a stateful version of this function is available through the | |||
|
27 | SystemExec class.""" | |||
|
28 | ||||
|
29 | if verbose or debug: print header+cmd | |||
|
30 | if not cmd: | |||
|
31 | if split: | |||
|
32 | return [],[] | |||
|
33 | else: | |||
|
34 | return '','' | |||
|
35 | if not debug: | |||
|
36 | p = subprocess.Popen(cmd, shell=True, | |||
|
37 | stdin=subprocess.PIPE, | |||
|
38 | stdout=subprocess.PIPE, | |||
|
39 | stderr=subprocess.PIPE, | |||
|
40 | close_fds=True) | |||
|
41 | pin, pout, perr = (p.stdin, p.stdout, p.stderr) | |||
|
42 | ||||
|
43 | tout = pout.read().rstrip() | |||
|
44 | terr = perr.read().rstrip() | |||
|
45 | pin.close() | |||
|
46 | pout.close() | |||
|
47 | perr.close() | |||
|
48 | if split: | |||
|
49 | return tout.split('\n'),terr.split('\n') | |||
|
50 | else: | |||
|
51 | return tout,terr |
@@ -71,6 +71,10 b" warnings.filterwarnings('ignore', 'the sets module is deprecated'," | |||||
71 | warnings.filterwarnings('ignore', 'the sha module is deprecated', |
|
71 | warnings.filterwarnings('ignore', 'the sha module is deprecated', | |
72 | DeprecationWarning) |
|
72 | DeprecationWarning) | |
73 |
|
73 | |||
|
74 | # Wx on Fedora11 spits these out | |||
|
75 | warnings.filterwarnings('ignore', 'wxPython/wxWidgets release number mismatch', | |||
|
76 | UserWarning) | |||
|
77 | ||||
74 | #----------------------------------------------------------------------------- |
|
78 | #----------------------------------------------------------------------------- | |
75 | # Logic for skipping doctests |
|
79 | # Logic for skipping doctests | |
76 | #----------------------------------------------------------------------------- |
|
80 | #----------------------------------------------------------------------------- | |
@@ -274,33 +278,34 b' def make_runners():' | |||||
274 | """Define the top-level packages that need to be tested. |
|
278 | """Define the top-level packages that need to be tested. | |
275 | """ |
|
279 | """ | |
276 |
|
280 | |||
277 | nose_packages = ['config', 'core', 'extensions', 'frontend', 'lib', |
|
281 | # Packages to be tested via nose, that only depend on the stdlib | |
278 | 'scripts', 'testing', 'utils', |
|
282 | nose_pkg_names = ['config', 'core', 'extensions', 'frontend', 'lib', | |
279 | # Note that we list the kernel here, though the bulk of it |
|
283 | 'scripts', 'testing', 'utils' ] | |
280 | # is twisted-based, because nose picks up doctests that |
|
|||
281 | # twisted doesn't. |
|
|||
282 | 'kernel'] |
|
|||
283 | # The machinery in kernel needs twisted for real testing |
|
284 | # The machinery in kernel needs twisted for real testing | |
284 |
trial_p |
|
285 | trial_pkg_names = [] | |
285 |
|
286 | |||
286 | if have_wx: |
|
287 | if have_wx: | |
287 |
nose_p |
|
288 | nose_pkg_names.append('gui') | |
288 |
|
||||
289 | #nose_packages = ['config', 'utils'] # dbg |
|
|||
290 | #trial_packages = [] # dbg |
|
|||
291 |
|
||||
292 | nose_packages = ['IPython.%s' % m for m in nose_packages ] |
|
|||
293 | trial_packages = ['IPython.%s' % m for m in trial_packages ] |
|
|||
294 |
|
289 | |||
295 | # Make runners, most with nose |
|
|||
296 | nose_testers = [IPTester(params=v) for v in nose_packages] |
|
|||
297 | runners = zip(nose_packages, nose_testers) |
|
|||
298 |
|
||||
299 | # And add twisted ones if conditions are met |
|
290 | # And add twisted ones if conditions are met | |
300 | if have_zi and have_twisted and have_foolscap: |
|
291 | if have_zi and have_twisted and have_foolscap: | |
301 | trial_testers = [IPTester('trial', params=v) for v in trial_packages] |
|
292 | # Note that we list the kernel here, though the bulk of it is | |
302 | runners.extend(zip(trial_packages, trial_testers)) |
|
293 | # twisted-based, because nose picks up doctests that twisted doesn't. | |
303 |
|
294 | nose_pkg_names.append('kernel') | ||
|
295 | trial_pkg_names.append('kernel') | |||
|
296 | ||||
|
297 | # For debugging this code, only load quick stuff | |||
|
298 | #nose_pkg_names = ['config', 'utils'] # dbg | |||
|
299 | #trial_pkg_names = [] # dbg | |||
|
300 | ||||
|
301 | # Make fully qualified package names prepending 'IPython.' to our name lists | |||
|
302 | nose_packages = ['IPython.%s' % m for m in nose_pkg_names ] | |||
|
303 | trial_packages = ['IPython.%s' % m for m in trial_pkg_names ] | |||
|
304 | ||||
|
305 | # Make runners | |||
|
306 | runners = [ (v, IPTester('iptest', params=v)) for v in nose_packages ] | |||
|
307 | runners.extend([ (v, IPTester('trial', params=v)) for v in trial_packages ]) | |||
|
308 | ||||
304 | return runners |
|
309 | return runners | |
305 |
|
310 | |||
306 |
|
311 |
@@ -11,6 +11,7 b' these things are also convenient when working at the command line.' | |||||
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #***************************************************************************** |
|
13 | #***************************************************************************** | |
|
14 | from __future__ import absolute_import | |||
14 |
|
15 | |||
15 | #**************************************************************************** |
|
16 | #**************************************************************************** | |
16 | # required modules from the Python standard library |
|
17 | # required modules from the Python standard library | |
@@ -45,7 +46,7 b' from IPython.external.Itpl import itpl,printpl' | |||||
45 | from IPython.utils import platutils |
|
46 | from IPython.utils import platutils | |
46 | from IPython.utils.generics import result_display |
|
47 | from IPython.utils.generics import result_display | |
47 | from IPython.external.path import path |
|
48 | from IPython.external.path import path | |
48 |
|
49 | from .baseutils import getoutputerror | ||
49 |
|
50 | |||
50 | #**************************************************************************** |
|
51 | #**************************************************************************** | |
51 | # Exceptions |
|
52 | # Exceptions | |
@@ -194,16 +195,19 b' def warn(msg,level=2,exit_val=1):' | |||||
194 | print >> Term.cerr,'Exiting.\n' |
|
195 | print >> Term.cerr,'Exiting.\n' | |
195 | sys.exit(exit_val) |
|
196 | sys.exit(exit_val) | |
196 |
|
197 | |||
|
198 | ||||
197 | def info(msg): |
|
199 | def info(msg): | |
198 | """Equivalent to warn(msg,level=1).""" |
|
200 | """Equivalent to warn(msg,level=1).""" | |
199 |
|
201 | |||
200 | warn(msg,level=1) |
|
202 | warn(msg,level=1) | |
201 |
|
203 | |||
|
204 | ||||
202 | def error(msg): |
|
205 | def error(msg): | |
203 | """Equivalent to warn(msg,level=3).""" |
|
206 | """Equivalent to warn(msg,level=3).""" | |
204 |
|
207 | |||
205 | warn(msg,level=3) |
|
208 | warn(msg,level=3) | |
206 |
|
209 | |||
|
210 | ||||
207 | def fatal(msg,exit_val=1): |
|
211 | def fatal(msg,exit_val=1): | |
208 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
212 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" | |
209 |
|
213 | |||
@@ -282,6 +286,7 b' except ImportError:' | |||||
282 | This just returns clock() and zero.""" |
|
286 | This just returns clock() and zero.""" | |
283 | return time.clock(),0.0 |
|
287 | return time.clock(),0.0 | |
284 |
|
288 | |||
|
289 | ||||
285 | def timings_out(reps,func,*args,**kw): |
|
290 | def timings_out(reps,func,*args,**kw): | |
286 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
291 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) | |
287 |
|
292 | |||
@@ -310,6 +315,7 b' def timings_out(reps,func,*args,**kw):' | |||||
310 | av_time = tot_time / reps |
|
315 | av_time = tot_time / reps | |
311 | return tot_time,av_time,out |
|
316 | return tot_time,av_time,out | |
312 |
|
317 | |||
|
318 | ||||
313 | def timings(reps,func,*args,**kw): |
|
319 | def timings(reps,func,*args,**kw): | |
314 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
320 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) | |
315 |
|
321 | |||
@@ -319,6 +325,7 b' def timings(reps,func,*args,**kw):' | |||||
319 |
|
325 | |||
320 | return timings_out(reps,func,*args,**kw)[0:2] |
|
326 | return timings_out(reps,func,*args,**kw)[0:2] | |
321 |
|
327 | |||
|
328 | ||||
322 | def timing(func,*args,**kw): |
|
329 | def timing(func,*args,**kw): | |
323 | """timing(func,*args,**kw) -> t_total |
|
330 | """timing(func,*args,**kw) -> t_total | |
324 |
|
331 | |||
@@ -348,6 +355,7 b' def arg_split(s,posix=False):' | |||||
348 | lex.whitespace_split = True |
|
355 | lex.whitespace_split = True | |
349 | return list(lex) |
|
356 | return list(lex) | |
350 |
|
357 | |||
|
358 | ||||
351 | def system(cmd,verbose=0,debug=0,header=''): |
|
359 | def system(cmd,verbose=0,debug=0,header=''): | |
352 | """Execute a system command, return its exit status. |
|
360 | """Execute a system command, return its exit status. | |
353 |
|
361 | |||
@@ -369,6 +377,7 b" def system(cmd,verbose=0,debug=0,header=''):" | |||||
369 | if not debug: stat = os.system(cmd) |
|
377 | if not debug: stat = os.system(cmd) | |
370 | return stat |
|
378 | return stat | |
371 |
|
379 | |||
|
380 | ||||
372 | def abbrev_cwd(): |
|
381 | def abbrev_cwd(): | |
373 | """ Return abbreviated version of cwd, e.g. d:mydir """ |
|
382 | """ Return abbreviated version of cwd, e.g. d:mydir """ | |
374 | cwd = os.getcwd().replace('\\','/') |
|
383 | cwd = os.getcwd().replace('\\','/') | |
@@ -439,6 +448,7 b" if os.name in ('nt','dos'):" | |||||
439 |
|
448 | |||
440 | shell.__doc__ = shell_ori.__doc__ |
|
449 | shell.__doc__ = shell_ori.__doc__ | |
441 |
|
450 | |||
|
451 | ||||
442 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): |
|
452 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): | |
443 | """Dummy substitute for perl's backquotes. |
|
453 | """Dummy substitute for perl's backquotes. | |
444 |
|
454 | |||
@@ -468,45 +478,11 b" def getoutput(cmd,verbose=0,debug=0,header='',split=0):" | |||||
468 | else: |
|
478 | else: | |
469 | return output |
|
479 | return output | |
470 |
|
480 | |||
471 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): |
|
|||
472 | """Return (standard output,standard error) of executing cmd in a shell. |
|
|||
473 |
|
||||
474 | Accepts the same arguments as system(), plus: |
|
|||
475 |
|
||||
476 | - split(0): if true, each of stdout/err is returned as a list split on |
|
|||
477 | newlines. |
|
|||
478 |
|
||||
479 | Note: a stateful version of this function is available through the |
|
|||
480 | SystemExec class.""" |
|
|||
481 |
|
||||
482 | if verbose or debug: print header+cmd |
|
|||
483 | if not cmd: |
|
|||
484 | if split: |
|
|||
485 | return [],[] |
|
|||
486 | else: |
|
|||
487 | return '','' |
|
|||
488 | if not debug: |
|
|||
489 | p = subprocess.Popen(cmd, shell=True, |
|
|||
490 | stdin=subprocess.PIPE, |
|
|||
491 | stdout=subprocess.PIPE, |
|
|||
492 | stderr=subprocess.PIPE, |
|
|||
493 | close_fds=True) |
|
|||
494 | pin, pout, perr = (p.stdin, p.stdout, p.stderr) |
|
|||
495 |
|
||||
496 | tout = pout.read().rstrip() |
|
|||
497 | terr = perr.read().rstrip() |
|
|||
498 | pin.close() |
|
|||
499 | pout.close() |
|
|||
500 | perr.close() |
|
|||
501 | if split: |
|
|||
502 | return tout.split('\n'),terr.split('\n') |
|
|||
503 | else: |
|
|||
504 | return tout,terr |
|
|||
505 |
|
||||
506 | # for compatibility with older naming conventions |
|
481 | # for compatibility with older naming conventions | |
507 | xsys = system |
|
482 | xsys = system | |
508 | bq = getoutput |
|
483 | bq = getoutput | |
509 |
|
484 | |||
|
485 | ||||
510 | class SystemExec: |
|
486 | class SystemExec: | |
511 | """Access the system and getoutput functions through a stateful interface. |
|
487 | """Access the system and getoutput functions through a stateful interface. | |
512 |
|
488 |
@@ -11,12 +11,25 b' to use these functions in platform agnostic fashion.' | |||||
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #***************************************************************************** |
|
13 | #***************************************************************************** | |
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | # Imports | |||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | from __future__ import absolute_import | |||
14 |
|
18 | |||
15 | import sys |
|
19 | import sys | |
16 | import os |
|
20 | import os | |
17 |
|
21 | |||
|
22 | from .baseutils import getoutputerror | |||
|
23 | ||||
|
24 | #----------------------------------------------------------------------------- | |||
|
25 | # Globals | |||
|
26 | #----------------------------------------------------------------------------- | |||
|
27 | ||||
18 | ignore_termtitle = True |
|
28 | ignore_termtitle = True | |
19 |
|
29 | |||
|
30 | #----------------------------------------------------------------------------- | |||
|
31 | # Functions | |||
|
32 | #----------------------------------------------------------------------------- | |||
20 |
|
33 | |||
21 | def _dummy_op(*a, **b): |
|
34 | def _dummy_op(*a, **b): | |
22 | """ A no-op function """ |
|
35 | """ A no-op function """ | |
@@ -36,7 +49,7 b' else:' | |||||
36 |
|
49 | |||
37 | def find_cmd(cmd): |
|
50 | def find_cmd(cmd): | |
38 | """Find the full path to a command using which.""" |
|
51 | """Find the full path to a command using which.""" | |
39 |
return |
|
52 | return getoutputerror('which %s' % cmd)[0] | |
40 |
|
53 | |||
41 |
|
54 | |||
42 | def get_long_path_name(path): |
|
55 | def get_long_path_name(path): |
General Comments 0
You need to be logged in to leave comments.
Login now