<commit_before>from collections import OrderedDict
from contextlib import contextmanager
from os.path import normpath
from pprint import pformat, saferepr

from django import http
from django.conf.urls import url
from django.core import signing
from django.db.models.query import QuerySet, RawQuerySet
from django.template import RequestContext, Template
from django.test.signals import template_rendered
from django.test.utils import instrumented_test_render
from django.utils.translation import gettext_lazy as _

from debug_toolbar.panels import Panel
from debug_toolbar.panels.sql.tracking import SQLQueryTriggered, recording
from debug_toolbar.panels.templates import views

# Monkey-patch to enable the template_rendered signal. The receiver returns
# immediately when the panel is disabled to keep the overhead small.

# Code taken and adapted from Simon Willison and Django Snippets:
# https://www.djangosnippets.org/snippets/766/

if Template._render != instrumented_test_render:
    Template.original_render = Template._render
    Template._render = instrumented_test_render

# Monkey-patch to store items added by template context processors. The
# overhead is sufficiently small to justify enabling it unconditionally.

@contextmanager
def _request_context_bind_template(self, template):
    if self.template is not None:
        raise RuntimeError("Context is already bound to a template")

    self.template = template
    # Set context processors according to the template engine's settings.
    processors = template.engine.template_context_processors + self._processors
    self.context_processors = OrderedDict()
    updates = {}
    for processor in processors:
        name = "{}.{}".format(processor.__module__, processor.__name__)
        context = processor(self.request)
        self.context_processors[name] = context
        updates.update(context)
    self.dicts[self._processors_index] = updates

    try:
        yield
    finally:
        self.template = None
        # Unset context processors.
        self.dicts[self._processors_index] = {}

RequestContext.bind_template = _request_context_bind_template

class TemplatesPanel(Panel):
    """
    A panel that lists all templates used during processing of a response.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.templates = []
        # An associated list of dictionaries and their prettified
        # representation.
        self.pformat_layers = []

    def _store_template_info(self, sender, **kwargs):
        template, context = kwargs["template"], kwargs["context"]

        # Skip templates that we are generating through the debug toolbar.
        is_debug_toolbar_template = isinstance(template.name, str) and (
            template.name.startswith("debug_toolbar/")
            or template.name.startswith(
                tuple(self.toolbar.config["SKIP_TEMPLATE_PREFIXES"])
            )
        )
        if is_debug_toolbar_template:
            return

        context_list = []
        for context_layer in context.dicts:
            if context_layer:
                # Check if the layer is in the cache.
                key_values = sorted(context_layer.items())
                pformatted = None
                for _key_values, _pformatted in self.pformat_layers:
                    if _key_values == key_values:
                        pformatted = _pformatted
                        break

                if pformatted is None:
                    temp_layer = {}
                    for key, value in context_layer.items():
                        # Replace any request elements - they have a large
                        # unicode representation and the request data is
                        # already made available from the Request panel.
                        if isinstance(value, http.HttpRequest):
                            temp_layer[key] = "<<request>>"
                        # Replace the debugging sql_queries element. The SQL
                        # data is already made available from the SQL panel.
                        elif key == "sql_queries" and isinstance(value, list):
                            temp_layer[key] = "<<sql_queries>>"
                        # Replace LANGUAGES, which is available in i18n context
                        # processor
                        elif key == "LANGUAGES" and isinstance(value, tuple):
                            temp_layer[key] = "<<languages>>"
                        # QuerySet would trigger the database: user can run the
                        # query from SQL Panel
                        elif isinstance(value, (QuerySet, RawQuerySet)):
                            temp_layer[key] = "<<{} of {}>>".format(
                                value.__class__.__name__.lower(),
                                value.model._meta.label,
                            )
                        else:
                            recording(False)
                            try:
                                saferepr(value)  # this MAY trigger a db query
                            except SQLQueryTriggered:
                                temp_layer[key] = "<<triggers database query>>"
                            except UnicodeEncodeError:
                                temp_layer[key] = "<<unicode encode error>>"
                            except Exception:
                                temp_layer[key] = "<<unhandled exception>>"
                            else:
                                temp_layer[key] = value
                            finally:
                                recording(True)
                    pformatted = pformat(temp_layer)
                    self.pformat_layers.append((key_values, pformatted))
                context_list.append(pformatted)

        kwargs["context"] = context_list
        kwargs["context_processors"] = getattr(context, "context_processors", None)
        self.templates.append(kwargs)

    # Implement the Panel API

    nav_title = _("Templates")

    @property
    def title(self):
        num_templates = len(self.templates)
        return _("Templates (%(num_templates)s rendered)") % {
            "num_templates": num_templates
        }

    @property
    def nav_subtitle(self):
        if self.templates:
            return self.templates[0]["template"].name
        return ""

    template = "debug_toolbar/panels/templates.html"

    @classmethod
    def get_urls(cls):
        return [
            url(r"^template_source/$", views.template_source, name="template_source")
        ]

    def enable_instrumentation(self):
        template_rendered.connect(self._store_template_info)

    def disable_instrumentation(self):
        template_rendered.disconnect(self._store_template_info)

    def generate_stats(self, request, response):
        template_context = []
        for template_data in self.templates:
            info = {}
            # Clean up some info about templates
            template = template_data.get("template", None)
            if hasattr(template, "origin") and template.origin and template.origin.name:
                template.origin_name = template.origin.name
                template.origin_hash = signing.dumps(template.origin.name)
            else:
                template.origin_name = _("No origin")
                template.origin_hash = ""
            info["template"] = template
            # Clean up context for better readability
            if self.toolbar.config["SHOW_TEMPLATE_CONTEXT"]:
                context_list = template_data.get("context", [])
                info["context"] = "\n".join(context_list)
            template_context.append(info)

        # Fetch context_processors/template_dirs from any template
        if self.templates:
            context_processors = self.templates[0]["context_processors"]
            template = self.templates[0]["template"]
            # django templates have the 'engine' attribute, while jinja
            # templates use 'backend'
            engine_backend = getattr(template, "engine", None) or getattr(
                template, "backend"
            )
            template_dirs = engine_backend.dirs
        else:
            context_processors = None
            template_dirs = []

        self.record_stats(
            {
                "templates": template_context,
                "template_dirs": [normpath(x) for x in template_dirs],
                "context_processors": context_processors,
            }
        )
<commit_msg>Revert "Remove unnecessary guard"<commit_after>from collections import OrderedDict
from contextlib import contextmanager
from os.path import normpath
from pprint import pformat, saferepr

from django import http
from django.conf.urls import url
from django.core import signing
from django.db.models.query import QuerySet, RawQuerySet
from django.template import RequestContext, Template
from django.test.signals import template_rendered
from django.test.utils import instrumented_test_render
from django.utils.translation import gettext_lazy as _

from debug_toolbar.panels import Panel
from debug_toolbar.panels.sql.tracking import SQLQueryTriggered, recording
from debug_toolbar.panels.templates import views

# Monkey-patch to enable the template_rendered signal. The receiver returns
# immediately when the panel is disabled to keep the overhead small.

# Code taken and adapted from Simon Willison and Django Snippets:
# https://www.djangosnippets.org/snippets/766/

if Template._render != instrumented_test_render:
    Template.original_render = Template._render
    Template._render = instrumented_test_render

# Monkey-patch to store items added by template context processors. The
# overhead is sufficiently small to justify enabling it unconditionally.

@contextmanager
def _request_context_bind_template(self, template):
    if self.template is not None:
        raise RuntimeError("Context is already bound to a template")

    self.template = template
    # Set context processors according to the template engine's settings.
    processors = template.engine.template_context_processors + self._processors
    self.context_processors = OrderedDict()
    updates = {}
    for processor in processors:
        name = "{}.{}".format(processor.__module__, processor.__name__)
        context = processor(self.request)
        self.context_processors[name] = context
        updates.update(context)
    self.dicts[self._processors_index] = updates

    try:
        yield
    finally:
        self.template = None
        # Unset context processors.
        self.dicts[self._processors_index] = {}

RequestContext.bind_template = _request_context_bind_template

class TemplatesPanel(Panel):
    """
    A panel that lists all templates used during processing of a response.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.templates = []
        # An associated list of dictionaries and their prettified
        # representation.
        self.pformat_layers = []

    def _store_template_info(self, sender, **kwargs):
        template, context = kwargs["template"], kwargs["context"]

        # Skip templates that we are generating through the debug toolbar.
        is_debug_toolbar_template = isinstance(template.name, str) and (
            template.name.startswith("debug_toolbar/")
            or template.name.startswith(
                tuple(self.toolbar.config["SKIP_TEMPLATE_PREFIXES"])
            )
        )
        if is_debug_toolbar_template:
            return

        context_list = []
        for context_layer in context.dicts:
            if hasattr(context_layer, "items") and context_layer:
                # Check if the layer is in the cache.
                key_values = sorted(context_layer.items())
                pformatted = None
                for _key_values, _pformatted in self.pformat_layers:
                    if _key_values == key_values:
                        pformatted = _pformatted
                        break

                if pformatted is None:
                    temp_layer = {}
                    for key, value in context_layer.items():
                        # Replace any request elements - they have a large
                        # unicode representation and the request data is
                        # already made available from the Request panel.
                        if isinstance(value, http.HttpRequest):
                            temp_layer[key] = "<<request>>"
                        # Replace the debugging sql_queries element. The SQL
                        # data is already made available from the SQL panel.
                        elif key == "sql_queries" and isinstance(value, list):
                            temp_layer[key] = "<<sql_queries>>"
                        # Replace LANGUAGES, which is available in i18n context
                        # processor
                        elif key == "LANGUAGES" and isinstance(value, tuple):
                            temp_layer[key] = "<<languages>>"
                        # QuerySet would trigger the database: user can run the
                        # query from SQL Panel
                        elif isinstance(value, (QuerySet, RawQuerySet)):
                            temp_layer[key] = "<<{} of {}>>".format(
                                value.__class__.__name__.lower(),
                                value.model._meta.label,
                            )
                        else:
                            recording(False)
                            try:
                                saferepr(value)  # this MAY trigger a db query
                            except SQLQueryTriggered:
                                temp_layer[key] = "<<triggers database query>>"
                            except UnicodeEncodeError:
                                temp_layer[key] = "<<unicode encode error>>"
                            except Exception:
                                temp_layer[key] = "<<unhandled exception>>"
                            else:
                                temp_layer[key] = value
                            finally:
                                recording(True)
                    pformatted = pformat(temp_layer)
                    self.pformat_layers.append((key_values, pformatted))
                context_list.append(pformatted)

        kwargs["context"] = context_list
        kwargs["context_processors"] = getattr(context, "context_processors", None)
        self.templates.append(kwargs)

    # Implement the Panel API

    nav_title = _("Templates")

    @property
    def title(self):
        num_templates = len(self.templates)
        return _("Templates (%(num_templates)s rendered)") % {
            "num_templates": num_templates
        }

    @property
    def nav_subtitle(self):
        if self.templates:
            return self.templates[0]["template"].name
        return ""

    template = "debug_toolbar/panels/templates.html"

    @classmethod
    def get_urls(cls):
        return [
            url(r"^template_source/$", views.template_source, name="template_source")
        ]

    def enable_instrumentation(self):
        template_rendered.connect(self._store_template_info)

    def disable_instrumentation(self):
        template_rendered.disconnect(self._store_template_info)

    def generate_stats(self, request, response):
        template_context = []
        for template_data in self.templates:
            info = {}
            # Clean up some info about templates
            template = template_data.get("template", None)
            if hasattr(template, "origin") and template.origin and template.origin.name:
                template.origin_name = template.origin.name
                template.origin_hash = signing.dumps(template.origin.name)
            else:
                template.origin_name = _("No origin")
                template.origin_hash = ""
            info["template"] = template
            # Clean up context for better readability
            if self.toolbar.config["SHOW_TEMPLATE_CONTEXT"]:
                context_list = template_data.get("context", [])
                info["context"] = "\n".join(context_list)
            template_context.append(info)

        # Fetch context_processors/template_dirs from any template
        if self.templates:
            context_processors = self.templates[0]["context_processors"]
            template = self.templates[0]["template"]
            # django templates have the 'engine' attribute, while jinja
            # templates use 'backend'
            engine_backend = getattr(template, "engine", None) or getattr(
                template, "backend"
            )
            template_dirs = engine_backend.dirs
        else:
            context_processors = None
            template_dirs = []

        self.record_stats(
            {
                "templates": template_context,
                "template_dirs": [normpath(x) for x in template_dirs],
                "context_processors": context_processors,
            }
        )
