<commit_before>from django.db import models
from django.utils.translation import ugettext_lazy as _

from common.validators import date_is_past, date_is_present_or_past
from geo.models import Country, Locality

GENRE_TYPE_MALE = 'M'
GENRE_TYPE_FEMALE = 'F'
GENRE_TYPES = (
    (GENRE_TYPE_MALE, _('Male')),
    (GENRE_TYPE_FEMALE, _('Female'))
)

class PhysicalAddress(models.Model):
    """
    Physical address are of high importance due to impositive regulations.
    """
    street_address = models.CharField(
        _('street address'),
        max_length=150,
        default="",
        blank=True
    )
    floor_number = models.CharField(
        _('floor number'),
        max_length=4,
        default="",
        blank=True
    )
    apartment_number = models.CharField(
        _('apartment number'),
        max_length=6,
        default="",
        blank=True
    )
    locality = models.ForeignKey(
        Locality,
        related_name='physical_addresses',
        related_query_name='physical_address',
        verbose_name=_('locality'),
        on_delete=models.PROTECT,
        blank=True,
        null=True
    )
    postal_code = models.CharField(
        _('postal code'),
        max_length=20,
        default="",
        blank=True
    )

    def __str__(self):
        return self.street_address

    class Meta:
        verbose_name = _('physical address')
        verbose_name_plural = _('physical addresses')
        default_permissions = ('view', 'add', 'change', 'delete')

class Company(models.Model):
    """
    The companies using HeimdalERP.
    """
    fantasy_name = models.CharField(
        _('fantasy name'),
        max_length=150,
        unique=False  # Temporary until DRF 3.4.0 is released
    )
    legal_name = models.CharField(
        _('legal name'),
        max_length=300,
        default="",
        blank=True
    )
    slogan = models.CharField(
        _('slogan'),
        max_length=200,
        default="",
        blank=True
    )
    initiated_activities = models.DateField(
        _('initiated activities'),
        blank=True,
        null=True,
        validators=[date_is_present_or_past]
    )

    def __str__(self):
        return self.fantasy_name

    class Meta:
        verbose_name = _('company')
        verbose_name_plural = _('companies')
        default_permissions = ('view', 'add', 'change', 'delete')

class PersonProfile(models.Model):
    """
    This is an abstract class where Employee and Client inherit their
    main attributes.
    """
    persons_company = models.ForeignKey(
        Company,
        verbose_name=_('company'),
        related_name='+',
        related_query_name='+',
        db_index=True,
        on_delete=models.PROTECT
    )
    birth_date = models.DateField(
        _('birth date'),
        blank=True,
        null=True,
        validators=[date_is_past]
    )
    born_in = models.ForeignKey(
        Country,
        verbose_name=_('born in'),
        related_name='+',
        related_query_name='+',
        on_delete=models.PROTECT,
        blank=True,
        null=True
    )
    phone_numbers = models.CharField(
        _('phone numbers'),
        max_length=300,
        default="",
        blank=True
    )
    extra_emails = models.CharField(
        _('extra email addresses'),
        max_length=300,
        default="",
        blank=True,
    )
    home_address = models.ForeignKey(
        PhysicalAddress,
        on_delete=models.CASCADE,
        verbose_name=_('home address'),
        related_name='+',
        related_query_name='+',
        db_index=False,
        blank=True,
        null=True
    )

    class Meta:
        abstract = True
<commit_msg>FIX persons/models.py since DRF 3.4.0 is out<commit_after>from django.db import models
from django.utils.translation import ugettext_lazy as _

from common.validators import date_is_past, date_is_present_or_past
from geo.models import Country, Locality

GENRE_TYPE_MALE = 'M'
GENRE_TYPE_FEMALE = 'F'
GENRE_TYPES = (
    (GENRE_TYPE_MALE, _('Male')),
    (GENRE_TYPE_FEMALE, _('Female'))
)

class PhysicalAddress(models.Model):
    """
    Physical address are of high importance due to impositive regulations.
    """
    street_address = models.CharField(
        _('street address'),
        max_length=150,
        default="",
        blank=True
    )
    floor_number = models.CharField(
        _('floor number'),
        max_length=4,
        default="",
        blank=True
    )
    apartment_number = models.CharField(
        _('apartment number'),
        max_length=6,
        default="",
        blank=True
    )
    locality = models.ForeignKey(
        Locality,
        related_name='physical_addresses',
        related_query_name='physical_address',
        verbose_name=_('locality'),
        on_delete=models.PROTECT,
        blank=True,
        null=True
    )
    postal_code = models.CharField(
        _('postal code'),
        max_length=20,
        default="",
        blank=True
    )

    def __str__(self):
        return self.street_address

    class Meta:
        verbose_name = _('physical address')
        verbose_name_plural = _('physical addresses')
        default_permissions = ('view', 'add', 'change', 'delete')

class Company(models.Model):
    """
    The companies using HeimdalERP.
    """
    fantasy_name = models.CharField(
        _('fantasy name'),
        max_length=150,
        unique=True
    )
    legal_name = models.CharField(
        _('legal name'),
        max_length=300,
        default="",
        blank=True
    )
    slogan = models.CharField(
        _('slogan'),
        max_length=200,
        default="",
        blank=True
    )
    initiated_activities = models.DateField(
        _('initiated activities'),
        blank=True,
        null=True,
        validators=[date_is_present_or_past]
    )

    def __str__(self):
        return self.fantasy_name

    class Meta:
        verbose_name = _('company')
        verbose_name_plural = _('companies')
        default_permissions = ('view', 'add', 'change', 'delete')

class PersonProfile(models.Model):
    """
    This is an abstract class where Employee and Client inherit their
    main attributes.
    """
    persons_company = models.ForeignKey(
        Company,
        verbose_name=_('company'),
        related_name='+',
        related_query_name='+',
        db_index=True,
        on_delete=models.PROTECT
    )
    birth_date = models.DateField(
        _('birth date'),
        blank=True,
        null=True,
        validators=[date_is_past]
    )
    born_in = models.ForeignKey(
        Country,
        verbose_name=_('born in'),
        related_name='+',
        related_query_name='+',
        on_delete=models.PROTECT,
        blank=True,
        null=True
    )
    phone_numbers = models.CharField(
        _('phone numbers'),
        max_length=300,
        default="",
        blank=True
    )
    extra_emails = models.CharField(
        _('extra email addresses'),
        max_length=300,
        default="",
        blank=True,
    )
    home_address = models.ForeignKey(
        PhysicalAddress,
        on_delete=models.CASCADE,
        verbose_name=_('home address'),
        related_name='+',
        related_query_name='+',
        db_index=False,
        blank=True,
        null=True
    )

    class Meta:
        abstract = True
<|endoftext|>


<commit_before># from django.shortcuts import render
from django.http import JsonResponse
from django.views.generic import ListView, CreateView, DetailView
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from django.db.models import Count
from braces.views import OrderableListMixin, AjaxResponseMixin
from .utils import get_signature_form
import swapper

Petition = swapper.load_model("petition", "Petition")
Signature = swapper.load_model("petition", "Signature")

class SignatureList(OrderableListMixin, AjaxResponseMixin, ListView):
    model = Signature
    orderable_columns = ("pk", "city")
    orderable_columns_default = "-pk"
    paginate_by = 50

    def get_petition_list(self):
        return (Petition.objects.filter(public=True).
                annotate(signature_count=Count('signature')).all())

    def get_context_data(self, **kwargs):
        context = super(SignatureList, self).get_context_data(**kwargs)
        if 'slug' in self.kwargs:
            context['petition'] = get_object_or_404(Petition.objects.filter(public=True),
                slug=self.kwargs['slug'])
        else:
            context['petition'] = get_object_or_404(Petition.objects.filter(public=True),
                main=True)
        context['count'] = Signature.objects.visible().count()
        context['petitions'] = self.get_petition_list()
        return context

    def get_queryset(self, *args, **kwargs):
        qs = super(SignatureList, self).get_queryset(*args, **kwargs)
        if 'slug' in self.kwargs:
            qs = qs.filter(petition__slug=self.kwargs['slug'])
        return qs.visible()

class SignatureApiList(ListView):
    model = Signature
    ajax_fields = ('pk', "first_name", "second_name", "city", "lat", "lng")

    def get_paginate_by(self, *args, **kwargs):
        if 'per_page' in self.request.GET and self.request.GET['per_page'].isdigit():
            return int(self.request.GET['per_page'])
        return 100

    def get_queryset(self, *args, **kwargs):
        qs = super(SignatureApiList, self).get_queryset(*args, **kwargs)
        if 'slug' in kwargs:
            qs = qs.filter(petition__slug=self.kwargs['slug'])
        return qs.visible().location_full()

    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        data = [dict((field, getattr(obj, field))
            for field in self.ajax_fields)
            for obj in self.object_list]
        return JsonResponse(data, safe=False)

class SignatureCreate(CreateView):
    model = Signature
    form_class = get_signature_form()

    def get_context_data(self, **kwargs):
        context = super(SignatureCreate, self).get_context_data(**kwargs)
        context['petition'] = (Petition.objects.annotate(signature_count=Count('signature')).
            get(main=True))
        return context

    def get_success_url(self):
        return reverse('petition:thank-you')

class PetitionDetail(DetailView):
    model = Petition

    def get_queryset(self):
        qs = super(PetitionDetail, self).get_queryset()
        return qs.filter(public=True).annotate(signature_count=Count('signature'))

class SignatureCreateDone(DetailView):
    template_name = 'petition/signature_thank_you.html'

    def get_object(self):
        return Petition.objects.filter(main=True).get()
<commit_msg>Update views.py<commit_after># from django.shortcuts import render
from django.http import JsonResponse
from django.views.generic import ListView, CreateView, DetailView
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from django.db.models import Count
from braces.views import OrderableListMixin, AjaxResponseMixin
from .utils import get_signature_form
import swapper

Petition = swapper.load_model("petition", "Petition")
Signature = swapper.load_model("petition", "Signature")

class SignatureList(OrderableListMixin, AjaxResponseMixin, ListView):
    model = Signature
    orderable_columns = ("pk", "city")
    orderable_columns_default = "-pk"
    paginate_by = 50

    def get_petition_list(self):
        return (Petition.objects.filter(public=True).
                annotate(signature_count=Count('signature')).all())

    def get_context_data(self, **kwargs):
        context = super(SignatureList, self).get_context_data(**kwargs)
        if 'slug' in self.kwargs:
            context['petition'] = get_object_or_404(Petition.objects.filter(public=True),
                                                    slug=self.kwargs['slug'])
        else:
            context['petition'] = get_object_or_404(Petition.objects.filter(public=True),
                                                    main=True)
        context['count'] = Signature.objects.visible().count()
        context['petitions'] = self.get_petition_list()
        return context

    def get_queryset(self, *args, **kwargs):
        qs = super(SignatureList, self).get_queryset(*args, **kwargs)
        if 'slug' in self.kwargs:
            qs = qs.filter(petition__slug=self.kwargs['slug'])
        return qs.visible()

