<commit_before># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Tests for :module:`flocker.node.script`.
"""

from StringIO import StringIO

from twisted.trial.unittest import SynchronousTestCase
from twisted.python.usage import UsageError
from twisted.application.service import Service

from yaml import safe_dump, safe_load
from ...testtools import FlockerScriptTestsMixin, StandardOptionsTestsMixin
from ..script import (
    ChangeStateOptions, ChangeStateScript,
    ReportStateScript, ReportStateOptions)
from ..gear import FakeGearClient, Unit
from .._deploy import Deployer
from .._model import Application, Deployment, DockerImage, Node, AttachedVolume
from ...volume.testtools import create_volume_service

class ChangeStateScriptTests(FlockerScriptTestsMixin, SynchronousTestCase):
    """
    Tests for ``ChangeStateScript``.
    """
    script = staticmethod(lambda: ChangeStateScript(lambda: None))
    options = ChangeStateOptions
    command_name = u'flocker-changestate'

class ChangeStateScriptMainTests(SynchronousTestCase):
    """
    Tests for ``ChangeStateScript.main``.
    """
    def test_deployer_type(self):
        """
        ``ChangeStateScript._deployer`` is an instance of :class:`Deployer`.
        """
        script = ChangeStateScript(lambda: None)
        self.assertIsInstance(script._deployer, Deployer)

    def test_deployer_volume_service(self):
        """
        ``ChangeStateScript._deployer`` is configured with a volume service
        created by the given callable.
        """
        service = object()
        script = ChangeStateScript(lambda: service)
        self.assertIs(script._deployer.volume_service, service)

    def test_main_starts_service(self):
        """
        ``ChangeStateScript.main`` starts the volume service that was created
        by the given callable.
        """
        service = Service()
        script = ChangeStateScript(lambda: service)
        running_before_main = service.running
        self.patch(
            script._deployer, 'change_node_state', lambda *args, **kwargs: None)
        options = dict(deployment=object(),
                       current=object(),
                       hostname="")
        script.main(None, options)
        self.assertEqual((running_before_main, service.running),
                         (False, True))

    def test_main_calls_deployer_change_node_state(self):
        """
        ``ChangeStateScript.main`` calls ``Deployer.change_node_state`` with
        the ``Deployment`` and `hostname` supplied on the command line.
        """
        script = ChangeStateScript(lambda: Service())

        change_node_state_calls = []

        def spy_change_node_state(desired_state, current_cluster_state,
                                  hostname):
            """
            A stand in for ``Deployer.change_node_state`` which records calls
            made to it.
            """
            change_node_state_calls.append((desired_state,
                                            current_cluster_state, hostname))

        self.patch(
            script._deployer, 'change_node_state', spy_change_node_state)

        expected_deployment = object()
        expected_current = object()
        expected_hostname = b'node1.example.com'
        options = dict(deployment=expected_deployment,
                       current=expected_current,
                       hostname=expected_hostname)
        script.main(reactor=object(), options=options)

        self.assertEqual(
            [(expected_deployment, expected_current, expected_hostname)],
            change_node_state_calls
        )

class ChangeStateOptionsTests(StandardOptionsTestsMixin, SynchronousTestCase):
    """
    Tests for :class:`ChangeStateOptions`.
    """
    options = ChangeStateOptions

    def test_custom_configs(self):
        """
        The supplied application and deployment configuration strings are
        parsed as a :class:`Deployment` on the options instance.
        """
        application = Application(
            name=u'mysql-hybridcluster',
            image=DockerImage(repository=u'hybridlogic/mysql5.9',
                              tag=u'latest'))

        node = Node(hostname='node1.example.com',
                    applications=frozenset([application]))
        options = self.options()
        deployment_config = {"nodes": {node.hostname: [application.name]},
                             "version": 1}

        application_config = dict(
            version=1,
            applications={
                application.name: {'image': 'hybridlogic/mysql5.9:latest'}
            }
        )

        current_config = {'node2.example.com': {
            'applications': {
                'mysql-something': {
                    'image': 'unknown',
                    'volume': {'mountpoint': None},
                }
            },
            'version': 1
        }}

        options.parseOptions(
            [safe_dump(deployment_config),
             safe_dump(application_config),
             safe_dump(current_config),
             b'node1.example.com'])

        self.assertEqual(
            Deployment(nodes=frozenset([node])), options['deployment'])

    def test_current_configuration(self):
        """
        The supplied current cluster configuration strings is parsed as a
        :class:`Deployment` on the options instance.
        """
        options = self.options()
        deployment_config = {"nodes": {},
                             "version": 1}

        application_config = dict(
            version=1,
            applications={},
        )

        current_config = {'node2.example.com': {
            'applications': {
                'mysql-something': {
                    'image': 'unknown',
                    'volume': {'mountpoint': None},
                }
            },
            'version': 1
        }}

        expected_current_config = Deployment(nodes=frozenset([
            Node(hostname='node2.example.com', applications=frozenset([
                Application(
                    name='mysql-something',
                    image=DockerImage.from_string('unknown'),
                    ports=frozenset(),
                    volume=AttachedVolume(
                        name='mysql-something',
                        mountpoint=None,
                    )
                ),
            ]))]))

        options.parseOptions(
            [safe_dump(deployment_config),
             safe_dump(application_config),
             safe_dump(current_config),
             b'node1.example.com'])

        self.assertEqual(expected_current_config, options['current'])

    def test_configuration_error(self):
        """
        If the supplied configuration strings are valid `YAML` but are not
        valid, a ``UsageError`` is raised with a string representation of the
        error.
        """
        options = self.options()
        application = Application(
            name=u'mysql-hybridcluster',
            image=DockerImage(
                repository=u'hybridlogic/mysql5.9', tag=u'latest'),
        )

        node = Node(hostname='node1.example.com',
                    applications=frozenset([application]))
        options = self.options()
        deployment_config = {"nodes": {node.hostname: [application.name]},
                             "version": 1}

        exception = self.assertRaises(
            UsageError,
            options.parseOptions,
            [safe_dump(deployment_config), safe_dump({}), safe_dump({}),
             b'node1.example.com']
        )

        self.assertEqual(
            str(exception),
            ("Configuration Error: "
             "Application configuration has an error. Missing "
             "'applications' key.")
        )

    def test_invalid_deployment_yaml(self):
        """
        If the supplied deployment_config is not valid `YAML`, a ``UsageError``
        is raised.
        """
        options = self.options()
        deployment_bad_yaml = "{'foo':'bar', 'x':y, '':'"
        e = self.assertRaises(
            UsageError, options.parseOptions,
            [deployment_bad_yaml, b'', b'{}', b'node1.example.com'])

        # See https://github.com/ClusterHQ/flocker/issues/282 for more complete
        # testing of this string.
        self.assertTrue(
            str(e).startswith('Deployment config could not be parsed as YAML')
        )

    def test_invalid_application_yaml(self):
        """
        If the supplied application_config is not valid `YAML`, a
        ``UsageError`` is raised.
        """
        options = self.options()
        application_bad_yaml = "{'foo':'bar', 'x':y, '':'"
        e = self.assertRaises(
            UsageError, options.parseOptions,
            [b'', application_bad_yaml, b'{}', b'node1.example.com'])

        # See https://github.com/ClusterHQ/flocker/issues/282 for more complete
        # testing of this string.
        self.assertTrue(
            str(e).startswith('Application config could not be parsed as YAML')
        )

    def test_invalid_current_yaml(self):
        """
        If the supplied current config is not valid `YAML`, a
        ``UsageError`` is raised.
        """
        options = self.options()
        bad_yaml = "{'foo':'bar', 'x':y, '':'"
        e = self.assertRaises(
            UsageError, options.parseOptions,
            [b'', b'', bad_yaml, b'node1.example.com'])

        # See https://github.com/ClusterHQ/flocker/issues/282 for more complete
        # testing of this string.
        self.assertTrue(
            str(e).startswith('Current config could not be parsed as YAML')
        )

    def test_hostname_key(self):
        """
        The supplied hostname is assigned to a `hostname` key.
        """
        expected_hostname = u'foobar.example.com'
        options = self.options()
        options.parseOptions(
            [b'{nodes: {}, version: 1}',
             b'{applications: {}, version: 1}',
             b'{}',
             expected_hostname.encode('ascii')])
        self.assertEqual(
            (expected_hostname, unicode),
            (options['hostname'], type(options['hostname']))
        )

    def test_nonascii_hostname(self):
        """
        A ``UsageError`` is raised if the supplied hostname is not ASCII
        encoded.
        """
        hostname = u'\xa3'.encode('utf8')
        options = self.options()
        e = self.assertRaises(
            UsageError,
            options.parseOptions,
            [b'{nodes: {}, version: 1}',
             b'{applications: {}, version: 1}',
             b'{}',
             hostname]
        )

        self.assertEqual(
            "Non-ASCII hostname: {hostname}".format(hostname=hostname),
            str(e)
        )

class ReportStateOptionsTests(StandardOptionsTestsMixin, SynchronousTestCase):
    """
    Tests for :class:`ReportStateOptions`.
    """
    options = ReportStateOptions

    def test_no_options(self):
        """
        ``ReportStateOptions`` can instantiate and successfully parse
        without any (non-standard) options.
        """
        options = self.options()
        options.parseOptions([])

    def test_wrong_number_options(self):
        """
        If any additional arguments are supplied, a ``UsageError`` is raised.
        """
        options = self.options()
        e = self.assertRaises(
            UsageError,
            options.parseOptions,
            ['someparameter']
        )
        self.assertEqual(str(e), b"Wrong number of arguments.")

class ReportStateScriptTests(FlockerScriptTestsMixin, SynchronousTestCase):
    """
    Tests for ``ReportStateScript``.
    """
    script = staticmethod(lambda: ReportStateScript(lambda: None))
    options = ReportStateOptions
    command_name = u'flocker-reportstate'

class ReportStateScriptMainTests(SynchronousTestCase):
    """
    Tests for ``ReportStateScript.main``.
    """
    def test_deployer_type(self):
        """
        ``ReportStateScript._deployer`` is an instance of :class:`Deployer`.
        """
        script = ReportStateScript(lambda: None)
        self.assertIsInstance(script._deployer, Deployer)

    def test_yaml_callback(self):
        """
        ``ReportStateScript.main`` returns a deferred which writes out the
        YAML representation of all the applications (running or not) from
        ``Deployer.discover_node_configuration``
        """
        unit1 = Unit(name=u'site-example.com', activation_state=u'active')
        unit2 = Unit(name=u'site-example.net', activation_state=u'inactive')
        units = {unit1.name: unit1, unit2.name: unit2}

        fake_gear = FakeGearClient(units=units)

        expected = {
            'applications': {
                'site-example.net': {'image': 'unknown', 'ports': []},
                'site-example.com': {'image': 'unknown', 'ports': []}
            },
            'version': 1
        }

        script = ReportStateScript(lambda: create_volume_service(self),
                                   fake_gear)
        content = StringIO()

        def content_capture(data):
            content.write(data)
            content.seek(0)
        self.patch(script, '_print_yaml', content_capture)
        script.main(reactor=object(), options=[])
        self.assertEqual(safe_load(content.read()), expected)
<commit_msg>Lint fix.<commit_after># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Tests for :module:`flocker.node.script`.
"""

