<commit_before>import unittest

import numpy

import chainer
from chainer import functions
from chainer import testing
from chainer.testing import attr
import chainerx

if chainerx.is_available():
    import chainerx.testing

@testing.parameterize(*testing.product_dict(
    [
        {'shape': (3, 4)},
        {'shape': ()},
    ],
    [
        {'in_type': numpy.bool_},
        {'in_type': numpy.uint8},
        {'in_type': numpy.uint64},
        {'in_type': numpy.int8},
        {'in_type': numpy.int64},
        {'in_type': numpy.float16},
        {'in_type': numpy.float32},
        {'in_type': numpy.float64},
    ],
    [
        {'out_type': numpy.bool_},
        {'out_type': numpy.uint8},
        {'out_type': numpy.uint64},
        {'out_type': numpy.int8},
        {'out_type': numpy.int64},
        {'out_type': numpy.float16},
        {'out_type': numpy.float32},
        {'out_type': numpy.float64},
    ]
))
@testing.inject_backend_tests(
    None,
    # CPU tests
    [
        {},
    ]
    # GPU tests
    + testing.product({
        'use_cuda': [True],
        'use_cudnn': ['never', 'always'],
        'cuda_device': [0, 1],
    })
    # ChainerX tests
    + [
        {'use_chainerx': True, 'chainerx_device': 'native:0'},
        {'use_chainerx': True, 'chainerx_device': 'cuda:0'},
        {'use_chainerx': True, 'chainerx_device': 'cuda:1'},
    ]
)
@attr.chainerx
class TestCast(testing.FunctionTestCase):

    def _skip_chainerx_unsupported_dtype(self):
        supported_dtypes = chainerx.testing.dtypes.all_dtypes
        if (self.in_type.__name__ not in supported_dtypes
                or self.out_type.__name__ not in supported_dtypes):
            raise unittest.SkipTest(
                'ChainerX does not support either of {} or {} dtypes'.format(
                    self.in_type.__name__, self.out_type.__name__))

    def setUp(self):
        # Skip e.g. uint64 for ChainerX.
        self._skip_chainerx_unsupported_dtype()

        if (numpy.dtype(self.in_type).kind != 'f'
                or numpy.dtype(self.out_type).kind != 'f'):
            self.skip_backward_test = True
            self.skip_double_backward_test = True

        self.check_backward_options = {
            'eps': 2.0 ** -2, 'atol': 1e-2, 'rtol': 1e-3}
        self.check_double_backward_options = {
            'eps': 2.0 ** -2, 'atol': 1e-2, 'rtol': 1e-3}

    def generate_inputs(self):
        x = numpy.asarray(numpy.random.randn(*self.shape)).astype(self.in_type)
        # The result of a cast from a negative floating-point number to
        # an unsigned integer is not specified. Avoid testing that condition.
        float_to_uint = (
            issubclass(self.in_type, numpy.floating)
            and issubclass(self.out_type, numpy.unsignedinteger))
        if float_to_uint:
            x[x < 0] *= -1
        return x,

    def forward_expected(self, inputs):
        x, = inputs
        return x.astype(self.out_type),

    def forward(self, inputs, devices):
        x, = inputs
        y = functions.cast(x, self.out_type)
        return y,

class TestNoCast(unittest.TestCase):

    def setUp(self):
        self.dtype = numpy.float32
        self.x = numpy.empty(1, self.dtype)

    def check_forward_no_cast(self, x_data):
        y = functions.cast(x_data, self.dtype)
        assert isinstance(y, chainer.Variable)
        assert y.data is x_data

    def test_forward_no_cast_array(self):
        y = functions.cast(self.x, self.dtype)
        assert isinstance(y, chainer.Variable)
        assert y.data is self.x

    def test_forward_no_cast_variable(self):
        # If backprop is disabled, it's safe to simply return the input
        # variable for no-op casts.
        x = chainer.Variable(self.x)
        with chainer.using_config('enable_backprop', False):
            y = functions.cast(x, self.dtype)
        assert y is x

    def test_forward_no_cast_grad(self):
        # This test would fail if F.cast does not create new function nodes for
        # no-op casts
        x = chainer.Variable(self.x)
        y1 = functions.cast(x, self.dtype)
        y2 = functions.cast(x, self.dtype)
        z = y1 + y2
        gy1, gy2 = chainer.grad([z], [y1, y2], [numpy.ones_like(z.data)])
        assert gy1.dtype == self.dtype
        assert gy2.dtype == self.dtype
        numpy.testing.assert_array_equal(gy1.data, numpy.ones_like(y1.data))
        numpy.testing.assert_array_equal(gy2.data, numpy.ones_like(y2.data))

testing.run_module(__name__, __file__)
<commit_msg>Avoid empty in F.cast test that can cause overflow warning<commit_after>import unittest

import numpy

import chainer
from chainer import functions
from chainer import testing
from chainer.testing import attr
import chainerx

if chainerx.is_available():
    import chainerx.testing

@testing.parameterize(*testing.product_dict(
    [
        {'shape': (3, 4)},
        {'shape': ()},
    ],
    [
        {'in_type': numpy.bool_},
        {'in_type': numpy.uint8},
        {'in_type': numpy.uint64},
        {'in_type': numpy.int8},
        {'in_type': numpy.int64},
        {'in_type': numpy.float16},
        {'in_type': numpy.float32},
        {'in_type': numpy.float64},
    ],
    [
        {'out_type': numpy.bool_},
        {'out_type': numpy.uint8},
        {'out_type': numpy.uint64},
        {'out_type': numpy.int8},
        {'out_type': numpy.int64},
        {'out_type': numpy.float16},
        {'out_type': numpy.float32},
        {'out_type': numpy.float64},
    ]
))
@testing.inject_backend_tests(
    None,
    # CPU tests
    [
        {},
    ]
    # GPU tests
    + testing.product({
        'use_cuda': [True],
        'use_cudnn': ['never', 'always'],
        'cuda_device': [0, 1],
    })
    # ChainerX tests
    + [
        {'use_chainerx': True, 'chainerx_device': 'native:0'},
        {'use_chainerx': True, 'chainerx_device': 'cuda:0'},
        {'use_chainerx': True, 'chainerx_device': 'cuda:1'},
    ]
)
@attr.chainerx
class TestCast(testing.FunctionTestCase):

    def _skip_chainerx_unsupported_dtype(self):
        supported_dtypes = chainerx.testing.dtypes.all_dtypes
        if (self.in_type.__name__ not in supported_dtypes
                or self.out_type.__name__ not in supported_dtypes):
            raise unittest.SkipTest(
                'ChainerX does not support either of {} or {} dtypes'.format(
                    self.in_type.__name__, self.out_type.__name__))

    def setUp(self):
        # Skip e.g. uint64 for ChainerX.
        self._skip_chainerx_unsupported_dtype()

        if (numpy.dtype(self.in_type).kind != 'f'
                or numpy.dtype(self.out_type).kind != 'f'):
            self.skip_backward_test = True
            self.skip_double_backward_test = True

        self.check_backward_options = {
            'eps': 2.0 ** -2, 'atol': 1e-2, 'rtol': 1e-3}
        self.check_double_backward_options = {
            'eps': 2.0 ** -2, 'atol': 1e-2, 'rtol': 1e-3}

    def generate_inputs(self):
        x = numpy.asarray(numpy.random.randn(*self.shape)).astype(self.in_type)
        # The result of a cast from a negative floating-point number to
        # an unsigned integer is not specified. Avoid testing that condition.
        float_to_uint = (
            issubclass(self.in_type, numpy.floating)
            and issubclass(self.out_type, numpy.unsignedinteger))
        if float_to_uint:
            x[x < 0] *= -1
        return x,

    def forward_expected(self, inputs):
        x, = inputs
        return x.astype(self.out_type),

    def forward(self, inputs, devices):
        x, = inputs
        y = functions.cast(x, self.out_type)
        return y,

class TestNoCast(unittest.TestCase):

    def setUp(self):
        self.dtype = numpy.float32
        self.x = numpy.random.uniform(-100, 100, (1,)).astype(self.dtype)

    def check_forward_no_cast(self, x_data):
        y = functions.cast(x_data, self.dtype)
        assert isinstance(y, chainer.Variable)
        assert y.data is x_data

    def test_forward_no_cast_array(self):
        y = functions.cast(self.x, self.dtype)
        assert isinstance(y, chainer.Variable)
        assert y.data is self.x

    def test_forward_no_cast_variable(self):
        # If backprop is disabled, it's safe to simply return the input
        # variable for no-op casts.
        x = chainer.Variable(self.x)
        with chainer.using_config('enable_backprop', False):
            y = functions.cast(x, self.dtype)
        assert y is x

    def test_forward_no_cast_grad(self):
        # This test would fail if F.cast does not create new function nodes for
        # no-op casts
        x = chainer.Variable(self.x)
        y1 = functions.cast(x, self.dtype)
        y2 = functions.cast(x, self.dtype)
        z = y1 + y2
        gy1, gy2 = chainer.grad([z], [y1, y2], [numpy.ones_like(z.data)])
        assert gy1.dtype == self.dtype
        assert gy2.dtype == self.dtype
        numpy.testing.assert_array_equal(gy1.data, numpy.ones_like(y1.data))
        numpy.testing.assert_array_equal(gy2.data, numpy.ones_like(y2.data))

testing.run_module(__name__, __file__)
<|endoftext|>


<commit_before>import unittest

import numpy

import chainer
from chainer import cuda
from chainer import gradient_check
from chainer import links
from chainer import testing
from chainer.testing import attr

def _sigmoid(x):
    xp = cuda.get_array_module(x)
    half = x.dtype.type(0.5)
    return xp.tanh(x * half) * half + half

def _gru(func, h, x):
    xp = cuda.get_array_module(h, x)

    r = _sigmoid(x.dot(func.W_r.W.data.T) + h.dot(func.U_r.W.data.T))
    z = _sigmoid(x.dot(func.W_z.W.data.T) + h.dot(func.U_z.W.data.T))
    h_bar = xp.tanh(x.dot(func.W.W.data.T) + (r * h).dot(func.U.W.data.T))
    y = (1 - z) * h + z * h_bar
    return y

