from typing import List


def propagate_jemalloc_env_var(*, jemalloc_path: str, jemalloc_conf: str,
                               jemalloc_comps: List[str], process_type: str):
    """Read the jemalloc memory profiling related
        env var and return the dictionary that translates
        them to proper jemalloc related env vars.

        For example, if users specify `CLOUDTIK_JEMALLOC_LIB_PATH`,
        it is translated into `LD_PRELOAD` which is needed to
        run Jemalloc as a shared library.

        Params:
            jemalloc_path (str): The path to the jemalloc shared library.
            jemalloc_conf (str): `,` separated string of jemalloc config.
            jemalloc_comps List(str): The list of components
                that we will profile.
            process_type (str): The process type that needs jemalloc
                env var for memory profiling. If it doesn't match one of
                jemalloc_comps, the function will return an empty dict.

        Returns:
            dictionary of {env_var: value}
                that are needed to jemalloc profiling. The caller can
                call `dict.update(return_value_of_this_func)` to
                update the dict of env vars. If the process_type doesn't
                match jemalloc_comps, it will return an empty dict.
    """
    assert isinstance(jemalloc_comps, list)
    assert process_type is not None
    process_type = process_type.lower()
    if (not jemalloc_path or process_type not in jemalloc_comps):
        return {}

    env_vars = {
        "LD_PRELOAD": jemalloc_path,
    }
    if jemalloc_conf:
        env_vars.update({"MALLOC_CONF": jemalloc_conf})
    return env_vars