<|endoftext|>


<commit_before>from __future__ import absolute_import, unicode_literals

from django.http import HttpResponseBadRequest
from django.conf import settings
from django.shortcuts import render_to_response
from django.template import TemplateDoesNotExist
from django.template.loader import find_template_loader
from django.utils.safestring import mark_safe

def template_source(request):
    """
    Return the source of a template, syntax-highlighted by Pygments if
    it's available.
    """
    template_name = request.GET.get('template', None)
    if template_name is None:
        return HttpResponseBadRequest('"template" key is required')

    loaders = []
    for loader_name in settings.TEMPLATE_LOADERS:
        loader = find_template_loader(loader_name)
        if loader is not None:
            # When the loader has loaders associated with it,
            # append those loaders to the list. This occurs with
            # django.template.loaders.cached.Loader
            if hasattr(loader, 'loaders'):
                loaders += loader.loaders
            else:
                loaders.append(loader)
    for loader in loaders:
        try:
            source, display_name = loader.load_template_source(template_name)
            break
        except TemplateDoesNotExist:
            source = "Template Does Not Exist: %s" % (template_name,)

    try:
        from pygments import highlight
        from pygments.lexers import HtmlDjangoLexer
        from pygments.formatters import HtmlFormatter

        source = highlight(source, HtmlDjangoLexer(), HtmlFormatter())
        source = mark_safe(source)
        source.pygmentized = True
    except ImportError:
        pass

    # Using render_to_response avoids running global context processors.
    return render_to_response('debug_toolbar/panels/template_source.html', {
        'source': source,
        'template_name': template_name
    })
<commit_msg>fix find_template_loader move on django 1.8<commit_after>from __future__ import absolute_import, unicode_literals

from django.http import HttpResponseBadRequest
from django.conf import settings
from django.shortcuts import render_to_response
from django.template import TemplateDoesNotExist
try:
    from django.template.loaders.utils import find_template_loader