@testing.parameterize(
    {'gru': links.StatelessGRU, 'state': 'random',
     'in_size': 3, 'out_size': 5},
    {'gru': links.StatelessGRU, 'state': 'random', 'out_size': 5},
    {'gru': links.GRU, 'state': 'random', 'in_size': 3, 'out_size': 5},
    {'gru': links.GRU, 'state': 'zero', 'in_size': 3, 'out_size': 5},
    {'gru': links.StatefulGRU, 'state': 'random', 'in_size': 3, 'out_size': 5},
    {'gru': links.StatefulGRU, 'state': 'zero', 'in_size': 3, 'out_size': 5},
)
class TestGRU(unittest.TestCase):

    def setUp(self):
        if self.gru == links.StatelessGRU:
            if hasattr(self, 'in_size'):
                self.link = self.gru(self.in_size, self.out_size)
            else:
                self.link = self.gru(None, self.out_size)
                self.in_size = self.out_size
        elif self.gru == links.StatefulGRU or self.gru == links.GRU:
            self.link = self.gru(self.in_size, self.out_size)
        else:
            self.fail('Unsupported link(only GRU, StatelessGRU and '
                      'StatefulGRU are supported):{}'.format(self.gru))

        self.x = numpy.random.uniform(
            -1, 1, (3, self.in_size)).astype(numpy.float32)
        if self.state == 'random':
            self.h = numpy.random.uniform(
                -1, 1, (3, self.out_size)).astype(numpy.float32)
        elif self.state == 'zero':
            self.h = numpy.zeros((3, self.out_size), dtype=numpy.float32)
        else:
            self.fail('Unsupported state initialization:{}'.format(self.state))
        self.gy = numpy.random.uniform(
            -1, 1, (3, self.out_size)).astype(numpy.float32)

    def _forward(self, link, h, x):
        if isinstance(link, links.StatelessGRU):
            return link(h, x)
        else:
            if self.state != 'zero':
                link.set_state(h)
            return link(x)

    def check_forward(self, h_data, x_data):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self._forward(self.link, h, x)

        self.assertEqual(y.data.dtype, numpy.float32)
        y_expect = _gru(self.link, h_data, x_data)
        testing.assert_allclose(y_expect, y.data)
        if isinstance(self.link, links.StatefulGRU):
            testing.assert_allclose(self.link.h.data, y.data)

    def test_forward_cpu(self):
        self.check_forward(self.h, self.x)

    @attr.gpu
    def test_forward_gpu(self):
        self.link.to_gpu()
        self.check_forward(cuda.to_gpu(self.h),
                           cuda.to_gpu(self.x))

    def check_backward(self, h_data, x_data, y_grad):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self._forward(self.link, h, x)
        y.grad = y_grad
        y.backward()

        def f():
            return _gru(self.link, h_data, x_data),
        gx, = gradient_check.numerical_grad(f, (x.data,), (y.grad,))
        testing.assert_allclose(gx, x.grad, atol=1e-3)

        if isinstance(self.link, links.StatelessGRU):
            gh, = gradient_check.numerical_grad(f, (h.data,), (y.grad,))
            testing.assert_allclose(gh, h.grad, atol=1e-3)

    def test_backward_cpu(self):
        self.check_backward(self.h, self.x, self.gy)

    @attr.gpu
    def test_backward_gpu(self):
        self.link.to_gpu()
        self.check_backward(cuda.to_gpu(self.h),
                            cuda.to_gpu(self.x),
                            cuda.to_gpu(self.gy))

@testing.parameterize(
    *testing.product({
        'link_array_module': ['to_cpu', 'to_gpu'],
        'state_array_module': ['to_cpu', 'to_gpu'],
        'gru': [links.GRU, links.StatefulGRU]
    }))
class TestGRUState(unittest.TestCase):

    def setUp(self):
        in_size, out_size = 10, 8
        self.link = self.gru(in_size, out_size)
        self.h = chainer.Variable(
            numpy.random.uniform(-1, 1, (3, out_size)).astype(numpy.float32))

    def check_set_state(self, h):
        self.link.set_state(h)
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)

    def test_set_state_cpu(self):
        self.check_set_state(self.h)

    @attr.gpu
    def test_set_state_gpu(self):
        getattr(self.link, self.link_array_module)()
        getattr(self.h, self.state_array_module)()
        self.check_set_state(self.h)

    def check_reset_state(self):
        self.link.reset_state()
        self.assertIsNone(self.link.h)

    def test_reset_state_cpu(self):
        self.check_reset_state()

    @attr.gpu
    def test_reset_state_gpu(self):
        getattr(self.link, self.link_array_module)()
        self.check_reset_state()

@testing.parameterize(
    {'gru': links.GRU},
    {'gru': links.StatefulGRU}
)
class TestGRUToCPUToGPU(unittest.TestCase):

    def setUp(self):
        in_size, out_size = 10, 8
        self.link = links.StatefulGRU(in_size, out_size)
        self.h = chainer.Variable(
            numpy.random.uniform(-1, 1, (3, out_size)).astype(numpy.float32))

    def check_to_cpu(self, h):
        self.link.set_state(h)
        self.link.to_cpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_cpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)

    def test_to_cpu_cpu(self):
        self.check_to_cpu(self.h)

    @attr.gpu
    def test_to_cpu_gpu(self):
        self.h.to_gpu()
        self.check_to_cpu(self.h)

    def check_to_cpu_to_gpu(self, h):
        self.link.set_state(h)
        self.link.to_gpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_gpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_cpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_gpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)

    @attr.gpu
    def test_to_cpu_to_gpu_cpu(self):
        self.check_to_cpu_to_gpu(self.h)

    @attr.gpu
    def test_to_cpu_to_gpu_gpu(self):
        self.h.to_gpu()
        self.check_to_cpu_to_gpu(self.h)

class InvalidCallOfGRU(unittest.TestCase):

    def setUp(self):
        self.gru = links.GRU(10, 10)

    def test_no_argument(self):
        with self.assertRaises(ValueError):
            self.gru()

    def test_too_many_argument_1(self):
        x = numpy.random.uniform(-1, 1, (5, 10))
        h = numpy.random.uniform(-1, 1, (5, 10))
        with self.assertRaises(ValueError):
            self.gru(x, h)

    def test_too_many_argument_2(self):
        x = numpy.random.uniform(-1, 1, (5, 10))
        h = numpy.random.uniform(-1, 1, (5, 10))
        z = numpy.random.uniform(-1, 1, (5, 10))
        with self.assertRaises(ValueError):
            self.gru(x, h, z)

testing.run_module(__name__, __file__)
<commit_msg>autopep8<commit_after>import unittest

import numpy

import chainer
from chainer import cuda
from chainer import gradient_check
from chainer import links
from chainer import testing
from chainer.testing import attr

def _sigmoid(x):
    xp = cuda.get_array_module(x)
    half = x.dtype.type(0.5)
    return xp.tanh(x * half) * half + half

def _gru(func, h, x):
    xp = cuda.get_array_module(h, x)

    r = _sigmoid(x.dot(func.W_r.W.data.T) + h.dot(func.U_r.W.data.T))
    z = _sigmoid(x.dot(func.W_z.W.data.T) + h.dot(func.U_z.W.data.T))
    h_bar = xp.tanh(x.dot(func.W.W.data.T) + (r * h).dot(func.U.W.data.T))
    y = (1 - z) * h + z * h_bar
    return y

@testing.parameterize(
    {'gru': links.StatelessGRU, 'state': 'random',
     'in_size': 3, 'out_size': 5},
    {'gru': links.StatelessGRU, 'state': 'random', 'out_size': 5},
    {'gru': links.GRU, 'state': 'random', 'in_size': 3, 'out_size': 5},
    {'gru': links.GRU, 'state': 'zero', 'in_size': 3, 'out_size': 5},
    {'gru': links.StatefulGRU, 'state': 'random', 'in_size': 3, 'out_size': 5},
    {'gru': links.StatefulGRU, 'state': 'zero', 'in_size': 3, 'out_size': 5},
)
class TestGRU(unittest.TestCase):

    def setUp(self):
        if self.gru == links.StatelessGRU:
            if hasattr(self, 'in_size'):
                self.link = self.gru(self.in_size, self.out_size)
            else:
                self.link = self.gru(None, self.out_size)
                self.in_size = self.out_size
        elif self.gru == links.StatefulGRU or self.gru == links.GRU:
            self.link = self.gru(self.in_size, self.out_size)
        else:
            self.fail('Unsupported link(only GRU, StatelessGRU and '
                      'StatefulGRU are supported):{}'.format(self.gru))

        self.x = numpy.random.uniform(
            -1, 1, (3, self.in_size)).astype(numpy.float32)
        if self.state == 'random':
            self.h = numpy.random.uniform(
                -1, 1, (3, self.out_size)).astype(numpy.float32)
        elif self.state == 'zero':
            self.h = numpy.zeros((3, self.out_size), dtype=numpy.float32)
        else:
            self.fail('Unsupported state initialization:{}'.format(self.state))
        self.gy = numpy.random.uniform(
            -1, 1, (3, self.out_size)).astype(numpy.float32)

    def _forward(self, link, h, x):
        if isinstance(link, links.StatelessGRU):
            return link(h, x)
        else:
            if self.state != 'zero':
                link.set_state(h)
            return link(x)

    def check_forward(self, h_data, x_data):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self._forward(self.link, h, x)

        self.assertEqual(y.data.dtype, numpy.float32)
        y_expect = _gru(self.link, h_data, x_data)
        testing.assert_allclose(y_expect, y.data)
        if isinstance(self.link, links.StatefulGRU):
            testing.assert_allclose(self.link.h.data, y.data)

    def test_forward_cpu(self):
        self.check_forward(self.h, self.x)

    @attr.gpu
    def test_forward_gpu(self):
        self.link.to_gpu()
        self.check_forward(cuda.to_gpu(self.h),
                           cuda.to_gpu(self.x))

    def check_backward(self, h_data, x_data, y_grad):
        h = chainer.Variable(h_data)
        x = chainer.Variable(x_data)
        y = self._forward(self.link, h, x)
        y.grad = y_grad
        y.backward()

        def f():
            return _gru(self.link, h_data, x_data),
        gx, = gradient_check.numerical_grad(f, (x.data,), (y.grad,))
        testing.assert_allclose(gx, x.grad, atol=1e-3)

        if isinstance(self.link, links.StatelessGRU):
            gh, = gradient_check.numerical_grad(f, (h.data,), (y.grad,))
            testing.assert_allclose(gh, h.grad, atol=1e-3)

    def test_backward_cpu(self):
        self.check_backward(self.h, self.x, self.gy)

    @attr.gpu
    def test_backward_gpu(self):
        self.link.to_gpu()
        self.check_backward(cuda.to_gpu(self.h),
                            cuda.to_gpu(self.x),
                            cuda.to_gpu(self.gy))