class SignatureApiList(ListView):
    model = Signature
    ajax_fields = ('pk', "first_name", "second_name", "city", "lat", "lng")

    def get_paginate_by(self, *args, **kwargs):
        if 'per_page' in self.request.GET and self.request.GET['per_page'].isdigit():
            return int(self.request.GET['per_page'])
        return 100

    def get_queryset(self, *args, **kwargs):
        qs = super(SignatureApiList, self).get_queryset(*args, **kwargs)
        if 'slug' in kwargs:
            qs = qs.filter(petition__slug=self.kwargs['slug'])
        return qs.visible().location_full()

    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        data = [dict((field, getattr(obj, field))
                     for field in self.ajax_fields)
                for obj in self.object_list]
        return JsonResponse(data, safe=False)

class SignatureCreate(CreateView):
    model = Signature
    form_class = get_signature_form()

    def get_context_data(self, **kwargs):
        context = super(SignatureCreate, self).get_context_data(**kwargs)
        context['petition'] = (Petition.objects.annotate(signature_count=Count('signature')).
                               get(main=True))
        return context

    def get_success_url(self):
        return reverse('petition:thank-you')

class PetitionDetail(DetailView):
    model = Petition

    def get_queryset(self):
        qs = super(PetitionDetail, self).get_queryset()
        return qs.filter(public=True).annotate(signature_count=Count('signature'))

class SignatureCreateDone(DetailView):
    template_name = 'petition/signature_thank_you.html'

    def get_object(self):
        return Petition.objects.filter(main=True).get()
<|endoftext|>


<commit_before>'''
==============
pg_bawler.core
==============

Base classes for LISTEN / NOTIFY.  Postgresql documentation for
`LISTEN<https://www.postgresql.org/docs/current/static/sql-listen.html>`_ /
`NOTIFY<https://www.postgresql.org/docs/current/static/sql-notify.html>`_.
'''
import asyncio
import logging

import aiopg

LOGGER = logging.getLogger(name='pg_bawler.core')

def cache_async_def(func):
    cache_attr_name = '_cache_async_def_{func.__name__}'.format(func=func)
    async def _cache_method(self, *args, **kwargs):
        if not hasattr(self, cache_attr_name):
            setattr(self, cache_attr_name, await func(self, *args, **kwargs))
        return getattr(self, cache_attr_name)
    # simulate functools.update_wrapper
    _cache_method.__name__ = func.__name__
    _cache_method.__doc__ = func.__doc__
    _cache_method.__module__ = func.__module__
    # save cache_attr_name on function
    # so delattr(self, func.cache_attr_name) will clear the cache
    _cache_method.cache_attr_name = cache_attr_name
    return _cache_method

class BawlerBase:
    '''
    Base ``pg_bawler`` class with convenience methods around ``aiopg``.
    '''

    def __init__(self, *, connection_params, loop=None):
        self.connection_params = connection_params
        self._connection = None
        self.loop = asyncio.get_event_loop() if loop is None else loop

    @cache_async_def
    async def pg_pool(self):
        return await aiopg.create_pool(**self.connection_params)

    @cache_async_def
    async def pg_connection(self):
        return await (await self.pg_pool()).acquire()

class SenderMixin:

    NOTIFY_SEND_TPL = 'SELECT pg_notify(\'{channel}\', \'{payload}\')'

    def get_notify_statement(self, *, channel, payload):
        return self.NOTIFY_SEND_TPL.format(channel=channel, payload=payload)

    async def send(self, *, channel, payload):
        async with (await self.pg_connection()).cursor() as pg_cursor:
            await pg_cursor.execute(
                self.get_notify_statement(channel=channel, payload=payload))

class ListenerMixin:

    CHANNEL_REGISTRATION_TPL = 'LISTEN {channel}'
    listen_timeout = None
    _stopped = False

    def stop(self):
        # TODO: Politely close all opened connections.
        self._stopped = True

    @property
    def is_stopped(self):
        return self._stopped

    def _get_listen_statement(self, channel):
        return self.CHANNEL_REGISTRATION_TPL.format(channel=channel)

    async def register_channel(self, channel):
        async with (await self.pg_connection()).cursor() as cursor:
            await cursor.execute(self._get_listen_statement(channel))

    async def get_notification(self):
        try:
            notification = await asyncio.wait_for(
                (await self.pg_connection()).notifies.get(),
                self.listen_timeout)
        except asyncio.TimeoutError:
            LOGGER.debug(
                'Timed out. No notification for last %s seconds.',
                self.listen_timeout)
            LOGGER.debug(
                'Checking health of connection.')
            async with (await self.pg_connection()).cursor() as pg_cursor:
                await pg_cursor.execute('SELECT 1')
                is_healthy = await pg_cursor.fetchone() == (1, )
            if is_healthy:
                LOGGER.debug('Connection looks to be OK.')
            else:
                LOGGER.error('Failed postgres connection!')
                LOGGER.info('Dropping this connection and creating new one.')
                pg_conn = (await self.pg_connection())
                pg_conn.close()
                await (await self.pg_pool()).release(pg_conn)
                del self.pg_connection.cache_attr_name
            return None
        else:
            LOGGER.debug(
                'Received notification from channel %s: %s',
                notification.channel, notification.payload)
            return notification

    def register_handler(self, handler):
        '''
        Registers ``handler`` with this listener and returns handler's index in
        the list of handlers.
        '''
        if not hasattr(self, 'handlers'):
            self.handlers = [handler]
        else:
            self.handlers.append(handler)
        return len(self.handlers) - 1

    def unregister_handler(self, handler):
        '''
        Unregisters ``handler`` by removing it from handlers list.

        Returns ``True`` if handler was successfully removed, ``False``
        otherwise.
        '''
        # TODO: Should we raise exception here?
        #       Like [].remove, raises ValueError
        try:
            self.handlers.remove(handler)
        except AttributeError:
            LOGGER.debug('No handlers registered yet.')
            return False
        except ValueError:
            LOGGER.debug('Handler is not registered.')
            return False
        else:
            LOGGER.debug('Handler %s unregistered.')
            return True

    async def listen(self):
        while not self.is_stopped:
            notification = await self.get_notification()
            # TODO: If Queue.get() for notifications timed out,
            #       we may want to reset connection or just continue
            if notification:
                for handler in getattr(self, 'handlers', []):
                    self.loop.create_task(handler(notification))
<commit_msg>Properly clear cached connection attribute<commit_after>'''
==============
pg_bawler.core
==============

Base classes for LISTEN / NOTIFY.  Postgresql documentation for
`LISTEN<https://www.postgresql.org/docs/current/static/sql-listen.html>`_ /
`NOTIFY<https://www.postgresql.org/docs/current/static/sql-notify.html>`_.
'''
import asyncio
import logging

import aiopg

LOGGER = logging.getLogger(name='pg_bawler.core')

def cache_async_def(func):
    cache_attr_name = '_cache_async_def_{func.__name__}'.format(func=func)
    async def _cache_method(self, *args, **kwargs):
        if not hasattr(self, cache_attr_name):
            setattr(self, cache_attr_name, await func(self, *args, **kwargs))
        return getattr(self, cache_attr_name)
    # simulate functools.update_wrapper
    _cache_method.__name__ = func.__name__
    _cache_method.__doc__ = func.__doc__
    _cache_method.__module__ = func.__module__
    # save cache_attr_name on function
    # so delattr(self, func.cache_attr_name) will clear the cache
    _cache_method.cache_attr_name = cache_attr_name
    return _cache_method

class BawlerBase:
    '''
    Base ``pg_bawler`` class with convenience methods around ``aiopg``.
    '''

    def __init__(self, *, connection_params, loop=None):
        self.connection_params = connection_params
        self._connection = None
        self.loop = asyncio.get_event_loop() if loop is None else loop

    @cache_async_def
    async def pg_pool(self):
        return await aiopg.create_pool(**self.connection_params)

    @cache_async_def
    async def pg_connection(self):
        return await (await self.pg_pool()).acquire()

class SenderMixin:

    NOTIFY_SEND_TPL = 'SELECT pg_notify(\'{channel}\', \'{payload}\')'

    def get_notify_statement(self, *, channel, payload):
        return self.NOTIFY_SEND_TPL.format(channel=channel, payload=payload)

    async def send(self, *, channel, payload):
        async with (await self.pg_connection()).cursor() as pg_cursor:
            await pg_cursor.execute(
                self.get_notify_statement(channel=channel, payload=payload))

class ListenerMixin:

    CHANNEL_REGISTRATION_TPL = 'LISTEN {channel}'
    listen_timeout = None
    _stopped = False

    def stop(self):
        # TODO: Politely close all opened connections.
        self._stopped = True

    @property
    def is_stopped(self):
        return self._stopped

    def _get_listen_statement(self, channel):
        return self.CHANNEL_REGISTRATION_TPL.format(channel=channel)

    async def register_channel(self, channel):
        async with (await self.pg_connection()).cursor() as cursor:
            await cursor.execute(self._get_listen_statement(channel))

    async def get_notification(self):
        try:
            notification = await asyncio.wait_for(
                (await self.pg_connection()).notifies.get(),
                self.listen_timeout)
        except asyncio.TimeoutError:
            LOGGER.debug(
                'Timed out. No notification for last %s seconds.',
                self.listen_timeout)
            LOGGER.debug(
                'Checking health of connection.')
            async with (await self.pg_connection()).cursor() as pg_cursor:
                await pg_cursor.execute('SELECT 1')
                is_healthy = await pg_cursor.fetchone() == (1, )
            if is_healthy:
                LOGGER.debug('Connection looks to be OK.')
            else:
                LOGGER.error('Failed postgres connection!')
                LOGGER.info('Dropping this connection and creating new one.')
                pg_conn = (await self.pg_connection())
                pg_conn.close()
                await (await self.pg_pool()).release(pg_conn)
                # clear the cache (cache_async_def)
                delattr(self, self.pg_connection.cache_attr_name)
            return None
        else:
            LOGGER.debug(
                'Received notification from channel %s: %s',
                notification.channel, notification.payload)
            return notification

    def register_handler(self, handler):
        '''
        Registers ``handler`` with this listener and returns handler's index in
        the list of handlers.
        '''
        if not hasattr(self, 'handlers'):
            self.handlers = [handler]
        else:
            self.handlers.append(handler)
        return len(self.handlers) - 1

    def unregister_handler(self, handler):
        '''
        Unregisters ``handler`` by removing it from handlers list.

        Returns ``True`` if handler was successfully removed, ``False``
        otherwise.
        '''
        # TODO: Should we raise exception here?
        #       Like [].remove, raises ValueError
        try:
            self.handlers.remove(handler)
        except AttributeError:
            LOGGER.debug('No handlers registered yet.')
            return False
        except ValueError:
            LOGGER.debug('Handler is not registered.')
            return False
        else:
            LOGGER.debug('Handler %s unregistered.')
            return True

    async def listen(self):
        while not self.is_stopped:
            notification = await self.get_notification()
            # TODO: If Queue.get() for notifications timed out,
            #       we may want to reset connection or just continue
            if notification:
                for handler in getattr(self, 'handlers', []):
                    self.loop.create_task(handler(notification))
