<commit_before>import os
import typing
import warnings

from msrest.authentication import Authentication, BasicAuthentication
from vsts.build.v4_1.build_client import BuildClient
from vsts.build.v4_1.models import (
    BuildDefinition,
    BuildDefinitionReference,
    SourceRepositories,
    SourceRepository,
)
from vsts.service_endpoint.v4_1.models import ServiceEndpoint
from vsts.service_endpoint.v4_1.service_endpoint_client import ServiceEndpointClient
from vsts.task_agent.v4_0.models import TaskAgentQueue
from vsts.task_agent.v4_0.task_agent_client import TaskAgentClient
from vsts.vss_connection import VssConnection


class AzureConfig:

    _default_org = "conda-forge"
    _default_project_name = "feedstock-builds"

    def __init__(self, org_or_user=None, project_name=None, team_instance=None):
        self.org_or_user = org_or_user or os.getenv(
            "AZURE_ORG_OR_USER", self._default_org
        )
        # This will only need to be changed if you need to point to a non-standard azure
        # instance.
        self.instance_base_url = team_instance or os.getenv(
            "AZURE_INSTANCE", f"https://dev.azure.com/{self.org_or_user}"
        )
        self.project_name = project_name or os.getenv(
            "AZURE_PROJECT_NAME", self._default_project_name
        )

        try:
            with open(os.path.expanduser("~/.conda-smithy/azure.token"), "r") as fh:
                self.token = fh.read().strip()
            if not self.token:
                raise ValueError()
        except (IOError, ValueError):
            self.token = None

        # By default for now don't report on the build information back to github
        self.azure_report_build_status = os.getenv("AZURE_REPORT_BUILD_STATUS", "true")

    @property
    def connection(self):
        connection = VssConnection(
            base_url=self.instance_base_url, creds=self.credentials
        )
        return connection

    @property
    def credentials(self):
        if self.token:
            return BasicAuthentication("", self.token)
        else:
            warnings.warn("No token available.  No modfications will be possible!")
            return Authentication()


default_config = AzureConfig()


def get_service_endpoint(config: AzureConfig = default_config):
    service_endpoint_client = ServiceEndpointClient(
        base_url=config.azure_team_instance, creds=config.credentials
    )
    endpoints: typing.List[
        ServiceEndpoint
    ] = service_endpoint_client.get_service_endpoints(
        project=config.project_name, type="GitHub"
    )
    for service_endpoint in endpoints:
        if service_endpoint.name == config.azure_org_or_user:
            return service_endpoint
    else:
        raise KeyError("Service endpoint not found")


def get_queues(config: AzureConfig = default_config) -> typing.List[TaskAgentQueue]:
    aclient = TaskAgentClient(config.azure_team_instance, config.credentials)
    return aclient.get_agent_queues(config.project_name)


def get_default_queue(project_name):
    queues = get_queues(project_name)
    for q in queues:
        if q.name == "Default":
            return q
    else:
        raise ValueError("Default queue not found")


def get_repo_reference(config: AzureConfig, github_org, repo_name):<commit_msg>Replace config.azure_team_instance with config.instance_base_url<commit_after>import os
import typing
import warnings

from msrest.authentication import Authentication, BasicAuthentication
from vsts.build.v4_1.build_client import BuildClient
from vsts.build.v4_1.models import (
    BuildDefinition,
    BuildDefinitionReference,
    SourceRepositories,
    SourceRepository,
)
from vsts.service_endpoint.v4_1.models import ServiceEndpoint
from vsts.service_endpoint.v4_1.service_endpoint_client import ServiceEndpointClient
from vsts.task_agent.v4_0.models import TaskAgentQueue
from vsts.task_agent.v4_0.task_agent_client import TaskAgentClient
from vsts.vss_connection import VssConnection


class AzureConfig:

    _default_org = "conda-forge"
    _default_project_name = "feedstock-builds"

    def __init__(self, org_or_user=None, project_name=None, team_instance=None):
        self.org_or_user = org_or_user or os.getenv(
            "AZURE_ORG_OR_USER", self._default_org
        )
        # This will only need to be changed if you need to point to a non-standard azure
        # instance.
        self.instance_base_url = team_instance or os.getenv(
            "AZURE_INSTANCE", f"https://dev.azure.com/{self.org_or_user}"
        )
        self.project_name = project_name or os.getenv(
            "AZURE_PROJECT_NAME", self._default_project_name
        )

        try:
            with open(os.path.expanduser("~/.conda-smithy/azure.token"), "r") as fh:
                self.token = fh.read().strip()
            if not self.token:
                raise ValueError()
        except (IOError, ValueError):
            self.token = None

        # By default for now don't report on the build information back to github
        self.azure_report_build_status = os.getenv("AZURE_REPORT_BUILD_STATUS", "true")

    @property
    def connection(self):
        connection = VssConnection(
            base_url=self.instance_base_url, creds=self.credentials
        )
        return connection

    @property
    def credentials(self):
        if self.token:
            return BasicAuthentication("", self.token)
        else:
            warnings.warn("No token available.  No modfications will be possible!")
            return Authentication()


default_config = AzureConfig()


def get_service_endpoint(config: AzureConfig = default_config):
    service_endpoint_client = ServiceEndpointClient(
        base_url=config.instance_base_url, creds=config.credentials
    )
    endpoints: typing.List[
        ServiceEndpoint
    ] = service_endpoint_client.get_service_endpoints(
        project=config.project_name, type="GitHub"
    )
    for service_endpoint in endpoints:
        if service_endpoint.name == config.azure_org_or_user:
            return service_endpoint
    else:
        raise KeyError("Service endpoint not found")


def get_queues(config: AzureConfig = default_config) -> typing.List[TaskAgentQueue]:
    aclient = TaskAgentClient(config.instance_base_url, config.credentials)
    return aclient.get_agent_queues(config.project_name)


def get_default_queue(project_name):
    queues = get_queues(project_name)
    for q in queues:
        if q.name == "Default":
            return q
    else:
        raise ValueError("Default queue not found")


def get_repo_reference(config: AzureConfig, github_org, repo_name):
{'fraction_non_alphanumeric': 0.07758231844835363, 'fraction_numerical': 0.0036084799278304014, 'mean_word_length': 3.1575, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3842254', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1913, 'n_words': 432}