except ImportError:  # django < 1.8
    from django.template.loader import find_template_loader
from django.utils.safestring import mark_safe

def template_source(request):
    """
    Return the source of a template, syntax-highlighted by Pygments if
    it's available.
    """
    template_name = request.GET.get('template', None)
    if template_name is None:
        return HttpResponseBadRequest('"template" key is required')

    loaders = []
    for loader_name in settings.TEMPLATE_LOADERS:
        loader = find_template_loader(loader_name)
        if loader is not None:
            # When the loader has loaders associated with it,
            # append those loaders to the list. This occurs with
            # django.template.loaders.cached.Loader
            if hasattr(loader, 'loaders'):
                loaders += loader.loaders
            else:
                loaders.append(loader)
    for loader in loaders:
        try:
            source, display_name = loader.load_template_source(template_name)
            break
        except TemplateDoesNotExist:
            source = "Template Does Not Exist: %s" % (template_name,)

    try:
        from pygments import highlight
        from pygments.lexers import HtmlDjangoLexer
        from pygments.formatters import HtmlFormatter

        source = highlight(source, HtmlDjangoLexer(), HtmlFormatter())
        source = mark_safe(source)
        source.pygmentized = True
    except ImportError:
        pass

    # Using render_to_response avoids running global context processors.
    return render_to_response('debug_toolbar/panels/template_source.html', {
        'source': source,
        'template_name': template_name
    })
<|endoftext|>


<commit_before>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2018 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_graql():
     git_repository(
         name = "graknlabs_graql",
         remote = "https://github.com/graknlabs/graql",
         commit = "db95f9a43cdcd46855a88460ab182dcac8750fcd",
     )

def graknlabs_client_java():
     git_repository(
         name = "graknlabs_client_java",
         remote = "https://github.com/graknlabs/client-java",
         commit = "d9091e05e61a592752da5bc487af5ed2ba518695",
     )

def graknlabs_benchmark():
    git_repository(
        name = "graknlabs_benchmark",
        remote = "https://github.com/graknlabs/benchmark.git",
        commit = "ceb5a2ebb71ee526d788fb4b17a104a6669d4b70" # keep in sync with protocol changes
    )

def graknlabs_build_tools():
    git_repository(
        name = "graknlabs_build_tools",
        remote = "https://github.com/graknlabs/build-tools",
        commit = "7667b49963eb7d4ab6d23075f54c84291f89fa70", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
    )
<commit_msg>//ci:sync-dependencies: graknlabs/bazel-distribution@fc63822<commit_after>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2018 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_graql():
     git_repository(
         name = "graknlabs_graql",
         remote = "https://github.com/graknlabs/graql",
         commit = "db95f9a43cdcd46855a88460ab182dcac8750fcd",
     )

def graknlabs_client_java():
     git_repository(
         name = "graknlabs_client_java",
         remote = "https://github.com/graknlabs/client-java",
         commit = "d9091e05e61a592752da5bc487af5ed2ba518695",
     )

def graknlabs_benchmark():
    git_repository(
        name = "graknlabs_benchmark",
        remote = "https://github.com/graknlabs/benchmark.git",
        commit = "ceb5a2ebb71ee526d788fb4b17a104a6669d4b70" # keep in sync with protocol changes
    )

def graknlabs_build_tools():
    git_repository(
        name = "graknlabs_build_tools",
        remote = "https://github.com/graknlabs/build-tools",
        commit = "e1dec9aa71a63d0043c5d5375b6ea8032899cf4a", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
    )
<|endoftext|>


<commit_before>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2018 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_graql():
     git_repository(
         name = "graknlabs_graql",
         remote = "https://github.com/graknlabs/graql",
         commit = "3aca6d63b7c5b287c8f473aaf47646b55a7ba0f4", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_graql
     )

def graknlabs_client_java():
     git_repository(
         name = "graknlabs_client_java",
         remote = "https://github.com/graknlabs/client-java",
         commit = "a86ad5b10f2ccb1c9b46c561040188761fd31052",
     )

def graknlabs_build_tools():
     git_repository(
         name = "graknlabs_build_tools",
         remote = "https://github.com/graknlabs/build-tools",
         commit = "c7bd536e6903e61baff4af074912f1de8340a11d", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
     )<commit_msg>Update @graknlabs_build_tools dependency to latest 'master' branch<commit_after>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2018 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_graql():
     git_repository(
         name = "graknlabs_graql",
         remote = "https://github.com/graknlabs/graql",
         commit = "3aca6d63b7c5b287c8f473aaf47646b55a7ba0f4", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_graql
     )

def graknlabs_client_java():
     git_repository(
         name = "graknlabs_client_java",
         remote = "https://github.com/graknlabs/client-java",
         commit = "a86ad5b10f2ccb1c9b46c561040188761fd31052",
     )

def graknlabs_build_tools():
     git_repository(
         name = "graknlabs_build_tools",
         remote = "https://github.com/graknlabs/build-tools",
         commit = "a0eab084dc7972398a8244a9578b4712328b899a", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
     )<|endoftext|>


<commit_before>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2019 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_build_tools():
    git_repository(
        name = "graknlabs_build_tools",
        remote = "https://github.com/graknlabs/build-tools",
        commit = "f17e730b628ff9957cbd31bfa3e1780857ac86c9", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
    )

def graknlabs_common():
    git_repository(
        name = "graknlabs_common",
        remote = "https://github.com/graknlabs/common",
        commit = "80e7505bd01afc6e048891e0b6f88ab80239f3f0", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_common
    )

def graknlabs_graql():
     git_repository(
         name = "graknlabs_graql",
         remote = "https://github.com/graknlabs/graql",
         commit = "5bf8ea37b0555abb16e66c8d1e90e980347ac46e", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_graql
     )

def graknlabs_protocol():
    git_repository(
        name = "graknlabs_protocol",
        remote = "https://github.com/graknlabs/protocol",
        commit = "143b06e0559c8d52323e694fafd3a1b4169c969b", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
    )

def graknlabs_client_java():
     git_repository(
         name = "graknlabs_client_java",
         remote = "https://github.com/flyingsilverfin/client-java",
         commit = "b848620c24f1905b85e8f307cfc0a9b5dcd5cd74",
     )

def graknlabs_console():
    git_repository(
        name = "graknlabs_console",
        remote = "https://github.com/graknlabs/console",
        commit = "4ef9b54c5fe9c0b24a62dccf2ae193e6d0a40ae4"
    )

def graknlabs_benchmark():
    git_repository(
        name = "graknlabs_benchmark",
        remote = "https://github.com/graknlabs/benchmark.git",
        commit = "78869c68a2b6073917f7d6ee085ffd0b0d6a29b0" # keep in sync with protocol changes
    )
<commit_msg>//ci:sync-dependencies: graknlabs/bazel-distribution@d10c8c5<commit_after>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2019 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_build_tools():
    git_repository(
        name = "graknlabs_build_tools",
        remote = "https://github.com/graknlabs/build-tools",
        commit = "d8e266644cc5c1d44dc17515069b1a6b70c98c44", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
    )

def graknlabs_common():
    git_repository(
        name = "graknlabs_common",
        remote = "https://github.com/graknlabs/common",
        commit = "63592883dbd2dbfd730b12676a6ee18f8058b00b", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_common
    )