<|endoftext|>


<commit_before>__version__ = "3.3.1"
<commit_msg>Releasing version 3.4.0<commit_after>__version__ = "3.4.0"
<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-

import mock

from django.db import connection
from django.test import TestCase

from ..utils import almostequal, sql_extent, uniquify
from ..utils.postgresql import debug_pg_notices
from ..utils.import_celery import (create_tmp_destination,
                                   subclasses,
                                   )

from geotrek.common.parsers import Parser

class UtilsTest(TestCase):

    def test_almostequal(self):
        self.assertTrue(almostequal(0.001, 0.002))
        self.assertFalse(almostequal(0.001, 0.002, precision=3))
        self.assertFalse(almostequal(1, 2, precision=0))
        self.assertFalse(almostequal(-1, 1))
        self.assertFalse(almostequal(1, -1))

    def test_sqlextent(self):
        ext = sql_extent(
            "SELECT ST_Extent('LINESTRING(0 0, 10 10)'::geometry)")
        self.assertEqual((0.0, 0.0, 10.0, 10.0), ext)

    def test_uniquify(self):
        self.assertEqual([3, 2, 1], uniquify([3, 3, 2, 1, 3, 1, 2]))

    def test_postgresql_notices(self):
        def raisenotice():
            cursor = connection.cursor()
            cursor.execute("""
                CREATE OR REPLACE FUNCTION geotrek.raisenotice() RETURNS boolean AS $$
                BEGIN
                RAISE NOTICE 'hello'; RETURN FALSE;
                END; $$ LANGUAGE plpgsql;
                SELECT raisenotice();""")
        raisenotice = debug_pg_notices(raisenotice)
        with mock.patch('geotrek.common.utils.postgresql.logger') as fake_log:
            raisenotice()
            fake_log.debug.assert_called_with('hello')

    def test_subclasses(self):
        class_list = subclasses(Parser)
        for classname in (
                'TrekParser',
                'TourInSoftParser',
                'CityParser',
                'ExcelParser',
                'OpenSystemParser',
                'ShapeParser',
                'ApidaeParser',
                'TourismSystemParser'):
            self.assert_(classname not in class_list)

    def test_create_tmp_directory(self):
        self.assertTupleEqual(
            ('/tmp/geotrek/bombadil', '/tmp/geotrek/bombadil/bombadil'),
            create_tmp_destination('bombadil')
        )
<commit_msg>Fix test tmp<commit_after># -*- encoding: utf-8 -*-

import mock

from django.db import connection
from django.test import TestCase

from ..utils import almostequal, sql_extent, uniquify
from ..utils.postgresql import debug_pg_notices
from ..utils.import_celery import (create_tmp_destination,
                                   subclasses,
                                   )

from geotrek.common.parsers import Parser

class UtilsTest(TestCase):

    def test_almostequal(self):
        self.assertTrue(almostequal(0.001, 0.002))
        self.assertFalse(almostequal(0.001, 0.002, precision=3))
        self.assertFalse(almostequal(1, 2, precision=0))
        self.assertFalse(almostequal(-1, 1))
        self.assertFalse(almostequal(1, -1))

    def test_sqlextent(self):
        ext = sql_extent(
            "SELECT ST_Extent('LINESTRING(0 0, 10 10)'::geometry)")
        self.assertEqual((0.0, 0.0, 10.0, 10.0), ext)

    def test_uniquify(self):
        self.assertEqual([3, 2, 1], uniquify([3, 3, 2, 1, 3, 1, 2]))

    def test_postgresql_notices(self):
        def raisenotice():
            cursor = connection.cursor()
            cursor.execute("""
                CREATE OR REPLACE FUNCTION geotrek.raisenotice() RETURNS boolean AS $$
                BEGIN
                RAISE NOTICE 'hello'; RETURN FALSE;
                END; $$ LANGUAGE plpgsql;
                SELECT raisenotice();""")
        raisenotice = debug_pg_notices(raisenotice)
        with mock.patch('geotrek.common.utils.postgresql.logger') as fake_log:
            raisenotice()
            fake_log.debug.assert_called_with('hello')

    def test_subclasses(self):
        class_list = subclasses(Parser)
        for classname in (
                'TrekParser',
                'TourInSoftParser',
                'CityParser',
                'ExcelParser',
                'OpenSystemParser',
                'ShapeParser',
                'ApidaeParser',
                'TourismSystemParser'):
            self.assert_(classname not in class_list)

    def test_create_tmp_directory(self):
        self.assertTupleEqual(
            ('/app/src/var/tmp/bombadil', '/app/src/var/tmp/bombadil/bombadil'),
            create_tmp_destination('bombadil')
        )
<|endoftext|>


<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals

# NOTE: this file is imported by setup.py, so cannot have any dependencies or side-effect.
__version__ = '2.8.4'
<commit_msg>This is v3.0.0<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals

# NOTE: this file is imported by setup.py, so cannot have any dependencies or side-effect.
__version__ = '3.0.0'
<|endoftext|>


<commit_before>from datetime import datetime, timedelta
from gitmostwanted.app import db, celery, log
from gitmostwanted.models.repo import Repo, RepoStars, RepoMean
from sqlalchemy.sql import expression
from statistics import variance, mean
from types import GeneratorType

@celery.task()
def status_detect(num_days, num_segments):
    repos = Repo.query.filter(Repo.status == 'unknown')
    for repo in repos:
        result = db.session.query(RepoStars.day, RepoStars.stars)\
            .filter(RepoStars.repo_id == repo.id)\
            .order_by(expression.asc(RepoStars.day))\
            .limit(num_days)\
            .all()

        val = 0 if not result else repo_mean(
            result, num_days, num_segments, last_known_mean(repo.id)
        )

        repo.status = 'hopeless' if val < 1 else 'promising'

        db.session.merge(RepoMean(repo=repo, value=val, created_at=datetime.now()))
        db.session.commit()

@celery.task()
def status_refresh(num_days):
    repos = Repo.query\
        .filter(Repo.status.in_(('promising', 'hopeless')))\
        .filter(Repo.status_updated_at <= datetime.now() + timedelta(days=num_days * -1))
    for repo in repos:
        RepoStars.query.filter(RepoStars.repo_id == repo.id).delete()

        if repo.status != 'promising':
            repo.worth += -1
        else:
            repo.worth += 1
            log.info('The "worth" value for {0} has been increased by 1'.format(repo.full_name))

        repo.status = 'new' if repo.worth > -1 else 'deleted'

        db.session.commit()

def last_known_mean(repo_id: int, default: float = 0.0):
    last_mean = db.session.query(RepoMean.value) \
        .filter(RepoMean.repo_id == repo_id) \
        .order_by(expression.desc(RepoMean.created_at)) \
        .first()
    return default if not last_mean else last_mean.value

def repo_mean(lst: list, lst_size: int, num_segments: int, default_gap_val: float):
    return result_mean(
        result_split(list(result_normalize(lst, lst_size)), num_segments),
        default_gap_val
    )

def result_mean(chunks: GeneratorType, normalized_val: float):
    return mean([normalized_val if variance(chunk) >= 1000 else mean(chunk) for chunk in chunks])

def result_normalize(lst: list, lst_size: int):
    fst = sorted(lst)[0][0]
    lst = dict(lst)
    for i in range(lst_size):
        key = fst + i
        yield 0 if key not in lst else lst[key]

def result_split(lst: list, num_rows: int):
    num_segments = len(lst) // num_rows
    for i in range(num_segments):
        yield lst[(i * num_rows):((i + 1) * num_rows)]
<commit_msg>No data for some repositories, closes #218<commit_after>from datetime import datetime, timedelta
from gitmostwanted.app import db, celery, log
from gitmostwanted.models.repo import Repo, RepoStars, RepoMean
from sqlalchemy.sql import expression
from statistics import variance, mean
from types import GeneratorType

@celery.task()
def status_detect(num_days, num_segments):
    repos = Repo.query.filter(Repo.status == 'unknown')
    for repo in repos:
        result = db.session.query(RepoStars.day, RepoStars.stars)\
            .filter(RepoStars.repo_id == repo.id)\
            .order_by(expression.asc(RepoStars.day))\
            .limit(num_days)\
            .all()

        val = 0 if not result else repo_mean(
            result, num_days, num_segments, last_known_mean(repo.id)
        )

        repo.status = 'hopeless' if val < 1 else 'promising'

        db.session.merge(RepoMean(repo=repo, value=val, created_at=datetime.now()))
        db.session.commit()

@celery.task()
def status_refresh(num_days):
    repos = Repo.query\
        .filter(Repo.status.in_(('promising', 'hopeless')))\
        .filter(
            Repo.status_updated_at.is_(None) |
            (Repo.status_updated_at <= datetime.now() + timedelta(days=num_days * -1))
        )
    for repo in repos:
        RepoStars.query.filter(RepoStars.repo_id == repo.id).delete()

        if repo.status != 'promising':
            repo.worth += -1
        else:
            repo.worth += 1
            log.info('The "worth" value for {0} has been increased by 1'.format(repo.full_name))

        repo.status = 'new' if repo.worth > -1 else 'deleted'

        db.session.commit()

def last_known_mean(repo_id: int, default: float = 0.0):
    last_mean = db.session.query(RepoMean.value) \
        .filter(RepoMean.repo_id == repo_id) \
        .order_by(expression.desc(RepoMean.created_at)) \
        .first()
    return default if not last_mean else last_mean.value

def repo_mean(lst: list, lst_size: int, num_segments: int, default_gap_val: float):
    return result_mean(
        result_split(list(result_normalize(lst, lst_size)), num_segments),
        default_gap_val
    )

def result_mean(chunks: GeneratorType, normalized_val: float):
    return mean([normalized_val if variance(chunk) >= 1000 else mean(chunk) for chunk in chunks])

def result_normalize(lst: list, lst_size: int):
    fst = sorted(lst)[0][0]
    lst = dict(lst)
    for i in range(lst_size):
        key = fst + i
        yield 0 if key not in lst else lst[key]

