<commit_before>from collections import defaultdict

from pluginsdk._scriptapi.debug import SetBreakpoint, DeleteBreakpoint, \
    SetHardwareBreakpoint, DeleteHardwareBreakpoint, HardwareType

NONE = 0
NORMAL = 1
HARDWARE = 2
MEMORY = 4

ACCESS = HardwareType.HardwareAccess
WRITE = HardwareType.HardwareWrite
EXECUTE = HardwareType.HardwareExecute

class Breakpoint(object):
    BP_NONE = NONE
    BP_NORMAL = NORMAL
    BP_HARDWARE = HARDWARE
    BP_MEMORY = MEMORY

    HW_ACCESS = ACCESS
    HW_WRITE = WRITE
    HW_EXECUTE = EXECUTE

    def __init__(self, event_object):
        self.__breakpoints = defaultdict(dict)
        self.__event_object = event_object

        self.enable()

    def __breakpoint_function(self, **kwargs):
        print kwargs

        address = kwargs['addr']
        if address in self.__breakpoints:
            if not (kwargs['enabled'] and kwargs['active']):
                return

            self.__breakpoints[address]['callback']()

    def enable(self):
        self.__event_object.listen('breakpoint', self.__breakpoint_function)

    def disable(self):
        self.__event_object.listen('breakpoint', None)

    def add(self, address, callback, bp_type=NORMAL, hw_type=EXECUTE):
        if bp_type == self.BP_NORMAL:
            SetBreakpoint(address)
        elif bp_type == self.BP_HARDWARE:
            SetHardwareBreakpoint(address, hw_type)

        self.__breakpoints[address]['callback'] = callback
        self.__breakpoints[address]['type'] = bp_type

    def remove(self, address):
        if address not in self.__breakpoints:
            return

        bp_type = self.__breakpoints[address]['type']
        if bp_type == self.BP_NORMAL:
            DeleteBreakpoint(address)
        elif bp_type == self.BP_HARDWARE:
            DeleteHardwareBreakpoint(address)

        del self.__breakpoints[address]

    def list(self):
        return self.__breakpoints.keys()
<commit_msg>Remove Debug Print (Breakpoint)<commit_after>from collections import defaultdict

from pluginsdk._scriptapi.debug import SetBreakpoint, DeleteBreakpoint, \
    SetHardwareBreakpoint, DeleteHardwareBreakpoint, HardwareType

NONE = 0
NORMAL = 1
HARDWARE = 2
MEMORY = 4

ACCESS = HardwareType.HardwareAccess
WRITE = HardwareType.HardwareWrite
EXECUTE = HardwareType.HardwareExecute

class Breakpoint(object):
    BP_NONE = NONE
    BP_NORMAL = NORMAL
    BP_HARDWARE = HARDWARE
    BP_MEMORY = MEMORY

    HW_ACCESS = ACCESS
    HW_WRITE = WRITE
    HW_EXECUTE = EXECUTE

    def __init__(self, event_object):
        self.__breakpoints = defaultdict(dict)
        self.__event_object = event_object

        self.enable()

    def __breakpoint_function(self, **kwargs):
        address = kwargs['addr']
        if address in self.__breakpoints:
            if not (kwargs['enabled'] and kwargs['active']):
                return

            self.__breakpoints[address]['callback']()

    def enable(self):
        self.__event_object.listen('breakpoint', self.__breakpoint_function)

    def disable(self):
        self.__event_object.listen('breakpoint', None)

    def add(self, address, callback, bp_type=NORMAL, hw_type=EXECUTE):
        if bp_type == self.BP_NORMAL:
            SetBreakpoint(address)
        elif bp_type == self.BP_HARDWARE:
            SetHardwareBreakpoint(address, hw_type)

        self.__breakpoints[address]['callback'] = callback
        self.__breakpoints[address]['type'] = bp_type

    def remove(self, address):
        if address not in self.__breakpoints:
            return

        bp_type = self.__breakpoints[address]['type']
        if bp_type == self.BP_NORMAL:
            DeleteBreakpoint(address)
        elif bp_type == self.BP_HARDWARE:
            DeleteHardwareBreakpoint(address)

        del self.__breakpoints[address]

    def list(self):
        return self.__breakpoints.keys()
<|endoftext|>


<commit_before># Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
# Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Examples:
# python manage.py rebuild_timeline --settings=settings.local_timeline --initial_date 2014-10-02 --final_date 2014-10-03
# python manage.py rebuild_timeline --settings=settings.local_timeline --purge
# python manage.py rebuild_timeline --settings=settings.local_timeline --initial_date 2014-10-02

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.core.management.base import BaseCommand
from django.db.models import Model
from django.db import reset_queries

from taiga.projects.models import Project
from taiga.projects.history import services as history_services
from taiga.projects.history.choices import HistoryType
from taiga.projects.history.models import HistoryEntry
from taiga.timeline.models import Timeline
from taiga.timeline.service import (_add_to_object_timeline, _get_impl_key_from_model,
    _timeline_impl_map, extract_user_info)
from taiga.timeline.signals import on_new_history_entry, _push_to_timelines
from taiga.users.models import User

from unittest.mock import patch
from optparse import make_option

import gc

class BulkCreator(object):
    def __init__(self):
        self.timeline_objects = []
        self.created = None

    def createElement(self, element):
        self.timeline_objects.append(element)
        if len(self.timeline_objects) > 1000:
            Timeline.objects.bulk_create(self.timeline_objects, batch_size=1000)
            del self.timeline_objects
            self.timeline_objects = []
            gc.collect()

bulk_creator = BulkCreator()

def custom_add_to_object_timeline(obj:object, instance:object, event_type:str, namespace:str="default", extra_data:dict={}):
    assert isinstance(obj, Model), "obj must be a instance of Model"
    assert isinstance(instance, Model), "instance must be a instance of Model"
    event_type_key = _get_impl_key_from_model(instance.__class__, event_type)
    impl = _timeline_impl_map.get(event_type_key, None)

    bulk_creator.createElement(Timeline(
        content_object=obj,
        namespace=namespace,
        event_type=event_type_key,
        project=instance.project,
        data=impl(instance, extra_data=extra_data),
        data_content_type = ContentType.objects.get_for_model(instance.__class__),
        created = bulk_creator.created,
    ))

def generate_timeline(initial_date, final_date):
    if initial_date or final_date:
        timelines = Timeline.objects.all()
        if initial_date:
            timelines = timelines.filter(created__gte=initial_date)
        if final_date:
            timelines = timelines.filter(created__lt=final_date)

        timelines.delete()

    with patch('taiga.timeline.service._add_to_object_timeline', new=custom_add_to_object_timeline):
        # Projects api wasn't a HistoryResourceMixin so we can't interate on the HistoryEntries in this case
        projects = Project.objects.order_by("created_date")
        history_entries = HistoryEntry.objects.order_by("created_at")

        if initial_date:
            projects = projects.filter(created_date__gte=initial_date)
            history_entries = history_entries.filter(created_at__gte=initial_date)

        if final_date:
            projects = projects.filter(created_date__lt=final_date)
            history_entries = history_entries.filter(created_at__lt=final_date)

        for project in projects.iterator():
            bulk_creator.created = project.created_date
            print("Project:", bulk_creator.created)
            extra_data = {
                "values_diff": {},
                "user": extract_user_info(project.owner),
            }
            _push_to_timelines(project, project.owner, project, "create", extra_data=extra_data)
            del extra_data

        for historyEntry in history_entries.iterator():
            print("History entry:", historyEntry.created_at)
            try:
                bulk_creator.created = historyEntry.created_at
                on_new_history_entry(None, historyEntry, None)
            except ObjectDoesNotExist as e:
                print("Ignoring")

class Command(BaseCommand):
    help = 'Regenerate project timeline'
    option_list = BaseCommand.option_list + (
        make_option('--purge',
                    action='store_true',
                    dest='purge',
                    default=False,
                    help='Purge existing timelines'),
        ) + (
        make_option('--initial_date',
                    action='store',
                    dest='initial_date',
                    default=None,
                    help='Initial date for timeline generation'),
        ) + (
        make_option('--final_date',
                    action='store',
                    dest='final_date',
                    default=None,
                    help='Final date for timeline generation'),
        )

    def handle(self, *args, **options):
        debug_enabled = settings.DEBUG
        if debug_enabled:
            print("Please, execute this script only with DEBUG mode disabled (DEBUG=False)")
            return

        if options["purge"] == True:
            Timeline.objects.all().delete()

        generate_timeline(options["initial_date"], options["final_date"])
<commit_msg>Improving rebuild_timeline command allowing rebuilding it only for a project<commit_after># Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
# Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Examples:
# python manage.py rebuild_timeline --settings=settings.local_timeline --initial_date 2014-10-02 --final_date 2014-10-03
# python manage.py rebuild_timeline --settings=settings.local_timeline --purge
# python manage.py rebuild_timeline --settings=settings.local_timeline --initial_date 2014-10-02

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.core.management.base import BaseCommand
from django.db.models import Model
from django.db import reset_queries

from taiga.projects.models import Project
from taiga.projects.history import services as history_services
from taiga.projects.history.choices import HistoryType
from taiga.projects.history.models import HistoryEntry
from taiga.timeline.models import Timeline
from taiga.timeline.service import (_add_to_object_timeline, _get_impl_key_from_model,
    _timeline_impl_map, extract_user_info)
