[cwconfig] Always use "sys.prefix" to determine installation prefix 3.26
authorDenis Laxalde <denis.laxalde@logilab.fr>
Thu, 15 Feb 2018 14:19:15 +0100
branch3.26
changeset 12273 9d08f89a3d85
parent 12272 36032de96867
child 12274 d2e9999861a5
[cwconfig] Always use "sys.prefix" to determine installation prefix We drop all legacy logic implemented in _find_prefix() which now happens to be buggy since we cleaned up setup.py in 3.26 (noticeably because virtualenv is no longer properly detected). Closes #17132990.
cubicweb/cwconfig.py
cubicweb/test/unittest_cwconfig.py
--- a/cubicweb/cwconfig.py	Thu Feb 15 10:55:49 2018 +0100
+++ b/cubicweb/cwconfig.py	Thu Feb 15 14:19:15 2018 +0100
@@ -240,36 +240,6 @@
     return modes[0]
 
 
-def _find_prefix(start_path=None):
-    """Return the prefix path of CubicWeb installation.
-
-    Walk parent directories of `start_path` looking for one containing a
-    'share/cubicweb' directory. The first matching directory is assumed as the
-    prefix installation of CubicWeb.
-
-    If run from within a virtualenv, the virtualenv root is used as
-    `start_path`. Otherwise, `start_path` defaults to cubicweb package
-    directory path.
-    """
-    if start_path is None:
-        try:
-            prefix = os.environ['VIRTUAL_ENV']
-        except KeyError:
-            prefix = CW_SOFTWARE_ROOT
-    else:
-        prefix = start_path
-    if not isdir(prefix):
-        prefix = dirname(prefix)
-    old_prefix = None
-    while (not isdir(join(prefix, 'share', 'cubicweb'))
-           or prefix.endswith('.egg')):
-        if prefix == old_prefix:
-            return sys.prefix
-        old_prefix = prefix
-        prefix = dirname(prefix)
-    return prefix
-
-
 def _cube_pkgname(cube):
     if not cube.startswith('cubicweb_'):
         return 'cubicweb_' + cube
@@ -392,10 +362,7 @@
     }
 
 
-try:
-    _INSTALL_PREFIX = os.environ['CW_INSTALL_PREFIX']
-except KeyError:
-    _INSTALL_PREFIX = _find_prefix()
+_INSTALL_PREFIX = os.environ.get('CW_INSTALL_PREFIX', sys.prefix)
 _USR_INSTALL = _INSTALL_PREFIX == '/usr'
 
 class CubicWebNoAppConfiguration(ConfigurationMixIn):
@@ -414,12 +381,12 @@
 
     if 'VIRTUAL_ENV' in os.environ:
         mode = os.environ.get('CW_MODE', 'user')
-        _CUBES_DIR = join(_INSTALL_PREFIX, 'share', 'cubicweb', 'cubes')
     else:
         mode = os.environ.get('CW_MODE', 'system')
-        _CUBES_DIR = join(_INSTALL_PREFIX, 'share', 'cubicweb', 'cubes')
     assert mode in ('system', 'user'), '"CW_MODE" should be either "user" or "system"'
 
+    _CUBES_DIR = join(_INSTALL_PREFIX, 'share', 'cubicweb', 'cubes')
+    assert _CUBES_DIR  # XXX only meaningful if CW_CUBES_DIR is not set
     CUBES_DIR = realpath(abspath(os.environ.get('CW_CUBES_DIR', _CUBES_DIR)))
     CUBES_PATH = os.environ.get('CW_CUBES_PATH', '').split(os.pathsep)
 
--- a/cubicweb/test/unittest_cwconfig.py	Thu Feb 15 10:55:49 2018 +0100
+++ b/cubicweb/test/unittest_cwconfig.py	Thu Feb 15 14:19:15 2018 +0100
@@ -35,7 +35,7 @@
 from cubicweb.devtools import ApptestConfiguration
 from cubicweb.devtools.testlib import BaseTestCase, TemporaryDirectory
 from cubicweb.cwconfig import (
-    CubicWebConfiguration, _find_prefix, _expand_modname)
+    CubicWebConfiguration, _expand_modname)
 
 
 def unabsolutize(path):
