<commit_before>#
# Copyright 2016 Genymobile
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# If the GM_PR_INITIAL_PROJECTS environment variable is set, this
# script will delete the database of projects and repos, and populate
# it based on the value of the environment variable.

# example environment variable value:
# env GM_PR_INITIAL_PROJECTS="Material design repos=material-design-lite,material-design-icons;GCM repos=gcm,go-gcm"

from gm_pr import models
from django.contrib.auth.models import User
from django.db import IntegrityError
import os
import logging

logger = logging.getLogger('gm_pr')

try:
    User.objects.create_superuser(
        os.environ.get('GM_PR_ADMIN_LOGIN', 'admin'),
        os.environ.get('GM_PR_ADMIN_EMAIL', 'admin@localhost'),
        os.environ.get('GM_PR_ADMIN_PASSWORD', '<PASSWORD>'))
except IntegrityError as e:
    logger.warning("admin user already created")

projects_str = os.environ.get("GM_PR_INITIAL_PROJECTS")
if projects_str:
    # We have an environment variable value, delete existing
    # projects and repos from the DB
    models.Project.objects.all().delete()
    models.Repo.objects.all().delete()
    projects = dict(item.split("=") for item in projects_str.split(";"))
    for project_name in projects:
        project = models.Project()
        project.name=project_name
        project.save()
        repo_list_str = projects[project_name]
        repo_list = repo_list_str.split(",")
        for repo_name in repo_list:
            repo = models.Repo()
            repo.name = repo_name
            # We need to save the repo before we link it
            # to the project, or else we'll get an error
            # "...needs to have a value for field...before this many-to-many relationship can be used"
            repo.save()
            repo.projects = [project]
            repo.save()
<commit_msg>Fix fail in init_projects.py if a repo is part of multiple projects<commit_after>#
# Copyright 2016 Genymobile
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# If the GM_PR_INITIAL_PROJECTS environment variable is set, this
# script will delete the database of projects and repos, and populate
# it based on the value of the environment variable.

# example environment variable value:
# env GM_PR_INITIAL_PROJECTS="Material design repos=material-design-lite,material-design-icons;GCM repos=gcm,go-gcm"

from gm_pr import models
from django.contrib.auth.models import User
from django.db import IntegrityError
import os
import logging

logger = logging.getLogger('gm_pr')

try:
    User.objects.create_superuser(
        os.environ.get('GM_PR_ADMIN_LOGIN', 'admin'),
        os.environ.get('GM_PR_ADMIN_EMAIL', 'admin@localhost'),
        os.environ.get('GM_PR_ADMIN_PASSWORD', '<PASSWORD>'))
except IntegrityError as e:
    logger.warning("admin user already created")

projects_str = os.environ.get("GM_PR_INITIAL_PROJECTS")
if projects_str:
    # We have an environment variable value, delete existing
    # projects and repos from the DB
    models.Project.objects.all().delete()
    models.Repo.objects.all().delete()
    projects = dict(item.split("=") for item in projects_str.split(";"))
    for project_name in projects:
        project = models.Project()
        project.name=project_name
        project.save()
        repo_list_str = projects[project_name]
        repo_list = repo_list_str.split(",")
        for repo_name in repo_list:
            repo, _ = models.Repo.objects.get_or_create(name=repo_name)
            repo.projects.add(project)
            repo.save()
{'fraction_non_alphanumeric': 0.08901098901098901, 'fraction_numerical': 0.0035164835164835165, 'mean_word_length': 3.63914373088685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2812015', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1283, 'n_words': 445}