from StringIO import StringIO

from twisted.trial.unittest import SynchronousTestCase
from twisted.python.usage import UsageError
from twisted.application.service import Service

from yaml import safe_dump, safe_load
from ...testtools import FlockerScriptTestsMixin, StandardOptionsTestsMixin
from ..script import (
    ChangeStateOptions, ChangeStateScript,
    ReportStateScript, ReportStateOptions)
from ..gear import FakeGearClient, Unit
from .._deploy import Deployer
from .._model import Application, Deployment, DockerImage, Node, AttachedVolume
from ...volume.testtools import create_volume_service

class ChangeStateScriptTests(FlockerScriptTestsMixin, SynchronousTestCase):
    """
    Tests for ``ChangeStateScript``.
    """
    script = staticmethod(lambda: ChangeStateScript(lambda: None))
    options = ChangeStateOptions
    command_name = u'flocker-changestate'

class ChangeStateScriptMainTests(SynchronousTestCase):
    """
    Tests for ``ChangeStateScript.main``.
    """
    def test_deployer_type(self):
        """
        ``ChangeStateScript._deployer`` is an instance of :class:`Deployer`.
        """
        script = ChangeStateScript(lambda: None)
        self.assertIsInstance(script._deployer, Deployer)

    def test_deployer_volume_service(self):
        """
        ``ChangeStateScript._deployer`` is configured with a volume service
        created by the given callable.
        """
        service = object()
        script = ChangeStateScript(lambda: service)
        self.assertIs(script._deployer.volume_service, service)

    def test_main_starts_service(self):
        """
        ``ChangeStateScript.main`` starts the volume service that was created
        by the given callable.
        """
        service = Service()
        script = ChangeStateScript(lambda: service)
        running_before_main = service.running
        self.patch(
            script._deployer, 'change_node_state',
            lambda *args, **kwargs: None)
        options = dict(deployment=object(),
                       current=object(),
                       hostname="")
        script.main(None, options)
        self.assertEqual((running_before_main, service.running),
                         (False, True))

    def test_main_calls_deployer_change_node_state(self):
        """
        ``ChangeStateScript.main`` calls ``Deployer.change_node_state`` with
        the ``Deployment`` and `hostname` supplied on the command line.
        """
        script = ChangeStateScript(lambda: Service())

        change_node_state_calls = []

        def spy_change_node_state(desired_state, current_cluster_state,
                                  hostname):
            """
            A stand in for ``Deployer.change_node_state`` which records calls
            made to it.
            """
            change_node_state_calls.append((desired_state,
                                            current_cluster_state, hostname))

        self.patch(
            script._deployer, 'change_node_state', spy_change_node_state)

        expected_deployment = object()
        expected_current = object()
        expected_hostname = b'node1.example.com'
        options = dict(deployment=expected_deployment,
                       current=expected_current,
                       hostname=expected_hostname)
        script.main(reactor=object(), options=options)

        self.assertEqual(
            [(expected_deployment, expected_current, expected_hostname)],
            change_node_state_calls
        )

