<commit_before># Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from telemetry.page import test_expectations

# Valid expectation conditions are:
#
# Operating systems:
#     win, xp, vista, win7, mac, leopard, snowleopard, lion, mountainlion,
#     linux, chromeos, android
#
# GPU vendors:
#     amd, arm, broadcom, hisilicon, intel, imagination, nvidia, qualcomm,
#     vivante
#
# Specific GPUs can be listed as a tuple with vendor name and device ID.
# Examples: ('nvidia', 0x1234), ('arm', 'Mali-T604')
# Device IDs must be paired with a GPU vendor.

class WebGLConformanceExpectations(test_expectations.TestExpectations):
  def SetExpectations(self):
    # Sample Usage:
    # self.Fail('gl-enable-vertex-attrib.html',
    #     ['mac', 'amd', ('nvidia', 0x1234)], bug=123)

    # Windows/Intel failures
    self.Fail('conformance/textures/texture-size.html',
        ['win', 'intel'], bug=121139)

    # Windows 7/Intel failures
    self.Fail('conformance/context/context-lost-restored.html',
        ['win7', 'intel'])
    self.Fail('conformance/context/premultiplyalpha-test.html',
        ['win7', 'intel'])
    self.Fail('conformance/extensions/oes-texture-float-with-image-data.html',
        ['win7', 'intel'])
    self.Fail('conformance/extensions/oes-texture-float.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-min-attribs.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-max-texture-dimensions.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-min-textures.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-min-uniforms.html',
        ['win7', 'intel'])
    self.Fail('conformance/rendering/gl-clear.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/copy-tex-image-and-sub-image-2d.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/gl-teximage.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-array-buffer-view.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgb565.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba4444.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba5551.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-with-format-and-type.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-sub-image-2d.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texparameter-test.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-active-bind-2.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-active-bind.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-complete.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-formats-test.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-mips.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-npot.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-size-cube-maps.html',
        ['win7', 'intel'])

    # Mac/Intel failures
    # The following two tests hang the WindowServer.
    self.Skip('conformance/canvas/drawingbuffer-static-canvas-test.html',
        ['mac', 'intel'], bug=303915)
    self.Skip('conformance/canvas/drawingbuffer-test.html',
        ['mac', 'intel'], bug=303915)
    # The following three tests only fail.
    # Radar 13499677
    self.Fail(
        'conformance/glsl/functions/glsl-function-smoothstep-gentype.html',
        ['mac', 'intel'], bug=225642)
    # Radar 13499466
    self.Fail('conformance/limits/gl-max-texture-dimensions.html',
        ['mac', 'intel'], bug=225642)
    # Radar 13499623
    self.Fail('conformance/textures/texture-size.html',
        ['mac', 'intel'], bug=225642)

    # Mac/Intel failures on 10.7
    self.Skip('conformance/glsl/functions/glsl-function-asin.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-dot.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-faceforward.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-length.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-normalize.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-reflect.html',
        ['lion', 'intel'])
    self.Skip(
        'conformance/glsl/functions/glsl-function-smoothstep-gentype.html',
        ['lion', 'intel'])
    self.Skip('conformance/limits/gl-max-texture-dimensions.html',
        ['lion', 'intel'])
    self.Skip('conformance/rendering/line-loop-tri-fan.html',
        ['lion', 'intel'])

    # Mac/ATI failures
    self.Skip('conformance/extensions/oes-texture-float-with-image-data.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/rendering/gl-clear.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-array-buffer-view.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgb565.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba4444.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba5551.html',
        ['mac', 'amd'], bug=308328)

    # Android failures
    self.Fail('conformance/textures/texture-npot-video.html',
        ['android'], bug=306485)
    # The following test is very slow and therefore times out on Android bot.
    self.Skip('conformance/rendering/multisample_corruption.html',
        ['android'])

<commit_msg>Fix a typo in webgl_conformance_expectations.py<commit_after># Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from telemetry.page import test_expectations

# Valid expectation conditions are:
#
# Operating systems:
#     win, xp, vista, win7, mac, leopard, snowleopard, lion, mountainlion,
#     linux, chromeos, android
#
# GPU vendors:
#     amd, arm, broadcom, hisilicon, intel, imagination, nvidia, qualcomm,
#     vivante
#
# Specific GPUs can be listed as a tuple with vendor name and device ID.
# Examples: ('nvidia', 0x1234), ('arm', 'Mali-T604')
# Device IDs must be paired with a GPU vendor.

class WebGLConformanceExpectations(test_expectations.TestExpectations):
  def SetExpectations(self):
    # Sample Usage:
    # self.Fail('gl-enable-vertex-attrib.html',
    #     ['mac', 'amd', ('nvidia', 0x1234)], bug=123)

    # Windows/Intel failures
    self.Fail('conformance/textures/texture-size.html',
        ['win', 'intel'], bug=121139)

    # Windows 7/Intel failures
    self.Fail('conformance/context/context-lost-restored.html',
        ['win7', 'intel'])
    self.Fail('conformance/context/premultiplyalpha-test.html',
        ['win7', 'intel'])
    self.Fail('conformance/extensions/oes-texture-float-with-image-data.html',
        ['win7', 'intel'])
    self.Fail('conformance/extensions/oes-texture-float.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-min-attribs.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-max-texture-dimensions.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-min-textures.html',
        ['win7', 'intel'])
    self.Fail('conformance/limits/gl-min-uniforms.html',
        ['win7', 'intel'])
    self.Fail('conformance/rendering/gl-clear.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/copy-tex-image-and-sub-image-2d.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/gl-teximage.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-array-buffer-view.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgb565.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba4444.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba5551.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-image-with-format-and-type.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/tex-sub-image-2d.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texparameter-test.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-active-bind-2.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-active-bind.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-complete.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-formats-test.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-mips.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-npot.html',
        ['win7', 'intel'])
    self.Fail('conformance/textures/texture-size-cube-maps.html',
        ['win7', 'intel'])

    # Mac/Intel failures
    # The following two tests hang the WindowServer.
    self.Skip('conformance/canvas/drawingbuffer-static-canvas-test.html',
        ['mac', 'intel'], bug=303915)
    self.Skip('conformance/canvas/drawingbuffer-test.html',
        ['mac', 'intel'], bug=303915)
    # The following three tests only fail.
    # Radar 13499677
    self.Fail(
        'conformance/glsl/functions/glsl-function-smoothstep-gentype.html',
        ['mac', 'intel'], bug=225642)
    # Radar 13499466
    self.Fail('conformance/limits/gl-max-texture-dimensions.html',
        ['mac', 'intel'], bug=225642)
    # Radar 13499623
    self.Fail('conformance/textures/texture-size.html',
        ['mac', 'intel'], bug=225642)

    # Mac/Intel failures on 10.7
    self.Skip('conformance/glsl/functions/glsl-function-asin.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-dot.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-faceforward.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-length.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-normalize.html',
        ['lion', 'intel'])
    self.Skip('conformance/glsl/functions/glsl-function-reflect.html',
        ['lion', 'intel'])
    self.Skip(
        'conformance/glsl/functions/glsl-function-smoothstep-gentype.html',
        ['lion', 'intel'])
    self.Skip('conformance/limits/gl-max-texture-dimensions.html',
        ['lion', 'intel'])
    self.Skip('conformance/rendering/line-loop-tri-fan.html',
        ['lion', 'intel'])

    # Mac/ATI failures
    self.Skip('conformance/extensions/oes-texture-float-with-image-data.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/rendering/gl-clear.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-array-buffer-view.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgb565.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba4444.html',
        ['mac', 'amd'], bug=308328)
    self.Skip('conformance/textures/tex-image-and-sub-image-2d-with-image-data-rgba5551.html',
        ['mac', 'amd'], bug=308328)

    # Android failures
    self.Fail('conformance/textures/texture-npot-video.html',
        ['android'], bug=306485)
    # The following test is very slow and therefore times out on Android bot.
    self.Skip('conformance/rendering/multisample-corruption.html',
        ['android'])

<|endoftext|>


<commit_before>from contentcuration.models import License
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.translation import ugettext_lazy as _

register = template.Library()

LICENSE_MAPPING = None

DESCRIPTION_MAPPING = {"CC BY": _("The Attribution License lets others distribute, "
                                  "remix, tweak, and build upon your work, even commercially, "
                                  "as long as they credit you for the original creation. This "
                                  "is the most accommodating of licenses offered. Recommended "
                                  "for maximum dissemination and use of licensed materials."),
                       "CC BY-SA": _("The Attribution-ShareAlike License lets others remix,"
                                     " tweak, and build upon your work even for commercial purposes,"
                                     " as long as they credit you and license their new creations "
                                     "under the identical terms. This license is often compared to "
                                     "\"copyleft\" free and open source software licenses. All new "
                                     "works based on yours will carry the same license, so any "
                                     "derivatives will also allow commercial use. This is the "
                                     "license used by Wikipedia, and is recommended for materials "
                                     "that would benefit from incorporating content from Wikipedia "
                                     "and similarly licensed projects."),
                       "CC BY-ND": _("The Attribution-NoDerivs License allows for redistribution,"
                                     " commercial and non-commercial, as long as it is passed along "
                                     "unchanged and in whole, with credit to you."),
                       "CC BY-NC": _("The Attribution-NonCommercial License lets others remix, "
                                     "tweak, and build upon your work non-commercially, and although "
                                     "their new works must also acknowledge you and be non-commercial, "
                                     "they don't have to license their derivative works on the same terms."),
                       "CC BY-NC-SA": _("The Attribution-NonCommercial-ShareAlike License lets "
                                        "others remix, tweak, and build upon your work non-commercially, "
                                        "as long as they credit you and license their new creations under "
                                        "the identical terms."),
                       "CC BY-NC-ND": _("The Attribution-NonCommercial-NoDerivs License is the "
                                        "most restrictive of our six main licenses, only allowing others "
                                        "to download your works and share them with others as long as they "
                                        "credit you, but they can't change them in any way or use them commercially."),
                       "All Rights Reserved": _("The All Rights Reserved License indicates that "
                                                "the copyright holder reserves, or holds for their own use, all "
                                                "the rights provided by copyright law under one specific copyright treaty."),
                       "Public Domain": _("Public Domain work has been identified as being free "
                                          "of known restrictions under copyright law, including all related "
                                          "and neighboring rights."),
                       "Special Permissions": _("Special Permissions is a custom license to use"
                                                " when the current licenses do not apply to the content. The "
                                                "owner of this license is responsible for creating a description "
                                                "of what this license entails."),
                       }