from taiga.timeline.signals import on_new_history_entry, _push_to_timelines
from taiga.users.models import User

from unittest.mock import patch
from optparse import make_option

import gc

class BulkCreator(object):
    def __init__(self):
        self.timeline_objects = []
        self.created = None

    def create_element(self, element):
        self.timeline_objects.append(element)
        if len(self.timeline_objects) > 1000:
            self.flush()

    def flush(self):
        Timeline.objects.bulk_create(self.timeline_objects, batch_size=1000)
        del self.timeline_objects
        self.timeline_objects = []
        gc.collect()

bulk_creator = BulkCreator()

def custom_add_to_object_timeline(obj:object, instance:object, event_type:str, namespace:str="default", extra_data:dict={}):
    assert isinstance(obj, Model), "obj must be a instance of Model"
    assert isinstance(instance, Model), "instance must be a instance of Model"
    event_type_key = _get_impl_key_from_model(instance.__class__, event_type)
    impl = _timeline_impl_map.get(event_type_key, None)

    bulk_creator.create_element(Timeline(
        content_object=obj,
        namespace=namespace,
        event_type=event_type_key,
        project=instance.project,
        data=impl(instance, extra_data=extra_data),
        data_content_type = ContentType.objects.get_for_model(instance.__class__),
        created = bulk_creator.created,
    ))

def generate_timeline(initial_date, final_date, project_id):
    if initial_date or final_date or project_id:
        timelines = Timeline.objects.all()
        if initial_date:
            timelines = timelines.filter(created__gte=initial_date)
        if final_date:
            timelines = timelines.filter(created__lt=final_date)
        if project_id:
            timelines = timelines.filter(project__id=project_id)

        timelines.delete()

    with patch('taiga.timeline.service._add_to_object_timeline', new=custom_add_to_object_timeline):
        # Projects api wasn't a HistoryResourceMixin so we can't interate on the HistoryEntries in this case
        projects = Project.objects.order_by("created_date")
        history_entries = HistoryEntry.objects.order_by("created_at")

        if initial_date:
            projects = projects.filter(created_date__gte=initial_date)
            history_entries = history_entries.filter(created_at__gte=initial_date)

        if final_date:
            projects = projects.filter(created_date__lt=final_date)
            history_entries = history_entries.filter(created_at__lt=final_date)

        if project_id:
            project = Project.objects.get(id=project_id)
            us_keys = ['userstories.userstory:%s'%(id) for id in project.user_stories.values_list("id", flat=True)]
            tasks_keys = ['tasks.task:%s'%(id) for id in project.tasks.values_list("id", flat=True)]
            issue_keys = ['issues.issue:%s'%(id) for id in project.issues.values_list("id", flat=True)]
            wiki_keys = ['wiki.wikipage:%s'%(id) for id in project.wiki_pages.values_list("id", flat=True)]
            keys = us_keys + tasks_keys + issue_keys + wiki_keys

            projects = projects.filter(id=project_id)
            history_entries = history_entries.filter(key__in=keys)

            #Memberships
            for membership in project.memberships.exclude(user=None):
                bulk_creator.created = membership.created_at
                _push_to_timelines(project, membership.user, membership, "create")

        for project in projects.iterator():
            bulk_creator.created = project.created_date
            print("Project:", bulk_creator.created)
            extra_data = {
                "values_diff": {},
                "user": extract_user_info(project.owner),
            }
            _push_to_timelines(project, project.owner, project, "create", extra_data=extra_data)
            del extra_data

        for historyEntry in history_entries.iterator():
            print("History entry:", historyEntry.created_at)
            try:
                bulk_creator.created = historyEntry.created_at
                on_new_history_entry(None, historyEntry, None)
            except ObjectDoesNotExist as e:
                print("Ignoring")

    bulk_creator.flush()

class Command(BaseCommand):
    help = 'Regenerate project timeline'
    option_list = BaseCommand.option_list + (
        make_option('--purge',
                    action='store_true',
                    dest='purge',
                    default=False,
                    help='Purge existing timelines'),
        ) + (
        make_option('--initial_date',
                    action='store',
                    dest='initial_date',
                    default=None,
                    help='Initial date for timeline generation'),
        ) + (
        make_option('--final_date',
                    action='store',
                    dest='final_date',
                    default=None,
                    help='Final date for timeline generation'),
        ) + (
        make_option('--project',
                    action='store',
                    dest='project',
                    default=None,
                    help='Selected project id for timeline generation'),
        )

    def handle(self, *args, **options):
        debug_enabled = settings.DEBUG
        if debug_enabled:
            print("Please, execute this script only with DEBUG mode disabled (DEBUG=False)")
            return

        if options["purge"] == True:
            Timeline.objects.all().delete()

        generate_timeline(options["initial_date"], options["final_date"], options["project"])
<|endoftext|>


<commit_before>
import base64
import os.path

import pygments
import pygments.util
import pygments.lexers
import pygments.formatters

from django.conf import settings
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.http import HttpResponseForbidden, Http404
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.utils.html import escape

from review.diffview import get_chunks_html, split_lines, NoWrapperHtmlFormatter
from review.models import CodeReview, ChangeStatusLog, get_all_reviewers
from extensions import models

from decorators import ajax_view, model_view, post_only_view
from utils import render

IMAGE_TYPES = {
    '.png':  'image/png',
    '.jpg':  'image/jpeg',
    '.jpeg': 'image/jpeg',
    '.gif':  'image/gif',
    '.bmp':  'image/bmp',
    '.svg':  'image/svg+xml',
}

code_formatter = pygments.formatters.HtmlFormatter(style="borland", cssclass="code")
diff_formatter = NoWrapperHtmlFormatter(style="borland")

def can_review_extension(user, extension):
    if user == extension.creator:
        return True

    if user.has_perm("review.can-review-extensions"):
        return True

    return False

def can_approve_extension(user, extension):
    if user.is_superuser:
        return True

    if user.has_perm("review.can-review-extensions"):
        return user != extension.creator

    return False

def highlight_file(filename, raw, formatter):
    try:
        lexer = pygments.lexers.guess_lexer_for_filename(filename, raw)
    except pygments.util.ClassNotFound:
        # released pygments doesn't yet have .json
        # so hack around it here.
        if filename.endswith('.json'):
            lexer = pygments.lexers.get_lexer_by_name('js')
        else:
            lexer = pygments.lexers.get_lexer_by_name('text')

    return pygments.highlight(raw, lexer, formatter)

def html_for_file(filename, raw):
    base, extension = os.path.splitext(filename)

    file_ = dict(filename=filename)

    if extension in IMAGE_TYPES:
        mime = IMAGE_TYPES[extension]
        raw_base64 = base64.standard_b64encode(raw)

        return dict(html='' % (mime, raw_base64,),
                    num_lines=0)

    else:
        return dict(html=highlight_file(filename, raw, code_formatter),
                    num_lines=len(raw.strip().splitlines()))

def get_zipfiles(version):
    extension = version.extension

    new_zipfile = version.get_zipfile('r')
    old_zipfile = extension.latest_version.get_zipfile('r')

    return old_zipfile, new_zipfile

def get_diff(old_zipfile, new_zipfile, filename, highlight):
    old, new = old_zipfile.open(filename, 'r'), new_zipfile.open(filename, 'r')
    oldcontent, newcontent = old.read(), new.read()
    old.close()
    new.close()

    if highlight:
        oldmarkup = highlight_file(filename, oldcontent, diff_formatter)
        newmarkup = highlight_file(filename, newcontent, diff_formatter)
    else:
        oldmarkup = escape(oldcontent)
        newmarkup = escape(newcontent)

    oldlines = split_lines(oldmarkup)
    newlines = split_lines(newmarkup)

    old_htmls, new_htmls = get_chunks_html(oldlines, newlines)
    return '\n'.join(old_htmls), '\n'.join(new_htmls)

@ajax_view
@model_view(models.ExtensionVersion)
def ajax_get_file_list_view(request, obj):
    version, extension = obj, obj.extension

    if not can_review_extension(request.user, extension):
        return HttpResponseForbidden()

    old_zipfile, new_zipfile = get_zipfiles(version)

    new_filelist = set(new_zipfile.namelist())
    old_filelist = set(old_zipfile.namelist())

    both    = new_filelist & old_filelist
    added   = new_filelist - old_filelist
    deleted = old_filelist - new_filelist

    return dict(both=sorted(both),
                added=sorted(added),
                deleted=sorted(deleted))

@ajax_view
@model_view(models.ExtensionVersion)
def ajax_get_file_diff_view(request, obj):
    version, extension = obj, obj.extension

    if not can_review_extension(request.user, extension):
        return HttpResponseForbidden()

    old_zipfile, new_zipfile = get_zipfiles(version)

    filename = request.GET['filename']
    highlight = request.GET.get('highlight', True)

    new_filelist = set(new_zipfile.namelist())
    old_filelist = set(old_zipfile.namelist())

    diff = None

    if filename in old_filelist:
        if filename in new_filelist:
            operation = 'both'
            diff = get_diff(old_zipfile, new_zipfile,
                            filename, highlight)
        else:
            operation = 'deleted'
    elif filename in new_filelist:
        operation = 'added'
    else:
        raise Http404()

    return dict(operation=operation,
                diff=diff)

