import gzip
import os
import re
import subprocess
import shlex
import shutil
import sys
import threading
import whole_archive
_BAT_PREFIX = 'cmd /c call '
def _GzipThenDelete(src_path, dest_path):
with open(src_path, 'rb') as f_in, gzip.GzipFile(dest_path, 'wb', 1) as f_out:
shutil.copyfileobj(f_in, f_out)
os.unlink(src_path)
def CommandToRun(command):
if command[0].startswith(_BAT_PREFIX):
command = command[0].split(None, 3) + command[1:]
return command
def RunLinkWithOptionalMapFile(command, env=None, map_file=None):
tmp_map_path = None
if map_file and map_file.endswith('.gz'):
tmp_map_path = map_file + '.tmp'
command.append('-Wl,-Map,' + tmp_map_path)
elif map_file:
command.append('-Wl,-Map,' + map_file)
command = whole_archive.wrap_with_whole_archive(command)
result = subprocess.call(command, env=env)
if tmp_map_path and result == 0:
threading.Thread(
target=lambda: _GzipThenDelete(tmp_map_path, map_file)).start()
elif tmp_map_path and os.path.exists(tmp_map_path):
os.unlink(tmp_map_path)
return result
def CaptureCommandStderr(command, env=None):
child = subprocess.Popen(command, stderr=subprocess.PIPE, env=env)
_, stderr = child.communicate()
return child.returncode, stderr