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

import os
import sys
import subprocess
import struct
import optparse
import binascii
from io import BytesIO

class GitWrapper:
    @classmethod
    def command(cls, txt):
        cmd = "git " + txt
        pr = subprocess.Popen( cmd , shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
        (out, error) = pr.communicate()
        if len(error):
            raise Exception(cmd +" failed with [" + error.strip() + "]")
        return out

class AppDescriptor(object):
    """
    UAVCAN firmware image descriptor format:
    uint64_t signature (bytes [7:0] set to 'APDesc00' by linker script)
    uint64_t image_crc (set to 0 by linker script)
    uint32_t image_size (set to 0 by linker script)
    uint32_t vcs_commit (set in source or by this tool)
    uint8_t version_major (set in source)
    uint8_t version_minor (set in source)
    uint8_t reserved[6] (set to 0xFF by linker script)
    """

    LENGTH = 8 + 8 + 4 + 4 + 1 + 1 + 6
    SIGNATURE = b"APDesc00"
    RESERVED =  b"\xFF" * 6

    def __init__(self, bytes=None):
        self.signature = AppDescriptor.SIGNATURE
        self.image_crc = 0
        self.image_size = 0
        self.vcs_commit = 0
        self.version_major = 0
        self.version_minor = 0
        self.reserved = AppDescriptor.RESERVED

        if bytes:
            try:
                self.unpack(bytes)
            except Exception:
                raise ValueError("Invalid AppDescriptor: {0}".format(
                                 binascii.b2a_hex(bytes)))

    def pack(self):
        return struct.pack("<8sQLLBB6s", self.signature, self.image_crc,
                           self.image_size, self.vcs_commit,
                           self.version_major, self.version_minor,
                           self.reserved)

    def unpack(self, bytes):
        (self.signature, self.image_crc, self.image_size, self.vcs_commit,
            self.version_major, self.version_minor, self.reserved) = \
            struct.unpack("<8sQLLBB6s", bytes)

        if not self.empty and not self.valid:
            raise ValueError()

    @property
    def empty(self):
        return (self.signature == AppDescriptor.SIGNATURE and
                self.image_crc == 0 and self.image_size == 0 and
                self.reserved == AppDescriptor.RESERVED)

    @property
    def valid(self):
        return (self.signature == AppDescriptor.SIGNATURE and
                self.image_crc != 0 and self.image_size > 0 and
                self.reserved == AppDescriptor.RESERVED)

class FirmwareImage(object):
    def __init__(self, path_or_file, mode="r"):
        if getattr(path_or_file, "read", None):
            self._file = path_or_file
            self._do_close = False
            self._padding = 0
        else:
            if "b" not in mode:
                self._file = open(path_or_file, mode + "b")
            else:
                self._file = open(path_or_file, mode)
            self._do_close = True
            self._padding = 4

        if "r" in mode:
            self._contents = BytesIO(self._file.read())
        else:
            self._contents = BytesIO()
        self._do_write = False

        self._length = None
        self._descriptor_offset = None
        self._descriptor_bytes = None
        self._descriptor = None

    def __enter__(self):
        return self

    def __getattr__(self, attr):
        if attr == "write":
            self._do_write = True
        return getattr(self._contents, attr)

    def __iter__(self):
        return iter(self._contents)

    def __exit__(self, *args):
        if self._do_write:
            if getattr(self._file, "seek", None):
                self._file.seek(0)
            self._file.write(self._contents.getvalue())
            if  self._padding:
                self._file.write(b'\xff' * self._padding)

        if self._do_close:
            self._file.close()

    def _write_descriptor_raw(self):
        # Seek to the appropriate location, write the serialized
        # descriptor, and seek back.
        prev_offset = self._contents.tell()
        self._contents.seek(self._descriptor_offset)
        self._contents.write(self._descriptor.pack())
        self._contents.seek(prev_offset)

    def write_descriptor(self):
        # Set the descriptor's length and CRC to the values required for
        # CRC computation
        self.app_descriptor.image_size = self.length
        self.app_descriptor.image_crc = 0

        self._write_descriptor_raw()

        # Update the descriptor's CRC based on the computed value and write
        # it out again
        self.app_descriptor.image_crc = self.crc

        self._write_descriptor_raw()

    @property
    def crc(self):
        MASK = 0xFFFFFFFFFFFFFFFF
        POLY = 0x42F0E1EBA9EA3693

        # Calculate the image CRC with the image_crc field in the app
        # descriptor zeroed out.
        crc_offset = self.app_descriptor_offset + len(AppDescriptor.SIGNATURE)
        content = bytearray(self._contents.getvalue())
        content[crc_offset:crc_offset + 8] = bytearray.fromhex("00" * 8)
        if  self._padding:
            content += bytearray.fromhex("ff" * self._padding)
        val = MASK
        for byte in content:
            val ^= (byte << 56) & MASK
            for bit in range(8):
                if val & (1 << 63):
                    val = ((val << 1) & MASK) ^ POLY
                else:
                    val <<= 1

        return (val & MASK) ^ MASK

    @property
    def padding(self):
        return self._padding

    @property
    def length(self):
        if not self._length:
            # Find the length of the file by seeking to the end and getting
            # the offset
            prev_offset = self._contents.tell()
            self._contents.seek(0, os.SEEK_END)
            self._length = self._contents.tell()
            if self._padding:
                fill = self._length % self._padding
                if fill:
                    self._length += fill
                self._padding = fill
            self._contents.seek(prev_offset)

        return self._length

    @property
    def app_descriptor_offset(self):
        if not self._descriptor_offset:
            # Save the current position
            prev_offset = self._contents.tell()
            # Check each byte in the file to see if a valid descriptor starts
            # at that location. Slow, but not slow enough to matter.
            offset = 0
            while offset < self.length - AppDescriptor.LENGTH:
                self._contents.seek(offset)
                try:
                    # If this throws an exception, there isn't a valid
                    # descriptor at this offset
                    AppDescriptor(self._contents.read(AppDescriptor.LENGTH))
                except Exception:
                    offset += 1
                else:
                    self._descriptor_offset = offset
                    break
            # Go back to the previous position
            self._contents.seek(prev_offset)
            if not self._descriptor_offset:
                raise Exception('AppDescriptor not found')

        return self._descriptor_offset

    @property
    def app_descriptor(self):
        if not self._descriptor:
            # Save the current position
            prev_offset = self._contents.tell()
            # Jump to the descriptor adn parse it
            self._contents.seek(self.app_descriptor_offset)
            self._descriptor_bytes = self._contents.read(AppDescriptor.LENGTH)
            self._descriptor = AppDescriptor(self._descriptor_bytes)
            # Go back to the previous offset
            self._contents.seek(prev_offset)

        return self._descriptor

    @app_descriptor.setter
    def app_descriptor(self, value):
        self._descriptor = value

if __name__ == "__main__":
    parser = optparse.OptionParser(usage="usage: %prog [options] [IN OUT]")
    parser.add_option("--vcs-commit", dest="vcs_commit", default=None,
                      help="set the descriptor's VCS commit value to COMMIT",
                      metavar="COMMIT")
    parser.add_option("-g", "--use-git-hash", dest="use_git_hash", action="store_true",
                      help="set the descriptor's VCS commit value to the current git hash",
                      metavar="GIT")
    parser.add_option("--bootloader-size", dest="bootloader_size", default=0,
                      help="don't write the first SIZE bytes of the image",
                      metavar="SIZE")
    parser.add_option("--bootloader-image", dest="bootloader_image", default=0,
                      help="prepend a bootloader image to the output file",
                      metavar="IMAGE")
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
                      help="show additional firmware information on stdout")

    options, args = parser.parse_args()
    if len(args) not in (0, 2):
        parser.error("specify both IN or OUT for file operation, or " +
                     "neither for stdin/stdout operation")

    if options.vcs_commit and options.use_git_hash:
        parser.error("options --vcs-commit and --use-git-commit are mutually exclusive")

    if options.use_git_hash:
        try:
            options.vcs_commit = int(GitWrapper.command("rev-list HEAD --max-count=1 --abbrev=8 --abbrev-commit"),16)
        except Exception  as e:
            print("Git Command failed "+ str(e) +"- Exiting!")
            quit()

    if args:
        in_file = args[0]
        out_file = args[1]
    else:
        in_file = sys.stdin
        out_file = sys.stdout

    bootloader_image = b""
    if options.bootloader_image:
        with open(options.bootloader_image, "rb") as bootloader:
            bootloader_image = bootloader.read()

    bootloader_size = int(options.bootloader_size)

    with FirmwareImage(in_file, "rb") as in_image:
        with FirmwareImage(out_file, "wb") as out_image:
            image = in_image.read()
            out_image.write(bootloader_image)
            out_image.write(image[bootloader_size:])
            if options.vcs_commit:
                out_image.app_descriptor.vcs_commit = options.vcs_commit
            out_image.write_descriptor()

            if options.verbose:
                sys.stderr.write(
"""
Application descriptor located at offset 0x{0.app_descriptor_offset:08X}

""".format(in_image, in_image.app_descriptor, out_image.app_descriptor,
           bootloader_size, len(bootloader_image)))
                if bootloader_size:
                    sys.stderr.write(
"""Ignored the first {3:d} bytes of the input image. Prepended {4:d} bytes of
bootloader image to the output image.

""".format(in_image, in_image.app_descriptor, out_image.app_descriptor,
           bootloader_size, len(bootloader_image)))
                sys.stderr.write(
"""READ VALUES
------------------------------------------------------------------------------

Field               Type              Value
signature           uint64            {1.signature!r}
image_crc           uint64            0x{1.image_crc:016X}
image_size          uint32            0x{1.image_size:X} ({1.image_size:d} B)
vcs_commit          uint32            {1.vcs_commit:08X}
version_major       uint8             {1.version_major:d}
version_minor       uint8             {1.version_minor:d}
reserved            uint8[6]          {1.reserved!r}

WRITTEN VALUES
------------------------------------------------------------------------------

Field               Type              Value
signature           uint64            {2.signature!r}
image_crc           uint64            0x{2.image_crc:016X}
image_size          uint32            0x{2.image_size:X} ({2.image_size:d} B)
vcs_commit          uint32            {2.vcs_commit:08X}
version_major       uint8             {2.version_major:d}
version_minor       uint8             {2.version_minor:d}
reserved            uint8[6]          {2.reserved!r}

""".format(in_image, in_image.app_descriptor, out_image.app_descriptor,
           bootloader_size, len(bootloader_image)))
                if out_image.padding:
                    sys.stderr.write(
"""
padding added {}
""".format(out_image.padding))
<commit_msg>make_can_boot_descriptor:Get padding math correct<commit_after>#!/usr/bin/env python3

