def check_provide_contents(dirname):
        environ = dict()
        local_state_file = LocalStateFile.load_for_directory(dirname)
        local_state_file.set_service_run_state("myservice", dict(port=42))
        requirement = EnvVarRequirement(RequirementsRegistry(), env_var="FOO")
        status = requirement.check_status(environ, local_state_file, 'default',
                                          UserConfigOverrides())
        context = ProvideContext(environ=environ,
                                 local_state_file=local_state_file,
                                 default_env_spec_name='default',
                                 status=status,
                                 mode=PROVIDE_MODE_DEVELOPMENT,
                                 frontend=NullFrontend())

        def transform_it(state):
            assert 42 == state['port']
            state['port'] = 43
            state['foo'] = 'bar'
            return 1234

        result = context.transform_service_run_state("myservice", transform_it)
        assert 1234 == result
        assert dict(
            port=43,
            foo='bar') == local_state_file.get_service_run_state("myservice")
示例#2
0
 def check_provide_contents(dirname):
     environ = dict()
     local_state_file = LocalStateFile.load_for_directory(dirname)
     requirement = EnvVarRequirement(RequirementsRegistry(), env_var="FOO")
     status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides())
     context = ProvideContext(environ=environ,
                              local_state_file=local_state_file,
                              default_env_spec_name='default',
                              status=status,
                              mode=PROVIDE_MODE_DEVELOPMENT,
                              frontend=NullFrontend())
     with pytest.raises(IOError) as excinfo:
         context.ensure_service_directory("foo")
     assert "this is not EEXIST" in repr(excinfo.value)
示例#3
0
 def check_provide_contents(dirname):
     environ = dict(foo='bar')
     local_state_file = LocalStateFile.load_for_directory(dirname)
     requirement = EnvVarRequirement(RequirementsRegistry(), env_var="FOO")
     status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides())
     context = ProvideContext(environ=environ,
                              local_state_file=local_state_file,
                              default_env_spec_name='default',
                              status=status,
                              mode=PROVIDE_MODE_DEVELOPMENT,
                              frontend=NullFrontend())
     assert dict(foo='bar') == context.environ
     assert context.status is status
示例#4
0
    def check_env_var_provider(dirname):
        provider = EnvVarProvider()
        requirement = _load_env_var_requirement(dirname, "FOO")
        local_state_file = LocalStateFile.load_for_directory(dirname)
        status = requirement.check_status(dict(), local_state_file, 'default', UserConfigOverrides())
        context = ProvideContext(environ=dict(),
                                 local_state_file=local_state_file,
                                 default_env_spec_name='default',
                                 status=status,
                                 mode=PROVIDE_MODE_DEVELOPMENT,
                                 frontend=NullFrontend())

        provider.provide(requirement, context=context)
        assert 'FOO' not in context.environ
示例#5
0
    def check_provide_contents(dirname):
        environ = dict()
        local_state_file = LocalStateFile.load_for_directory(dirname)
        requirement = EnvVarRequirement(RequirementsRegistry(), env_var="FOO")
        status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides())
        context = ProvideContext(environ=environ,
                                 local_state_file=local_state_file,
                                 default_env_spec_name='default',
                                 status=status,
                                 mode=PROVIDE_MODE_DEVELOPMENT,
                                 frontend=NullFrontend())
        workpath = context.ensure_service_directory("foo")
        assert os.path.isdir(workpath)
        assert workpath.endswith("foo")
        parent = os.path.dirname(workpath)
        assert parent.endswith("services")
        parent = os.path.dirname(parent)
        assert parent == dirname

        # be sure we can create if it already exists
        workpath2 = context.ensure_service_directory("foo")
        assert os.path.isdir(workpath2)
        assert workpath == workpath2
示例#6
0
 def check_env_var_provider(dirname):
     provider = EnvVarProvider()
     requirement = _load_env_var_requirement(dirname, "FOO")
     local_state_file = LocalStateFile.load_for_directory(dirname)
     environ = dict(FOO='from_environ')
     status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides())
     assert dict(source='environ', value='from_environ') == status.analysis.config
     context = ProvideContext(environ=environ,
                              local_state_file=local_state_file,
                              default_env_spec_name='default',
                              status=status,
                              mode=PROVIDE_MODE_DEVELOPMENT,
                              frontend=NullFrontend())
     result = provider.provide(requirement, context=context)
     assert [] == result.errors
     assert 'FOO' in context.environ
     assert 'from_environ' == context.environ['FOO']