def graknlabs_graql():
     git_repository(
         name = "graknlabs_graql",
         remote = "https://github.com/graknlabs/graql",
         commit = "1e2a90f29b169de1fd014db1ac7df354b383b57a", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_graql
     )

def graknlabs_protocol():
    git_repository(
        name = "graknlabs_protocol",
        remote = "https://github.com/graknlabs/protocol",
        commit = "246139d83ad8d3b5b3f37e2bf26d3b56fad4d8bc", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
    )

def graknlabs_client_java():
     git_repository(
         name = "graknlabs_client_java",
         remote = "https://github.com/flyingsilverfin/client-java",
         commit = "b848620c24f1905b85e8f307cfc0a9b5dcd5cd74",
     )

def graknlabs_console():
    git_repository(
        name = "graknlabs_console",
        remote = "https://github.com/graknlabs/console",
        commit = "4ef9b54c5fe9c0b24a62dccf2ae193e6d0a40ae4"
    )

def graknlabs_benchmark():
    git_repository(
        name = "graknlabs_benchmark",
        remote = "https://github.com/graknlabs/benchmark.git",
        commit = "78869c68a2b6073917f7d6ee085ffd0b0d6a29b0" # keep in sync with protocol changes
    )
<|endoftext|>


<commit_before>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2019 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_build_tools():
    git_repository(
        name = "graknlabs_build_tools",
        remote = "https://github.com/graknlabs/build-tools",
        commit = "b731eae85c90128878de58848a1cd8fe516ce58e",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
    )

def graknlabs_common():
    git_repository(
        name = "graknlabs_common",
        remote = "https://github.com/graknlabs/common",
        tag = "0.2.2",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_common
    )

def graknlabs_graql():
    git_repository(
        name = "graknlabs_graql",
        remote = "https://github.com/graknlabs/graql",
        tag = "1.0.6",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_graql
    )

def graknlabs_protocol():
    git_repository(
        name = "graknlabs_protocol",
        remote = "https://github.com/graknlabs/protocol",
        tag = "1.0.4",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
    )

def graknlabs_client_java():
    git_repository(
        name = "graknlabs_client_java",
        remote = "https://github.com/graknlabs/client-java",
        tag = "1.6.2",
    )

def graknlabs_console():
    git_repository(
        name = "graknlabs_console",
        remote = "https://github.com/graknlabs/console",
        tag = "1.0.3",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_console
    )

def graknlabs_benchmark():
    git_repository(
        name = "graknlabs_benchmark",
        remote = "https://github.com/graknlabs/benchmark.git",
        commit = "78869c68a2b6073917f7d6ee085ffd0b0d6a29b0",  # keep in sync with protocol changes
    )

def graknlabs_simulation():
    git_repository(
        name = "graknlabs_simulation",
        remote = "https://github.com/graknlabs/simulation",
        commit = "c9a40339ca314b7ae46a1f20e268fd6b3a40e026",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_simulation
    )

def graknlabs_theory():
    git_repository(
        name = "graknlabs_verification",
        remote = "git@github.com:graknlabs/verification.git",
        commit = "38422edb87643b95fecbf560ef92f84ab47c693e",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_verification
    )
