<commit_before>#
#   Copyright 2012 Cisco Systems, Inc.
#
#   Author: <NAME> <<EMAIL>>
#
#   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.
#
import datetime
import glob
import logging
import os
import os.path
import random
import paramiko
import select
import shutil
import StringIO
import socket
import sys
import tempfile
import termios
import textwrap
import time
import tty

from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import models
from django.template.loader import render_to_string
from django.utils import timezone

if settings.TESTING:
    import mock
    client = mock.Mock()
else:
    from novaclient.v1_1 import client


from repomgmt import utils
from repomgmt.exceptions import CommandFailed

logger = logging.getLogger(__name__)


class Repository(models.Model):
    name = models.CharField(max_length=200, primary_key=True)
    signing_key_id = models.CharField(max_length=200)
    uploaders = models.ManyToManyField(User)
    contact = models.EmailField()

    class Meta:
        verbose_name_plural = "repositories"

    def __unicode__(self):
        return self.name

    def _reprepro(self, *args):
        arg_list = list(args)
        cmd = ['reprepro', '-b', self.reprepro_dir] + arg_list
        return utils.run_cmd(cmd)

    @property
    def signing_key(self):
        return GPGKey(self.signing_key_id)

    @property
    def reprepro_dir(self):
        return '%s/%s' % (settings.BASE_REPO_DIR, self.name)

    @property
    def reprepro_outdir(self):
        return '%s/%s' % (settings.BASE_PUBLIC_REPO_DIR, self.name)

    @property
    def reprepro_incomingdir(self):
        return '%s/%s' % (settings.BASE_INCOMING_DIR, self.name)

    def process_incoming(self):
        self._reprepro('processincoming', 'incoming')

    def not_closed_series(self):
        return self.series_set.exclude(state=Series.CLOSED)

    def build_nodes(self):
        return BuildNode.objects.filter(buildrecord__series__repository=self)

    def write_configuration(self):
        confdir = '%s/conf' % (self.reprepro_dir,)

        settings_module_name = os.environ['DJANGO_SETTINGS_MODULE']
        settings_module = __import__(settings_module_name)
        settings_module_dir = os.path.dirname(settings_module.__file__)
        basedir = os.path.normpath(os.path.join(settings_module_dir,
                                                os.pardir))

        for d in [settings.BASE_PUBLIC_REPO_DIR,
                  confdir, self.reprepro_incomingdir]:
            if not os.path.exists(d):
                os.makedirs(d)

        for f in ['distributions', 'incoming', 'options', 'pulls',
                  'uploaders', 'create-build-records.sh']:
            s = render_to_string('reprepro/%s.tmpl' % (f,),
                                 {'repository': self,
                                  'architectures': Architecture.objects.all(),
                                  'settings': settings,
                                  'basedir': basedir,
                                  'outdir': self.reprepro_outdir})
            path = '%s/%s' % (confdir, f)

            with open(path, 'w') as fp:
                fp.write(s)

            if path.endswith('.sh'):
                os.chmod(path, 0755)

        if self.series_set.count() > 0:
            self._reprepro('export')


class UploaderKey(models.Model):
    key_id = models.CharField(max_length=200, primary_key=True)
    uploader = models.ForeignKey(User)

    def save(self):
        utils.run_cmd(['gpg', '--recv-keys', self.key_id])
        super(UploaderKey, self).save()

    def __unicode__(self):
        return '%s (%s)' % (self.key_id, self.uploader)


class GPGKey(object):
    def __init__(self, key_id):
        self.key_id = key_id

    def _gpg_export(self, private=False):
        if private:
            arg = '--export-secret-key'
        else:
            arg = '--export'

        out = utils.run_cmd(['gpg', '-a', '--export-options',
                             'export-clean', arg, self.key_id])

        if 'nothing exported' in out:
            raise Exception('Key with ID %s not found' % self.key_id)<commit_msg>Sync repo conf on save()<commit_after>#
