<commit_before>import sys

import collections
import json
import logging
import os
import re

import jsonschema
import semantic_version
import yaml

from pkg_resources import Requirement, resource_filename

from jinja2 import Environment, FileSystemLoader

from .utils import RichStatus, SourcedDict
from .mapping import Mapping

from scout import Scout

from .VERSION import Version

def get_semver(what, version_string):
    semver = None

    try:
        semver = semantic_version.Version(version_string)
    except ValueError:
        pass

    return semver

class Config (object):
    # Weird stuff. The build version looks like
    #
    # 0.12.0                    for a prod build, or
    # 0.12.1-b2.da5d895.DIRTY   for a dev build (in this case made from a dirty true)
    #
    # Now: 
    # - Scout needs a build number (semver "+something") to flag a non-prod release;
    #   but
    # - DockerHub cannot use a build number at all; but
    # - 0.12.1-b2 comes _before_ 0.12.1+b2 in SemVer land.
    # 
    # FFS.
    #
    # We cope with this by transforming e.g.
    #
    # 0.12.1-b2.da5d895.DIRTY into 0.12.1-b2+da5d895.DIRTY
    #
    # for Scout.

    scout_version = Version

    if '-' in scout_version:
        # Dev build!
        v, p = scout_version.split('-')
        p, b = p.split('.', 1) if ('.' in p) else (0, p)

        scout_version = "%s-%s+%s" % (v, p, b)

    # Use scout_version here, not __version__, because the version
    # coming back from Scout will use build numbers for dev builds, but
    # __version__ won't, and we need to be consistent for comparison.
    current_semver = get_semver("current", scout_version)

    runtime = "kubernetes" if os.environ.get('KUBERNETES_SERVICE_HOST', None) else "docker"
    namespace = os.environ.get('AMBASSADOR_NAMESPACE', 'default')
    scout_install_id = os.environ.get('AMBASSADOR_SCOUT_ID', None)

    _scout_args = dict(
        app="ambassador", version=scout_version,
    )

    if scout_install_id:
        _scout_args['install_id'] = scout_install_id
    else:
        _scout_args['id_plugin'] = Scout.configmap_install_id_plugin
        _scout_args['id_plugin_args'] = { "namespace": namespace }

    try:
        scout = Scout(**_scout_args)
        scout_error = None
    except OSError as e:
        scout_error = e

    scout_latest_version = None
    scout_latest_semver = None
    scout_notices = []

    @classmethod
    def scout_report(klass, force_result=None, **kwargs):
        result = force_result

        if not result:
            if Config.scout:
                result = Config.scout.report(**kwargs)
            else:
                result = { "scout": "unavailable" }

        _notices = []

        if not Config.current_semver:
            _notices.append({
                "level": "warning",
                "message": "Ambassador has bad version '%s'??!" % Config.scout_version
            })

        # Do version & notices stuff.      
        if 'latest_version' in result:
            latest_version = result['latest_version']
            latest_semver = get_semver("latest", latest_version)

            if latest_semver:
                Config.scout_latest_version = latest_version
                Config.scout_latest_semver = latest_semver
            else:<commit_msg>Always include runtime in Scout reports. Sigh.<commit_after>import sys

import collections
import json
import logging
import os
import re

import jsonschema
import semantic_version
import yaml

from pkg_resources import Requirement, resource_filename

from jinja2 import Environment, FileSystemLoader

from .utils import RichStatus, SourcedDict
from .mapping import Mapping

from scout import Scout

from .VERSION import Version

def get_semver(what, version_string):
    semver = None

    try:
        semver = semantic_version.Version(version_string)
    except ValueError:
        pass

    return semver

class Config (object):
    # Weird stuff. The build version looks like
    #
    # 0.12.0                    for a prod build, or
    # 0.12.1-b2.da5d895.DIRTY   for a dev build (in this case made from a dirty true)
    #
    # Now: 
    # - Scout needs a build number (semver "+something") to flag a non-prod release;
    #   but
    # - DockerHub cannot use a build number at all; but
    # - 0.12.1-b2 comes _before_ 0.12.1+b2 in SemVer land.
    # 
    # FFS.
    #
    # We cope with this by transforming e.g.
    #
    # 0.12.1-b2.da5d895.DIRTY into 0.12.1-b2+da5d895.DIRTY
    #
    # for Scout.

    scout_version = Version

    if '-' in scout_version:
        # Dev build!
        v, p = scout_version.split('-')
        p, b = p.split('.', 1) if ('.' in p) else (0, p)

        scout_version = "%s-%s+%s" % (v, p, b)

    # Use scout_version here, not __version__, because the version
    # coming back from Scout will use build numbers for dev builds, but
    # __version__ won't, and we need to be consistent for comparison.
    current_semver = get_semver("current", scout_version)

    runtime = "kubernetes" if os.environ.get('KUBERNETES_SERVICE_HOST', None) else "docker"
    namespace = os.environ.get('AMBASSADOR_NAMESPACE', 'default')
    scout_install_id = os.environ.get('AMBASSADOR_SCOUT_ID', None)

    _scout_args = dict(
        app="ambassador", version=scout_version,
    )

    if scout_install_id:
        _scout_args['install_id'] = scout_install_id
    else:
        _scout_args['id_plugin'] = Scout.configmap_install_id_plugin
        _scout_args['id_plugin_args'] = { "namespace": namespace }

    try:
        scout = Scout(**_scout_args)
        scout_error = None
    except OSError as e:
        scout_error = e

    scout_latest_version = None
    scout_latest_semver = None
    scout_notices = []

    @classmethod
    def scout_report(klass, force_result=None, **kwargs):
        result = force_result

        if not result:
            if Config.scout:
                result = Config.scout.report(runtime=Config.runtime, **kwargs)
            else:
                result = { "scout": "unavailable" }

        _notices = []

        if not Config.current_semver:
            _notices.append({
                "level": "warning",
                "message": "Ambassador has bad version '%s'??!" % Config.scout_version
            })

        # Do version & notices stuff.      
        if 'latest_version' in result:
            latest_version = result['latest_version']
            latest_semver = get_semver("latest", latest_version)

            if latest_semver:
                Config.scout_latest_version = latest_version
                Config.scout_latest_semver = latest_semver
            else:
{'fraction_non_alphanumeric': 0.09151014274981217, 'fraction_numerical': 0.013223140495867768, 'mean_word_length': 2.3872773536895675, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '643931', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2055, 'n_words': 574}