def result_split(lst: list, num_rows: int):
    num_segments = len(lst) // num_rows
    for i in range(num_segments):
        yield lst[(i * num_rows):((i + 1) * num_rows)]
<|endoftext|>


<commit_before>"""
Tests for django test runner
"""
from __future__ import absolute_import

import StringIO
from optparse import make_option
import warnings

from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django.test import simple
from django.test.simple import DjangoTestSuiteRunner, get_tests
from django.test.testcases import connections_support_transactions
from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils import unittest
from django.utils.importlib import import_module

from ..admin_scripts.tests import AdminScriptTestCase

TEST_APP_OK = 'regressiontests.test_runner.valid_app.models'
TEST_APP_ERROR = 'regressiontests.test_runner.invalid_app.models'

class DjangoTestRunnerTests(unittest.TestCase):
    def setUp(self):
        self._warnings_state = get_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning,
                                module='django.test.simple')

    def tearDown(self):
        restore_warnings_state(self._warnings_state)

    def test_failfast(self):
        class MockTestOne(unittest.TestCase):
            def runTest(self):
                assert False
        class MockTestTwo(unittest.TestCase):
            def runTest(self):
                assert False

        suite = unittest.TestSuite([MockTestOne(), MockTestTwo()])
        mock_stream = StringIO.StringIO()
        dtr = simple.DjangoTestRunner(verbosity=0, failfast=False, stream=mock_stream)
        result = dtr.run(suite)
        self.assertEqual(2, result.testsRun)
        self.assertEqual(2, len(result.failures))

        dtr = simple.DjangoTestRunner(verbosity=0, failfast=True, stream=mock_stream)
        result = dtr.run(suite)
        self.assertEqual(1, result.testsRun)
        self.assertEqual(1, len(result.failures))

class DependencyOrderingTests(unittest.TestCase):

    def test_simple_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
            ('s3', ('s3_db', ['charlie'])),
        ]
        dependencies = {
            'alpha': ['charlie'],
            'bravo': ['charlie'],
        }

        ordered = simple.dependency_ordered(raw, dependencies=dependencies)
        ordered_sigs = [sig for sig,value in ordered]

        self.assertIn('s1', ordered_sigs)
        self.assertIn('s2', ordered_sigs)
        self.assertIn('s3', ordered_sigs)
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))

    def test_chained_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
            ('s3', ('s3_db', ['charlie'])),
        ]
        dependencies = {
            'alpha': ['bravo'],
            'bravo': ['charlie'],
        }

        ordered = simple.dependency_ordered(raw, dependencies=dependencies)
        ordered_sigs = [sig for sig,value in ordered]

        self.assertIn('s1', ordered_sigs)
        self.assertIn('s2', ordered_sigs)
        self.assertIn('s3', ordered_sigs)

        # Explicit dependencies
        self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))

        # Implied dependencies
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))

    def test_multiple_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
            ('s3', ('s3_db', ['charlie'])),
            ('s4', ('s4_db', ['delta'])),
        ]
        dependencies = {
            'alpha': ['bravo','delta'],
            'bravo': ['charlie'],
            'delta': ['charlie'],
        }

        ordered = simple.dependency_ordered(raw, dependencies=dependencies)
        ordered_sigs = [sig for sig,aliases in ordered]

        self.assertIn('s1', ordered_sigs)
        self.assertIn('s2', ordered_sigs)
        self.assertIn('s3', ordered_sigs)
        self.assertIn('s4', ordered_sigs)

        # Explicit dependencies
        self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s4'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s4'))

        # Implicit dependencies
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))

    def test_circular_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
        ]
        dependencies = {
            'bravo': ['alpha'],
            'alpha': ['bravo'],
        }

        self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies)

class MockTestRunner(object):
    invoked = False

    def __init__(self, *args, **kwargs):
        pass

    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        MockTestRunner.invoked = True

class ManageCommandTests(unittest.TestCase):

    def test_custom_test_runner(self):
        call_command('test', 'sites',
                     testrunner='regressiontests.test_runner.tests.MockTestRunner')
        self.assertTrue(MockTestRunner.invoked,
                        "The custom test runner has not been invoked")

class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner):
    option_list = (
        make_option('--option_a','-a', action='store', dest='option_a', default='1'),
        make_option('--option_b','-b', action='store', dest='option_b', default='2'),
        make_option('--option_c','-c', action='store', dest='option_c', default='3'),
    )

    def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs):
        super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive,
                                                      failfast=failfast)
        self.option_a = option_a
        self.option_b = option_b
        self.option_c = option_c

    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        print "%s:%s:%s" % (self.option_a, self.option_b, self.option_c)

class CustomTestRunnerOptionsTests(AdminScriptTestCase):

    def setUp(self):
        settings = {
            'TEST_RUNNER': '\'regressiontests.test_runner.tests.CustomOptionsTestRunner\'',
        }
        self.write_settings('settings.py', sdict=settings)

    def tearDown(self):
        self.remove_settings('settings.py')

    def test_default_options(self):
        args = ['test', '--settings=regressiontests.settings']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, '1:2:3')

    def test_default_and_given_options(self):
        args = ['test', '--settings=regressiontests.settings', '--option_b=foo']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, '1:foo:3')

    def test_option_name_and_value_separated(self):
        args = ['test', '--settings=regressiontests.settings', '--option_b', 'foo']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, '1:foo:3')

    def test_all_options_given(self):
        args = ['test', '--settings=regressiontests.settings', '--option_a=bar', '--option_b=foo', '--option_c=31337']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, 'bar:foo:31337')

class Ticket16885RegressionTests(unittest.TestCase):
    def test_ticket_16885(self):
        """Features are also confirmed on mirrored databases."""
        from django import db
        old_db_connections = db.connections
        try:
            db.connections = db.ConnectionHandler({
                'default': {
                    'ENGINE': 'django.db.backends.sqlite3',
                },
                'slave': {
                    'ENGINE': 'django.db.backends.sqlite3',
                    'TEST_MIRROR': 'default',
                },
            })
            slave = db.connections['slave']
            self.assertEqual(slave.features.supports_transactions, None)
            DjangoTestSuiteRunner(verbosity=0).setup_databases()
            self.assertNotEqual(slave.features.supports_transactions, None)
        finally:
            db.connections = old_db_connections

class Ticket17477RegressionTests(AdminScriptTestCase):
    def setUp(self):
        self.write_settings('settings.py')

    def tearDown(self):
        self.remove_settings('settings.py')

    def test_ticket_17477(self):
        """'manage.py help test' works after r16352."""
        args = ['help', 'test']
        out, err = self.run_manage(args)
        self.assertNoOutput(err)

class ModulesTestsPackages(unittest.TestCase):
    def test_get_tests(self):
        "Check that the get_tests helper function can find tests in a directory"
        module = import_module(TEST_APP_OK)
        tests = get_tests(module)
        self.assertIsInstance(tests, type(module))

    def test_import_error(self):
        "Test for #12658 - Tests with ImportError's shouldn't fail silently"
        module = import_module(TEST_APP_ERROR)
        self.assertRaises(ImportError, get_tests, module)

class Sqlite3InMemoryTestDbs(unittest.TestCase):
    def test_transaction_support(self):
        """Ticket #16329: sqlite3 in-memory test databases"""
        from django import db
        old_db_connections = db.connections
        for option in ('NAME', 'TEST_NAME'):
            try:
                db.connections = db.ConnectionHandler({
                    'default': {
                        'ENGINE': 'django.db.backends.sqlite3',
                        option: ':memory:',
                    },
                    'other': {
                        'ENGINE': 'django.db.backends.sqlite3',
                        option: ':memory:',
                    },
                })
                other = db.connections['other']
                self.assertEqual(other.features.supports_transactions, None)
                DjangoTestSuiteRunner(verbosity=0).setup_databases()
                # Transaction support should be properly initialised for the 'other' DB
                self.assertNotEqual(
                    other.features.supports_transactions,
                    None,
                    "DATABASES setting '%s' option set to sqlite3's ':memory:' value causes problems with transaction support detection." % option
                )
                # And all the DBs should report that they support transactions
                self.assertTrue(
                    connections_support_transactions(),
                    "DATABASES setting '%s' option set to sqlite3's ':memory:' value causes problems with transaction support detection." % option
                )
            finally:
                db.connections = old_db_connections
<commit_msg>Tweaked tests from r17702 to run only when using sqlite3 DB(s).<commit_after>"""
Tests for django test runner
"""
from __future__ import absolute_import

import StringIO
from optparse import make_option
import warnings

from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django import db
from django.test import simple
from django.test.simple import DjangoTestSuiteRunner, get_tests
from django.test.testcases import connections_support_transactions
from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils import unittest
from django.utils.importlib import import_module

from ..admin_scripts.tests import AdminScriptTestCase

TEST_APP_OK = 'regressiontests.test_runner.valid_app.models'
TEST_APP_ERROR = 'regressiontests.test_runner.invalid_app.models'

class DjangoTestRunnerTests(unittest.TestCase):
    def setUp(self):
        self._warnings_state = get_warnings_state()
        warnings.filterwarnings('ignore', category=DeprecationWarning,
                                module='django.test.simple')

    def tearDown(self):
        restore_warnings_state(self._warnings_state)

    def test_failfast(self):
        class MockTestOne(unittest.TestCase):
            def runTest(self):
                assert False
        class MockTestTwo(unittest.TestCase):
            def runTest(self):
                assert False

        suite = unittest.TestSuite([MockTestOne(), MockTestTwo()])
        mock_stream = StringIO.StringIO()
        dtr = simple.DjangoTestRunner(verbosity=0, failfast=False, stream=mock_stream)
        result = dtr.run(suite)
        self.assertEqual(2, result.testsRun)
        self.assertEqual(2, len(result.failures))

        dtr = simple.DjangoTestRunner(verbosity=0, failfast=True, stream=mock_stream)
        result = dtr.run(suite)
        self.assertEqual(1, result.testsRun)
        self.assertEqual(1, len(result.failures))

