def test_env_vars(): """Test that we are correctly encoding env vars in our kernel spec""" # Create a variable with the file system encoding and save it # in our PYTHONPATH env_var = to_fs_from_unicode(u'ñññ') CONF.set('main', 'spyder_pythonpath', [env_var]) # Create a kernel spec kernel_spec = SpyderKernelSpec() # Assert PYTHONPATH is in env vars and it's not empty assert kernel_spec.env['PYTHONPATH'] != '' # Assert all env vars are binary strings assert all([is_binary_string(v) for v in kernel_spec.env.values()]) # Remove our entry from PYTHONPATH CONF.set('main', 'spyder_pythonpath', [])
def create_rope_project(self, root_path): """Create a Rope project on a desired path""" if PY2: root_path = encoding.to_fs_from_unicode(root_path) else: #TODO: test if this is working without any further change in # Python 3 with a user account containing unicode characters pass try: import rope.base.project self.project = rope.base.project.Project(root_path, **ROPE_PREFS) except ImportError: print >> STDERR, 'project error' self.project = None if DEBUG_EDITOR: log_last_error(LOG_FILENAME, "create_rope_project: %r" % root_path) except TypeError: self.project = None if DEBUG_EDITOR: log_last_error(LOG_FILENAME, "create_rope_project: %r" % root_path) self.validate()
def create_rope_project(self, root_path): """Create a Rope project on a desired path""" if PY2: root_path = encoding.to_fs_from_unicode(root_path) else: #TODO: test if this is working without any further change in # Python 3 with a user account containing unicode characters pass try: import rope.base.project self.project = rope.base.project.Project(root_path, **ROPE_PREFS) except ImportError: print >>STDERR, 'project error' self.project = None if DEBUG_EDITOR: log_last_error(LOG_FILENAME, "create_rope_project: %r" % root_path) except TypeError: self.project = None if DEBUG_EDITOR: log_last_error(LOG_FILENAME, "create_rope_project: %r" % root_path) self.validate()
def run_python_script_in_terminal(fname, wdir, args, interact, debug, python_args, executable=None): """ Run Python script in an external system terminal. :str wdir: working directory, may be empty. """ if executable is None: executable = get_python_executable() # If fname or python_exe contains spaces, it can't be ran on Windows, so we # have to enclose them in quotes. Also wdir can come with / as os.sep, so # we need to take care of it. if os.name == 'nt': fname = '"' + fname + '"' wdir = wdir.replace('/', '\\') executable = '"' + executable + '"' p_args = [executable] p_args += get_python_args(fname, python_args, interact, debug, args) if os.name == 'nt': cmd = 'start cmd.exe /c "cd %s && ' % wdir + ' '.join(p_args) + '"' # Command line and cwd have to be converted to the filesystem # encoding before passing them to subprocess, but only for # Python 2. # See https://bugs.python.org/issue1759845#msg74142 and Issue 1856 if PY2: cmd = encoding.to_fs_from_unicode(cmd) wdir = encoding.to_fs_from_unicode(wdir) try: run_shell_command(cmd, cwd=wdir) except WindowsError: from qtpy.QtWidgets import QMessageBox from spyder.config.base import _ QMessageBox.critical( None, _('Run'), _("It was not possible to run this file in " "an external terminal"), QMessageBox.Ok) elif os.name == 'posix': 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
def run_python_script_in_terminal(fname, wdir, args, interact, debug, python_args, executable=None): """ Run Python script in an external system terminal. :str wdir: working directory, may be empty. """ if executable is None: executable = get_python_executable() # If fname or python_exe contains spaces, it can't be ran on Windows, so we # have to enclose them in quotes. Also wdir can come with / as os.sep, so # we need to take care of it. if os.name == 'nt': fname = '"' + fname + '"' wdir = wdir.replace('/', '\\') executable = '"' + executable + '"' p_args = [executable] p_args += get_python_args(fname, python_args, interact, debug, args) if os.name == 'nt': cmd = 'start cmd.exe /K "' if wdir: cmd += 'cd ' + wdir + ' && ' cmd += ' '.join(p_args) + '"' + ' ^&^& exit' # Command line and cwd have to be converted to the filesystem # encoding before passing them to subprocess, but only for # Python 2. # See https://bugs.python.org/issue1759845#msg74142 and # spyder-ide/spyder#1856. if PY2: cmd = encoding.to_fs_from_unicode(cmd) wdir = encoding.to_fs_from_unicode(wdir) try: if wdir: run_shell_command(cmd, cwd=wdir) else: run_shell_command(cmd) except WindowsError: from qtpy.QtWidgets import QMessageBox from spyder.config.base import _ QMessageBox.critical( None, _('Run'), _("It was not possible to run this file in " "an external terminal"), QMessageBox.Ok) elif sys.platform.startswith('linux'): 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 elif sys.platform == 'darwin': f = tempfile.NamedTemporaryFile('wt', prefix='run_spyder_', suffix='.sh', dir=get_temp_dir(), delete=False) if wdir: f.write('cd {}\n'.format(wdir)) f.write(' '.join(p_args)) f.close() os.chmod(f.name, 0o777) def run_terminal_thread(): proc = run_shell_command('open -a Terminal.app ' + f.name) # Prevent race condition time.sleep(3) proc.wait() os.remove(f.name) thread = threading.Thread(target=run_terminal_thread) thread.start() else: raise NotImplementedError
def run_python_script_in_terminal(fname, wdir, args, interact, debug, python_args, executable=None): """ Run Python script in an external system terminal. :str wdir: working directory, may be empty. """ if executable is None: executable = get_python_executable() # If fname or python_exe contains spaces, it can't be ran on Windows, so we # have to enclose them in quotes. Also wdir can come with / as os.sep, so # we need to take care of it. if os.name == 'nt': fname = '"' + fname + '"' wdir = wdir.replace('/', '\\') executable = '"' + executable + '"' p_args = [executable] p_args += get_python_args(fname, python_args, interact, debug, args) if os.name == 'nt': cmd = 'start cmd.exe /c "cd %s && ' % wdir + ' '.join(p_args) + '"' # Command line and cwd have to be converted to the filesystem # encoding before passing them to subprocess, but only for # Python 2. # See https://bugs.python.org/issue1759845#msg74142 and Issue 1856 if PY2: cmd = encoding.to_fs_from_unicode(cmd) wdir = encoding.to_fs_from_unicode(wdir) try: run_shell_command(cmd, cwd=wdir) except WindowsError: from qtpy.QtWidgets import QMessageBox from spyder.config.base import _ QMessageBox.critical(None, _('Run'), _("It was not possible to run this file in " "an external terminal"), QMessageBox.Ok) elif os.name == 'posix': 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