Skip to content

Commit

Permalink
Correctly set the environment.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mgautierfr committed Nov 8, 2023
1 parent 42a0658 commit 1ed7fc4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
33 changes: 23 additions & 10 deletions kiwixbuild/buildenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import platform
import distro

from .utils import pj, download_remote, Defaultdict
from .utils import pj, download_remote, escape_path
from ._global import neutralEnv, option


Expand Down Expand Up @@ -125,21 +125,34 @@ def get_env(self, *, cross_comp_flags, cross_compilers, cross_path):
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)
Expand Down
2 changes: 1 addition & 1 deletion kiwixbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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

Expand Down
2 changes: 1 addition & 1 deletion kiwixbuild/dependencies/libmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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):
Expand Down
19 changes: 16 additions & 3 deletions kiwixbuild/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import urllib.error
import ssl
import subprocess
import re
from collections import namedtuple, defaultdict

from kiwixbuild._global import neutralEnv, option
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'):
Expand Down

0 comments on commit 1ed7fc4

Please sign in to comment.