class DependencyOrderingTests(unittest.TestCase):

    def test_simple_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
            ('s3', ('s3_db', ['charlie'])),
        ]
        dependencies = {
            'alpha': ['charlie'],
            'bravo': ['charlie'],
        }

        ordered = simple.dependency_ordered(raw, dependencies=dependencies)
        ordered_sigs = [sig for sig,value in ordered]

        self.assertIn('s1', ordered_sigs)
        self.assertIn('s2', ordered_sigs)
        self.assertIn('s3', ordered_sigs)
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))

    def test_chained_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
            ('s3', ('s3_db', ['charlie'])),
        ]
        dependencies = {
            'alpha': ['bravo'],
            'bravo': ['charlie'],
        }

        ordered = simple.dependency_ordered(raw, dependencies=dependencies)
        ordered_sigs = [sig for sig,value in ordered]

        self.assertIn('s1', ordered_sigs)
        self.assertIn('s2', ordered_sigs)
        self.assertIn('s3', ordered_sigs)

        # Explicit dependencies
        self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))

        # Implied dependencies
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))

    def test_multiple_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
            ('s3', ('s3_db', ['charlie'])),
            ('s4', ('s4_db', ['delta'])),
        ]
        dependencies = {
            'alpha': ['bravo','delta'],
            'bravo': ['charlie'],
            'delta': ['charlie'],
        }

        ordered = simple.dependency_ordered(raw, dependencies=dependencies)
        ordered_sigs = [sig for sig,aliases in ordered]

        self.assertIn('s1', ordered_sigs)
        self.assertIn('s2', ordered_sigs)
        self.assertIn('s3', ordered_sigs)
        self.assertIn('s4', ordered_sigs)

        # Explicit dependencies
        self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s4'), ordered_sigs.index('s1'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s4'))

        # Implicit dependencies
        self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))

    def test_circular_dependencies(self):
        raw = [
            ('s1', ('s1_db', ['alpha'])),
            ('s2', ('s2_db', ['bravo'])),
        ]
        dependencies = {
            'bravo': ['alpha'],
            'alpha': ['bravo'],
        }

        self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies)

class MockTestRunner(object):
    invoked = False

    def __init__(self, *args, **kwargs):
        pass

    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        MockTestRunner.invoked = True

class ManageCommandTests(unittest.TestCase):

    def test_custom_test_runner(self):
        call_command('test', 'sites',
                     testrunner='regressiontests.test_runner.tests.MockTestRunner')
        self.assertTrue(MockTestRunner.invoked,
                        "The custom test runner has not been invoked")

class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner):
    option_list = (
        make_option('--option_a','-a', action='store', dest='option_a', default='1'),
        make_option('--option_b','-b', action='store', dest='option_b', default='2'),
        make_option('--option_c','-c', action='store', dest='option_c', default='3'),
    )

    def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs):
        super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive,
                                                      failfast=failfast)
        self.option_a = option_a
        self.option_b = option_b
        self.option_c = option_c

    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        print "%s:%s:%s" % (self.option_a, self.option_b, self.option_c)

class CustomTestRunnerOptionsTests(AdminScriptTestCase):

    def setUp(self):
        settings = {
            'TEST_RUNNER': '\'regressiontests.test_runner.tests.CustomOptionsTestRunner\'',
        }
        self.write_settings('settings.py', sdict=settings)

    def tearDown(self):
        self.remove_settings('settings.py')

    def test_default_options(self):
        args = ['test', '--settings=regressiontests.settings']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, '1:2:3')

    def test_default_and_given_options(self):
        args = ['test', '--settings=regressiontests.settings', '--option_b=foo']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, '1:foo:3')

    def test_option_name_and_value_separated(self):
        args = ['test', '--settings=regressiontests.settings', '--option_b', 'foo']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, '1:foo:3')

    def test_all_options_given(self):
        args = ['test', '--settings=regressiontests.settings', '--option_a=bar', '--option_b=foo', '--option_c=31337']
        out, err = self.run_django_admin(args)
        self.assertNoOutput(err)
        self.assertOutput(out, 'bar:foo:31337')

class Ticket16885RegressionTests(unittest.TestCase):
    def test_ticket_16885(self):
        """Features are also confirmed on mirrored databases."""
        old_db_connections = db.connections
        try:
            db.connections = db.ConnectionHandler({
                'default': {
                    'ENGINE': 'django.db.backends.sqlite3',
                },
                'slave': {
                    'ENGINE': 'django.db.backends.sqlite3',
                    'TEST_MIRROR': 'default',
                },
            })
            slave = db.connections['slave']
            self.assertEqual(slave.features.supports_transactions, None)
            DjangoTestSuiteRunner(verbosity=0).setup_databases()
            self.assertNotEqual(slave.features.supports_transactions, None)
        finally:
            db.connections = old_db_connections

class Ticket17477RegressionTests(AdminScriptTestCase):
    def setUp(self):
        self.write_settings('settings.py')

    def tearDown(self):
        self.remove_settings('settings.py')

    def test_ticket_17477(self):
        """'manage.py help test' works after r16352."""
        args = ['help', 'test']
        out, err = self.run_manage(args)
        self.assertNoOutput(err)

class ModulesTestsPackages(unittest.TestCase):
    def test_get_tests(self):
        "Check that the get_tests helper function can find tests in a directory"
        module = import_module(TEST_APP_OK)
        tests = get_tests(module)
        self.assertIsInstance(tests, type(module))

    def test_import_error(self):
        "Test for #12658 - Tests with ImportError's shouldn't fail silently"
        module = import_module(TEST_APP_ERROR)
        self.assertRaises(ImportError, get_tests, module)

class Sqlite3InMemoryTestDbs(unittest.TestCase):

    @unittest.skipUnless(all(db.connections[conn].vendor == 'sqlite' for conn in db.connections),
                         "This is a sqlite-specific issue")
    def test_transaction_support(self):
        """Ticket #16329: sqlite3 in-memory test databases"""
        old_db_connections = db.connections
        for option in ('NAME', 'TEST_NAME'):
            try:
                db.connections = db.ConnectionHandler({
                    'default': {
                        'ENGINE': 'django.db.backends.sqlite3',
                        option: ':memory:',
                    },
                    'other': {
                        'ENGINE': 'django.db.backends.sqlite3',
                        option: ':memory:',
                    },
                })
                other = db.connections['other']
                self.assertIsNone(other.features.supports_transactions)
                DjangoTestSuiteRunner(verbosity=0).setup_databases()
                msg = "DATABASES setting '%s' option set to sqlite3's ':memory:' value shouldn't interfere with transaction support detection." % option
                # Transaction support should be properly initialised for the 'other' DB
                self.assertIsNotNone(other.features.supports_transactions, msg)
                # And all the DBs should report that they support transactions
                self.assertTrue(connections_support_transactions(), msg)
            finally:
                db.connections = old_db_connections
<|endoftext|>


<commit_before>from unittest import TestCase
import mock

from documentmanager_case import DocumentManagerTestCase
from iepy.models import PreProcessSteps, InvalidPreprocessSteps
from factories import IEDocFactory
from timelapse import timekeeper

class TestDocumentsPreprocessMetadata(TestCase):

    def test_preprocess_steps(self):
        self.assertEqual(
            [p.name for p in PreProcessSteps],
            ['tokenization', 'segmentation', 'tagging', 'nerc'])

    def test_just_created_document_has_no_preprocess_done(self):
        doc = IEDocFactory()
        for step in PreProcessSteps:
            self.assertFalse(doc.was_preprocess_done(step))

    def test_get_preprocess_result_when_not_done_gives_nothing(self):
        doc = IEDocFactory()
        for step in PreProcessSteps:
            self.assertIsNone(doc.get_preprocess_result(step))

    def test_setting_tokenization_result_can_be_later_retrieved(self):
        doc = IEDocFactory()
        simple_tokens = doc.text.split()
        step = PreProcessSteps.tokenization
        doc.set_preprocess_result(step, simple_tokens)
        self.assertTrue(doc.was_preprocess_done(step))
        self.assertEqual(doc.get_preprocess_result(step), simple_tokens)

    def test_cannot_set_segmentation_if_not_tokenization_stored(self):
        doc = IEDocFactory(text='Some sentence.')
        segments = [0]
        step = PreProcessSteps.segmentation
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        self.assertFalse(doc.was_preprocess_done(step))

    def test_cannot_set_segmentation_larger_than_tokens(self):
        # segmentation numers must be valid indexes of tokens list
        doc = IEDocFactory(text='Some sentence.')
        doc.set_preprocess_result(PreProcessSteps.tokenization, doc.text.split())
        segments = [35]
        step = PreProcessSteps.segmentation
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        self.assertFalse(doc.was_preprocess_done(step))

    def test_segmentation_result_must_be_order_list_of_numbers(self):
        doc = IEDocFactory(text='Some sentence . And some other . Indeed!')
        segments = [7, 3, 0]
        step = PreProcessSteps.segmentation
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        # also must be strictly ascending
        segments = [0, 0, 3]
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        self.assertFalse(doc.was_preprocess_done(step))

    def test_setting_segmentation_result_can_be_later_retrieved(self):
        doc = IEDocFactory(text='Some sentence . And some other . Indeed!')
        doc.set_preprocess_result(PreProcessSteps.tokenization, doc.text.split())
        simple_segments = [0, 3, 7]
        step = PreProcessSteps.segmentation
        doc.set_preprocess_result(step, simple_segments)
        self.assertTrue(doc.was_preprocess_done(step))
        self.assertEqual(doc.get_preprocess_result(step), simple_segments)

    def test_setting_tagging_result_can_be_later_retrieved(self):
        doc = IEDocFactory(text='Some sentence. And some other. Indeed!')
        pathetic_tags = ['NN' for token in doc.text.split()]
        step = PreProcessSteps.tagging
        doc.set_preprocess_result(step, pathetic_tags)
        self.assertTrue(doc.was_preprocess_done(step))
        self.assertEqual(doc.get_preprocess_result(step), pathetic_tags)

class TestStorePreprocessOutousSideEffects(TestCase):
    # Just one step. We'll assume that for the others is the same
    step = PreProcessSteps.tokenization

    def test_if_step_is_not_preprocessstep_error_is_raised(self):
        doc1 = IEDocFactory(text='').save()
        self.assertRaises(InvalidPreprocessSteps,
                          doc1.set_preprocess_result, 'spell it', [])

    def test_doc_is_returned(self):
        doc1 = IEDocFactory(text='')
        r = doc1.set_preprocess_result(self.step, [])
        self.assertEqual(r, doc1)

    def test_save_is_not_called_inside_of_set_method(self):
        doc1 = IEDocFactory(text='')
        with mock.patch.object(doc1, 'save') as save:
            doc1.set_preprocess_result(self.step, [])
            self.assertFalse(save.called)

    def test_preprocess_metadata_is_created_after_call(self):
        doc1 = IEDocFactory(text='')
        self.assertNotIn(self.step.name, doc1.preprocess_metadata)
        doc1.set_preprocess_result(self.step, [])
        self.assertIn(self.step.name, doc1.preprocess_metadata)

    def test_preprocess_metadata_has_done_at(self):
        doc1 = IEDocFactory(text='')
        with timekeeper() as interval:
            doc1.set_preprocess_result(self.step, [])
        mdata = doc1.preprocess_metadata[self.step.name]
        interval.assertHasDate(mdata['done_at'])

