Commit 8c1a4892 authored by Jérome Perrin's avatar Jérome Perrin

grid/utils: set PYTHONNOUSERSITE

When PYTHONNOUSERSITE environemnt variable is set, python does not load user
site.
User site packages may contain a different version of buildout, which would
be selected when slapos runs buildout, causing unexpected behaviours.
parent fe539da3
Pipeline #12283 passed with stage
in 0 seconds
...@@ -56,8 +56,6 @@ PYTHON_ENVIRONMENT_REMOVE_LIST = [ ...@@ -56,8 +56,6 @@ PYTHON_ENVIRONMENT_REMOVE_LIST = [
'PYTHONDEBUG', 'PYTHONDEBUG',
'PYTHONDONTWRITEBYTECODE', 'PYTHONDONTWRITEBYTECODE',
'PYTHONINSPECT', 'PYTHONINSPECT',
'PYTHONNOUSERSITE',
'PYTHONNOUSERSITE',
'PYTHONUNBUFFERED', 'PYTHONUNBUFFERED',
'PYTHONVERBOSE', 'PYTHONVERBOSE',
] ]
...@@ -149,6 +147,7 @@ def getCleanEnvironment(logger, home_path='/tmp'): ...@@ -149,6 +147,7 @@ def getCleanEnvironment(logger, home_path='/tmp'):
if old is not None: if old is not None:
removed_env.append(k) removed_env.append(k)
changed_env['HOME'] = env['HOME'] = home_path changed_env['HOME'] = env['HOME'] = home_path
changed_env['PYTHONNOUSERSITE'] = env['PYTHONNOUSERSITE'] = 'true'
for k, v in sorted(changed_env.items()): for k, v in sorted(changed_env.items()):
logger.debug('Overridden %s = %r', k, v) logger.debug('Overridden %s = %r', k, v)
if removed_env: if removed_env:
......
...@@ -294,6 +294,38 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase): ...@@ -294,6 +294,38 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase):
with open(os.path.join(partition.partition_path, 'env_HOME')) as f: with open(os.path.join(partition.partition_path, 'env_HOME')) as f:
self.assertEqual(f.read().strip(), partition.partition_path) self.assertEqual(f.read().strip(), partition.partition_path)
def test_no_user_site_packages(self):
# When running instance buildout, python's user site packages are ignored
computer = ComputerForTest(self.software_root, self.instance_root)
partition = computer.instance_list[0]
partition.requested_state = 'started'
# Make a broken user site package in this partition home, that will error
# as soon as site is initialized, making python unusable. While this is
# a bit unrealistic, real life problems were observed after installing a
# broken buildout with `pip install --editable --user`, because user site
# packages have priority over sys.path assignments in buildout generated
# scripts.
home_site_packages_dir = os.path.join(
partition.partition_path,
'.local',
'lib',
'python{version[0]}.{version[1]}'.format(version=sys.version_info),
'site-packages',
)
os.makedirs(home_site_packages_dir)
with open(os.path.join(home_site_packages_dir, 'dummy-nspkg.pth'),
'w') as f:
f.write('import sys; raise SystemExit("ahaha")')
partition.software.setBuildout(textwrap.dedent('''\
#!{sys.executable}
print("ok")
'''.format(sys=sys)))
with httmock.HTTMock(computer.request_handler):
self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS)
class MasterMixin(BasicMixin): class MasterMixin(BasicMixin):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment