Skip to content

Commit

Permalink
Run script: Allow for wdir = '' in run_python_script_in_terminal().
Browse files Browse the repository at this point in the history
Fixes spyder-ide#3155: Executing a python script in external terminal without
specifying a working directory leads to wdir = '', which is not handled
correctly.

In the previous version, the working directory was passed as an option
to the script if xterm is used. This did not make sense to me, so I
removd that.
  • Loading branch information
jitseniesen committed Jun 11, 2016
1 parent ebb7ca7 commit de54122
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions spyderlib/utils/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ def get_python_args(fname, python_args, interact, debug, end_args):

def run_python_script_in_terminal(fname, wdir, args, interact,
debug, python_args):
"""Run Python script in an external system terminal"""
"""
Run Python script in an external system terminal.
:str wdir: working directory, may be empty.
"""

# If fname has spaces on it it can't be ran on Windows, so we have to
# enclose it in quotes. Also wdir can come with / as os.sep, so we
Expand Down Expand Up @@ -285,25 +289,30 @@ def run_python_script_in_terminal(fname, wdir, args, interact,
"an external terminal"),
QMessageBox.Ok)
elif os.name == 'posix':
cmd = 'gnome-terminal'
if is_program_installed(cmd):
run_program(cmd, ['--working-directory', wdir, '-x'] + p_args,
cwd=wdir)
return
cmd = 'konsole'
if is_program_installed(cmd):
run_program(cmd, ['--workdir', wdir, '-e'] + p_args,
cwd=wdir)
return
cmd = 'xfce4-terminal'
if is_program_installed(cmd):
run_program(cmd, ['--working-directory', wdir, '-x'] + p_args,
cwd=wdir)
return
cmd = 'xterm'
if is_program_installed(cmd):
run_program(cmd, ['-e'] + p_args + [wdir])
return
programs = [{'cmd': 'gnome-terminal',
'wdir-option': '--working-directory',
'execute-option': '-x'},
{'cmd': 'konsole',
'wdir-option': '--workdir',
'execute-option': '-e'},
{'cmd': 'xfce4-terminal',
'wdir-option': '--working-directory',
'execute-option': '-x'},
{'cmd': 'xterm',
'wdir-option': None,
'execute-option': '-e'},]
for program in programs:
if is_program_installed(program['cmd']):
arglist = []
if program['wdir-option'] and wdir:
arglist += [program['wdir-option'], wdir]
arglist.append(program['execute-option'])
arglist += p_args
if wdir:
run_program(program['cmd'], arglist, cwd=wdir)
else:
run_program(program['cmd'], arglist)
return
# TODO: Add a fallback to OSX
else:
raise NotImplementedError
Expand Down

0 comments on commit de54122

Please sign in to comment.