@register.filter(is_safe=True)
@stringfilter
def get_license_url(value):
    if not LICENSE_MAPPING:
        LICENSE_MAPPING = {l.license_name: l.license_url for l in License.objects.all()}

    return LICENSE_MAPPING.get(value)

@register.filter(is_safe=True)
@stringfilter
def get_license_description(value):
    return DESCRIPTION_MAPPING.get(value) or License.objects.get(license_name=value).description
<commit_msg>ensure we refer to the global LICENSE_MAPPING var<commit_after>from contentcuration.models import License
from django import template
from django.template.defaultfilters import stringfilter
from django.utils.translation import ugettext_lazy as _

register = template.Library()

LICENSE_MAPPING = None

DESCRIPTION_MAPPING = {"CC BY": _("The Attribution License lets others distribute, "
                                  "remix, tweak, and build upon your work, even commercially, "
                                  "as long as they credit you for the original creation. This "
                                  "is the most accommodating of licenses offered. Recommended "
                                  "for maximum dissemination and use of licensed materials."),
                       "CC BY-SA": _("The Attribution-ShareAlike License lets others remix,"
                                     " tweak, and build upon your work even for commercial purposes,"
                                     " as long as they credit you and license their new creations "
                                     "under the identical terms. This license is often compared to "
                                     "\"copyleft\" free and open source software licenses. All new "
                                     "works based on yours will carry the same license, so any "
                                     "derivatives will also allow commercial use. This is the "
                                     "license used by Wikipedia, and is recommended for materials "
                                     "that would benefit from incorporating content from Wikipedia "
                                     "and similarly licensed projects."),
                       "CC BY-ND": _("The Attribution-NoDerivs License allows for redistribution,"
                                     " commercial and non-commercial, as long as it is passed along "
                                     "unchanged and in whole, with credit to you."),
                       "CC BY-NC": _("The Attribution-NonCommercial License lets others remix, "
                                     "tweak, and build upon your work non-commercially, and although "
                                     "their new works must also acknowledge you and be non-commercial, "
                                     "they don't have to license their derivative works on the same terms."),
                       "CC BY-NC-SA": _("The Attribution-NonCommercial-ShareAlike License lets "
                                        "others remix, tweak, and build upon your work non-commercially, "
                                        "as long as they credit you and license their new creations under "
                                        "the identical terms."),
                       "CC BY-NC-ND": _("The Attribution-NonCommercial-NoDerivs License is the "
                                        "most restrictive of our six main licenses, only allowing others "
                                        "to download your works and share them with others as long as they "
                                        "credit you, but they can't change them in any way or use them commercially."),
                       "All Rights Reserved": _("The All Rights Reserved License indicates that "
                                                "the copyright holder reserves, or holds for their own use, all "
                                                "the rights provided by copyright law under one specific copyright treaty."),
                       "Public Domain": _("Public Domain work has been identified as being free "
                                          "of known restrictions under copyright law, including all related "
                                          "and neighboring rights."),
                       "Special Permissions": _("Special Permissions is a custom license to use"
                                                " when the current licenses do not apply to the content. The "
                                                "owner of this license is responsible for creating a description "
                                                "of what this license entails."),
                       }

@register.filter(is_safe=True)
@stringfilter
def get_license_url(value):
    global LICENSE_MAPPING
    if not LICENSE_MAPPING:
        LICENSE_MAPPING = {l.license_name: l.license_url for l in License.objects.all()}

    return LICENSE_MAPPING.get(value)

@register.filter(is_safe=True)
@stringfilter
def get_license_description(value):
    return DESCRIPTION_MAPPING.get(value) or License.objects.get(license_name=value).description
<|endoftext|>


<commit_before>"""
Tests for third_party_auth decorators.
"""
import ddt
import unittest
import datetime

from django.conf import settings
from django.http import HttpResponse
from django.test import RequestFactory

from third_party_auth.decorators import xframe_allow_whitelisted, allow_frame_from_whitelisted_url
from third_party_auth.tests.testutil import TestCase

SCORM_CLOUD_URL = 'https://cloud.scorm.com'

def mock_view(_request):
    """ A test view for testing purposes. """
    return HttpResponse()

# remove this decorator once third_party_auth is enabled in CMS
@unittest.skipIf(
    'third_party_auth' not in settings.INSTALLED_APPS,
    'third_party_auth is not currently installed in CMS'
)
@ddt.ddt
class TestXFrameWhitelistDecorator(TestCase):
    """ Test the xframe_allow_whitelisted decorator. """

    def setUp(self):
        super(TestXFrameWhitelistDecorator, self).setUp()
        self.configure_lti_provider(name='Test', lti_hostname='localhost', lti_consumer_key='test_key', enabled=True)
        self.factory = RequestFactory()

    def construct_request(self, referer):
        """ Add the given referer to a request and then return it. """
        request = self.factory.get('/login')
        request.META['HTTP_REFERER'] = referer
        return request

    @ddt.unpack
    @ddt.data(
        ('http://localhost:8000/login', 'ALLOW'),
        ('http://not-a-real-domain.com/login', 'DENY'),
        (None, 'DENY')
    )
    def test_x_frame_options(self, url, expected_result):
        request = self.construct_request(url)

        response = xframe_allow_whitelisted(mock_view)(request)

        self.assertEqual(response['X-Frame-Options'], expected_result)

    @ddt.data('http://localhost/login', 'http://not-a-real-domain.com', None)
    def test_feature_flag_off(self, url):
        with self.settings(FEATURES={'ENABLE_THIRD_PARTY_AUTH': False}):
            request = self.construct_request(url)
            response = xframe_allow_whitelisted(mock_view)(request)
            self.assertEqual(response['X-Frame-Options'], 'DENY')

# remove this decorator once third_party_auth is enabled in CMS
@unittest.skipIf(
    'third_party_auth' not in settings.INSTALLED_APPS,
    'third_party_auth is not currently installed in CMS'
)
@ddt.ddt
class TestXFrameWhitelistDecoratorForSAML(TestCase):
    """ Test the allow_frame_from_whitelisted_url decorator. """

    def setUp(self):
        super(TestXFrameWhitelistDecoratorForSAML, self).setUp()
        self.configure_saml_provider_data(
            entity_id='https://idp.testshib.org/idp/shibboleth',
            sso_url='https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO',
            public_key='testkey',
            fetched_at=datetime.datetime.now()
        )
        self.factory = RequestFactory()

    def construct_request(self, referer):
        """ Add the given referer to a request and then return it. """
        request = self.factory.get('/auth/custom_auth_entry')
        request.META['HTTP_REFERER'] = referer
        return request

    @ddt.unpack
    @ddt.data(
        (
            'https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO',
            {
                'X-Frame-Options': 'ALLOW-FROM %s' % SCORM_CLOUD_URL,
                'Content-Security-Policy': "frame-ancestors %s" % SCORM_CLOUD_URL,

            }
        ),
        (
            'http://not-a-real-domain.com/SSO',
            {
                'X-Frame-Options': 'DENY',
                'Content-Security-Policy': "frame-ancestors 'none'",

            }
        ),
        (
            None,
            {
                'X-Frame-Options': 'DENY',
                'Content-Security-Policy': "frame-ancestors 'none'",

            }
        )
    )
    def test_x_frame_options(self, url, expected_headers):
        with self.settings(THIRD_PARTY_AUTH_FRAME_ALLOWED_FROM_URL=SCORM_CLOUD_URL):
            request = self.construct_request(url)
            response = allow_frame_from_whitelisted_url(mock_view)(request)
            for header, value in expected_headers.items():
                self.assertEqual(response[header], value)

    @ddt.data('https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO', 'http://not-a-real-domain.com/SSO', None)
    def test_feature_flag_off(self, url):
        with self.settings(FEATURES={'ENABLE_THIRD_PARTY_AUTH': False}):
            request = self.construct_request(url)
            response = allow_frame_from_whitelisted_url(mock_view)(request)
            self.assertEqual(response['X-Frame-Options'], 'DENY')
            self.assertEqual(response['Content-Security-Policy'], "frame-ancestors 'none'")
<commit_msg>fixed broken unit test<commit_after>"""
Tests for third_party_auth decorators.
"""
import ddt
import unittest
import datetime

from django.conf import settings
from django.http import HttpResponse
from django.test import RequestFactory

from third_party_auth.decorators import xframe_allow_whitelisted, allow_frame_from_whitelisted_url
from third_party_auth.tests.testutil import TestCase

SCORM_CLOUD_URL = 'https://cloud.scorm.com'

def mock_view(_request):
    """ A test view for testing purposes. """
    return HttpResponse()