class ChangeStateOptionsTests(StandardOptionsTestsMixin, SynchronousTestCase):
    """
    Tests for :class:`ChangeStateOptions`.
    """
    options = ChangeStateOptions

    def test_custom_configs(self):
        """
        The supplied application and deployment configuration strings are
        parsed as a :class:`Deployment` on the options instance.
        """
        application = Application(
            name=u'mysql-hybridcluster',
            image=DockerImage(repository=u'hybridlogic/mysql5.9',
                              tag=u'latest'))

        node = Node(hostname='node1.example.com',
                    applications=frozenset([application]))
        options = self.options()
        deployment_config = {"nodes": {node.hostname: [application.name]},
                             "version": 1}

        application_config = dict(
            version=1,
            applications={
                application.name: {'image': 'hybridlogic/mysql5.9:latest'}
            }
        )

        current_config = {'node2.example.com': {
            'applications': {
                'mysql-something': {
                    'image': 'unknown',
                    'volume': {'mountpoint': None},
                }
            },
            'version': 1
        }}

        options.parseOptions(
            [safe_dump(deployment_config),
             safe_dump(application_config),
             safe_dump(current_config),
             b'node1.example.com'])

        self.assertEqual(
            Deployment(nodes=frozenset([node])), options['deployment'])

    def test_current_configuration(self):
        """
        The supplied current cluster configuration strings is parsed as a
        :class:`Deployment` on the options instance.
        """
        options = self.options()
        deployment_config = {"nodes": {},
                             "version": 1}

        application_config = dict(
            version=1,
            applications={},
        )

        current_config = {'node2.example.com': {
            'applications': {
                'mysql-something': {
                    'image': 'unknown',
                    'volume': {'mountpoint': None},
                }
            },
            'version': 1
        }}

        expected_current_config = Deployment(nodes=frozenset([
            Node(hostname='node2.example.com', applications=frozenset([
                Application(
                    name='mysql-something',
                    image=DockerImage.from_string('unknown'),
                    ports=frozenset(),
                    volume=AttachedVolume(
                        name='mysql-something',
                        mountpoint=None,
                    )
                ),
            ]))]))

        options.parseOptions(
            [safe_dump(deployment_config),
             safe_dump(application_config),
             safe_dump(current_config),
             b'node1.example.com'])

        self.assertEqual(expected_current_config, options['current'])

    def test_configuration_error(self):
        """
        If the supplied configuration strings are valid `YAML` but are not
        valid, a ``UsageError`` is raised with a string representation of the
        error.
        """
        options = self.options()
        application = Application(
            name=u'mysql-hybridcluster',
            image=DockerImage(
                repository=u'hybridlogic/mysql5.9', tag=u'latest'),
        )

        node = Node(hostname='node1.example.com',
                    applications=frozenset([application]))
        options = self.options()
        deployment_config = {"nodes": {node.hostname: [application.name]},
                             "version": 1}

        exception = self.assertRaises(
            UsageError,
            options.parseOptions,
            [safe_dump(deployment_config), safe_dump({}), safe_dump({}),
             b'node1.example.com']
        )

        self.assertEqual(
            str(exception),
            ("Configuration Error: "
             "Application configuration has an error. Missing "
             "'applications' key.")
        )

    def test_invalid_deployment_yaml(self):
        """
        If the supplied deployment_config is not valid `YAML`, a ``UsageError``
        is raised.
        """
        options = self.options()
        deployment_bad_yaml = "{'foo':'bar', 'x':y, '':'"
        e = self.assertRaises(
            UsageError, options.parseOptions,
            [deployment_bad_yaml, b'', b'{}', b'node1.example.com'])

        # See https://github.com/ClusterHQ/flocker/issues/282 for more complete
        # testing of this string.
        self.assertTrue(
            str(e).startswith('Deployment config could not be parsed as YAML')
        )

    def test_invalid_application_yaml(self):
        """
        If the supplied application_config is not valid `YAML`, a
        ``UsageError`` is raised.
        """
        options = self.options()
        application_bad_yaml = "{'foo':'bar', 'x':y, '':'"
        e = self.assertRaises(
            UsageError, options.parseOptions,
            [b'', application_bad_yaml, b'{}', b'node1.example.com'])

        # See https://github.com/ClusterHQ/flocker/issues/282 for more complete
        # testing of this string.
        self.assertTrue(
            str(e).startswith('Application config could not be parsed as YAML')
        )

    def test_invalid_current_yaml(self):
        """
        If the supplied current config is not valid `YAML`, a
        ``UsageError`` is raised.
        """
        options = self.options()
        bad_yaml = "{'foo':'bar', 'x':y, '':'"
        e = self.assertRaises(
            UsageError, options.parseOptions,
            [b'', b'', bad_yaml, b'node1.example.com'])

        # See https://github.com/ClusterHQ/flocker/issues/282 for more complete
        # testing of this string.
        self.assertTrue(
            str(e).startswith('Current config could not be parsed as YAML')
        )

    def test_hostname_key(self):
        """
        The supplied hostname is assigned to a `hostname` key.
        """
        expected_hostname = u'foobar.example.com'
        options = self.options()
        options.parseOptions(
            [b'{nodes: {}, version: 1}',
             b'{applications: {}, version: 1}',
             b'{}',
             expected_hostname.encode('ascii')])
        self.assertEqual(
            (expected_hostname, unicode),
            (options['hostname'], type(options['hostname']))
        )

    def test_nonascii_hostname(self):
        """
        A ``UsageError`` is raised if the supplied hostname is not ASCII
        encoded.
        """
        hostname = u'\xa3'.encode('utf8')
        options = self.options()
        e = self.assertRaises(
            UsageError,
            options.parseOptions,
            [b'{nodes: {}, version: 1}',
             b'{applications: {}, version: 1}',
             b'{}',
             hostname]
        )

        self.assertEqual(
            "Non-ASCII hostname: {hostname}".format(hostname=hostname),
            str(e)
        )

class ReportStateOptionsTests(StandardOptionsTestsMixin, SynchronousTestCase):
    """
    Tests for :class:`ReportStateOptions`.
    """
    options = ReportStateOptions

    def test_no_options(self):
        """
        ``ReportStateOptions`` can instantiate and successfully parse
        without any (non-standard) options.
        """
        options = self.options()
        options.parseOptions([])

    def test_wrong_number_options(self):
        """
        If any additional arguments are supplied, a ``UsageError`` is raised.
        """
        options = self.options()
        e = self.assertRaises(
            UsageError,
            options.parseOptions,
            ['someparameter']
        )
        self.assertEqual(str(e), b"Wrong number of arguments.")

class ReportStateScriptTests(FlockerScriptTestsMixin, SynchronousTestCase):
    """
    Tests for ``ReportStateScript``.
    """
    script = staticmethod(lambda: ReportStateScript(lambda: None))
    options = ReportStateOptions
    command_name = u'flocker-reportstate'

class ReportStateScriptMainTests(SynchronousTestCase):
    """
    Tests for ``ReportStateScript.main``.
    """
    def test_deployer_type(self):
        """
        ``ReportStateScript._deployer`` is an instance of :class:`Deployer`.
        """
        script = ReportStateScript(lambda: None)
        self.assertIsInstance(script._deployer, Deployer)

    def test_yaml_callback(self):
        """
        ``ReportStateScript.main`` returns a deferred which writes out the
        YAML representation of all the applications (running or not) from
        ``Deployer.discover_node_configuration``
        """
        unit1 = Unit(name=u'site-example.com', activation_state=u'active')
        unit2 = Unit(name=u'site-example.net', activation_state=u'inactive')
        units = {unit1.name: unit1, unit2.name: unit2}

        fake_gear = FakeGearClient(units=units)

        expected = {
            'applications': {
                'site-example.net': {'image': 'unknown', 'ports': []},
                'site-example.com': {'image': 'unknown', 'ports': []}
            },
            'version': 1
        }

        script = ReportStateScript(lambda: create_volume_service(self),
                                   fake_gear)
        content = StringIO()

        def content_capture(data):
            content.write(data)
            content.seek(0)
        self.patch(script, '_print_yaml', content_capture)
        script.main(reactor=object(), options=[])
        self.assertEqual(safe_load(content.read()), expected)
<|endoftext|>


<commit_before>#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License
#

from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

import os
import re
import system_test
import unittest
from subprocess import PIPE
import subprocess
import shutil
from proton import Url, SSLDomain, SSLUnavailable, SASL
from system_test import main_module

class ConsoleTest(system_test.TestCase):
    """Run console tests"""
    @classmethod
    def setUpClass(cls):
        super(ConsoleTest, cls).setUpClass()
        config = system_test.Qdrouterd.Config([
            ('router', {'id': 'QDR.A', 'workerThreads': 1}),
            ('listener', {'port': cls.tester.get_port()}),
        ])
        cls.router = cls.tester.qdrouterd('test-router', config)

    def run_console_test(self):
        cwd = os.getcwd()   # expecting <base-path>/build/system_test.dir/system_tests_console/ConsoleTest/test_console
        def get_base(remove):
            base = cwd.split('/')[:-remove]   #path that ends with qpid-dispatch's home dir
            test_cmd = '/'.join(base + ['build', 'console', 'node_modules', '.bin', 'mocha'])
            test_dir = '/'.join(base + ['console', 'stand-alone', 'test'])
            src_dir = '/'.join(base + ['console', 'stand-alone'])
            return (base, test_cmd, test_dir, src_dir)

        (base, test_cmd, test_dir, src_dir) = get_base(6)
        from_ctest = os.path.isdir(src_dir)
        if not from_ctest:
            (base, test_cmd, test_dir, src_dir) = get_base(5)

        # The console test needs a node_modules dir in the source directory
        # If the node_modules dir is not present in the source dir, create it.
        # An alternative is to copy all the source files to the build/console dir.
        node_dir = '/'.join(base + ['console', 'stand-alone', 'node_modules'])
        node_modules = os.path.isdir(node_dir)
        if not node_modules:
            p0 = subprocess.Popen(['npm', 'install', '--loglevel=error'], stdout=PIPE, cwd=src_dir)
            p0.wait();

        prg = [test_cmd,'--require', 'babel-core/register', test_dir, '--src=%s/' % src_dir]
        p = self.popen(prg, stdout=PIPE, expect=None)
        out = p.communicate()[0]

        # write the output
        with open('run_console_test.out', 'w') as popenfile:
            popenfile.write('returncode was %s\n' % p.returncode)
            popenfile.write('out was:\n')
            popenfile.writelines(out)

        # if we created the node_modules dir, remove it
        if not node_modules:
            shutil.rmtree(node_dir)

        assert p.returncode == 0, \
            "console test exit status %s, output:\n%s" % (p.returncode, out)
        return out

    def test_console(self):
        self.run_console_test()