<commit_msg>Updated simulation dependency (#5608)<commit_after>#
# GRAKN.AI - THE KNOWLEDGE GRAPH
# Copyright (C) 2019 Grakn Labs Ltd
#
# 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 <https://www.gnu.org/licenses/>.
#

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def graknlabs_build_tools():
    git_repository(
        name = "graknlabs_build_tools",
        remote = "https://github.com/graknlabs/build-tools",
        commit = "b731eae85c90128878de58848a1cd8fe516ce58e",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_build_tools
    )

def graknlabs_common():
    git_repository(
        name = "graknlabs_common",
        remote = "https://github.com/graknlabs/common",
        tag = "0.2.2",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_common
    )

def graknlabs_graql():
    git_repository(
        name = "graknlabs_graql",
        remote = "https://github.com/graknlabs/graql",
        tag = "1.0.6",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_graql
    )

def graknlabs_protocol():
    git_repository(
        name = "graknlabs_protocol",
        remote = "https://github.com/graknlabs/protocol",
        tag = "1.0.4",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_protocol
    )

def graknlabs_client_java():
    git_repository(
        name = "graknlabs_client_java",
        remote = "https://github.com/graknlabs/client-java",
        tag = "1.6.2",
    )

def graknlabs_console():
    git_repository(
        name = "graknlabs_console",
        remote = "https://github.com/graknlabs/console",
        tag = "1.0.3",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_console
    )

def graknlabs_benchmark():
    git_repository(
        name = "graknlabs_benchmark",
        remote = "https://github.com/graknlabs/benchmark.git",
        commit = "78869c68a2b6073917f7d6ee085ffd0b0d6a29b0",  # keep in sync with protocol changes
    )

def graknlabs_simulation():
    git_repository(
        name = "graknlabs_simulation",
        remote = "https://github.com/graknlabs/simulation",
        commit = "ba210f264ca219c50318d837508ac0957f938375",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_simulation
    )

def graknlabs_theory():
    git_repository(
        name = "graknlabs_verification",
        remote = "git@github.com:graknlabs/verification.git",
        commit = "38422edb87643b95fecbf560ef92f84ab47c693e",  # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_verification
    )
<|endoftext|>


<commit_before>from datetime import datetime, timedelta
from depot.helpers import get_depot_if_allowed, extract_item_quantities
from django.shortcuts import render
from django.views import View
from rental.availability import Availability

class DepotCreateRentalView(View):
    """
    Show a form to create a new rental for the given depot

    :author: Benedikt Seidl
    """

    def get(self, request, depot_id):
        depot = get_depot_if_allowed(depot_id, request.user)

        # configure time frame
        start_date, return_date = self.get_start_return_date(request.GET)

        item_list = depot.visible_items(request.user)

        availability = Availability(start_date, return_date, depot_id)

        item_availability_intervals = availability.get_availability_intervals_list(item_list)

        availability_data = []
        for item, intervals in item_availability_intervals:
            availability_data.append((
                item,
                self.get_chart_data(intervals),
                availability.get_minimum_availability(intervals)
            ))

        errors = request.session.pop('errors', None)
        data = request.session.pop('data', {})

        return render(request, 'depot/create-rental.html', {
            'depot': depot,
            'show_visibility': depot.show_private_items(request.user),
            'availability_data': availability_data,
            'errors': errors,
            'data': data,
            'item_quantities': extract_item_quantities(data),
            'start_date': start_date,
            'return_date': return_date,
            'start_date_formatted': start_date.strftime('%Y-%m-%d %H:%M'),
            'return_date_formatted': return_date.strftime('%Y-%m-%d %H:%M')
        })

    def get_start_return_date(self, data):
        """
        Return the start and return dates from the request

        If required data is missing or invalid, default values are returned.
        """

        try:
            start_date = datetime.strptime(data.get('start_date'), '%Y-%m-%d %H:%M')
        except:
            start_date = datetime.now() + timedelta(days=1)

        try:
            return_date = datetime.strptime(data.get('return_date'), '%Y-%m-%d %H:%M')
        except:
            return_date = start_date + timedelta(days=3)

        return (start_date, max(start_date, return_date))

    def get_chart_data(self, intervals):
        """
        Generate the data the JavaScript can render
        """

        data = []

        for begin, end, availability in intervals:
            data.append({
                "x": begin.isoformat(),
                "y": availability
            })
            data.append({
                "x": end.isoformat(),
                "y": availability
            })

        return data
<commit_msg>Show availability statistics for greater time frame<commit_after>from datetime import datetime, timedelta
from depot.helpers import get_depot_if_allowed, extract_item_quantities
from django.shortcuts import render
from django.views import View
from rental.availability import Availability

class DepotCreateRentalView(View):
    """
    Show a form to create a new rental for the given depot

    :author: Benedikt Seidl
    """

    def get(self, request, depot_id):
        depot = get_depot_if_allowed(depot_id, request.user)

        # configure time frame
        start_date, return_date = self.get_start_return_date(request.GET)

        item_list = depot.visible_items(request.user)

        availability = Availability(
            datetime.combine(start_date.date() - timedelta(days=1), datetime.min.time()),
            datetime.combine(return_date.date() + timedelta(days=1), datetime.min.time()),
            depot_id
        )

        item_availability_intervals = availability.get_availability_intervals_list(item_list)

        availability_data = []
        for item, intervals in item_availability_intervals:
            availability_data.append((
                item,
                self.get_chart_data(intervals),
                availability.get_minimum_availability(intervals)
            ))

        errors = request.session.pop('errors', None)
        data = request.session.pop('data', {})

        return render(request, 'depot/create-rental.html', {
            'depot': depot,
            'show_visibility': depot.show_private_items(request.user),
            'availability_data': availability_data,
            'errors': errors,
            'data': data,
            'item_quantities': extract_item_quantities(data),
            'start_date': start_date,
            'return_date': return_date,
            'start_date_formatted': start_date.strftime('%Y-%m-%d %H:%M'),
            'return_date_formatted': return_date.strftime('%Y-%m-%d %H:%M')
        })

    def get_start_return_date(self, data):
        """
        Return the start and return dates from the request

        If required data is missing or invalid, default values are returned.
        """

        try:
            start_date = datetime.strptime(data.get('start_date'), '%Y-%m-%d %H:%M')
        except:
            start_date = datetime.now() + timedelta(days=1)

        try:
            return_date = datetime.strptime(data.get('return_date'), '%Y-%m-%d %H:%M')
        except:
            return_date = start_date + timedelta(days=3)

        return (start_date, max(start_date, return_date))

    def get_chart_data(self, intervals):
        """
        Generate the data the JavaScript can render
        """

        data = []

        for begin, end, availability in intervals:
            data.append({
                "x": begin.isoformat(),
                "y": availability
            })
            data.append({
                "x": end.isoformat(),
                "y": availability
            })

        return data
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import copy
import os
import platform
import subprocess
from multiprocessing import cpu_count

isLinux = (platform.system() == 'Linux')
isMac = (platform.system() == 'Darwin')
isWin = (platform.system() == 'Windows')

ENV_PATH_SEPARATOR = ';' if os.name == 'nt' else ':'

def normExpUserPath(p):
    return os.path.normpath(os.path.expanduser(p))

LLVM_ROOT = normExpUserPath(os.path.join('~', 'llvm'))
LLVM_BUILD_DIR = normExpUserPath(os.path.join(LLVM_ROOT, 'build'))

def cloneLLVMGit():
    '''Clones the Git mirror of LLVM.'''
    print 'Cloning git mirror of LLVM to compile clang...'
    subprocess.check_call(['git', 'clone', 'https://github.com/llvm-mirror/llvm.git'],
                          cwd=os.path.abspath(os.path.join(LLVM_ROOT, os.pardir)))
    # All 3 revisions are different because the SVN repo has 3 parts, so the git repos have 3 hashes.
    # SVN r214697 or git rev 534100b is assumed.
    subprocess.check_call(['git', 'checkout', '534100b31e1eba23effe750c1c996f594ea2e3b5'],
                          cwd=LLVM_ROOT)
    subprocess.check_call(['git', 'clone', 'https://github.com/llvm-mirror/clang.git'],
                          cwd=os.path.join(LLVM_ROOT, 'tools'))
    # SVN r214699 or git rev 5a85cc5 is assumed.
    subprocess.check_call(['git', 'checkout', '5a85cc570a8fb55f153afca689e905cdbfc93e7d'],
                          cwd=os.path.join(LLVM_ROOT, 'tools', 'clang'))
    subprocess.check_call(['git', 'clone', 'https://github.com/llvm-mirror/compiler-rt'],
                          cwd=os.path.join(LLVM_ROOT, 'projects'))
    # SVN r214604 or git rev 7e5e68a is assumed.
    subprocess.check_call(['git', 'checkout', '7e5e68aa23ebac22842938060ba4f308251f48f8'],
                          cwd=os.path.join(LLVM_ROOT, 'projects', 'compiler-rt'))
    print 'Finished cloning git mirror of LLVM.'

def compileLLVM():
    '''Compiles LLVM using cmake.'''
    print 'Running cmake...'
    cmakeCmdList = ['cmake', '-DCMAKE_BUILD_TYPE:STRING=Release']
    if isMac:
        cmakeCmdList.append('-DLLVM_ENABLE_LIBCXX=ON')
    cmakeCmdList.append(LLVM_ROOT)
    subprocess.check_call(cmakeCmdList, cwd=LLVM_BUILD_DIR)
    print 'Running make...'
    subprocess.check_call(['make', '-s', '-j' + str(cpu_count())], cwd=LLVM_BUILD_DIR)
    print 'Finished compiling LLVM.'

def envWithPath(path, runningEnv=os.environ):
    '''Appends the path to the appropriate library path on various platforms.'''
    if isLinux:
        libPath = 'LD_LIBRARY_PATH'
    elif isMac:
        libPath = 'DYLD_LIBRARY_PATH'
    elif isWin:
        libPath = 'PATH'

    env = copy.deepcopy(runningEnv)
    if libPath in env:
        if path not in env[libPath]:
            env[libPath] += ENV_PATH_SEPARATOR + path
    else:
        env[libPath] = path

    return env

def findLlvmBinPath():
    '''Returns the path to compiled LLVM binaries, which differs depending on compilation method.'''
    # https://developer.mozilla.org/en-US/docs/Building_Firefox_with_Address_Sanitizer#Manual_Build
    if isWin:
        return None  # The harness does not yet support Clang on Windows

    if not os.path.isdir(LLVM_ROOT) and not os.path.isdir(os.path.join(LLVM_ROOT, '.git')):
        cloneLLVMGit()
        try:
            os.mkdir(LLVM_BUILD_DIR)
        except OSError:
            raise Exception('Unable to create LLVM build folder.')
        compileLLVM()

    possibleBinPaths = [
        normExpUserPath(os.path.join(LLVM_BUILD_DIR, 'bin')),
        normExpUserPath(os.path.join(LLVM_BUILD_DIR, 'Release', 'bin')),
        normExpUserPath(os.path.join(LLVM_BUILD_DIR, 'Release+Asserts', 'bin'))
    ]

    for path in possibleBinPaths:
        if os.path.isdir(path):
            assert os.path.isfile(normExpUserPath(os.path.join(path, 'clang')))
            assert os.path.isfile(normExpUserPath(os.path.join(path, 'clang++')))
            assert os.path.isfile(normExpUserPath(os.path.join(path, 'llvm-symbolizer')))
            return path

    return None
<commit_msg>Our specified versions of Clang for ASan last compiled with GCC 4.9. Ubuntu 15.10 comes with GCC 5.2 by default, which seems to break things, so fall back on GCC 4.9.<commit_after>#!/usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import copy
import os
import platform
import subprocess
from multiprocessing import cpu_count

isLinux = (platform.system() == 'Linux')
isMac = (platform.system() == 'Darwin')
isWin = (platform.system() == 'Windows')

ENV_PATH_SEPARATOR = ';' if os.name == 'nt' else ':'

def normExpUserPath(p):
    return os.path.normpath(os.path.expanduser(p))

LLVM_ROOT = normExpUserPath(os.path.join('~', 'llvm'))
LLVM_BUILD_DIR = normExpUserPath(os.path.join(LLVM_ROOT, 'build'))

def cloneLLVMGit():
    '''Clones the Git mirror of LLVM.'''
    print 'Cloning git mirror of LLVM to compile clang...'
    subprocess.check_call(['git', 'clone', 'https://github.com/llvm-mirror/llvm.git'],
                          cwd=os.path.abspath(os.path.join(LLVM_ROOT, os.pardir)))
    # All 3 revisions are different because the SVN repo has 3 parts, so the git repos have 3 hashes.
    # SVN r214697 or git rev 534100b is assumed.
    subprocess.check_call(['git', 'checkout', '534100b31e1eba23effe750c1c996f594ea2e3b5'],
                          cwd=LLVM_ROOT)
    subprocess.check_call(['git', 'clone', 'https://github.com/llvm-mirror/clang.git'],
                          cwd=os.path.join(LLVM_ROOT, 'tools'))
    # SVN r214699 or git rev 5a85cc5 is assumed.
    subprocess.check_call(['git', 'checkout', '5a85cc570a8fb55f153afca689e905cdbfc93e7d'],
                          cwd=os.path.join(LLVM_ROOT, 'tools', 'clang'))
    subprocess.check_call(['git', 'clone', 'https://github.com/llvm-mirror/compiler-rt'],
                          cwd=os.path.join(LLVM_ROOT, 'projects'))
    # SVN r214604 or git rev 7e5e68a is assumed.
    subprocess.check_call(['git', 'checkout', '7e5e68aa23ebac22842938060ba4f308251f48f8'],
                          cwd=os.path.join(LLVM_ROOT, 'projects', 'compiler-rt'))
    print 'Finished cloning git mirror of LLVM.'

def compileLLVM():
    '''Compiles LLVM using cmake.'''
    print 'Running cmake...'
    cmakeCmdList = []
    if isLinux and float(platform.linux_distribution()[1]) > 15.04:
        # The revisions specified above fail to compile with GCC 5.2, which comes with Ubuntu 15.10 by default.
        cmakeCmdList += ['CC=/usr/bin/gcc-4.9', 'CXX=/usr/bin/g++-4.9']
    cmakeCmdList += ['cmake', '-DCMAKE_BUILD_TYPE:STRING=Release']
    if isMac:
        cmakeCmdList.append('-DLLVM_ENABLE_LIBCXX=ON')
    cmakeCmdList.append(LLVM_ROOT)
    subprocess.check_call(cmakeCmdList, cwd=LLVM_BUILD_DIR)
    print 'Running make...'
    subprocess.check_call(['make', '-s', '-j' + str(cpu_count())], cwd=LLVM_BUILD_DIR)
    print 'Finished compiling LLVM.'

def envWithPath(path, runningEnv=os.environ):
    '''Appends the path to the appropriate library path on various platforms.'''
    if isLinux:
        libPath = 'LD_LIBRARY_PATH'
    elif isMac:
        libPath = 'DYLD_LIBRARY_PATH'
    elif isWin:
        libPath = 'PATH'

    env = copy.deepcopy(runningEnv)
    if libPath in env:
        if path not in env[libPath]:
            env[libPath] += ENV_PATH_SEPARATOR + path
    else:
        env[libPath] = path

    return env

def findLlvmBinPath():
    '''Returns the path to compiled LLVM binaries, which differs depending on compilation method.'''
    # https://developer.mozilla.org/en-US/docs/Building_Firefox_with_Address_Sanitizer#Manual_Build
    if isWin:
        return None  # The harness does not yet support Clang on Windows

    if not os.path.isdir(LLVM_ROOT) and not os.path.isdir(os.path.join(LLVM_ROOT, '.git')):
        cloneLLVMGit()
        try:
            os.mkdir(LLVM_BUILD_DIR)
        except OSError:
            raise Exception('Unable to create LLVM build folder.')
        compileLLVM()

    possibleBinPaths = [
        normExpUserPath(os.path.join(LLVM_BUILD_DIR, 'bin')),
        normExpUserPath(os.path.join(LLVM_BUILD_DIR, 'Release', 'bin')),
        normExpUserPath(os.path.join(LLVM_BUILD_DIR, 'Release+Asserts', 'bin'))
    ]

    for path in possibleBinPaths:
        if os.path.isdir(path):
            assert os.path.isfile(normExpUserPath(os.path.join(path, 'clang')))
            assert os.path.isfile(normExpUserPath(os.path.join(path, 'clang++')))
            assert os.path.isfile(normExpUserPath(os.path.join(path, 'llvm-symbolizer')))
            return path

    return None
<|endoftext|>


<commit_before>import logging
import os
import pytest
import sys

from intermol.tests.testing_tools import convert_one_to_all, ENGINES

logger = logging.getLogger('InterMolLog')

# Make a logger for unit and system tests.
testing_logger = logging.getLogger('testing')
if not testing_logger.handlers:
    testing_logger.setLevel(logging.DEBUG)
    h = logging.StreamHandler()
    h.setLevel(logging.INFO)  # ignores DEBUG level for now
    f = logging.Formatter("%(levelname)s %(asctime)s %(message)s",
                          "%Y-%m-%d %H:%M:%S")
    h.setFormatter(f)
    testing_logger.addHandler(h)

def test_gromacs_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='gromacs', test_type='unit', energy=energy,
                       output_dir=output_dir)