@testing.parameterize(
    *testing.product({
        'link_array_module': ['to_cpu', 'to_gpu'],
        'state_array_module': ['to_cpu', 'to_gpu'],
        'gru': [links.GRU, links.StatefulGRU]
    }))
class TestGRUState(unittest.TestCase):

    def setUp(self):
        in_size, out_size = 10, 8
        self.link = self.gru(in_size, out_size)
        self.h = chainer.Variable(
            numpy.random.uniform(-1, 1, (3, out_size)).astype(numpy.float32))

    def check_set_state(self, h):
        self.link.set_state(h)
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)

    def test_set_state_cpu(self):
        self.check_set_state(self.h)

    @attr.gpu
    def test_set_state_gpu(self):
        getattr(self.link, self.link_array_module)()
        getattr(self.h, self.state_array_module)()
        self.check_set_state(self.h)

    def check_reset_state(self):
        self.link.reset_state()
        self.assertIsNone(self.link.h)

    def test_reset_state_cpu(self):
        self.check_reset_state()

    @attr.gpu
    def test_reset_state_gpu(self):
        getattr(self.link, self.link_array_module)()
        self.check_reset_state()

@testing.parameterize(
    {'gru': links.GRU},
    {'gru': links.StatefulGRU}
)
class TestGRUToCPUToGPU(unittest.TestCase):

    def setUp(self):
        in_size, out_size = 10, 8
        self.link = links.StatefulGRU(in_size, out_size)
        self.h = chainer.Variable(
            numpy.random.uniform(-1, 1, (3, out_size)).astype(numpy.float32))

    def check_to_cpu(self, h):
        self.link.set_state(h)
        self.link.to_cpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_cpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)

    def test_to_cpu_cpu(self):
        self.check_to_cpu(self.h)

    @attr.gpu
    def test_to_cpu_gpu(self):
        self.h.to_gpu()
        self.check_to_cpu(self.h)

    def check_to_cpu_to_gpu(self, h):
        self.link.set_state(h)
        self.link.to_gpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_gpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_cpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        self.link.to_gpu()
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)

    @attr.gpu
    def test_to_cpu_to_gpu_cpu(self):
        self.check_to_cpu_to_gpu(self.h)

    @attr.gpu
    def test_to_cpu_to_gpu_gpu(self):
        self.h.to_gpu()
        self.check_to_cpu_to_gpu(self.h)

class InvalidCallOfGRU(unittest.TestCase):

    def setUp(self):
        self.gru = links.GRU(10, 10)

    def test_no_argument(self):
        with self.assertRaises(ValueError):
            self.gru()

    def test_too_many_argument_1(self):
        x = numpy.random.uniform(-1, 1, (5, 10))
        h = numpy.random.uniform(-1, 1, (5, 10))
        with self.assertRaises(ValueError):
            self.gru(x, h)

    def test_too_many_argument_2(self):
        x = numpy.random.uniform(-1, 1, (5, 10))
        h = numpy.random.uniform(-1, 1, (5, 10))
        z = numpy.random.uniform(-1, 1, (5, 10))
        with self.assertRaises(ValueError):
            self.gru(x, h, z)

testing.run_module(__name__, __file__)
<|endoftext|>


<commit_before>#!/usr/bin/python

# -*- coding: utf-8 -*-

"""
PyCOMPSs Testbench
========================
"""

# Imports
import argparse
from pycompss.api.task import task

@task()
def assign(letter):
    print("%s squad ready to distribute computation" % letter)

def main():
    print(" - Parsing arguments")
    # Arguments parsing
    parser = argparse.ArgumentParser()

    parser.add_argument('-a', '--alpha', type=int, help="Alpha parameter", required=True)
    parser.add_argument('-b', '--beta', type=int, help="Beta parameter", required=True)
    parser.add_argument('-g', '--gamma', type=int, help="Gamma parameter", required=True)
    parser.add_argument('-d', '--delta', type=str, help="Delta parameter",
                        required=True)
    parser.add_argument('-e', '--epsilon', type=float, help="Epsilon parameter",
                        required=True)
    parser.add_argument('-z', '--zeta', type=int, help="Zeta parameter",
                        required=True)

    args = parser.parse_args()
    alpha = args.alpha
    beta = args.beta
    gamma = args.gamma
    delta = args.delta
    epsilon = args.epsilon
    zeta = args.zeta

    values = [alpha, beta, gamma, delta, epsilon, zeta]
    expected_values = [6, 66, 666, "z6666", 6.6, 666666]

    for v in values:
        if type(v) == int:
            assign(v)

    if values == expected_values:
        print("Argparse received the expected arguments and values:\n%s" % values)
    else:
        print("Argparse did not received the expected arguments and values\n",
              "Expected: %s\nGot: %s" % (expected_values, values))

if __name__ == "__main__":
    main()
<commit_msg>Fixed print and added barrier to test 71<commit_after>#!/usr/bin/python

# -*- coding: utf-8 -*-

"""
PyCOMPSs Testbench
========================
"""

# Imports
import argparse
from pycompss.api.task import task
from pycompss.api.api import compss_barrier

@task()
def assign(letter):
    print("%s squad ready to distribute computation" % letter)

def main():
    print(" - Parsing arguments")
    # Arguments parsing
    parser = argparse.ArgumentParser()

    parser.add_argument('-a', '--alpha', type=int, help="Alpha parameter", required=True)
    parser.add_argument('-b', '--beta', type=int, help="Beta parameter", required=True)
    parser.add_argument('-g', '--gamma', type=int, help="Gamma parameter", required=True)
    parser.add_argument('-d', '--delta', type=str, help="Delta parameter",
                        required=True)
    parser.add_argument('-e', '--epsilon', type=float, help="Epsilon parameter",
                        required=True)
    parser.add_argument('-z', '--zeta', type=int, help="Zeta parameter",
                        required=True)

    args = parser.parse_args()
    alpha = args.alpha
    beta = args.beta
    gamma = args.gamma
    delta = args.delta
    epsilon = args.epsilon
    zeta = args.zeta

    values = [alpha, beta, gamma, delta, epsilon, zeta]
    expected_values = [6, 66, 666, "z6666", 6.6, 666666]

    for v in values:
        if type(v) == int:
            assign(v)

    compss_barrier()

    if values == expected_values:
        print("Argparse received the expected arguments and values:\n%s" % values)
    else:
        print("Argparse did not received the expected arguments and values.")
        print("Expected: %s" % (expected_values))
        print("Got: %s" % (values))

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


<commit_before>from common_fixtures import *  # NOQA
import requests

def test_sibling_pinging(client, one_per_host):
    instances = one_per_host
    hosts = {}
    hostnames = set()

    for i in instances:
        port = i.ports()[0]

        host = port.publicIpAddress().address
        port = port.publicPort
        base_url = 'http://{}:{}'.format(host, port)
        pong = requests.get(base_url + '/ping').text
        hostname = requests.get(base_url + '/hostname').text

        assert pong == 'pong'
        assert hostname not in hostnames

        hostnames.add(hostname)
        hosts[hostname] = base_url

    count = 0
    for hostname, base_url in hosts.items():
        url = base_url + '/get'
        for other_hostname, other_url in hosts.items():
            if other_hostname == hostname:
                continue

            test_hostname = requests.get(url, params={
                'url': other_url + '/hostname'
            }).text

            count += 1
            assert other_hostname == test_hostname

    assert count == len(instances) * (len(instances) - 1)

    delete_all(client, instances)

def test_dynamic_port(client, test_name, managed_network):
    c = client.create_container(name=test_name,
                                networkIds=[managed_network.id],
                                imageUuid=TEST_IMAGE_UUID)
    c = client.wait_success(c)

    ports = c.ports()
    assert len(ports) == 1

    port = ports[0]

    assert port.publicPort is None

    port = client.wait_success(client.update(port, publicPort=3001))

    assert port.publicPort == 3001
    ping_port(port)

    port = client.wait_success(client.update(port, publicPort=3002))

    assert port.publicPort == 3002
    ping_port(port)

    delete_all(client, [c])

def test_linking(client, test_name, managed_network):
    hosts = client.list_host(kind='docker', removed_null=True)
    assert len(hosts) > 2

    random_val = random_str()
    random_val2 = random_str()

    link_server = client.create_container(name=test_name + '-server',
                                          imageUuid=TEST_IMAGE_UUID,
                                          networkIds=[managed_network.id],
                                          hostname=test_name + '-server',
                                          environment={
                                              'VALUE': random_val
                                          },
                                          requestedHostId=hosts[2].id)
    link_server2 = client.create_container(name=test_name + '-server2',
                                           imageUuid=TEST_IMAGE_UUID,
                                           networkIds=[managed_network.id],
                                           hostname=test_name + '-server2',
                                           environment={
                                               'VALUE': random_val2
                                           },
                                           requestedHostId=hosts[1].id)
    link_client = client.create_container(name=test_name + '-client',
                                          imageUuid=TEST_IMAGE_UUID,
                                          networkIds=[managed_network.id],
                                          ports=['3000:3000'],
                                          hostname=test_name + '-client1',
                                          instanceLinks={
                                              'client': link_server.id
                                          },
                                          requestedHostId=hosts[0].id)

    link_client = client.wait_success(link_client)
    link_server = client.wait_success(link_server)

    ping_link(link_client, 'client', var='VALUE', value=random_val)

    link_server2 = client.wait_success(link_server2)

    link = link_client.instanceLinks()[0]
    link = client.update(link, targetInstanceId=link_server2.id)
    client.wait_success(link)

    ping_link(link_client, 'client', var='VALUE', value=random_val2)

    delete_all(client, [link_client, link_server, link_server2])
<commit_msg>Add test to verify IP is getting injected<commit_after>from common_fixtures import *  # NOQA
import requests
import websocket as ws

def test_sibling_pinging(client, one_per_host):
    instances = one_per_host
    hosts = {}
    hostnames = set()

    for i in instances:
        port = i.ports()[0]

        host = port.publicIpAddress().address
        port = port.publicPort
        base_url = 'http://{}:{}'.format(host, port)
        pong = requests.get(base_url + '/ping').text
        hostname = requests.get(base_url + '/hostname').text

        assert pong == 'pong'
        assert hostname not in hostnames

        hostnames.add(hostname)
        hosts[hostname] = base_url

    count = 0
    for hostname, base_url in hosts.items():
        url = base_url + '/get'
        for other_hostname, other_url in hosts.items():
            if other_hostname == hostname:
                continue

            test_hostname = requests.get(url, params={
                'url': other_url + '/hostname'
            }).text

            count += 1
            assert other_hostname == test_hostname

    assert count == len(instances) * (len(instances) - 1)

    delete_all(client, instances)