#   Copyright 2012 Cisco Systems, Inc.
#
#   Author: <NAME> <<EMAIL>>
#
#   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.
#
import datetime
import glob
import logging
import os
import os.path
import random
import paramiko
import select
import shutil
import StringIO
import socket
import sys
import tempfile
import termios
import textwrap
import time
import tty

from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import models
from django.template.loader import render_to_string
from django.utils import timezone

if settings.TESTING:
    import mock
    client = mock.Mock()
else:
    from novaclient.v1_1 import client


from repomgmt import utils
from repomgmt.exceptions import CommandFailed

logger = logging.getLogger(__name__)


class Repository(models.Model):
    name = models.CharField(max_length=200, primary_key=True)
    signing_key_id = models.CharField(max_length=200)
    uploaders = models.ManyToManyField(User)
    contact = models.EmailField()

    class Meta:
        verbose_name_plural = "repositories"

    def __unicode__(self):
        return self.name

    def _reprepro(self, *args):
        arg_list = list(args)
        cmd = ['reprepro', '-b', self.reprepro_dir] + arg_list
        return utils.run_cmd(cmd)

    @property
    def signing_key(self):
        return GPGKey(self.signing_key_id)

    @property
    def reprepro_dir(self):
        return '%s/%s' % (settings.BASE_REPO_DIR, self.name)

    @property
    def reprepro_outdir(self):
        return '%s/%s' % (settings.BASE_PUBLIC_REPO_DIR, self.name)

    @property
    def reprepro_incomingdir(self):
        return '%s/%s' % (settings.BASE_INCOMING_DIR, self.name)

    def process_incoming(self):
        self._reprepro('processincoming', 'incoming')

    def not_closed_series(self):
        return self.series_set.exclude(state=Series.CLOSED)

    def build_nodes(self):
        return BuildNode.objects.filter(buildrecord__series__repository=self)

    def write_configuration(self):
        confdir = '%s/conf' % (self.reprepro_dir,)

        settings_module_name = os.environ['DJANGO_SETTINGS_MODULE']
        settings_module = __import__(settings_module_name)
        settings_module_dir = os.path.dirname(settings_module.__file__)
        basedir = os.path.normpath(os.path.join(settings_module_dir,
                                                os.pardir))

        for d in [settings.BASE_PUBLIC_REPO_DIR,
                  confdir, self.reprepro_incomingdir]:
            if not os.path.exists(d):
                os.makedirs(d)

        for f in ['distributions', 'incoming', 'options', 'pulls',
                  'uploaders', 'create-build-records.sh']:
            s = render_to_string('reprepro/%s.tmpl' % (f,),
                                 {'repository': self,
                                  'architectures': Architecture.objects.all(),
                                  'settings': settings,
                                  'basedir': basedir,
                                  'outdir': self.reprepro_outdir})
            path = '%s/%s' % (confdir, f)

            with open(path, 'w') as fp:
                fp.write(s)

            if path.endswith('.sh'):
                os.chmod(path, 0755)

        if self.series_set.count() > 0:
            self._reprepro('export')

    def save(self, *args, **kwargs):
        self.write_configuration()
        return super(Repository, self).save(*args, **kwargs)


class UploaderKey(models.Model):
    key_id = models.CharField(max_length=200, primary_key=True)
    uploader = models.ForeignKey(User)

    def save(self):
        utils.run_cmd(['gpg', '--recv-keys', self.key_id])
        super(UploaderKey, self).save()

    def __unicode__(self):
        return '%s (%s)' % (self.key_id, self.uploader)


class GPGKey(object):
    def __init__(self, key_id):
        self.key_id = key_id

    def _gpg_export(self, private=False):
        if private:
            arg = '--export-secret-key'
        else:
            arg = '--export'

        out = utils.run_cmd(['gpg', '-a', '--export-options',
                             'export-clean', arg, self.key_id])

        if 'nothing exported' in out:
            raise Exception('Key with ID %s not found' % self.key_id)
{'fraction_non_alphanumeric': 0.10359187922956793, 'fraction_numerical': 0.004997397188964081, 'mean_word_length': 2.5122486288848265, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '722361', 'n_tokens_mistral': 2950, 'n_tokens_neox': 2837, 'n_words': 726}