示例#7
0
 def check_env_var_provider(dirname):
     provider = EnvVarProvider()
     requirement = _load_env_var_requirement(dirname, "FOO_SECRET")
     assert requirement.encrypted
     assert dict(default='from_default') == requirement.options
     local_state_file = LocalStateFile.load_for_directory(dirname)
     environ = dict()
     status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides())
     context = ProvideContext(environ=environ,
                              local_state_file=local_state_file,
                              default_env_spec_name='default',
                              status=status,
                              mode=PROVIDE_MODE_DEVELOPMENT,
                              frontend=NullFrontend())
     result = provider.provide(requirement, context=context)
     assert [] == result.errors
     assert 'FOO_SECRET' in context.environ
     assert 'from_default' == context.environ['FOO_SECRET']
示例#8
0
 def check_env_var_provider(dirname):
     provider = EnvVarProvider()
     requirement = _load_env_var_requirement(dirname, "FOO_PASSWORD")
     assert requirement.encrypted
     local_state_file = LocalStateFile.load_for_directory(dirname)
     # set in environ to be sure we override it with local state
     environ = dict(FOO_PASSWORD='******')
     status = requirement.check_status(environ, local_state_file, 'default', UserConfigOverrides())
     assert dict(value="from_local_state", source="variables") == status.analysis.config
     context = ProvideContext(environ=environ,
                              local_state_file=local_state_file,
                              default_env_spec_name='default',
                              status=status,
                              mode=PROVIDE_MODE_DEVELOPMENT,
                              frontend=NullFrontend())
     result = provider.provide(requirement, context=context)
     assert [] == result.errors
     assert 'FOO_PASSWORD' in context.environ
     assert 'from_local_state' == context.environ['FOO_PASSWORD']
示例#9
0
    def provide_stage(stage):
        def get_missing_to_provide(status):
            return status.analysis.missing_env_vars_to_provide

        sorted = _sort_statuses(environ, local_state, statuses,
                                get_missing_to_provide)

        # we have to recheck all the statuses in case configuration happened
        rechecked = []
        for status in sorted:
            rechecked.append(
                status.recheck(environ, local_state, default_env_spec_name,
                               overrides))

        errors = []
        did_any_providing = False
        results_by_status = dict()

        for status in rechecked:
            if not _in_provide_whitelist(provide_whitelist,
                                         status.requirement):
                continue
            elif status.has_been_provided:
                continue
            else:
                did_any_providing = True
                context = ProvideContext(environ, local_state,
                                         default_env_spec_name, status, mode,
                                         project.frontend)
                result = status.provider.provide(status.requirement, context)
                errors.extend(result.errors)
                results_by_status[status] = result

        if did_any_providing:
            old = rechecked
            rechecked = []
            for status in old:
                rechecked.append(
                    status.recheck(
                        environ,
                        local_state,
                        default_env_spec_name,
                        overrides,
                        latest_provide_result=results_by_status.get(status)))

        failed = False
        for status in rechecked:
            if not status:
                errors.append(
                    "missing requirement to run this project: {requirement.description}"
                    .format(requirement=status.requirement))
                project.frontend.error(errors[-1])
                errors.append(
                    "  {why_not}".format(why_not=status.status_description))
                project.frontend.error(errors[-1])
                failed = True

        result_statuses = _refresh_status_list(all_statuses, rechecked)

        current_env_spec_name = None
        for status in result_statuses:
            if status.env_spec_name is not None:
                # we're expecting exactly one status to set this,
                # the status from the CondaEnvRequirement. Possibly
                # we sometimes do a prepare with no CondaEnvRequirement?
                # but doing one with two wouldn't make sense afaik.

                # TODO: Should we just remove this? (considering the case of bootstrap env)
                # assert current_env_spec_name is None
                current_env_spec_name = status.env_spec_name

        if failed:
            stage.set_result(
                PrepareFailure(statuses=result_statuses,
                               errors=errors,
                               environ=environ,
                               overrides=overrides,
                               env_spec_name=current_env_spec_name), rechecked)
            if keep_going_until_success:
                return _start_over(stage.statuses_after_execute, rechecked)
            else:
                return None
        else:
            if command is None:
                exec_info = None
            else:
                exec_info = command.exec_info_for_environment(
                    environ, extra_args=extra_command_args)
            stage.set_result(
                PrepareSuccess(statuses=result_statuses,
                               command_exec_info=exec_info,
                               environ=environ,
                               overrides=overrides,
                               env_spec_name=current_env_spec_name), rechecked)
            return None