import os
import sys
import subprocess
import struct
import optparse
import binascii
from io import BytesIO

class GitWrapper:
    @classmethod
    def command(cls, txt):
        cmd = "git " + txt
        pr = subprocess.Popen( cmd , shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
        (out, error) = pr.communicate()
        if len(error):
            raise Exception(cmd +" failed with [" + error.strip() + "]")
        return out

class AppDescriptor(object):
    """
    UAVCAN firmware image descriptor format:
    uint64_t signature (bytes [7:0] set to 'APDesc00' by linker script)
    uint64_t image_crc (set to 0 by linker script)
    uint32_t image_size (set to 0 by linker script)
    uint32_t vcs_commit (set in source or by this tool)
    uint8_t version_major (set in source)
    uint8_t version_minor (set in source)
    uint8_t reserved[6] (set to 0xFF by linker script)
    """

    LENGTH = 8 + 8 + 4 + 4 + 1 + 1 + 6
    SIGNATURE = b"APDesc00"
    RESERVED =  b"\xFF" * 6

    def __init__(self, bytes=None):
        self.signature = AppDescriptor.SIGNATURE
        self.image_crc = 0
        self.image_size = 0
        self.vcs_commit = 0
        self.version_major = 0
        self.version_minor = 0
        self.reserved = AppDescriptor.RESERVED

        if bytes:
            try:
                self.unpack(bytes)
            except Exception:
                raise ValueError("Invalid AppDescriptor: {0}".format(
                                 binascii.b2a_hex(bytes)))

    def pack(self):
        return struct.pack("<8sQLLBB6s", self.signature, self.image_crc,
                           self.image_size, self.vcs_commit,
                           self.version_major, self.version_minor,
                           self.reserved)

    def unpack(self, bytes):
        (self.signature, self.image_crc, self.image_size, self.vcs_commit,
            self.version_major, self.version_minor, self.reserved) = \
            struct.unpack("<8sQLLBB6s", bytes)

        if not self.empty and not self.valid:
            raise ValueError()

    @property
    def empty(self):
        return (self.signature == AppDescriptor.SIGNATURE and
                self.image_crc == 0 and self.image_size == 0 and
                self.reserved == AppDescriptor.RESERVED)

    @property
    def valid(self):
        return (self.signature == AppDescriptor.SIGNATURE and
                self.image_crc != 0 and self.image_size > 0 and
                self.reserved == AppDescriptor.RESERVED)

class FirmwareImage(object):
    def __init__(self, path_or_file, mode="r"):
        if getattr(path_or_file, "read", None):
            self._file = path_or_file
            self._do_close = False
            self._padding = 0
        else:
            if "b" not in mode:
                self._file = open(path_or_file, mode + "b")
            else:
                self._file = open(path_or_file, mode)
            self._do_close = True
            self._padding = 4

        if "r" in mode:
            self._contents = BytesIO(self._file.read())
        else:
            self._contents = BytesIO()
        self._do_write = False

        self._length = None
        self._descriptor_offset = None
        self._descriptor_bytes = None
        self._descriptor = None

    def __enter__(self):
        return self

    def __getattr__(self, attr):
        if attr == "write":
            self._do_write = True
        return getattr(self._contents, attr)

    def __iter__(self):
        return iter(self._contents)

    def __exit__(self, *args):
        if self._do_write:
            if getattr(self._file, "seek", None):
                self._file.seek(0)
            self._file.write(self._contents.getvalue())
            if  self._padding:
                self._file.write(b'\xff' * self._padding)

        if self._do_close:
            self._file.close()

    def _write_descriptor_raw(self):
        # Seek to the appropriate location, write the serialized
        # descriptor, and seek back.
        prev_offset = self._contents.tell()
        self._contents.seek(self._descriptor_offset)
        self._contents.write(self._descriptor.pack())
        self._contents.seek(prev_offset)

    def write_descriptor(self):
        # Set the descriptor's length and CRC to the values required for
        # CRC computation
        self.app_descriptor.image_size = self.length
        self.app_descriptor.image_crc = 0

        self._write_descriptor_raw()

        # Update the descriptor's CRC based on the computed value and write
        # it out again
        self.app_descriptor.image_crc = self.crc

        self._write_descriptor_raw()

    @property
    def crc(self):
        MASK = 0xFFFFFFFFFFFFFFFF
        POLY = 0x42F0E1EBA9EA3693

        # Calculate the image CRC with the image_crc field in the app
        # descriptor zeroed out.
        crc_offset = self.app_descriptor_offset + len(AppDescriptor.SIGNATURE)
        content = bytearray(self._contents.getvalue())
        content[crc_offset:crc_offset + 8] = bytearray.fromhex("00" * 8)
        if  self._padding:
            content += bytearray.fromhex("ff" * self._padding)
        val = MASK
        for byte in content:
            val ^= (byte << 56) & MASK
            for bit in range(8):
                if val & (1 << 63):
                    val = ((val << 1) & MASK) ^ POLY
                else:
                    val <<= 1

        return (val & MASK) ^ MASK

    @property
    def padding(self):
        return self._padding

    @property
    def length(self):
        if not self._length:
            # Find the length of the file by seeking to the end and getting
            # the offset
            prev_offset = self._contents.tell()
            self._contents.seek(0, os.SEEK_END)
            self._length = self._contents.tell()
            if self._padding:
                fill = self._padding - (self._length % self._padding)
                if fill:
                    self._length += fill
                self._padding = fill
            self._contents.seek(prev_offset)

        return self._length

    @property
    def app_descriptor_offset(self):
        if not self._descriptor_offset:
            # Save the current position
            prev_offset = self._contents.tell()
            # Check each byte in the file to see if a valid descriptor starts
            # at that location. Slow, but not slow enough to matter.
            offset = 0
            while offset < self.length - AppDescriptor.LENGTH:
                self._contents.seek(offset)
                try:
                    # If this throws an exception, there isn't a valid
                    # descriptor at this offset
                    AppDescriptor(self._contents.read(AppDescriptor.LENGTH))
                except Exception:
                    offset += 1
                else:
                    self._descriptor_offset = offset
                    break
            # Go back to the previous position
            self._contents.seek(prev_offset)
            if not self._descriptor_offset:
                raise Exception('AppDescriptor not found')

        return self._descriptor_offset

    @property
    def app_descriptor(self):
        if not self._descriptor:
            # Save the current position
            prev_offset = self._contents.tell()
            # Jump to the descriptor adn parse it
            self._contents.seek(self.app_descriptor_offset)
            self._descriptor_bytes = self._contents.read(AppDescriptor.LENGTH)
            self._descriptor = AppDescriptor(self._descriptor_bytes)
            # Go back to the previous offset
            self._contents.seek(prev_offset)

        return self._descriptor

    @app_descriptor.setter
    def app_descriptor(self, value):
        self._descriptor = value

if __name__ == "__main__":
    parser = optparse.OptionParser(usage="usage: %prog [options] [IN OUT]")
    parser.add_option("--vcs-commit", dest="vcs_commit", default=None,
                      help="set the descriptor's VCS commit value to COMMIT",
                      metavar="COMMIT")
    parser.add_option("-g", "--use-git-hash", dest="use_git_hash", action="store_true",
                      help="set the descriptor's VCS commit value to the current git hash",
                      metavar="GIT")
    parser.add_option("--bootloader-size", dest="bootloader_size", default=0,
                      help="don't write the first SIZE bytes of the image",
                      metavar="SIZE")
    parser.add_option("--bootloader-image", dest="bootloader_image", default=0,
                      help="prepend a bootloader image to the output file",
                      metavar="IMAGE")
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
                      help="show additional firmware information on stdout")

    options, args = parser.parse_args()
    if len(args) not in (0, 2):
        parser.error("specify both IN or OUT for file operation, or " +
                     "neither for stdin/stdout operation")

    if options.vcs_commit and options.use_git_hash:
        parser.error("options --vcs-commit and --use-git-commit are mutually exclusive")

    if options.use_git_hash:
        try:
            options.vcs_commit = int(GitWrapper.command("rev-list HEAD --max-count=1 --abbrev=8 --abbrev-commit"),16)
        except Exception  as e:
            print("Git Command failed "+ str(e) +"- Exiting!")
            quit()

    if args:
        in_file = args[0]
        out_file = args[1]
    else:
        in_file = sys.stdin
        out_file = sys.stdout

    bootloader_image = b""
    if options.bootloader_image:
        with open(options.bootloader_image, "rb") as bootloader:
            bootloader_image = bootloader.read()

    bootloader_size = int(options.bootloader_size)

    with FirmwareImage(in_file, "rb") as in_image:
        with FirmwareImage(out_file, "wb") as out_image:
            image = in_image.read()
            out_image.write(bootloader_image)
            out_image.write(image[bootloader_size:])
            if options.vcs_commit:
                out_image.app_descriptor.vcs_commit = options.vcs_commit
            out_image.write_descriptor()

            if options.verbose:
                sys.stderr.write(
"""
Application descriptor located at offset 0x{0.app_descriptor_offset:08X}

""".format(in_image, in_image.app_descriptor, out_image.app_descriptor,
           bootloader_size, len(bootloader_image)))
                if bootloader_size:
                    sys.stderr.write(
"""Ignored the first {3:d} bytes of the input image. Prepended {4:d} bytes of
bootloader image to the output image.

""".format(in_image, in_image.app_descriptor, out_image.app_descriptor,
           bootloader_size, len(bootloader_image)))
                sys.stderr.write(
"""READ VALUES
------------------------------------------------------------------------------

Field               Type              Value
signature           uint64            {1.signature!r}
image_crc           uint64            0x{1.image_crc:016X}
image_size          uint32            0x{1.image_size:X} ({1.image_size:d} B)
vcs_commit          uint32            {1.vcs_commit:08X}
version_major       uint8             {1.version_major:d}
version_minor       uint8             {1.version_minor:d}
reserved            uint8[6]          {1.reserved!r}

WRITTEN VALUES
------------------------------------------------------------------------------

Field               Type              Value
signature           uint64            {2.signature!r}
image_crc           uint64            0x{2.image_crc:016X}
image_size          uint32            0x{2.image_size:X} ({2.image_size:d} B)
vcs_commit          uint32            {2.vcs_commit:08X}
version_major       uint8             {2.version_major:d}
version_minor       uint8             {2.version_minor:d}
reserved            uint8[6]          {2.reserved!r}

""".format(in_image, in_image.app_descriptor, out_image.app_descriptor,
           bootloader_size, len(bootloader_image)))
                if out_image.padding:
                    sys.stderr.write(
"""
padding added {}
""".format(out_image.padding))
<|endoftext|>