# remove this decorator once third_party_auth is enabled in CMS
@unittest.skipIf(
    'third_party_auth' not in settings.INSTALLED_APPS,
    'third_party_auth is not currently installed in CMS'
)
@ddt.ddt
class TestXFrameWhitelistDecorator(TestCase):
    """ Test the xframe_allow_whitelisted decorator. """

    def setUp(self):
        super(TestXFrameWhitelistDecorator, self).setUp()
        self.configure_lti_provider(name='Test', lti_hostname='localhost', lti_consumer_key='test_key', enabled=True)
        self.factory = RequestFactory()

    def construct_request(self, referer):
        """ Add the given referer to a request and then return it. """
        request = self.factory.get('/login')
        request.META['HTTP_REFERER'] = referer
        return request

    @ddt.unpack
    @ddt.data(
        ('http://localhost:8000/login', 'ALLOW'),
        ('http://not-a-real-domain.com/login', 'DENY'),
        (None, 'DENY')
    )
    def test_x_frame_options(self, url, expected_result):
        request = self.construct_request(url)

        response = xframe_allow_whitelisted(mock_view)(request)

        self.assertEqual(response['X-Frame-Options'], expected_result)

    @ddt.data('http://localhost/login', 'http://not-a-real-domain.com', None)
    def test_feature_flag_off(self, url):
        with self.settings(FEATURES={'ENABLE_THIRD_PARTY_AUTH': False}):
            request = self.construct_request(url)
            response = xframe_allow_whitelisted(mock_view)(request)
            self.assertEqual(response['X-Frame-Options'], 'DENY')

# remove this decorator once third_party_auth is enabled in CMS
@unittest.skipIf(
    'third_party_auth' not in settings.INSTALLED_APPS,
    'third_party_auth is not currently installed in CMS'
)
@ddt.ddt
class TestXFrameWhitelistDecoratorForSAML(TestCase):
    """ Test the allow_frame_from_whitelisted_url decorator. """

    def setUp(self):
        super(TestXFrameWhitelistDecoratorForSAML, self).setUp()
        self.configure_saml_provider_data(
            entity_id='https://idp.testshib.org/idp/shibboleth',
            sso_url='https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO',
            public_key='testkey',
            fetched_at=datetime.datetime.now()
        )
        self.factory = RequestFactory()

    def construct_request(self, referer):
        """ Add the given referer to a request and then return it. """
        request = self.factory.get('/auth/custom_auth_entry')
        request.META['HTTP_REFERER'] = referer
        return request

    @ddt.unpack
    @ddt.data(
        (
            'https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO',
            {
                'X-Frame-Options': 'ALLOW-FROM %s' % SCORM_CLOUD_URL,
                'Content-Security-Policy': "frame-ancestors %s" % SCORM_CLOUD_URL,

            }
        ),
        (
            'http://not-a-real-domain.com/SSO',
            {
                'X-Frame-Options': 'DENY',
                'Content-Security-Policy': "frame-ancestors 'none'",

            }
        ),
        (
            None,
            {
                'X-Frame-Options': 'DENY',
                'Content-Security-Policy': "frame-ancestors 'none'",

            }
        )
    )
    def test_x_frame_options(self, url, expected_headers):
        with self.settings(THIRD_PARTY_AUTH_FRAME_ALLOWED_FROM_URL=[SCORM_CLOUD_URL]):
            request = self.construct_request(url)
            response = allow_frame_from_whitelisted_url(mock_view)(request)
            for header, value in expected_headers.items():
                self.assertEqual(response[header], value)

    @ddt.data('https://idp.testshib.org/idp/profile/SAML2/Redirect/SSO', 'http://not-a-real-domain.com/SSO', None)
    def test_feature_flag_off(self, url):
        with self.settings(FEATURES={'ENABLE_THIRD_PARTY_AUTH': False}):
            request = self.construct_request(url)
            response = allow_frame_from_whitelisted_url(mock_view)(request)
            self.assertEqual(response['X-Frame-Options'], 'DENY')
            self.assertEqual(response['Content-Security-Policy'], "frame-ancestors 'none'")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals

import sys
from collections import defaultdict

from django.conf import settings
from django.db import migrations

from corehq.apps.userreports.models import DataSourceConfiguration, StaticDataSourceConfiguration
from corehq.apps.userreports.util import get_table_name, LEGACY_UCR_TABLE_PREFIX
from corehq.sql_db.connections import connection_manager

GIT_COMMIT_WITH_MANAGEMENT_COMMAND = "8ec458ce9dd6a690c0b48bba07ffee2455f267d2"
AUTO_MIGRATE_FAILED_MESSAGE = """
A migration must be performed before this environment can be upgraded to the
latest version of CommCareHQ. To perform the migration you will need to do the following:

* Checkout an older version of CommCareHQ:

    git checkout {commit}

* Run the following management commands:

    python manage.py rename_ucr_tables create-views --verbose --execute --noconfirm

    python manage.py rename_ucr_tables rename-tables --verbose --execute --noconfirm

""".format(commit=GIT_COMMIT_WITH_MANAGEMENT_COMMAND)

def table_exists(connection, table_name):
    res = connection.execute("select 1 from pg_tables where tablename = %s", table_name)
    return bool(list(res))

def get_legacy_table_name(data_source):
    return get_table_name(
        data_source.domain, data_source.table_id, max_length=63, prefix=LEGACY_UCR_TABLE_PREFIX
    )

def _data_sources_by_engine_id():
    by_engine_id = defaultdict(list)
    for ds in StaticDataSourceConfiguration.all():
        ds_engine_id = ds['engine_id']
        by_engine_id[ds_engine_id].append(ds)

    for ds in DataSourceConfiguration.all():
        ds_engine_id = ds['engine_id']
        by_engine_id[ds_engine_id].append(ds)

    return by_engine_id

def _assert_migrated(apps, schema_editor):
    if settings.UNIT_TESTING:
        return

    for engine_id, data_sources in _data_sources_by_engine_id().items():
        with connection_manager.get_engine(engine_id).begin() as conn:
            for data_source in data_sources:
                legacy_table_name = get_legacy_table_name(data_source)
                new_table_name = get_table_name(data_source.domain, data_source.table_id)
                if (
                    table_exists(conn, legacy_table_name)
                    and not table_exists(conn, new_table_name)
                ):
                    print("")
                    print(AUTO_MIGRATE_FAILED_MESSAGE)
                    sys.exit(1)

class Migration(migrations.Migration):

    dependencies = [
        ('userreports', '0008_new_table_name_views'),
    ]

    operations = [
        migrations.RunPython(_assert_migrated, migrations.RunPython.noop, elidable=True)
    ]
<commit_msg>add point about stopping pillows<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals

import sys
from collections import defaultdict

from django.conf import settings
from django.db import migrations

from corehq.apps.userreports.models import DataSourceConfiguration, StaticDataSourceConfiguration
from corehq.apps.userreports.util import get_table_name, LEGACY_UCR_TABLE_PREFIX
from corehq.sql_db.connections import connection_manager

GIT_COMMIT_WITH_MANAGEMENT_COMMAND = "8ec458ce9dd6a690c0b48bba07ffee2455f267d2"
AUTO_MIGRATE_FAILED_MESSAGE = """
A migration must be performed before this environment can be upgraded to the
latest version of CommCareHQ. To perform the migration you will need to do the following:

* Checkout an older version of CommCareHQ:

    git checkout {commit}

* Stop all UCR pillow processes

* Run the following management commands:

    python manage.py rename_ucr_tables create-views --verbose --execute --noconfirm

    python manage.py rename_ucr_tables rename-tables --verbose --execute --noconfirm

""".format(commit=GIT_COMMIT_WITH_MANAGEMENT_COMMAND)

def table_exists(connection, table_name):
    res = connection.execute("select 1 from pg_tables where tablename = %s", table_name)
    return bool(list(res))

def get_legacy_table_name(data_source):
    return get_table_name(
        data_source.domain, data_source.table_id, max_length=63, prefix=LEGACY_UCR_TABLE_PREFIX
    )

def _data_sources_by_engine_id():
    by_engine_id = defaultdict(list)
    for ds in StaticDataSourceConfiguration.all():
        ds_engine_id = ds['engine_id']
        by_engine_id[ds_engine_id].append(ds)

    for ds in DataSourceConfiguration.all():
        ds_engine_id = ds['engine_id']
        by_engine_id[ds_engine_id].append(ds)

    return by_engine_id

def _assert_migrated(apps, schema_editor):
    if settings.UNIT_TESTING:
        return

    for engine_id, data_sources in _data_sources_by_engine_id().items():
        with connection_manager.get_engine(engine_id).begin() as conn:
            for data_source in data_sources:
                legacy_table_name = get_legacy_table_name(data_source)
                new_table_name = get_table_name(data_source.domain, data_source.table_id)
                if (
                    table_exists(conn, legacy_table_name)
                    and not table_exists(conn, new_table_name)
                ):
                    print("")
                    print(AUTO_MIGRATE_FAILED_MESSAGE)
                    sys.exit(1)

class Migration(migrations.Migration):

    dependencies = [
        ('userreports', '0008_new_table_name_views'),
    ]

    operations = [
        migrations.RunPython(_assert_migrated, migrations.RunPython.noop, elidable=True)
    ]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import logging

from desktop.lib.django_util import PopupException, render, render_to_string
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.forms.util import ErrorList
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
import jobsub.views as jobsub
from jobsub.models import Submission
from jobsubd.ttypes import (BinHadoopStep, LocalizedFile, LocalizeFilesStep,
                            State, SubmissionPlan, SubmissionPlanStep)

