def get_environment()

in tsqa/environment.py [0:0]


    def get_environment(self, configure=None, env=None):
        '''
        Build (or return cached) environment with configure/env
        '''
        # set defaults, if none where passed in
        if configure is None:
            configure = self.default_configure
        else:
            configure = tsqa.utils.merge_dicts(self.default_configure, configure)
        if env is None:
            env = self.default_env
        else:
            env = tsqa.utils.merge_dicts(self.default_env, env)

        # TODO: global?
        # TODO: other things that can change the build...
        env_key = {}
        for whitelisted_key in ('PATH',):
            env_key[whitelisted_key] = env.get(whitelisted_key)

        key = self._get_key(configure, env_key)
        log.debug('Key is: %s, args are: %s %s' % (key, configure, env_key))

        # if we don't have it built already, lets build it
        if key not in self.environment_stash:
            if key in EnvironmentFactory.negative_cache:
                raise EnvironmentFactory.negative_cache[key]
            try:
                self.autoreconf()
                # if we have a build dir configured, lets use thatm otherwise
                # lets use a tmp one
                builddir = self.build_dir or tempfile.mkdtemp()

                kwargs = {
                    'cwd': builddir,
                    'env': env,
                    'stdout': subprocess.PIPE,
                    'stderr': subprocess.PIPE
                }

                if log.isEnabledFor(logging.DEBUG):
                    # if this is debug, lets not capture the output and let it go to
                    # stdout and stderr
                    kwargs['stdout'] = None
                    kwargs['stderr'] = None
                log.info('Starting build ({0}): configure {1}'.format(key, configure))

                # configure
                args = [os.path.join(self.source_dir, 'configure'), '--prefix=/'] + tsqa.utils.configure_list(configure)
                tsqa.utils.run_sync_command(args, **kwargs)

                # make
                tsqa.utils.run_sync_command(['make', '-j{0}'.format(multiprocessing.cpu_count())], **kwargs)
                installdir = tempfile.mkdtemp(dir=self.env_cache_dir)

                # make install
                tsqa.utils.run_sync_command(['make', 'install', 'DESTDIR={0}'.format(installdir)], **kwargs)

                # if we had to create a tmp dir, we should delete it
                if self.build_dir is None:
                    shutil.rmtree(builddir)
                # stash the env
                self.environment_stash[key] = {
                        'path': installdir,
                        'configuration': args,
                        'env': env_key,
                }
                log.info('Build completed ({0}): configure {1}'.format(key, configure))
            except Exception as e:
                EnvironmentFactory.negative_cache[key] = e
                raise

        # create a layout
        layout = Layout(self.environment_stash[key]['path'])

        # return an environment cloned from that layout
        ret = Environment()
        ret.clone(layout=layout)
        return ret