if __name__ == '__main__':
    unittest.main(main_module())
<commit_msg>DISPATCH-1057 Add space after # to make python happy. Add more error checking in console test<commit_after>#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License
#

from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

import os
import re
import system_test
import unittest
from subprocess import PIPE
import subprocess
import shutil
from proton import Url, SSLDomain, SSLUnavailable, SASL
from system_test import main_module

class ConsoleTest(system_test.TestCase):
    """Run console tests"""
    @classmethod
    def setUpClass(cls):
        super(ConsoleTest, cls).setUpClass()
        cls.http_port = cls.tester.get_port()
        config = system_test.Qdrouterd.Config([
            ('router', {'id': 'QDR.A', 'workerThreads': 1}),
            ('listener', {'port': cls.http_port, 'http': True}),
        ])
        cls.router = cls.tester.qdrouterd('test-router', config)

    def run_console_test(self):
        cwd = os.getcwd()   # expecting <base-path>/build/system_test.dir/system_tests_console/ConsoleTest/test_console
                            #    /foo/qpid-dispatch/build/system_test.dir/system_tests_console/ConsoleTest/test_console/
                            #
                            #
                            # run_console_test.out
        def get_base(remove):
            base = cwd.split('/')[:-remove]   # path that ends with qpid-dispatch's home dir
            test_cmd = '/'.join(base + ['build', 'console', 'node_modules', '.bin', 'mocha'])
            test_dir = '/'.join(base + ['console', 'stand-alone', 'test'])
            src_dir = '/'.join(base + ['console', 'stand-alone'])
            return (base, test_cmd, test_dir, src_dir)

        (base, test_cmd, test_dir, src_dir) = get_base(6)
        found_src = os.path.isdir(src_dir)
        # running the test from the command line results in a different path
        if not found_src:
            (base, test_cmd, test_dir, src_dir) = get_base(5)
            found_src = os.path.isdir(src_dir)

        pret = 0
        out = 'Skipped'
        if found_src:  # if we are unable to find the console's source directory. Skip the test
            # The console test needs a node_modules dir in the source directory
            # If the node_modules dir is not present in the source dir, create it.
            # An alternative is to copy all the source files to the build/console dir.
            node_dir = '/'.join(base + ['console', 'stand-alone', 'node_modules'])
            node_modules = os.path.isdir(node_dir)
            if not node_modules:
                p0 = subprocess.Popen(['npm', 'install', '--loglevel=error'], stdout=PIPE, cwd=src_dir)
                p0.wait();

            prg = [test_cmd,'--require', 'babel-core/register', test_dir, '--http_port=%s' % self.http_port, '--src=%s/' % src_dir]
            p = self.popen(prg, stdout=PIPE, expect=None)
            out = p.communicate()[0]
            pret = p.returncode

            # write the output
            with open('run_console_test.out', 'w') as popenfile:
                popenfile.write('returncode was %s\n' % p.returncode)
                popenfile.write('out was:\n')
                popenfile.writelines(out)

            # if we created the node_modules dir, remove it
            if not node_modules:
                shutil.rmtree(node_dir)

        assert pret == 0, \
            "console test exit status %s, output:\n%s" % (pret, out)
        return out

    def test_console(self):
        self.run_console_test()

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


<commit_before># Software License Agreement (BSD License)
#
# Copyright (c) 2013, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * 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.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
# COPYRIGHT OWNER 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: Ioan Sucan

from moveit_commander import MoveGroupCommander, MoveItCommanderException
from moveit_ros_planning_interface import _moveit_robot_interface
from moveit_msgs.msg import RobotState
import conversions

class RobotCommander(object):

    class Joint(object):
        def __init__(self, robot, name):
            self._robot = robot
            self._name = name
        def name(self):
            return self._name
        def variable_count(self):
            return len(self.__get_joint_limits())
        def bounds(self):
            l = self.__get_joint_limits()
            if len(l) == 1:
                return l[0]
            else:
                return l
        def min_bound(self):
            limits = self.__get_joint_limits()
            if len(limits) == 1:
                return limits[0][0]
            else:
                return [l[0] for l in limits]
        def max_bound(self):
            limits = self.__get_joint_limits()
            if len(limits) == 1:
                return limits[0][1]
            else:
                return [l[1] for l in limits]
        def value(self):
            vals = self._robot._r.get_current_joint_values(self._name)
            if len(vals) == 1:
                return vals[0]
            else:
                return vals
        def move(self, position, wait = True):
            group = self._robot.get_default_owner_group()
            if group is None:
                raise MoveItCommanderException("There is no known group containing joint %s. Cannot move." % self._name)
            gc = self._robot.get_group(group)
            if gc is not None:
                gc.set_joint_value_target(gc.get_current_joint_values())
                gc.set_joint_value_target(self._name, position)
                return gc.go(wait)
            return False
        def __get_joint_limits(self):
            return self._robot._r.get_joint_limits(self._name)

    class Link(object):
        def __init__(self, robot, name):
            self._robot = robot
            self._name = name
        def name(self):
            return self._name
        def pose(self):
            return conversions.list_to_pose_stamped(self._robot._r.get_link_pose(self._name), self._robot.get_planning_frame())

    def __init__(self):
        self._r = _moveit_robot_interface.RobotInterface("robot_description")
        self._groups = {}
        self._joint_owner_group = {}

    def get_planning_frame(self):
        """Get the frame of reference in which planning is done (and environment is maintained)"""
        return self._r.get_planning_frame()

    def get_root_link(self):
        """Get the name of the root link of the robot model """
        return self._r.get_robot_root_link()

    def get_joint_names(self, group = None):
        """Get the names of all the movable joints that make up a group (mimic joints and fixed joints are excluded). If no group name is specified, all joints in the robot model are returned, including fixed and mimic joints """
        if group is not None:
            if self.has_group(group):
                return self._r.get_group_joint_names(group)
            else:
                raise MoveItCommanderException("There is no group named %s" % group)
        else:
            return self._r.get_joint_names()

    def get_link_names(self, group = None):
        """Get the links that make up a group. If no group name is specified, all the links in the robot model are returned. """
        if group is not None:
            if self.has_group(group):
                return self._r.get_group_link_names(group)
            else:
                raise MoveItCommanderException("There is no group named %s" % group)
        else:
            return self._r.get_link_names()

    def get_group_names(self):
        """Get the names of the groups defined for the robot"""
        return self._r.get_group_names()

    def get_current_state(self):
        """ Get a RobotState message describing the current state of the robot"""
        s = RobotState()
        s.deserialize(self._r.get_current_state())
        return s

    def get_current_variable_values(self):
        """Get a dictionary mapping variable names to values. Note that a joint may consist of one or more variables """
        return self._r.get_current_variable_values()

    def get_joint(self, name):
        if name in self.get_joint_names():
            return self.Joint(self, name)
        else:
            raise MoveItCommanderException("There is no joint named %s" % name)

    def get_link(self, name):
        if name in self.get_link_names():
            return self.Link(self, name)
        else:
            raise MoveItCommanderException("There is no link named %s" % name)

    def get_group(self, name):
        if not self._groups.has_key(name):
            if not self.has_group(name):
                raise MoveItCommanderException("There is no group named %s" % name)
            self._groups[name] = MoveGroupCommander(name)
        return self._groups[name]

    def has_group(self, name):
        return self._r.has_group(name)

    def get_default_owner_group(self, joint_name):
        """Get the name of the smallest group (fewest joints) that includes the joint name specified as argument"""
        if not self._joint_owner_groups.has_key(joint_name):
            group = None
            for g in self.get_group_names():
                if joint_name in self.get_joint_names(g):
                    if group is None:
                        group = g
                    else:
                        if len(self.get_link_names(g)) < len(self.get_link_names(group)):
                            group = g
            self._joint_owner_groups[joint_name] = group
        return self._joint_owner_groups[joint_name]

    def __getattr__(self, name):
        """ We catch the names of groups, joints and links to allow easy access to their properties """
        if name in self.get_group_names():
            return self.get_group(name)
        elif name in self.get_joint_names():
            return self.Joint(self,name)
        elif name in self.get_link_names():
            return self.Link(self,name)
        else:
            return object.__getattribute__(self, name)