def test_gromacs_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='gromacs', test_type='stress', energy=energy,
                       output_dir=output_dir)

def test_lammps_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='lammps', test_type='unit', energy=energy,
                       output_dir=output_dir)

def test_lammps_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='lammps', test_type='stress', energy=energy,
                       output_dir=output_dir)

def test_desmond_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='desmond', test_type='unit', energy=energy,
                       output_dir=output_dir)

@pytest.mark.skipif(os.getenv('CI') is True,
                    reason='Desmond stress tests take too long to run')
def test_desmond_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='desmond', test_type='stress', energy=energy,
                       output_dir=output_dir)

def test_amber_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='amber', test_type='unit', energy=energy,
                       output_dir=output_dir)

def test_amber_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='amber', test_type='stress', energy=energy,
                       output_dir=output_dir)

find_test_func = {('unit', 'gromacs'): test_gromacs_unit,
                  ('unit', 'amber'): test_amber_unit,
                  ('unit', 'desmond'): test_desmond_unit,
                  ('unit', 'lammps'): test_lammps_unit,

                  ('stress', 'gromacs'): test_gromacs_stress,
                  ('stress', 'amber'): test_amber_stress,
                  ('stress', 'desmond'): test_desmond_stress,
                  ('stress', 'lammps'): test_lammps_stress,
}

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(
        description='Convert tests from one engine to all others.')

    engine = parser.add_argument('-p', '--program', metavar='engine',
            default='gromacs', help="The engine to convert from: {}".format(', '.join(ENGINES)))
    type_of_test = parser.add_argument('-t', '--type', metavar='test_type',
            default='unit', help="The type of tests to run: unit, stress.")
    compute_energies = parser.add_argument('-e', '--energy', dest='compute_energies',
            action='store_true', help="Compute and compare the input and output energies.")
    output_dir = parser.add_argument('-o', '--output', dest='output_dir',
            default=os.getcwd(), help="Output directory.")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = vars(parser.parse_args())
    testing_function = find_test_func[(args['type'].lower(),
                                       args['program'].lower())]
    testing_function(args['compute_energies'], args['output_dir'])