from cosmos import conf, mongo, paths
from cosmos.models import JobRun
from cosmos.forms import RunJobForm

LOGGER = logging.getLogger(__name__)
TEMP_JAR = "tmp.jar"
DEFAULT_PAGE = 1

def index(request):
    """List job runs."""

    return render('index.mako', request, dict(
        job_runs=JobRun.objects.filter(user=request.user)
                               .order_by('-start_date')
    ))

def validate_hdfs_path(fs, form, field):
    has_file = fs.exists(form.cleaned_data[field])
    if not has_file:
        errors = form._errors.setdefault(field, ErrorList())
        errors.append("File does not exist")
    return has_file

def submit(job):
    LOGGER.info("Submitting job %s (%d) for %s with JAR=%s on dataset %s" %
                (job.name, job.id, job.user.username, job.jar_path,
                 job.dataset_path))

    lf = LocalizedFile(target_name=TEMP_JAR, path_on_hdfs=job.jar_path)
    lfs = LocalizeFilesStep(localize_files=[lf])
    bhs = BinHadoopStep(arguments=job.hadoop_args(TEMP_JAR))
    plan = SubmissionPlan(name=job.name,
                          user=job.user.username,
                          groups=job.user.get_groups(),
                          steps=[SubmissionPlanStep(localize_files_step=lfs),
                                 SubmissionPlanStep(bin_hadoop_step=bhs)])
    submission = Submission(owner=job.user,
                            name=job.name,
                            last_seen_state=State.SUBMITTED,
                            submission_plan=plan)
    submission.save()
    job.submission = submission
    job.save()
    try:
        try:
            submission.submission_handle = jobsub.get_client().submit(plan)
        except Exception:
            import ipdb; ipdb.set_trace()
            submission.last_seen_state = State.ERROR
            raise
    finally:
        submission.save()

def run_job(request):
    """Starts a new job."""

    if request.method == 'POST':
        form = RunJobForm(request.POST, request.FILES)

        if form.is_valid():
            has_jar = validate_hdfs_path(request.fs, form, "jar_path")
            has_dataset = validate_hdfs_path(request.fs, form, "dataset_path")
            if has_jar and has_dataset:
                data = form.cleaned_data
                job = JobRun(name=data['name'], description=data['description'],
                             user=request.user,
                             dataset_path=data['dataset_path'],
                             jar_path=data['jar_path'])
                job.save()
                submit(job)
                return redirect(reverse('list_jobs'))
    else:
        form = RunJobForm()

    return render('job_run.mako', request, dict(
        form=form
    ))

def upload_index(request):
    """Allows to upload datasets and jars."""

    return render('upload_index.mako', request, dict(
        datasets_base=paths.datasets_base(request.user),
        jars_base=paths.jars_base(request.user)
    ))

def show_results(request, job_id):
    job = get_object_or_404(JobRun, pk=job_id, user=request.user)
    if job.submission is None:
        raise Http404

    try:
        primary_key = request.GET.get('primary_key')
        paginator = mongo.retrieve_results(job.id, primary_key)

        try:
            page_num = request.GET.get('page', DEFAULT_PAGE)
        except ValueError:
            page_num = DEFAULT_PAGE

        try:
            page = paginator.page(page_num)
        except PageNotAnInteger:
            page = paginator.page(DEFAULT_PAGE)
        except EmptyPage:
            page = paginator.page(paginator.num_pages)

        prototype_result = page.object_list[0]
        if primary_key is None:
            primary_key = prototype_result.pk

        return render('job_results.mako', request, dict(
            title='Results of job %s' % job.id,
            page=page,
            hidden_keys=mongo.HIDDEN_KEYS,
            primary_key=primary_key
        ))

    except mongo.NoResultsError:
        return render('job_results.mako', request, dict(
            title='Results of job %s' % job.id,
            hidden_keys=mongo.HIDDEN_KEYS
        ))

    except mongo.NoConnectionError:
        raise PopupException('Database not available')
<commit_msg>Ensuring the existence of upload directories<commit_after># -*- coding: utf-8 -*-
import logging

from desktop.lib.django_util import PopupException, render, render_to_string
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.forms.util import ErrorList
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
import jobsub.views as jobsub
from jobsub.models import Submission
from jobsubd.ttypes import (BinHadoopStep, LocalizedFile, LocalizeFilesStep,
                            State, SubmissionPlan, SubmissionPlanStep)

from cosmos import conf, mongo, paths
from cosmos.models import JobRun
from cosmos.forms import RunJobForm

LOGGER = logging.getLogger(__name__)
TEMP_JAR = "tmp.jar"
DEFAULT_PAGE = 1

def index(request):
    """List job runs."""

    return render('index.mako', request, dict(
        job_runs=JobRun.objects.filter(user=request.user)
                               .order_by('-start_date')
    ))

def validate_hdfs_path(fs, form, field):
    has_file = fs.exists(form.cleaned_data[field])
    if not has_file:
        errors = form._errors.setdefault(field, ErrorList())
        errors.append("File does not exist")
    return has_file

def submit(job):
    LOGGER.info("Submitting job %s (%d) for %s with JAR=%s on dataset %s" %
                (job.name, job.id, job.user.username, job.jar_path,
                 job.dataset_path))

    lf = LocalizedFile(target_name=TEMP_JAR, path_on_hdfs=job.jar_path)
    lfs = LocalizeFilesStep(localize_files=[lf])
    bhs = BinHadoopStep(arguments=job.hadoop_args(TEMP_JAR))
    plan = SubmissionPlan(name=job.name,
                          user=job.user.username,
                          groups=job.user.get_groups(),
                          steps=[SubmissionPlanStep(localize_files_step=lfs),
                                 SubmissionPlanStep(bin_hadoop_step=bhs)])
    submission = Submission(owner=job.user,
                            name=job.name,
                            last_seen_state=State.SUBMITTED,
                            submission_plan=plan)
    submission.save()
    job.submission = submission
    job.save()
    try:
        try:
            submission.submission_handle = jobsub.get_client().submit(plan)
        except Exception:
            import ipdb; ipdb.set_trace()
            submission.last_seen_state = State.ERROR
            raise
    finally:
        submission.save()

def run_job(request):
    """Starts a new job."""

    if request.method == 'POST':
        form = RunJobForm(request.POST, request.FILES)

        if form.is_valid():
            has_jar = validate_hdfs_path(request.fs, form, "jar_path")
            has_dataset = validate_hdfs_path(request.fs, form, "dataset_path")
            if has_jar and has_dataset:
                data = form.cleaned_data
                job = JobRun(name=data['name'], description=data['description'],
                             user=request.user,
                             dataset_path=data['dataset_path'],
                             jar_path=data['jar_path'])
                job.save()
                submit(job)
                return redirect(reverse('list_jobs'))
    else:
        form = RunJobForm()

    return render('job_run.mako', request, dict(
        form=form
    ))

def ensure_dir(fs, path):
    """Makes sure a directory with a given path exists or it is created.
       Raises a popup if a file is in the way"""
    if fs.isdir(path):
        return
    if fs.exists(path):
        raise PopupException(('A file named "%s" is preventing the creation ' +
                              'of the upload directory. Please, rename it to ' +
                              'proceed.') % path)
    else:
        fs.mkdir(path)

def upload_index(request):
    """Allows to upload datasets and jars."""

    datasets_base = paths.datasets_base(request.user)
    ensure_dir(request.fs, datasets_base)
    jars_base = paths.jars_base(request.user)
    ensure_dir(request.fs, jars_base)
    return render('upload_index.mako', request, dict(
        datasets_base=datasets_base,
        jars_base=jars_base
    ))

def show_results(request, job_id):
    job = get_object_or_404(JobRun, pk=job_id, user=request.user)
    if job.submission is None:
        raise Http404

    try:
        primary_key = request.GET.get('primary_key')
        paginator = mongo.retrieve_results(job.id, primary_key)

        try:
            page_num = request.GET.get('page', DEFAULT_PAGE)
        except ValueError:
            page_num = DEFAULT_PAGE

        try:
            page = paginator.page(page_num)
        except PageNotAnInteger:
            page = paginator.page(DEFAULT_PAGE)
        except EmptyPage:
            page = paginator.page(paginator.num_pages)

        prototype_result = page.object_list[0]
        if primary_key is None:
            primary_key = prototype_result.pk

        return render('job_results.mako', request, dict(
            title='Results of job %s' % job.id,
            page=page,
            hidden_keys=mongo.HIDDEN_KEYS,
            primary_key=primary_key
        ))

    except mongo.NoResultsError:
        return render('job_results.mako', request, dict(
            title='Results of job %s' % job.id,
            hidden_keys=mongo.HIDDEN_KEYS
        ))

    except mongo.NoConnectionError:
        raise PopupException('Database not available')
<|endoftext|>


<commit_before>"""
Write entry into scenario log table
"""

__copyright__   = "Reiner Lemoine Institut gGmbH"
__license__     = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__         = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ 	    = "nesnoj, Ludee"

from datetime import datetime
from sqlalchemy.orm import sessionmaker
from egoio.db_tables.model_draft import ScenarioLog as orm_scenario_log

