<commit_before>"""Utility functions that are used by both views and celery tasks."""

from __future__ import absolute_import
import hashlib
import logging

from readthedocs.builds.constants import NON_REPOSITORY_VERSIONS
from readthedocs.builds.models import Version
from readthedocs.search.indexes import PageIndex, ProjectIndex, SectionIndex

log = logging.getLogger(__name__)


def sync_versions(project, versions, type):  # pylint: disable=redefined-builtin
    """Update the database with the current versions from the repository."""
    # Bookkeeping for keeping tag/branch identifies correct
    verbose_names = [v['verbose_name'] for v in versions]
    project.versions.filter(verbose_name__in=verbose_names).update(type=type)

    old_versions = {}
    old_version_values = project.versions.values('identifier', 'verbose_name')
    for version in old_version_values:
        old_versions[version['verbose_name']] = version['identifier']

    added = set()
    # Add new versions
    for version in versions:
        version_id = version['identifier']
        version_name = version['verbose_name']
        if version_name in list(old_versions.keys()):
            if version_id == old_versions[version_name]:
                # Version is correct
                continue
            else:
                # Update slug with new identifier
                Version.objects.filter(
                    project=project, verbose_name=version_name
                ).update(
                    identifier=version_id,
                    type=type,
                    machine=False,
                )
                log.info("(Sync Versions) Updated Version: [%s=%s] ",
                         version['verbose_name'], version['identifier'])
        else:
            # New Version
            created_version = Version.objects.create(
                project=project,<commit_msg>Do not update version type when sync versions<commit_after>"""Utility functions that are used by both views and celery tasks."""

from __future__ import absolute_import
import hashlib
import logging

from readthedocs.builds.constants import NON_REPOSITORY_VERSIONS
from readthedocs.builds.models import Version
from readthedocs.search.indexes import PageIndex, ProjectIndex, SectionIndex

log = logging.getLogger(__name__)


def sync_versions(project, versions, type):  # pylint: disable=redefined-builtin
    """Update the database with the current versions from the repository."""
    old_versions = {}
    old_version_values = project.versions.filter(type=type).values(
        'identifier', 'verbose_name')
    for version in old_version_values:
        old_versions[version['verbose_name']] = version['identifier']

    added = set()
    # Add new versions
    for version in versions:
        version_id = version['identifier']
        version_name = version['verbose_name']
        if version_name in list(old_versions.keys()):
            if version_id == old_versions[version_name]:
                # Version is correct
                continue
            else:
                # Update slug with new identifier
                Version.objects.filter(
                    project=project, verbose_name=version_name
                ).update(
                    identifier=version_id,
                    type=type,
                    machine=False,
                )
                log.info("(Sync Versions) Updated Version: [%s=%s] ",
                         version['verbose_name'], version['identifier'])
        else:
            # New Version
            created_version = Version.objects.create(
                project=project,
{'fraction_non_alphanumeric': 0.08259911894273128, 'fraction_numerical': 0.0, 'mean_word_length': 2.4468690702087286, 'pattern_counts': {'":': 0, '<': 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': '6060211', 'n_tokens_mistral': 922, 'n_tokens_neox': 878, 'n_words': 242}