<commit_msg>Skip desmond stress tests<commit_after>import logging
import os
import pytest
import sys

from intermol.tests.testing_tools import convert_one_to_all, ENGINES

logger = logging.getLogger('InterMolLog')

# Make a logger for unit and system tests.
testing_logger = logging.getLogger('testing')
if not testing_logger.handlers:
    testing_logger.setLevel(logging.DEBUG)
    h = logging.StreamHandler()
    h.setLevel(logging.INFO)  # ignores DEBUG level for now
    f = logging.Formatter("%(levelname)s %(asctime)s %(message)s",
                          "%Y-%m-%d %H:%M:%S")
    h.setFormatter(f)
    testing_logger.addHandler(h)

def test_gromacs_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='gromacs', test_type='unit', energy=energy,
                       output_dir=output_dir)

def test_gromacs_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='gromacs', test_type='stress', energy=energy,
                       output_dir=output_dir)

def test_lammps_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='lammps', test_type='unit', energy=energy,
                       output_dir=output_dir)

def test_lammps_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='lammps', test_type='stress', energy=energy,
                       output_dir=output_dir)

def test_desmond_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='desmond', test_type='unit', energy=energy,
                       output_dir=output_dir)

@pytest.mark.skipif(os.getenv('CI') == 'true',
                    reason='Desmond stress tests take too long to run')
def test_desmond_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='desmond', test_type='stress', energy=energy,
                       output_dir=output_dir)

def test_amber_unit(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='amber', test_type='unit', energy=energy,
                       output_dir=output_dir)

def test_amber_stress(energy=False, output_dir=os.getcwd()):
    convert_one_to_all(input_engine='amber', test_type='stress', energy=energy,
                       output_dir=output_dir)

find_test_func = {('unit', 'gromacs'): test_gromacs_unit,
                  ('unit', 'amber'): test_amber_unit,
                  ('unit', 'desmond'): test_desmond_unit,
                  ('unit', 'lammps'): test_lammps_unit,

                  ('stress', 'gromacs'): test_gromacs_stress,
                  ('stress', 'amber'): test_amber_stress,
                  ('stress', 'desmond'): test_desmond_stress,
                  ('stress', 'lammps'): test_lammps_stress,
}

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(
        description='Convert tests from one engine to all others.')

    engine = parser.add_argument('-p', '--program', metavar='engine',
            default='gromacs', help="The engine to convert from: {}".format(', '.join(ENGINES)))
    type_of_test = parser.add_argument('-t', '--type', metavar='test_type',
            default='unit', help="The type of tests to run: unit, stress.")
    compute_energies = parser.add_argument('-e', '--energy', dest='compute_energies',
            action='store_true', help="Compute and compare the input and output energies.")
    output_dir = parser.add_argument('-o', '--output', dest='output_dir',
            default=os.getcwd(), help="Output directory.")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = vars(parser.parse_args())
    testing_function = find_test_func[(args['type'].lower(),
                                       args['program'].lower())]
    testing_function(args['compute_energies'], args['output_dir'])
<|endoftext|>


<commit_before>#!/usr/bin/env python

from optparse import OptionParser
import logging

from common import (setup_logging, load_devilry_plugins, add_debug_opt,
    add_quiet_opt, add_settings_option, set_django_settings_module)

extra_help = """
Actions:
    validate-gradeplugins
        Check that all grade plugins used in the database is
        available/correct.

    validate-gradeplugin-content-types
        AssignmentGroup objects have a grade attribute that should be a
        instance of the grade plugin of the Assignment. This that this is
        correct onchecks all AssignmentGroups.

    fix-gradeplugin-content-type-errors
        Fixes any of the errors explained in the
        <validate-gradeplugin-content-types> action.

    check-assignmentgroup-status
        We store the status of a AssignmentGroup as a number in the status
        field. This number is set by post_save events, using a query, so
        it might become corrupted over time. This action checks all these
        numbers, but changes nothing.

    set-correct-assignmentgroup-status
        Fixes the status attribute on all AssignmentGroups with wrong
        status.
"""

p = OptionParser(
        usage = "%prog [options] <action>")
add_settings_option(p)
p.add_option("--djangoadmin-script", dest="djangoadmin",
        default='django-admin.py',
        help="Path to the django-admin.py script. Defaults to "\
            "'django-admin.py', which means that it must be on the PATH.",
            metavar="PATH")
p.add_option("--no-backup-on-restore", action="store_false",
    dest="backup_on_restore", default=True,
    help="Normally a backup is made before a restore (because restore "\
        "destroys data). Use this to skip the backup.")
add_quiet_opt(p)
add_debug_opt(p)
(opt, args) = p.parse_args()
setup_logging(opt)

# Django must be imported after setting DJANGO_SETTINGS_MODULE
set_django_settings_module(opt)
load_devilry_plugins()
from devilry.core.models import Feedback, Assignment, AssignmentGroup
from devilry.core.gradeplugin import (GradePluginDoesNotExistError,
        WrongContentTypeError, GradePluginError)

def exit_help(msg=""):
    p.print_help()
    print
    print extra_help
    raise SystemExit(msg)

def validate_gradeplugins():
    for assignment in Assignment.objects.all():
        try:
            assignment.validate_gradeplugin()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (assignment, str(e)))
        else:
            logging.info('%-70s  [ OK ]' % assignment)

def validate_gradeplugin_contenttypes():
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        else:
            logging.info("%-70s  [ OK ]" % feedback)

def check_assignmentgroup_status():
    for ag in AssignmentGroup.objects.all():
        correct_status = ag._get_status_from_qry()
        if ag.status == correct_status:
            logging.info("%s correct status:%s, current status: %s." % (ag,
                ag.status, correct_status))
        else:
            logging.error("%s correct status:%s, current status: %s." % (ag,
                ag.status, correct_status))

def set_correct_assignmentgroup_status():
    for ag in AssignmentGroup.objects.all():
        correct_status = ag._get_status_from_qry()
        if ag.status == correct_status:
            logging.info("%s skipped (has correct status: %s)" % (ag,
                ag.status))
        else:
            logging.error("%s status changed from %s to %s." % (ag,
                ag.status, correct_status))
            ag.status = correct_status
            ag.save()

if len(args) == 0:
    exit_help()
