Python os.environ.copy() Examples

The following are 17 code examples of os.environ.copy(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os.environ , or try the search function .
Example #1
Source File: probe_endtoend.py    From telepresence with Apache License 2.0 6 votes vote down vote up
def disconnect_telepresence(namespace):
    # Kill off sshd server process the SSH client is talking to, forcing
    # disconnection:
    env = environ.copy()
    # Don't want torsocks messing with kubectl:
    for name in ["LD_PRELOAD", "DYLD_INSERT_LIBRARIES"]:
        if name in env:
            del env[name]
    # We can't tell if this succeeded, sadly, since it kills ssh session used
    # by kubectl exec!
    command = [
        "kubectl", "exec", "--namespace=" + namespace,
        "--container=" + environ["TELEPRESENCE_CONTAINER"],
        environ["TELEPRESENCE_POD"], "--", "/bin/sh", "-c",
        r"kill $(ps xa | tail -n +2 | " + r"sed 's/ *\([0-9][0-9]*\).*/\1/')"
    ]
    print("Using kubectl to kill Telepresence support processes:")
    print("\t{}".format(" ".join(command)), flush=True)

    run(command, env=env)
    sleep(10)
    # The test expects 3, which is how telepresence exits when one of its
    # subprocesses dies. That is, we expect to be killed before we reach this
    # point, if we exit with 66 that means disconnect-detection failed.
    raise SystemExit(66) 
Example #2
Source File: piomisc.py    From web2board with GNU Lesser General Public License v3.0 5 votes vote down vote up
def GetCompilerType(env):
    try:
        sysenv = environ.copy()
        sysenv['PATH'] = str(env['ENV']['PATH'])
        result = exec_command([env.subst("$CC"), "-v"], env=sysenv)
    except OSError:
        return None
    if result['returncode'] != 0:
        return None
    output = "".join([result['out'], result['err']]).lower()
    for type_ in ("clang", "gcc"):
        if type_ in output:
            return type_
    return None 
Example #3
Source File: config.py    From cookiecutter-python-app with MIT License 5 votes vote down vote up
def __init__(self, params=None):
        """ Initialize this object.

        :param params: key-value replacement mapping
        """
        self._params = environ.copy()
        try:
            self._params.update(params)
        except TypeError:
            pass  # params is None
        return 
Example #4
Source File: test_bootresources.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def patch_and_capture_env_for_download_all_boot_resources(self):
        class CaptureEnv:
            """Fake function; records a copy of the environment."""

            def __call__(self, *args, **kwargs):
                self.args = args
                self.env = environ.copy()

        capture = self.patch(
            bootresources, "download_all_boot_resources", CaptureEnv()
        )
        return capture 
Example #5
Source File: test_bootsources.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def patch_and_capture_env_for_download_all_image_descriptions(testcase):
    class CaptureEnv:
        """Fake function; records a copy of the environment."""

        def __call__(self, *args, **kwargs):
            self.args = args
            self.env = environ.copy()
            return MagicMock()

    capture = testcase.patch(
        bootsources, "download_all_image_descriptions", CaptureEnv()
    )
    return capture 
Example #6
Source File: __init__.py    From venv-update with MIT License 5 votes vote down vote up
def run(*cmd, **env):
    if env:
        from os import environ
        tmp = env
        env = environ.copy()
        env.update(tmp)
    else:
        env = None

    from .capture_subprocess import capture_subprocess
    from venv_update import info, colorize
    info('\nTEST> ' + colorize(cmd))
    out, err = capture_subprocess(cmd, env=env)
    err = strip_coverage_warnings(err)
    return out, err 
Example #7
Source File: simple_test.py    From venv-update with MIT License 5 votes vote down vote up
def pipe_output(read, write):
    from os import environ
    environ = environ.copy()

    from subprocess import Popen
    vupdate = Popen(
        ('venv-update', 'venv=', '--version'),
        env=environ,
        stdout=write,
        close_fds=True,
    )

    from os import close
    from testing.capture_subprocess import read_all
    close(write)
    result = read_all(read)
    vupdate.wait()

    result = result.decode('US-ASCII')
    print(result)
    uncolored = uncolor(result)
    assert uncolored.startswith('> ')
    # FIXME: Sometimes this is 'python -m', sometimes 'python2.7 -m'. Weird.
    import virtualenv
    assert uncolored.endswith('''
> virtualenv --version
%s
''' % virtualenv.__version__)

    return result, uncolored 
Example #8
Source File: build.py    From nuclio-jupyter with Apache License 2.0 5 votes vote down vote up
def build_notebook(nb_file, no_embed=False, tag=""):
    env = environ.copy()  # Pass argument to exporter via environment
    yaml_path = mktemp('.yaml')
    py_path = ''
    code = ''
    if no_embed:
        py_path = mktemp('.py')
        env[env_keys.code_target_path] = py_path
    env[env_keys.drop_nb_outputs] = 'y'

    cmd = [
        executable, '-m', 'nbconvert',
        '--to', 'nuclio.export.NuclioExporter',
        '--output', yaml_path,
        nb_file,
    ]
    out = run(cmd, env=env, stdout=PIPE, stderr=PIPE)
    if out.returncode != 0:
        print(out.stdout.decode('utf-8'))
        print(out.stderr.decode('utf-8'), file=stderr)
        raise BuildError('cannot convert notebook')

    if not path.isfile(yaml_path):
        raise BuildError('failed to convert, tmp file %s not found', yaml_path)

    with open(yaml_path) as yp:
        config_data = yp.read()
    config = yaml.safe_load(config_data)
    os.remove(yaml_path)

    if py_path:
        with open(py_path) as pp:
            code = pp.read()
        os.remove(py_path)

    return config, code 
Example #9
Source File: Fuzzer.py    From macke with Apache License 2.0 5 votes vote down vote up
def execute_afl_tmin(self, cgroup, inputfile, outputfile, functionname):
        asan_env = environ.copy()
        asan_env["AFL_USE_ASAN"] = "1"
        return cgroups_run_checked_silent_subprocess([AFLTMIN, "-e", "-t", "50", "-i", inputfile, "-o", outputfile, "-m", "none", "--", self.minimizer, "--fuzz-driver=" + functionname], cgroup, env=asan_env) 
Example #10
Source File: Fuzzer.py    From macke with Apache License 2.0 5 votes vote down vote up
def execute_reproducer(self, cgroup, inputfile, functionname):
        menv = environ.copy()
        menv["ASAN_OPTIONS"] = "detect_leaks=0"
        infd = open(inputfile, "r")
        (returncode, stdoutdata, stderrdata) = cgroups_run_timed_subprocess(
            [self.reproducer, "--fuzz-driver=" + functionname], cgroup=cgroup, stdin=infd, env=menv)
        ret = AsanResult(stderrdata, inputfile, functionname)
        infd.close()
        return ret

    # Function to check for saturation of AFL 
Example #11
Source File: backend.py    From cwl-airflow with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        makedirs(DAGS_FOLDER, mode=0o0775, exist_ok=True)
        extend_env = {"AIRFLOW__CORE__LOGGING_LEVEL": "ERROR"}
        self.env = environ.copy()
        self.env.update(extend_env) 
Example #12
Source File: telemetry_batch_view.py    From telemetry-airflow with Mozilla Public License 2.0 5 votes vote down vote up
def call_exit_errors(command):
    print("+ {}".format(" ".join(command)))
    rc = call(command, env=environ.copy())
    if rc > 0:
       exit(rc) 
Example #13
Source File: SP1099_missing_jira_env_vars.py    From SnowAlert with Apache License 2.0 5 votes vote down vote up
def without_jira_vars():
    backup = environ.copy()
    del environ['JIRA_PROJECT']
    del environ['JIRA_USER']
    del environ['JIRA_PASSWORD']
    del environ['JIRA_URL']
    reload(jira)
    yield
    environ.update(backup)
    reload(jira) 
Example #14
Source File: filestreams.py    From openelevationservice with Apache License 2.0 5 votes vote down vote up
def raster2pgsql():
    """
    Imports SRTM v4.1 tiles to PostGIS.
    
    :raises subprocess.CalledProcessError: Raised when raster2pgsql throws an error.
    """
    
    pg_settings = SETTINGS['provider_parameters']
    
    # Copy all env variables and add PGPASSWORD
    env_current = environ.copy()
    env_current['PGPASSWORD'] = pg_settings['password']
    
    # Tried to import every raster individually by user-specified xyrange 
    # similar to download(), but raster2pgsql fuck it up somehow.. The PostGIS
    # raster will not be what one would expect. Instead doing a bulk import of all files.
    cmd_raster2pgsql = r"raster2pgsql -s 4326 -a -C -M -P -t 50x50 {filename} {table_name} | psql -q -h {host} -p {port} -U {user_name} -d {db_name}"
    # -s: raster SRID
    # -a: append to table (assumes it's been create with 'create()')
    # -C: apply all raster Constraints
    # -P: pad tiles to guarantee all tiles have the same width and height
    # -M: vacuum analyze after import
    # -t: specifies the pixel size of each row. Important to keep low for performance!
    
    cmd_raster2pgsql = cmd_raster2pgsql.format(**{'filename': path.join(TILES_DIR, '*.tif'),
                                                  **pg_settings})
    
    proc = subprocess.Popen(cmd_raster2pgsql,
                         stdout=subprocess.PIPE, 
                         stderr=subprocess.STDOUT,
                         shell=True,
                         env=env_current
                         )
    
#    for line in proc.stdout:
#        log.debug(line.decode())
#    proc.stdout.close()
    return_code = proc.wait()
    if return_code:
        raise subprocess.CalledProcessError(return_code, cmd_raster2pgsql) 
Example #15
Source File: ptr.py    From ptr with MIT License 5 votes vote down vote up
def _set_build_env(build_base_path: Optional[Path]) -> Dict[str, str]:
    build_environ = environ.copy()

    if not build_base_path or not build_base_path.exists():
        if build_base_path:
            LOG.error(
                f"Configured local build env path {build_base_path} does not exist"
            )
        return build_environ

    if build_base_path.exists():
        build_env_vars = [
            (
                ("PATH", build_base_path / "Scripts")
                if WINDOWS
                else ("PATH", build_base_path / "sbin")
            ),
            ("C_INCLUDE_PATH", build_base_path / "include"),
            ("CPLUS_INCLUDE_PATH", build_base_path / "include"),
        ]
        if not WINDOWS:
            build_env_vars.append(("PATH", build_base_path / "bin"))

        for var_name, value in build_env_vars:
            if var_name in build_environ:
                build_environ[var_name] = f"{value}:{build_environ[var_name]}"
            else:
                build_environ[var_name] = str(value)
    else:
        LOG.error(
            "{} does not exist. Not add int PATH + INCLUDE Env variables".format(
                build_base_path
            )
        )

    return build_environ 
Example #16
Source File: test_environment.py    From me-ica with GNU Lesser General Public License v2.1 5 votes vote down vote up
def setup_environment():
    """Setup test environment for some functions that are tested
    in this module. In particular this functions stores attributes
    and other things that we need to stub in some test functions.
    This needs to be done on a function level and not module level because
    each testfunction needs a pristine environment.
    """
    global GIVEN_ENV
    GIVEN_ENV['env'] = env.copy() 
Example #17
Source File: backend.py    From cwl-airflow with Apache License 2.0 4 votes vote down vote up
def wes_collect_attachments(self, run_id):
        tempdir = tempfile.mkdtemp(dir=get_folder(path.abspath(conf_get_default('cwl', 'tmp_folder', '/tmp'))), prefix="run_id_"+run_id+"_")
        logger.debug(f"""Save all attached files to {tempdir}""")        
        for k, ls in iterlists(connexion.request.files):
            logger.debug(f"""Process attachment parameter {k}""")
            if k == "workflow_attachment":
                for v in ls:
                    try:
                        logger.debug(f"""Process attached file {v}""")
                        sp = v.filename.split("/")
                        fn = []
                        for p in sp:
                            if p not in ("", ".", ".."):
                                fn.append(secure_filename(p))
                        dest = path.join(tempdir, *fn)
                        if not path.isdir(path.dirname(dest)):
                            makedirs(path.dirname(dest))
                        logger.debug(f"""Save {v.filename} to {dest}""")
                        v.save(dest)
                    except Exception as err:
                        raise ValueError(f"""Failed to process attached file {v}, {err}""")
        body = {}       
        for k, ls in iterlists(connexion.request.form):
            logger.debug(f"""Process form parameter {k}""")
            for v in ls:
                try:
                    if not v:
                        continue
                    if k == "workflow_params":
                        job_file = path.join(tempdir, "job.json")
                        with open(job_file, "w") as f:
                            json.dump(json.loads(v), f, indent=4)
                        logger.debug(f"""Save job file to {job_file}""")
                        loader = Loader(load.jobloaderctx.copy())
                        job_order_object, _ = loader.resolve_ref(job_file, checklinks=False)
                        body[k] = job_order_object
                    else:
                        body[k] = v
                except Exception as err:
                    raise ValueError(f"""Failed to process form parameter {k}, {v}, {err}""")

        if "workflow_params" not in body or "workflow_url" not in body:
            raise ValueError("Missing 'workflow_params' or 'workflow_url' in submission")
        
        body["workflow_url"] = path.join(tempdir, secure_filename(body["workflow_url"]))

        return tempdir, body