@ajax_view
@model_view(models.ExtensionVersion)
def ajax_get_file_view(request, obj):
    if not can_review_extension(request.user, obj.extension):
        return HttpResponseForbidden()

    zipfile = obj.get_zipfile('r')
    filename = request.GET['filename']

    try:
        f = zipfile.open(filename, 'r')
    except KeyError:
        raise Http404()

    raw = f.read()
    return html_for_file(filename, raw)

@post_only_view
@model_view(models.ExtensionVersion)
def submit_review_view(request, obj):
    extension, version = obj.extension, obj

    if not can_review_extension(request.user, extension):
        return HttpResponseForbidden()

    review = CodeReview(version=version,
                        reviewer=request.user,
                        comments=request.POST.get('comments'))

    messages.info(request, "Thank you for reviewing %s" % (extension.name,))

    if can_approve_extension(request.user, obj.extension):
        status_string = request.POST.get('status')
        newstatus = dict(approve=models.STATUS_ACTIVE,
                         reject=models.STATUS_REJECTED).get(status_string, None)

        if newstatus is not None:
            log = ChangeStatusLog(user=request.user,
                                  version=obj,
                                  newstatus=newstatus)
            log.save()

            obj.status = newstatus
            obj.save()

            review.changelog = log

            models.status_changed.send(sender=request, version=obj, log=log)

    review.save()

    models.reviewed.send(sender=request, request=request,
                         version=version, review=review)

    return redirect('review-list')

@model_view(models.ExtensionVersion)
def review_version_view(request, obj):
    extension, version = obj.extension, obj

    # Reviews on previous versions of the same extension.
    previous_versions = extension.versions.order_by('-version')

    # Exclude this version...
    previous_versions = previous_versions.exclude(version=version.version)

    # Other reviews on the same version
    previous_reviews = version.reviews.all()

    can_approve = can_approve_extension(request.user, extension)

    context = dict(extension=extension,
                   version=version,
                   previous_versions=previous_versions,
                   previous_reviews=previous_reviews,
                   can_approve=can_approve)

    if can_review_extension(request.user, extension):
        template_name = "review/review_reviewer.html"
    else:
        template_name = "review/review.html"

    return render(request, template_name, context)

def send_email_on_submitted(sender, request, version, **kwargs):
    extension = version.extension

    url = request.build_absolute_uri(reverse('review-version',
                                             kwargs=dict(pk=version.pk)))

    data = dict(version=version,
                extension=extension,
                url=url)

    reviewers = get_all_reviewers().values_list('email', flat=True)

    subject = render_to_string('review/submitted_mail_subject.txt', data).strip()
    subject = subject.replace('\n', '')
    subject = subject.replace('\r', '')

    message = render_to_string('review/submitted_mail.txt', data).strip()

    send_mail(subject=subject,
              message=message,
              from_email=settings.DEFAULT_FROM_EMAIL,
              recipient_list=reviewers)

models.submitted_for_review.connect(send_email_on_submitted)

def send_email_on_reviewed(sender, request, version, review, **kwargs):
    extension = version.extension

    url = request.build_absolute_uri(reverse('review-version',
                                             kwargs=dict(pk=version.pk)))

    data = dict(version=version,
                extension=extension,
                url=url)

    subject = render_to_string('review/reviewed_mail_subject.txt', data).strip()
    subject = subject.replace('\n', '')
    subject = subject.replace('\r', '')

    message = render_to_string('review/reviewed_mail.txt', data).strip()

    recipient_list = list(version.reviews.values_list('reviewer__email', flat=True).distinct())
    recipient_list.append(extension.creator.email)

    if review.reviewer.email in recipient_list:
        # Don't spam the reviewer with his own review.
        recipient_list.remove(review.reviewer.email)

    send_mail(subject=subject,
              message=message,
              from_email=settings.DEFAULT_FROM_EMAIL,
              recipient_list=recipient_list)

models.reviewed.connect(send_email_on_reviewed)
<commit_msg>Remove an unused variable<commit_after>
import base64
import os.path

import pygments
import pygments.util
import pygments.lexers
import pygments.formatters

from django.conf import settings
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.http import HttpResponseForbidden, Http404
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.utils.html import escape

from review.diffview import get_chunks_html, split_lines, NoWrapperHtmlFormatter
from review.models import CodeReview, ChangeStatusLog, get_all_reviewers
from extensions import models

from decorators import ajax_view, model_view, post_only_view
from utils import render

IMAGE_TYPES = {
    '.png':  'image/png',
    '.jpg':  'image/jpeg',
    '.jpeg': 'image/jpeg',
    '.gif':  'image/gif',
    '.bmp':  'image/bmp',
    '.svg':  'image/svg+xml',
}

code_formatter = pygments.formatters.HtmlFormatter(style="borland", cssclass="code")
diff_formatter = NoWrapperHtmlFormatter(style="borland")

def can_review_extension(user, extension):
    if user == extension.creator:
        return True

    if user.has_perm("review.can-review-extensions"):
        return True

    return False

def can_approve_extension(user, extension):
    if user.is_superuser:
        return True

    if user.has_perm("review.can-review-extensions"):
        return user != extension.creator

    return False

def highlight_file(filename, raw, formatter):
    try:
        lexer = pygments.lexers.guess_lexer_for_filename(filename, raw)
    except pygments.util.ClassNotFound:
        # released pygments doesn't yet have .json
        # so hack around it here.
        if filename.endswith('.json'):
            lexer = pygments.lexers.get_lexer_by_name('js')
        else:
            lexer = pygments.lexers.get_lexer_by_name('text')

    return pygments.highlight(raw, lexer, formatter)

def html_for_file(filename, raw):
    base, extension = os.path.splitext(filename)

    if extension in IMAGE_TYPES:
        mime = IMAGE_TYPES[extension]
        raw_base64 = base64.standard_b64encode(raw)

        return dict(html='' % (mime, raw_base64,),
                    num_lines=0)

    else:
        return dict(html=highlight_file(filename, raw, code_formatter),
                    num_lines=len(raw.strip().splitlines()))

def get_zipfiles(version):
    extension = version.extension

    new_zipfile = version.get_zipfile('r')
    old_zipfile = extension.latest_version.get_zipfile('r')

    return old_zipfile, new_zipfile

def get_diff(old_zipfile, new_zipfile, filename, highlight):
    old, new = old_zipfile.open(filename, 'r'), new_zipfile.open(filename, 'r')
    oldcontent, newcontent = old.read(), new.read()
    old.close()
    new.close()

    if highlight:
        oldmarkup = highlight_file(filename, oldcontent, diff_formatter)
        newmarkup = highlight_file(filename, newcontent, diff_formatter)
    else:
        oldmarkup = escape(oldcontent)
        newmarkup = escape(newcontent)

    oldlines = split_lines(oldmarkup)
    newlines = split_lines(newmarkup)

    old_htmls, new_htmls = get_chunks_html(oldlines, newlines)
    return '\n'.join(old_htmls), '\n'.join(new_htmls)

@ajax_view
@model_view(models.ExtensionVersion)
def ajax_get_file_list_view(request, obj):
    version, extension = obj, obj.extension

    if not can_review_extension(request.user, extension):
        return HttpResponseForbidden()

    old_zipfile, new_zipfile = get_zipfiles(version)

    new_filelist = set(new_zipfile.namelist())
    old_filelist = set(old_zipfile.namelist())

    both    = new_filelist & old_filelist
    added   = new_filelist - old_filelist
    deleted = old_filelist - new_filelist

    return dict(both=sorted(both),
                added=sorted(added),
                deleted=sorted(deleted))

@ajax_view
@model_view(models.ExtensionVersion)
def ajax_get_file_diff_view(request, obj):
    version, extension = obj, obj.extension

    if not can_review_extension(request.user, extension):
        return HttpResponseForbidden()

    old_zipfile, new_zipfile = get_zipfiles(version)

    filename = request.GET['filename']
    highlight = request.GET.get('highlight', True)

    new_filelist = set(new_zipfile.namelist())
    old_filelist = set(old_zipfile.namelist())

    diff = None

    if filename in old_filelist:
        if filename in new_filelist:
            operation = 'both'
            diff = get_diff(old_zipfile, new_zipfile,
                            filename, highlight)
        else:
            operation = 'deleted'
    elif filename in new_filelist:
        operation = 'added'
    else:
        raise Http404()

    return dict(operation=operation,
                diff=diff)

@ajax_view
@model_view(models.ExtensionVersion)
def ajax_get_file_view(request, obj):
    if not can_review_extension(request.user, obj.extension):
        return HttpResponseForbidden()

    zipfile = obj.get_zipfile('r')
    filename = request.GET['filename']

    try:
        f = zipfile.open(filename, 'r')
    except KeyError:
        raise Http404()

    raw = f.read()
    return html_for_file(filename, raw)

@post_only_view
@model_view(models.ExtensionVersion)
def submit_review_view(request, obj):
    extension, version = obj.extension, obj

    if not can_review_extension(request.user, extension):
        return HttpResponseForbidden()

    review = CodeReview(version=version,
                        reviewer=request.user,
                        comments=request.POST.get('comments'))

    messages.info(request, "Thank you for reviewing %s" % (extension.name,))

    if can_approve_extension(request.user, obj.extension):
        status_string = request.POST.get('status')
        newstatus = dict(approve=models.STATUS_ACTIVE,
                         reject=models.STATUS_REJECTED).get(status_string, None)

        if newstatus is not None:
            log = ChangeStatusLog(user=request.user,
                                  version=obj,
                                  newstatus=newstatus)
            log.save()

            obj.status = newstatus
            obj.save()

            review.changelog = log

            models.status_changed.send(sender=request, version=obj, log=log)

    review.save()

    models.reviewed.send(sender=request, request=request,
                         version=version, review=review)

    return redirect('review-list')