def test_dynamic_port(client, test_name, managed_network):
    c = client.create_container(name=test_name,
                                networkIds=[managed_network.id],
                                imageUuid=TEST_IMAGE_UUID)
    c = client.wait_success(c)

    ports = c.ports()
    assert len(ports) == 1

    port = ports[0]

    assert port.publicPort is None

    port = client.wait_success(client.update(port, publicPort=3001))

    assert port.publicPort == 3001
    ping_port(port)

    port = client.wait_success(client.update(port, publicPort=3002))

    assert port.publicPort == 3002
    ping_port(port)

    delete_all(client, [c])

def test_linking(client, test_name, managed_network):
    hosts = client.list_host(kind='docker', removed_null=True)
    assert len(hosts) > 2

    random_val = random_str()
    random_val2 = random_str()

    link_server = client.create_container(name=test_name + '-server',
                                          imageUuid=TEST_IMAGE_UUID,
                                          networkIds=[managed_network.id],
                                          hostname=test_name + '-server',
                                          environment={
                                              'VALUE': random_val
                                          },
                                          requestedHostId=hosts[2].id)
    link_server2 = client.create_container(name=test_name + '-server2',
                                           imageUuid=TEST_IMAGE_UUID,
                                           networkIds=[managed_network.id],
                                           hostname=test_name + '-server2',
                                           environment={
                                               'VALUE': random_val2
                                           },
                                           requestedHostId=hosts[1].id)
    link_client = client.create_container(name=test_name + '-client',
                                          imageUuid=TEST_IMAGE_UUID,
                                          networkIds=[managed_network.id],
                                          ports=['3000:3000'],
                                          hostname=test_name + '-client1',
                                          instanceLinks={
                                              'client': link_server.id
                                          },
                                          requestedHostId=hosts[0].id)

    link_client = client.wait_success(link_client)
    link_server = client.wait_success(link_server)

    ping_link(link_client, 'client', var='VALUE', value=random_val)

    link_server2 = client.wait_success(link_server2)

    link = link_client.instanceLinks()[0]
    link = client.update(link, targetInstanceId=link_server2.id)
    client.wait_success(link)

    ping_link(link_client, 'client', var='VALUE', value=random_val2)

    delete_all(client, [link_client, link_server, link_server2])

def create_container(admin_client, net):
    cmd = '/bin/bash -c "sleep 5; ip addr show eth0"'
    c = admin_client.create_container(imageUuid=TEST_IMAGE_UUID,
                                      networkIds=[net.id],
                                      command=cmd)
    c = admin_client.wait_success(c)
    logs = c.logs()

    return logs, c, c.primaryIpAddress

def cleanup(container):
        try:
            container.stop(remove=True, timeout=0)
        except:
            # We've tried our best!
            pass

def test_ip_inject(admin_client, managed_network, request):
    logs, c, ip = create_container(admin_client, managed_network)
    request.addfinalizer(lambda: cleanup(c))
    conn = ws.create_connection(logs.url + '?token='+logs.token)

    count = 0
    found_ip = False
    while count <= 100:
        count += 1
        try:
            result = conn.recv()
            if ip in result:
                found_ip = True
                break
        except ws.WebSocketConnectionClosedException:
            break

    assert found_ip
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copy this file to 'config.py' and make necessary changes for your local setup.
import os

base_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "<RELATIVE PATH TO DATA FOLDER>"))

# Base URI for resources
QBR_BASE = "http://data.socialhistory.org/resource/"

# Base URI for vocabulary
QBRV_BASE = "http://data.socialhistory.org/vocab/"

# SPARQL Endpoint Configuration
ENDPOINT_URL = '<URL OF SPARQL ENDPOINT>'
UPDATE_URL = '<URL OF SPARQL UPDATE ENDPOINT>'

# Virtuoso specific stuff
CRUD_URL = '<CRUD URL>'
from requests.auth import HTTPDigestAuth
CRUD_AUTH = HTTPDigestAuth('<USER>', '<PASS>')

# Is the API running behind a proxy?
BEHIND_PROXY = False

# Should the API log at DEBUG level?
DEBUG = True

# Stardog specific stuff
# (these are the default settings for HTTP Basic authenticating)
REASONING_TYPE = 'NONE'
AUTH = ('admin','admin')

# Respond to GitHub webhooks
FOLLOW_GITHUB = True
FOLLOW_REPO = 'https://github.com/CLARIAH/wp4-csdh-api'
FOLLOW_REF = 'refs/heads/master'
<commit_msg>Added Dataverse configuration parameters<commit_after># -*- coding: utf-8 -*-
# Copy this file to 'config.py' and make necessary changes for your local setup.
import os

base_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "<RELATIVE PATH TO DATA FOLDER>"))

# Base URI for resources
QBR_BASE = "http://data.socialhistory.org/resource/"

# Base URI for vocabulary
QBRV_BASE = "http://data.socialhistory.org/vocab/"

# SPARQL Endpoint Configuration
ENDPOINT_URL = '<URL OF SPARQL ENDPOINT>'
UPDATE_URL = '<URL OF SPARQL UPDATE ENDPOINT>'

# Virtuoso specific stuff
CRUD_URL = '<CRUD URL>'
from requests.auth import HTTPDigestAuth
CRUD_AUTH = HTTPDigestAuth('<USER>', '<PASS>')

# Dataverse configuration
DATAVERSE_HOST = '<DATAVERSE_HOST>' # e.g. dataverse.harvard.edu (without the http:// bit)
DATAVERSE_TOKEN = '<API TOKEN>' # The API token key for connecting to dataverse

# Is the API running behind a proxy?
BEHIND_PROXY = False

# Should the API log at DEBUG level?
DEBUG = True

# Stardog specific stuff
# (these are the default settings for HTTP Basic authenticating)
REASONING_TYPE = 'NONE'
AUTH = ('admin','admin')

# Respond to GitHub webhooks
FOLLOW_GITHUB = True
FOLLOW_REPO = 'https://github.com/CLARIAH/wp4-csdh-api'
FOLLOW_REF = 'refs/heads/master'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from __future__ import absolute_import, division
import six
import json
import collections
from armet import transcoders
from .base import Encoder

class _TypeCoerableJSONEncoder(json.JSONEncoder):

    def default(self, obj):
        if isinstance(obj, collections.Iterable):
            # This is an iterable but not recognizable as such
            # by the JSON encoder.
            return list(obj)

        # Raise up our hands; we cannot encode this.
        return super(_TypeCoerableJSONEncoder, self).default(obj)

class Encoder(transcoders.Json, Encoder):

    def __init__(self, *args, **kwargs):
        # Let the base class figure out things.
        super(Encoder, self).__init__(*args, **kwargs)

        #! The configuration options that are passed to the
        #! JSON encoder.
        self.options = {
            'ensure_ascii': True,
            'separators': (',', ':',),
            'cls': _TypeCoerableJSONEncoder
        }

        pprint = self.params.get('pretty_print', '')
        if pprint == '1' or pprint.lower() == 'true':
            # We are pretty printing; turn on indents and
            # add spaces around separators
            self.options['indent'] = 2
            self.options['separators'] = (', ', ': ')

    def encode(self, obj=None):
        # If we have nothing; encode as an empty object.
        if obj is None:
            return '{}'

        # Ensure it is atleast wrapped in an array.
        if isinstance(obj, six.string_types):
            obj = [obj]

        elif not isinstance(obj, collections.Iterable):
            obj = [obj]

        # Encode and return the resultant text.
        return super(Encoder, self).encode(json.dumps(obj, **self.options))
<commit_msg>Fixed bug where the json encoder would sometimes return a string instead of a response object.<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from __future__ import absolute_import, division
import six
import json
import collections
from armet import transcoders
from .base import Encoder

class _TypeCoerableJSONEncoder(json.JSONEncoder):

    def default(self, obj):
        if isinstance(obj, collections.Iterable):
            # This is an iterable but not recognizable as such
            # by the JSON encoder.
            return list(obj)

        # Raise up our hands; we cannot encode this.
        return super(_TypeCoerableJSONEncoder, self).default(obj)

class Encoder(transcoders.Json, Encoder):

    def __init__(self, *args, **kwargs):
        # Let the base class figure out things.
        super(Encoder, self).__init__(*args, **kwargs)

        #! The configuration options that are passed to the
        #! JSON encoder.
        self.options = {
            'ensure_ascii': True,
            'separators': (',', ':',),
            'cls': _TypeCoerableJSONEncoder
        }

        pprint = self.params.get('pretty_print', '')
        if pprint == '1' or pprint.lower() == 'true':
            # We are pretty printing; turn on indents and
            # add spaces around separators
            self.options['indent'] = 2
            self.options['separators'] = (', ', ': ')

    def encode(self, obj=None):
        # If we have nothing; encode as an empty object.
        if obj is None:
            obj = {}

        # Ensure it is atleast wrapped in an array.
        if isinstance(obj, six.string_types):
            obj = [obj]

        elif not isinstance(obj, collections.Iterable):
            obj = [obj]

        # Encode and return the resultant text.
        return super(Encoder, self).encode(json.dumps(obj, **self.options))
<|endoftext|>


<commit_before>import datetime, re

from google_credentials import GoogleCredentials
from gmail import Gmail
from contacts import Contacts
from MessagesDB import MessagesDB

# q= rfc822msgid:kuku7@messages2gmail.com