action = args[0]

if action == "fix-gradeplugin-content-type-errors":
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        except WrongContentTypeError, e:
            assignment = feedback.delivery.assignment_group.parentnode
            correct_ct = assignment.get_gradeplugin_registryitem().get_content_type()
            feedback.content_type = correct_ct
            feedback.save()
            logging.warning("%-70s: Content type changed to: id: %s (%s)." % (
                feedback, correct_ct.pk, correct_ct))
        else:
            logging.debug("%-70s  [ NO CHANGE REQUIRED ]" % feedback)

elif action == "validate-gradeplugins":
    validate_gradeplugins()
elif action == "validate-gradeplugin-content-types":
    validate_gradeplugin_contenttypes()
elif action == "check-assignmentgroup-status":
    check_assignmentgroup_status()
elif action == "set-correct-assignmentgroup-status":
    set_correct_assignmentgroup_status()
else:
    exit_help("ERROR: Invalid action.")
<commit_msg>Changed ERROR to WARNING when changing assignmentgroup status in dbadmin.<commit_after>#!/usr/bin/env python

from optparse import OptionParser
import logging

from common import (setup_logging, load_devilry_plugins, add_debug_opt,
    add_quiet_opt, add_settings_option, set_django_settings_module)

extra_help = """
Actions:
    validate-gradeplugins
        Check that all grade plugins used in the database is
        available/correct.

    validate-gradeplugin-content-types
        AssignmentGroup objects have a grade attribute that should be a
        instance of the grade plugin of the Assignment. This that this is
        correct onchecks all AssignmentGroups.

    fix-gradeplugin-content-type-errors
        Fixes any of the errors explained in the
        <validate-gradeplugin-content-types> action.

    check-assignmentgroup-status
        We store the status of a AssignmentGroup as a number in the status
        field. This number is set by post_save events, using a query, so
        it might become corrupted over time. This action checks all these
        numbers, but changes nothing.

    set-correct-assignmentgroup-status
        Fixes the status attribute on all AssignmentGroups with wrong
        status.
"""

p = OptionParser(
        usage = "%prog [options] <action>")
add_settings_option(p)
p.add_option("--djangoadmin-script", dest="djangoadmin",
        default='django-admin.py',
        help="Path to the django-admin.py script. Defaults to "\
            "'django-admin.py', which means that it must be on the PATH.",
            metavar="PATH")
p.add_option("--no-backup-on-restore", action="store_false",
    dest="backup_on_restore", default=True,
    help="Normally a backup is made before a restore (because restore "\
        "destroys data). Use this to skip the backup.")
add_quiet_opt(p)
add_debug_opt(p)
(opt, args) = p.parse_args()
setup_logging(opt)

# Django must be imported after setting DJANGO_SETTINGS_MODULE
set_django_settings_module(opt)
load_devilry_plugins()
from devilry.core.models import Feedback, Assignment, AssignmentGroup
from devilry.core.gradeplugin import (GradePluginDoesNotExistError,
        WrongContentTypeError, GradePluginError)

def exit_help(msg=""):
    p.print_help()
    print
    print extra_help
    raise SystemExit(msg)

def validate_gradeplugins():
    for assignment in Assignment.objects.all():
        try:
            assignment.validate_gradeplugin()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (assignment, str(e)))
        else:
            logging.info('%-70s  [ OK ]' % assignment)

def validate_gradeplugin_contenttypes():
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        else:
            logging.info("%-70s  [ OK ]" % feedback)

def check_assignmentgroup_status():
    for ag in AssignmentGroup.objects.all():
        correct_status = ag._get_status_from_qry()
        if ag.status == correct_status:
            logging.info("%s correct status:%s, current status: %s." % (ag,
                ag.status, correct_status))
        else:
            logging.error("%s correct status:%s, current status: %s." % (ag,
                ag.status, correct_status))

def set_correct_assignmentgroup_status():
    for ag in AssignmentGroup.objects.all():
        correct_status = ag._get_status_from_qry()
        if ag.status == correct_status:
            logging.info("%s skipped (has correct status: %s)" % (ag,
                ag.status))
        else:
            logging.warning("%s status changed from %s to %s." % (ag,
                ag.status, correct_status))
            ag.status = correct_status
            ag.save()

if len(args) == 0:
    exit_help()
action = args[0]

if action == "fix-gradeplugin-content-type-errors":
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        except WrongContentTypeError, e:
            assignment = feedback.delivery.assignment_group.parentnode
            correct_ct = assignment.get_gradeplugin_registryitem().get_content_type()
            feedback.content_type = correct_ct
            feedback.save()
            logging.warning("%-70s: Content type changed to: id: %s (%s)." % (
                feedback, correct_ct.pk, correct_ct))
        else:
            logging.debug("%-70s  [ NO CHANGE REQUIRED ]" % feedback)

elif action == "validate-gradeplugins":
    validate_gradeplugins()
elif action == "validate-gradeplugin-content-types":
    validate_gradeplugin_contenttypes()
elif action == "check-assignmentgroup-status":
    check_assignmentgroup_status()
elif action == "set-correct-assignmentgroup-status":
    set_correct_assignmentgroup_status()
else:
    exit_help("ERROR: Invalid action.")
<|endoftext|>


<commit_before>from __future__ import absolute_import

from sentry.api.base import Endpoint
from sentry.api.exceptions import ResourceDoesNotExist
from sentry.api.bases.project import ProjectPermission
from sentry.models import Group

class GroupPermission(ProjectPermission):
    scope_map = {
        'GET': ['event:read', 'event:write', 'event:delete'],
        'POST': ['event:write', 'event:delete'],
        'PUT': ['event:write', 'event:delete'],
        'DELETE': ['event:delete'],
    }

    def has_object_permission(self, request, view, group):
        return super(GroupPermission, self).has_object_permission(
            request, view, group.project)

class GroupEndpoint(Endpoint):
    permission_classes = (GroupPermission,)

    def convert_args(self, request, group_id, *args, **kwargs):
        try:
            group = Group.objects.get(
                id=group_id,
            )
        except Group.DoesNotExist:
            raise ResourceDoesNotExist

        self.check_object_permissions(request, group)

        kwargs['group'] = group
        return (args, kwargs)
<commit_msg>Always fetch project with group<commit_after>from __future__ import absolute_import

from sentry.api.base import Endpoint
from sentry.api.exceptions import ResourceDoesNotExist
from sentry.api.bases.project import ProjectPermission
from sentry.models import Group

class GroupPermission(ProjectPermission):
    scope_map = {
        'GET': ['event:read', 'event:write', 'event:delete'],
        'POST': ['event:write', 'event:delete'],
        'PUT': ['event:write', 'event:delete'],
        'DELETE': ['event:delete'],
    }

    def has_object_permission(self, request, view, group):
        return super(GroupPermission, self).has_object_permission(
            request, view, group.project)

class GroupEndpoint(Endpoint):
    permission_classes = (GroupPermission,)

    def convert_args(self, request, group_id, *args, **kwargs):
        try:
            group = Group.objects.select_related('project').get(
                id=group_id,
            )
        except Group.DoesNotExist:
            raise ResourceDoesNotExist

        self.check_object_permissions(request, group)

        kwargs['group'] = group
        return (args, kwargs)
<|endoftext|>