'Create or get a tool_dependency record that is defined by the received package_name and package_version.
An env.sh file will be created for the tool_dependency in the received dependent_install_dir.'

def create_tool_dependency_with_initialized_env_sh_file(self, dependent_install_dir, tool_shed_repository, required_repository, package_name, package_version, tool_dependencies_config):

    tool_dependencies = []
    if (not os.path.exists(dependent_install_dir)):
        os.makedirs(dependent_install_dir)
    required_tool_dependency_env_file_path = None
    if tool_dependencies_config:
        (required_td_tree, error_message) = xml_util.parse_xml(tool_dependencies_config)
        if required_td_tree:
            required_td_root = required_td_tree.getroot()
            for required_td_elem in required_td_root:
                if (required_td_elem.tag == 'package'):
                    required_td_package_name = required_td_elem.get('name', None)
                    required_td_package_version = required_td_elem.get('version', None)
                    required_tool_dependency = tool_dependency_util.get_tool_dependency_by_name_version_type_repository(self.app, required_repository, required_td_package_name, required_td_package_version, 'package')
                    if ((required_td_package_name == package_name) and (required_td_package_version == package_version)):
                        tool_dependency = tool_dependency_util.create_or_update_tool_dependency(app=self.app, tool_shed_repository=tool_shed_repository, name=package_name, version=package_version, type='package', status=self.app.install_model.ToolDependency.installation_status.NEVER_INSTALLED, set_status=True)
                        required_tool_dependency_env_file_path = self.get_required_repository_package_env_sh_path(package_name, package_version, required_repository)
                        env_file_builder = EnvFileBuilder(tool_dependency.installation_directory(self.app))
                        env_file_builder.append_line(action='source', value=required_tool_dependency_env_file_path)
                        return_code = env_file_builder.return_code
                        if return_code:
                            error_message = ('Error    defining    env.sh    file    for    package    %s,    return_code:    %s' % (str(package_name), str(return_code)))
                            tool_dependency = tool_dependency_util.set_tool_dependency_attributes(self.app, tool_dependency=tool_dependency, status=self.app.install_model.ToolDependency.installation_status.ERROR, error_message=error_message)
                        elif ((required_tool_dependency is not None) and required_tool_dependency.in_error_state):
                            error_message = ("This    tool    dependency's    required    tool    dependency    %s    version    %s    has    status    %s." % (str(required_tool_dependency.name), str(required_tool_dependency.version), str(required_tool_dependency.status)))
                            tool_dependency = tool_dependency_util.set_tool_dependency_attributes(self.app, tool_dependency=tool_dependency, status=self.app.install_model.ToolDependency.installation_status.ERROR, error_message=error_message)
                        else:
                            tool_dependency = tool_dependency_util.set_tool_dependency_attributes(self.app, tool_dependency=tool_dependency, status=self.app.install_model.ToolDependency.installation_status.INSTALLED)
                        tool_dependencies.append(tool_dependency)
    return tool_dependencies
