Skip to content

Commit

Permalink
Move add_pathlist_to_PYTHONPATH function to utils/misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Dec 25, 2015
1 parent b7170e9 commit 252c9ad
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
25 changes: 24 additions & 1 deletion spyderlib/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 Pierre Raybaut
# Copyright © 2009- The Spyder Developmet Team
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)

Expand All @@ -11,6 +11,8 @@
import sys
import stat

from spyderlib.py3compat import is_text_string


def __remove_pyc_pyo(fname):
"""Eventually remove .pyc and .pyo files associated to a Python script"""
Expand All @@ -19,6 +21,7 @@ def __remove_pyc_pyo(fname):
if osp.exists(fname+ending):
os.remove(fname+ending)


def rename_file(source, dest):
"""
Rename file from *source* to *dest*
Expand All @@ -27,6 +30,7 @@ def rename_file(source, dest):
os.rename(source, dest)
__remove_pyc_pyo(source)


def remove_file(fname):
"""
Remove file *fname*
Expand All @@ -35,6 +39,7 @@ def remove_file(fname):
os.remove(fname)
__remove_pyc_pyo(fname)


def move_file(source, dest):
"""
Move file from *source* to *dest*
Expand Down Expand Up @@ -227,6 +232,24 @@ def get_common_path(pathlist):
else:
return osp.abspath(common)


def add_pathlist_to_PYTHONPATH(env, pathlist, drop_env=False):
# PyQt API 1/2 compatibility-related tests:
assert isinstance(env, list)
assert all([is_text_string(path) for path in env])

pypath = "PYTHONPATH"
pathstr = os.pathsep.join(pathlist)
if os.environ.get(pypath) is not None and not drop_env:
for index, var in enumerate(env[:]):
if var.startswith(pypath+'='):
env[index] = var.replace(pypath+'=',
pypath+'='+pathstr+os.pathsep)
env.append('OLD_PYTHONPATH='+os.environ[pypath])
else:
env.append(pypath+'='+pathstr)


if __name__ == '__main__':
if os.name == 'nt':
assert get_common_path([
Expand Down
22 changes: 2 additions & 20 deletions spyderlib/widgets/externalshell/baseshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# pylint: disable=R0911
# pylint: disable=R0201

import os
import os.path as osp
from time import time, strftime, gmtime

Expand All @@ -22,29 +21,12 @@
LOCALE_CODEC = QTextCodec.codecForLocale()

# Local imports
from spyderlib.utils.qthelpers import (create_toolbutton, create_action,
from spyderlib.utils.qthelpers import (create_toolbutton, create_action,
add_actions)
from spyderlib.config.base import get_conf_path, _
from spyderlib.py3compat import is_text_string, to_text_string
from spyderlib.py3compat import to_text_string


def add_pathlist_to_PYTHONPATH(env, pathlist, drop_env=False):
# PyQt API 1/2 compatibility-related tests:
assert isinstance(env, list)
assert all([is_text_string(path) for path in env])

pypath = "PYTHONPATH"
pathstr = os.pathsep.join(pathlist)
if os.environ.get(pypath) is not None and not drop_env:
for index, var in enumerate(env[:]):
if var.startswith(pypath+'='):
env[index] = var.replace(pypath+'=',
pypath+'='+pathstr+os.pathsep)
env.append('OLD_PYTHONPATH='+os.environ[pypath])
else:
env.append(pypath+'='+pathstr)


#TODO: code refactoring/cleaning (together with systemshell.py and pythonshell.py)
class ExternalShellBase(QWidget):
"""External Shell widget: execute Python script in a separate process"""
Expand Down
4 changes: 2 additions & 2 deletions spyderlib/widgets/externalshell/pythonshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from spyderlib.widgets.shell import PythonShellWidget
from spyderlib.widgets.variableexplorer.namespacebrowser import NamespaceBrowser
from spyderlib.utils.bsdsocket import communicate, write_packet
from spyderlib.widgets.externalshell.baseshell import (ExternalShellBase,
add_pathlist_to_PYTHONPATH)
from spyderlib.utils.misc import add_pathlist_to_PYTHONPATH
from spyderlib.widgets.externalshell.baseshell import ExternalShellBase
from spyderlib.widgets.variableexplorer.collectionseditor import CollectionsEditor
from spyderlib.py3compat import (is_text_string, to_text_string,
to_binary_string)
Expand Down
4 changes: 2 additions & 2 deletions spyderlib/widgets/externalshell/systemshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
# Local imports
from spyderlib.utils.programs import shell_split
from spyderlib.config.base import _
from spyderlib.widgets.externalshell.baseshell import (ExternalShellBase,
add_pathlist_to_PYTHONPATH)
from spyderlib.widgets.externalshell.baseshell import ExternalShellBase
from spyderlib.widgets.shell import TerminalWidget
from spyderlib.py3compat import to_text_string, is_text_string
from spyderlib.utils.misc import add_pathlist_to_PYTHONPATH
import spyderlib.utils.icon_manager as ima


Expand Down

0 comments on commit 252c9ad

Please sign in to comment.