<commit_msg>[moveit_commander/robot.py] Code cleaning; semi-PEP8.<commit_after># Software License Agreement (BSD License)
#
# Copyright (c) 2013, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * 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.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
# COPYRIGHT OWNER 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: Ioan Sucan

from moveit_commander import MoveGroupCommander, MoveItCommanderException
from moveit_ros_planning_interface import _moveit_robot_interface
from moveit_msgs.msg import RobotState
import conversions

class RobotCommander(object):

    class Joint(object):
        def __init__(self, robot, name):
            self._robot = robot
            self._name = name

        def name(self):
            return self._name

        def variable_count(self):
            return len(self.__get_joint_limits())

        def bounds(self):
            l = self.__get_joint_limits()
            if len(l) == 1:
                return l[0]
            else:
                return l

        def min_bound(self):
            limits = self.__get_joint_limits()
            if len(limits) == 1:
                return limits[0][0]
            else:
                return [l[0] for l in limits]

        def max_bound(self):
            limits = self.__get_joint_limits()
            if len(limits) == 1:
                return limits[0][1]
            else:
                return [l[1] for l in limits]

        def value(self):
            vals = self._robot._r.get_current_joint_values(self._name)
            if len(vals) == 1:
                return vals[0]
            else:
                return vals

        def move(self, position, wait=True):
            group = self._robot.get_default_owner_group()
            if group is None:
                raise MoveItCommanderException("There is no known group containing joint %s. Cannot move." % self._name)
            gc = self._robot.get_group(group)
            if gc is not None:
                gc.set_joint_value_target(gc.get_current_joint_values())
                gc.set_joint_value_target(self._name, position)
                return gc.go(wait)
            return False

        def __get_joint_limits(self):
            return self._robot._r.get_joint_limits(self._name)

    class Link(object):
        def __init__(self, robot, name):
            self._robot = robot
            self._name = name

        def name(self):
            return self._name

        def pose(self):
            return conversions.list_to_pose_stamped(self._robot._r.get_link_pose(self._name), self._robot.get_planning_frame())

    def __init__(self):
        self._r = _moveit_robot_interface.RobotInterface("robot_description")
        self._groups = {}
        self._joint_owner_group = {}

    def get_planning_frame(self):
        """
        Get the frame of reference in which planning is done (and environment
        is maintained)
        """
        return self._r.get_planning_frame()

    def get_root_link(self):
        """Get the name of the root link of the robot model """
        return self._r.get_robot_root_link()

    def get_joint_names(self, group=None):
        """
        Get the names of all the movable joints that make up a group
        (mimic joints and fixed joints are excluded). If no group name is
        specified, all joints in the robot model are returned, including
        fixed and mimic joints.
        """
        if group is not None:
            if self.has_group(group):
                return self._r.get_group_joint_names(group)
            else:
                raise MoveItCommanderException("There is no group named %s" % group)
        else:
            return self._r.get_joint_names()

    def get_link_names(self, group=None):
        """
        Get the links that make up a group. If no group name is specified,
        all the links in the robot model are returned.
        """
        if group is not None:
            if self.has_group(group):
                return self._r.get_group_link_names(group)
            else:
                raise MoveItCommanderException("There is no group named %s" % group)
        else:
            return self._r.get_link_names()

    def get_group_names(self):
        """Get the names of the groups defined for the robot"""
        return self._r.get_group_names()

    def get_current_state(self):
        """ Get a RobotState message describing the current state of the robot"""
        s = RobotState()
        s.deserialize(self._r.get_current_state())
        return s

    def get_current_variable_values(self):
        """
        Get a dictionary mapping variable names to values.
        Note that a joint may consist of one or more variables.
        """
        return self._r.get_current_variable_values()

    def get_joint(self, name):
        if name in self.get_joint_names():
            return self.Joint(self, name)
        else:
            raise MoveItCommanderException("There is no joint named %s" % name)

    def get_link(self, name):
        if name in self.get_link_names():
            return self.Link(self, name)
        else:
            raise MoveItCommanderException("There is no link named %s" % name)

    def get_group(self, name):
        if not self._groups.has_key(name):
            if not self.has_group(name):
                raise MoveItCommanderException("There is no group named %s" % name)
            self._groups[name] = MoveGroupCommander(name)
        return self._groups[name]

    def has_group(self, name):
        return self._r.has_group(name)

    def get_default_owner_group(self, joint_name):
        """
        Get the name of the smallest group (fewest joints) that includes
        the joint name specified as argument.
        """
        if not self._joint_owner_groups.has_key(joint_name):
            group = None
            for g in self.get_group_names():
                if joint_name in self.get_joint_names(g):
                    if group is None:
                        group = g
                    else:
                        if len(self.get_link_names(g)) < len(self.get_link_names(group)):
                            group = g
            self._joint_owner_groups[joint_name] = group
        return self._joint_owner_groups[joint_name]

    def __getattr__(self, name):
        """
        We catch the names of groups, joints and links to allow easy access
        to their properties.
        """
        if name in self.get_group_names():
            return self.get_group(name)
        elif name in self.get_joint_names():
            return self.Joint(self, name)
        elif name in self.get_link_names():
            return self.Link(self, name)
        else:
            return object.__getattribute__(self, name)
<|endoftext|>


<commit_before>#!/usr/bin/python3

import sys
sys.path.insert(0, "/usr/local/opencv3/lib/python2.7/site-packages/")

import argparse
import cv2
import fnmatch
import os.path

sys.path.append('../lib')
import ProjectMgr

# for all the images in source_dir, scale them and write the scaled
# version to dest_dir.
#
# converter = 'imagemagick' uses the 'convert' command.  This should
#             preserve image meta data (exif, etc.)
#
# converter = 'opencv' uses opencv commands to resize the image.  This
#             is much faster than the external convert command, but
#             does not preserve any meta data.

parser = argparse.ArgumentParser(description='Import a directory of images into a project.')
parser.add_argument('--project', required=True, help='project directory')
parser.add_argument('--source', required=True, help='image source directory')
parser.add_argument('--scale', type=float, default=0.25, help='scale factor')
parser.add_argument('--converter', default='imagemagick',
                    choices=['imagemagick', 'opencv'])

args = parser.parse_args()

proj = ProjectMgr.ProjectMgr(args.project)
proj.set_source_dir( args.source )
proj.import_images( scale=args.scale, converter=args.converter)
proj.load_image_info(force_compute_sizes=True)
proj.save()

<commit_msg>Prepair to depricate this script.<commit_after>#!/usr/bin/python3

import sys

import argparse
import cv2
import fnmatch
import os.path

sys.path.append('../lib')
import ProjectMgr

# This script is deprecated and will soon be relegated to the archive
# folder.  The detector script will read directly from the image
# source directory and scale the images on the fly, then remap the
# feature coordinates back to the original dimensions.

print("This script is deprecated, please see script comments for details.")
quit()

# for all the images in source_dir, scale them and write the scaled
# version to dest_dir.
#
# converter = 'imagemagick' uses the 'convert' command.  This should
#             preserve image meta data (exif, etc.)
#
# converter = 'opencv' uses opencv commands to resize the image.  This
#             is much faster than the external convert command, but
#             does not preserve any meta data.

parser = argparse.ArgumentParser(description='Import a directory of images into a project.')
parser.add_argument('--project', required=True, help='project directory')
parser.add_argument('--source', required=True, help='image source directory')
parser.add_argument('--scale', type=float, default=0.25, help='scale factor')
parser.add_argument('--converter', default='imagemagick',
                    choices=['imagemagick', 'opencv'])

args = parser.parse_args()

proj = ProjectMgr.ProjectMgr(args.project)
proj.set_source_dir( args.source )
proj.import_images( scale=args.scale, converter=args.converter)
proj.load_image_info(force_compute_sizes=True)
proj.save()

<|endoftext|>


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

import threading

from module.plugins.internal.Plugin import Plugin
from module.plugins.internal.misc import Periodical, isiterable

def threaded(fn):
    def run(*args, **kwargs):
        hookManager.startThread(fn, *args, **kwargs)

    return run

class Expose(object):
    """
    Used for decoration to declare rpc services
    """
    def __new__(cls, fn, *args, **kwargs):
        hookManager.addRPC(fn.__module__, fn.func_name, fn.func_doc)
        return fn

class Addon(Plugin):
    __name__    = "Addon"
    __type__    = "hook"  #@TODO: Change to `addon` in 0.4.10
    __version__ = "0.50"
    __status__  = "stable"

    __threaded__ = []  #@TODO: Remove in 0.4.10

    __description__ = """Base addon plugin"""
    __license__     = "GPLv3"
    __authors__     = [("Walter Purcaro", "vuolter@gmail.com")]

    def __init__(self, core, manager):
        self._init(core)

        #: `HookManager`
        self.manager = manager
        self.lock    = threading.Lock()

        #: Automatically register event listeners for functions, attribute will be deleted dont use it yourself
        self.event_map = {}

        self.info['ip'] = None  #@TODO: Remove in 0.4.10

        #: Callback of periodical job task, used by HookManager
        self.periodical = Periodical(self, self.periodical_task)
        self.cb = self.periodical.cb  #@TODO: Recheck in 0.4.10

        self.init()
        self._init_events()  #@TODO: Remove in 0.4.10
        self.init_events()

    @property
    def activated(self):
        """
        Checks if addon is activated
        """
        return self.config.get("activated")

    #@TODO: Remove in 0.4.10
    def _log(self, level, plugintype, pluginname, messages):
        plugintype = "addon" if plugintype is "hook" else plugintype
        return super(Addon, self)._log(level, plugintype, pluginname, messages)

    #@TODO: Remove in 0.4.10
    def _init_events(self):
        event_map = {'allDownloadsFinished' : "all_downloads_finished" ,
                     'allDownloadsProcessed': "all_downloads_processed",
                     'configChanged'        : "config_changed"         ,
                     'download_processed'   : "download_processed"     ,
                     'download_start'       : "download_start"         ,
                     'linksAdded'           : "links_added"            ,
                     'packageDeleted'       : "package_deleted"        ,
                     'package_failed'       : "package_failed"         ,
                     'package_processed'    : "package_processed"      }
        for event, funcs in event_map.items():
            self.manager.addEvent(event, getattr(self, funcs))

    def init_events(self):
        if self.event_map:
            for event, funcs in self.event_map.items():
                if isiterable(funcs):
                    for f in funcs:
                        self.manager.addEvent(event, getattr(self, f))
                else:
                    self.manager.addEvent(event, getattr(self, funcs))

            #: Delete for various reasons
            self.event_map = None

    def periodical_task(self):
        raise NotImplementedError

    #: Deprecated method, use `activated` property instead (Remove in 0.4.10)
    def isActivated(self):
        return self.activated

    def deactivate(self):
        """
        Called when addon was deactivated
        """
        pass

    #: Deprecated method, use `deactivate` instead (Remove in 0.4.10)
    def unload(self):
        self.db.store("info", self.info)
        return self.deactivate()

    def activate(self):
        """
        Called when addon was activated
        """
        pass

    #: Deprecated method, use `activate` instead (Remove in 0.4.10)
    def coreReady(self):
        self.db.retrieve("info", self.info)
        return self.activate()

    def exit(self):
        """
        Called by core.shutdown just before pyLoad exit
        """
        pass

    #: Deprecated method, use `exit` instead (Remove in 0.4.10)
    def coreExiting(self):
        self.unload()  #@TODO: Fix in 0.4.10
        return self.exit()

    def config_changed(self, category, option, value, section):
        pass

    def all_downloads_finished(self):
        pass

    def all_downloads_processed(self):
        pass

    def links_added(self, urls, pypack):
        pass

    def download_preparing(self, pyfile):
        pass

    #: Deprecated method, use `download_preparing` instead (Remove in 0.4.10)
    def downloadPreparing(self, pyfile):
        if pyfile.plugin.req is not None:  #@TODO: Remove in 0.4.10
            return self.download_preparing(pyfile)

    def download_start(self, pyfile, url, filename):
        pass

    def download_processed(self, pyfile):
        pass

    def download_finished(self, pyfile):
        pass

    #: Deprecated method, use `download_finished` instead (Remove in 0.4.10)
    def downloadFinished(self, pyfile):
        if pyfile.hasStatus("finished"):  #: Check if still "finished" (Fix in 0.4.10)
            return self.download_finished(pyfile)

    def download_failed(self, pyfile):
        pass

    #: Deprecated method, use `download_failed` instead (Remove in 0.4.10)
    def downloadFailed(self, pyfile):
        if pyfile.hasStatus("failed"):  #: Check if still "failed" (Fix in 0.4.10)
            return self.download_failed(pyfile)

    def package_processed(self, pypack):
        pass

    def package_deleted(self, pid):
        pass

    def package_failed(self, pypack):
        pass

    def package_finished(self, pypack):
        pass

    #: Deprecated method, use `package_finished` instead (Remove in 0.4.10)
    def packageFinished(self, pypack):
        return self.package_finished(pypack)

    def before_reconnect(self, ip):
        pass

    #: Deprecated method, use `before_reconnect` instead (Remove in 0.4.10)
    def beforeReconnecting(self, ip):
        return self.before_reconnect(ip)

    def after_reconnect(self, ip, oldip):
        pass

    #: Deprecated method, use `after_reconnect` instead (Remove in 0.4.10)
    def afterReconnecting(self, ip):
        self.after_reconnect(ip, self.info['ip'])
        self.info['ip'] = ip

    def captcha_task(self, task):
        """
        New captcha task for the plugin, it MUST set the handler and timeout or will be ignored
        """
        pass

    #: Deprecated method, use `captcha_task` instead (Remove in 0.4.10)
    def newCaptchaTask(self, task):
        return self.captcha_task(task)

    def captcha_correct(self, task):
        pass

    #: Deprecated method, use `captcha_correct` instead (Remove in 0.4.10)
    def captchaCorrect(self, task):
        return self.captcha_correct(task)

    def captcha_invalid(self, task):
        pass

    #: Deprecated method, use `captcha_invalid` instead (Remove in 0.4.10)
    def captchaInvalid(self, task):
        return self.captcha_invalid(task)
<commit_msg>[Addon] Fix download_finished call<commit_after># -*- coding: utf-8 -*-

import threading

from module.plugins.internal.Plugin import Plugin
from module.plugins.internal.misc import Periodical, isiterable

def threaded(fn):
    def run(*args, **kwargs):
        hookManager.startThread(fn, *args, **kwargs)

    return run

class Expose(object):
    """
    Used for decoration to declare rpc services
    """
    def __new__(cls, fn, *args, **kwargs):
        hookManager.addRPC(fn.__module__, fn.func_name, fn.func_doc)
        return fn