@model_view(models.ExtensionVersion)
def review_version_view(request, obj):
    extension, version = obj.extension, obj

    # Reviews on previous versions of the same extension.
    previous_versions = extension.versions.order_by('-version')

    # Exclude this version...
    previous_versions = previous_versions.exclude(version=version.version)

    # Other reviews on the same version
    previous_reviews = version.reviews.all()

    can_approve = can_approve_extension(request.user, extension)

    context = dict(extension=extension,
                   version=version,
                   previous_versions=previous_versions,
                   previous_reviews=previous_reviews,
                   can_approve=can_approve)

    if can_review_extension(request.user, extension):
        template_name = "review/review_reviewer.html"
    else:
        template_name = "review/review.html"

    return render(request, template_name, context)

def send_email_on_submitted(sender, request, version, **kwargs):
    extension = version.extension

    url = request.build_absolute_uri(reverse('review-version',
                                             kwargs=dict(pk=version.pk)))

    data = dict(version=version,
                extension=extension,
                url=url)

    reviewers = get_all_reviewers().values_list('email', flat=True)

    subject = render_to_string('review/submitted_mail_subject.txt', data).strip()
    subject = subject.replace('\n', '')
    subject = subject.replace('\r', '')

    message = render_to_string('review/submitted_mail.txt', data).strip()

    send_mail(subject=subject,
              message=message,
              from_email=settings.DEFAULT_FROM_EMAIL,
              recipient_list=reviewers)

models.submitted_for_review.connect(send_email_on_submitted)

def send_email_on_reviewed(sender, request, version, review, **kwargs):
    extension = version.extension

    url = request.build_absolute_uri(reverse('review-version',
                                             kwargs=dict(pk=version.pk)))

    data = dict(version=version,
                extension=extension,
                url=url)

    subject = render_to_string('review/reviewed_mail_subject.txt', data).strip()
    subject = subject.replace('\n', '')
    subject = subject.replace('\r', '')

    message = render_to_string('review/reviewed_mail.txt', data).strip()

    recipient_list = list(version.reviews.values_list('reviewer__email', flat=True).distinct())
    recipient_list.append(extension.creator.email)

    if review.reviewer.email in recipient_list:
        # Don't spam the reviewer with his own review.
        recipient_list.remove(review.reviewer.email)

    send_mail(subject=subject,
              message=message,
              from_email=settings.DEFAULT_FROM_EMAIL,
              recipient_list=recipient_list)

models.reviewed.connect(send_email_on_reviewed)
<|endoftext|>


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

import os
import shutil
import sys
import tarfile
import zipfile

from .util import info