<commit_before>from flask import Flask, Response
from camera import create_camera
from io import BytesIO
from mjpeg import FrameSplitter

PAGE = """\

        <title>RapBot MJPEG Stream</title>

RapBot MJPEG Stream

    """.encode('utf-8')

app = Flask(__name__)
video_output = FrameSplitter()
camera = create_camera(video_output)

@app.route("/")
def root():
    return PAGE

@app.route("/still.jpg")
def still():
    def generate(buffer):
        camera.capture(buffer, format='jpeg', use_video_port=True)

        buffer.seek(0)

        yield buffer.read()

    return Response(generate(BytesIO()), mimetype='image/bmp')

@app.route("/stream.mjpg")
def stream():
    def generate():
        video_output.truncate()

        while True:
            frame = video_output.read()
            if frame:
                yield b'--FRAME\r\n'
                yield b'Content-Type: image/jpeg\r\n\r\n'
                yield frame
                yield b'\r\n'

    return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=FRAME')
<commit_msg>Update MIME type.<commit_after>from flask import Flask, Response
from camera import create_camera
from io import BytesIO
from mjpeg import FrameSplitter

PAGE = """\

        <title>RapBot MJPEG Stream</title>

RapBot MJPEG Stream

    """.encode('utf-8')

app = Flask(__name__)
video_output = FrameSplitter()
camera = create_camera(video_output)

@app.route("/")
def root():
    return PAGE

@app.route("/still.jpg")
def still():
    def generate(buffer):
        camera.capture(buffer, format='jpeg', use_video_port=True)

        buffer.seek(0)

        yield buffer.read()

    return Response(generate(BytesIO()), mimetype='image/jpeg')

@app.route("/stream.mjpg")
def stream():
    def generate():
        video_output.truncate()

        while True:
            frame = video_output.read()
            if frame:
                yield b'--FRAME\r\n'
                yield b'Content-Type: image/jpeg\r\n\r\n'
                yield frame
                yield b'\r\n'

    return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=FRAME')
<|endoftext|>


<commit_before>import numpy
from numpy import linalg

import cupy
from cupy.cuda import cublas
from cupy.cuda import cusolver_enabled
from cupy.cuda import device

if cusolver_enabled:
    from cupy.cuda import cusolver

def cholesky(a):
    '''Cholesky decomposition.

    Decompose a given two-dimensional square matrix into `L * L.T`,
    where `L` is a lower-triangular matrix and `.T` is a conjugate transpose
    operator. Note that in the current implementation `a` must be a real
    matrix, and only float32 and float64 are supported.

    Args:
        a (cupy.ndarray): The input matrix with dimension `(N, N)`

    .. seealso:: :func:`numpy.linalg.cholesky`
    '''
    if not cusolver_enabled:
        raise RuntimeError('Current cupy only supports cusolver in CUDA 8.0')

    # TODO(Saito): Current implementation only accepts two-dimensional arrays
    _assertCupyArray(a)
    _assertRank2(a)
    _assertNdSquareness(a)

    ret_dtype = a.dtype.char
    # Cast to float32 or float64
    if ret_dtype == 'f' or ret_dtype == 'd':
        dtype = ret_dtype
    else:
        dtype = numpy.find_common_type((ret_dtype, 'f'), ()).char

    x = a.astype(dtype, copy=True)
    n = a.shape[0]
    handle = device.get_cusolver_handle()
    devInfo = cupy.empty(1, dtype=numpy.int32)
    if x.dtype.char == 'f':
        buffersize = cusolver.spotrf_bufferSize(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n)
        workspace = cupy.empty(buffersize, dtype=numpy.float32)
        cusolver.spotrf(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n,
            workspace.data.ptr, buffersize, devInfo.data.ptr)
    else:  # a.dtype.char == 'd'
        buffersize = cusolver.dpotrf_bufferSize(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n)
        workspace = cupy.empty(buffersize, dtype=numpy.float64)
        cusolver.dpotrf(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n,
            workspace.data.ptr, buffersize, devInfo.data.ptr)
    status = int(devInfo[0])
    if status > 0:
        raise linalg.LinAlgError(
            'The leading minor of order {} '
            'is not positive definite'.format(status))
    elif status < 0:
        raise linalg.LinAlgError(
            'Parameter error (maybe caused by a bug in cupy.linalg?)')
    _tril(x, k=0)
    return x

# TODO(okuta): Implement qr

# TODO(okuta): Implement svd

def _assertCupyArray(*arrays):
    for a in arrays:
        if not isinstance(a, cupy.core.ndarray):
            raise linalg.LinAlgError('cupy.linalg only supports cupy.core.ndarray')

def _assertRank2(*arrays):
    for a in arrays:
        if len(a.shape) != 2:
            raise linalg.LinAlgError(
                '{}-dimensional array given. Array must be '
                'two-dimensional'.format(len(a.shape)))

def _assertNdSquareness(*arrays):
    for a in arrays:
        if max(a.shape[-2:]) != min(a.shape[-2:]):
            raise linalg.LinAlgError('Last 2 dimensions of the array must be square')

