Correctly set the environment.

When running command without `shell=True`, python's subproccess will look
for variable `"PATH"` and `b"PATH"` and will complain if both are set.
So the defaultdict should not return something for `b"PATH"`.

We also escape space ` ` in the environment variables to not break
everything with directory containing space.
This commit is contained in:
Matthieu Gautier 2018-06-19 10:55:00 +02:00
parent 738bfa6744
commit e87835c61d
4 changed files with 41 additions and 15 deletions

View File

@ -4,7 +4,7 @@ import subprocess
import platform
import distro
from .utils import pj, download_remote, Defaultdict
from .utils import pj, download_remote, escape_path
from ._global import neutralEnv, option
@ -125,21 +125,34 @@ class BuildEnv:
pkgconfig_path = pj(self.install_dir, self.libprefix, 'pkgconfig')
env['PKG_CONFIG_PATH'] = ':'.join([env['PKG_CONFIG_PATH'], pkgconfig_path])
env['PATH'] = ':'.join([pj(self.install_dir, 'bin'), env['PATH']])
env['PATH'] = ':'.join([
escape_path(pj(self.install_dir, 'bin')),
env['PATH']
])
env['LD_LIBRARY_PATH'] = ':'.join([env['LD_LIBRARY_PATH'],
pj(self.install_dir, 'lib'),
pj(self.install_dir, self.libprefix)
])
env['QMAKE_CXXFLAGS'] = " ".join(['-I'+pj(self.install_dir, 'include'), env['QMAKE_CXXFLAGS']])
env['CPPFLAGS'] = " ".join(['-I'+pj(self.install_dir, 'include'), env['CPPFLAGS']])
env['QMAKE_LFLAGS'] = " ".join(['-L'+pj(self.install_dir, 'lib'),
'-L'+pj(self.install_dir, self.libprefix),
env['QMAKE_LFLAGS']])
env['LDFLAGS'] = " ".join(['-L'+pj(self.install_dir, 'lib'),
'-L'+pj(self.install_dir, self.libprefix),
env['LDFLAGS']])
env['QMAKE_CXXFLAGS'] = " ".join([
escape_path('-I'+pj(self.install_dir, 'include')),
env['QMAKE_CXXFLAGS']
])
env['CPPFLAGS'] = " ".join([
escape_path('-I'+pj(self.install_dir, 'include')),
env['CPPFLAGS']
])
env['QMAKE_LFLAGS'] = " ".join([
escape_path('-L'+pj(self.install_dir, 'lib')),
escape_path('-L'+pj(self.install_dir, self.libprefix)),
env['QMAKE_LFLAGS']
])
env['LDFLAGS'] = " ".join([
escape_path('-L'+pj(self.install_dir, 'lib')),
escape_path('-L'+pj(self.install_dir, self.libprefix)),
env['LDFLAGS']
])
if cross_comp_flags:
self.platformInfo.set_comp_flags(env)

View File

@ -3,7 +3,7 @@ import os
import shutil
import time
from kiwixbuild.utils import pj, Context, SkipCommand, WarningMessage, extract_archive, Defaultdict, StopBuild, run_command, colorize, copy_tree
from kiwixbuild.utils import pj, Context, SkipCommand, WarningMessage, extract_archive, StopBuild, run_command, colorize, copy_tree
from kiwixbuild.versions import main_project_versions, base_deps_versions
from kiwixbuild._global import neutralEnv, option, get_target_step

View File

@ -6,7 +6,7 @@ from .base import (
MakeBuilder,
)
from kiwixbuild.utils import Remotefile, pj, Defaultdict, SkipCommand, run_command
from kiwixbuild.utils import Remotefile, pj, SkipCommand, run_command
from kiwixbuild._global import get_target_step
class LibMagic(Dependency):

View File

@ -8,6 +8,7 @@ import urllib.request
import urllib.error
import ssl
import subprocess
import re
from collections import namedtuple, defaultdict
from kiwixbuild._global import neutralEnv, option
@ -39,13 +40,25 @@ def xrun_find(name):
return output[:-1].decode()
regex_space = re.compile(r'((?<!\\) )')
def escape_path(path):
path = str(path)
return regex_space.sub(r'\ ', path)
class Defaultdict(defaultdict):
def __getattr__(self, name):
return self[name]
def DefaultEnv():
return Defaultdict(str, os.environ)
class DefaultEnv(Defaultdict):
def __init__(self):
super().__init__(str, os.environ)
def __getitem__(self, name):
if name == b'PATH':
raise KeyError
return super().__getitem__(name)
def remove_duplicates(iterable, key_function=None):
@ -265,7 +278,7 @@ def extract_archive(archive_path, dest_dir, topdir=None, name=None):
def run_command(command, cwd, context, *, env=None, input=None):
os.makedirs(cwd, exist_ok=True)
if env is None:
env = Defaultdict(str, os.environ)
env = DefaultEnv()
log = None
try:
if not option('verbose'):