if __name__ == '__main__':
	google_credentials = GoogleCredentials()
	google_credentials.authenticate()

	gmail = Gmail(google_credentials.credentials)

	contacts = Contacts(google_credentials.credentials)
	contacts.load_contacts()

	MessagesDB = MessagesDB('~/backup_messages_2017/')

	# handle_id to Name cache
	handle_to_name = {}
	phone_number_reg = re.compile('^[a-z0-9\.\+ \(\)\+]+$')
	for handle in MessagesDB.get_handles():
		id = handle['id']
		if '@' in id:
			names = contacts.get_by_email(id)
		elif phone_number_reg.match(id):
			names = contacts.get_by_phone_number(id)
		if names:
			handle_to_name[handle['rowid']] = names
		else:
			handle_to_name[handle['rowid']] = (id, id)

	labels = gmail.get_labels("me")
	if 'Text' not in labels.keys():
		gmail.create_label("me", Gmail.make_label('Text'))

	threads = {}
	for message in MessagesDB.get_messages(19285):

		print message

		msg_id = '<%s_%s>' % (message['guid'], google_credentials.email)
		date = datetime.datetime.fromtimestamp(978307200 + message['date'])

		names = ''
		names_and_addressess = ''
		thread_key = '%s' % date.strftime("%d%m%Y")

		chat_handles = list(message['chat_handles'])
		chat_handles.sort()
		no_of_handles = len(chat_handles)
		for i in range(0, no_of_handles):
			handle_id = chat_handles[i]
			thread_key += '_%s' % handle_id
			name, name_and_address = handle_to_name[handle_id]
			print '%s %s %s' % (handle_id, name, name_and_address)
			if i == no_of_handles-1: # last element
				names += name
				names_and_addressess += name_and_address
			elif i == no_of_handles-2: # one before last handle
				names += name + ' and '
				names_and_addressess += name_and_address+'; '
			else:
				names += name + ', '
				names_and_addressess += name_and_address+'; '

		print thread_key

		if message['is_from_me']:
			sender = google_credentials.email
			to = names_and_addressess
		else:
			to = google_credentials.email
			name, name_and_address = handle_to_name[message['handle_id']]
			sender = name_and_address

		subject = "Chat with %s" % (names)

		thread = threads.get(thread_key, {"thread_id":None, "in_reply_to":None})

		print sender.encode('utf-8')
		print to.encode('utf-8')
		print subject.encode('utf-8')
		if message['text']:
			print message['text'].encode('utf-8')
		if message['attachments']:
			print message['attachments']
		print

		msg = Gmail.create_message_with_attachments(msg_id, sender, to, subject, date, message['text'], message['attachments'],
								   in_reply_to=thread['in_reply_to'], references=thread['in_reply_to'])

		msg = gmail.insert_message(msg, labelIds=[labels['Text']], threadId=thread['thread_id'])

		threads[thread_key] = {"thread_id":msg['threadId'], "in_reply_to":msg['id']}
<commit_msg>better handling of the self contact<commit_after>import datetime, re

from google_credentials import GoogleCredentials
from gmail import Gmail
from contacts import Contacts
from MessagesDB import MessagesDB

# q= rfc822msgid:kuku7@messages2gmail.com

if __name__ == '__main__':
	google_credentials = GoogleCredentials()
	google_credentials.authenticate()

	gmail = Gmail(google_credentials.credentials)

	contacts = Contacts(google_credentials.credentials)
	contacts.load_contacts()

	MessagesDB = MessagesDB('~/backup_messages_2017/')

	# handle_id to Name cache
	handle_to_name = {}
	phone_number_reg = re.compile('^[a-z0-9\.\+ \(\)\+]+$')
	for handle in MessagesDB.get_handles():
		id = handle['id']
		if '@' in id:
			names = contacts.get_by_email(id)
		elif phone_number_reg.match(id):
			names = contacts.get_by_phone_number(id)
		if names:
			handle_to_name[handle['rowid']] = names
		else:
			handle_to_name[handle['rowid']] = (id, id)

	if contacts.get_by_email(google_credentials.email):
		me = contacts.get_by_email(google_credentials.email)[1]
	else:
		me = google_credentials.email

	labels = gmail.get_labels("me")
	if 'Text' not in labels.keys():
		gmail.create_label("me", Gmail.make_label('Text'))

	threads = {}
	for message in MessagesDB.get_messages(19285):

		print message

		msg_id = '<%s_%s>' % (message['guid'], google_credentials.email)
		date = datetime.datetime.fromtimestamp(978307200 + message['date'])

		names = ''
		names_and_addressess = ''
		thread_key = '%s' % date.strftime("%d%m%Y")

		chat_handles = list(message['chat_handles'])
		chat_handles.sort()
		no_of_handles = len(chat_handles)
		for i in range(0, no_of_handles):
			handle_id = chat_handles[i]
			thread_key += '_%s' % handle_id
			name, name_and_address = handle_to_name[handle_id]
			print '%s %s %s' % (handle_id, name, name_and_address)
			if i == no_of_handles-1: # last element
				names += name
				names_and_addressess += name_and_address
			elif i == no_of_handles-2: # one before last handle
				names += name + ' and '
				names_and_addressess += name_and_address+'; '
			else:
				names += name + ', '
				names_and_addressess += name_and_address+'; '

		print thread_key

		if message['is_from_me']:
			sender = me
			to = names_and_addressess
		else:
			to = me
			name, name_and_address = handle_to_name[message['handle_id']]
			sender = name_and_address

		subject = "Chat with %s" % (names)

		thread = threads.get(thread_key, {"thread_id":None, "in_reply_to":None})

		print sender.encode('utf-8')
		print to.encode('utf-8')
		print subject.encode('utf-8')
		if message['text']:
			print message['text'].encode('utf-8')
		if message['attachments']:
			print message['attachments']
		print

		msg = Gmail.create_message_with_attachments(msg_id, sender, to, subject, date, message['text'], message['attachments'],
								   in_reply_to=thread['in_reply_to'], references=thread['in_reply_to'])

		msg = gmail.insert_message(msg, labelIds=[labels['Text']], threadId=thread['thread_id'])

		threads[thread_key] = {"thread_id":msg['threadId'], "in_reply_to":msg['id']}
<|endoftext|>


<commit_before>########################################################################################################################
# Robot Turtles
# Copyright Alexander L Gutierrez 2015
#
# Released under Apache open source license
########################################################################################################################

import subprocess

class NetworkManager:
    '''
    https://github.com/dpaleino/wicd/blob/master/cli/README.cli
    '''

    def __init__(self):
        pass

    def find_and_connect(self, saveID, networkName, networkPassword):
        available_networks = subprocess.check_output(['/usr/bin/wicd-cli','-y','-S', '-l']).split('\n')

        network_num = None

        for network in available_networks:
            # match network name to available networks
            available_network = network.split('\t')
            print str(available_network)

            network_name = available_network[3]

            network_num = available_network[0]

            if(network_num != '#'):
                print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num),'-d']))

            if(network_name == networkName):
                print 'Matched Network: '+str(network_name)
                network_num = available_network[0]
                network_bssid = available_network[1]
                break

        network_details = None

        if(network_num == None):
            raise ValueError('Network ' + str(networkName) + ' not found')

        network_details = subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num),'-d']).split('\n')

        properties = dict()

        for detail in network_details:
            if(detail != ''):
                propertyPair = detail.split(':')
                properties[str(propertyPair[0])] = str(propertyPair[1]).strip()

        encryptionMethod = None
        if(properties.has_key('Encryption')):
            if(properties['Encryption'] == 'On'):
                encryptionMethod = properties['Encryption Method']

        if(encryptionMethod != None):
            print 'Encryption: ' + str(encryptionMethod)

        self.connect(network_num, encryptionMethod, networkPassword)

        # Once Found, Configure that specific network
        pass

    def getProperties(self,network_type):
        return {
            'WPA2':'wpa',
            'WEP':'wep-hex',
        }[network_type]

    def connect(self, network_num, encryption_type, network_password):

        if(encryption_type != None):
            encType = self.getEncType(encryption_type)

            print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num), '-p encType', '-s '+str(encType)]))
            print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num), '-p key', '-s '+str(network_password)]))

        print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num), '--connect']))

if(__name__ == '__main__'):
    NetworkManager().find_and_connect('default','bananas','mysteryspot2')<commit_msg>Slowly working encryption<commit_after>########################################################################################################################
# Robot Turtles
# Copyright Alexander L Gutierrez 2015
#
# Released under Apache open source license
########################################################################################################################

import subprocess

class NetworkManager:
    '''
    https://github.com/dpaleino/wicd/blob/master/cli/README.cli
    '''

    def __init__(self):
        pass

    def find_and_connect(self, saveID, networkName, networkPassword):
        available_networks = subprocess.check_output(['/usr/bin/wicd-cli','-y','-S', '-l']).split('\n')

        network_num = None

        for network in available_networks:
            # match network name to available networks
            available_network = network.split('\t')
            print str(available_network)

            network_name = available_network[3]

            network_num = available_network[0]

            if(network_num != '#'):
                print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num),'-d']))

            if(network_name == networkName):
                print 'Matched Network: '+str(network_name)
                network_num = available_network[0]
                network_bssid = available_network[1]
                break

        network_details = None

        if(network_num == None):
            raise ValueError('Network ' + str(networkName) + ' not found')

        network_details = subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num),'-d']).split('\n')

        properties = dict()

        for detail in network_details:
            if(detail != ''):
                propertyPair = detail.split(':')
                properties[str(propertyPair[0])] = str(propertyPair[1]).strip()

        encryptionMethod = None
        if(properties.has_key('Encryption')):
            if(properties['Encryption'] == 'On'):
                encryptionMethod = properties['Encryption Method']

        if(encryptionMethod != None):
            print 'Encryption: ' + str(encryptionMethod)

        self.connect(network_num, encryptionMethod, networkPassword)

        # Once Found, Configure that specific network
        pass

    def getEncType(self,network_type):
        return {
            'WPA2':'wpa',
            'WEP':'wep-hex',
        }[network_type]

    def connect(self, network_num, encryption_type, network_password):

        if(encryption_type != None):
            encType = self.getEncType(encryption_type)

            print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num), '-p encType', '-s '+str(encType)]))
            print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num), '-p key', '-s '+str(network_password)]))

        print(subprocess.check_output(['/usr/bin/wicd-cli','-y','-n '+ str(network_num), '--connect']))

if(__name__ == '__main__'):
    NetworkManager().find_and_connect('default','bananas','mysteryspot2')<|endoftext|>


<commit_before># Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import argparse
import contextlib
import hashlib
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile

def get(url, path, verbose=False):
    sha_url = url + ".sha256"
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_path = temp_file.name
    with tempfile.NamedTemporaryFile(suffix=".sha256", delete=False) as sha_file:
        sha_path = sha_file.name

    try:
        download(sha_path, sha_url, verbose)
        download(temp_path, url, verbose)
        verify(temp_path, sha_path, verbose)
        print("moving {} to {}".format(temp_path, path))
        shutil.move(temp_path, path)
    finally:
        delete_if_present(sha_path)
        delete_if_present(temp_path)

def delete_if_present(path):
    if os.path.isfile(path):
        print("removing " + path)
        os.unlink(path)

def download(path, url, verbose):
    print("downloading {} to {}".format(url, path))
    # see http://serverfault.com/questions/301128/how-to-download
    if sys.platform == 'win32':
        run(["PowerShell.exe", "/nologo", "-Command",
             "(New-Object System.Net.WebClient)"
             ".DownloadFile('{}', '{}')".format(url, path)],
            verbose=verbose)
    else:
        run(["curl", "-o", path, url], verbose=verbose)

