Skip to content

Commit

Permalink
do not polute the current environment on chroot
Browse files Browse the repository at this point in the history
setting env. variables in the current process env. instead of the
subprocess can lead to various issues, including not being able to
edit the sources.list because after xnest the DESKTOP variable is
set to :13 which is no longer valid. this change also seems to fix
an issue where on Xubuntu the XDG menu did not display all menu
entries, now this is no longer the case (all applications are shown)
  • Loading branch information
fluxer committed Mar 10, 2015
1 parent e17a154 commit a2eb05f
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/lib/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def dir_current():
cwd = '/'
return cwd

def system_command(command, shell=False, cwd=None, catch=False):
def system_command(command, shell=False, cwd=None, catch=False, env=None):
if not cwd:
cwd = dir_current()
elif not os.path.isdir(cwd):
Expand All @@ -111,13 +111,13 @@ def system_command(command, shell=False, cwd=None, catch=False):
command = shlex.split(command)
if catch or CATCH:
pipe = subprocess.Popen(command, stderr=subprocess.PIPE, \
shell=shell, cwd=cwd)
shell=shell, cwd=cwd, env=env)
pipe.wait()
if pipe.returncode != 0:
raise(Exception(pipe.communicate()[1].strip()))
return pipe.returncode
else:
return subprocess.check_call(command, shell=shell, cwd=cwd)
return subprocess.check_call(command, shell=shell, cwd=cwd, env=env)

def chroot_exec(command, prepare=True, mount=True, output=False, xnest=False, shell=False):
real_root = os.open('/', os.O_RDONLY)
Expand Down Expand Up @@ -157,29 +157,31 @@ def chroot_exec(command, prepare=True, mount=True, output=False, xnest=False, sh
if not config.LOCALES == 'C':
system_command(('locale-gen', config.LOCALES))

os.putenv('PATH', '/usr/sbin:/usr/bin:/sbin:/bin')
os.putenv('HOME', '/root')
os.putenv('LC_ALL', config.LOCALES)
os.putenv('LANGUAGE', config.LOCALES)
os.putenv('LANG', config.LOCALES)
os.putenv('CASPER_GENERATE_UUID', '1')
if not xnest:
os.putenv('DEBIAN_FRONTEND', 'noninteractive')
# FIXME: os.putenv('DEBIAN_PRIORITY', '')
os.putenv('DEBCONF_NONINTERACTIVE_SEEN', 'true')
os.putenv('DEBCONF_NOWARNINGS', 'true')

environment = {
'PATH': '/usr/sbin:/usr/bin:/sbin:/bin',
'HOME': '/root',
'LC_ALL': config.LOCALES,
'LANGUAGE': config.LOCALES,
'LANG': config.LOCALES,
'CASPER_GENERATE_UUID': '1',
}
if xnest:
os.putenv('HOME', '/etc/skel')
os.putenv('XDG_CACHE_HOME', '/etc/skel/.cache')
os.putenv('XDG_DATA_HOME', '/etc/skel/.local/share')
os.putenv('XDG_CONFIG_HOME', '/etc/skel/.config')
os.putenv('DISPLAY', ':13')
environment['HOME'] = '/etc/skel'
environment['XDG_CACHE_HOME'] = '/etc/skel/.cache'
environment['XDG_DATA_HOME'] = '/etc/skel/.local/share'
environment['XDG_CONFIG_HOME'] = '/etc/skel/.config'
environment['DISPLAY'] = ':13'
else:
environment['DEBIAN_FRONTEND'] = 'noninteractive'
# FIXME: is this needed?
# environment['DEBIAN_PRIORITY'] = ''
environment['DEBCONF_NONINTERACTIVE_SEEN'] = 'true'
environment['DEBCONF_NOWARNINGS'] = 'true'

if output:
out = get_output(command)
else:
system_command(command, shell=shell)
system_command(command, shell=shell, env=environment)
finally:
os.fchdir(real_root)
os.chroot('.')
Expand Down

0 comments on commit a2eb05f

Please # to comment.