class TestDocumentManagerFiltersForPreprocess(DocumentManagerTestCase):

    def test_raw_documents_are_filtered(self):
        doc1 = IEDocFactory(text='').save()
        doc2 = IEDocFactory(text='something').save()
        raws = self.manager.get_raw_documents()
        self.assertIn(doc1, raws)
        self.assertNotIn(doc2, raws)

    def test_untokenized_documents_are_filtered(self):
        doc1 = IEDocFactory(text='').save()
        doc2 = IEDocFactory(text='something').save()
        doc3 = IEDocFactory(text='something nice').save()
        step = PreProcessSteps.tokenization
        doc3.set_preprocess_result(step, doc3.text.split()).save()
        untokeneds = self.manager.get_documents_lacking_preprocess(step)
        self.assertIn(doc1, untokeneds)
        self.assertIn(doc2, untokeneds)
        self.assertNotIn(doc3, untokeneds)
<commit_msg>typo fixed<commit_after>from unittest import TestCase
import mock

from documentmanager_case import DocumentManagerTestCase
from iepy.models import PreProcessSteps, InvalidPreprocessSteps
from factories import IEDocFactory
from timelapse import timekeeper

class TestDocumentsPreprocessMetadata(TestCase):

    def test_preprocess_steps(self):
        self.assertEqual(
            [p.name for p in PreProcessSteps],
            ['tokenization', 'segmentation', 'tagging', 'nerc'])

    def test_just_created_document_has_no_preprocess_done(self):
        doc = IEDocFactory()
        for step in PreProcessSteps:
            self.assertFalse(doc.was_preprocess_done(step))

    def test_get_preprocess_result_when_not_done_gives_nothing(self):
        doc = IEDocFactory()
        for step in PreProcessSteps:
            self.assertIsNone(doc.get_preprocess_result(step))

    def test_setting_tokenization_result_can_be_later_retrieved(self):
        doc = IEDocFactory()
        simple_tokens = doc.text.split()
        step = PreProcessSteps.tokenization
        doc.set_preprocess_result(step, simple_tokens)
        self.assertTrue(doc.was_preprocess_done(step))
        self.assertEqual(doc.get_preprocess_result(step), simple_tokens)

    def test_cannot_set_segmentation_if_not_tokenization_stored(self):
        doc = IEDocFactory(text='Some sentence.')
        segments = [0]
        step = PreProcessSteps.segmentation
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        self.assertFalse(doc.was_preprocess_done(step))

    def test_cannot_set_segmentation_larger_than_tokens(self):
        # segmentation numers must be valid indexes of tokens list
        doc = IEDocFactory(text='Some sentence.')
        doc.set_preprocess_result(PreProcessSteps.tokenization, doc.text.split())
        segments = [35]
        step = PreProcessSteps.segmentation
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        self.assertFalse(doc.was_preprocess_done(step))

    def test_segmentation_result_must_be_order_list_of_numbers(self):
        doc = IEDocFactory(text='Some sentence . And some other . Indeed!')
        segments = [7, 3, 0]
        step = PreProcessSteps.segmentation
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        # also must be strictly ascending
        segments = [0, 0, 3]
        self.assertRaises(ValueError, doc.set_preprocess_result, step, segments)
        self.assertFalse(doc.was_preprocess_done(step))

    def test_setting_segmentation_result_can_be_later_retrieved(self):
        doc = IEDocFactory(text='Some sentence . And some other . Indeed!')
        doc.set_preprocess_result(PreProcessSteps.tokenization, doc.text.split())
        simple_segments = [0, 3, 7]
        step = PreProcessSteps.segmentation
        doc.set_preprocess_result(step, simple_segments)
        self.assertTrue(doc.was_preprocess_done(step))
        self.assertEqual(doc.get_preprocess_result(step), simple_segments)

    def test_setting_tagging_result_can_be_later_retrieved(self):
        doc = IEDocFactory(text='Some sentence. And some other. Indeed!')
        pathetic_tags = ['NN' for token in doc.text.split()]
        step = PreProcessSteps.tagging
        doc.set_preprocess_result(step, pathetic_tags)
        self.assertTrue(doc.was_preprocess_done(step))
        self.assertEqual(doc.get_preprocess_result(step), pathetic_tags)

class TestStorePreprocessOutputSideEffects(TestCase):
    # Just one step. We'll assume that for the others is the same
    step = PreProcessSteps.tokenization

    def test_if_step_is_not_preprocessstep_error_is_raised(self):
        doc1 = IEDocFactory(text='').save()
        self.assertRaises(InvalidPreprocessSteps,
                          doc1.set_preprocess_result, 'spell it', [])

    def test_doc_is_returned(self):
        doc1 = IEDocFactory(text='')
        r = doc1.set_preprocess_result(self.step, [])
        self.assertEqual(r, doc1)

    def test_save_is_not_called_inside_of_set_method(self):
        doc1 = IEDocFactory(text='')
        with mock.patch.object(doc1, 'save') as save:
            doc1.set_preprocess_result(self.step, [])
            self.assertFalse(save.called)

    def test_preprocess_metadata_is_created_after_call(self):
        doc1 = IEDocFactory(text='')
        self.assertNotIn(self.step.name, doc1.preprocess_metadata)
        doc1.set_preprocess_result(self.step, [])
        self.assertIn(self.step.name, doc1.preprocess_metadata)

    def test_preprocess_metadata_has_done_at(self):
        doc1 = IEDocFactory(text='')
        with timekeeper() as interval:
            doc1.set_preprocess_result(self.step, [])
        mdata = doc1.preprocess_metadata[self.step.name]
        interval.assertHasDate(mdata['done_at'])

class TestDocumentManagerFiltersForPreprocess(DocumentManagerTestCase):

    def test_raw_documents_are_filtered(self):
        doc1 = IEDocFactory(text='').save()
        doc2 = IEDocFactory(text='something').save()
        raws = self.manager.get_raw_documents()
        self.assertIn(doc1, raws)
        self.assertNotIn(doc2, raws)

    def test_untokenized_documents_are_filtered(self):
        doc1 = IEDocFactory(text='').save()
        doc2 = IEDocFactory(text='something').save()
        doc3 = IEDocFactory(text='something nice').save()
        step = PreProcessSteps.tokenization
        doc3.set_preprocess_result(step, doc3.text.split()).save()
        untokeneds = self.manager.get_documents_lacking_preprocess(step)
        self.assertIn(doc1, untokeneds)
        self.assertIn(doc2, untokeneds)
        self.assertNotIn(doc3, untokeneds)
<|endoftext|>


<commit_before>"""Does the rest of the setup."""
from __future__ import print_function

import os
import re
import subprocess
import sys

def main():
    """Does the rest of the setup."""

    def newer(file_name_1, file_name_2):
        """Returns true if file_1 is newer than file_2."""
        return os.stat(file_name_1).st_atime > os.stat(file_name_2).st_mtime

    def exists(file_name):
        """Returns true if file exists."""
        return os.access(file_name, os.F_OK)

    def grep(file_name, pattern):
        """Searches a file for a regex pattern."""
        regex = re.compile(pattern)
        with open(file_name) as file_:
            for line in file_:
                if regex.search(line):
                    return True
        return False

    section_test_command_tuples = (
        (
            'start up',
            (
                not exists('/etc/init.d/eclipse-2017-hab-rc')
                or newer('eclipse-2017-hab-rc', '/etc/init.d/eclipse-2017-hab-rc')
            ),
            (
                'sudo cp eclipse-2017-hab-rc /etc/init.d/',
                'sudo chmod +x /etc/init.d/eclipse-2017-hab-rc',
                'sudo update-rc.d eclipse-2017-hab-rc defaults',
            )
        ),
        (
            'virtualenv',
            not exists('/home/pi/.virtualenvs/eclipse-2017-hab'),
            (
                'bash setup-virtualenv.sh',
            )
        ),
        (
            'SSH banner',
            not exists('/etc/banner'),
            (
                'sudo cp banner /etc',
                'sudo bash add-banner-to-sshd.sh',
            )
        ),
        (
            'network interfaces',
            (
                not exists('/etc/network/interfaces')
                or newer('interfaces', '/etc/network/interfaces')
            ),
            (
                'sudo cp interfaces /etc/network/interfaces',
            )
        ),
        (
            'disable HDMI',
            not grep('/etc/rc.local', 'tvservice'),
            (
                ('sudo', 'sed', '-i', 's/^exit 0$/\/usr\/bin\/tvservice -o\nexit 0/', '/etc/rc.local'),
            )
        ),
        (
            'disable Bluetooth',
            not grep('/boot/config.txt', 'pi3-disable-bt'),
            (
                'sudo bash append.sh /boot/config.txt dtoverlay=pi3-disable-bt',
            )
        ),
        (
            'disable WiFi',
            not grep('/boot/config.txt', 'pi3-disable-wifi'),
            (
                'sudo bash append.sh /boot/config.txt dtoverlay=pi3-disable-wifi',
            )
        ),
    )

    for section, test, commands in section_test_command_tuples:
        if test:
            print('+++ Running section {section}'.format(section=section))
            for command in commands:
                print(command)
                if isinstance(command, str):
                    return_code = subprocess.call(command.split(' '))
                else:
                    # Already split
                    return_code = subprocess.call(command)

                if return_code != 0:
                    print(
                        'Command failed with code {code}, aborting'.format(
                            code=return_code
                        )
                    )
                    sys.exit(return_code)
        else:
            print('Skipping section {section}'.format(section=section))

if __name__ == '__main__':
    main()
<commit_msg>Fix disabling HDMI again<commit_after>"""Does the rest of the setup."""
from __future__ import print_function

import os
import re
import subprocess
import sys

