def main(): self_path = os.path.normcase(os.path.dirname(os.path.abspath(__file__))) sys.path[:] = [ path for path in sys.path if os.path.normcase(path) != self_path ] libpath = get_pypackages_path() if not libpath: return # First, drop site related paths. original_sys_path = sys.path[:] known_paths = set() site.addusersitepackages(known_paths) site.addsitepackages(known_paths) known_paths = set(os.path.normcase(path) for path in known_paths) original_sys_path = [ path for path in original_sys_path if os.path.normcase(path) not in known_paths ] sys.path[:] = original_sys_path # Second, add lib directories, ensuring .pth file are processed. site.addsitedir(libpath) # Then add the removed path to the tail of the paths known_paths.clear() site.addusersitepackages(known_paths) site.addsitepackages(known_paths)
def test_no_home_directory(self): # bpo-10496: getuserbase() and getusersitepackages() must not fail if # the current user has no home directory (if expanduser() returns the # path unchanged). site.USER_SITE = None site.USER_BASE = None with EnvironmentVarGuard() as environ, \ mock.patch('os.path.expanduser', lambda path: path): del environ['PYTHONUSERBASE'] del environ['APPDATA'] user_base = site.getuserbase() self.assertTrue(user_base.startswith('~' + os.sep), user_base) user_site = site.getusersitepackages() self.assertTrue(user_site.startswith(user_base), user_site) with mock.patch('os.path.isdir', return_value=False) as mock_isdir, \ mock.patch.object(site, 'addsitedir') as mock_addsitedir, \ support.swap_attr(site, 'ENABLE_USER_SITE', True): # addusersitepackages() must not add user_site to sys.path # if it is not an existing directory known_paths = set() site.addusersitepackages(known_paths) mock_isdir.assert_called_once_with(user_site) mock_addsitedir.assert_not_called() self.assertFalse(known_paths)
def test_no_home_directory(self): # bpo-10496: getuserbase() and getusersitepackages() must not fail if # the current user has no home directory (if expanduser() returns the # path unchanged). site.USER_SITE = None site.USER_BASE = None sysconfig._CONFIG_VARS = None with EnvironmentVarGuard() as environ, \ support.swap_attr(os.path, 'expanduser', lambda path: path): del environ['PYTHONUSERBASE'] del environ['APPDATA'] user_base = site.getuserbase() self.assertTrue(user_base.startswith('~' + os.sep), user_base) user_site = site.getusersitepackages() self.assertTrue(user_site.startswith(user_base), user_site) def fake_isdir(path): fake_isdir.arg = path return False fake_isdir.arg = None def must_not_be_called(*args): raise AssertionError with support.swap_attr(os.path, 'isdir', fake_isdir), \ support.swap_attr(site, 'addsitedir', must_not_be_called), \ support.swap_attr(site, 'ENABLE_USER_SITE', True): # addusersitepackages() must not add user_site to sys.path # if it is not an existing directory known_paths = set() site.addusersitepackages(known_paths) self.assertEqual(fake_isdir.arg, user_site) self.assertFalse(known_paths)
def main(): self_path = os.path.normcase(os.path.dirname(os.path.abspath(__file__))) sys.path[:] = [ path for path in sys.path if os.path.normcase(path) != self_path ] if sys.version_info[0] == 2: load_next_sitecustomize_py2() else: load_next_sitecustomize_py3() libpath = get_pypackages_path() if not libpath: return # First, drop site related paths. original_sys_path = sys.path[:] known_paths = set() site.addusersitepackages(known_paths) site.addsitepackages(known_paths) known_paths = {os.path.normcase(path) for path in known_paths} original_sys_path = [ path for path in original_sys_path if os.path.normcase(path) not in known_paths ] sys.path[:] = original_sys_path # Second, add lib directories, ensuring .pth file are processed. site.addsitedir(libpath) if not os.environ.pop("NO_SITE_PACKAGES", None): # Then add the removed path to the tail of the paths known_paths.clear() site.addusersitepackages(known_paths) site.addsitepackages(known_paths) if "PEP582_PACKAGES" in os.environ: patch_sysconfig(libpath)
def update_event(self, inp=-1): self.set_output_val(0, site.addusersitepackages(self.input(0)))
import sys import site import os try: getusersitepackages = site.getusersitepackages except AttributeError: # Probably virtualenv. Don't do anything pass else: old_user_site = getusersitepackages() site.USER_BASE = os.environ.get("PYTHONUSERBASE", "/var/data/python") site.USER_SITE = None site.USER_SITE = getusersitepackages() sys.path = [ item for item in sys.path if not item.startswith(old_user_site) ] site.addusersitepackages(site.removeduppaths())