def _tril(x, k=0):
    m, n = x.shape
    u = cupy.arange(m).reshape(m, 1)
    v = cupy.arange(n).reshape(1, n)
    mask = v - u <= k
    x *= mask
    return x
<commit_msg>use snake-case<commit_after>import numpy
from numpy import linalg

import cupy
from cupy.cuda import cublas
from cupy.cuda import cusolver_enabled
from cupy.cuda import device

if cusolver_enabled:
    from cupy.cuda import cusolver

def cholesky(a):
    '''Cholesky decomposition.

    Decompose a given two-dimensional square matrix into `L * L.T`,
    where `L` is a lower-triangular matrix and `.T` is a conjugate transpose
    operator. Note that in the current implementation `a` must be a real
    matrix, and only float32 and float64 are supported.

    Args:
        a (cupy.ndarray): The input matrix with dimension `(N, N)`

    .. seealso:: :func:`numpy.linalg.cholesky`
    '''
    if not cusolver_enabled:
        raise RuntimeError('Current cupy only supports cusolver in CUDA 8.0')

    # TODO(Saito): Current implementation only accepts two-dimensional arrays
    _assert_cupy_array(a)
    _assert_rank2(a)
    _assert_nd_squareness(a)

    ret_dtype = a.dtype.char
    # Cast to float32 or float64
    if ret_dtype == 'f' or ret_dtype == 'd':
        dtype = ret_dtype
    else:
        dtype = numpy.find_common_type((ret_dtype, 'f'), ()).char

    x = a.astype(dtype, copy=True)
    n = len(a)
    handle = device.get_cusolver_handle()
    dev_info = cupy.empty(1, dtype=numpy.int32)
    if x.dtype.char == 'f':
        buffersize = cusolver.spotrf_bufferSize(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n)
        workspace = cupy.empty(buffersize, dtype=numpy.float32)
        cusolver.spotrf(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n,
            workspace.data.ptr, buffersize, dev_info.data.ptr)
    else:  # a.dtype.char == 'd'
        buffersize = cusolver.dpotrf_bufferSize(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n)
        workspace = cupy.empty(buffersize, dtype=numpy.float64)
        cusolver.dpotrf(
            handle, cublas.CUBLAS_FILL_MODE_UPPER, n, x.data.ptr, n,
            workspace.data.ptr, buffersize, dev_info.data.ptr)
    status = int(dev_info[0])
    if status > 0:
        raise linalg.LinAlgError(
            'The leading minor of order {} '
            'is not positive definite'.format(status))
    elif status < 0:
        raise linalg.LinAlgError(
            'Parameter error (maybe caused by a bug in cupy.linalg?)')
    _tril(x, k=0)
    return x

# TODO(okuta): Implement qr

# TODO(okuta): Implement svd

def _assert_cupy_array(*arrays):
    for a in arrays:
        if not isinstance(a, cupy.core.ndarray):
            raise linalg.LinAlgError('cupy.linalg only supports cupy.core.ndarray')

def _assert_rank2(*arrays):
    for a in arrays:
        if len(a.shape) != 2:
            raise linalg.LinAlgError(
                '{}-dimensional array given. Array must be '
                'two-dimensional'.format(len(a.shape)))

def _assert_nd_squareness(*arrays):
    for a in arrays:
        if max(a.shape[-2:]) != min(a.shape[-2:]):
            raise linalg.LinAlgError('Last 2 dimensions of the array must be square')

def _tril(x, k=0):
    m, n = x.shape
    u = cupy.arange(m).reshape(m, 1)
    v = cupy.arange(n).reshape(1, n)
    mask = v - u <= k
    x *= mask
    return x
<|endoftext|>


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

'''
    Copyright (c) 2016-2017 Wind River Systems, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at:
    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software  distributed
    under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
    OR CONDITIONS OF ANY KIND, either express or implied.
'''

"""
Wind River Helix Device Cloud Python-based Device Manager. This module provides
multiple device management methods and attributes that allow for management of
a connected device.
"""

from collections import namedtuple
import errno
import json
import os
from os.path import abspath
import pkg_resources
import platform
import signal
import sys
from time import sleep
import uuid

from helix import osal
from helix import ota_handler
from helix import relay
import helix as iot

running = True

def sighandler(signum, frame):
    """
    Signal handler for exiting app.
    """
    global running
    print("Received signal {}, stopping application...".format(signum))
    running = False

def ack_messages(client, path):
    """
    Check for a pending message to acknowledge and, if present, send the mailbox
    acknowledgement to the cloud.
    """
    if os.path.isfile(path):
        with open(path, 'r') as id_file:
            msg_id = id_file.read()
            if msg_id:
                client.action_acknowledge(msg_id)

        os.remove(path)

def action_register_conditional(client, name, callback, enabled, \
                                user_data=None):
    """
    Register an action with the HDC client if it is enabled, otherwise register
    it using a generic "not implemented" callback.
    """
    return client.action_register_callback(name, callback \
            if enabled else method_not_implemented, user_data)

def agent_reset(client, params, user_data, request):
    """
    Callback function for the "reset_agent" method. Will stop and restart the
    device manager app.
    """
    global running
    running = False

    path = os.path.join(user_data[0], "message_ids")
    with open(path, 'w') as id_file:
        id_file.write(request.request_id)

    user_data[1].join()
    client.disconnect(wait_for_replies=True)
    osal.execl("python", "device_manager.py")

    # If this return is hit, then the device manager did not restart properly
    return (iot.STATUS_FAILURE, "Device Manager Failed to Restart!")