def main():
    """Does the rest of the setup."""

    def newer(file_name_1, file_name_2):
        """Returns true if file_1 is newer than file_2."""
        return os.stat(file_name_1).st_atime > os.stat(file_name_2).st_mtime

    def exists(file_name):
        """Returns true if file exists."""
        return os.access(file_name, os.F_OK)

    def grep(file_name, pattern):
        """Searches a file for a regex pattern."""
        regex = re.compile(pattern)
        with open(file_name) as file_:
            for line in file_:
                if regex.search(line):
                    return True
        return False

    section_test_command_tuples = (
        (
            'start up',
            (
                not exists('/etc/init.d/eclipse-2017-hab-rc')
                or newer('eclipse-2017-hab-rc', '/etc/init.d/eclipse-2017-hab-rc')
            ),
            (
                'sudo cp eclipse-2017-hab-rc /etc/init.d/',
                'sudo chmod +x /etc/init.d/eclipse-2017-hab-rc',
                'sudo update-rc.d eclipse-2017-hab-rc defaults',
            )
        ),
        (
            'virtualenv',
            not exists('/home/pi/.virtualenvs/eclipse-2017-hab'),
            (
                'bash setup-virtualenv.sh',
            )
        ),
        (
            'SSH banner',
            not exists('/etc/banner'),
            (
                'sudo cp banner /etc',
                'sudo bash add-banner-to-sshd.sh',
            )
        ),
        (
            'network interfaces',
            (
                not exists('/etc/network/interfaces')
                or newer('interfaces', '/etc/network/interfaces')
            ),
            (
                'sudo cp interfaces /etc/network/interfaces',
            )
        ),
        (
            'disable HDMI',
            not grep('/etc/rc.local', 'tvservice'),
            (
                ('sudo', 'sed', '-i', 's/^exit 0$/\\/usr\\/bin\\/tvservice -o\\nexit 0/', '/etc/rc.local'),
            )
        ),
        (
            'disable Bluetooth',
            not grep('/boot/config.txt', 'pi3-disable-bt'),
            (
                'sudo bash append.sh /boot/config.txt dtoverlay=pi3-disable-bt',
            )
        ),
        (
            'disable WiFi',
            not grep('/boot/config.txt', 'pi3-disable-wifi'),
            (
                'sudo bash append.sh /boot/config.txt dtoverlay=pi3-disable-wifi',
            )
        ),
    )

    for section, test, commands in section_test_command_tuples:
        if test:
            print('+++ Running section {section}'.format(section=section))
            for command in commands:
                print(command)
                if isinstance(command, str):
                    return_code = subprocess.call(command.split(' '))
                else:
                    # Already split
                    return_code = subprocess.call(command)

                if return_code != 0:
                    print(
                        'Command failed with code {code}, aborting'.format(
                            code=return_code
                        )
                    )
                    sys.exit(return_code)
        else:
            print('Skipping section {section}'.format(section=section))

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>"""Generate and work with PEP 425 Compatibility Tags."""
from __future__ import absolute_import

import re
import sys
import warnings

try:
    import sysconfig
except ImportError:  # pragma nocover
    # Python < 2.7
    import distutils.sysconfig as sysconfig
import distutils.util

_osx_arch_pat = re.compile(r'(.+)_(\d+)_(\d+)_(.+)')

def get_abbr_impl():
    """Return abbreviated implementation name."""
    if hasattr(sys, 'pypy_version_info'):
        pyimpl = 'pp'
    elif sys.platform.startswith('java'):
        pyimpl = 'jy'
    elif sys.platform == 'cli':
        pyimpl = 'ip'
    else:
        pyimpl = 'cp'
    return pyimpl

def get_impl_ver():
    """Return implementation version."""
    return ''.join(map(str, sys.version_info[:2]))

def get_platform():
    """Return our platform name 'win32', 'linux_x86_64'"""
    # XXX remove distutils dependency
    return distutils.util.get_platform().replace('.', '_').replace('-', '_')

def get_supported(versions=None, noarch=False):
    """Return a list of supported tags for each version specified in
    `versions`.

    :param versions: a list of string versions, of the form ["33", "32"],
        or None. The first version will be assumed to support our ABI.
    """
    supported = []

    # Versions must be given with respect to the preference
    if versions is None:
        versions = []
        major = sys.version_info[0]
        # Support all previous minor Python versions.
        for minor in range(sys.version_info[1], -1, -1):
            versions.append(''.join(map(str, (major, minor))))

    impl = get_abbr_impl()

    abis = []

    try:
        soabi = sysconfig.get_config_var('SOABI')
    except IOError as e:  # Issue #1074
        warnings.warn("{0}".format(e), RuntimeWarning)
        soabi = None

    if soabi and soabi.startswith('cpython-'):
        abis[0:0] = ['cp' + soabi.split('-')[1]]

    abi3s = set()
    import imp
    for suffix in imp.get_suffixes():
        if suffix[0].startswith('.abi'):
            abi3s.add(suffix[0].split('.', 2)[1])

    abis.extend(sorted(list(abi3s)))

    abis.append('none')

    if not noarch:
        arch = get_platform()
        if sys.platform == 'darwin':
            # support macosx-10.6-intel on macosx-10.9-x86_64
            match = _osx_arch_pat.match(arch)
            if match:
                name, major, minor, actual_arch = match.groups()
                actual_arches = [actual_arch]
                if actual_arch in ('i386', 'ppc'):
                    actual_arches.append('fat')
                if actual_arch in ('i386', 'x86_64'):
                    actual_arches.append('intel')
                if actual_arch in ('ppc64', 'x86_64'):
                    actual_arches.append('fat64')
                if actual_arch in ('i386', 'ppc', 'x86_64'):
                    actual_arches.append('fat32')
                if actual_arch in ('i386', 'x86_64', 'intel', 'ppc', 'ppc64'):
                    actual_arches.append('universal')
                tpl = '{0}_{1}_%i_%s'.format(name, major)
                arches = []
                for m in range(int(minor) + 1):
                    for a in actual_arches:
                        arches.append(tpl % (m, a))
            else:
                # arch pattern didn't match (?!)
                arches = [arch]
        else:
            arches = [arch]

        # Current version, current API (built specifically for our Python):
        for abi in abis:
            for arch in arches:
                supported.append(('%s%s' % (impl, versions[0]), abi, arch))

        # Has binaries, does not use the Python API:
        supported.append(('py%s' % (versions[0][0]), 'none', arch))

    # No abi / arch, but requires our implementation:
    for i, version in enumerate(versions):
        supported.append(('%s%s' % (impl, version), 'none', 'any'))
        if i == 0:
            # Tagged specifically as being cross-version compatible
            # (with just the major version specified)
            supported.append(('%s%s' % (impl, versions[0][0]), 'none', 'any'))

    # No abi / arch, generic Python
    for i, version in enumerate(versions):
        supported.append(('py%s' % (version,), 'none', 'any'))
        if i == 0:
            supported.append(('py%s' % (version[0]), 'none', 'any'))

    return supported

supported_tags = get_supported()
supported_tags_noarch = get_supported(noarch=True)
<commit_msg>Prefer a newer SDK over an older SDK<commit_after>"""Generate and work with PEP 425 Compatibility Tags."""
from __future__ import absolute_import

import re
import sys
import warnings

try:
    import sysconfig
except ImportError:  # pragma nocover
    # Python < 2.7
    import distutils.sysconfig as sysconfig
import distutils.util

_osx_arch_pat = re.compile(r'(.+)_(\d+)_(\d+)_(.+)')

def get_abbr_impl():
    """Return abbreviated implementation name."""
    if hasattr(sys, 'pypy_version_info'):
        pyimpl = 'pp'
    elif sys.platform.startswith('java'):
        pyimpl = 'jy'
    elif sys.platform == 'cli':
        pyimpl = 'ip'
    else:
        pyimpl = 'cp'
    return pyimpl

def get_impl_ver():
    """Return implementation version."""
    return ''.join(map(str, sys.version_info[:2]))

def get_platform():
    """Return our platform name 'win32', 'linux_x86_64'"""
    # XXX remove distutils dependency
    return distutils.util.get_platform().replace('.', '_').replace('-', '_')

def get_supported(versions=None, noarch=False):
    """Return a list of supported tags for each version specified in
    `versions`.

    :param versions: a list of string versions, of the form ["33", "32"],
        or None. The first version will be assumed to support our ABI.
    """
    supported = []

    # Versions must be given with respect to the preference
    if versions is None:
        versions = []
        major = sys.version_info[0]
        # Support all previous minor Python versions.
        for minor in range(sys.version_info[1], -1, -1):
            versions.append(''.join(map(str, (major, minor))))

    impl = get_abbr_impl()

    abis = []

    try:
        soabi = sysconfig.get_config_var('SOABI')
    except IOError as e:  # Issue #1074
        warnings.warn("{0}".format(e), RuntimeWarning)
        soabi = None

    if soabi and soabi.startswith('cpython-'):
        abis[0:0] = ['cp' + soabi.split('-')[1]]

    abi3s = set()
    import imp
    for suffix in imp.get_suffixes():
        if suffix[0].startswith('.abi'):
            abi3s.add(suffix[0].split('.', 2)[1])

    abis.extend(sorted(list(abi3s)))

    abis.append('none')

    if not noarch:
        arch = get_platform()
        if sys.platform == 'darwin':
            # support macosx-10.6-intel on macosx-10.9-x86_64
            match = _osx_arch_pat.match(arch)
            if match:
                name, major, minor, actual_arch = match.groups()
                actual_arches = [actual_arch]
                if actual_arch in ('i386', 'ppc'):
                    actual_arches.append('fat')
                if actual_arch in ('i386', 'x86_64'):
                    actual_arches.append('intel')
                if actual_arch in ('ppc64', 'x86_64'):
                    actual_arches.append('fat64')
                if actual_arch in ('i386', 'ppc', 'x86_64'):
                    actual_arches.append('fat32')
                if actual_arch in ('i386', 'x86_64', 'intel', 'ppc', 'ppc64'):
                    actual_arches.append('universal')
                tpl = '{0}_{1}_%i_%s'.format(name, major)
                arches = []
                for m in reversed(range(int(minor) + 1)):
                    for a in actual_arches:
                        arches.append(tpl % (m, a))
            else:
                # arch pattern didn't match (?!)
                arches = [arch]
        else:
            arches = [arch]

        # Current version, current API (built specifically for our Python):
        for abi in abis:
            for arch in arches:
                supported.append(('%s%s' % (impl, versions[0]), abi, arch))

        # Has binaries, does not use the Python API:
        supported.append(('py%s' % (versions[0][0]), 'none', arch))

    # No abi / arch, but requires our implementation:
    for i, version in enumerate(versions):
        supported.append(('%s%s' % (impl, version), 'none', 'any'))
        if i == 0:
            # Tagged specifically as being cross-version compatible
            # (with just the major version specified)
            supported.append(('%s%s' % (impl, versions[0][0]), 'none', 'any'))

    # No abi / arch, generic Python
    for i, version in enumerate(versions):
        supported.append(('py%s' % (version,), 'none', 'any'))
        if i == 0:
            supported.append(('py%s' % (version[0]), 'none', 'any'))

    return supported

supported_tags = get_supported()
supported_tags_noarch = get_supported(noarch=True)
<|endoftext|>


<commit_before># Copyright [2015] Hewlett-Packard Development Company, L.P.
#
# 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.

from proboscis.asserts import assert_equal
from proboscis.asserts import assert_false
from proboscis.asserts import assert_raises
from proboscis.asserts import assert_true
from proboscis import before_class
from proboscis.check import Check
from proboscis import test
from troveclient.compat import exceptions