def write_scenario_log(conn, project, version, io, schema, table,
                           script, entries=None, comment=None, metadata=None):
    """
    Write entry into scenario log table

    Parameters
    ----------
    conn: SQLAlchemy connection object
    project: str
        Project name
    version: str
             Version number
    io: str
        IO-type (input, output, temp)
    schema: str
            Database schema
    table: str
           Database table
    script: str
            Script name
    entries: int
             Number of entries
    comment: str
             Comment
    metadata: str
             Meta data

    Example
    -------
    write_scenario_log(conn=conn,
                           project='eGoDP'
                           version='v0.3.0',
                           io='output',
                           schema='model_draft',
                           table='ego_demand_loadarea_peak_load',
                           script='peak_load_per_load_area.py',
                           entries=1000)
    """

    Session = sessionmaker(bind=conn)
    session = Session()

    # extract user from connection details
    # is there a better way?
    try:
        conn_details = conn.connection.connection.dsn
        for entry in conn_details.split(' '):
            if entry.split('=')[0] == 'user':
                user = entry.split('=')[1]
                break
    except:
        user = 'unknown'

    # Add data to orm object
    log_entry = orm_scenario_log(project=project,
                                 version=version,
                                 io=io,
                                 schema_name=schema,
                                 table_name=table,
                                 script_name=script,
                                 entries=entries,
                                 status=comment,
                                 user_name=user,
                                 timestamp=datetime.now(),
                                 meta_data=metadata)

    # Commit to DB
    session.add(log_entry)
    session.commit()