def build_and_test_and_package(args, job):
    print('# BEGIN SUBSECTION: ament build')
    # ignore ROS 1 bridge package for now
    ros1_bridge_path = os.path.join(args.sourcespace, 'ros2', 'ros1_bridge')
    info('ROS1 bridge path: %s' % ros1_bridge_path)
    ros1_bridge_ignore_marker = None
    if os.path.exists(ros1_bridge_path):
        ros1_bridge_ignore_marker = os.path.join(ros1_bridge_path, 'AMENT_IGNORE')
        info('ROS1 bridge path ignore file: %s' % ros1_bridge_ignore_marker)
        with open(ros1_bridge_ignore_marker, 'w'):
            pass

    ament_py = '"%s"' % os.path.join(
        '.', args.sourcespace, 'ament', 'ament_tools', 'scripts', 'ament.py'
    )
    # Now run ament build
    cmd = [
        job.python, '-u', ament_py, 'build',
        '"%s"' % args.sourcespace,
        '--build-space', '"%s"' % args.buildspace,
        '--install-space', '"%s"' % args.installspace,
    ]
    if args.isolated:
        cmd.append('--isolated')
    if args.cmake_build_type:
        cmd += ['--cmake-args', '-DCMAKE_BUILD_TYPE=' + args.cmake_build_type + ' --']
    job.run(cmd)

    if ros1_bridge_ignore_marker:
        os.remove(ros1_bridge_ignore_marker)
    print('# END SUBSECTION')

    # It only makes sense to build the bridge for Linux or OSX, since
    # ROS1 is not supported on Windows
    if args.os in ['linux', 'osx']:
        print('# BEGIN SUBSECTION: build ROS 1 bridge')
        # Now run ament build only for the bridge
        job.run([
            job.python, '-u', ament_py, 'build',
            '"%s"' % args.sourcespace,
            '--build-tests',
            '--build-space', '"%s"' % args.buildspace,
            '--install-space', '"%s"' % args.installspace,
            '--only-packages', 'ros1_bridge',
        ] + (['--isolated'] if args.isolated else []) +
            (
                ['--cmake-args', '-DCMAKE_BUILD_TYPE=' + args.cmake_build_type + ' --']
                if args.cmake_build_type else []
            ) + [
            '--make-flags', '-j1', '--'
        ])
        print('# END SUBSECTION')

        if args.test_bridge:
            print('# BEGIN SUBSECTION: install Python packages for ros1_bridge test')
            job.run(['"%s"' % job.python, '-m', 'pip', 'install', '-U', 'catkin_pkg', 'rospkg'], shell=True)
            print('# END SUBSECTION')

            print('# BEGIN SUBSECTION: test ROS 1 bridge')
            # Now run ament test only for the bridge
            ret_test = job.run([
                '"%s"' % job.python, '-u', '"%s"' % ament_py, 'test',
                '"%s"' % args.sourcespace,
                '--build-space', '"%s"' % args.buildspace,
                '--install-space', '"%s"' % args.installspace,
                '--only-packages', 'ros1_bridge',
                # Skip building and installing, since we just did that successfully.
                '--skip-build', '--skip-install',
                # Ignore return codes that indicate test failures;
                # they'll be picked up later in test reporting.
                '--ignore-return-codes',
            ] + (['--isolated'] if args.isolated else []) + args.ament_test_args,
                exit_on_error=False, shell=True)
            info("ament.py test returned: '{0}'".format(ret_test))
            print('# END SUBSECTION')
            if ret_test:
                return ret_test

            print('# BEGIN SUBSECTION: ament test_results')
            # Collect the test results
            ret_test_results = job.run(
                ['"%s"' % job.python, '-u', '"%s"' % ament_py, 'test_results', '"%s"' % args.buildspace],
                exit_on_error=False, shell=True
            )
            info("ament.py test_results returned: '{0}'".format(ret_test_results))
            print('# END SUBSECTION')

    # Only on Linux and OSX Python scripts have a shebang line
    if args.os in ['linux', 'osx']:
        print('# BEGIN SUBSECTION: rewrite shebang lines')
        bin_path = os.path.join(args.installspace, 'bin')
        for filename in os.listdir(bin_path):
            path = os.path.join(bin_path, filename)
            with open(path, 'rb') as h:
                content = h.read()
            shebang = b'#!%b' % job.python.encode()
            if content[0:len(shebang)] != shebang:
                continue
            print('- %s' % path)
            with open(path, 'wb') as h:
                h.write(b'#!/usr/bin/env python3')
                h.write(content[len(shebang):])
        print('# END SUBSECTION')

    print('# BEGIN SUBSECTION: create archive')
    # Remove "unnecessary" executables
    install_bin_path = os.path.join(args.installspace, 'bin')
    for filename in os.listdir(install_bin_path):
        if (
            '__rmw_' in filename or
            filename.startswith('simple_bridge') or
            filename.startswith('static_bridge')
        ):
            os.remove(os.path.join(install_bin_path, filename))

    # create an archive
    folder_name = 'ros2-' + args.os
    if args.os == 'linux' or args.os == 'osx':
        archive_path = 'ros2-package-%s.tar.bz2' % args.os

        def exclude(filename):
            if os.path.basename(filename) == 'SOURCES.txt':
                if os.path.dirname(filename).endswith('.egg-info'):
                    return True
            return False
        with tarfile.open(archive_path, 'w:bz2') as h:
            h.add(args.installspace, arcname=folder_name, exclude=exclude)
    elif args.os == 'windows':
        archive_path = 'ros2-package-windows.zip'
        # NOTE(esteve): hack to copy our custom built VS2015-compatible OpenCV DLLs
        opencv_libdir = os.path.join('c:/', 'opencv', 'build', 'x64', 'vc14', 'bin')
        for libfile in ['opencv_core2412.dll', 'opencv_highgui2412.dll']:
            libpath = os.path.join(opencv_libdir, libfile)
            shutil.copy(libpath, os.path.join(args.installspace, 'bin', libfile))
        with zipfile.ZipFile(archive_path, 'w') as zf:
            for dirname, subdirs, files in os.walk(args.installspace):
                arcname = os.path.join(
                    folder_name, os.path.relpath(dirname, start=args.installspace))
                zf.write(dirname, arcname=arcname)
                for filename in files:
                    if os.path.basename(filename) == 'SOURCES.txt':
                        if dirname.endswith('.egg-info'):
                            continue
                    filearcname = os.path.join(
                        folder_name, os.path.relpath(dirname, start=args.installspace), filename)
                    zf.write(os.path.join(dirname, filename), arcname=filearcname)
    else:
        raise RuntimeError('Unsupported operating system: %s' % args.os)
    info("created archive: '{0}'".format(archive_path))
    print('# END SUBSECTION')

    return 0

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>add arch to package name (#49)<commit_after># Copyright 2015 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import platform
import shutil
import sys
import tarfile
import zipfile

from .util import info

def build_and_test_and_package(args, job):
    print('# BEGIN SUBSECTION: ament build')
    # ignore ROS 1 bridge package for now
    ros1_bridge_path = os.path.join(args.sourcespace, 'ros2', 'ros1_bridge')
    info('ROS1 bridge path: %s' % ros1_bridge_path)
    ros1_bridge_ignore_marker = None
    if os.path.exists(ros1_bridge_path):
        ros1_bridge_ignore_marker = os.path.join(ros1_bridge_path, 'AMENT_IGNORE')
        info('ROS1 bridge path ignore file: %s' % ros1_bridge_ignore_marker)
        with open(ros1_bridge_ignore_marker, 'w'):
            pass

    ament_py = '"%s"' % os.path.join(
        '.', args.sourcespace, 'ament', 'ament_tools', 'scripts', 'ament.py'
    )
    # Now run ament build
    cmd = [
        job.python, '-u', ament_py, 'build',
        '"%s"' % args.sourcespace,
        '--build-space', '"%s"' % args.buildspace,
        '--install-space', '"%s"' % args.installspace,
    ]
    if args.isolated:
        cmd.append('--isolated')
    if args.cmake_build_type:
        cmd += ['--cmake-args', '-DCMAKE_BUILD_TYPE=' + args.cmake_build_type + ' --']
    job.run(cmd)

    if ros1_bridge_ignore_marker:
        os.remove(ros1_bridge_ignore_marker)
    print('# END SUBSECTION')

    # It only makes sense to build the bridge for Linux or OSX, since
    # ROS1 is not supported on Windows
    if args.os in ['linux', 'osx']:
        print('# BEGIN SUBSECTION: build ROS 1 bridge')
        # Now run ament build only for the bridge
        job.run([
            job.python, '-u', ament_py, 'build',
            '"%s"' % args.sourcespace,
            '--build-tests',
            '--build-space', '"%s"' % args.buildspace,
            '--install-space', '"%s"' % args.installspace,
            '--only-packages', 'ros1_bridge',
        ] + (['--isolated'] if args.isolated else []) +
            (
                ['--cmake-args', '-DCMAKE_BUILD_TYPE=' + args.cmake_build_type + ' --']
                if args.cmake_build_type else []
            ) + [
            '--make-flags', '-j1', '--'
        ])
        print('# END SUBSECTION')

        if args.test_bridge:
            print('# BEGIN SUBSECTION: install Python packages for ros1_bridge test')
            job.run(['"%s"' % job.python, '-m', 'pip', 'install', '-U', 'catkin_pkg', 'rospkg'], shell=True)
            print('# END SUBSECTION')

            print('# BEGIN SUBSECTION: test ROS 1 bridge')
            # Now run ament test only for the bridge
            ret_test = job.run([
                '"%s"' % job.python, '-u', '"%s"' % ament_py, 'test',
                '"%s"' % args.sourcespace,
                '--build-space', '"%s"' % args.buildspace,
                '--install-space', '"%s"' % args.installspace,
                '--only-packages', 'ros1_bridge',
                # Skip building and installing, since we just did that successfully.
                '--skip-build', '--skip-install',
                # Ignore return codes that indicate test failures;
                # they'll be picked up later in test reporting.
                '--ignore-return-codes',
            ] + (['--isolated'] if args.isolated else []) + args.ament_test_args,
                exit_on_error=False, shell=True)
            info("ament.py test returned: '{0}'".format(ret_test))
            print('# END SUBSECTION')
            if ret_test:
                return ret_test

            print('# BEGIN SUBSECTION: ament test_results')
            # Collect the test results
            ret_test_results = job.run(
                ['"%s"' % job.python, '-u', '"%s"' % ament_py, 'test_results', '"%s"' % args.buildspace],
                exit_on_error=False, shell=True
            )
            info("ament.py test_results returned: '{0}'".format(ret_test_results))
            print('# END SUBSECTION')

    # Only on Linux and OSX Python scripts have a shebang line
    if args.os in ['linux', 'osx']:
        print('# BEGIN SUBSECTION: rewrite shebang lines')
        bin_path = os.path.join(args.installspace, 'bin')
        for filename in os.listdir(bin_path):
            path = os.path.join(bin_path, filename)
            with open(path, 'rb') as h:
                content = h.read()
            shebang = b'#!%b' % job.python.encode()
            if content[0:len(shebang)] != shebang:
                continue
            print('- %s' % path)
            with open(path, 'wb') as h:
                h.write(b'#!/usr/bin/env python3')
                h.write(content[len(shebang):])
        print('# END SUBSECTION')

    print('# BEGIN SUBSECTION: create archive')
    # Remove "unnecessary" executables
    install_bin_path = os.path.join(args.installspace, 'bin')
    for filename in os.listdir(install_bin_path):
        if (
            '__rmw_' in filename or
            filename.startswith('simple_bridge') or
            filename.startswith('static_bridge')
        ):
            os.remove(os.path.join(install_bin_path, filename))

    # create an archive
    folder_name = 'ros2-' + args.os
    if args.os == 'linux' or args.os == 'osx':
        archive_path = 'ros2-package-%s-%s.tar.bz2' % (args.os, platform.machine())

        def exclude(filename):
            if os.path.basename(filename) == 'SOURCES.txt':
                if os.path.dirname(filename).endswith('.egg-info'):
                    return True
            return False
        with tarfile.open(archive_path, 'w:bz2') as h:
            h.add(args.installspace, arcname=folder_name, exclude=exclude)
    elif args.os == 'windows':
        archive_path = 'ros2-package-windows-%s.zip' % platform.machine()
        # NOTE(esteve): hack to copy our custom built VS2015-compatible OpenCV DLLs
        opencv_libdir = os.path.join('c:/', 'opencv', 'build', 'x64', 'vc14', 'bin')
        for libfile in ['opencv_core2412.dll', 'opencv_highgui2412.dll']:
            libpath = os.path.join(opencv_libdir, libfile)
            shutil.copy(libpath, os.path.join(args.installspace, 'bin', libfile))
        with zipfile.ZipFile(archive_path, 'w') as zf:
            for dirname, subdirs, files in os.walk(args.installspace):
                arcname = os.path.join(
                    folder_name, os.path.relpath(dirname, start=args.installspace))
                zf.write(dirname, arcname=arcname)
                for filename in files:
                    if os.path.basename(filename) == 'SOURCES.txt':
                        if dirname.endswith('.egg-info'):
                            continue
                    filearcname = os.path.join(
                        folder_name, os.path.relpath(dirname, start=args.installspace), filename)
                    zf.write(os.path.join(dirname, filename), arcname=filearcname)
    else:
        raise RuntimeError('Unsupported operating system: %s' % args.os)
    info("created archive: '{0}'".format(archive_path))
    print('# END SUBSECTION')

    return 0

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


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

import base64

from tempest.api.compute import base
from tempest import exceptions
from tempest import test

class ServerPersonalityTestJSON(base.BaseV2ComputeTest):

    @classmethod
    def setUpClass(cls):
        super(ServerPersonalityTestJSON, cls).setUpClass()
        cls.client = cls.servers_client
        cls.user_client = cls.limits_client

    @test.attr(type='gate')
    def test_personality_files_exceed_limit(self):
        # Server creation should fail if greater than the maximum allowed
        # number of files are injected into the server.
        file_contents = 'This is a test file.'
        personality = []
        max_file_limit = \
            self.user_client.get_specific_absolute_limit("maxPersonality")
        for i in range(0, int(max_file_limit) + 1):
            path = 'etc/test' + str(i) + '.txt'
            personality.append({'path': path,
                                'contents': base64.b64encode(file_contents)})
        self.assertRaises(exceptions.OverLimit, self.create_test_server,
                          personality=personality)

    @test.attr(type='gate')
    def test_can_create_server_with_max_number_personality_files(self):
        # Server should be created successfully if maximum allowed number of
        # files is injected into the server during creation.
        file_contents = 'This is a test file.'
        max_file_limit = \
            self.user_client.get_specific_absolute_limit("maxPersonality")
        person = []
        for i in range(0, int(max_file_limit)):
            path = 'etc/test' + str(i) + '.txt'
            person.append({
                'path': path,
                'contents': base64.b64encode(file_contents),
            })
        resp, server = self.create_test_server(personality=person)
        self.assertEqual('202', resp['status'])

    @test.attr(type='gate')
    def test_create_server_with_existent_personality_file(self):
        # Any existing file that match specified file will be renamed to
        # include the bak extension appended with a time stamp

        # TODO(zhikunliu): will add validations when ssh instance validation
        # re-factor is ready
        file_contents = 'This is a test file.'
        personality = [{'path': '/test.txt',
                       'contents': base64.b64encode(file_contents)}]
        resp, server = self.create_test_server(personality=personality,
                                               wait_until="ACTIVE")
        resp, image = self.create_image_from_server(server['id'],
                                                    wait_until="ACTIVE")
        resp, server = self.create_test_server(image_id=image['id'],
                                               personality=personality,
                                               wait_until="ACTIVE")
        self.assertEqual('202', resp['status'])

class ServerPersonalityTestXML(ServerPersonalityTestJSON):
    _interface = "xml"
<commit_msg>Revert "add server personality files test"<commit_after># Copyright 2012 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import base64

from tempest.api.compute import base
from tempest import exceptions
from tempest import test

class ServerPersonalityTestJSON(base.BaseV2ComputeTest):

    @classmethod
    def setUpClass(cls):
        super(ServerPersonalityTestJSON, cls).setUpClass()
        cls.client = cls.servers_client
        cls.user_client = cls.limits_client

    @test.attr(type='gate')
    def test_personality_files_exceed_limit(self):
        # Server creation should fail if greater than the maximum allowed
        # number of files are injected into the server.
        file_contents = 'This is a test file.'
        personality = []
        max_file_limit = \
            self.user_client.get_specific_absolute_limit("maxPersonality")
        for i in range(0, int(max_file_limit) + 1):
            path = 'etc/test' + str(i) + '.txt'
            personality.append({'path': path,
                                'contents': base64.b64encode(file_contents)})
        self.assertRaises(exceptions.OverLimit, self.create_test_server,
                          personality=personality)

    @test.attr(type='gate')
    def test_can_create_server_with_max_number_personality_files(self):
        # Server should be created successfully if maximum allowed number of
        # files is injected into the server during creation.
        file_contents = 'This is a test file.'
        max_file_limit = \
            self.user_client.get_specific_absolute_limit("maxPersonality")
        person = []
        for i in range(0, int(max_file_limit)):
            path = 'etc/test' + str(i) + '.txt'
            person.append({
                'path': path,
                'contents': base64.b64encode(file_contents),
            })
        resp, server = self.create_test_server(personality=person)
        self.assertEqual('202', resp['status'])

class ServerPersonalityTestXML(ServerPersonalityTestJSON):
    _interface = "xml"
<|endoftext|>


<commit_before># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe
from frappe.utils import getdate, cstr, flt, fmt_money
from frappe import _, _dict
from erpnext.accounts.utils import get_account_currency

def execute(filters=None):
	account_details = {}

	if filters and filters.get('print_in_account_currency') and \
		not filters.get('account'):
		frappe.throw(_("Select an account to print in account currency"))

	for acc in frappe.db.sql("""select name, is_group from tabAccount""", as_dict=1):
		account_details.setdefault(acc.name, acc)

	validate_filters(filters, account_details)

	validate_party(filters)

	filters = set_account_currency(filters)

	columns = get_columns(filters)

	res = get_result(filters, account_details)

	return columns, res

def validate_filters(filters, account_details):
	if not filters.get('company'):
		frappe.throw(_('{0} is mandatory').format(_('Company')))

	if filters.get("account") and not account_details.get(filters.account):
		frappe.throw(_("Account {0} does not exists").format(filters.account))

	if filters.get("account") and filters.get("group_by_account") \
			and account_details[filters.account].is_group == 0:
		frappe.throw(_("Can not filter based on Account, if grouped by Account"))

	if filters.get("voucher_no") and filters.get("group_by_voucher"):
		frappe.throw(_("Can not filter based on Voucher No, if grouped by Voucher"))

	if filters.from_date > filters.to_date:
		frappe.throw(_("From Date must be before To Date"))

def validate_party(filters):
	party_type, party = filters.get("party_type"), filters.get("party")

	if party:
		if not party_type:
			frappe.throw(_("To filter based on Party, select Party Type first"))
		elif not frappe.db.exists(party_type, party):
			frappe.throw(_("Invalid {0}: {1}").format(party_type, party))

def set_account_currency(filters):
	if not (filters.get("account") or filters.get("party")):
		return filters
	else:
		filters["company_currency"] = frappe.db.get_value("Company", filters.company, "default_currency")
		account_currency = None

		if filters.get("account"):
			account_currency = get_account_currency(filters.account)
		elif filters.get("party"):
			gle_currency = frappe.db.get_value("GL Entry", {"party_type": filters.party_type,
				"party": filters.party, "company": filters.company}, "account_currency")
			if gle_currency:
				account_currency = gle_currency
			else:
				account_currency = None if filters.party_type == "Employee" else \
					frappe.db.get_value(filters.party_type, filters.party, "default_currency")

		filters["account_currency"] = account_currency or filters.company_currency

		if filters.account_currency != filters.company_currency:
			filters["show_in_account_currency"] = 1

		return filters

def get_result(filters, account_details):
	gl_entries = get_gl_entries(filters)

	data = get_data_with_opening_closing(filters, account_details, gl_entries)

	result = get_result_as_list(data, filters)

	return result

def get_gl_entries(filters):
	select_fields = """, sum(debit_in_account_currency) as debit_in_account_currency,
		sum(credit_in_account_currency) as credit_in_account_currency""" \
		if filters.get("show_in_account_currency") else ""

	group_by_condition = "group by voucher_type, voucher_no, account, cost_center" \
		if filters.get("group_by_voucher") else "group by name"

	gl_entries = frappe.db.sql("""
		select
			posting_date, account, party_type, party,
			sum(debit) as debit, sum(credit) as credit,
			voucher_type, voucher_no, cost_center, project,
			against_voucher_type, against_voucher,
			remarks, against, is_opening {select_fields}
		from `tabGL Entry`
		where company=%(company)s {conditions}
		{group_by_condition}
		order by posting_date, account"""\
		.format(select_fields=select_fields, conditions=get_conditions(filters),
			group_by_condition=group_by_condition), filters, as_dict=1)

	return gl_entries

def get_conditions(filters):
	conditions = []
	if filters.get("account"):
		lft, rgt = frappe.db.get_value("Account", filters["account"], ["lft", "rgt"])
		conditions.append("""account in (select name from tabAccount
			where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))

	if filters.get("voucher_no"):
		conditions.append("voucher_no=%(voucher_no)s")

	if filters.get("party_type"):
		conditions.append("party_type=%(party_type)s")

	if filters.get("party"):
		conditions.append("party=%(party)s")

	if not (filters.get("account") or filters.get("party") or filters.get("group_by_account")):
		conditions.append("posting_date >=%(from_date)s")

	if filters.get("project"):
		conditions.append("project=%(project)s")

	from frappe.desk.reportview import build_match_conditions
	match_conditions = build_match_conditions("GL Entry")
	if match_conditions: conditions.append(match_conditions)

	return "and {}".format(" and ".join(conditions)) if conditions else ""

def get_data_with_opening_closing(filters, account_details, gl_entries):
	data = []
	gle_map = initialize_gle_map(gl_entries)

	totals, entries = get_accountwise_gle(filters, gl_entries, gle_map)

	# Opening for filtered account
	data.append(totals.opening)

	if filters.get("group_by_account"):
		for acc, acc_dict in gle_map.items():
			if acc_dict.entries:
				# opening
				data.append({})
				data.append(acc_dict.totals.opening)

				data += acc_dict.entries

				# totals
				data.append(acc_dict.totals.total)

				# closing
				data.append(acc_dict.totals.closing)
		data.append({})

	else:
		data += entries

	# totals
	data.append(totals.total)

	# closing
	data.append(totals.closing)

	return data

def get_totals_dict():
	def _get_debit_credit_dict(label):
		return _dict(
			account = "'{0}'".format(label),
			debit = 0.0,
			credit = 0.0,
			debit_in_account_currency = 0.0,
			credit_in_account_currency = 0.0
		)
	return _dict(
		opening = _get_debit_credit_dict(_('Opening')),
		total = _get_debit_credit_dict(_('Total')),
		closing = _get_debit_credit_dict(_('Closing (Opening + Total)'))
	)

def initialize_gle_map(gl_entries):
	gle_map = frappe._dict()
	for gle in gl_entries:
		gle_map.setdefault(gle.account, _dict(totals = get_totals_dict(), entries = []))
	return gle_map

def get_accountwise_gle(filters, gl_entries, gle_map):
	totals = get_totals_dict()
	entries = []

	def update_value_in_dict(data, key, gle):
		data[key].debit += flt(gle.debit)
		data[key].credit += flt(gle.credit)

		data[key].debit_in_account_currency += flt(gle.debit_in_account_currency)
		data[key].credit_in_account_currency += flt(gle.credit_in_account_currency)

	from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
	for gle in gl_entries:
		if gle.posting_date < from_date or cstr(gle.is_opening) == "Yes":
			update_value_in_dict(gle_map[gle.account].totals, 'opening', gle)
			update_value_in_dict(totals, 'opening', gle)

			update_value_in_dict(gle_map[gle.account].totals, 'closing', gle)
			update_value_in_dict(totals, 'closing', gle)

		elif gle.posting_date <= to_date:
			update_value_in_dict(gle_map[gle.account].totals, 'total', gle)
			update_value_in_dict(totals, 'total', gle)
			if filters.get("group_by_account"):
				gle_map[gle.account].entries.append(gle)
			else:
				entries.append(gle)

			update_value_in_dict(gle_map[gle.account].totals, 'closing', gle)
			update_value_in_dict(totals, 'closing', gle)

	return totals, entries

def get_result_as_list(data, filters):
	balance, balance_in_account_currency = 0, 0
	inv_details = get_supplier_invoice_details()

	for d in data:
		if not d.posting_date:
			balance, balance_in_account_currency = 0, 0

		balance, label = get_balance(d, balance, 'debit', 'credit')
		d['balance'] = '{0} {1}'.format(fmt_money(abs(balance)), label)

		if filters.get("show_in_account_currency"):
			balance_in_account_currency, label = get_balance(d, balance_in_account_currency,
				'debit_in_account_currency', 'credit_in_account_currency')
			d['balance_in_account_currency'] = '{0} {1}'.format(fmt_money(abs(balance_in_account_currency)), label)
		else:
			d['debit_in_account_currency'] = d.get('debit', 0)
			d['credit_in_account_currency'] = d.get('credit', 0)
			d['balance_in_account_currency'] = d.get('balance')

		d['account_currency'] = filters.account_currency
		d['bill_no'] = inv_details.get(d.against_voucher, '')

	return data

def get_supplier_invoice_details():
	inv_details = {}
	for d in frappe.db.sql(""" select name, bill_no from `tabPurchase Invoice`
		where docstatus = 1 and bill_no is not null and bill_no != '' """, as_dict=1):
		inv_details[d.name] = d.bill_no

	return inv_details

def get_balance(row, balance, debit_field, credit_field):
	balance += (row.get(debit_field, 0) -  row.get(credit_field, 0))
	label = 'DR' if balance > 0 else 'CR'

	return balance, label

def get_columns(filters):
	columns = [
		{
			"label": _("Posting Date"),
			"fieldname": "posting_date",
			"fieldtype": "Date",
			"width": 90
		},
		{
			"label": _("Account"),
			"fieldname": "account",
			"fieldtype": "Link",
			"options": "Account",
			"width": 180
		},
		{
			"label": _("Debit"),
			"fieldname": "debit",
			"fieldtype": "Float",
			"width": 100
		},
		{
			"label": _("Credit"),
			"fieldname": "credit",
			"fieldtype": "Float",
			"width": 100
		},
		{
			"label": _("Balance"),
			"fieldname": "balance",
			"fieldtype": "Data",
			"width": 100
		}
	]

	if filters.get("show_in_account_currency"):
		columns.extend([
			{
				"label": _("Debit") + " (" + filters.account_currency + ")",
				"fieldname": "debit_in_account_currency",
				"fieldtype": "Float",
				"width": 100
			},
			{
				"label": _("Credit") + " (" + filters.account_currency + ")",
				"fieldname": "credit_in_account_currency",
				"fieldtype": "Float",
				"width": 100
			},
			{
				"label": _("Balance") + " (" + filters.account_currency + ")",
				"fieldname": "balance_in_account_currency",
				"fieldtype": "Data",
				"width": 100
			}
		])

	columns.extend([
		{
			"label": _("Voucher Type"),
			"fieldname": "voucher_type",
			"width": 120
		},
		{
			"label": _("Voucher No"),
			"fieldname": "voucher_no",
			"fieldtype": "Dynamic Link",
			"options": "voucher_type",
			"width": 180
		},
		{
			"label": _("Against Account"),
			"fieldname": "against",
			"width": 120
		},
		{
			"label": _("Party Type"),
			"fieldname": "party_type",
			"width": 100
		},
		{
			"label": _("Party"),
			"fieldname": "party",
			"width": 100
		},
		{
			"label": _("Project"),
			"options": "Project",
			"fieldname": "project",
			"width": 100
		},
		{
			"label": _("Cost Center"),
			"options": "Cost Center",
			"fieldname": "cost_center",
			"width": 100
		},
		{
			"label": _("Against Voucher Type"),
			"fieldname": "against_voucher_type",
			"width": 100
		},
		{
			"label": _("Against Voucher"),
			"fieldname": "against_voucher",
			"fieldtype": "Dynamic Link",
			"options": "against_voucher_type",
			"width": 100
		},
		{
			"label": _("Supplier Invoice No"),
			"fieldname": "bill_no",
			"fieldtype": "Data",
			"width": 100
		},
		{
			"label": _("Remarks"),
			"fieldname": "remarks",
			"width": 400
		}
	])

	return columns
<commit_msg>group_by_account not displayed fix<commit_after># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe
from frappe.utils import getdate, cstr, flt, fmt_money
from frappe import _, _dict
from erpnext.accounts.utils import get_account_currency

def execute(filters=None):
	account_details = {}

	if filters and filters.get('print_in_account_currency') and \
		not filters.get('account'):
		frappe.throw(_("Select an account to print in account currency"))

	for acc in frappe.db.sql("""select name, is_group from tabAccount""", as_dict=1):
		account_details.setdefault(acc.name, acc)

	validate_filters(filters, account_details)

	validate_party(filters)

	filters = set_account_currency(filters)

	columns = get_columns(filters)

	res = get_result(filters, account_details)

	return columns, res

def validate_filters(filters, account_details):
	if not filters.get('company'):
		frappe.throw(_('{0} is mandatory').format(_('Company')))

	if filters.get("account") and not account_details.get(filters.account):
		frappe.throw(_("Account {0} does not exists").format(filters.account))

	if filters.get("account") and filters.get("group_by_account") \
			and account_details[filters.account].is_group == 0:
		frappe.throw(_("Can not filter based on Account, if grouped by Account"))

	if filters.get("voucher_no") and filters.get("group_by_voucher"):
		frappe.throw(_("Can not filter based on Voucher No, if grouped by Voucher"))

	if filters.from_date > filters.to_date:
		frappe.throw(_("From Date must be before To Date"))

def validate_party(filters):
	party_type, party = filters.get("party_type"), filters.get("party")

	if party:
		if not party_type:
			frappe.throw(_("To filter based on Party, select Party Type first"))
		elif not frappe.db.exists(party_type, party):
			frappe.throw(_("Invalid {0}: {1}").format(party_type, party))

def set_account_currency(filters):
	if not (filters.get("account") or filters.get("party")):
		return filters
	else:
		filters["company_currency"] = frappe.db.get_value("Company", filters.company, "default_currency")
		account_currency = None

		if filters.get("account"):
			account_currency = get_account_currency(filters.account)
		elif filters.get("party"):
			gle_currency = frappe.db.get_value("GL Entry", {"party_type": filters.party_type,
				"party": filters.party, "company": filters.company}, "account_currency")
			if gle_currency:
				account_currency = gle_currency
			else:
				account_currency = None if filters.party_type == "Employee" else \
					frappe.db.get_value(filters.party_type, filters.party, "default_currency")

		filters["account_currency"] = account_currency or filters.company_currency

		if filters.account_currency != filters.company_currency:
			filters["show_in_account_currency"] = 1

		return filters

def get_result(filters, account_details):
	gl_entries = get_gl_entries(filters)

	data = get_data_with_opening_closing(filters, account_details, gl_entries)

	result = get_result_as_list(data, filters)

	return result

def get_gl_entries(filters):
	select_fields = """, sum(debit_in_account_currency) as debit_in_account_currency,
		sum(credit_in_account_currency) as credit_in_account_currency""" \
		if filters.get("show_in_account_currency") else ""

	group_by_condition = "group by voucher_type, voucher_no, account, cost_center" \
		if filters.get("group_by_voucher") else "group by name"

	gl_entries = frappe.db.sql("""
		select
			posting_date, account, party_type, party,
			sum(debit) as debit, sum(credit) as credit,
			voucher_type, voucher_no, cost_center, project,
			against_voucher_type, against_voucher,
			remarks, against, is_opening {select_fields}
		from `tabGL Entry`
		where company=%(company)s {conditions}
		{group_by_condition}
		order by posting_date, account"""\
		.format(select_fields=select_fields, conditions=get_conditions(filters),
			group_by_condition=group_by_condition), filters, as_dict=1)

	return gl_entries

def get_conditions(filters):
	conditions = []
	if filters.get("account"):
		lft, rgt = frappe.db.get_value("Account", filters["account"], ["lft", "rgt"])
		conditions.append("""account in (select name from tabAccount
			where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))

	if filters.get("voucher_no"):
		conditions.append("voucher_no=%(voucher_no)s")

	if filters.get("party_type"):
		conditions.append("party_type=%(party_type)s")

	if filters.get("party"):
		conditions.append("party=%(party)s")

	if not (filters.get("account") or filters.get("party") or filters.get("group_by_account")):
		conditions.append("posting_date >=%(from_date)s")

	if filters.get("project"):
		conditions.append("project=%(project)s")

	from frappe.desk.reportview import build_match_conditions
	match_conditions = build_match_conditions("GL Entry")
	if match_conditions: conditions.append(match_conditions)

	return "and {}".format(" and ".join(conditions)) if conditions else ""

def get_data_with_opening_closing(filters, account_details, gl_entries):
	data = []
	gle_map = initialize_gle_map(gl_entries)

	totals, entries = get_accountwise_gle(filters, gl_entries, gle_map)

	# Opening for filtered account
	data.append(totals.opening)

	if filters.get("group_by_account"):
		for acc, acc_dict in gle_map.items():
			if acc_dict.entries:
				# opening
				data.append({})
				data.append(acc_dict.totals.opening)

				data += acc_dict.entries

				# totals
				data.append(acc_dict.totals.total)

				# closing
				data.append(acc_dict.totals.closing)
		data.append({})

	else:
		data += entries

	# totals
	data.append(totals.total)

	# closing
	data.append(totals.closing)

	return data

def get_totals_dict():
	def _get_debit_credit_dict(label):
		return _dict(
			account = "'{0}'".format(label),
			debit = 0.0,
			credit = 0.0,
			debit_in_account_currency = 0.0,
			credit_in_account_currency = 0.0
		)
	return _dict(
		opening = _get_debit_credit_dict(_('Opening')),
		total = _get_debit_credit_dict(_('Total')),
		closing = _get_debit_credit_dict(_('Closing (Opening + Total)'))
	)

def initialize_gle_map(gl_entries):
	gle_map = frappe._dict()
	for gle in gl_entries:
		gle_map.setdefault(gle.account, _dict(totals = get_totals_dict(), entries = []))
	return gle_map

def get_accountwise_gle(filters, gl_entries, gle_map):
	totals = get_totals_dict()
	entries = []

	def update_value_in_dict(data, key, gle):
		data[key].debit += flt(gle.debit)
		data[key].credit += flt(gle.credit)

		data[key].debit_in_account_currency += flt(gle.debit_in_account_currency)
		data[key].credit_in_account_currency += flt(gle.credit_in_account_currency)

	from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
	for gle in gl_entries:
		if gle.posting_date < from_date or cstr(gle.is_opening) == "Yes":
			update_value_in_dict(gle_map[gle.account].totals, 'opening', gle)
			update_value_in_dict(totals, 'opening', gle)

			update_value_in_dict(gle_map[gle.account].totals, 'closing', gle)
			update_value_in_dict(totals, 'closing', gle)

		elif gle.posting_date <= to_date:
			update_value_in_dict(gle_map[gle.account].totals, 'total', gle)
			update_value_in_dict(totals, 'total', gle)
			if filters.get("group_by_account"):
				gle_map[gle.account].entries.append(gle)
			else:
				entries.append(gle)

			update_value_in_dict(gle_map[gle.account].totals, 'closing', gle)
			update_value_in_dict(totals, 'closing', gle)

	return totals, entries

def get_result_as_list(data, filters):
	balance, balance_in_account_currency = 0, 0
	inv_details = get_supplier_invoice_details()

	for d in data:
		if not d.get('posting_date'):
			balance, balance_in_account_currency = 0, 0

		balance, label = get_balance(d, balance, 'debit', 'credit')
		d['balance'] = '{0} {1}'.format(fmt_money(abs(balance)), label)

		if filters.get("show_in_account_currency"):
			balance_in_account_currency, label = get_balance(d, balance_in_account_currency,
				'debit_in_account_currency', 'credit_in_account_currency')
			d['balance_in_account_currency'] = '{0} {1}'.format(fmt_money(abs(balance_in_account_currency)), label)
		else:
			d['debit_in_account_currency'] = d.get('debit', 0)
			d['credit_in_account_currency'] = d.get('credit', 0)
			d['balance_in_account_currency'] = d.get('balance')

		d['account_currency'] = filters.account_currency
		d['bill_no'] = inv_details.get(d.get('against_voucher'), '')

	return data

def get_supplier_invoice_details():
	inv_details = {}
	for d in frappe.db.sql(""" select name, bill_no from `tabPurchase Invoice`
		where docstatus = 1 and bill_no is not null and bill_no != '' """, as_dict=1):
		inv_details[d.name] = d.bill_no

	return inv_details

def get_balance(row, balance, debit_field, credit_field):
	balance += (row.get(debit_field, 0) -  row.get(credit_field, 0))
	label = 'DR' if balance > 0 else 'CR'

	return balance, label

def get_columns(filters):
	columns = [
		{
			"label": _("Posting Date"),
			"fieldname": "posting_date",
			"fieldtype": "Date",
			"width": 90
		},
		{
			"label": _("Account"),
			"fieldname": "account",
			"fieldtype": "Link",
			"options": "Account",
			"width": 180
		},
		{
			"label": _("Debit"),
			"fieldname": "debit",
			"fieldtype": "Float",
			"width": 100
		},
		{
			"label": _("Credit"),
			"fieldname": "credit",
			"fieldtype": "Float",
			"width": 100
		},
		{
			"label": _("Balance"),
			"fieldname": "balance",
			"fieldtype": "Data",
			"width": 100
		}
	]

	if filters.get("show_in_account_currency"):
		columns.extend([
			{
				"label": _("Debit") + " (" + filters.account_currency + ")",
				"fieldname": "debit_in_account_currency",
				"fieldtype": "Float",
				"width": 100
			},
			{
				"label": _("Credit") + " (" + filters.account_currency + ")",
				"fieldname": "credit_in_account_currency",
				"fieldtype": "Float",
				"width": 100
			},
			{
				"label": _("Balance") + " (" + filters.account_currency + ")",
				"fieldname": "balance_in_account_currency",
				"fieldtype": "Data",
				"width": 100
			}
		])

	columns.extend([
		{
			"label": _("Voucher Type"),
			"fieldname": "voucher_type",
			"width": 120
		},
		{
			"label": _("Voucher No"),
			"fieldname": "voucher_no",
			"fieldtype": "Dynamic Link",
			"options": "voucher_type",
			"width": 180
		},
		{
			"label": _("Against Account"),
			"fieldname": "against",
			"width": 120
		},
		{
			"label": _("Party Type"),
			"fieldname": "party_type",
			"width": 100
		},
		{
			"label": _("Party"),
			"fieldname": "party",
			"width": 100
		},
		{
			"label": _("Project"),
			"options": "Project",
			"fieldname": "project",
			"width": 100
		},
		{
			"label": _("Cost Center"),
			"options": "Cost Center",
			"fieldname": "cost_center",
			"width": 100
		},
		{
			"label": _("Against Voucher Type"),
			"fieldname": "against_voucher_type",
			"width": 100
		},
		{
			"label": _("Against Voucher"),
			"fieldname": "against_voucher",
			"fieldtype": "Dynamic Link",
			"options": "against_voucher_type",
			"width": 100
		},
		{
			"label": _("Supplier Invoice No"),
			"fieldname": "bill_no",
			"fieldtype": "Data",
			"width": 100
		},
		{
			"label": _("Remarks"),
			"fieldname": "remarks",
			"width": 400
		}
	])

	return columns
<|endoftext|>


<commit_before># Copyright 2014, 2015 The ODL development group
#
# This file is part of ODL.
#
# ODL is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ODL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ODL.  If not, see <http://www.gnu.org/licenses/>.

# Imports for common Python 2/3 codebase
from __future__ import print_function, division, absolute_import
from future import standard_library
standard_library.install_aliases()
from builtins import str, super

# External module imports
import pytest
import numpy as np

# ODL imports
import odl
from odl.operator.utility import matrix_representation
from odl.util.testutils import almost_equal

class MultiplyOp(odl.Operator):

    """Multiply with matrix.
    """

    def __init__(self, matrix, domain=None, range=None):
        domain = (odl.Rn(matrix.shape[1])
                  if domain is None else domain)
        range = (odl.Rn(matrix.shape[0])
                 if range is None else range)
        self.matrix = matrix

        super().__init__(domain, range, linear=True)

    def _apply(self, rhs, out):
        np.dot(self.matrix, rhs.data, out=out.data)

    @property
    def adjoint(self):
        return MultiplyOp(self.matrix.T, self.range, self.domain)

def test_matrix_representation():
    # Verify that the matrix representation function returns the correct matrix

    A = np.random.rand(3, 3)

    Aop = MultiplyOp(A)

    the_matrix = matrix_representation(Aop)

    assert almost_equal(np.sum(np.abs(A - the_matrix)), 1e-6)

if __name__ == '__main__':
    pytest.main(str(__file__.replace('\\', '/')) + ' -v')
<commit_msg>BUG: Fix bug due to the move.<commit_after># Copyright 2014, 2015 The ODL development group
#
# This file is part of ODL.
#
# ODL is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ODL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ODL.  If not, see <http://www.gnu.org/licenses/>.

# Imports for common Python 2/3 codebase
from __future__ import print_function, division, absolute_import
from future import standard_library
standard_library.install_aliases()
from builtins import str, super

# External module imports
import pytest
import numpy as np

# ODL imports
import odl
from odl.discr.utility import matrix_representation
from odl.util.testutils import almost_equal

class MultiplyOp(odl.Operator):

    """Multiply with matrix.
    """

    def __init__(self, matrix, domain=None, range=None):
        domain = (odl.Rn(matrix.shape[1])
                  if domain is None else domain)
        range = (odl.Rn(matrix.shape[0])
                 if range is None else range)
        self.matrix = matrix

        super().__init__(domain, range, linear=True)

    def _apply(self, rhs, out):
        np.dot(self.matrix, rhs.data, out=out.data)

    @property
    def adjoint(self):
        return MultiplyOp(self.matrix.T, self.range, self.domain)

def test_matrix_representation():
    # Verify that the matrix representation function returns the correct matrix

    A = np.random.rand(3, 3)

    Aop = MultiplyOp(A)

    the_matrix = matrix_representation(Aop)

    assert almost_equal(np.sum(np.abs(A - the_matrix)), 1e-6)

if __name__ == '__main__':
    pytest.main(str(__file__.replace('\\', '/')) + ' -v')
<|endoftext|>