'Create a ToolDependency to set an environment variable.  This is different from the process used to
set an environment variable that is associated with a package.  An example entry in a tool_dependencies.xml
file is::
<set_environment version="1.0">
<environment_variable name="R_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable>
</set_environment>
This method must also handle the sub-element tag::
<environment_variable name="R_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable>'

def set_environment(self, elem, tool_shed_repository, attr_tups_of_dependencies_for_install):

    env_manager = EnvManager(self.app)
    tool_dependencies = []
    env_var_version = elem.get('version', '1.0')
    tool_shed_repository_install_dir = os.path.abspath(tool_shed_repository.repo_files_directory(self.app))
    if (elem.tag == 'environment_variable'):
        elems = [elem]
    else:
        elems = [env_var_elem for env_var_elem in elem]
    for env_var_elem in elems:
        env_var_name = env_var_elem.get('name')
        if (not env_var_name):
            raise Exception('The    <environment_variable>    tag    must    have    a    name    attribute')
        env_var_action = env_var_elem.get('action')
        if (not env_var_action):
            raise Exception('The    <environment_variable>    tag    must    have    an    action    attribute')
        attr_tup = (env_var_name, None, 'set_environment')
        if (attr_tup in attr_tups_of_dependencies_for_install):
            install_dir = tool_dependency_util.get_tool_dependency_install_dir(app=self.app, repository_name=tool_shed_repository.name, repository_owner=tool_shed_repository.owner, repository_changeset_revision=tool_shed_repository.installed_changeset_revision, tool_dependency_type='set_environment', tool_dependency_name=env_var_name, tool_dependency_version=None)
            install_environment = InstallEnvironment(app=self.app, tool_shed_repository_install_dir=tool_shed_repository_install_dir, install_dir=install_dir)
            env_var_dict = env_manager.create_env_var_dict(elem=env_var_elem, install_environment=install_environment)
            if (not os.path.exists(install_dir)):
                os.makedirs(install_dir)
            status = self.app.install_model.ToolDependency.installation_status.INSTALLING
            tool_dependency = tool_dependency_util.create_or_update_tool_dependency(app=self.app, tool_shed_repository=tool_shed_repository, name=env_var_name, version=None, type='set_environment', status=status, set_status=True)
            if (env_var_version == '1.0'):
                env_file_builder = EnvFileBuilder(install_dir)
                return_code = env_file_builder.append_line(make_executable=True, **env_var_dict)
                if return_code:
                    error_message = ('Error    creating    env.sh    file    for    tool    dependency    %s,    return_code:    %s' % (str(tool_dependency.name), str(return_code)))
                    log.debug(error_message)
                    status = self.app.install_model.ToolDependency.installation_status.ERROR
                    tool_dependency = tool_dependency_util.set_tool_dependency_attributes(self.app, tool_dependency=tool_dependency, status=status, error_message=error_message)
                elif (tool_dependency.status not in [self.app.install_model.ToolDependency.installation_status.ERROR, self.app.install_model.ToolDependency.installation_status.INSTALLED]):
                    status = self.app.install_model.ToolDependency.installation_status.INSTALLED
                    tool_dependency = tool_dependency_util.set_tool_dependency_attributes(self.app, tool_dependency=tool_dependency, status=status)
                    log.debug(('Environment    variable    %s    set    in    %s    for    tool    dependency    %s.' % (str(env_var_name), str(install_dir), str(tool_dependency.name))))
            else:
                error_message = 'Only    set_environment    version    1.0    is    currently    supported    (i.e.,    change    your    tag    to    be    <set_environment    version="1.0">).'
                status = self.app.install_model.ToolDependency.installation_status.ERROR
                tool_dependency = tool_dependency_util.set_tool_dependency_attributes(self.app, tool_dependency=tool_dependency, status=status, error_message=error_message)
            tool_dependencies.append(tool_dependency)
    return tool_dependencies
