def test_get_home_dir_2(): """Testcase for py2exe logic, compressed lib """ unfrozen = path.get_home_dir() sys.frozen = True #fake filename for IPython.__init__ IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower() home_dir = path.get_home_dir(True) nt.assert_equal(home_dir, unfrozen)
def test_get_home_dir_8(): """Using registry hack for 'My Documents', os=='nt' HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. """ os.name = "nt" # Remove from stub environment all keys that may be set for key in ["HOME", "HOMESHARE", "HOMEDRIVE", "HOMEPATH", "USERPROFILE"]: env.pop(key, None) # Stub windows registry functions def OpenKey(x, y): class key: def Close(self): pass return key() def QueryValueEx(x, y): return [abspath(HOME_TEST_DIR)] wreg.OpenKey = OpenKey wreg.QueryValueEx = QueryValueEx home_dir = path.get_home_dir() nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
def test_get_home_dir_4(): """get_home_dir() still works if $HOME is not set""" if "HOME" in env: del env["HOME"] # this should still succeed, but we don't care what the answer is home = path.get_home_dir(False)
def test_get_home_dir_7(): """Using HOMESHARE, os=='nt'.""" os.name = 'nt' env["HOMESHARE"] = abspath(HOME_TEST_DIR) home_dir = path.get_home_dir() nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
def test_get_home_dir_4(): """get_home_dir() still works if $HOME is not set""" if 'HOME' in env: del env['HOME'] # this should still succeed, but we don't know what the answer should be home = path.get_home_dir(True) nt.assert_true(path._writable_dir(home))
def get_ipython_dir(): """Get the IPython directory for this platform and user. This uses the logic in `get_home_dir` to find the home directory and then adds .ipython to the end of the path. """ env = os.environ pjoin = os.path.join ipdir_def = ".ipython" home_dir = get_home_dir() xdg_dir = get_xdg_dir() # import pdb; pdb.set_trace() # dbg if "IPYTHON_DIR" in env: warn("The environment variable IPYTHON_DIR is deprecated. " "Please use IPYTHONDIR instead.") ipdir = env.get("IPYTHONDIR", env.get("IPYTHON_DIR", None)) if ipdir is None: # not set explicitly, use ~/.ipython ipdir = pjoin(home_dir, ipdir_def) if xdg_dir: # Several IPython versions (up to 1.x) defaulted to .config/ipython # on Linux. We have decided to go back to using .ipython everywhere xdg_ipdir = pjoin(xdg_dir, "ipython") if _writable_dir(xdg_ipdir): cu = compress_user if os.path.exists(ipdir): warn( ("Ignoring {0} in favour of {1}. Remove {0} to " "get rid of this message").format( cu(xdg_ipdir), cu(ipdir) ) ) elif os.path.islink(xdg_ipdir): warn( ("{0} is deprecated. Move link to {1} to " "get rid of this message").format( cu(xdg_ipdir), cu(ipdir) ) ) else: warn("Moving {0} to {1}".format(cu(xdg_ipdir), cu(ipdir))) shutil.move(xdg_ipdir, ipdir) ipdir = os.path.normpath(os.path.expanduser(ipdir)) if os.path.exists(ipdir) and not _writable_dir(ipdir): # ipdir exists, but is not writable warn("IPython dir '{0}' is not a writable location," " using a temp directory.".format(ipdir)) ipdir = tempfile.mkdtemp() elif not os.path.exists(ipdir): parent = os.path.dirname(ipdir) if not _writable_dir(parent): # ipdir does not exist and parent isn't writable warn("IPython parent '{0}' is not a writable location," " using a temp directory.".format(parent)) ipdir = tempfile.mkdtemp() return py3compat.cast_unicode(ipdir, fs_encoding)
def test_get_home_dir_1(): """Testcase for py2exe logic, un-compressed lib """ sys.frozen = True #fake filename for IPython.__init__ IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py")) home_dir = path.get_home_dir() nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
def _strip_home(path): """turns /home/you/.ipython/profile_foo into .ipython/profile_foo""" home = get_home_dir() if not home.endswith("/"): home = home + "/" if path.startswith(home): return path[len(home) :] else: return path
def test_get_xdg_dir_2(): """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" reload(path) path.get_home_dir = lambda : HOME_TEST_DIR os.name = "posix" env.pop('IPYTHON_DIR', None) env.pop('XDG_CONFIG_HOME', None) cfgdir=os.path.join(path.get_home_dir(), '.config') os.makedirs(cfgdir) nt.assert_equal(path.get_xdg_dir(), cfgdir)
def test_get_home_dir_5(): """Using HOMEDRIVE + HOMEPATH, os=='nt'. HOMESHARE is missing. """ os.name = 'nt' env.pop('HOMESHARE', None) env['HOMEDRIVE'], env['HOMEPATH'] = os.path.splitdrive(HOME_TEST_DIR) home_dir = path.get_home_dir() nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
def _remote_profie_dir_default(self): """turns /home/you/.ipython/profile_foo into .ipython/profile_foo """ home = get_home_dir() if not home.endswith('/'): home = home+'/' if self.profile_dir.startswith(home): return self.profile_dir[len(home):] else: return self.profile_dir
def test_get_home_dir_6(): """Using USERPROFILE, os=='nt'. HOMESHARE, HOMEDRIVE, HOMEPATH are missing. """ os.name = 'nt' env.pop('HOMESHARE', None) env.pop('HOMEDRIVE', None) env.pop('HOMEPATH', None) env["USERPROFILE"] = abspath(HOME_TEST_DIR) home_dir = path.get_home_dir() nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
def test_get_xdg_dir_2(): """test_get_xdg_dir_2, check xdg_dir default to ~/.config""" reload(path) path.get_home_dir = lambda: HOME_TEST_DIR os.name = "posix" sys.platform = "linux2" env.pop("IPYTHON_DIR", None) env.pop("IPYTHONDIR", None) env.pop("XDG_CONFIG_HOME", None) cfgdir = os.path.join(path.get_home_dir(), ".config") if not os.path.exists(cfgdir): os.makedirs(cfgdir) nt.assert_equal(path.get_xdg_dir(), cfgdir)
def test_get_xdg_dir_3(): """test_get_xdg_dir_3, check xdg_dir not used on OS X""" reload(path) path.get_home_dir = lambda : HOME_TEST_DIR os.name = "posix" sys.platform = "darwin" env.pop('IPYTHON_DIR', None) env.pop('IPYTHONDIR', None) env.pop('XDG_CONFIG_HOME', None) cfgdir=os.path.join(path.get_home_dir(), '.config') if not os.path.exists(cfgdir): os.makedirs(cfgdir) nt.assert_equal(path.get_xdg_dir(), None)
def test_get_home_dir_8(): """Using registry hack for 'My Documents', os=='nt' HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing. """ os.name = 'nt' # Remove from stub environment all keys that may be set for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']: env.pop(key, None) class key: def Close(self): pass with patch.object(wreg, 'OpenKey', return_value=key()), \ patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]): home_dir = path.get_home_dir() nt.assert_equal(home_dir, abspath(HOME_TEST_DIR))
def test_get_home_dir_3(): """get_home_dir() uses $HOME if set""" env["HOME"] = HOME_TEST_DIR home_dir = path.get_home_dir(True) # get_home_dir expands symlinks nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
def get_ipython_dir(): """Get the IPython directory for this platform and user. This uses the logic in `get_home_dir` to find the home directory and then adds .ipython to the end of the path. """ env = os.environ pjoin = os.path.join ipdir_def = ".ipython" home_dir = get_home_dir() xdg_dir = get_xdg_dir() # import pdb; pdb.set_trace() # dbg if "IPYTHON_DIR" in env: warn( "The environment variable IPYTHON_DIR is deprecated. " "Please use IPYTHONDIR instead." ) ipdir = env.get("IPYTHONDIR", env.get("IPYTHON_DIR", None)) if ipdir is None: # not set explicitly, use ~/.ipython ipdir = pjoin(home_dir, ipdir_def) if xdg_dir: # Several IPython versions (up to 1.x) defaulted to .config/ipython # on Linux. We have decided to go back to using .ipython everywhere xdg_ipdir = pjoin(xdg_dir, "ipython") if _writable_dir(xdg_ipdir): cu = compress_user if os.path.exists(ipdir): warn( ( "Ignoring {0} in favour of {1}. Remove {0} to " "get rid of this message" ).format(cu(xdg_ipdir), cu(ipdir)) ) elif os.path.islink(xdg_ipdir): warn( ( "{0} is deprecated. Move link to {1} to " "get rid of this message" ).format(cu(xdg_ipdir), cu(ipdir)) ) else: warn("Moving {0} to {1}".format(cu(xdg_ipdir), cu(ipdir))) shutil.move(xdg_ipdir, ipdir) ipdir = os.path.normpath(os.path.expanduser(ipdir)) if os.path.exists(ipdir) and not _writable_dir(ipdir): # ipdir exists, but is not writable warn( "IPython dir '{0}' is not a writable location," " using a temp directory.".format(ipdir) ) ipdir = tempfile.mkdtemp() elif not os.path.exists(ipdir): parent = os.path.dirname(ipdir) if not _writable_dir(parent): # ipdir does not exist and parent isn't writable warn( "IPython parent '{0}' is not a writable location," " using a temp directory.".format(parent) ) ipdir = tempfile.mkdtemp() return py3compat.cast_unicode(ipdir, fs_encoding)
def get_ipython_dir() -> str: """Get the IPython directory for this platform and user. This uses the logic in `get_home_dir` to find the home directory and then adds .ipython to the end of the path. """ env = os.environ pjoin = os.path.join ipdir_def = '.ipython' home_dir = get_home_dir() xdg_dir = get_xdg_dir() if 'IPYTHON_DIR' in env: warn( 'The environment variable IPYTHON_DIR is deprecated since IPython 3.0. ' 'Please use IPYTHONDIR instead.', DeprecationWarning) ipdir = env.get('IPYTHONDIR', env.get('IPYTHON_DIR', None)) if ipdir is None: # not set explicitly, use ~/.ipython ipdir = pjoin(home_dir, ipdir_def) if xdg_dir: # Several IPython versions (up to 1.x) defaulted to .config/ipython # on Linux. We have decided to go back to using .ipython everywhere xdg_ipdir = pjoin(xdg_dir, 'ipython') if _writable_dir(xdg_ipdir): cu = compress_user if os.path.exists(ipdir): warn(('Ignoring {0} in favour of {1}. Remove {0} to ' 'get rid of this message').format( cu(xdg_ipdir), cu(ipdir))) elif os.path.islink(xdg_ipdir): warn(('{0} is deprecated. Move link to {1} to ' 'get rid of this message').format( cu(xdg_ipdir), cu(ipdir))) else: ipdir = xdg_ipdir ipdir = os.path.normpath(os.path.expanduser(ipdir)) if os.path.exists(ipdir) and not _writable_dir(ipdir): # ipdir exists, but is not writable warn("IPython dir '{0}' is not a writable location," " using a temp directory.".format(ipdir)) ipdir = tempfile.mkdtemp() elif not os.path.exists(ipdir): parent = os.path.dirname(ipdir) if not _writable_dir(parent): # ipdir does not exist and parent isn't writable warn("IPython parent '{0}' is not a writable location," " using a temp directory.".format(parent)) ipdir = tempfile.mkdtemp() else: os.makedirs(ipdir, exist_ok=True) assert isinstance( ipdir, str), "all path manipulation should be str(unicode), but are not." return ipdir
def test_get_home_dir_3(): """Testcase $HOME is set, then use its value as home directory.""" env["HOME"] = HOME_TEST_DIR home_dir = path.get_home_dir() nt.assert_equal(home_dir, env["HOME"])
def test_get_home_dir_3(): """get_home_dir() uses $HOME if set""" env["HOME"] = HOME_TEST_DIR home_dir = path.get_home_dir(True) # get_home_dir expands symlinks nt.assert_equal(home_dir, os.path.realpath(env["HOME"]))
def test_get_home_dir_4(): """get_home_dir() still works if $HOME is not set""" if 'HOME' in env: del env['HOME'] # this should still succeed, but we don't care what the answer is home = path.get_home_dir(False)
def test_get_home_dir_3(): """Testcase $HOME is set, then use its value as home directory.""" env["HOME"] = HOME_TEST_DIR home_dir = path.get_home_dir() nt.assert_equal(home_dir, env["HOME"])
def test_get_home_dir_3(): """get_home_dir() uses $HOME if set""" env["HOME"] = HOME_TEST_DIR home_dir = path.get_home_dir(True) nt.assert_equal(home_dir, env["HOME"])
def test_get_home_dir_3(): """get_home_dir() uses $HOME if set""" env["HOME"] = HOME_TEST_DIR home_dir = path.get_home_dir(True) nt.assert_equal(home_dir, env["HOME"])