def verify(path, sha_path, verbose):
    print("verifying " + path)
    with open(path, "rb") as f:
        found = hashlib.sha256(f.read()).hexdigest()
    with open(sha_path, "r") as f:
        expected, _ = f.readline().split()
    if found != expected:
        err = ("invalid checksum:\n"
               "    found:    {}\n"
               "    expected: {}".format(found, expected))
        if verbose:
            raise RuntimeError(err)
        sys.exit(err)

def unpack(tarball, dst, verbose=False, match=None):
    print("extracting " + tarball)
    fname = os.path.basename(tarball).replace(".tar.gz", "")
    with contextlib.closing(tarfile.open(tarball)) as tar:
        for p in tar.getnames():
            if "/" not in p:
                continue
            name = p.replace(fname + "/", "", 1)
            if match is not None and not name.startswith(match):
                continue
            name = name[len(match) + 1:]

            fp = os.path.join(dst, name)
            if verbose:
                print("  extracting " + p)
            tar.extract(p, dst)
            tp = os.path.join(dst, p)
            if os.path.isdir(tp) and os.path.exists(fp):
                continue
            shutil.move(tp, fp)
    shutil.rmtree(os.path.join(dst, fname))

def run(args, verbose=False):
    if verbose:
        print("running: " + ' '.join(args))
    sys.stdout.flush()
    # Use Popen here instead of call() as it apparently allows powershell on
    # Windows to not lock up waiting for input presumably.
    ret = subprocess.Popen(args)
    code = ret.wait()
    if code != 0:
        err = "failed to run: " + ' '.join(args)
        if verbose:
            raise RuntimeError(err)
        sys.exit(err)

def stage0_data(rust_root):
    nightlies = os.path.join(rust_root, "src/stage0.txt")
    data = {}
    with open(nightlies, 'r') as nightlies:
        for line in nightlies:
            line = line.rstrip()  # Strip newline character, '\n'
            if line.startswith("#") or line == '':
                continue
            a, b = line.split(": ", 1)
            data[a] = b
    return data

class RustBuild:
    def download_stage0(self):
        cache_dst = os.path.join(self.build_dir, "cache")
        rustc_cache = os.path.join(cache_dst, self.stage0_rustc_date())
        cargo_cache = os.path.join(cache_dst, self.stage0_cargo_date())
        if not os.path.exists(rustc_cache):
            os.makedirs(rustc_cache)
        if not os.path.exists(cargo_cache):
            os.makedirs(cargo_cache)

        if self.rustc().startswith(self.bin_root()) and \
           (not os.path.exists(self.rustc()) or self.rustc_out_of_date()):
            if os.path.exists(self.bin_root()):
                shutil.rmtree(self.bin_root())
            channel = self.stage0_rustc_channel()
            filename = "rust-std-{}-{}.tar.gz".format(channel, self.build)
            url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date()
            tarball = os.path.join(rustc_cache, filename)
            if not os.path.exists(tarball):
                get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
            unpack(tarball, self.bin_root(),
                   match="rust-std-" + self.build,
                   verbose=self.verbose)

            filename = "rustc-{}-{}.tar.gz".format(channel, self.build)
            url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date()
            tarball = os.path.join(rustc_cache, filename)
            if not os.path.exists(tarball):
                get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
            unpack(tarball, self.bin_root(), match="rustc", verbose=self.verbose)
            with open(self.rustc_stamp(), 'w') as f:
                f.write(self.stage0_rustc_date())

        if self.cargo().startswith(self.bin_root()) and \
           (not os.path.exists(self.cargo()) or self.cargo_out_of_date()):
            channel = self.stage0_cargo_channel()
            filename = "cargo-{}-{}.tar.gz".format(channel, self.build)
            url = "https://static.rust-lang.org/cargo-dist/" + self.stage0_cargo_date()
            tarball = os.path.join(cargo_cache, filename)
            if not os.path.exists(tarball):
                get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
            unpack(tarball, self.bin_root(), match="cargo", verbose=self.verbose)
            with open(self.cargo_stamp(), 'w') as f:
                f.write(self.stage0_cargo_date())

    def stage0_cargo_date(self):
        return self._cargo_date

    def stage0_cargo_channel(self):
        return self._cargo_channel

    def stage0_rustc_date(self):
        return self._rustc_date

    def stage0_rustc_channel(self):
        return self._rustc_channel

    def rustc_stamp(self):
        return os.path.join(self.bin_root(), '.rustc-stamp')

    def cargo_stamp(self):
        return os.path.join(self.bin_root(), '.cargo-stamp')

    def rustc_out_of_date(self):
        if not os.path.exists(self.rustc_stamp()) or self.clean:
            return True
        with open(self.rustc_stamp(), 'r') as f:
            return self.stage0_rustc_date() != f.read()

    def cargo_out_of_date(self):
        if not os.path.exists(self.cargo_stamp()) or self.clean:
            return True
        with open(self.cargo_stamp(), 'r') as f:
            return self.stage0_cargo_date() != f.read()

    def bin_root(self):
        return os.path.join(self.build_dir, self.build, "stage0")

    def get_toml(self, key):
        for line in self.config_toml.splitlines():
            if line.startswith(key + ' ='):
                return self.get_string(line)
        return None

    def get_mk(self, key):
        for line in iter(self.config_mk.splitlines()):
            if line.startswith(key):
                return line[line.find(':=') + 2:].strip()
        return None

    def cargo(self):
        config = self.get_toml('cargo')
        if config:
            return config
        return os.path.join(self.bin_root(), "bin/cargo" + self.exe_suffix())

    def rustc(self):
        config = self.get_toml('rustc')
        if config:
            return config
        config = self.get_mk('CFG_LOCAL_RUST')
        if config:
            return config + '/bin/rustc' + self.exe_suffix()
        return os.path.join(self.bin_root(), "bin/rustc" + self.exe_suffix())

    def get_string(self, line):
        start = line.find('"')
        end = start + 1 + line[start+1:].find('"')
        return line[start+1:end]

    def exe_suffix(self):
        if sys.platform == 'win32':
            return '.exe'
        else:
            return ''

    def build_bootstrap(self):
        build_dir = os.path.join(self.build_dir, "bootstrap")
        if self.clean and os.path.exists(build_dir):
            shutil.rmtree(build_dir)
        env = os.environ.copy()
        env["CARGO_TARGET_DIR"] = build_dir
        env["RUSTC"] = self.rustc()
        env["LD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib")
        env["DYLD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib")
        env["PATH"] = os.path.join(self.bin_root(), "bin") + \
                      os.pathsep + env["PATH"]
        self.run([self.cargo(), "build", "--manifest-path",
                  os.path.join(self.rust_root, "src/bootstrap/Cargo.toml")],
                 env)

    def run(self, args, env):
        proc = subprocess.Popen(args, env=env)
        ret = proc.wait()
        if ret != 0:
            sys.exit(ret)

    def build_triple(self):
        config = self.get_toml('build')
        if config:
            return config
        config = self.get_mk('CFG_BUILD')
        if config:
            return config
        try:
            ostype = subprocess.check_output(['uname', '-s']).strip()
            cputype = subprocess.check_output(['uname', '-m']).strip()
        except subprocess.CalledProcessError:
            if sys.platform == 'win32':
                return 'x86_64-pc-windows-msvc'
            err = "uname not found"
            if self.verbose:
                raise Exception(err)
            sys.exit(err)

        # Darwin's `uname -s` lies and always returns i386. We have to use
        # sysctl instead.
        if ostype == 'Darwin' and cputype == 'i686':
            sysctl = subprocess.check_output(['sysctl', 'hw.optional.x86_64'])
            if ': 1' in sysctl:
                cputype = 'x86_64'

        # The goal here is to come up with the same triple as LLVM would,
        # at least for the subset of platforms we're willing to target.
        if ostype == 'Linux':
            ostype = 'unknown-linux-gnu'
        elif ostype == 'FreeBSD':
            ostype = 'unknown-freebsd'
        elif ostype == 'DragonFly':
            ostype = 'unknown-dragonfly'
        elif ostype == 'Bitrig':
            ostype = 'unknown-bitrig'
        elif ostype == 'OpenBSD':
            ostype = 'unknown-openbsd'
        elif ostype == 'NetBSD':
            ostype = 'unknown-netbsd'
        elif ostype == 'Darwin':
            ostype = 'apple-darwin'
        elif ostype.startswith('MINGW'):
            # msys' `uname` does not print gcc configuration, but prints msys
            # configuration. so we cannot believe `uname -m`:
            # msys1 is always i686 and msys2 is always x86_64.
            # instead, msys defines $MSYSTEM which is MINGW32 on i686 and
            # MINGW64 on x86_64.
            ostype = 'pc-windows-gnu'
            cputype = 'i686'
            if os.environ.get('MSYSTEM') == 'MINGW64':
                cputype = 'x86_64'
        elif ostype.startswith('MSYS'):
            ostype = 'pc-windows-gnu'
        elif ostype.startswith('CYGWIN_NT'):
            cputype = 'i686'
            if ostype.endswith('WOW64'):
                cputype = 'x86_64'
            ostype = 'pc-windows-gnu'
        else:
            err = "unknown OS type: " + ostype
            if self.verbose:
                raise ValueError(err)
            sys.exit(err)

        if cputype in {'i386', 'i486', 'i686', 'i786', 'x86'}:
            cputype = 'i686'
        elif cputype in {'xscale', 'arm'}:
            cputype = 'arm'
        elif cputype == 'armv7l':
            cputype = 'arm'
            ostype += 'eabihf'
        elif cputype == 'aarch64':
            cputype = 'aarch64'
        elif cputype in {'powerpc', 'ppc', 'ppc64'}:
            cputype = 'powerpc'
        elif cputype in {'amd64', 'x86_64', 'x86-64', 'x64'}:
            cputype = 'x86_64'
        else:
            err = "unknown cpu type: " + cputype
            if self.verbose:
                raise ValueError(err)
            sys.exit(err)

        return "{}-{}".format(cputype, ostype)

def main():
    parser = argparse.ArgumentParser(description='Build rust')
    parser.add_argument('--config')
    parser.add_argument('--clean', action='store_true')
    parser.add_argument('-v', '--verbose', action='store_true')

    args = [a for a in sys.argv if a != '-h']
    args, _ = parser.parse_known_args(args)

    # Configure initial bootstrap
    rb = RustBuild()
    rb.config_toml = ''
    rb.config_mk = ''
    rb.rust_root = os.path.abspath(os.path.join(__file__, '../../..'))
    rb.build_dir = os.path.join(os.getcwd(), "build")
    rb.verbose = args.verbose
    rb.clean = args.clean

    try:
        with open(args.config or 'config.toml') as config:
            rb.config_toml = config.read()
    except:
        pass
    try:
        rb.config_mk = open('config.mk').read()
    except:
        pass

    data = stage0_data(rb.rust_root)
    rb._rustc_channel, rb._rustc_date = data['rustc'].split('-', 1)
    rb._cargo_channel, rb._cargo_date = data['cargo'].split('-', 1)

    # Fetch/build the bootstrap
    rb.build = rb.build_triple()
    rb.download_stage0()
    sys.stdout.flush()
    rb.build_bootstrap()
    sys.stdout.flush()

    # Run the bootstrap
    args = [os.path.join(rb.build_dir, "bootstrap/debug/bootstrap")]
    args.append('--src')
    args.append(rb.rust_root)
    args.append('--build')
    args.append(rb.build)
    args.extend(sys.argv[1:])
    env = os.environ.copy()
    env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
    rb.run(args, env)

if __name__ == '__main__':
    main()
<commit_msg>Auto merge of #34597 - CensoredUsername:bootstrap-fix, r=alexcrichton<commit_after># Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import argparse
import contextlib
import hashlib
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile

def get(url, path, verbose=False):
    sha_url = url + ".sha256"
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_path = temp_file.name
    with tempfile.NamedTemporaryFile(suffix=".sha256", delete=False) as sha_file:
        sha_path = sha_file.name

    try:
        download(sha_path, sha_url, verbose)
        download(temp_path, url, verbose)
        verify(temp_path, sha_path, verbose)
        print("moving {} to {}".format(temp_path, path))
        shutil.move(temp_path, path)
    finally:
        delete_if_present(sha_path)
        delete_if_present(temp_path)

def delete_if_present(path):
    if os.path.isfile(path):
        print("removing " + path)
        os.unlink(path)

def download(path, url, verbose):
    print("downloading {} to {}".format(url, path))
    # see http://serverfault.com/questions/301128/how-to-download
    if sys.platform == 'win32':
        run(["PowerShell.exe", "/nologo", "-Command",
             "(New-Object System.Net.WebClient)"
             ".DownloadFile('{}', '{}')".format(url, path)],
            verbose=verbose)
    else:
        run(["curl", "-o", path, url], verbose=verbose)

def verify(path, sha_path, verbose):
    print("verifying " + path)
    with open(path, "rb") as f:
        found = hashlib.sha256(f.read()).hexdigest()
    with open(sha_path, "r") as f:
        expected, _ = f.readline().split()
    if found != expected:
        err = ("invalid checksum:\n"
               "    found:    {}\n"
               "    expected: {}".format(found, expected))
        if verbose:
            raise RuntimeError(err)
        sys.exit(err)

def unpack(tarball, dst, verbose=False, match=None):
    print("extracting " + tarball)
    fname = os.path.basename(tarball).replace(".tar.gz", "")
    with contextlib.closing(tarfile.open(tarball)) as tar:
        for p in tar.getnames():
            if "/" not in p:
                continue
            name = p.replace(fname + "/", "", 1)
            if match is not None and not name.startswith(match):
                continue
            name = name[len(match) + 1:]

            fp = os.path.join(dst, name)
            if verbose:
                print("  extracting " + p)
            tar.extract(p, dst)
            tp = os.path.join(dst, p)
            if os.path.isdir(tp) and os.path.exists(fp):
                continue
            shutil.move(tp, fp)
    shutil.rmtree(os.path.join(dst, fname))

def run(args, verbose=False):
    if verbose:
        print("running: " + ' '.join(args))
    sys.stdout.flush()
    # Use Popen here instead of call() as it apparently allows powershell on
    # Windows to not lock up waiting for input presumably.
    ret = subprocess.Popen(args)
    code = ret.wait()
    if code != 0:
        err = "failed to run: " + ' '.join(args)
        if verbose:
            raise RuntimeError(err)
        sys.exit(err)

def stage0_data(rust_root):
    nightlies = os.path.join(rust_root, "src/stage0.txt")
    data = {}
    with open(nightlies, 'r') as nightlies:
        for line in nightlies:
            line = line.rstrip()  # Strip newline character, '\n'
            if line.startswith("#") or line == '':
                continue
            a, b = line.split(": ", 1)
            data[a] = b
    return data

class RustBuild:
    def download_stage0(self):
        cache_dst = os.path.join(self.build_dir, "cache")
        rustc_cache = os.path.join(cache_dst, self.stage0_rustc_date())
        cargo_cache = os.path.join(cache_dst, self.stage0_cargo_date())
        if not os.path.exists(rustc_cache):
            os.makedirs(rustc_cache)
        if not os.path.exists(cargo_cache):
            os.makedirs(cargo_cache)

        if self.rustc().startswith(self.bin_root()) and \
           (not os.path.exists(self.rustc()) or self.rustc_out_of_date()):
            if os.path.exists(self.bin_root()):
                shutil.rmtree(self.bin_root())
            channel = self.stage0_rustc_channel()
            filename = "rust-std-{}-{}.tar.gz".format(channel, self.build)
            url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date()
            tarball = os.path.join(rustc_cache, filename)
            if not os.path.exists(tarball):
                get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
            unpack(tarball, self.bin_root(),
                   match="rust-std-" + self.build,
                   verbose=self.verbose)

            filename = "rustc-{}-{}.tar.gz".format(channel, self.build)
            url = "https://static.rust-lang.org/dist/" + self.stage0_rustc_date()
            tarball = os.path.join(rustc_cache, filename)
            if not os.path.exists(tarball):
                get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
            unpack(tarball, self.bin_root(), match="rustc", verbose=self.verbose)
            with open(self.rustc_stamp(), 'w') as f:
                f.write(self.stage0_rustc_date())

        if self.cargo().startswith(self.bin_root()) and \
           (not os.path.exists(self.cargo()) or self.cargo_out_of_date()):
            channel = self.stage0_cargo_channel()
            filename = "cargo-{}-{}.tar.gz".format(channel, self.build)
            url = "https://static.rust-lang.org/cargo-dist/" + self.stage0_cargo_date()
            tarball = os.path.join(cargo_cache, filename)
            if not os.path.exists(tarball):
                get("{}/{}".format(url, filename), tarball, verbose=self.verbose)
            unpack(tarball, self.bin_root(), match="cargo", verbose=self.verbose)
            with open(self.cargo_stamp(), 'w') as f:
                f.write(self.stage0_cargo_date())

    def stage0_cargo_date(self):
        return self._cargo_date

    def stage0_cargo_channel(self):
        return self._cargo_channel

    def stage0_rustc_date(self):
        return self._rustc_date

    def stage0_rustc_channel(self):
        return self._rustc_channel

    def rustc_stamp(self):
        return os.path.join(self.bin_root(), '.rustc-stamp')

    def cargo_stamp(self):
        return os.path.join(self.bin_root(), '.cargo-stamp')

    def rustc_out_of_date(self):
        if not os.path.exists(self.rustc_stamp()) or self.clean:
            return True
        with open(self.rustc_stamp(), 'r') as f:
            return self.stage0_rustc_date() != f.read()

    def cargo_out_of_date(self):
        if not os.path.exists(self.cargo_stamp()) or self.clean:
            return True
        with open(self.cargo_stamp(), 'r') as f:
            return self.stage0_cargo_date() != f.read()

    def bin_root(self):
        return os.path.join(self.build_dir, self.build, "stage0")

    def get_toml(self, key):
        for line in self.config_toml.splitlines():
            if line.startswith(key + ' ='):
                return self.get_string(line)
        return None

    def get_mk(self, key):
        for line in iter(self.config_mk.splitlines()):
            if line.startswith(key):
                return line[line.find(':=') + 2:].strip()
        return None

    def cargo(self):
        config = self.get_toml('cargo')
        if config:
            return config
        return os.path.join(self.bin_root(), "bin/cargo" + self.exe_suffix())

    def rustc(self):
        config = self.get_toml('rustc')
        if config:
            return config
        config = self.get_mk('CFG_LOCAL_RUST')
        if config:
            return config + '/bin/rustc' + self.exe_suffix()
        return os.path.join(self.bin_root(), "bin/rustc" + self.exe_suffix())

    def get_string(self, line):
        start = line.find('"')
        end = start + 1 + line[start+1:].find('"')
        return line[start+1:end]

    def exe_suffix(self):
        if sys.platform == 'win32':
            return '.exe'
        else:
            return ''

    def build_bootstrap(self):
        build_dir = os.path.join(self.build_dir, "bootstrap")
        if self.clean and os.path.exists(build_dir):
            shutil.rmtree(build_dir)
        env = os.environ.copy()
        env["CARGO_TARGET_DIR"] = build_dir
        env["RUSTC"] = self.rustc()
        env["LD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib")
        env["DYLD_LIBRARY_PATH"] = os.path.join(self.bin_root(), "lib")
        env["PATH"] = os.path.join(self.bin_root(), "bin") + \
                      os.pathsep + env["PATH"]
        self.run([self.cargo(), "build", "--manifest-path",
                  os.path.join(self.rust_root, "src/bootstrap/Cargo.toml")],
                 env)

    def run(self, args, env):
        proc = subprocess.Popen(args, env=env)
        ret = proc.wait()
        if ret != 0:
            sys.exit(ret)

    def build_triple(self):
        config = self.get_toml('build')
        if config:
            return config
        config = self.get_mk('CFG_BUILD')
        if config:
            return config
        try:
            ostype = subprocess.check_output(['uname', '-s']).strip()
            cputype = subprocess.check_output(['uname', '-m']).strip()
        except (subprocess.CalledProcessError, WindowsError):
            if sys.platform == 'win32':
                return 'x86_64-pc-windows-msvc'
            err = "uname not found"
            if self.verbose:
                raise Exception(err)
            sys.exit(err)

        # Darwin's `uname -s` lies and always returns i386. We have to use
        # sysctl instead.
        if ostype == 'Darwin' and cputype == 'i686':
            sysctl = subprocess.check_output(['sysctl', 'hw.optional.x86_64'])
            if ': 1' in sysctl:
                cputype = 'x86_64'

        # The goal here is to come up with the same triple as LLVM would,
        # at least for the subset of platforms we're willing to target.
        if ostype == 'Linux':
            ostype = 'unknown-linux-gnu'
        elif ostype == 'FreeBSD':
            ostype = 'unknown-freebsd'
        elif ostype == 'DragonFly':
            ostype = 'unknown-dragonfly'
        elif ostype == 'Bitrig':
            ostype = 'unknown-bitrig'
        elif ostype == 'OpenBSD':
            ostype = 'unknown-openbsd'
        elif ostype == 'NetBSD':
            ostype = 'unknown-netbsd'
        elif ostype == 'Darwin':
            ostype = 'apple-darwin'
        elif ostype.startswith('MINGW'):
            # msys' `uname` does not print gcc configuration, but prints msys
            # configuration. so we cannot believe `uname -m`:
            # msys1 is always i686 and msys2 is always x86_64.
            # instead, msys defines $MSYSTEM which is MINGW32 on i686 and
            # MINGW64 on x86_64.
            ostype = 'pc-windows-gnu'
            cputype = 'i686'
            if os.environ.get('MSYSTEM') == 'MINGW64':
                cputype = 'x86_64'
        elif ostype.startswith('MSYS'):
            ostype = 'pc-windows-gnu'
        elif ostype.startswith('CYGWIN_NT'):
            cputype = 'i686'
            if ostype.endswith('WOW64'):
                cputype = 'x86_64'
            ostype = 'pc-windows-gnu'
        else:
            err = "unknown OS type: " + ostype
            if self.verbose:
                raise ValueError(err)
            sys.exit(err)

        if cputype in {'i386', 'i486', 'i686', 'i786', 'x86'}:
            cputype = 'i686'
        elif cputype in {'xscale', 'arm'}:
            cputype = 'arm'
        elif cputype == 'armv7l':
            cputype = 'arm'
            ostype += 'eabihf'
        elif cputype == 'aarch64':
            cputype = 'aarch64'
        elif cputype in {'powerpc', 'ppc', 'ppc64'}:
            cputype = 'powerpc'
        elif cputype in {'amd64', 'x86_64', 'x86-64', 'x64'}:
            cputype = 'x86_64'
        else:
            err = "unknown cpu type: " + cputype
            if self.verbose:
                raise ValueError(err)
            sys.exit(err)

        return "{}-{}".format(cputype, ostype)