def config_load(cfg_dir=".", cfg_name="iot.cfg"):
    """
    Open and read configuration information from iot.cfg
    """
    config_data = None
    try:
        with open(os.path.join(cfg_dir, cfg_name), 'r') as cfg_file:
            config_data = json.load(cfg_file, \
                object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
    except IOError as error:
        print("Error parsing JSON from iot.cfg")
        print(error)

    return config_data

def device_decommission(client, params, user_data):
    """
    Callback for the "decommission_device" method that will remove HDC config
    files, preventing the device from reconnecting to the cloud. The device
    manager app will then shut down.
    """
    global running

    files_to_remove = [
        "iot.cfg",
        "iot-connect.cfg",
        "device_id"
        ]

    directories_to_remove = [
        os.path.join(user_data[0], "upload"),
        os.path.join(user_data[0], "download"),
        user_data[0]
        ]

    client.log(iot.LOGINFO, "Decommissioning Device!")

    for f in files_to_remove:
        try:
            os.remove(f)
        except OSError:
            client.log(iot.LOGWARNING, "Failed to remove {}".format(f))

    for directory in directories_to_remove:
        try:
            os.rmdir(directory)
        except OSError:
            client.log(iot.LOGWARNING, "Failed to remove {}".format(directory))

    client.log(iot.LOGINFO, "Device decommissioned! Stopping device manager.")
    running = False

    return (iot.STATUS_SUCCESS, "")

def device_reboot():
    """
    Callback for the "reboot_device" method which reboots the system.
    """
    retval = osal.system_reboot()

    if retval == 0:
        status = iot.STATUS_SUCCESS
        message = ""
    elif retval == osal.NOT_SUPPORTED:
        status = iot.STATUS_NOT_SUPPORTED
        message = "Reboot not supported on this platform!"
    else:
        status = iot.STATUS_FAILURE
        message = "Reboot failed with return code: " + str(retval)

    return (status, message)

def device_shutdown():
    """
    Callback for the "reboot_device" method which shuts-down the system.
    """
    retval = osal.system_shutdown()

    if retval == 0:
        status = iot.STATUS_SUCCESS
        message = ""
    elif retval == osal.NOT_SUPPORTED:
        status = iot.STATUS_NOT_SUPPORTED
        message = "Shutdown not supported on this platform!"
    else:
        status = iot.STATUS_FAILURE
        message = "Shutdown failed with return code: " + str(retval)

    return (status, message)

def file_download(client, params, user_data):
    """
    Callback for the "file_download" method which downloads a file from the
    cloud to the local system.
    """
    file_name = None
    file_path = None
    result = None
    if params:
        file_name = params.get("file_name")
        file_path = params.get("file_path")

        if file_name and not file_path:
            file_path = abspath(os.path.join(user_data[0], "download",
                                             file_name))
        if file_path and not file_name:
            file_name = os.path.basename(file_path)
        if file_path.startswith('~'):
            result = iot.STATUS_BAD_PARAMETER
            message = "Paths cannot use '~' to reference a home directory"
        elif not os.path.isabs(file_path):
            file_path = abspath(os.path.join(user_data[0], "download",
                                             file_path))

        file_global = params.get("use_global_store", False)

    if result is None:
        if file_name and file_path:
            dir_path = os.path.dirname(file_path)
            if not os.path.exists(dir_path):
                try:
                    os.makedirs(dir_path)
                except (OSError, IOError) as e:
                    result = iot.STATUS_IO_ERROR
                    message = ("Destination directory does not exist and could "
                               "not be created!")
                    client.error(message)
                    print(e)

            if result is None:
                client.log(iot.LOGINFO, "Downloading")
                result = client.file_download(file_name, file_path, \
                                              blocking=True, timeout=15, \
                                              file_global=file_global)
                if result == iot.STATUS_SUCCESS:
                    message = ""
                else:
                    message = iot.status_string(result)

        else:
            result = iot.STATUS_BAD_PARAMETER
            message = "No file name or destination given"

    return (result, message)

def file_upload(client, params, user_data):
    """
    Callback for the "file_upload" method which uploads a file from the
    cloud to the local system. Wildcards in the file name are supported.
    """
    file_name = None
    file_path = None
    result = None
    if params:
        file_name = params.get("file_name")
        file_path = params.get("file_path")

        if file_name and not file_path:
            file_path = abspath(os.path.join(user_data[0], "upload", file_name))
        if file_path and not file_name:
            file_name = os.path.basename(file_path)
        if file_path.startswith('~'):
            result = iot.STATUS_BAD_PARAMETER
            message = "Paths cannot use '~' to reference a home directory"
        elif not os.path.isabs(file_path):
            file_path = abspath(os.path.join(user_data[0], "upload",
                                             file_path))

        file_global = params.get("use_global_store", False)

    if result is None:
        if file_name and file_path:
            client.log(iot.LOGINFO, "Uploading {}".format(file_name))
            result = client.file_upload(file_path, upload_name=file_name, \
                                        blocking=True, timeout=240, \
                                        file_global=file_global)
            if result == iot.STATUS_SUCCESS:
                message = ""
                if user_data[1] and file_path.startswith(user_data[0]):
                    os.remove(file_name)
            else:
                message = iot.status_string(result)
        else:
            result = iot.STATUS_BAD_PARAMETER
            message = "No file name or location given"

    return (result, message)

def get_adapter_mac():
    """
    Returns the MAC address for the current network adapter
    """
    rmac = uuid.getnode()

    # Check if valid MAC or a fake one before proceeding
    if (rmac >> 40)%2:
        mac = "00:00:00:00:00:00"
    else:
        # Convert MAC into typical hex notation
        mac = hex(rmac).replace('0x', '')[:-1].upper().rjust(12, '0')
        for i in range(0, 5):
            ii = 2*(i+1)+i
            mac = mac[:ii] + ":" + mac[ii:]
    return mac

def method_not_implemented():
    """
    Callback for disabled methods that simply tells the cloud that the requested
    method is not enabled for this device
    """
    return (iot.STATUS_NOT_SUPPORTED, \
            "This method is disabled by its iot.cfg setting")

def publish_platform_info(client):
    """
    Function to publish information about the current platform (device) to the
    cloud as attributes.
    """
    client.log(iot.LOGINFO, "Publishing platform Info")

    try:
        hdc_version = pkg_resources.get_distribution("helix").version
    except pkg_resources.DistributionNotFound:
        hdc_version = "Unknown"

    client.attribute_publish("os_name", osal.os_name())
    client.attribute_publish("os_version", osal.os_version())
    client.attribute_publish("architecture", platform.machine())
    client.attribute_publish("hostname", platform.node())
    client.attribute_publish("kernel", osal.os_kernel())
    client.attribute_publish("hdc_version", hdc_version)
    client.attribute_publish("mac_address", get_adapter_mac())

def quit_me():
    """
    Callback for the "quit" method which exits the device manager app.
    """
    global running
    running = False
    return (iot.STATUS_SUCCESS, "")

def remote_access(client, params):
    """
    Start relay which will attempt to connect to a Cloud service and a local
    service and securely tunnel information between the two. Used primarily for
    Telnet.
    """
    url = params["url"]
    host = params["host"]
    protocol = int(params["protocol"])

    secure = client.config.validate_cloud_cert is not False
    try:
        relay.create_relay(url, host, protocol, secure=secure,
                           log_func=client.log)
        return (iot.STATUS_SUCCESS, "")
    except Exception as error:
        client.error(str(error))
        return (iot.STATUS_FAILURE, str(error))

if __name__ == "__main__":
    signal.signal(signal.SIGINT, sighandler)
    if osal.POSIX:
        signal.signal(signal.SIGQUIT, sighandler)

    # Initialize client called 'device_manager_py'
    client = iot.Client("device_manager_py")
    client.config.config_file = "iot-connect.cfg"
    client.initialize()

    config = config_load()
    runtime_dir = config.runtime_dir

    upload_dir = os.path.join(runtime_dir, "upload")
    download_dir = os.path.join(runtime_dir, "download")

    try:
        if not os.path.isdir(runtime_dir):
            os.mkdir(runtime_dir)

        if not os.path.isdir(upload_dir):
            os.mkdir(upload_dir)

        if not os.path.isdir(download_dir):
            os.mkdir(download_dir)

    except (OSError, IOError) as e:
        print(e)
        client.log(iot.LOGERROR, ("Could not create one or more runtime "
                                  "directories! Did you run the device manager "
                                  "with sufficient priviliges?"))

    # Setup an OTA Handler
    ota = ota_handler.OTAHandler()

    # Set action callbacks, if enabled in iot.cfg
    action_register_conditional(client, "file_download", file_download, \
                                config.actions_enabled.file_transfers, \
                                (runtime_dir,))
    action_register_conditional(client, "file_upload", file_upload, \
                                config.actions_enabled.file_transfers, \
                                (runtime_dir, config.upload_remove_on_success))

    action_register_conditional(client, "shutdown_device", device_shutdown, \
                                config.actions_enabled.shutdown_device)

    action_register_conditional(client, "reboot_device", device_reboot, \
                                config.actions_enabled.reboot_device)

    action_register_conditional(client, "remote-access", remote_access, \
                                config.actions_enabled.remote_login)

    action_register_conditional(client, "decommission_device", \
                                device_decommission, \
                                config.actions_enabled.decommission_device, \
                                (runtime_dir,))

    action_register_conditional(client, "software_update", \
                                ota.update_callback, \
                                config.actions_enabled.software_update,
                                (runtime_dir,))

    action_register_conditional(client, "reset_agent", agent_reset, \
                                config.actions_enabled.reset_agent, \
                                (runtime_dir, ota))
    action_register_conditional(client, "quit", quit_me, \
                                config.actions_enabled.reset_agent)

    # Connect to Cloud
    if client.connect(timeout=10) != iot.STATUS_SUCCESS:
        client.error("Failed")
        sys.exit(1)

    ack_messages(client, os.path.join(runtime_dir, "message_ids"))

    # Publish system details
    publish_platform_info(client)

    while running and client.is_alive():
        # Wrap sleep with an exception handler to fix SIGINT handling on Windows
        try:
            sleep(1)
        except IOError as err:
            if err.errno != errno.EINTR:
                raise

    # Stop remote access
    relay.stop_relays()

    # Wait for any OTA operations to finish
    if ota.is_running():
        client.log(iot.LOGINFO, "Waiting for OTA to finish...")
        ota.join()

    client.disconnect(wait_for_replies=True)

<commit_msg>WORKING: device manager<commit_after>#!/usr/bin/env python

'''
    Copyright (c) 2016-2017 Wind River Systems, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at:
    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software  distributed
    under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
    OR CONDITIONS OF ANY KIND, either express or implied.
'''

"""
Wind River Helix Device Cloud Python-based Device Manager. This module provides
multiple device management methods and attributes that allow for management of
a connected device.
"""

from collections import namedtuple
import errno
import json
import os
from os.path import abspath
import pkg_resources
import platform
import signal
import sys
from time import sleep
import uuid

from helix import osal
from helix import ota_handler
from helix import relay
import helix as iot

running = True

default_confg_dir = "."

def sighandler(signum, frame):
    """
    Signal handler for exiting app.
    """
    global running
    print("Received signal {}, stopping application...".format(signum))
    running = False

def ack_messages(client, path):
    """
    Check for a pending message to acknowledge and, if present, send the mailbox
    acknowledgement to the cloud.
    """
    if os.path.isfile(path):
        with open(path, 'r') as id_file:
            msg_id = id_file.read()
            if msg_id:
                client.action_acknowledge(msg_id)

        os.remove(path)

def action_register_conditional(client, name, callback, enabled, \
                                user_data=None):
    """
    Register an action with the HDC client if it is enabled, otherwise register
    it using a generic "not implemented" callback.
    """
    return client.action_register_callback(name, callback \
            if enabled else method_not_implemented, user_data)

def agent_reset(client, params, user_data, request):
    """
    Callback function for the "reset_agent" method. Will stop and restart the
    device manager app.
    """
    global running
    running = False

    path = os.path.join(user_data[0], "message_ids")
    with open(path, 'w') as id_file:
        id_file.write(request.request_id)

    user_data[1].join()
    client.disconnect(wait_for_replies=True)
    osal.execl("python", "device_manager.py")

    # If this return is hit, then the device manager did not restart properly
    return (iot.STATUS_FAILURE, "Device Manager Failed to Restart!")

def config_load(cfg_dir=default_cfg_dir, cfg_name="iot.cfg"):
    """
    Open and read configuration information from iot.cfg
    """
    config_data = None
    try:
        with open(os.path.join(cfg_dir, cfg_name), 'r') as cfg_file:
            config_data = json.load(cfg_file, \
                object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
    except IOError as error:
        print("Error parsing JSON from iot.cfg")
        print(error)

    return config_data

def device_decommission(client, params, user_data):
    """
    Callback for the "decommission_device" method that will remove HDC config
    files, preventing the device from reconnecting to the cloud. The device
    manager app will then shut down.
    """
    global running

    # TODO: fix this for paths use default_cfg_dir and
    # default_runtime_dir
    files_to_remove = [
        default_cfg_dir + "iot.cfg",
        default_cfg_dir + "iot-connect.cfg",
        runtime_dir + "device_id"
        ]

    directories_to_remove = [
        os.path.join(user_data[0], "upload"),
        os.path.join(user_data[0], "download"),
        user_data[0]
        ]

    client.log(iot.LOGINFO, "Decommissioning Device!")

    for f in files_to_remove:
        try:
            os.remove(f)
        except OSError:
            client.log(iot.LOGWARNING, "Failed to remove {}".format(f))

    for directory in directories_to_remove:
        try:
            os.rmdir(directory)
        except OSError:
            client.log(iot.LOGWARNING, "Failed to remove {}".format(directory))

    client.log(iot.LOGINFO, "Device decommissioned! Stopping device manager.")
    running = False

    return (iot.STATUS_SUCCESS, "")

def device_reboot():
    """
    Callback for the "reboot_device" method which reboots the system.
    """
    retval = osal.system_reboot()

    if retval == 0:
        status = iot.STATUS_SUCCESS
        message = ""
    elif retval == osal.NOT_SUPPORTED:
        status = iot.STATUS_NOT_SUPPORTED
        message = "Reboot not supported on this platform!"
    else:
        status = iot.STATUS_FAILURE
        message = "Reboot failed with return code: " + str(retval)

    return (status, message)

def device_shutdown():
    """
    Callback for the "reboot_device" method which shuts-down the system.
    """
    retval = osal.system_shutdown()

    if retval == 0:
        status = iot.STATUS_SUCCESS
        message = ""
    elif retval == osal.NOT_SUPPORTED:
        status = iot.STATUS_NOT_SUPPORTED
        message = "Shutdown not supported on this platform!"
    else:
        status = iot.STATUS_FAILURE
        message = "Shutdown failed with return code: " + str(retval)

    return (status, message)

def file_download(client, params, user_data):
    """
    Callback for the "file_download" method which downloads a file from the
    cloud to the local system.
    """
    file_name = None
    file_path = None
    result = None
    if params:
        file_name = params.get("file_name")
        file_path = params.get("file_path")

        if file_name and not file_path:
            file_path = abspath(os.path.join(user_data[0], "download",
                                             file_name))
        if file_path and not file_name:
            file_name = os.path.basename(file_path)
        if file_path.startswith('~'):
            result = iot.STATUS_BAD_PARAMETER
            message = "Paths cannot use '~' to reference a home directory"
        elif not os.path.isabs(file_path):
            file_path = abspath(os.path.join(user_data[0], "download",
                                             file_path))

        file_global = params.get("use_global_store", False)

    if result is None:
        if file_name and file_path:
            dir_path = os.path.dirname(file_path)
            if not os.path.exists(dir_path):
                try:
                    os.makedirs(dir_path)
                except (OSError, IOError) as e:
                    result = iot.STATUS_IO_ERROR
                    message = ("Destination directory does not exist and could "
                               "not be created!")
                    client.error(message)
                    print(e)

            if result is None:
                client.log(iot.LOGINFO, "Downloading")
                result = client.file_download(file_name, file_path, \
                                              blocking=True, timeout=15, \
                                              file_global=file_global)
                if result == iot.STATUS_SUCCESS:
                    message = ""
                else:
                    message = iot.status_string(result)

        else:
            result = iot.STATUS_BAD_PARAMETER
            message = "No file name or destination given"

    return (result, message)

def file_upload(client, params, user_data):
    """
    Callback for the "file_upload" method which uploads a file from the
    cloud to the local system. Wildcards in the file name are supported.
    """
    file_name = None
    file_path = None
    result = None
    if params:
        file_name = params.get("file_name")
        file_path = params.get("file_path")

        if file_name and not file_path:
            file_path = abspath(os.path.join(user_data[0], "upload", file_name))
        if file_path and not file_name:
            file_name = os.path.basename(file_path)
        if file_path.startswith('~'):
            result = iot.STATUS_BAD_PARAMETER
            message = "Paths cannot use '~' to reference a home directory"
        elif not os.path.isabs(file_path):
            file_path = abspath(os.path.join(user_data[0], "upload",
                                             file_path))

        file_global = params.get("use_global_store", False)

    if result is None:
        if file_name and file_path:
            client.log(iot.LOGINFO, "Uploading {}".format(file_name))
            result = client.file_upload(file_path, upload_name=file_name, \
                                        blocking=True, timeout=240, \
                                        file_global=file_global)
            if result == iot.STATUS_SUCCESS:
                message = ""
                if user_data[1] and file_path.startswith(user_data[0]):
                    os.remove(file_name)
            else:
                message = iot.status_string(result)
        else:
            result = iot.STATUS_BAD_PARAMETER
            message = "No file name or location given"

    return (result, message)

def get_adapter_mac():
    """
    Returns the MAC address for the current network adapter
    """
    rmac = uuid.getnode()

    # Check if valid MAC or a fake one before proceeding
    if (rmac >> 40)%2:
        mac = "00:00:00:00:00:00"
    else:
        # Convert MAC into typical hex notation
        mac = hex(rmac).replace('0x', '')[:-1].upper().rjust(12, '0')
        for i in range(0, 5):
            ii = 2*(i+1)+i
            mac = mac[:ii] + ":" + mac[ii:]
    return mac

def method_not_implemented():
    """
    Callback for disabled methods that simply tells the cloud that the requested
    method is not enabled for this device
    """
    return (iot.STATUS_NOT_SUPPORTED, \
            "This method is disabled by its iot.cfg setting")

def publish_platform_info(client):
    """
    Function to publish information about the current platform (device) to the
    cloud as attributes.
    """
    client.log(iot.LOGINFO, "Publishing platform Info")

    try:
        hdc_version = pkg_resources.get_distribution("helix").version
    except pkg_resources.DistributionNotFound:
        hdc_version = "Unknown"

    client.attribute_publish("os_name", osal.os_name())
    client.attribute_publish("os_version", osal.os_version())
    client.attribute_publish("architecture", platform.machine())
    client.attribute_publish("hostname", platform.node())
    client.attribute_publish("kernel", osal.os_kernel())
    client.attribute_publish("hdc_version", hdc_version)
    client.attribute_publish("mac_address", get_adapter_mac())

def quit_me():
    """
    Callback for the "quit" method which exits the device manager app.
    """
    global running
    running = False
    return (iot.STATUS_SUCCESS, "")

def remote_access(client, params):
    """
    Start relay which will attempt to connect to a Cloud service and a local
    service and securely tunnel information between the two. Used primarily for
    Telnet.
    """
    url = params["url"]
    host = params["host"]
    protocol = int(params["protocol"])

    secure = client.config.validate_cloud_cert is not False
    try:
        relay.create_relay(url, host, protocol, secure=secure,
                           log_func=client.log)
        return (iot.STATUS_SUCCESS, "")
    except Exception as error:
        client.error(str(error))
        return (iot.STATUS_FAILURE, str(error))

if __name__ == "__main__":
    signal.signal(signal.SIGINT, sighandler)
    if osal.POSIX:
        signal.signal(signal.SIGQUIT, sighandler)

    # Initialize client called 'device_manager_py'
    client = iot.Client("device_manager_py")
    client.config.config_file = default_cfg_dir + "/iot-connect.cfg"
    client.initialize()

    config = config_load()
    runtime_dir = config.runtime_dir

    upload_dir = os.path.join(runtime_dir, "upload")
    download_dir = os.path.join(runtime_dir, "download")

    try:
        if not os.path.isdir(runtime_dir):
            os.mkdir(runtime_dir)

        if not os.path.isdir(upload_dir):
            os.mkdir(upload_dir)

        if not os.path.isdir(download_dir):
            os.mkdir(download_dir)

    except (OSError, IOError) as e:
        print(e)
        client.log(iot.LOGERROR, ("Could not create one or more runtime "
                                  "directories! Did you run the device manager "
                                  "with sufficient priviliges?"))

    # Setup an OTA Handler
    ota = ota_handler.OTAHandler()

    # Set action callbacks, if enabled in iot.cfg
    action_register_conditional(client, "file_download", file_download, \
                                config.actions_enabled.file_transfers, \
                                (runtime_dir,))
    action_register_conditional(client, "file_upload", file_upload, \
                                config.actions_enabled.file_transfers, \
                                (runtime_dir, config.upload_remove_on_success))

    action_register_conditional(client, "shutdown_device", device_shutdown, \
                                config.actions_enabled.shutdown_device)

    action_register_conditional(client, "reboot_device", device_reboot, \
                                config.actions_enabled.reboot_device)

    action_register_conditional(client, "remote-access", remote_access, \
                                config.actions_enabled.remote_login)

    action_register_conditional(client, "decommission_device", \
                                device_decommission, \
                                config.actions_enabled.decommission_device, \
                                (runtime_dir,))

    action_register_conditional(client, "software_update", \
                                ota.update_callback, \
                                config.actions_enabled.software_update,
                                (runtime_dir,))

    action_register_conditional(client, "reset_agent", agent_reset, \
                                config.actions_enabled.reset_agent, \
                                (runtime_dir, ota))
    action_register_conditional(client, "quit", quit_me, \
                                config.actions_enabled.reset_agent)

    # Connect to Cloud
    if client.connect(timeout=10) != iot.STATUS_SUCCESS:
        client.error("Failed")
        sys.exit(1)

    ack_messages(client, os.path.join(runtime_dir, "message_ids"))

    # Publish system details
    publish_platform_info(client)

    while running and client.is_alive():
        # Wrap sleep with an exception handler to fix SIGINT handling on Windows
        try:
            sleep(1)
        except IOError as err:
            if err.errno != errno.EINTR:
                raise

    # Stop remote access
    relay.stop_relays()

    # Wait for any OTA operations to finish
    if ota.is_running():
        client.log(iot.LOGINFO, "Waiting for OTA to finish...")
        ota.join()

    client.disconnect(wait_for_replies=True)

<|endoftext|>


<commit_before>"""
  Sub class of BaseInventory, implemented with dictionary:
  key: id of the product
  value: the Product object
"""

from base_inventory import BaseInventory
from product import Product

class DictInventory(BaseInventory):

  def __init__(self):
    BaseInventory.__init__(self)
    self.products = {}
    self.test_init()

  def get_product(self, product_id):
    if product_id in self.products:
      return self.products[product_id]
    else:
      return None

  def get_all(self):
    result = {}
    for k in self.products:
      result[k] = self.products[k].__str__()
    return result

  def put_product(self, id, info):
    self.products[id] = Product(id, info['location_id'], info['used'],
                                info['new'], info['open_box'], info['restock_level'])
    return True

  def delete_product(self, product_id):
    del self.products[product_id]
    return True

  def test_init(self):
    self.products[1] = Product(1,1,1,1,1,4)
    self.products[2] = Product(2,2,2,2,2,7)<commit_msg>explicitly raise key error<commit_after>"""
  Sub class of BaseInventory, implemented with dictionary:
  key: id of the product
  value: the Product object
"""

from base_inventory import BaseInventory
from product import Product

class DictInventory(BaseInventory):

  def __init__(self):
    BaseInventory.__init__(self)
    self.products = {}
    self.test_init()

  def get_product(self, product_id):
    if product_id in self.products:
      return self.products[product_id]
    else:
      return None

  def get_all(self):
    result = {}
    for k in self.products:
      result[k] = self.products[k].__str__()
    return result

  def put_product(self, id, info):
    self.products[id] = Product(id, info['location_id'], info['used'],
                                info['new'], info['open_box'], info['restock_level'])
    return True

  def delete_product(self, product_id):
    if product_id in self.products:
      del self.products[product_id]
      return True
    else:
      raise KeyError('No product with product id %d' % product_id)

  def test_init(self):
    self.products[1] = Product(1,1,1,1,1,4)
    self.products[2] = Product(2,2,2,2,2,7)<|endoftext|>


<commit_before>from hgext import color

# define new style
color._styles['diff.inserted_highlight'] = 'green_background'
color._styles['diff.deleted_highlight'] = 'red_background'

class colorui(color.colorui):
    changes = None

    def __init__(self, src=None):
        super(colorui, self).__init__(src)
        self.changes = []

    def write(self, *args, **opts):
        label = opts.get('label')
        if label in ('diff.inserted', 'diff.deleted'):
            self.changes.append((args, opts))
        elif label == 'diff.trailingwhitespace':  # merge to hunk
            change = self.changes.pop()
            self.changes.append((change[0] + args, change[1]))
        elif label == '' and args == ("\n",) and self.changes:
            self.changes.append((args, opts))
        else:
            self.flush_hunk()
            super(colorui, self).write(*args, **opts)

    def flush_hunk(self):
        if self.changes is None:  # not initialized yet
            return

        inserted = [change for change in self.changes
                    if change[1]['label'] == 'diff.inserted']
        deleted = [change for change in self.changes
                   if change[1]['label'] == 'diff.deleted']

        if len(inserted) == 0 or len(deleted) == 0:
            # it's new line or deleted line
            pass
        elif len(inserted) != len(deleted):
            # do not markup word-highlights if numbers of lines are mismatched
            pass
        else:
            self.rearrange_highlight(inserted, deleted)

        # flush hunk
        for change in self.changes:
            super(colorui, self).write(*change[0], **change[1])

        self.changes = []

    def rearrange_highlight(self, inserted, deleted):
        self.changes = []
        deleted_lines = []
        write = super(colorui, self).write

        for i in range(len(inserted)):
            inserted_diff, deleted_diff = worddiff("".join(inserted[i][0]),
                                                   "".join(deleted[i][0]))
            deleted_lines.append(deleted_diff)

            prefix, highlighted, suffix = inserted_diff
            if prefix:
                write(prefix, label='diff.inserted')
            if highlighted:
                write(highlighted, label='diff.inserted_highlight')
            if suffix:
                write(suffix, label='diff.inserted')

            write("\n")

        for prefix, highlighted, suffix in deleted_lines:
            if prefix:
                write(prefix, label='diff.deleted')
            if highlighted:
                write(highlighted, label='diff.deleted_highlight')
            if suffix:
                write(suffix, label='diff.deleted')

            write("\n")

    def flush(self):
        self.flush_hunk()
        super(colorui, self).flush()

def worddiff(inserted, deleted):
    # find common prefix
    prefix = 0
    if inserted[0] == '+' and deleted[0] == '-':
        prefix = 1

    while prefix < len(inserted) and prefix < len(deleted):
        if inserted[prefix] == deleted[prefix]:
            prefix += 1
        else:
            break

    # find common suffix
    suffix = 0
    while prefix + suffix < len(inserted) and prefix + suffix < len(deleted):
        if inserted[-suffix - 1] == deleted[-suffix - 1]:
            suffix += 1
        else:
            break
    suffix_of_inserted = len(inserted) - suffix
    suffix_of_deleted = len(deleted) - suffix

    ret = []
    if prefix <= 1 and suffix == 0:
        # two lines are different in whole
        ret.append((inserted, '', ''))
        ret.append((deleted, '', ''))
    else:
        if prefix == suffix_of_inserted:  # nothing newly inserted
            ret.append((inserted, '', ''))
        else:
            ret.append((inserted[0:prefix],
                        inserted[prefix:suffix_of_inserted],
                        inserted[suffix_of_inserted:]))

        if prefix == suffix_of_deleted:  # nothing newly deleted
            ret.append((deleted, '', ''))
        else:
            ret.append((deleted[0:prefix],
                        deleted[prefix:suffix_of_deleted],
                        deleted[suffix_of_deleted:]))

    return ret

def uisetup(ui):
    if ui.plain():
        return
    if not isinstance(ui, colorui):
        colorui.__bases__ = (ui.__class__,)
        ui.__class__ = colorui
<commit_msg>Add license header: Apache-2.0<commit_after># -*- coding: utf-8 -*-
#  Copyright 2013 Takeshi KOMIYA
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

from hgext import color

# define new style
color._styles['diff.inserted_highlight'] = 'green_background'
color._styles['diff.deleted_highlight'] = 'red_background'

class colorui(color.colorui):
    changes = None

    def __init__(self, src=None):
        super(colorui, self).__init__(src)
        self.changes = []

    def write(self, *args, **opts):
        label = opts.get('label')
        if label in ('diff.inserted', 'diff.deleted'):
            self.changes.append((args, opts))
        elif label == 'diff.trailingwhitespace':  # merge to hunk
            change = self.changes.pop()
            self.changes.append((change[0] + args, change[1]))
        elif label == '' and args == ("\n",) and self.changes:
            self.changes.append((args, opts))
        else:
            self.flush_hunk()
            super(colorui, self).write(*args, **opts)

    def flush_hunk(self):
        if self.changes is None:  # not initialized yet
            return

        inserted = [change for change in self.changes
                    if change[1]['label'] == 'diff.inserted']
        deleted = [change for change in self.changes
                   if change[1]['label'] == 'diff.deleted']

        if len(inserted) == 0 or len(deleted) == 0:
            # it's new line or deleted line
            pass
        elif len(inserted) != len(deleted):
            # do not markup word-highlights if numbers of lines are mismatched
            pass
        else:
            self.rearrange_highlight(inserted, deleted)

        # flush hunk
        for change in self.changes:
            super(colorui, self).write(*change[0], **change[1])

        self.changes = []

    def rearrange_highlight(self, inserted, deleted):
        self.changes = []
        deleted_lines = []
        write = super(colorui, self).write

        for i in range(len(inserted)):
            inserted_diff, deleted_diff = worddiff("".join(inserted[i][0]),
                                                   "".join(deleted[i][0]))
            deleted_lines.append(deleted_diff)

            prefix, highlighted, suffix = inserted_diff
            if prefix:
                write(prefix, label='diff.inserted')
            if highlighted:
                write(highlighted, label='diff.inserted_highlight')
            if suffix:
                write(suffix, label='diff.inserted')

            write("\n")

        for prefix, highlighted, suffix in deleted_lines:
            if prefix:
                write(prefix, label='diff.deleted')
            if highlighted:
                write(highlighted, label='diff.deleted_highlight')
            if suffix:
                write(suffix, label='diff.deleted')

            write("\n")

    def flush(self):
        self.flush_hunk()
        super(colorui, self).flush()

def worddiff(inserted, deleted):
    # find common prefix
    prefix = 0
    if inserted[0] == '+' and deleted[0] == '-':
        prefix = 1

    while prefix < len(inserted) and prefix < len(deleted):
        if inserted[prefix] == deleted[prefix]:
            prefix += 1
        else:
            break

    # find common suffix
    suffix = 0
    while prefix + suffix < len(inserted) and prefix + suffix < len(deleted):
        if inserted[-suffix - 1] == deleted[-suffix - 1]:
            suffix += 1
        else:
            break
    suffix_of_inserted = len(inserted) - suffix
    suffix_of_deleted = len(deleted) - suffix

    ret = []
    if prefix <= 1 and suffix == 0:
        # two lines are different in whole
        ret.append((inserted, '', ''))
        ret.append((deleted, '', ''))
    else:
        if prefix == suffix_of_inserted:  # nothing newly inserted
            ret.append((inserted, '', ''))
        else:
            ret.append((inserted[0:prefix],
                        inserted[prefix:suffix_of_inserted],
                        inserted[suffix_of_inserted:]))

        if prefix == suffix_of_deleted:  # nothing newly deleted
            ret.append((deleted, '', ''))
        else:
            ret.append((deleted[0:prefix],
                        deleted[prefix:suffix_of_deleted],
                        deleted[suffix_of_deleted:]))

    return ret

def uisetup(ui):
    if ui.plain():
        return
    if not isinstance(ui, colorui):
        colorui.__bases__ = (ui.__class__,)
        ui.__class__ = colorui
<|endoftext|>


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

# version is used by serialization below so we need it before everything
__version__ = '0.12.0'

from dimod.constrained import *
import dimod.constrained

from dimod.core import *
import dimod.core

from dimod.cyutilities import *

from dimod.reference import *
import dimod.reference

from dimod.roof_duality import fix_variables

from dimod.binary import *
import dimod.binary

from dimod.discrete import *

import dimod.testing

from dimod.converters import *

import dimod.decorators

import dimod.generators

from dimod.exceptions import *
import dimod.exceptions

from dimod.higherorder import make_quadratic, make_quadratic_cqm, reduce_binary_polynomial, poly_energy, poly_energies, BinaryPolynomial
import dimod.higherorder

from dimod.package_info import __version__, __author__, __authoremail__, __description__

from dimod.quadratic import *
import dimod.quadratic

from dimod.traversal import *

from dimod.sampleset import *

from dimod.serialization.format import set_printoptions

import dimod.lp

from dimod.utilities import *
import dimod.utilities

from dimod.vartypes import *

# flags for some global features
REAL_INTERACTIONS = False
<commit_msg>Update version 0.12.0 -> 0.12.1<commit_after># Copyright 2018 D-Wave Systems Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

# version is used by serialization below so we need it before everything
__version__ = '0.12.1'

from dimod.constrained import *
import dimod.constrained

from dimod.core import *
import dimod.core

from dimod.cyutilities import *

from dimod.reference import *
import dimod.reference

from dimod.roof_duality import fix_variables

from dimod.binary import *
import dimod.binary

from dimod.discrete import *

import dimod.testing

from dimod.converters import *

import dimod.decorators

import dimod.generators

from dimod.exceptions import *
import dimod.exceptions

from dimod.higherorder import make_quadratic, make_quadratic_cqm, reduce_binary_polynomial, poly_energy, poly_energies, BinaryPolynomial
import dimod.higherorder

from dimod.package_info import __version__, __author__, __authoremail__, __description__

from dimod.quadratic import *
import dimod.quadratic

from dimod.traversal import *

from dimod.sampleset import *

from dimod.serialization.format import set_printoptions

import dimod.lp

from dimod.utilities import *
import dimod.utilities

from dimod.vartypes import *

# flags for some global features
REAL_INTERACTIONS = False
<|endoftext|>


<commit_before>"""
Vartype
--------

Vartype is an enumeration of the valid types of binary variables
for binary quadratic models.

Examples:
    >>> vartype = pm.Vartype.SPIN
    >>> print(vartype)
    Vartype.SPIN
    >>> isinstance(vartype, pm.Vartype)
    True

    Access can also be by value or name.

    >>> print(pm.Vartype({0, 1}))
    Vartype.BINARY
    >>> print(pm.Vartype['SPIN'])
    Vartype.SPIN

    To check correctness, use the `.value` parameter.

    >>> sample = {'u': -1, 'v': 1}
    >>> vartype = pm.Vartype.SPIN
    >>> all(val in vartype.value for val in sample.values())
    True

    The different Vartypes are also in the main namespace
    for easy access.

    >>> vartype = pm.SPIN
    >>> print(vartype)
    Vartype.SPIN
    >>> vartype = pm.BINARY
    >>> print(vartype)
    Vartype.BINARY

"""
import enum

__all__ = ['Vartype', 'SPIN', 'BINARY']

class Vartype(enum.Enum):
    """An :py:class:`~enum.Enum` over the types of variables for the binary quadratic model.

    Attributes:
        SPIN (:class:`.Vartype`): The vartype for spin-valued models. That
            is the variables of the model are either -1 or 1.
        BINARY (:class:`.Vartype`): The vartype for binary models. That is
            the variables of the model are either 0 or 1.

    """
    SPIN = frozenset({-1, 1})
    BINARY = frozenset({0, 1})

SPIN = Vartype.SPIN
BINARY = Vartype.BINARY
<commit_msg>Update module docstring<commit_after>"""
Vartype
--------

Vartype is an enumeration of the valid types of binary variables
for binary quadratic models.

Examples:
    >>> vartype = dimod.Vartype.SPIN
    >>> print(vartype)
    Vartype.SPIN
    >>> isinstance(vartype, dimod.Vartype)
    True

    Access can also be by value or name.

    >>> print(dimod.Vartype({0, 1}))
    Vartype.BINARY
    >>> print(dimod.Vartype['SPIN'])
    Vartype.SPIN

    To check correctness, use the `.value` parameter.

    >>> sample = {'u': -1, 'v': 1}
    >>> vartype = dimod.Vartype.SPIN
    >>> all(val in vartype.value for val in sample.values())
    True

    The different Vartypes are also in the main namespace
    for easy access.

    >>> vartype = dimod.SPIN
    >>> print(vartype)
    Vartype.SPIN
    >>> vartype = dimod.BINARY
    >>> print(vartype)
    Vartype.BINARY

"""
import enum

__all__ = ['Vartype', 'SPIN', 'BINARY']

class Vartype(enum.Enum):
    """An :py:class:`~enum.Enum` over the types of variables for the binary quadratic model.

    Attributes:
        SPIN (:class:`.Vartype`): The vartype for spin-valued models. That
            is the variables of the model are either -1 or 1.
        BINARY (:class:`.Vartype`): The vartype for binary models. That is
            the variables of the model are either 0 or 1.

    """
    SPIN = frozenset({-1, 1})
    BINARY = frozenset({0, 1})

SPIN = Vartype.SPIN
BINARY = Vartype.BINARY
<|endoftext|>