<commit_msg>#226 changed column name to comment<commit_after>"""
Write entry into scenario log table
"""

__copyright__   = "Reiner Lemoine Institut gGmbH"
__license__     = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__         = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ 	    = "nesnoj, Ludee"

from datetime import datetime
from sqlalchemy.orm import sessionmaker
from egoio.db_tables.model_draft import ScenarioLog as orm_scenario_log

def write_scenario_log(conn, project, version, io, schema, table,
                           script, entries=None, comment=None, metadata=None):
    """
    Write entry into scenario log table

    Parameters
    ----------
    conn: SQLAlchemy connection object
    project: str
        Project name
    version: str
             Version number
    io: str
        IO-type (input, output, temp)
    schema: str
            Database schema
    table: str
           Database table
    script: str
            Script name
    entries: int
             Number of entries
    comment: str
             Comment
    metadata: str
             Meta data

    Example
    -------
    write_scenario_log(conn=conn,
                           project='eGoDP'
                           version='v0.3.0',
                           io='output',
                           schema='model_draft',
                           table='ego_demand_loadarea_peak_load',
                           script='peak_load_per_load_area.py',
                           entries=1000)
    """

    Session = sessionmaker(bind=conn)
    session = Session()

    # extract user from connection details
    # is there a better way?
    try:
        conn_details = conn.connection.connection.dsn
        for entry in conn_details.split(' '):
            if entry.split('=')[0] == 'user':
                user = entry.split('=')[1]
                break
    except:
        user = 'unknown'

    # Add data to orm object
    log_entry = orm_scenario_log(project=project,
                                 version=version,
                                 io=io,
                                 schema_name=schema,
                                 table_name=table,
                                 script_name=script,
                                 entries=entries,
                                 comment=comment,
                                 user_name=user,
                                 timestamp=datetime.now(),
                                 meta_data=metadata)

    # Commit to DB
    session.add(log_entry)
    session.commit()
<|endoftext|>


<commit_before>"""
Write entry into scenario log table
"""

__copyright__   = "Reiner Lemoine Institut gGmbH"
__license__     = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__         = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ 	    = "nesnoj, Ludee"

from datetime import datetime
from sqlalchemy.orm import sessionmaker
from egoio.db_tables.model_draft import EgoScenarioLog as orm_scenario_log

def write_ego_scenario_log(conn, version, io, schema, table,
                           script, entries=None, comment=None, metadata=None):
    """
    Write entry into scenario log table

    Parameters
    ----------
    conn: SQLAlchemy connection object
    version: str
             Version number
    io: str
        IO-type (input, output, temp)
    schema: str
            Database schema
    table: str
           Database table
    script: str
            Script name
    entries: int
             Number of entries
    comment: str
             Comment
    metadata: str
             Meta data

    Example
    -------
    write_ego_scenario_log(conn=conn,
                           version='v0.3.0',
                           io='output',
                           schema='model_draft',
                           table='ego_demand_loadarea_peak_load',
                           script='peak_load_per_load_area.py',
                           entries=1000)
    """

    Session = sessionmaker(bind=conn)
    session = Session()

    # extract user from connection details
    # is there a better way?
    try:
        conn_details = conn.connection.connection.dsn
        for entry in conn_details.split(' '):
            if entry.split('=')[0] == 'user':
                user = entry.split('=')[1]
                break
    except:
        user = 'unknown'

    # Add data to orm object
    log_entry = orm_scenario_log(version=version,
                                 io=io,
                                 schema_name=schema,
                                 table_name=table,
                                 script_name=script,
                                 entries=entries,
                                 status=comment,
                                 user_name=user,
                                 timestamp=datetime.now(),
                                 meta_data=metadata)

    # Commit to DB
    session.add(log_entry)
    session.commit()
<commit_msg>#226 modified function write_ego_scenario_log, modified ORM class EgoScenarioLog(egoio)<commit_after>"""
Write entry into scenario log table
"""

__copyright__   = "Reiner Lemoine Institut gGmbH"
__license__     = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__         = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ 	    = "nesnoj, Ludee"

from datetime import datetime
from sqlalchemy.orm import sessionmaker
from egoio.db_tables.model_draft import EgoScenarioLog as orm_scenario_log

def write_ego_scenario_log(conn, project, version, io, schema, table,
                           script, entries=None, comment=None, metadata=None):
    """
    Write entry into scenario log table

    Parameters
    ----------
    conn: SQLAlchemy connection object
    project: str
        Project name
    version: str
             Version number
    io: str
        IO-type (input, output, temp)
    schema: str
            Database schema
    table: str
           Database table
    script: str
            Script name
    entries: int
             Number of entries
    comment: str
             Comment
    metadata: str
             Meta data

    Example
    -------
    write_ego_scenario_log(conn=conn,
                           project='eGoDP'
                           version='v0.3.0',
                           io='output',
                           schema='model_draft',
                           table='ego_demand_loadarea_peak_load',
                           script='peak_load_per_load_area.py',
                           entries=1000)
    """

    Session = sessionmaker(bind=conn)
    session = Session()

    # extract user from connection details
    # is there a better way?
    try:
        conn_details = conn.connection.connection.dsn
        for entry in conn_details.split(' '):
            if entry.split('=')[0] == 'user':
                user = entry.split('=')[1]
                break
    except:
        user = 'unknown'

    # Add data to orm object
    log_entry = orm_scenario_log(project=project,
                                 version=version,
                                 io=io,
                                 schema_name=schema,
                                 table_name=table,
                                 script_name=script,
                                 entries=entries,
                                 status=comment,
                                 user_name=user,
                                 timestamp=datetime.now(),
                                 meta_data=metadata)

    # Commit to DB
    session.add(log_entry)
    session.commit()
<|endoftext|>


<commit_before>import lib_generation
import sys
import os
import generators

# USER CONFIG. MIGRATE ELSEWHERE
SLIP_RADIO_ID = [1,]
NODECOUNT = 5
# END OF USER CONFIG

motelist = []

if len(sys.argv) < 2:
	sys.exit("Error, missing input files\nUse:\n\tpython3 %s template_path [output_folder]" % sys.argv[0])

if len(sys.argv) < 3:
	outputfolder = '..' + os.path.sep + 'output' + os.path.sep;
else:
	outputfolder = sys.argv[2]

template_path = sys.argv[1]

lib_generation.mkdir(outputfolder)

simfilepath = os.path.normpath(outputfolder) + os.path.sep + 'simfile.csc'

sim = lib_generation.sim(simfilepath, template_path)
print(sim)

type_slipradio = lib_generation.sim_mote_type('slipradio', '[CONTIKI_DIR]/examples/ipv6/slip-radio/', 'slip-radio', '', "6LBR Slip Radio")
type_node = lib_generation.sim_mote_type('client', '[CONTIKI_DIR]/examples/6lbr-demo/', '6lbr-demo', 'WITH_SHELL=1 WITH_WEBSERVER=0 WITH_DELAY_IP=1', "6LBR Slip Radio")

sim.insert_sky_motetype(type_slipradio)
sim.insert_sky_motetype(type_node)

coords = generators.genline(30, NODECOUNT)

for index,coord in enumerate(coords):
        index = index + 1
	nodeid = index
	if nodeid in SLIP_RADIO_ID:
		mote = lib_generation.sim_mote(type_slipradio, nodeid)
	else:
		mote = lib_generation.sim_mote(type_node, nodeid)
	mote.set_coords(coord[0], coord[1], coord[2])
	motelist.append(mote)

sim.add_motes(motelist)
sim.set_timeout(999999999) #stop time in ms

sim.save_simfile()

<commit_msg>Set tx range and interference range from the sim generation example<commit_after>import lib_generation
import sys
import os
import generators

# USER CONFIG. MIGRATE ELSEWHERE
SLIP_RADIO_ID = [1,]
NODECOUNT = 5
STEP = 30
TXRANGE = 45
# END OF USER CONFIG

motelist = []

if len(sys.argv) < 2:
	sys.exit("Error, missing input files\nUse:\n\tpython3 %s template_path [output_folder]" % sys.argv[0])

if len(sys.argv) < 3:
	outputfolder = '..' + os.path.sep + 'output' + os.path.sep;
else:
	outputfolder = sys.argv[2]

template_path = sys.argv[1]

lib_generation.mkdir(outputfolder)

simfilepath = os.path.normpath(outputfolder) + os.path.sep + 'simfile.csc'

sim = lib_generation.sim(simfilepath, template_path)
print(sim)

type_slipradio = lib_generation.sim_mote_type('slipradio', '[CONTIKI_DIR]/examples/ipv6/slip-radio/', 'slip-radio', '', "6LBR Slip Radio")
type_node = lib_generation.sim_mote_type('client', '[CONTIKI_DIR]/examples/6lbr-demo/', '6lbr-demo', 'WITH_SHELL=1 WITH_WEBSERVER=0 WITH_DELAY_IP=1', "6LBR Slip Radio")

sim.insert_sky_motetype(type_slipradio)
sim.insert_sky_motetype(type_node)
sim.udgm_set_range(TXRANGE)
sim.udgm_set_interference_range(TXRANGE)

coords = generators.genline(STEP, NODECOUNT)

for index,coord in enumerate(coords):
        index = index + 1
	nodeid = index
	if nodeid in SLIP_RADIO_ID:
		mote = lib_generation.sim_mote(type_slipradio, nodeid)
	else:
		mote = lib_generation.sim_mote(type_node, nodeid)
	mote.set_coords(coord[0], coord[1], coord[2])
	motelist.append(mote)

sim.add_motes(motelist)
sim.set_timeout(999999999) #stop time in ms

sim.save_simfile()

<|endoftext|>


<commit_before>"""
Tests for dit.multivariate.intrinsic_mutual_information
"""
from __future__ import division

from nose.tools import assert_almost_equal, raises

import numpy as np

from dit import Distribution
from dit.exceptions import ditException
from dit.multivariate import total_correlation
from dit.multivariate.intrinsic_mutual_information import (intrinsic_total_correlation,
                                                           intrinsic_dual_total_correlation,
                                                           intrinsic_caekl_mutual_information,
                                                           intrinsic_mutual_information,
                                                           IntrinsicTotalCorrelation)

dist1 = Distribution([(0,0,0), (0,1,1), (1,0,1), (1,1,0), (2,2,2), (3,3,3)], [1/8]*4+[1/4]*2)
dist2 = Distribution(['000', '011', '101', '110', '222', '333'], [1/8]*4+[1/4]*2)
dist2.set_rv_names('XYZ')
dist3 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2)
dist4 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2)
dist4.set_rv_names('VWXYZ')

def test_itc1():
    """
    Test against standard result.
    """
    itc = intrinsic_total_correlation(dist1, [[0], [1]], [2])
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist1, [[0], [2]], [1])
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist1, [[1], [2]], [0])
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist3, [[0,1], [2]], [3, 4])
    assert_almost_equal(itc, 0)

def test_itc2():
    """
    Test against standard result, with rv names.
    """
    itc = intrinsic_total_correlation(dist2, ['X', 'Y'], 'Z')
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist2, ['X', 'Z'], 'Y')
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist2, ['Y', 'Z'], 'X')
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist4, ['VW', 'X'], 'YZ')
    assert_almost_equal(itc, 0)

def test_itc3():
    """
    Test multivariate
    """
    itc = intrinsic_total_correlation(dist3, [[0], [1], [2,3]], [4])
    assert_almost_equal(itc, 3.3306155324443121)

def test_itc4():
    """
    Test multivariate, with rv names
    """
    itc = intrinsic_total_correlation(dist4, ['V', 'W', 'XY'], 'Z')
    assert_almost_equal(itc, 3.3306155324443121)

def test_itc5():
    """
    Test with initial condition.
    """
    itc = IntrinsicTotalCorrelation(dist1, [[0], [1]], [2])
    itc.optimize(x0=np.eye(4).ravel(), nhops=5)
    d = itc.construct_distribution()
    print(d)
    val = total_correlation(d, [[0], [1]], [3])
    assert_almost_equal(val, 0)

def test_idtc1():
    """
    Test against standard result.
    """
    idtc = intrinsic_dual_total_correlation(dist1, [[0], [1]], [2])
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist1, [[0], [2]], [1])
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist1, [[1], [2]], [0])
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist3, [[0,1], [2]], [3, 4])
    assert_almost_equal(idtc, 0)

def test_idtc2():
    """
    Test against standard result, with rv names.
    """
    idtc = intrinsic_dual_total_correlation(dist2, ['X', 'Y'], 'Z')
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist2, ['X', 'Z'], 'Y')
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist2, ['Y', 'Z'], 'X')
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist4, ['VW', 'X'], 'YZ')
    assert_almost_equal(idtc, 0)

def test_idtc3():
    """
    Test multivariate
    """
    idtc = intrinsic_dual_total_correlation(dist3, [[0], [1], [2,3]], [4])
    assert_almost_equal(idtc, 1.6887218755408671)

def test_idtc4():
    """
    Test multivariate, with rv names
    """
    idtc = intrinsic_dual_total_correlation(dist4, ['V', 'W', 'XY'], 'Z')
    assert_almost_equal(idtc, 1.6887218755408671)

def test_icmi1():
    """
    Test against standard result.
    """
    icmi = intrinsic_caekl_mutual_information(dist1, [[0], [1]], [2])
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist1, [[0], [2]], [1])
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist1, [[1], [2]], [0])
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist3, [[0,1], [2]], [3, 4])
    assert_almost_equal(icmi, 0)

def test_icmi2():
    """
    Test against standard result, with rv names.
    """
    icmi = intrinsic_caekl_mutual_information(dist2, ['X', 'Y'], 'Z')
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist2, ['X', 'Z'], 'Y')
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist2, ['Y', 'Z'], 'X')
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist4, ['VW', 'X'], 'YZ')
    assert_almost_equal(icmi, 0)

def test_icmi3():
    """
    Test multivariate
    """
    icmi = intrinsic_caekl_mutual_information(dist3, [[0], [1], [2]], [3,4])
    assert_almost_equal(icmi, 0)

def test_icmi4():
    """
    Test multivariate, with rv names
    """
    icmi = intrinsic_caekl_mutual_information(dist4, ['V', 'W', 'X'], 'YZ', nhops=128)
    assert_almost_equal(icmi, 0)

def test_constructor():
    """
    Test the generic constructor.
    """
    test = intrinsic_mutual_information(total_correlation).functional()
    itc = intrinsic_total_correlation(dist1, [[0], [1]], [2], nhops=16)
    itc2 = test(dist1, [[0], [1]], [2], nhops=16)
    assert_almost_equal(itc, itc2, places=4)

@raises(ditException)
def test_imi_fail():
    """
    Test that things fail when not provided with a conditional variable.
    """
    intrinsic_total_correlation(dist1, [[0], [1], [2]])
<commit_msg>simplify multivariate tests<commit_after>"""
Tests for dit.multivariate.intrinsic_mutual_information
"""
from __future__ import division

from nose.tools import assert_almost_equal, raises

import numpy as np

from dit import Distribution
from dit.exceptions import ditException
from dit.multivariate import total_correlation
from dit.multivariate.intrinsic_mutual_information import (intrinsic_total_correlation,
                                                           intrinsic_dual_total_correlation,
                                                           intrinsic_caekl_mutual_information,
                                                           intrinsic_mutual_information,
                                                           IntrinsicTotalCorrelation)

dist1 = Distribution([(0,0,0), (0,1,1), (1,0,1), (1,1,0), (2,2,2), (3,3,3)], [1/8]*4+[1/4]*2)
dist2 = Distribution(['000', '011', '101', '110', '222', '333'], [1/8]*4+[1/4]*2)
dist2.set_rv_names('XYZ')
dist3 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2)
dist4 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2)
dist4.set_rv_names('VWXYZ')
dist5 = Distribution(['0000', '0011', '0101', '0110', '1001', '1010', '1100', '1111'], [1/8]*8)
dist6 = Distribution(['0000', '0011', '0101', '0110', '1001', '1010', '1100', '1111'], [1/8]*8)
dist6.set_rv_names('WXYZ')

def test_itc1():
    """
    Test against standard result.
    """
    itc = intrinsic_total_correlation(dist1, [[0], [1]], [2])
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist1, [[0], [2]], [1])
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist1, [[1], [2]], [0])
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist3, [[0,1], [2]], [3, 4])
    assert_almost_equal(itc, 0)

def test_itc2():
    """
    Test against standard result, with rv names.
    """
    itc = intrinsic_total_correlation(dist2, ['X', 'Y'], 'Z')
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist2, ['X', 'Z'], 'Y')
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist2, ['Y', 'Z'], 'X')
    assert_almost_equal(itc, 0)
    itc = intrinsic_total_correlation(dist4, ['VW', 'X'], 'YZ')
    assert_almost_equal(itc, 0)

def test_itc3():
    """
    Test multivariate
    """
    itc = intrinsic_total_correlation(dist5, [[0], [1], [2]], [3])
    assert_almost_equal(itc, 0)

def test_itc4():
    """
    Test multivariate, with rv names
    """
    itc = intrinsic_total_correlation(dist6, ['W', 'X', 'Y'], 'Z')
    assert_almost_equal(itc, 0)

def test_itc5():
    """
    Test with initial condition.
    """
    itc = IntrinsicTotalCorrelation(dist1, [[0], [1]], [2])
    itc.optimize(x0=np.eye(4).ravel(), nhops=5)
    d = itc.construct_distribution()
    print(d)
    val = total_correlation(d, [[0], [1]], [3])
    assert_almost_equal(val, 0)

def test_idtc1():
    """
    Test against standard result.
    """
    idtc = intrinsic_dual_total_correlation(dist1, [[0], [1]], [2])
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist1, [[0], [2]], [1])
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist1, [[1], [2]], [0])
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist3, [[0,1], [2]], [3, 4])
    assert_almost_equal(idtc, 0)

def test_idtc2():
    """
    Test against standard result, with rv names.
    """
    idtc = intrinsic_dual_total_correlation(dist2, ['X', 'Y'], 'Z')
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist2, ['X', 'Z'], 'Y')
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist2, ['Y', 'Z'], 'X')
    assert_almost_equal(idtc, 0)
    idtc = intrinsic_dual_total_correlation(dist4, ['VW', 'X'], 'YZ')
    assert_almost_equal(idtc, 0)

def test_idtc3():
    """
    Test multivariate
    """
    idtc = intrinsic_dual_total_correlation(dist5, [[0], [1], [2]], [3])
    assert_almost_equal(idtc, 0)

def test_idtc4():
    """
    Test multivariate, with rv names
    """
    idtc = intrinsic_dual_total_correlation(dist6, ['W', 'X', 'Y'], 'Z')
    assert_almost_equal(idtc, 0)

def test_icmi1():
    """
    Test against standard result.
    """
    icmi = intrinsic_caekl_mutual_information(dist1, [[0], [1]], [2])
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist1, [[0], [2]], [1])
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist1, [[1], [2]], [0])
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist3, [[0,1], [2]], [3, 4])
    assert_almost_equal(icmi, 0)

def test_icmi2():
    """
    Test against standard result, with rv names.
    """
    icmi = intrinsic_caekl_mutual_information(dist2, ['X', 'Y'], 'Z')
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist2, ['X', 'Z'], 'Y')
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist2, ['Y', 'Z'], 'X')
    assert_almost_equal(icmi, 0)
    icmi = intrinsic_caekl_mutual_information(dist4, ['VW', 'X'], 'YZ')
    assert_almost_equal(icmi, 0)

def test_icmi3():
    """
    Test multivariate
    """
    icmi = intrinsic_caekl_mutual_information(dist5, [[0], [1], [2]], [3])
    assert_almost_equal(icmi, 0)

def test_icmi4():
    """
    Test multivariate, with rv names
    """
    icmi = intrinsic_caekl_mutual_information(dist6, ['W', 'X', 'Y'], 'Z')
    assert_almost_equal(icmi, 0)

def test_constructor():
    """
    Test the generic constructor.
    """
    test = intrinsic_mutual_information(total_correlation).functional()
    itc = intrinsic_total_correlation(dist1, [[0], [1]], [2], nhops=16)
    itc2 = test(dist1, [[0], [1]], [2], nhops=16)
    assert_almost_equal(itc, itc2, places=4)

@raises(ditException)
def test_imi_fail():
    """
    Test that things fail when not provided with a conditional variable.
    """
    intrinsic_total_correlation(dist1, [[0], [1], [2]])
<|endoftext|>


<commit_before># encoding: utf-8

# FastCGI Windows Server Django installation command
#
# Copyright (c) 2012 Openance
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

__author__ = 'Antoine Martin <antoine@openance.com>'

import os
import os.path
import logging
import sys
import re
from optparse import OptionParser
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import get_template
from django.template import Context
from django.conf import settings
from optparse import make_option
import subprocess

class Command(BaseCommand):
    args = '[root_path]'
    help = '''Installs the current application as a fastcgi application under windows.

    If the root path is not specified, the command will take the
    root directory of the project.

    Don't forget to run this command as Administrator
    '''

    CONFIGURATION_TEMPLATE = '''/+[fullPath='{python_interpreter}',arguments='{script} winfcgi --pythonpath={project_dir}',maxInstances='{maxInstances}',idleTimeout='{idleTimeout}',activityTimeout='{activityTimeout}',requestTimeout='{requestTimeout}',instanceMaxRequests='{instanceMaxRequests}',protocol='NamedPipe',flushNamedPipe='False',monitorChangesTo='{monitorChangesTo}']'''

    DELETE_TEMPLATE = '''/[arguments='{script} winfcgi --pythonpath={project_dir}']'''

    FASTCGI_SECTION = 'system.webServer/fastCgi'

    option_list = BaseCommand.option_list + (
        make_option('--delete',
            action='store_true',
            dest='delete',
            default=False,
            help='Deletes the configuration instead of creating it'),
        make_option('--max-instances',
            dest='maxInstances',
            default=4,
            help='Maximum number of pyhton processes'),
        make_option('--idle-timeout',
            dest='idleTimeout',
            default=1800,
            help='Idle time in seconds after which a python process is recycled'),
        make_option('--activity-timeout',
            dest='activityTimeout',
            default=30,
            help='Number of seconds without data transfer after which a process is stopped'),
        make_option('--request-timeout',
            dest='requestTimeout',
            default=90,
            help='Total time in seconds for a request'),
        make_option('--instance-max-requests',
            dest='instanceMaxRequests',
            default=10000,
            help='Number of requests after which a python process is recycled'),
        make_option('--monitor-changes-to',
            dest='monitorChangesTo',
            default='',
            help='Application is restarted when this file changes'),
        make_option('--skip-fastcgi',
            action='store_true',
            dest='skip_fastcgi',
            default=False,
            help='Skips The FastCGI application installation'),
    )

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.appcmd = os.path.join(os.environ['windir'], 'system32', 'inetsrv', 'appcmd.exe')
        self.current_script = os.path.abspath(sys.argv[0])
        self.project_dir, self.script_name = os.path.split(self.current_script)
        self.python_interpreter = sys.executable

    def config_command(self, command, section, *args):
        arguments = [ self.appcmd, command, section]
        arguments.extend(args)
        #print ' '.join(arguments)
        return subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    def run_config_command(self, command, section, *args):
        command = self.config_command(command, section, *args)
        (out, err) = command.communicate()
        result = command.returncode == 0
        self.last_command_error = out if not result else None
        return result

    def check_config_section_exists(self, section_name):
        return self.run_config_command('list', 'config', '-section:%s' % section_name)

    def create_fastcgi_section(self, options):
        template_options = options.copy()
        template_options['script'] = self.current_script
        template_options['project_dir'] = self.project_dir
        template_options['python_interpreter'] = self.python_interpreter
        param = self.CONFIGURATION_TEMPLATE.format(**template_options)
        return self.run_config_command('set', 'config', '-section:%s' % self.FASTCGI_SECTION, param, '/commit:apphost')

    def delete_fastcgi_section(self):
        template_options = dict(script = self.current_script, project_dir = self.project_dir)
        param = self.DELETE_TEMPLATE.format(**template_options)
        return self.run_config_command('clear', 'config', '-section:%s' % self.FASTCGI_SECTION, param, '/commit:apphost')

    def install(self, args, options):
        if os.path.exists(self.web_config):
            raise CommandError('A web site configuration already exists in [%s] !' % self.install_dir)

        # now getting static files directory and URL
        static_dir = os.path.normcase(os.path.abspath(getattr(settings, 'STATIC_ROOT', '')))
        static_url = getattr(settings, 'STATIC_URL', '/static/')

        static_match = re.match('^/([^/]+)/$', static_url)
        if static_match:
            static_is_local = True
            static_name = static_match.group(1)
            static_needs_virtual_dir = static_dir != os.path.join(self.install_dir, static_name)
        else:
            static_is_local = False

        if static_dir == self.install_dir and static_is_local:
            raise CommandError('''\
The web site directory cannot be the same as the static directory,
for we cannot have two different web.config files in the same
directory !''')

        if options['monitorChangesTo'] == '':
            options['monitorChangesTo'] = os.path.join(self.install_dir, 'web.config')

        # create FastCGI application
        if not options['skip_fastcgi'] and not self.create_fastcgi_section(options):
            raise CommandError('The FastCGI application creation has failed')

        # create web.config
        template = get_template('windows_tools/iis/web.config')
        file = open(self.web_config, 'w')
        file.write(template.render(Context(self.__dict__)))
        file.close()

    def delete(self, args, options):
        if not os.path.exists(self.web_config):
            raise CommandError('A web site configuration does not exists in [%s] !' % self.install_dir)

        os.remove(self.web_config)

        if not self.delete_fastcgi_section():
            raise CommandError('The FastCGI application removal has failed')

    def handle(self, *args, **options):
        if self.script_name == 'django-admin.py':
            raise CommandError("""\