def main():
    parser = argparse.ArgumentParser(description='Build rust')
    parser.add_argument('--config')
    parser.add_argument('--clean', action='store_true')
    parser.add_argument('-v', '--verbose', action='store_true')

    args = [a for a in sys.argv if a != '-h']
    args, _ = parser.parse_known_args(args)

    # Configure initial bootstrap
    rb = RustBuild()
    rb.config_toml = ''
    rb.config_mk = ''
    rb.rust_root = os.path.abspath(os.path.join(__file__, '../../..'))
    rb.build_dir = os.path.join(os.getcwd(), "build")
    rb.verbose = args.verbose
    rb.clean = args.clean

    try:
        with open(args.config or 'config.toml') as config:
            rb.config_toml = config.read()
    except:
        pass
    try:
        rb.config_mk = open('config.mk').read()
    except:
        pass

    data = stage0_data(rb.rust_root)
    rb._rustc_channel, rb._rustc_date = data['rustc'].split('-', 1)
    rb._cargo_channel, rb._cargo_date = data['cargo'].split('-', 1)

    # Fetch/build the bootstrap
    rb.build = rb.build_triple()
    rb.download_stage0()
    sys.stdout.flush()
    rb.build_bootstrap()
    sys.stdout.flush()

    # Run the bootstrap
    args = [os.path.join(rb.build_dir, "bootstrap/debug/bootstrap")]
    args.append('--src')
    args.append(rb.rust_root)
    args.append('--build')
    args.append(rb.build)
    args.extend(sys.argv[1:])
    env = os.environ.copy()
    env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
    rb.run(args, env)

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


<commit_before># flake8: noqa
import copy
from collections import OrderedDict, deque

import numpy as np
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _

def describe_location(location, locations):
    if location.can_describe:
        final_location = locations.get(location.pk)
        if final_location is not None:
            location = final_location
        else:
            location.can_describe = False
    return location.serialize(detailed=False, describe_only=True, simple_geometry=True)

class Route:
    def __init__(self, router, origin, destination, distance, path_nodes, origin_addition, destination_addition):
        self.router = router
        self.origin = origin
        self.destination = destination
        self.distance = distance
        self.path_nodes = path_nodes
        self.origin_addition = origin_addition
        self.destination_addition = destination_addition

    def serialize(self, locations):
        nodes = [[node, None] for node in self.path_nodes]
        if self.origin_addition and any(self.origin_addition):
            nodes.insert(0, (self.origin_addition[0], None))
            nodes[1][1] = self.origin_addition[1]
        if self.destination_addition and any(self.destination_addition):
            nodes.append(self.destination_addition)

        items = deque()
        last_node = None
        last_item = None
        distance = 0
        for i, (node, edge) in enumerate(nodes):
            if edge is None:
                edge = self.router.edges[last_node, node] if last_node else None
            node_obj = self.router.nodes[node] if isinstance(node, (int, np.int32, np.int64)) else node
            item = RouteItem(self, node_obj, edge, last_item)
            if edge:
                distance += edge.distance
            items.append(item)
            last_item = item
            last_node = node

        # descriptions for waytypes
        next_item = None
        for item in reversed(items):
            if item.waytype:
                if item.waytype.join_edges and next_item and next_item.waytype == item.waytype:
                    continue
                if item.waytype.up_separate and item.edge.rise > 0:
                    item.description.append(item.waytype.description_up)
                else:
                    item.description.append(item.waytype.description)
            elif item.new_space:
                pass  # todo: custom space transition descriptions

        items[-1].description.append(_('You have reached your destination.'))

        return OrderedDict((
            ('origin', describe_location(self.origin, locations)),
            ('destination', describe_location(self.destination, locations)),
            ('distance', round(distance, 2)),
            ('items', tuple(item.serialize(locations=locations) for item in items)),
        ))

class RouteItem:
    def __init__(self, route, node, edge, last_item):
        self.route = route
        self.node = node
        self.edge = edge
        self.last_item = last_item
        self.descriptions = []

    @cached_property
    def waytype(self):
        if self.edge and self.edge.waytype:
            return self.route.router.waytypes[self.edge.waytype]

    @cached_property
    def space(self):
        return self.route.router.spaces[self.node.space]

    @cached_property
    def level(self):
        return self.route.router.levels[self.space.level_id]

    @cached_property
    def new_space(self):
        return not self.last_item or self.space.pk != self.last_item.space.pk

    @cached_property
    def new_level(self):
        return not self.last_item or self.level.pk != self.last_item.level.pk

    def serialize(self, locations):
        result = OrderedDict((
            ('id', self.node.pk),
            ('coordinates', (self.node.x, self.node.y, self.node.altitude)),
            ('waytype', (self.route.router.waytypes[self.edge.waytype].serialize(detailed=False)
                         if self.edge and self.edge.waytype else None)),
        ))
        if self.waytype:
            result['waytype'] = self.waytype.serialize(detailed=False)

        if self.new_space:
            result['space'] = describe_location(self.space, locations)

        if self.new_level:
            result['level'] = describe_location(self.level, locations)

        if self.description:
            result['description'] = self.description
        return result

class NoRoute:
    distance = np.inf
<commit_msg>description → descriptions<commit_after># flake8: noqa
import copy
from collections import OrderedDict, deque

import numpy as np
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _

def describe_location(location, locations):
    if location.can_describe:
        final_location = locations.get(location.pk)
        if final_location is not None:
            location = final_location
        else:
            location.can_describe = False
    return location.serialize(detailed=False, describe_only=True, simple_geometry=True)

class Route:
    def __init__(self, router, origin, destination, distance, path_nodes, origin_addition, destination_addition):
        self.router = router
        self.origin = origin
        self.destination = destination
        self.distance = distance
        self.path_nodes = path_nodes
        self.origin_addition = origin_addition
        self.destination_addition = destination_addition

    def serialize(self, locations):
        nodes = [[node, None] for node in self.path_nodes]
        if self.origin_addition and any(self.origin_addition):
            nodes.insert(0, (self.origin_addition[0], None))
            nodes[1][1] = self.origin_addition[1]
        if self.destination_addition and any(self.destination_addition):
            nodes.append(self.destination_addition)

        items = deque()
        last_node = None
        last_item = None
        distance = 0
        for i, (node, edge) in enumerate(nodes):
            if edge is None:
                edge = self.router.edges[last_node, node] if last_node else None
            node_obj = self.router.nodes[node] if isinstance(node, (int, np.int32, np.int64)) else node
            item = RouteItem(self, node_obj, edge, last_item)
            if edge:
                distance += edge.distance
            items.append(item)
            last_item = item
            last_node = node

        # descriptions for waytypes
        next_item = None
        for item in reversed(items):
            if item.waytype:
                if item.waytype.join_edges and next_item and next_item.waytype == item.waytype:
                    continue
                if item.waytype.up_separate and item.edge.rise > 0:
                    item.descriptions.append(item.waytype.description_up)
                else:
                    item.descriptions.append(item.waytype.description)
            elif item.new_space:
                pass  # todo: custom space transition descriptions

        items[-1].descriptions.append(_('You have reached your destination.'))

        return OrderedDict((
            ('origin', describe_location(self.origin, locations)),
            ('destination', describe_location(self.destination, locations)),
            ('distance', round(distance, 2)),
            ('items', tuple(item.serialize(locations=locations) for item in items)),
        ))

class RouteItem:
    def __init__(self, route, node, edge, last_item):
        self.route = route
        self.node = node
        self.edge = edge
        self.last_item = last_item
        self.descriptions = []

    @cached_property
    def waytype(self):
        if self.edge and self.edge.waytype:
            return self.route.router.waytypes[self.edge.waytype]

    @cached_property
    def space(self):
        return self.route.router.spaces[self.node.space]

    @cached_property
    def level(self):
        return self.route.router.levels[self.space.level_id]

    @cached_property
    def new_space(self):
        return not self.last_item or self.space.pk != self.last_item.space.pk

    @cached_property
    def new_level(self):
        return not self.last_item or self.level.pk != self.last_item.level.pk

    def serialize(self, locations):
        result = OrderedDict((
            ('id', self.node.pk),
            ('coordinates', (self.node.x, self.node.y, self.node.altitude)),
            ('waytype', (self.route.router.waytypes[self.edge.waytype].serialize(detailed=False)
                         if self.edge and self.edge.waytype else None)),
        ))
        if self.waytype:
            result['waytype'] = self.waytype.serialize(detailed=False)

        if self.new_space:
            result['space'] = describe_location(self.space, locations)

        if self.new_level:
            result['level'] = describe_location(self.level, locations)

        result['descriptions'] = self.descriptions
        return result

class NoRoute:
    distance = np.inf
<|endoftext|>