class Addon(Plugin):
    __name__    = "Addon"
    __type__    = "hook"  #@TODO: Change to `addon` in 0.4.10
    __version__ = "0.51"
    __status__  = "stable"

    __threaded__ = []  #@TODO: Remove in 0.4.10

    __description__ = """Base addon plugin"""
    __license__     = "GPLv3"
    __authors__     = [("Walter Purcaro", "vuolter@gmail.com")]

    def __init__(self, core, manager):
        self._init(core)

        #: `HookManager`
        self.manager = manager
        self.lock    = threading.Lock()

        #: Automatically register event listeners for functions, attribute will be deleted dont use it yourself
        self.event_map = {}

        self.info['ip'] = None  #@TODO: Remove in 0.4.10

        #: Callback of periodical job task, used by HookManager
        self.periodical = Periodical(self, self.periodical_task)
        self.cb = self.periodical.cb  #@TODO: Recheck in 0.4.10

        self.init()
        self._init_events()  #@TODO: Remove in 0.4.10
        self.init_events()

    @property
    def activated(self):
        """
        Checks if addon is activated
        """
        return self.config.get("activated")

    #@TODO: Remove in 0.4.10
    def _log(self, level, plugintype, pluginname, messages):
        plugintype = "addon" if plugintype is "hook" else plugintype
        return super(Addon, self)._log(level, plugintype, pluginname, messages)

    #@TODO: Remove in 0.4.10
    def _init_events(self):
        event_map = {'allDownloadsFinished' : "all_downloads_finished" ,
                     'allDownloadsProcessed': "all_downloads_processed",
                     'configChanged'        : "config_changed"         ,
                     'download_processed'   : "download_processed"     ,
                     'download_start'       : "download_start"         ,
                     'linksAdded'           : "links_added"            ,
                     'packageDeleted'       : "package_deleted"        ,
                     'package_failed'       : "package_failed"         ,
                     'package_processed'    : "package_processed"      }
        for event, funcs in event_map.items():
            self.manager.addEvent(event, getattr(self, funcs))

    def init_events(self):
        if self.event_map:
            for event, funcs in self.event_map.items():
                if isiterable(funcs):
                    for f in funcs:
                        self.manager.addEvent(event, getattr(self, f))
                else:
                    self.manager.addEvent(event, getattr(self, funcs))

            #: Delete for various reasons
            self.event_map = None

    def periodical_task(self):
        raise NotImplementedError

    #: Deprecated method, use `activated` property instead (Remove in 0.4.10)
    def isActivated(self):
        return self.activated

    def deactivate(self):
        """
        Called when addon was deactivated
        """
        pass

    #: Deprecated method, use `deactivate` instead (Remove in 0.4.10)
    def unload(self):
        self.db.store("info", self.info)
        return self.deactivate()

    def activate(self):
        """
        Called when addon was activated
        """
        pass

    #: Deprecated method, use `activate` instead (Remove in 0.4.10)
    def coreReady(self):
        self.db.retrieve("info", self.info)
        return self.activate()

    def exit(self):
        """
        Called by core.shutdown just before pyLoad exit
        """
        pass

    #: Deprecated method, use `exit` instead (Remove in 0.4.10)
    def coreExiting(self):
        self.unload()  #@TODO: Fix in 0.4.10
        return self.exit()

    def config_changed(self, category, option, value, section):
        pass

    def all_downloads_finished(self):
        pass

    def all_downloads_processed(self):
        pass

    def links_added(self, urls, pypack):
        pass

    def download_preparing(self, pyfile):
        pass

    #: Deprecated method, use `download_preparing` instead (Remove in 0.4.10)
    def downloadPreparing(self, pyfile):
        if pyfile.plugin.req is not None:  #@TODO: Remove in 0.4.10
            return self.download_preparing(pyfile)

    def download_start(self, pyfile, url, filename):
        pass

    def download_processed(self, pyfile):
        pass

    def download_finished(self, pyfile):
        pass

    #: Deprecated method, use `download_finished` instead (Remove in 0.4.10)
    def downloadFinished(self, pyfile):
        return self.download_finished(pyfile)

    def download_failed(self, pyfile):
        pass

    #: Deprecated method, use `download_failed` instead (Remove in 0.4.10)
    def downloadFailed(self, pyfile):
        if pyfile.hasStatus("failed"):  #: Check if still "failed" (Fix in 0.4.10)
            return self.download_failed(pyfile)

    def package_processed(self, pypack):
        pass

    def package_deleted(self, pid):
        pass

    def package_failed(self, pypack):
        pass

    def package_finished(self, pypack):
        pass

    #: Deprecated method, use `package_finished` instead (Remove in 0.4.10)
    def packageFinished(self, pypack):
        return self.package_finished(pypack)

    def before_reconnect(self, ip):
        pass

    #: Deprecated method, use `before_reconnect` instead (Remove in 0.4.10)
    def beforeReconnecting(self, ip):
        return self.before_reconnect(ip)

    def after_reconnect(self, ip, oldip):
        pass

    #: Deprecated method, use `after_reconnect` instead (Remove in 0.4.10)
    def afterReconnecting(self, ip):
        self.after_reconnect(ip, self.info['ip'])
        self.info['ip'] = ip

    def captcha_task(self, task):
        """
        New captcha task for the plugin, it MUST set the handler and timeout or will be ignored
        """
        pass

    #: Deprecated method, use `captcha_task` instead (Remove in 0.4.10)
    def newCaptchaTask(self, task):
        return self.captcha_task(task)

    def captcha_correct(self, task):
        pass

    #: Deprecated method, use `captcha_correct` instead (Remove in 0.4.10)
    def captchaCorrect(self, task):
        return self.captcha_correct(task)

    def captcha_invalid(self, task):
        pass

    #: Deprecated method, use `captcha_invalid` instead (Remove in 0.4.10)
    def captchaInvalid(self, task):
        return self.captcha_invalid(task)
<|endoftext|>


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

import os
import logging
import time
import sys
from datetime import datetime