This command does not work when used with django-admin.py.
Please run it with the manage.py of the root directory of your project.
""")
        # Getting installation directory and doing some little checks
        self.install_dir = args[0] if args else self.project_dir
        if not os.path.exists(self.install_dir):
            raise CommandError('The web site directory [%s] does not exist !' % self.install_dir)

        if not os.path.isdir(self.install_dir):
            raise CommandError('The web site directory [%s] is not a directory !' % self.install_dir)

        self.install_dir = os.path.normcase(os.path.abspath(self.install_dir))

        print 'Using installation directory %s' % self.install_dir

        self.web_config = os.path.join(self.install_dir, 'web.config')

        if not os.path.exists(self.appcmd):
            raise CommandError('It seems that IIS is not installed on your machine')

        if not self.check_config_section_exists(self.FASTCGI_SECTION):
            raise CommandError('It seems that The CGI module is not installed')

        if options['delete']:
            self.delete(args,options)
        else:
            self.install(args,options)

if __name__ == '__main__':
    print 'This is supposed to be run as a django management command'
        <commit_msg>Now web.config can be edited by ysers<commit_after># encoding: utf-8

# FastCGI Windows Server Django installation command
#
# Copyright (c) 2012 Openance
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

__author__ = 'Antoine Martin <antoine@openance.com>'

import os
import os.path
import logging
import sys
import re
import stat
from optparse import OptionParser
from django.core.management.base import BaseCommand, CommandError
from django.template.loader import get_template
from django.template import Context
from django.conf import settings
from optparse import make_option
import subprocess

def set_file_readable(filename):
    import win32api
    import win32security
    import ntsecuritycon as con

    users, domain, type = win32security.LookupAccountName ("", "Users")
    admins, domain, type = win32security.LookupAccountName ("", "Administrators")
    user, domain, type = win32security.LookupAccountName ("", win32api.GetUserName ())

    sd = win32security.GetFileSecurity (filename, win32security.DACL_SECURITY_INFORMATION)

    dacl = win32security.ACL ()
    dacl.AddAccessAllowedAce (win32security.ACL_REVISION, con.FILE_ALL_ACCESS, users)
    dacl.AddAccessAllowedAce (win32security.ACL_REVISION, con.FILE_ALL_ACCESS, user)
    dacl.AddAccessAllowedAce (win32security.ACL_REVISION, con.FILE_ALL_ACCESS, admins)
    sd.SetSecurityDescriptorDacl (1, dacl, 0)
    win32security.SetFileSecurity (filename, win32security.DACL_SECURITY_INFORMATION, sd)

class Command(BaseCommand):
    args = '[root_path]'
    help = '''Installs the current application as a fastcgi application under windows.

    If the root path is not specified, the command will take the
    root directory of the project.

    Don't forget to run this command as Administrator
    '''

    CONFIGURATION_TEMPLATE = '''/+[fullPath='{python_interpreter}',arguments='{script} winfcgi --pythonpath={project_dir}',maxInstances='{maxInstances}',idleTimeout='{idleTimeout}',activityTimeout='{activityTimeout}',requestTimeout='{requestTimeout}',instanceMaxRequests='{instanceMaxRequests}',protocol='NamedPipe',flushNamedPipe='False',monitorChangesTo='{monitorChangesTo}']'''

    DELETE_TEMPLATE = '''/[arguments='{script} winfcgi --pythonpath={project_dir}']'''

    FASTCGI_SECTION = 'system.webServer/fastCgi'

    option_list = BaseCommand.option_list + (
        make_option('--delete',
            action='store_true',
            dest='delete',
            default=False,
            help='Deletes the configuration instead of creating it'),
        make_option('--max-instances',
            dest='maxInstances',
            default=4,
            help='Maximum number of pyhton processes'),
        make_option('--idle-timeout',
            dest='idleTimeout',
            default=1800,
            help='Idle time in seconds after which a python process is recycled'),
        make_option('--activity-timeout',
            dest='activityTimeout',
            default=30,
            help='Number of seconds without data transfer after which a process is stopped'),
        make_option('--request-timeout',
            dest='requestTimeout',
            default=90,
            help='Total time in seconds for a request'),
        make_option('--instance-max-requests',
            dest='instanceMaxRequests',
            default=10000,
            help='Number of requests after which a python process is recycled'),
        make_option('--monitor-changes-to',
            dest='monitorChangesTo',
            default='',
            help='Application is restarted when this file changes'),
        make_option('--site-name',
            dest='site_name',
            default='',
            help='IIS site name (defaults to name of installation directory)'),
        make_option('--binding',
            dest='binding',
            default='http://*:80',
            help='IIS site binding. Defaults to http://*:80'),
        make_option('--skip-fastcgi',
            action='store_true',
            dest='skip_fastcgi',
            default=False,
            help='Skips The FastCGI application installation'),
        make_option('--skip-site',
            action='store_true',
            dest='skip_site',
            default=False,
            help='Skips The site creation'),
        make_option('--skip-config',
            action='store_true',
            dest='skip_config',
            default=False,
            help='Skips The configuration creation'),
    )

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.appcmd = os.path.join(os.environ['windir'], 'system32', 'inetsrv', 'appcmd.exe')
        self.current_script = os.path.abspath(sys.argv[0])
        self.project_dir, self.script_name = os.path.split(self.current_script)
        self.python_interpreter = sys.executable

    def config_command(self, command, section, *args):
        arguments = [ self.appcmd, command, section]
        arguments.extend(args)
        #print ' '.join(arguments)
        return subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    def run_config_command(self, command, section, *args):
        command = self.config_command(command, section, *args)
        (out, err) = command.communicate()
        result = command.returncode == 0
        self.last_command_error = out if not result else None
        return result

    def check_config_section_exists(self, section_name):
        return self.run_config_command('list', 'config', '-section:%s' % section_name)

    def create_fastcgi_section(self, options):
        template_options = options.copy()
        template_options['script'] = self.current_script
        template_options['project_dir'] = self.project_dir
        template_options['python_interpreter'] = self.python_interpreter
        param = self.CONFIGURATION_TEMPLATE.format(**template_options)
        return self.run_config_command('set', 'config', '-section:%s' % self.FASTCGI_SECTION, param, '/commit:apphost')

    def delete_fastcgi_section(self):
        template_options = dict(script = self.current_script, project_dir = self.project_dir)
        param = self.DELETE_TEMPLATE.format(**template_options)
        return self.run_config_command('clear', 'config', '-section:%s' % self.FASTCGI_SECTION, param, '/commit:apphost')

    def install(self, args, options):
        if os.path.exists(self.web_config) and not options['skip_config']:
            raise CommandError('A web site configuration already exists in [%s] !' % self.install_dir)

        # now getting static files directory and URL
        static_dir = os.path.normcase(os.path.abspath(getattr(settings, 'STATIC_ROOT', '')))
        static_url = getattr(settings, 'STATIC_URL', '/static/')

        static_match = re.match('^/([^/]+)/$', static_url)
        if static_match:
            static_is_local = True
            static_name = static_match.group(1)
            static_needs_virtual_dir = static_dir != os.path.join(self.install_dir, static_name)
        else:
            static_is_local = False

        if static_dir == self.install_dir and static_is_local:
            raise CommandError('''\
The web site directory cannot be the same as the static directory,
for we cannot have two different web.config files in the same
directory !''')

        # create web.config
        if not options['skip_config']:
            print "Creating web.config"
            template = get_template('windows_tools/iis/web.config')
            file = open(self.web_config, 'w')
            file.write(template.render(Context(self.__dict__)))
            file.close()
            set_file_readable(self.web_config)

        if options['monitorChangesTo'] == '':
            options['monitorChangesTo'] = os.path.join(self.install_dir, 'web.config')

        # create FastCGI application
        if not options['skip_fastcgi']:
            print "Creating FastCGI application"
            if not self.create_fastcgi_section(options):
                raise CommandError('The FastCGI application creation has failed with the following message :\n%s' % self.last_command_error)

        # Create sites
        if not options['skip_site']:
            site_name = options['site_name']
            print "Creating application pool with name %s"  % site_name
            if not self.run_config_command('add', 'apppool', '/name:%s' % site_name):
                raise CommandError('The Application Pool creation has failed with the following message :\n%s' % self.last_command_error)

            print "Creating the site"
            if not self.run_config_command('add', 'site', '/name:%s' % site_name, '/bindings:%s' % options['binding'], '/physicalPath:%s' % self.install_dir):
                raise CommandError('The site creation has failed with the following message :\n%s' % self.last_command_error)

            print "Adding the site to the application pool"
            if not self.run_config_command('set', 'app', '%s/' % site_name, '/applicationPool:%s' % site_name):
                raise CommandError('Adding the site to the application pool has failed with the following message :\n%s' % self.last_command_error)

    def delete(self, args, options):
        if not os.path.exists(self.web_config) and not options['skip_config']:
            raise CommandError('A web site configuration does not exists in [%s] !' % self.install_dir)

        if not options['skip_config']:
            print "Removing site configuration"
            os.remove(self.web_config)

        if not options['skip_site']:
            site_name = options['site_name']
            print "Removing The site"
            if not self.run_config_command('delete', 'site', site_name):
                raise CommandError('Removing the site has failed with the following message :\n%s' % self.last_command_error)

            print "Removing The application pool"
            if not self.run_config_command('delete', 'apppool', site_name):
                raise CommandError('Removing the site has failed with the following message :\n%s' % self.last_command_error)

        if not options['skip_fastcgi']:
            print "Removing FastCGI application"
            if not self.delete_fastcgi_section():
                raise CommandError('The FastCGI application removal has failed')

    def handle(self, *args, **options):
        if self.script_name == 'django-admin.py':
            raise CommandError("""\
This command does not work when used with django-admin.py.
Please run it with the manage.py of the root directory of your project.
""")
        # Getting installation directory and doing some little checks
        self.install_dir = args[0] if args else self.project_dir
        if not os.path.exists(self.install_dir):
            raise CommandError('The web site directory [%s] does not exist !' % self.install_dir)

        if not os.path.isdir(self.install_dir):
            raise CommandError('The web site directory [%s] is not a directory !' % self.install_dir)

        self.install_dir = os.path.normcase(os.path.abspath(self.install_dir))

        print 'Using installation directory %s' % self.install_dir

        self.web_config = os.path.join(self.install_dir, 'web.config')

        if options['site_name'] == '':
            options['site_name'] = os.path.split(self.install_dir)[1]

        if not os.path.exists(self.appcmd):
            raise CommandError('It seems that IIS is not installed on your machine')

        if not self.check_config_section_exists(self.FASTCGI_SECTION):
            raise CommandError('It seems that The CGI module is not installed')

        if options['delete']:
            self.delete(args,options)
        else:
            self.install(args,options)

if __name__ == '__main__':
    print 'This is supposed to be run as a django management command'
        <|endoftext|>