# Path: catkin_tools/common.py
# def parse_env_str(environ_str):
#     """Parse a quoted environment string generated by format_env_dict, or `typeset` builtin.
# 
#     environ_str must be encoded in utf-8
#     """
#     variables = environ_str.decode().rstrip('\x00').split('\x00')
#     environment = {}
#     for v in variables:
#         try:
#             key, value = v.split('=', 1)
#             environment[key] = value
#         except ValueError:
#             print('WARNING: Could not parse env string: `{}`'.format(v),
#                   file=sys.stderr)
#     return environment


import os
import subprocess
import sys
from hashlib import md5
from shlex import quote as cmd_quote
from .common import parse_env_str

    # Check to make sure result_space_path contains the appropriate setup file
    setup_file_path = os.path.join(result_space_path, 'env.sh')
    if not os.path.exists(setup_file_path):
        if quiet:
            return dict()
        raise IOError(
            "Cannot load environment from resultspace \"%s\" because the "
            "required setup file \"%s\" does not exist." % (result_space_path, setup_file_path)
        )

    # Construct a command list which sources the setup file and prints the env to stdout
    command = ' '.join([
        cmd_quote(setup_file_path),
        shell_path,
        '-c',
        '"env --null"',
    ])

    # Define some "skiplisted" environment variables which shouldn't be copied
    skiplisted_keys = ('_', 'PWD')
    env_dict = {}

    try:
        # Run the command synchronously to get the resultspace environment
        lines = subprocess.check_output(command, cwd=os.getcwd(), env=base_env, shell=True)

        # Extract the environment variables
        env_dict = {
            k: v

# Path: catkin_tools/common.py
# def parse_env_str(environ_str):
#     """Parse a quoted environment string generated by format_env_dict, or `typeset` builtin.
# 
#     environ_str must be encoded in utf-8
#     """
#     variables = environ_str.decode().rstrip('\x00').split('\x00')
#     environment = {}
#     for v in variables:
#         try:
#             key, value = v.split('=', 1)
#             environment[key] = value
#         except ValueError:
#             print('WARNING: Could not parse env string: `{}`'.format(v),
#                   file=sys.stderr)
#     return environment


# Path: catkin_tools/resultspace.py
import os
import subprocess
import sys
from hashlib import md5
from shlex import quote as cmd_quote
from .common import parse_env_str


    # Check to make sure result_space_path contains the appropriate setup file
    setup_file_path = os.path.join(result_space_path, 'env.sh')
    if not os.path.exists(setup_file_path):
        if quiet:
            return dict()
        raise IOError(
            "Cannot load environment from resultspace \"%s\" because the "
            "required setup file \"%s\" does not exist." % (result_space_path, setup_file_path)
        )

    # Construct a command list which sources the setup file and prints the env to stdout
    command = ' '.join([
        cmd_quote(setup_file_path),
        shell_path,
        '-c',
        '"env --null"',
    ])

    # Define some "skiplisted" environment variables which shouldn't be copied
    skiplisted_keys = ('_', 'PWD')
    env_dict = {}

    try:
        # Run the command synchronously to get the resultspace environment
        lines = subprocess.check_output(command, cwd=os.getcwd(), env=base_env, shell=True)

        # Extract the environment variables
        env_dict = {
            k: v

            for k, v in parse_env_str(lines).items()