@@ -295,101 +295,6 @@
         self.assertNotIn('cubicweb_mycube.ccplugin', sys.modules, sorted(sys.modules))
 
 
-class FindPrefixTC(unittest.TestCase):
-
-    def make_dirs(self, basedir, *args):
-        path = join(basedir, *args)
-        if not os.path.exists(path):
-            os.makedirs(path)
-        return path
-
-    def make_file(self, basedir, *args):
-        self.make_dirs(basedir, *args[:-1])
-        file_path = join(basedir, *args)
-        with open(file_path, 'w') as f:
-            f.write('""" None """')
-        return file_path
-
-    def test_samedir(self):
-        with TemporaryDirectory() as prefix:
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            self.assertEqual(_find_prefix(prefix), prefix)
-
-    def test_samedir_filepath(self):
-        with TemporaryDirectory() as prefix:
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            file_path = self.make_file(prefix, 'bob.py')
-            self.assertEqual(_find_prefix(file_path), prefix)
-
-    def test_dir_inside_prefix(self):
-        with TemporaryDirectory() as prefix:
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            dir_path = self.make_dirs(prefix, 'bob')
-            self.assertEqual(_find_prefix(dir_path), prefix)
-
-    def test_file_in_dir_inside_prefix(self):
-        with TemporaryDirectory() as prefix:
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            file_path = self.make_file(prefix, 'bob', 'toto.py')
-            self.assertEqual(_find_prefix(file_path), prefix)
-
-    def test_file_in_deeper_dir_inside_prefix(self):
-        with TemporaryDirectory() as prefix:
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            file_path = self.make_file(prefix, 'bob', 'pyves', 'alain',
-                                       'adim', 'syt', 'toto.py')
-            self.assertEqual(_find_prefix(file_path), prefix)
-
-    def test_multiple_candidate_prefix(self):
-        with TemporaryDirectory() as tempdir:
-            self.make_dirs(tempdir, 'share', 'cubicweb')
-            prefix = self.make_dirs(tempdir, 'bob')
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            file_path = self.make_file(prefix, 'pyves', 'alain',
-                                       'adim', 'syt', 'toto.py')
-            self.assertEqual(_find_prefix(file_path), prefix)
-
-    def test_sister_candidate_prefix(self):
-        with TemporaryDirectory() as prefix:
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            self.make_dirs(prefix, 'bob', 'share', 'cubicweb')
-            file_path = self.make_file(prefix, 'bell', 'toto.py')
-            self.assertEqual(_find_prefix(file_path), prefix)
-
-    def test_multiple_parent_candidate_prefix(self):
-        with TemporaryDirectory() as tempdir:
-            self.make_dirs(tempdir, 'share', 'cubicweb')
-            prefix = self.make_dirs(tempdir, 'share', 'cubicweb', 'bob')
-            self.make_dirs(tempdir, 'share', 'cubicweb', 'bob', 'share',
-                           'cubicweb')
-            file_path = self.make_file(tempdir, 'share', 'cubicweb', 'bob',
-                                       'pyves', 'alain', 'adim', 'syt',
-                                       'toto.py')
-            self.assertEqual(_find_prefix(file_path), prefix)
-
-    def test_upper_candidate_prefix(self):
-        with TemporaryDirectory() as prefix:
-            self.make_dirs(prefix, 'share', 'cubicweb')
-            self.make_dirs(prefix, 'bell', 'bob', 'share', 'cubicweb')
-            file_path = self.make_file(prefix, 'bell', 'toto.py')
-            self.assertEqual(_find_prefix(file_path), prefix)
-
-    def test_no_prefix(self):
-        with TemporaryDirectory() as prefix:
-            self.assertEqual(_find_prefix(prefix), sys.prefix)
-
-    def test_virtualenv(self):
-        venv = os.environ.get('VIRTUAL_ENV')
-        try:
-            with TemporaryDirectory() as prefix:
-                os.environ['VIRTUAL_ENV'] = prefix
-                self.make_dirs(prefix, 'share', 'cubicweb')
-                self.assertEqual(_find_prefix(), prefix)
-        finally:
-            if venv:
-                os.environ['VIRTUAL_ENV'] = venv
-
-
 class ModnamesTC(unittest.TestCase):
 
     @templibdir