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

@ -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'):