logging.basicConfig(filename='controller.log',level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

EXEVERS=False

def main():
    if sys.platform.startswith('win'):
        if EXEVERS:
            status = os.system("\"server.exe\" 2>> error.log")
        else:
            status = os.system("\"server.py\" 2>> error.log")
    else:
        status = os.system("python server.py")
    logging.info("Server stopped with a status code of: "+str(status))
    #             Windows               Linux
    if str(status)=="5" or str(status)=="1280":
        print str(datetime.today())+" [Controller] Server stopped."
        os._exit(0)
    elif str(status)=="10" or str(status)=="2560":
        print str(datetime.today())+" [Controller] Server restarted."
        status=""
        main()
    elif str(status)=="11" or str(status)=="2816":
        print str(datetime.today())+" [Controller] Server restarted to clear error log."
        if sys.platform.startswith('win'):
            os.system("del error.log")
        else:
            os.system("rm ./error.log")
        status=""
        main()
    elif str(status)=="12" or str(status)=="3072":
        print str(datetime.today())+" [Controller] Delayed restart. Restarting in 10 minutes."
        time.sleep(600)
        status=""
        main()
    elif str(status)=="13" or str(status)=="3328":
        print str(datetime.today())+" [Controller] Delayed restart. Restarting in 5 minutes."
        time.sleep(300)
        status=""
        main()
    elif str(status)=="14" or str(status)=="3584":
        print str(datetime.today())+" [Controller] Delayed restart. Restarting in 20 minutes."
        time.sleep(1200)
        status=""
        main()
    elif str(status)=="15": #Unused
        if EXEVERS:
            print str(datetime.today())+" [Controller] Transformice Server.exe missing."
        else:
            print str(datetime.today())+" [Controller] Transformice Server.py missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="16" or str(status)=="4096":
        print str(datetime.today())+" [Controller] Kikoo.swf missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="17" or str(status)=="4352":
        print str(datetime.today())+" [Controller] dbfile.sqlite empty/missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="18": #Does not apply to linux
        print str(datetime.today())+" [Controller] uptime.exe missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="19" or str(status)=="4864":
        if EXEVERS:
            print str(datetime.today())+" [Controller] Controller.exe renamed."
        else:
            print str(datetime.today())+" [Controller] Controller.py renamed."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="20" or str(status)=="5120":
        #Server update.
        print str(datetime.today())+" [Controller] Server restarted for update."
        if EXEVERS:
            os.system("del \"Transformice Server.exe\"")
            os.system("rename \"UPDATE.DAT\" \"Transformice Server.exe\"")
        else:
            if sys.platform.startswith('win'):
                os.system("del \"Transformice Server.py\"")
                os.system("rename \"UPDATE.DAT\" \"Transformice Server.py\"")
            else:
                os.system("rm ./Transformice\\ Server.py")
                os.system("mv ./UPDATE.DAT ./Transformice\\ Server.py")
        status=""
        main()
    elif str(status)=="256":
        if not sys.platform.startswith('win'):
            print "A problem was found in the configuration, attempting to fix..."
            os.system("sudo touch /etc/authbind/byport/443")
            os.system("sudo chown administrator:administrator /etc/authbind/byport/443")
            os.system("sudo chmod 755 /etc/authbind/byport/443")
            print "Restart Controller.py and see if it works."
            os._exit(0)
        else:
            print str(datetime.today())+" [Controller] Server crashed, restarting in 10 seconds."
            time.sleep(10)
            status=""
            main()
    elif str(status)=="50":
        print str(datetime.today())+" [Controller] Invalid Name/Key."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    elif str(status)=="51":
        print str(datetime.today())+" [Controller] Validation Failure."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    elif str(status)=="52":
        print str(datetime.today())+" [Controller] Unable to connect to validation server."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    elif str(status)=="53":
        print str(datetime.today())+" [Controller] Misc validation failure."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    else:
        print str(datetime.today())+" [Controller] Server crashed, restarting in 10 seconds."
        time.sleep(10)
        status=""
        main()

if __name__ == '__main__':
    main()
<commit_msg>Change Copyright for Controller.py<commit_after># This file is in the public domain.

import os
import logging
import time
import sys
from datetime import datetime

logging.basicConfig(filename='controller.log',level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

EXEVERS=False

def main():
    if sys.platform.startswith('win'):
        if EXEVERS:
            status = os.system("\"server.exe\" 2>> error.log")
        else:
            status = os.system("\"server.py\" 2>> error.log")
    else:
        status = os.system("python server.py")
    logging.info("Server stopped with a status code of: "+str(status))
    #             Windows               Linux
    if str(status)=="5" or str(status)=="1280":
        print str(datetime.today())+" [Controller] Server stopped."
        os._exit(0)
    elif str(status)=="10" or str(status)=="2560":
        print str(datetime.today())+" [Controller] Server restarted."
        status=""
        main()
    elif str(status)=="11" or str(status)=="2816":
        print str(datetime.today())+" [Controller] Server restarted to clear error log."
        if sys.platform.startswith('win'):
            os.system("del error.log")
        else:
            os.system("rm ./error.log")
        status=""
        main()
    elif str(status)=="12" or str(status)=="3072":
        print str(datetime.today())+" [Controller] Delayed restart. Restarting in 10 minutes."
        time.sleep(600)
        status=""
        main()
    elif str(status)=="13" or str(status)=="3328":
        print str(datetime.today())+" [Controller] Delayed restart. Restarting in 5 minutes."
        time.sleep(300)
        status=""
        main()
    elif str(status)=="14" or str(status)=="3584":
        print str(datetime.today())+" [Controller] Delayed restart. Restarting in 20 minutes."
        time.sleep(1200)
        status=""
        main()
    elif str(status)=="15": #Unused
        if EXEVERS:
            print str(datetime.today())+" [Controller] Transformice Server.exe missing."
        else:
            print str(datetime.today())+" [Controller] Transformice Server.py missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="16" or str(status)=="4096":
        print str(datetime.today())+" [Controller] Kikoo.swf missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="17" or str(status)=="4352":
        print str(datetime.today())+" [Controller] dbfile.sqlite empty/missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="18": #Does not apply to linux
        print str(datetime.today())+" [Controller] uptime.exe missing."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="19" or str(status)=="4864":
        if EXEVERS:
            print str(datetime.today())+" [Controller] Controller.exe renamed."
        else:
            print str(datetime.today())+" [Controller] Controller.py renamed."
        time.sleep(10)
        status=""
        main()
    elif str(status)=="20" or str(status)=="5120":
        #Server update.
        print str(datetime.today())+" [Controller] Server restarted for update."
        if EXEVERS:
            os.system("del \"Transformice Server.exe\"")
            os.system("rename \"UPDATE.DAT\" \"Transformice Server.exe\"")
        else:
            if sys.platform.startswith('win'):
                os.system("del \"Transformice Server.py\"")
                os.system("rename \"UPDATE.DAT\" \"Transformice Server.py\"")
            else:
                os.system("rm ./Transformice\\ Server.py")
                os.system("mv ./UPDATE.DAT ./Transformice\\ Server.py")
        status=""
        main()
    elif str(status)=="256":
        if not sys.platform.startswith('win'):
            print "A problem was found in the configuration, attempting to fix..."
            os.system("sudo touch /etc/authbind/byport/443")
            os.system("sudo chown administrator:administrator /etc/authbind/byport/443")
            os.system("sudo chmod 755 /etc/authbind/byport/443")
            print "Restart Controller.py and see if it works."
            os._exit(0)
        else:
            print str(datetime.today())+" [Controller] Server crashed, restarting in 10 seconds."
            time.sleep(10)
            status=""
            main()
    elif str(status)=="50":
        print str(datetime.today())+" [Controller] Invalid Name/Key."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    elif str(status)=="51":
        print str(datetime.today())+" [Controller] Validation Failure."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    elif str(status)=="52":
        print str(datetime.today())+" [Controller] Unable to connect to validation server."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    elif str(status)=="53":
        print str(datetime.today())+" [Controller] Misc validation failure."
        if sys.platform.startswith('win'):
            os.system("cmd")
        os._exit(0)
    else:
        print str(datetime.today())+" [Controller] Server crashed, restarting in 10 seconds."
        time.sleep(10)
        status=""
        main()

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


<commit_before># encoding: utf-8

"""Basic file controller."""

import webob

import web.core

from web.extras.contentment.api import action, view
from web.extras.contentment.components.asset.controller import AssetController
from web.extras.contentment.components.file.scaler import scale

from cStringIO import StringIO

log = __import__('logging').getLogger(__name__)
__all__ = ['FileController']

class FileController(AssetController):
    @view('Preview', "Preview uploaded content.")
    def view_preview(self):
        return 'preview', None

    @view('Download', "Download this file.")
    def view_download(self, filename=None, inline=False):
        asset = self.asset
        response = webob.Response(request=web.core.request, conditional_response=True)
        filename = asset.filename if filename is None else filename

        response.content_type = asset.mimetype
        response.content_length = asset.size
        response.last_modified = asset.modified if asset.modified else asset.created
        response.accept_ranges = 'bytes'
        response.etag = '%s-%s-%s' % (asset.modified if asset.modified else asset.created, asset.size, hash(filename))
        response.cache_control = 'public'
        response.content_transfer_encoding = 'binary'
        response.content_disposition = "inline" if inline else ('attachment; filename=' + filename)
        response.range = (0, asset.size)

        response.app_iter = asset.content.get()

        return response

    # TODO: Caching.
    @view('Scale', "Display a scaled version of this file.")
    def view_scale(self, filename=None, inline=True, **kw):
        asset = self.asset
        response = webob.Response(request=web.core.request, conditional_response=True)
        filename = asset.filename if filename is None else filename

        response.content_type = "image/jpeg"
        response.content_length = asset.size
        response.last_modified = asset.modified if asset.modified else asset.created
        response.accept_ranges = 'bytes'
        response.etag = '%s-%s-%s' % (asset.modified if asset.modified else asset.created, asset.size, hash(filename))
        response.cache_control = 'public'
        response.content_transfer_encoding = 'binary'
        response.content_disposition = "inline" if inline else ('attachment; filename=' + filename)
        response.range = (0, asset.size)

        target = StringIO()
        scale(asset.content.get(), target, raw=False, **kw)
        response.body = target.getvalue()

        return response

    # TODO: Raw stream.
    # TODO: Preview.  (Utilizes the embedded view.)
    # TODO: Embedded view.  (w/ handlers for application/pdf, text/*, image/*, audio/*, and video/*.)
<commit_msg>Removed old TODOs.<commit_after># encoding: utf-8

"""Basic file controller."""

import webob

import web.core

from web.extras.contentment.api import action, view
from web.extras.contentment.components.asset.controller import AssetController
from web.extras.contentment.components.file.scaler import scale

from cStringIO import StringIO

log = __import__('logging').getLogger(__name__)
__all__ = ['FileController']

class FileController(AssetController):
    @view('Preview', "Preview uploaded content.")
    def view_preview(self):
        return 'preview', None

    @view('Download', "Download this file.")
    def view_download(self, filename=None, inline=False):
        asset = self.asset
        response = webob.Response(request=web.core.request, conditional_response=True)
        filename = asset.filename if filename is None else filename

        response.content_type = asset.mimetype
        response.content_length = asset.size
        response.last_modified = asset.modified if asset.modified else asset.created
        response.accept_ranges = 'bytes'
        response.etag = '%s-%s-%s' % (asset.modified if asset.modified else asset.created, asset.size, hash(filename))
        response.cache_control = 'public'
        response.content_transfer_encoding = 'binary'
        response.content_disposition = "inline" if inline else ('attachment; filename=' + filename)
        response.range = (0, asset.size)

        response.app_iter = asset.content.get()

        return response

    # TODO: Caching.
    @view('Scale', "Display a scaled version of this file.")
    def view_scale(self, filename=None, inline=True, **kw):
        asset = self.asset
        response = webob.Response(request=web.core.request, conditional_response=True)
        filename = asset.filename if filename is None else filename

        response.content_type = "image/jpeg"
        response.content_length = asset.size
        response.last_modified = asset.modified if asset.modified else asset.created
        response.accept_ranges = 'bytes'
        response.etag = '%s-%s-%s' % (asset.modified if asset.modified else asset.created, asset.size, hash(filename))
        response.cache_control = 'public'
        response.content_transfer_encoding = 'binary'
        response.content_disposition = "inline" if inline else ('attachment; filename=' + filename)
        response.range = (0, asset.size)

        target = StringIO()
        scale(asset.content.get(), target, raw=False, **kw)
        response.body = target.getvalue()

        return response
<|endoftext|>