from trove.tests.config import CONFIG
from trove.tests.util import create_client
from trove.tests.util import create_dbaas_client
from trove.tests.util import create_nova_client
from trove.tests.util import test_config
from trove.tests.util.users import Requirements

GROUP = "dbaas.api.mgmt.ds_versions"

@test(groups=[GROUP])
def mgmt_datastore_version_list_requires_admin_account():
    """Verify that an admin context is required to call this function."""
    client = create_client(is_admin=False)
    assert_raises(exceptions.Unauthorized, client.mgmt_datastore_versions.list)

@test(groups=[GROUP])
class MgmtDataStoreVersion(object):
    """Tests the mgmt datastore version methods."""

    @before_class
    def setUp(self):
        """Create client for tests."""
        reqs = Requirements(is_admin=True)
        self.user = CONFIG.users.find_user(reqs)
        self.client = create_dbaas_client(self.user)
        if test_config.nova_client is not None:
            nova_user = test_config.users.find_user(
                Requirements(services=["nova"]))
            self.nova_client = create_nova_client(nova_user)
            self.images = self.nova_client.images.list()

    def _find_ds_version_by_name(self, ds_version_name):
        ds_versions = self.client.mgmt_datastore_versions.list()
        for ds_version in ds_versions:
            if ds_version_name == ds_version.name:
                return ds_version

    @test
    def test_mgmt_ds_version_list_original_count(self):
        """Tests the mgmt datastore version list method."""
        self.ds_versions = self.client.mgmt_datastore_versions.list()
        # By default we create two datastore-versions for mysql
        assert_equal(2, len(self.ds_versions.items))

    @test(depends_on=[test_mgmt_ds_version_list_original_count])
    def test_mgmt_ds_version_list_fields_present(self):
        """Verify that all expected fields are returned by list method."""

        expected_fields = [
            'id',
            'name',
            'datastore_id',
            'datastore_name',
            'datastore_manager',
            'image',
            'packages',
            'active',
            'default',
        ]

        for ds_version in self.ds_versions:
            with Check() as check:
                for field in expected_fields:
                    check.true(hasattr(ds_version, field),
                               "List lacks field %s." % field)

    @test(depends_on=[test_mgmt_ds_version_list_original_count])
    def test_mgmt_ds_version_get(self):
        """Tests the mgmt datastore version get method."""
        test_version = self.ds_versions[0]
        found_ds_version = self.client.mgmt_datastore_versions.get(
            test_version.id)
        assert_equal(test_version.name, found_ds_version.name)
        assert_equal(test_version.datastore_id, found_ds_version.datastore_id)
        assert_equal(test_version.datastore_name,
                     found_ds_version.datastore_name)
        assert_equal(test_version.datastore_manager,
                     found_ds_version.datastore_manager)
        assert_equal(test_version.image, found_ds_version.image)
        assert_equal(test_version.packages, found_ds_version.packages)
        assert_equal(test_version.active, found_ds_version.active)
        assert_equal(test_version.default, found_ds_version.default)

    @test(depends_on=[test_mgmt_ds_version_list_original_count])
    def test_mgmt_ds_version_create(self):
        """Tests the mgmt datastore version create method."""
        response = self.client.mgmt_datastore_versions.create(
            'test_version1', 'test_ds', 'test_mgr',
            self.images[0].id, ['vertica-7.1'])
        assert_equal(None, response)
        assert_equal(202, self.client.last_http_code)

        # Since we created one more ds_version
        # lets check count of total ds_versions, it should be increased by 1
        new_ds_versions = self.client.mgmt_datastore_versions.list()
        assert_equal(len(self.ds_versions.items) + 1,
                     len(new_ds_versions.items))

        # Match the contents of newly created ds_version.
        self.created_version = self._find_ds_version_by_name('test_version1')
        assert_equal('test_version1', self.created_version.name)
        assert_equal('test_ds', self.created_version.datastore_name)
        assert_equal('test_mgr', self.created_version.datastore_manager)
        assert_equal(self.images[0].id, self.created_version.image)
        assert_equal(['vertica-7.1'], self.created_version.packages)
        assert_true(self.created_version.active)
        assert_false(self.created_version.default)

    @test(depends_on=[test_mgmt_ds_version_create])
    def test_mgmt_ds_version_patch(self):
        """Tests the mgmt datastore version edit method."""
        self.client.mgmt_datastore_versions.edit(
            self.created_version.id, image=self.images[1].id,
            packages=['pkg1'])
        assert_equal(202, self.client.last_http_code)

        # Lets match the content of patched datastore
        patched_ds_version = self._find_ds_version_by_name('test_version1')
        assert_equal(self.images[1].id, patched_ds_version.image)
        assert_equal(['pkg1'], patched_ds_version.packages)

    @test(depends_on=[test_mgmt_ds_version_patch])
    def test_mgmt_ds_version_delete(self):
        """Tests the mgmt datastore version delete method."""
        self.client.mgmt_datastore_versions.delete(self.created_version.id)
        assert_equal(202, self.client.last_http_code)

        # Lets match the total count of ds_version,
        # it should get back to original
        ds_versions = self.client.mgmt_datastore_versions.list()
        assert_equal(len(self.ds_versions.items), len(ds_versions.items))
<commit_msg>Fix troveclient to support Mistral<commit_after># Copyright [2015] Hewlett-Packard Development Company, L.P.
#
# 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.

from proboscis.asserts import assert_equal
from proboscis.asserts import assert_false
from proboscis.asserts import assert_raises
from proboscis.asserts import assert_true
from proboscis import before_class
from proboscis.check import Check
from proboscis import test
from troveclient.compat import exceptions

from trove.tests.config import CONFIG
from trove.tests.util import create_client
from trove.tests.util import create_dbaas_client
from trove.tests.util import create_nova_client
from trove.tests.util import test_config
from trove.tests.util.users import Requirements

GROUP = "dbaas.api.mgmt.ds_versions"

@test(groups=[GROUP])
def mgmt_datastore_version_list_requires_admin_account():
    """Verify that an admin context is required to call this function."""
    client = create_client(is_admin=False)
    assert_raises(exceptions.Unauthorized, client.mgmt_datastore_versions.list)

@test(groups=[GROUP])
class MgmtDataStoreVersion(object):
    """Tests the mgmt datastore version methods."""

    @before_class
    def setUp(self):
        """Create client for tests."""
        reqs = Requirements(is_admin=True)
        self.user = CONFIG.users.find_user(reqs)
        self.client = create_dbaas_client(self.user)
        if test_config.nova_client is not None:
            nova_user = test_config.users.find_user(
                Requirements(services=["nova"]))
            self.nova_client = create_nova_client(nova_user)
            self.images = self.nova_client.images.list()

    def _find_ds_version_by_name(self, ds_version_name):
        ds_versions = self.client.mgmt_datastore_versions.list()
        for ds_version in ds_versions:
            if ds_version_name == ds_version.name:
                return ds_version

    @test
    def test_mgmt_ds_version_list_original_count(self):
        """Tests the mgmt datastore version list method."""
        self.ds_versions = self.client.mgmt_datastore_versions.list()
        # By default we create two datastore-versions for mysql
        assert_equal(2, len(self.ds_versions))

    @test(depends_on=[test_mgmt_ds_version_list_original_count])
    def test_mgmt_ds_version_list_fields_present(self):
        """Verify that all expected fields are returned by list method."""

        expected_fields = [
            'id',
            'name',
            'datastore_id',
            'datastore_name',
            'datastore_manager',
            'image',
            'packages',
            'active',
            'default',
        ]

        for ds_version in self.ds_versions:
            with Check() as check:
                for field in expected_fields:
                    check.true(hasattr(ds_version, field),
                               "List lacks field %s." % field)

    @test(depends_on=[test_mgmt_ds_version_list_original_count])
    def test_mgmt_ds_version_get(self):
        """Tests the mgmt datastore version get method."""
        test_version = self.ds_versions[0]
        found_ds_version = self.client.mgmt_datastore_versions.get(
            test_version.id)
        assert_equal(test_version.name, found_ds_version.name)
        assert_equal(test_version.datastore_id, found_ds_version.datastore_id)
        assert_equal(test_version.datastore_name,
                     found_ds_version.datastore_name)
        assert_equal(test_version.datastore_manager,
                     found_ds_version.datastore_manager)
        assert_equal(test_version.image, found_ds_version.image)
        assert_equal(test_version.packages, found_ds_version.packages)
        assert_equal(test_version.active, found_ds_version.active)
        assert_equal(test_version.default, found_ds_version.default)

    @test(depends_on=[test_mgmt_ds_version_list_original_count])
    def test_mgmt_ds_version_create(self):
        """Tests the mgmt datastore version create method."""
        response = self.client.mgmt_datastore_versions.create(
            'test_version1', 'test_ds', 'test_mgr',
            self.images[0].id, ['vertica-7.1'])
        assert_equal(None, response)
        assert_equal(202, self.client.last_http_code)

        # Since we created one more ds_version
        # lets check count of total ds_versions, it should be increased by 1
        new_ds_versions = self.client.mgmt_datastore_versions.list()
        assert_equal(len(self.ds_versions) + 1,
                     len(new_ds_versions))

        # Match the contents of newly created ds_version.
        self.created_version = self._find_ds_version_by_name('test_version1')
        assert_equal('test_version1', self.created_version.name)
        assert_equal('test_ds', self.created_version.datastore_name)
        assert_equal('test_mgr', self.created_version.datastore_manager)
        assert_equal(self.images[0].id, self.created_version.image)
        assert_equal(['vertica-7.1'], self.created_version.packages)
        assert_true(self.created_version.active)
        assert_false(self.created_version.default)

    @test(depends_on=[test_mgmt_ds_version_create])
    def test_mgmt_ds_version_patch(self):
        """Tests the mgmt datastore version edit method."""
        self.client.mgmt_datastore_versions.edit(
            self.created_version.id, image=self.images[1].id,
            packages=['pkg1'])
        assert_equal(202, self.client.last_http_code)

        # Lets match the content of patched datastore
        patched_ds_version = self._find_ds_version_by_name('test_version1')
        assert_equal(self.images[1].id, patched_ds_version.image)
        assert_equal(['pkg1'], patched_ds_version.packages)

    @test(depends_on=[test_mgmt_ds_version_patch])
    def test_mgmt_ds_version_delete(self):
        """Tests the mgmt datastore version delete method."""
        self.client.mgmt_datastore_versions.delete(self.created_version.id)
        assert_equal(202, self.client.last_http_code)

        # Lets match the total count of ds_version,
        # it should get back to original
        ds_versions = self.client.mgmt_datastore_versions.list()
        assert_equal(len(self.ds_versions), len(ds_versions))
<|endoftext|>