<commit_before># ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2006-2008 Alex Holkner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of pyglet nor the names of its
#    contributors may be used to endorse or promote products
#    derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

'''
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id: pil.py 163 2006-11-13 04:15:46Z Alex.Holkner $'

from ctypes import *

from pyglet.gl import *
from pyglet.image import *
from pyglet.image.codecs import *
from pyglet.window.win32.constants import *
from pyglet.window.win32.types import *

ole32 = windll.ole32
kernel32 = windll.kernel32
gdiplus = windll.gdiplus
mapi32 = windll.mapi32

LPSTREAM = c_void_p
REAL = c_float

PixelFormat1bppIndexed    = 196865
PixelFormat4bppIndexed    = 197634
PixelFormat8bppIndexed    = 198659
PixelFormat16bppGrayScale = 1052676
PixelFormat16bppRGB555    = 135173
PixelFormat16bppRGB565    = 135174
PixelFormat16bppARGB1555  = 397319
PixelFormat24bppRGB       = 137224
PixelFormat32bppRGB       = 139273
PixelFormat32bppARGB      = 2498570
PixelFormat32bppPARGB     = 925707
PixelFormat48bppRGB       = 1060876
PixelFormat64bppARGB      = 3424269
PixelFormat64bppPARGB     = 29622286
PixelFormatMax            = 15

ImageLockModeRead = 1
ImageLockModeWrite = 2
ImageLockModeUserInputBuf = 4

class GdiplusStartupInput(Structure):
    _fields_ = [
        ('GdiplusVersion', c_uint32),
        ('DebugEventCallback', c_void_p),
        ('SuppressBackgroundThread', BOOL),
        ('SuppressExternalCodecs', BOOL)
    ]

class GdiplusStartupOutput(Structure):
    _fields = [
        ('NotificationHookProc', c_void_p),
        ('NotificationUnhookProc', c_void_p)
    ]

class BitmapData(Structure):
    _fields_ = [
        ('Width', c_uint),
        ('Height', c_uint),
        ('Stride', c_int),
        ('PixelFormat', c_int),
        ('Scan0', POINTER(c_byte)),
        ('Reserved', POINTER(c_uint))
    ]

class Rect(Structure):
    _fields_ = [
        ('X', c_int),
        ('Y', c_int),
        ('Width', c_int),
        ('Height', c_int)
    ]

kernel32.GlobalAlloc.restype = HGLOBAL
kernel32.GlobalLock.restype = c_void_p

PropertyTagFrameDelay = 0x5100

class PropertyItem(Structure):
    _fields_ = [
        ('id', c_uint),
        ('length', c_ulong),
        ('type', c_short),
        ('value', c_void_p)
    ]

class GDIPlusDecoder(ImageDecoder):
    def get_file_extensions(self):
        return ['.bmp', '.gif', '.jpg', '.jpeg', '.exif', '.png', '.tif',
                '.tiff']

    def get_animation_file_extensions(self):
        # TIFF also supported as a multi-page image; but that's not really an
        # animation, is it?
        return ['.gif']

    def _load_bitmap(self, file):
        data = file.read()

        # Create a HGLOBAL with image data
        hglob = kernel32.GlobalAlloc(GMEM_MOVEABLE, len(data))
        ptr = kernel32.GlobalLock(hglob)
        memmove(ptr, data, len(data))
        kernel32.GlobalUnlock(hglob)

        # Create IStream for the HGLOBAL
        stream = LPSTREAM()
        ole32.CreateStreamOnHGlobal(hglob, True, byref(stream))

        # Load image from stream
        bitmap = c_void_p()
        status = gdiplus.GdipCreateBitmapFromStream(stream, byref(bitmap))
        if status != 0:
            # TODO release stream
            raise ImageDecodeException(
                'GDI+ cannot load %r' % (filename or file))

        return bitmap

    def _get_image(self, bitmap):
        # Get size of image (Bitmap subclasses Image)
        width = REAL()
        height = REAL()
        gdiplus.GdipGetImageDimension(bitmap, byref(width), byref(height))
        width = int(width.value)
        height = int(height.value)

        # Get image pixel format
        pf = c_int()
        gdiplus.GdipGetImagePixelFormat(bitmap, byref(pf))
        pf = pf.value

        # Reverse from what's documented because of Intel little-endianness.
        format = 'BGRA'
        if pf == PixelFormat24bppRGB:
            format = 'BGR'
        elif pf == PixelFormat32bppRGB:
            pass
        elif pf == PixelFormat32bppARGB:
            pass
        elif pf in (PixelFormat16bppARGB1555, PixelFormat32bppPARGB,
                    PixelFormat64bppARGB, PixelFormat64bppPARGB):
            pf = PixelFormat32bppARGB
        else:
            format = 'BGR'
            pf = PixelFormat24bppRGB

        # Lock pixel data in best format
        rect = Rect()
        rect.X = 0
        rect.Y = 0
        rect.Width = width
        rect.Height = height
        bitmap_data = BitmapData()
        gdiplus.GdipBitmapLockBits(bitmap,
            byref(rect), ImageLockModeRead, pf, byref(bitmap_data))

        # Create buffer for RawImage
        buffer = create_string_buffer(bitmap_data.Stride * height)
        memmove(buffer, bitmap_data.Scan0, len(buffer))

        # Unlock data
        gdiplus.GdipBitmapUnlockBits(bitmap, byref(bitmap_data))

        return ImageData(width, height, format, buffer, -bitmap_data.Stride)

    def _delete_bitmap(self, bitmap):
        # Release image and stream
        gdiplus.GdipDisposeImage(bitmap)
        # TODO: How to call IUnknown::Release on stream?

    def decode(self, file, filename):
        bitmap = self._load_bitmap(file)
        image = self._get_image(bitmap)
        self._delete_bitmap(bitmap)
        return image

    def decode_animation(self, file, filename):
        bitmap = self._load_bitmap(file)

        dimension_count = c_uint()
        gdiplus.GdipImageGetFrameDimensionsCount(bitmap, byref(dimension_count))
        if dimension_count.value < 1:
            self._delete_bitmap(bitmap)
            raise ImageDecodeException('Image has no frame dimensions')

        # XXX Make sure this dimension is time?
        dimensions = (c_void_p * dimension_count.value)()
        gdiplus.GdipImageGetFrameDimensionsList(bitmap, dimensions,
                                                dimension_count.value)

        frame_count = c_uint()
        gdiplus.GdipImageGetFrameCount(bitmap, dimensions, byref(frame_count))

        prop_id = PropertyTagFrameDelay
        prop_size = c_uint()
        gdiplus.GdipGetPropertyItemSize(bitmap, prop_id, byref(prop_size))

        prop_buffer = c_buffer(prop_size.value)
        prop_item = cast(prop_buffer, POINTER(PropertyItem)).contents
        gdiplus.GdipGetPropertyItem(bitmap, prop_id, prop_size.value,
            prop_buffer)

        # XXX Sure it's long?
        n_delays = prop_item.length / sizeof(c_long)
        delays = cast(prop_item.value, POINTER(c_long * n_delays)).contents

        animation = Animation([])

        for i in range(frame_count.value):
            gdiplus.GdipImageSelectActiveFrame(bitmap, dimensions, i)
            image = self._get_image(bitmap)

            delay = delays[i] / 100.
            animation.frames.append(AnimationFrame(image, delay))

        self._delete_bitmap(bitmap)

        return animation

def get_decoders():
    return [GDIPlusDecoder()]

def get_encoders():
    return []

def init():
    token = c_ulong()
    startup_in = GdiplusStartupInput()
    startup_in.GdiplusVersion = 1
    startup_out = GdiplusStartupOutput()
    gdiplus.GdiplusStartup(byref(token), byref(startup_in), byref(startup_out))

    # Shutdown later?
    # gdiplus.GdiplusShutdown(token)

init()
<commit_msg>Issue 232: remove mapi32 from windows dlls.<commit_after># ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2006-2008 Alex Holkner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of pyglet nor the names of its
#    contributors may be used to endorse or promote products
#    derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

'''
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id: pil.py 163 2006-11-13 04:15:46Z Alex.Holkner $'

from ctypes import *

from pyglet.gl import *
from pyglet.image import *
from pyglet.image.codecs import *
from pyglet.window.win32.constants import *
from pyglet.window.win32.types import *

ole32 = windll.ole32
kernel32 = windll.kernel32
gdiplus = windll.gdiplus

LPSTREAM = c_void_p
REAL = c_float

PixelFormat1bppIndexed    = 196865
PixelFormat4bppIndexed    = 197634
PixelFormat8bppIndexed    = 198659
PixelFormat16bppGrayScale = 1052676
PixelFormat16bppRGB555    = 135173
PixelFormat16bppRGB565    = 135174
PixelFormat16bppARGB1555  = 397319
PixelFormat24bppRGB       = 137224
PixelFormat32bppRGB       = 139273
PixelFormat32bppARGB      = 2498570
PixelFormat32bppPARGB     = 925707
PixelFormat48bppRGB       = 1060876
PixelFormat64bppARGB      = 3424269
PixelFormat64bppPARGB     = 29622286
PixelFormatMax            = 15

ImageLockModeRead = 1
ImageLockModeWrite = 2
ImageLockModeUserInputBuf = 4

class GdiplusStartupInput(Structure):
    _fields_ = [
        ('GdiplusVersion', c_uint32),
        ('DebugEventCallback', c_void_p),
        ('SuppressBackgroundThread', BOOL),
        ('SuppressExternalCodecs', BOOL)
    ]

class GdiplusStartupOutput(Structure):
    _fields = [
        ('NotificationHookProc', c_void_p),
        ('NotificationUnhookProc', c_void_p)
    ]

class BitmapData(Structure):
    _fields_ = [
        ('Width', c_uint),
        ('Height', c_uint),
        ('Stride', c_int),
        ('PixelFormat', c_int),
        ('Scan0', POINTER(c_byte)),
        ('Reserved', POINTER(c_uint))
    ]

class Rect(Structure):
    _fields_ = [
        ('X', c_int),
        ('Y', c_int),
        ('Width', c_int),
        ('Height', c_int)
    ]

kernel32.GlobalAlloc.restype = HGLOBAL
kernel32.GlobalLock.restype = c_void_p

PropertyTagFrameDelay = 0x5100

class PropertyItem(Structure):
    _fields_ = [
        ('id', c_uint),
        ('length', c_ulong),
        ('type', c_short),
        ('value', c_void_p)
    ]

class GDIPlusDecoder(ImageDecoder):
    def get_file_extensions(self):
        return ['.bmp', '.gif', '.jpg', '.jpeg', '.exif', '.png', '.tif',
                '.tiff']

    def get_animation_file_extensions(self):
        # TIFF also supported as a multi-page image; but that's not really an
        # animation, is it?
        return ['.gif']

    def _load_bitmap(self, file):
        data = file.read()

        # Create a HGLOBAL with image data
        hglob = kernel32.GlobalAlloc(GMEM_MOVEABLE, len(data))
        ptr = kernel32.GlobalLock(hglob)
        memmove(ptr, data, len(data))
        kernel32.GlobalUnlock(hglob)

        # Create IStream for the HGLOBAL
        stream = LPSTREAM()
        ole32.CreateStreamOnHGlobal(hglob, True, byref(stream))

        # Load image from stream
        bitmap = c_void_p()
        status = gdiplus.GdipCreateBitmapFromStream(stream, byref(bitmap))
        if status != 0:
            # TODO release stream
            raise ImageDecodeException(
                'GDI+ cannot load %r' % (filename or file))

        return bitmap

    def _get_image(self, bitmap):
        # Get size of image (Bitmap subclasses Image)
        width = REAL()
        height = REAL()
        gdiplus.GdipGetImageDimension(bitmap, byref(width), byref(height))
        width = int(width.value)
        height = int(height.value)

        # Get image pixel format
        pf = c_int()
        gdiplus.GdipGetImagePixelFormat(bitmap, byref(pf))
        pf = pf.value

        # Reverse from what's documented because of Intel little-endianness.
        format = 'BGRA'
        if pf == PixelFormat24bppRGB:
            format = 'BGR'
        elif pf == PixelFormat32bppRGB:
            pass
        elif pf == PixelFormat32bppARGB:
            pass
        elif pf in (PixelFormat16bppARGB1555, PixelFormat32bppPARGB,
                    PixelFormat64bppARGB, PixelFormat64bppPARGB):
            pf = PixelFormat32bppARGB
        else:
            format = 'BGR'
            pf = PixelFormat24bppRGB

        # Lock pixel data in best format
        rect = Rect()
        rect.X = 0
        rect.Y = 0
        rect.Width = width
        rect.Height = height
        bitmap_data = BitmapData()
        gdiplus.GdipBitmapLockBits(bitmap,
            byref(rect), ImageLockModeRead, pf, byref(bitmap_data))

        # Create buffer for RawImage
        buffer = create_string_buffer(bitmap_data.Stride * height)
        memmove(buffer, bitmap_data.Scan0, len(buffer))

        # Unlock data
        gdiplus.GdipBitmapUnlockBits(bitmap, byref(bitmap_data))

        return ImageData(width, height, format, buffer, -bitmap_data.Stride)

    def _delete_bitmap(self, bitmap):
        # Release image and stream
        gdiplus.GdipDisposeImage(bitmap)
        # TODO: How to call IUnknown::Release on stream?

    def decode(self, file, filename):
        bitmap = self._load_bitmap(file)
        image = self._get_image(bitmap)
        self._delete_bitmap(bitmap)
        return image

    def decode_animation(self, file, filename):
        bitmap = self._load_bitmap(file)

        dimension_count = c_uint()
        gdiplus.GdipImageGetFrameDimensionsCount(bitmap, byref(dimension_count))
        if dimension_count.value < 1:
            self._delete_bitmap(bitmap)
            raise ImageDecodeException('Image has no frame dimensions')

        # XXX Make sure this dimension is time?
        dimensions = (c_void_p * dimension_count.value)()
        gdiplus.GdipImageGetFrameDimensionsList(bitmap, dimensions,
                                                dimension_count.value)

        frame_count = c_uint()
        gdiplus.GdipImageGetFrameCount(bitmap, dimensions, byref(frame_count))

        prop_id = PropertyTagFrameDelay
        prop_size = c_uint()
        gdiplus.GdipGetPropertyItemSize(bitmap, prop_id, byref(prop_size))

        prop_buffer = c_buffer(prop_size.value)
        prop_item = cast(prop_buffer, POINTER(PropertyItem)).contents
        gdiplus.GdipGetPropertyItem(bitmap, prop_id, prop_size.value,
            prop_buffer)

        # XXX Sure it's long?
        n_delays = prop_item.length / sizeof(c_long)
        delays = cast(prop_item.value, POINTER(c_long * n_delays)).contents

        animation = Animation([])

        for i in range(frame_count.value):
            gdiplus.GdipImageSelectActiveFrame(bitmap, dimensions, i)
            image = self._get_image(bitmap)

            delay = delays[i] / 100.
            animation.frames.append(AnimationFrame(image, delay))

        self._delete_bitmap(bitmap)

        return animation

def get_decoders():
    return [GDIPlusDecoder()]

def get_encoders():
    return []

def init():
    token = c_ulong()
    startup_in = GdiplusStartupInput()
    startup_in.GdiplusVersion = 1
    startup_out = GdiplusStartupOutput()
    gdiplus.GdiplusStartup(byref(token), byref(startup_in), byref(startup_out))

    # Shutdown later?
    # gdiplus.GdiplusShutdown(token)

init()
<|endoftext|>


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

from __future__ import unicode_literals
from behave import step
from dogtail.tree import root
from dogtail.rawinput import typeText, pressKey, keyCombo
from dogtail.utils import GnomeShell
from time import sleep
from common_steps import wait_until
from subprocess import call, check_output, Popen, CalledProcessError, PIPE
import re
import libvirt
import libxml2
from utils import get_showing_node_name

TIMER = 0.2

@step('About is shown')
def about_shown(context):
    assert context.app.child('About Boxes') != None, "About window cannot be focused"

@step('Box "{name}" "{state}" exist')
def does_box_exists(context, name, state):
    found = False
    pane = context.app.child(roleName='layered pane')
    counter = 0
    # on slower machines more attempts may be needed
    while counter < 5:
        for child in pane.children:
            if child.text == name:
                found = True
                break
        if found:
            break
        sleep(5*TIMER)
        counter += 1

    if state == 'does':
        assert found == True, "Machine %s was not found in overview" % name
    if state == 'does not':
        assert found == False, "Machine %s was found in overview" % name

@step('Boxes are not running')
def boxes_not_running(context):
    assert context.app_class.isRunning() != True, "Boxes window still visible"

@step('Boxes app has "{num}" windows')
def number_of_windows(context, num):
    assert len(context.app.children) == int(num), "App has just %s windows not %s" %(len(context.app.children), num)

@step('Customize mem to "{mem}" MB')
def customize_vm(context, mem):
    sleep(1)
    context.app.child('Customize…').click()
    sleep(TIMER)
    pressKey('Tab')
    pressKey('Tab')
    memory_label = context.app.findChildren(lambda x: x.name == 'Memory: ' and x.showing)[0]
    mem = mem+" MiB"
    counter = 0
    while not memory_label.parent.findChildren(lambda x: x.name == mem and x.showing):
        pressKey('Left')
        counter += 1
        if counter == 100:
            break
    context.app.findChildren(lambda x: x.name == 'Back' and x.showing)[0].click()
    sleep(TIMER)

@step('Focus VM')
def focus_vm(context):
    drawing_area = None
    drawing_area = context.app.findChildren(lambda x: x.roleName == 'drawing area' and x.showing)
    if drawing_area:
        drawing_area[0].click()

@step('Go into "{vm}" box')
def go_into_vm(context, vm):
    pane = context.app.child(roleName='layered pane')
    for child in pane.children:
        if child.text == vm:
            child.click()
            sleep(3*TIMER)
            break

@step('Help is shown')
def help_shown(context):
    sleep(1)
    yelp = root.application('yelp').child('Boxes')
    call("pkill -9 yelp", shell=True)
    sleep(2)
    assert yelp != None, "Yelp wasn't opened"

@step('Install TC Linux package "{pkg}" and wait "{time}" seconds')
def install_tc_linux_package(context, pkg, time):
    if "/" in pkg:
        call("xdotool type --delay 150 'wget %s'" %pkg, shell=True)
        call("xdotool key 'Return'", shell=True)
        call("xdotool type --delay 150 'tce-load -i %s'" %pkg.split('/')[-1], shell=True)
        call("xdotool key 'Return'", shell=True)
    else:
        call("xdotool type --delay 150 'tce-load -wi %s'" %pkg, shell=True)
        call("xdotool key 'Return'", shell=True)

    sleep(int(time))

@step('No box is visible')
def no_box_sign(context):
    assert context.app.child('Just hit the New button to create your first one.') != None

@step('Press "{action}" in "{vm}" vm')
def press_back_in_vm(context, action, vm):
    panel = context.app.child(vm).children[0].findChildren(lambda x: x.roleName == 'panel' and x.showing)[0]
    buttons = panel.findChildren(lambda x: x.roleName == 'push button' and x.showing)
    if action == 'back':
        get_showing_node_name('Back', context.app).click()
    elif action == 'prefs':
        buttons[1].click()
    elif action == "Send key combinations":
        panel.child('Keyboard shortcuts').click()
    else:
        context.app.child(vm).child(action).click()
    sleep(TIMER)

@step('Launch "{action}" for "{box}" box')
def launch_action_for_box(context, action, box):
    pane = context.app.child(roleName='layered pane')
    item = pane.findChildren(lambda x: x.text == box and x.roleName == 'icon')[0]
    item.click(button=3)
    popup = context.app.findChildren(lambda x: x.name == 'Box actions' and x.roleName == 'popup menu' and x.showing)[0]
    popup.child(action).click()
    sleep(TIMER)

@step('Press "{action}" in alert')
def press_back_in_prefs(context, action):
    button = context.app.child(roleName='alert').child(action)
    button.click()
    sleep(TIMER)

@step('Quit Boxes')
def quit_boxes(context):
    keyCombo('<Ctrl><Q>')
    counter = 0
    while call('pidof gnome-boxes > /dev/null', shell=True) != 1:
        sleep(TIMER)
        counter += 1
        if counter == 100:
            raise Exception("Failed to turn off Boxes in 50 seconds")

@step('Rename "{machine}" to "{name}" via "{way}"')
def rename_vm(context, machine, name, way):
    if way == 'button':
        context.app.child(machine, roleName='push button').click()
        sleep(TIMER)
    if way == 'label':
        context.app.child('General').child('Name').parent.child(roleName='text').click()
        keyCombo('<Ctrl><a>')
    typeText(name)
    pressKey('Enter')
    sleep(TIMER)

def libvirt_domain_get_context(dom):
    xmldesc = dom.XMLDesc(0)
    doc = libxml2.parseDoc(xmldesc)
    return doc.xpathNewContext()

def libvirt_domain_get_val(ctx, path):
    res = ctx.xpathEval(path)
    if res is None or len(res) == 0:
        value="Unknown"
    else:
        value = res[0].content
    return value

def libvirt_domain_get_mac(vm_title):
    mac = None

    conn = libvirt.openReadOnly(None)
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/title") == vm_title:
            return libvirt_domain_get_val(ctx, "/domain/devices/interface/mac/@address")

    conn = libvirt.openReadOnly('qemu:///system')
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/name") == vm_title:
            return libvirt_domain_get_val(ctx, "/domain/devices/interface/mac/@address")
    return mac

def get_ip_from_ip_neigh_cmd(mac, reachable):
    out = ""
    wait = 0
    while wait < 120:
        if reachable:
            cmd = Popen("ip neighbour show dev virbr0 nud reachable\
                            |grep %s\
                            |tail -n 1\
                            |grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
                            %mac, shell=True, stdout=PIPE)
        else:
            cmd = Popen("ip neighbour show dev virbr0\
                            |grep %s\
                            |tail -n 1\
                            |grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
                            %mac, shell=True, stdout=PIPE)

        out = cmd.communicate()[0]
        ret = cmd.wait()
        if out != "":
            return out.strip()
        sleep(TIMER*3)
        wait += 1
    return None

@step('Save reachable IP for machine "{vm}"')
def ip_for_reachable_vm(context, vm):
    save_ip_for_vm(context, vm, reachable=True)

@step('Save IP for machine "{vm}"')
def ip_for_vm(context, vm):
    save_ip_for_vm(context, vm)

def save_ip_for_vm(context, vm, reachable=False):
    if not hasattr(context, 'ips'):
        context.ips = {}

    ip = get_ip_from_ip_neigh_cmd(libvirt_domain_get_mac(vm), reachable)

    if not ip:
        raise Exception("No address was assigned for this machine %s" %vm)

    count = 1
    for key in context.ips.keys():
        if key.find(vm) != -1:
            count += 1
    if count != 1:
        vm = vm + " %s" %count

    context.ips[vm] = ip

@step('Select "{vm}" box')
def select_vm(context, vm):
    select_button = context.app.child('Select Items')
    if select_button.showing:
        select_button.click()
    pane = context.app.child(roleName='layered pane')
    for child in pane.children:
        if child.text == vm:
            child.click()
            sleep(TIMER)
            break

@step('Select "{action}" from supermenu')
def select_menu_action(context, action):
    sleep(0.5)
    keyCombo("<Super_L><F10>")
    gs = GnomeShell()
    buttons = gs.getApplicationMenuList()
    sleep(0.5)
    for button in buttons:
        if button.name == action:
            button.click()
            break

@step('Start Boxes')
def start_boxes(context):
    cmd = 'gnome-boxes'
    Popen(cmd, shell=True)
    sleep(1)
    context.app = root.application('gnome-boxes')

@step('Start showkey signal recording')
def start_showkey_recording(context):
    # Have to sleep a bit before as otherwise release of return can be caught
    call("xdotool type --delay 100 'sleep 0.5; showkey > /tmp/showkey.txt'", shell=True)
    call("xdotool key 'Return'", shell=True)
    sleep(1)

@step('Verify previously recorded signals')
def verify_existing_showkey_signals(context):
    call("xdotool type --delay 100 'grep keycode /tmp/showkey.txt > /tmp/final.txt\n'", shell=True)
    # If all signals received as expected
    call("xdotool type --delay 100 'if [[ \"$(echo $(sed \"s/keycode//g\" /tmp/final.txt))\" == '", shell=True)
    call("xdotool type --delay 100 ' \"29 press 56 press 14 press 14 release 56 release 29 release'", shell=True)
    call("xdotool type --delay 100 ' 29 press 56 press 59 press 59 release 56 release 29 release'", shell=True)
    call("xdotool type --delay 100 ' 29 press 56 press 60 press 60 release 56 release 29 release'", shell=True)
    call("xdotool type --delay 100 ' 29 press 56 press 65 press 65 release 56 release 29 release\"'", shell=True)
    # Turn down network so observer from outside the VM can say pass/fail.
    call("xdotool type --delay 100 ' ]]; then sudo ifconfig eth0 down; fi\n'", shell=True)
    sleep(TIMER)

@step('Start box name "{box}"')
def start_boxes_via_vm(context, box):
    cmd = 'gnome-boxes %s' %box
    Popen(cmd, shell=True)
    sleep(10)
    context.app = root.application('gnome-boxes')

@step('Verify back button "{state}" visible for machine "{vm_name}"')
def verify_back_button_visibility(context, state, vm_name):
    if state == "is":
        main = context.app.children[0]
        core = context.app.findChildren(lambda x: x.name == vm_name)[-1]
        frame = core.findAncestor(predicate.GenericPredicate(name='Boxes', roleName='frame'))
        assert frame != main, "Cannot focus detached window"
        top_frame_panel = frame.children[0]
        main_window_panel = top_frame_panel.children[-1]
        back_button = main_window_panel.child(roleName='push button')
        assert back_button.showing == False, "Back button is visible but it shouldn't be"

@step('Wait until overview is loaded')
def initial_page_loaded(context):
    wait_until(lambda x: x.name != 'New', context.app)
<commit_msg> two more missing Return presses<commit_after># -*- coding: UTF-8 -*-

from __future__ import unicode_literals
from behave import step
from dogtail.tree import root
from dogtail.rawinput import typeText, pressKey, keyCombo
from dogtail.utils import GnomeShell
from time import sleep
from common_steps import wait_until
from subprocess import call, check_output, Popen, CalledProcessError, PIPE
import re
import libvirt
import libxml2
from utils import get_showing_node_name

TIMER = 0.2

@step('About is shown')
def about_shown(context):
    assert context.app.child('About Boxes') != None, "About window cannot be focused"

@step('Box "{name}" "{state}" exist')
def does_box_exists(context, name, state):
    found = False
    pane = context.app.child(roleName='layered pane')
    counter = 0
    # on slower machines more attempts may be needed
    while counter < 5:
        for child in pane.children:
            if child.text == name:
                found = True
                break
        if found:
            break
        sleep(5*TIMER)
        counter += 1

    if state == 'does':
        assert found == True, "Machine %s was not found in overview" % name
    if state == 'does not':
        assert found == False, "Machine %s was found in overview" % name

@step('Boxes are not running')
def boxes_not_running(context):
    assert context.app_class.isRunning() != True, "Boxes window still visible"

@step('Boxes app has "{num}" windows')
def number_of_windows(context, num):
    assert len(context.app.children) == int(num), "App has just %s windows not %s" %(len(context.app.children), num)

@step('Customize mem to "{mem}" MB')
def customize_vm(context, mem):
    sleep(1)
    context.app.child('Customize…').click()
    sleep(TIMER)
    pressKey('Tab')
    pressKey('Tab')
    memory_label = context.app.findChildren(lambda x: x.name == 'Memory: ' and x.showing)[0]
    mem = mem+" MiB"
    counter = 0
    while not memory_label.parent.findChildren(lambda x: x.name == mem and x.showing):
        pressKey('Left')
        counter += 1
        if counter == 100:
            break
    context.app.findChildren(lambda x: x.name == 'Back' and x.showing)[0].click()
    sleep(TIMER)

@step('Focus VM')
def focus_vm(context):
    drawing_area = None
    drawing_area = context.app.findChildren(lambda x: x.roleName == 'drawing area' and x.showing)
    if drawing_area:
        drawing_area[0].click()

@step('Go into "{vm}" box')
def go_into_vm(context, vm):
    pane = context.app.child(roleName='layered pane')
    for child in pane.children:
        if child.text == vm:
            child.click()
            sleep(3*TIMER)
            break

@step('Help is shown')
def help_shown(context):
    sleep(1)
    yelp = root.application('yelp').child('Boxes')
    call("pkill -9 yelp", shell=True)
    sleep(2)
    assert yelp != None, "Yelp wasn't opened"

@step('Install TC Linux package "{pkg}" and wait "{time}" seconds')
def install_tc_linux_package(context, pkg, time):
    if "/" in pkg:
        call("xdotool type --delay 150 'wget %s'" %pkg, shell=True)
        call("xdotool key 'Return'", shell=True)
        call("xdotool type --delay 150 'tce-load -i %s'" %pkg.split('/')[-1], shell=True)
        call("xdotool key 'Return'", shell=True)
    else:
        call("xdotool type --delay 150 'tce-load -wi %s'" %pkg, shell=True)
        call("xdotool key 'Return'", shell=True)

    sleep(int(time))

@step('No box is visible')
def no_box_sign(context):
    assert context.app.child('Just hit the New button to create your first one.') != None

@step('Press "{action}" in "{vm}" vm')
def press_back_in_vm(context, action, vm):
    panel = context.app.child(vm).children[0].findChildren(lambda x: x.roleName == 'panel' and x.showing)[0]
    buttons = panel.findChildren(lambda x: x.roleName == 'push button' and x.showing)
    if action == 'back':
        get_showing_node_name('Back', context.app).click()
    elif action == 'prefs':
        buttons[1].click()
    elif action == "Send key combinations":
        panel.child('Keyboard shortcuts').click()
    else:
        context.app.child(vm).child(action).click()
    sleep(TIMER)

@step('Launch "{action}" for "{box}" box')
def launch_action_for_box(context, action, box):
    pane = context.app.child(roleName='layered pane')
    item = pane.findChildren(lambda x: x.text == box and x.roleName == 'icon')[0]
    item.click(button=3)
    popup = context.app.findChildren(lambda x: x.name == 'Box actions' and x.roleName == 'popup menu' and x.showing)[0]
    popup.child(action).click()
    sleep(TIMER)

@step('Press "{action}" in alert')
def press_back_in_prefs(context, action):
    button = context.app.child(roleName='alert').child(action)
    button.click()
    sleep(TIMER)

@step('Quit Boxes')
def quit_boxes(context):
    keyCombo('<Ctrl><Q>')
    counter = 0
    while call('pidof gnome-boxes > /dev/null', shell=True) != 1:
        sleep(TIMER)
        counter += 1
        if counter == 100:
            raise Exception("Failed to turn off Boxes in 50 seconds")

@step('Rename "{machine}" to "{name}" via "{way}"')
def rename_vm(context, machine, name, way):
    if way == 'button':
        context.app.child(machine, roleName='push button').click()
        sleep(TIMER)
    if way == 'label':
        context.app.child('General').child('Name').parent.child(roleName='text').click()
        keyCombo('<Ctrl><a>')
    typeText(name)
    pressKey('Enter')
    sleep(TIMER)

def libvirt_domain_get_context(dom):
    xmldesc = dom.XMLDesc(0)
    doc = libxml2.parseDoc(xmldesc)
    return doc.xpathNewContext()

def libvirt_domain_get_val(ctx, path):
    res = ctx.xpathEval(path)
    if res is None or len(res) == 0:
        value="Unknown"
    else:
        value = res[0].content
    return value

def libvirt_domain_get_mac(vm_title):
    mac = None

    conn = libvirt.openReadOnly(None)
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/title") == vm_title:
            return libvirt_domain_get_val(ctx, "/domain/devices/interface/mac/@address")

    conn = libvirt.openReadOnly('qemu:///system')
    doms = conn.listAllDomains()
    for dom in doms:
        try:
            dom0 = conn.lookupByName(dom.name())
            # Annoyiingly, libvirt prints its own error message here
        except libvirt.libvirtError:
            print("Domain %s is not running" % name)
        ctx = libvirt_domain_get_context(dom0)
        if libvirt_domain_get_val(ctx, "/domain/name") == vm_title:
            return libvirt_domain_get_val(ctx, "/domain/devices/interface/mac/@address")
    return mac

def get_ip_from_ip_neigh_cmd(mac, reachable):
    out = ""
    wait = 0
    while wait < 120:
        if reachable:
            cmd = Popen("ip neighbour show dev virbr0 nud reachable\
                            |grep %s\
                            |tail -n 1\
                            |grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
                            %mac, shell=True, stdout=PIPE)
        else:
            cmd = Popen("ip neighbour show dev virbr0\
                            |grep %s\
                            |tail -n 1\
                            |grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
                            %mac, shell=True, stdout=PIPE)

        out = cmd.communicate()[0]
        ret = cmd.wait()
        if out != "":
            return out.strip()
        sleep(TIMER*3)
        wait += 1
    return None

@step('Save reachable IP for machine "{vm}"')
def ip_for_reachable_vm(context, vm):
    save_ip_for_vm(context, vm, reachable=True)

@step('Save IP for machine "{vm}"')
def ip_for_vm(context, vm):
    save_ip_for_vm(context, vm)

def save_ip_for_vm(context, vm, reachable=False):
    if not hasattr(context, 'ips'):
        context.ips = {}

    ip = get_ip_from_ip_neigh_cmd(libvirt_domain_get_mac(vm), reachable)

    if not ip:
        raise Exception("No address was assigned for this machine %s" %vm)

    count = 1
    for key in context.ips.keys():
        if key.find(vm) != -1:
            count += 1
    if count != 1:
        vm = vm + " %s" %count

    context.ips[vm] = ip

@step('Select "{vm}" box')
def select_vm(context, vm):
    select_button = context.app.child('Select Items')
    if select_button.showing:
        select_button.click()
    pane = context.app.child(roleName='layered pane')
    for child in pane.children:
        if child.text == vm:
            child.click()
            sleep(TIMER)
            break

@step('Select "{action}" from supermenu')
def select_menu_action(context, action):
    sleep(0.5)
    keyCombo("<Super_L><F10>")
    gs = GnomeShell()
    buttons = gs.getApplicationMenuList()
    sleep(0.5)
    for button in buttons:
        if button.name == action:
            button.click()
            break

@step('Start Boxes')
def start_boxes(context):
    cmd = 'gnome-boxes'
    Popen(cmd, shell=True)
    sleep(1)
    context.app = root.application('gnome-boxes')

@step('Start showkey signal recording')
def start_showkey_recording(context):
    # Have to sleep a bit before as otherwise release of return can be caught
    call("xdotool type --delay 100 'sleep 0.5; showkey > /tmp/showkey.txt'", shell=True)
    call("xdotool key 'Return'", shell=True)
    sleep(1)

@step('Verify previously recorded signals')
def verify_existing_showkey_signals(context):
    call("xdotool type --delay 100 'grep keycode /tmp/showkey.txt > /tmp/final.txt'", shell=True)
    call("xdotool key 'Return'", shell=True)
    # If all signals received as expected
    call("xdotool type --delay 100 'if [[ \"$(echo $(sed \"s/keycode//g\" /tmp/final.txt))\" == '", shell=True)
    call("xdotool type --delay 100 ' \"29 press 56 press 14 press 14 release 56 release 29 release'", shell=True)
    call("xdotool type --delay 100 ' 29 press 56 press 59 press 59 release 56 release 29 release'", shell=True)
    call("xdotool type --delay 100 ' 29 press 56 press 60 press 60 release 56 release 29 release'", shell=True)
    call("xdotool type --delay 100 ' 29 press 56 press 65 press 65 release 56 release 29 release\"'", shell=True)
    # Turn down network so observer from outside the VM can say pass/fail.
    call("xdotool type --delay 100 ' ]]; then sudo ifconfig eth0 down; fi'", shell=True)
    call("xdotool key 'Return'", shell=True)
    sleep(TIMER)

@step('Start box name "{box}"')
def start_boxes_via_vm(context, box):
    cmd = 'gnome-boxes %s' %box
    Popen(cmd, shell=True)
    sleep(10)
    context.app = root.application('gnome-boxes')

@step('Verify back button "{state}" visible for machine "{vm_name}"')
def verify_back_button_visibility(context, state, vm_name):
    if state == "is":
        main = context.app.children[0]
        core = context.app.findChildren(lambda x: x.name == vm_name)[-1]
        frame = core.findAncestor(predicate.GenericPredicate(name='Boxes', roleName='frame'))
        assert frame != main, "Cannot focus detached window"
        top_frame_panel = frame.children[0]
        main_window_panel = top_frame_panel.children[-1]
        back_button = main_window_panel.child(roleName='push button')
        assert back_button.showing == False, "Back button is visible but it shouldn't be"

@step('Wait until overview is loaded')
def initial_page_loaded(context):
    wait_until(lambda x: x.name != 'New', context.app)
<|endoftext|>


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

"""
Copyright 2017 EMBL-European Bioinformatics Institute

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.
"""

class fastqreader:

    def __init__(self):
        """
        Initialise the module and
        """
        self.fastq1 = ''
        self.fastq2 = ''

        self.f1_file = None
        self.f2_file = None

        self.f1_eof = False
        self.f2_eof = False

        self.f1_output_file = None
        self.f2_output_file = None

        self.output_tag = ''
        self.output_file_count = 0

    def openPairedFastQ(self, file1, file2):
        """
        Create file handles for reading the FastQ files
        """
        self.fastq1 = file1
        self.fastq2 = file2

        self.f1_file = open(self.fastq1, "r")
        self.f2_file = open(self.fastq2, "r")

        self.f1_eof = False
        self.f2_eof = False

    def closePairedFastQ(self):
        """
        Close file handles for the FastQ files.
        """
        self.f1_file.close()
        self.f2_file.close()

    def eof(self, side = 1):
        """
        Indicate if the end of the file has been reached
        """
        if side == 1:
            return self.f1_eof
        elif side == 2:
            return self.f2_eof
        else:
            return "ERROR"

    def next(self, side = 1):
        """
        Get the next read element for the specific FastQ file pair
        """
        read_id = ''
        read_seq = ''
        read_addition = ''
        read_score = ''

        if side == 1:
            read_id = self.f1_file.readline()
            read_seq = self.f1_file.readline()
            read_addition = self.f1_file.readline()
            read_score = self.f1_file.readline()
        elif side == 2:
            read_id = self.f2_file.readline()
            read_seq = self.f2_file.readline()
            read_addition = self.f2_file.readline()
            read_score = self.f2_file.readline()
        else:
            return 'ERROR'

        if read_id == '':
            if side == 1:
                self.f1_eof = True
                return False
            if side == 2:
                self.f2_eof = True
                return False
        return {'id': read_id.rstrip(), 'seq': read_seq.rstrip(), 'add': read_addition.rstrip(), 'score': read_score.rstrip()}

    def createOutputFiles(self, tag = ''):
        if tag != '' and self.output_tag != tag:
            self.output_tag = tag

        """
        Create and open the file handles for the output files
        """
        f1 = self.fastq1.split("/")
        f1[-1] = f1[-1].replace(".fastq", "." + str(self.output_tag) + "_" + str(self.output_file_count) + ".fastq")
        f1.insert(-1, "tmp")

        f2 = self.fastq2.split("/")
        f2[-1] = f2[-1].replace(".fastq", "." + str(self.output_tag) + "_" + str(self.output_file_count) + ".fastq")
        f2.insert(-1, "tmp")

        self.f1_output_file = open("/".join(f1), "w")
        self.f2_output_file = open("/".join(f2), "w")

    def writeOutput(self, read, side = 1):
        """
        Writer to print the extracted lines
        """
        line = read["id"] + "\n" + read["seq"] + "\n" + read["add"] + "\n" + read["score"] + "\n"
        if side == 1:
            self.f1_output_file.write(line)
        elif side == 2:
            self.f2_output_file.write(line)
        else:
            return False
        return True

    def closeOutputFiles(self):
        """
        Close the output file handles
        """
        self.f1_output_file.close()
        self.f2_output_file.close()

    def incrementOutputFiles(self):
        """
        Increment the counter and create new files for splitting the original
        FastQ paired end files.
        """
        self.closeOutputFiles()

        self.output_file_count+=1

        self.createOutputFiles(self.output_tag)
<commit_msg>Modifications so that it is able to handle paired and single ended FASTQ files<commit_after>#!/usr/bin/env python

"""
Copyright 2017 EMBL-European Bioinformatics Institute

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.
"""

class fastqreader:

    def __init__(self):
        """
        Initialise the module and
        """
        self.paired = False

        self.fastq1 = ''
        self.fastq2 = ''

        self.f1_file = None
        self.f2_file = None

        self.f1_eof = False
        self.f2_eof = False

        self.f1_output_file = None
        self.f2_output_file = None

        self.output_tag = ''
        self.output_file_count = 0

    def openFastQ(self, file1, file2 = None):
        """
        Create file handles for reading the FastQ files
        """
        self.fastq1 = file1
        self.f1_file = open(self.fastq1, "r")
        self.f1_eof = False

        if file2 is not None:
            self.fastq2 = file2
            self.f2_file = open(self.fastq2, "r")
            self.f2_eof = False
            self.paired = True

    def closeFastQ(self):
        """
        Close file handles for the FastQ files.
        """
        self.f1_file.close()

        if self.paired == True:
            self.f2_file.close()

    def eof(self, side = 1):
        """
        Indicate if the end of the file has been reached
        """
        if side == 1:
            return self.f1_eof
        elif side == 2:
            return self.f2_eof
        else:
            return "ERROR"

    def next(self, side = 1):
        """
        Get the next read element for the specific FastQ file pair
        """
        read_id = ''
        read_seq = ''
        read_addition = ''
        read_score = ''

        if side == 1:
            read_id = self.f1_file.readline()
            read_seq = self.f1_file.readline()
            read_addition = self.f1_file.readline()
            read_score = self.f1_file.readline()
        elif side == 2:
            read_id = self.f2_file.readline()
            read_seq = self.f2_file.readline()
            read_addition = self.f2_file.readline()
            read_score = self.f2_file.readline()
        else:
            return 'ERROR'

        if read_id == '':
            if side == 1:
                self.f1_eof = True
                return False
            if side == 2:
                self.f2_eof = True
                return False
        return {'id': read_id.rstrip(), 'seq': read_seq.rstrip(), 'add': read_addition.rstrip(), 'score': read_score.rstrip()}

    def createOutputFiles(self, tag = ''):
        if tag != '' and self.output_tag != tag:
            self.output_tag = tag

        """
        Create and open the file handles for the output files
        """
        f1 = self.fastq1.split("/")
        f1[-1] = f1[-1].replace(".fastq", "." + str(self.output_tag) + "_" + str(self.output_file_count) + ".fastq")
        f1.insert(-1, "tmp")
        self.f1_output_file = open("/".join(f1), "w")

        if self.paired == True:
            f2 = self.fastq2.split("/")
            f2[-1] = f2[-1].replace(".fastq", "." + str(self.output_tag) + "_" + str(self.output_file_count) + ".fastq")
            f2.insert(-1, "tmp")
            self.f2_output_file = open("/".join(f2), "w")

    def writeOutput(self, read, side = 1):
        """
        Writer to print the extracted lines
        """
        line = read["id"] + "\n" + read["seq"] + "\n" + read["add"] + "\n" + read["score"] + "\n"
        if side == 1:
            self.f1_output_file.write(line)
        elif side == 2:
            self.f2_output_file.write(line)
        else:
            return False
        return True

    def closeOutputFiles(self):
        """
        Close the output file handles
        """
        self.f1_output_file.close()

        if self.paired == True:
            self.f2_output_file.close()

    def incrementOutputFiles(self):
        """
        Increment the counter and create new files for splitting the original
        FastQ paired end files.
        """
        self.closeOutputFiles()

        self.output_file_count+=1

        self.createOutputFiles(self.output_tag)
<|endoftext|>


<commit_before>"""
Test that expr will time out and allow other threads to run if it blocks.
"""

import os, time
import re
import unittest2
import lldb, lldbutil
from lldbtest import *

class ExprDoesntDeadlockTestCase(TestBase):

    mydir = os.path.join("functionalities", "expr-doesnt-deadlock")

    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
    @dsym_test
    def test_with_dsym_and_run_command(self):
        """Test that expr will time out and allow other threads to run if it blocks - with dsym."""
        self.buildDsym()
        self.expr_doesnt_deadlock()

    @dwarf_test
    @expectedFailureLinux # due to bugzilla 14437
    def test_with_dwarf_and_run_command(self):
        """Test that expr will time out and allow other threads to run if it blocks."""
        self.buildDwarf()
        self.expr_doesnt_deadlock()

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)

    def expr_doesnt_deadlock (self):
        """Test that expr will time out and allow other threads to run if it blocks."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Now create a breakpoint at source line before call_me_to_get_lock gets called.

        main_file_spec = lldb.SBFileSpec ("locking.c")
        breakpoint = target.BreakpointCreateBySourceRegex('Break here', main_file_spec)
        if self.TraceOn():
            print "breakpoint:", breakpoint
        self.assertTrue(breakpoint and
                        breakpoint.GetNumLocations() == 1,
                        VALID_BREAKPOINT)

        # Now launch the process, and do not stop at entry point.
        process = target.LaunchSimple(None, None, os.getcwd())
        self.assertTrue(process, PROCESS_IS_VALID)

        # Frame #0 should be on self.line1 and the break condition should hold.
        from lldbutil import get_stopped_thread
        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")

        frame0 = thread.GetFrameAtIndex(0)

        var = frame0.EvaluateExpression ("call_me_to_get_lock()")
        self.assertTrue (var.IsValid())
        self.assertTrue (var.GetValueAsSigned (0) == 567)

if __name__ == '__main__':
    import atexit
    lldb.SBDebugger.Initialize()
    atexit.register(lambda: lldb.SBDebugger.Terminate())
    unittest2.main()
<commit_msg>Skip test that causes lldb to assert due to inferior getting a SIGSEGV with si_code == SI_KERNEL (PR-15258) - crash does not always reproduce<commit_after>"""
Test that expr will time out and allow other threads to run if it blocks.
"""

import os, time
import re
import unittest2
import lldb, lldbutil
from lldbtest import *

class ExprDoesntDeadlockTestCase(TestBase):

    mydir = os.path.join("functionalities", "expr-doesnt-deadlock")

    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
    @dsym_test
    def test_with_dsym_and_run_command(self):
        """Test that expr will time out and allow other threads to run if it blocks - with dsym."""
        self.buildDsym()
        self.expr_doesnt_deadlock()

    @dwarf_test
    @skipOnLinux # PR-15258: disabled due to assertion failure in ProcessMonitor::GetCrashReasonForSIGSEGV:
    def test_with_dwarf_and_run_command(self):
        """Test that expr will time out and allow other threads to run if it blocks."""
        self.buildDwarf()
        self.expr_doesnt_deadlock()

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)

    def expr_doesnt_deadlock (self):
        """Test that expr will time out and allow other threads to run if it blocks."""
        exe = os.path.join(os.getcwd(), "a.out")

        # Create a target by the debugger.
        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Now create a breakpoint at source line before call_me_to_get_lock gets called.

        main_file_spec = lldb.SBFileSpec ("locking.c")
        breakpoint = target.BreakpointCreateBySourceRegex('Break here', main_file_spec)
        if self.TraceOn():
            print "breakpoint:", breakpoint
        self.assertTrue(breakpoint and
                        breakpoint.GetNumLocations() == 1,
                        VALID_BREAKPOINT)

        # Now launch the process, and do not stop at entry point.
        process = target.LaunchSimple(None, None, os.getcwd())
        self.assertTrue(process, PROCESS_IS_VALID)

        # Frame #0 should be on self.line1 and the break condition should hold.
        from lldbutil import get_stopped_thread
        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
        self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")

        frame0 = thread.GetFrameAtIndex(0)

        var = frame0.EvaluateExpression ("call_me_to_get_lock()")
        self.assertTrue (var.IsValid())
        self.assertTrue (var.GetValueAsSigned (0) == 567)

if __name__ == '__main__':
    import atexit
    lldb.SBDebugger.Initialize()
    atexit.register(lambda: lldb.SBDebugger.Terminate())
    unittest2.main()
<|endoftext|>


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

from django.shortcuts import get_object_or_404

from .compat import NoReverseMatch, reverse
from .utils import get_account_model, get_current_account

class AccountMixinBase(object):

    @property
    def account(self):
        if not hasattr(self, '_account'):
            self._account = self.get_account()
        return self._account

    def get_account(self):
        from . import settings
        account_url_kwarg = settings.ACCOUNT_URL_KWARG
        if account_url_kwarg in self.kwargs:
            return get_object_or_404(get_account_model(),
                slug=self.kwargs.get(account_url_kwarg))
        return get_current_account()

    def get_context_data(self, **kwargs):
        context = super(AccountMixinBase, self).get_context_data(**kwargs)
        from . import settings
        url_kwargs = {}
        for url_kwarg in [settings.ACCOUNT_URL_KWARG, 'organization']:
            if url_kwarg in kwargs:
                url_kwargs.update({url_kwarg: kwargs[url_kwarg]})
        urls_pages = {
            'api_sources': reverse(
                'pages_api_sources', kwargs=url_kwargs)}
        try:
            urls_pages.update({
                'api_themes': reverse(
                    'pages_api_themes', kwargs=url_kwargs),
                'theme_base': reverse(
                    'theme_update', kwargs=url_kwargs),
                'theme_download': reverse(
                    'theme_download', kwargs=url_kwargs)})
        except NoReverseMatch:
            # Themes are optional
            pass
        if 'urls' in context:
            if 'pages' in context['urls']:
                context['urls']['pages'].update(urls_pages)
            else:
                context['urls'].update({'pages': urls_pages})
        else:
            context.update({'urls': {'pages': urls_pages}})
        return context
<commit_msg>reverts url kwargs<commit_after># Copyright (c) 2018, DjaoDjin inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from django.shortcuts import get_object_or_404

from .compat import NoReverseMatch, reverse
from .utils import get_account_model, get_current_account

class AccountMixinBase(object):

    @property
    def account(self):
        if not hasattr(self, '_account'):
            self._account = self.get_account()
        return self._account

    def get_account(self):
        from . import settings
        account_url_kwarg = settings.ACCOUNT_URL_KWARG
        if account_url_kwarg in self.kwargs:
            return get_object_or_404(get_account_model(),
                slug=self.kwargs.get(account_url_kwarg))
        return get_current_account()

    def get_context_data(self, **kwargs):
        context = super(AccountMixinBase, self).get_context_data(**kwargs)
        from . import settings
        url_kwargs = {}
        for url_kwarg in [settings.ACCOUNT_URL_KWARG]:
            if url_kwarg in kwargs:
                url_kwargs.update({url_kwarg: kwargs[url_kwarg]})
        urls_pages = {
            'api_sources': reverse(
                'pages_api_sources', kwargs=url_kwargs)}
        try:
            urls_pages.update({
                'api_themes': reverse(
                    'pages_api_themes', kwargs=url_kwargs),
                'theme_base': reverse(
                    'theme_update', kwargs=url_kwargs),
                'theme_download': reverse(
                    'theme_download', kwargs=url_kwargs)})
        except NoReverseMatch:
            # Themes are optional
            pass
        if 'urls' in context:
            if 'pages' in context['urls']:
                context['urls']['pages'].update(urls_pages)
            else:
                context['urls'].update({'pages': urls_pages})
        else:
            context.update({'urls': {'pages': urls_pages}})
        return context
<|endoftext|>


<commit_before>import os.path

from zinc.models import *
from zinc.helpers import *

from tests import *

class ZincIndexTestCase(TempDirTestCase):

    def test_versions_for_nonexistant_bundle(self):
        index = ZincIndex()
        self.assertTrue(len(index.versions_for_bundle("meep")) == 0)

    def test_add_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        self.assertTrue(1 in index.versions_for_bundle("meep"))

    def test_add_duplicate_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.add_version_for_bundle("meep", 1)
        self.assertTrue(1 in index.versions_for_bundle("meep"))
        self.assertTrue(len(index.versions_for_bundle("meep")) == 1)

    def test_del_version_for_nonexistant_bundle(self):
        index = ZincIndex()
        self.assertRaises(Exception, index.delete_bundle_version, "meep", 1)
        self.assertTrue(len(index.versions_for_bundle("meep")) == 0)

    def test_del_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.add_version_for_bundle("meep", 2)
        index.delete_bundle_version("meep", 1)
        self.assertTrue(len(index.versions_for_bundle("meep")) == 1)
        self.assertTrue(2 in index.versions_for_bundle("meep"))

    def test_del_nonexistant_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.delete_bundle_version("meep", 2)
        self.assertTrue(len(index.versions_for_bundle("meep")) == 1)
        self.assertTrue(1 in index.versions_for_bundle("meep"))

    def test_del_version_for_bundle_in_active_distro_raises(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)
        self.assertRaises(Exception, index.delete_bundle_version, "meep", 1)

    def test_update_distro_bad_bundle(self):
        index = ZincIndex()
        self.assertRaises(ValueError, index.update_distribution, "live", "beep", 1)

    def test_update_distro_bad_bundle_version(self):
        index = ZincIndex()
        index.add_version_for_bundle("beep", 1)
        self.assertRaises(ValueError, index.update_distribution, "live", "beep", 2)

    def test_update_distro_ok(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)

    def test_delete_distro_ok(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)
        self.assertTrue(len(index.distributions_for_bundle("meep")) == 1)
        index.delete_distribution("live", "meep")
        self.assertTrue(len(index.distributions_for_bundle("meep")) == 0)

    def test_version_for_distro(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)
        self.assertTrue(index.version_for_bundle("meep", "live") == 1)

    def test_distributions_for_bundle_by_version_unknown_bundle(self):
        """Tests that an exception is raised if called with an unknown bundle name"""
        index = ZincIndex()
        self.assertRaises(Exception, index.distributions_for_bundle_by_version, "meep")

    def test_distributions_for_bundle_by_version_no_distros(self):
        """Tests that the result is empty if no distros exist"""
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        distros = index.distributions_for_bundle_by_version("meep")
        self.assertEquals(len(distros), 0)

    def test_distributions_for_bundle_by_version_single_distro(self):
        """Tests that the result is correct if there is one distro associated
        with the version."""
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("master", "meep", 1)
        distros = index.distributions_for_bundle_by_version("meep")
        self.assertEquals(distros[1], ["master",])

    def test_distributions_for_bundle_by_version_multiple_distros(self):
        """Tests that the result is correct if there is one distro associated
        with the version."""
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("master", "meep", 1)
        index.update_distribution("test", "meep", 1)
        distros = index.distributions_for_bundle_by_version("meep")
        self.assertTrue("master" in distros[1])
        self.assertTrue("test" in distros[1])

    def test_next_version_for_bundle_from_old_index(self):
        p = abs_path_for_fixture("index-pre-next_version.json")
        index = ZincIndex.from_path(p)

        bundle_name = "meep"
        expected_next_version = 2

        # ensure it returns the right value
        next_version = index.next_version_for_bundle(bundle_name)
        self.assertEquals(next_version, expected_next_version)

        # ensure the 'next_version' key is written
        next_version = index.to_dict()["bundles"][bundle_name]["next_version"]
        self.assertEquals(next_version, expected_next_version)

    def test_next_version_for_bundle_with_old_bad_key(self):
        p = abs_path_for_fixture("index-with-bad-next-version.json")
        index = ZincIndex.from_path(p)

        bundle_name = "meep"
        expected_next_version = 2

        # ensure it returns the right value
        next_version = index.next_version_for_bundle(bundle_name)
        self.assertEquals(next_version, expected_next_version)

        # ensure the 'next_version' key is written
        next_version = index.to_dict()["bundles"][bundle_name]["next_version"]
        self.assertEquals(next_version, expected_next_version)

        # ensure the 'next-version' key is deleted
        bad_key = index.to_dict()["bundles"][bundle_name].get('next-version')
        self.assertTrue(bad_key is None)

    def test_immutable(self):
        index = ZincIndex(mutable=False)
        self.assertFalse(index.is_mutable)
        self.assertRaises(TypeError, index.add_version_for_bundle, "meep", 1)
        self.assertRaises(TypeError, index.delete_bundle_version, "meep", 1)
        self.assertRaises(TypeError, index.update_distribution, "master", "meep", 1)
        self.assertRaises(TypeError, index.delete_distribution, "master", "meep")

    def test_immutable_from_dict(self):
        index = ZincIndex(id='com.foo')
        index.add_version_for_bundle("meep", 1)
        d = index.to_dict()
        immutable_index = ZincIndex.from_dict(d, mutable=False)
        self.assertFalse(immutable_index.is_mutable)

class ZincFileListTestCase(unittest.TestCase):

    def test_immutable(self):

        filelist = ZincFileList(mutable=False)
        self.assertFalse(filelist.is_mutable)
        self.assertRaises(TypeError, filelist.add_file, "/tmp/foo", "123")
        self.assertRaises(
                TypeError, filelist.add_format_for_file, "/tmp/foo", "gz", 123)
        self.assertRaises(
                TypeError, filelist.add_flavor_for_file, "/tmp/foo", "small")

    def test_immutable_from_dict(self):
        filelist = ZincFileList()
        filelist.add_file('/tmp/foo', '123')
        d = filelist.to_dict()
        immutable_filelist = ZincFileList.from_dict(d, mutable=False)
        self.assertFalse(immutable_filelist.is_mutable)

class ZincManifestTestCase(TempDirTestCase):

    def test_save_and_load_with_files(self):
        manifest1 = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest1.files = {
                'a': {
                    'sha': 'ea502a7bbd407872e50b9328956277d0228272d4',
                    'formats': {
                        'raw' : {
                            'size': 123
                            }
                        }
                    }
                }
        path = os.path.join(self.dir, "manifest.json")
        manifest1.write(path)
        manifest2 = ZincManifest.from_path(path)
        self.assertEquals(manifest1, manifest2)

    def test_save_and_load_with_flavors(self):
        manifest1 = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest1._flavors = ['green']
        path = os.path.join(self.dir, "manifest.json")
        manifest1.write(path)
        manifest2 = ZincManifest.from_path(path)
        self.assertEquals(manifest1, manifest2)

    def test_add_flavor(self):
        manifest = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest.add_file('foo', 'ea502a7bbd407872e50b9328956277d0228272d4')
        manifest.add_flavor_for_file('foo', 'green')
        flavors = manifest.flavors_for_file('foo')
        self.assertEquals(len(flavors), 1)
        self.assertTrue('green' in flavors)
        self.assertTrue('green' in manifest.flavors)

    def test_flavors_are_added_from_files(self):
        # 1) create a FileList object with flavors
        filelist = ZincFileList()
        filelist.add_file('foo', 'ea502a7bbd407872e50b9328956277d0228272d4')
        filelist.add_flavor_for_file('foo', 'green')

        # 2) manifest.files = (that FileList)
        manifest = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest.files = filelist

        # 3) self.assertTrue(flavor in manifest.flavors
        self.assertTrue('green' in manifest.flavors)

    def test_immutable(self):
        manifest = ZincManifest('com.foo', 'stuff', 1, mutable=False)
        # TODO: test files setter
        self.assertRaises(
                TypeError, manifest.add_file, '/tmp/hi', '123')
        self.assertRaises(
                TypeError, manifest.add_format_for_file, '/tmp/hi', 'gz', 123)

    def test_immutable_from_dict(self):
        manifest = ZincManifest('com.foo', 'stuff', 1)
        d = manifest.to_dict()
        immutable_manifest = ZincManifest.from_dict(d, mutable=False)
        self.assertFalse(immutable_manifest.is_mutable)

class ZincFlavorSpecTestCase(unittest.TestCase):

    def test_load_from_dict_1(self):
        d = {'small' : ['+ 50x50'], 'large' : ['+ 100x100']}
        spec = ZincFlavorSpec.from_dict(d)
        self.assertTrue(spec is not None)
        self.assertEquals(len(spec.flavors), 2)

    def test_immutable(self):
        spec = ZincFlavorSpec(mutable=False)
        self.assertFalse(spec.is_mutable)
        self.assertRaises(TypeError, spec.add_flavor, 'small', None)

class BundleDescriptorTestCase(unittest.TestCase):

    def test_bundle_id_from_descriptor_without_flavor(self):
        descriptor = 'com.foo.bar-1'
        bundle_id = 'com.foo.bar'
        self.assertEquals(bundle_id, bundle_id_from_bundle_descriptor(descriptor))

    def test_bundle_id_from_descriptor_with_flavor(self):
        descriptor = 'com.foo.bar-1~green'
        bundle_id = 'com.foo.bar'
        self.assertEquals(bundle_id, bundle_id_from_bundle_descriptor(descriptor))

    def test_bundle_version_from_descriptor_without_flavor(self):
        descriptor = 'com.foo.bar-1'
        bundle_version = 1
        self.assertEquals(bundle_version, bundle_version_from_bundle_descriptor(descriptor))

    def test_bundle_version_from_descriptor_with_flavor(self):
        descriptor = 'com.foo.bar-1~green'
        bundle_version = 1
        self.assertEquals(bundle_version, bundle_version_from_bundle_descriptor(descriptor))

    def test_bundle_version_from_descriptor_with_flavor_with_dash(self):
        descriptor = 'com.foo.bar-1~green-ish'
        bundle_version = 1
        self.assertEquals(bundle_version, bundle_version_from_bundle_descriptor(descriptor))

<commit_msg>Adding TODO comment<commit_after>import os.path

from zinc.models import *
from zinc.helpers import *

from tests import *

class ZincIndexTestCase(TempDirTestCase):

    def test_versions_for_nonexistant_bundle(self):
        index = ZincIndex()
        self.assertTrue(len(index.versions_for_bundle("meep")) == 0)

    def test_add_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        self.assertTrue(1 in index.versions_for_bundle("meep"))

    def test_add_duplicate_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.add_version_for_bundle("meep", 1)
        self.assertTrue(1 in index.versions_for_bundle("meep"))
        self.assertTrue(len(index.versions_for_bundle("meep")) == 1)

    def test_del_version_for_nonexistant_bundle(self):
        index = ZincIndex()
        self.assertRaises(Exception, index.delete_bundle_version, "meep", 1)
        self.assertTrue(len(index.versions_for_bundle("meep")) == 0)

    def test_del_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.add_version_for_bundle("meep", 2)
        index.delete_bundle_version("meep", 1)
        self.assertTrue(len(index.versions_for_bundle("meep")) == 1)
        self.assertTrue(2 in index.versions_for_bundle("meep"))

    def test_del_nonexistant_version_for_bundle(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.delete_bundle_version("meep", 2)
        self.assertTrue(len(index.versions_for_bundle("meep")) == 1)
        self.assertTrue(1 in index.versions_for_bundle("meep"))

    def test_del_version_for_bundle_in_active_distro_raises(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)
        self.assertRaises(Exception, index.delete_bundle_version, "meep", 1)

    def test_update_distro_bad_bundle(self):
        index = ZincIndex()
        self.assertRaises(ValueError, index.update_distribution, "live", "beep", 1)

    def test_update_distro_bad_bundle_version(self):
        index = ZincIndex()
        index.add_version_for_bundle("beep", 1)
        self.assertRaises(ValueError, index.update_distribution, "live", "beep", 2)

    def test_update_distro_ok(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)

    def test_delete_distro_ok(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)
        self.assertTrue(len(index.distributions_for_bundle("meep")) == 1)
        index.delete_distribution("live", "meep")
        self.assertTrue(len(index.distributions_for_bundle("meep")) == 0)

    def test_version_for_distro(self):
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("live", "meep", 1)
        self.assertTrue(index.version_for_bundle("meep", "live") == 1)

    def test_distributions_for_bundle_by_version_unknown_bundle(self):
        """Tests that an exception is raised if called with an unknown bundle name"""
        index = ZincIndex()
        self.assertRaises(Exception, index.distributions_for_bundle_by_version, "meep")

    def test_distributions_for_bundle_by_version_no_distros(self):
        """Tests that the result is empty if no distros exist"""
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        distros = index.distributions_for_bundle_by_version("meep")
        self.assertEquals(len(distros), 0)

    def test_distributions_for_bundle_by_version_single_distro(self):
        """Tests that the result is correct if there is one distro associated
        with the version."""
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("master", "meep", 1)
        distros = index.distributions_for_bundle_by_version("meep")
        self.assertEquals(distros[1], ["master",])

    def test_distributions_for_bundle_by_version_multiple_distros(self):
        """Tests that the result is correct if there is one distro associated
        with the version."""
        index = ZincIndex()
        index.add_version_for_bundle("meep", 1)
        index.update_distribution("master", "meep", 1)
        index.update_distribution("test", "meep", 1)
        distros = index.distributions_for_bundle_by_version("meep")
        self.assertTrue("master" in distros[1])
        self.assertTrue("test" in distros[1])

    def test_next_version_for_bundle_from_old_index(self):
        p = abs_path_for_fixture("index-pre-next_version.json")
        index = ZincIndex.from_path(p)

        bundle_name = "meep"
        expected_next_version = 2

        # ensure it returns the right value
        next_version = index.next_version_for_bundle(bundle_name)
        self.assertEquals(next_version, expected_next_version)

        # ensure the 'next_version' key is written
        next_version = index.to_dict()["bundles"][bundle_name]["next_version"]
        self.assertEquals(next_version, expected_next_version)

    def test_next_version_for_bundle_with_old_bad_key(self):
        p = abs_path_for_fixture("index-with-bad-next-version.json")
        index = ZincIndex.from_path(p)

        bundle_name = "meep"
        expected_next_version = 2

        # ensure it returns the right value
        next_version = index.next_version_for_bundle(bundle_name)
        self.assertEquals(next_version, expected_next_version)

        # ensure the 'next_version' key is written
        next_version = index.to_dict()["bundles"][bundle_name]["next_version"]
        self.assertEquals(next_version, expected_next_version)

        # ensure the 'next-version' key is deleted
        bad_key = index.to_dict()["bundles"][bundle_name].get('next-version')
        self.assertTrue(bad_key is None)

    def test_immutable(self):
        index = ZincIndex(mutable=False)
        self.assertFalse(index.is_mutable)
        self.assertRaises(TypeError, index.add_version_for_bundle, "meep", 1)
        self.assertRaises(TypeError, index.delete_bundle_version, "meep", 1)
        self.assertRaises(TypeError, index.update_distribution, "master", "meep", 1)
        self.assertRaises(TypeError, index.delete_distribution, "master", "meep")

    def test_immutable_from_dict(self):
        index = ZincIndex(id='com.foo')
        index.add_version_for_bundle("meep", 1)
        d = index.to_dict()
        immutable_index = ZincIndex.from_dict(d, mutable=False)
        self.assertFalse(immutable_index.is_mutable)

class ZincFileListTestCase(unittest.TestCase):

    def test_immutable(self):

        filelist = ZincFileList(mutable=False)
        self.assertFalse(filelist.is_mutable)
        self.assertRaises(TypeError, filelist.add_file, "/tmp/foo", "123")
        self.assertRaises(
                TypeError, filelist.add_format_for_file, "/tmp/foo", "gz", 123)
        self.assertRaises(
                TypeError, filelist.add_flavor_for_file, "/tmp/foo", "small")

    def test_immutable_from_dict(self):
        filelist = ZincFileList()
        filelist.add_file('/tmp/foo', '123')
        d = filelist.to_dict()
        immutable_filelist = ZincFileList.from_dict(d, mutable=False)
        self.assertFalse(immutable_filelist.is_mutable)

class ZincManifestTestCase(TempDirTestCase):

    def test_save_and_load_with_files(self):
        manifest1 = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest1.files = {
                'a': {
                    'sha': 'ea502a7bbd407872e50b9328956277d0228272d4',
                    'formats': {
                        'raw' : {
                            'size': 123
                            }
                        }
                    }
                }
        path = os.path.join(self.dir, "manifest.json")
        manifest1.write(path)
        manifest2 = ZincManifest.from_path(path)
        self.assertEquals(manifest1, manifest2)

    def test_save_and_load_with_flavors(self):
        manifest1 = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest1._flavors = ['green']
        path = os.path.join(self.dir, "manifest.json")
        manifest1.write(path)
        manifest2 = ZincManifest.from_path(path)
        self.assertEquals(manifest1, manifest2)

    def test_add_flavor(self):
        manifest = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest.add_file('foo', 'ea502a7bbd407872e50b9328956277d0228272d4')
        manifest.add_flavor_for_file('foo', 'green')
        flavors = manifest.flavors_for_file('foo')
        self.assertEquals(len(flavors), 1)
        self.assertTrue('green' in flavors)
        self.assertTrue('green' in manifest.flavors)

    def test_flavors_are_added_from_files(self):
        # 1) create a FileList object with flavors
        filelist = ZincFileList()
        filelist.add_file('foo', 'ea502a7bbd407872e50b9328956277d0228272d4')
        filelist.add_flavor_for_file('foo', 'green')

        # 2) manifest.files = (that FileList)
        manifest = ZincManifest('com.mindsnacks.test', 'meep', 1)
        manifest.files = filelist

        # 3) self.assertTrue(flavor in manifest.flavors
        self.assertTrue('green' in manifest.flavors)

    def test_immutable(self):
        manifest = ZincManifest('com.foo', 'stuff', 1, mutable=False)
        # TODO: test files setter
        self.assertRaises(
                TypeError, manifest.add_file, '/tmp/hi', '123')
        self.assertRaises(
                TypeError, manifest.add_format_for_file, '/tmp/hi', 'gz', 123)

    def test_immutable_from_dict(self):
        manifest = ZincManifest('com.foo', 'stuff', 1)
        d = manifest.to_dict()
        immutable_manifest = ZincManifest.from_dict(d, mutable=False)
        self.assertFalse(immutable_manifest.is_mutable)

class ZincFlavorSpecTestCase(unittest.TestCase):

    def test_load_from_dict_1(self):
        d = {'small' : ['+ 50x50'], 'large' : ['+ 100x100']}
        spec = ZincFlavorSpec.from_dict(d)
        self.assertTrue(spec is not None)
        self.assertEquals(len(spec.flavors), 2)

    def test_immutable(self):
        spec = ZincFlavorSpec(mutable=False)
        self.assertFalse(spec.is_mutable)
        self.assertRaises(TypeError, spec.add_flavor, 'small', None)

# TODO: relocate?
class BundleDescriptorTestCase(unittest.TestCase):

    def test_bundle_id_from_descriptor_without_flavor(self):
        descriptor = 'com.foo.bar-1'
        bundle_id = 'com.foo.bar'
        self.assertEquals(bundle_id, bundle_id_from_bundle_descriptor(descriptor))

    def test_bundle_id_from_descriptor_with_flavor(self):
        descriptor = 'com.foo.bar-1~green'
        bundle_id = 'com.foo.bar'
        self.assertEquals(bundle_id, bundle_id_from_bundle_descriptor(descriptor))

    def test_bundle_version_from_descriptor_without_flavor(self):
        descriptor = 'com.foo.bar-1'
        bundle_version = 1
        self.assertEquals(bundle_version, bundle_version_from_bundle_descriptor(descriptor))

    def test_bundle_version_from_descriptor_with_flavor(self):
        descriptor = 'com.foo.bar-1~green'
        bundle_version = 1
        self.assertEquals(bundle_version, bundle_version_from_bundle_descriptor(descriptor))

    def test_bundle_version_from_descriptor_with_flavor_with_dash(self):
        descriptor = 'com.foo.bar-1~green-ish'
        bundle_version = 1
        self.assertEquals(bundle_version, bundle_version_from_bundle_descriptor(descriptor))

<|endoftext|>


<commit_before>import unittest

import numpy

from chainer import cuda
from chainer import functions
from chainer import gradient_check
from chainer import testing
from chainer.testing import attr
from chainer.testing import condition

@testing.parameterize(*testing.product({
    'in_shape': [(2, 3, 8, 6), (2, 1, 4, 6)],
}))
class TestResizeImagesForwardIdentity(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(
            size=self.in_shape).astype(numpy.float32)

    def check_forward(self, x, output_shape):
        y = functions.resize_images(x, output_shape)
        testing.assert_allclose(y.data, x)

    def test_forward_cpu(self):
        self.check_forward(self.x, output_shape=self.in_shape[2:])

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward(cuda.to_gpu(self.x), output_shape=self.in_shape[2:])

class TestResizeImagesForwardDownScale(unittest.TestCase):

    in_shape = (2, 2, 4, 4)
    output_shape = (2, 2, 2, 2)

    def setUp(self):
        self.x = numpy.zeros(self.in_shape, dtype=numpy.float32)
        self.x[:, :, :2, :2] = 1
        self.x[:, :, 2:, :2] = 2
        self.x[:, :, :2, 2:] = 3
        self.x[:, :, 2:, 2:] = 4

        self.out = numpy.zeros(self.output_shape, dtype=numpy.float32)
        self.out[:, :, 0, 0] = 1
        self.out[:, :, 1, 0] = 2
        self.out[:, :, 0, 1] = 3
        self.out[:, :, 1, 1] = 4

    def check_forward(self, x, output_shape):
        y = functions.resize_images(x, output_shape)
        testing.assert_allclose(y.data, self.out)

    def test_forward_cpu(self):
        self.check_forward(self.x, output_shape=self.output_shape[2:])

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward(
            cuda.to_gpu(self.x), output_shape=self.output_shape[2:])

class TestResizeImagesForwardUpScale(unittest.TestCase):

    in_shape = (1, 1, 2, 2)
    output_shape = (1, 1, 3, 3)

    def setUp(self):
        self.x = numpy.zeros(self.in_shape, dtype=numpy.float32)
        self.x[:, :, 0, 0] = 1
        self.x[:, :, 1, 0] = 2
        self.x[:, :, 0, 1] = 3
        self.x[:, :, 1, 1] = 4

        self.out = numpy.zeros(self.output_shape, dtype=numpy.float32)
        self.out[0, 0, :, :] = numpy.array(
            [[1., 2., 3.],
             [1.5, 2.5, 3.5],
             [2., 3., 4.]],
            dtype=numpy.float32)

    def check_forward(self, x, output_shape):
        y = functions.resize_images(x, output_shape)
        testing.assert_allclose(y.data, self.out)

    def test_forward_cpu(self):
        self.check_forward(self.x, output_shape=self.output_shape[2:])

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward(
            cuda.to_gpu(self.x), output_shape=self.output_shape[2:])

@testing.parameterize(*testing.product({
    'in_shape': [(2, 3, 8, 6), (2, 1, 4, 6)],
    'output_shape': [(10, 5), (3, 4)]
}))
class TestResizeImagesBackward(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(
            size=self.in_shape).astype(numpy.float32)
        output_shape_4d = self.in_shape[:2] + self.output_shape
        self.gy = numpy.random.uniform(
            size=output_shape_4d).astype(numpy.float32)
        self.ggx = numpy.random.uniform(
            size=self.in_shape).astype(numpy.float32)

    def check_backward(self, x, output_shape, gy):
        def f(x):
            return functions.resize_images(x, output_shape)

        gradient_check.check_backward(
            f, (x,), (gy,), dtype='d', atol=1e-2, rtol=1e-3, eps=1e-5)

    @condition.retry(3)
    def test_backward_cpu(self):
        self.check_backward(self.x, self.output_shape, self.gy)

    @attr.gpu
    @condition.retry(3)
    def test_backward_gpu(self):
        self.check_backward(cuda.to_gpu(self.x), self.output_shape,
                            cuda.to_gpu(self.gy))

    def check_double_backward(self, x, output_shape, gy, ggx):
        def f(x):
            y = functions.resize_images(x, output_shape)
            return y * y

        gradient_check.check_double_backward(f, (x,), (gy,), (ggx,), dtype='f')

    def test_doube_backward_cpu(self):
        self.check_double_backward(
            self.x, self.output_shape, self.gy, self.ggx)

    @attr.gpu
    def test_double_backward_gpu(self):
        self.check_double_backward(cuda.to_gpu(self.x), self.output_shape,
                                   cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx))

testing.run_module(__name__, __file__)
<commit_msg>Fix function test<commit_after>import unittest

import numpy

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

@testing.parameterize(*testing.product({
    'in_shape': [(2, 3, 8, 6), (2, 1, 4, 6)],
}))
class TestResizeImagesForwardIdentity(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(
            size=self.in_shape).astype(numpy.float32)

    def check_forward(self, x, output_shape):
        y = functions.resize_images(x, output_shape)
        testing.assert_allclose(y.data, x)

    def test_forward_cpu(self):
        self.check_forward(self.x, output_shape=self.in_shape[2:])

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward(cuda.to_gpu(self.x), output_shape=self.in_shape[2:])

class TestResizeImagesForwardDownScale(unittest.TestCase):

    in_shape = (2, 2, 4, 4)
    output_shape = (2, 2, 2, 2)

    def setUp(self):
        self.x = numpy.zeros(self.in_shape, dtype=numpy.float32)
        self.x[:, :, :2, :2] = 1
        self.x[:, :, 2:, :2] = 2
        self.x[:, :, :2, 2:] = 3
        self.x[:, :, 2:, 2:] = 4

        self.out = numpy.zeros(self.output_shape, dtype=numpy.float32)
        self.out[:, :, 0, 0] = 1
        self.out[:, :, 1, 0] = 2
        self.out[:, :, 0, 1] = 3
        self.out[:, :, 1, 1] = 4

    def check_forward(self, x, output_shape):
        y = functions.resize_images(x, output_shape)
        testing.assert_allclose(y.data, self.out)

    def test_forward_cpu(self):
        self.check_forward(self.x, output_shape=self.output_shape[2:])

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward(
            cuda.to_gpu(self.x), output_shape=self.output_shape[2:])

class TestResizeImagesForwardUpScale(unittest.TestCase):

    in_shape = (1, 1, 2, 2)
    output_shape = (1, 1, 3, 3)

    def setUp(self):
        self.x = numpy.zeros(self.in_shape, dtype=numpy.float32)
        self.x[:, :, 0, 0] = 1
        self.x[:, :, 1, 0] = 2
        self.x[:, :, 0, 1] = 3
        self.x[:, :, 1, 1] = 4

        self.out = numpy.zeros(self.output_shape, dtype=numpy.float32)
        self.out[0, 0, :, :] = numpy.array(
            [[1., 2., 3.],
             [1.5, 2.5, 3.5],
             [2., 3., 4.]],
            dtype=numpy.float32)

    def check_forward(self, x, output_shape):
        y = functions.resize_images(x, output_shape)
        testing.assert_allclose(y.data, self.out)

    def test_forward_cpu(self):
        self.check_forward(self.x, output_shape=self.output_shape[2:])

    @attr.gpu
    def test_forward_gpu(self):
        self.check_forward(
            cuda.to_gpu(self.x), output_shape=self.output_shape[2:])

@testing.parameterize(*testing.product({
    'in_shape': [(2, 3, 8, 6), (2, 1, 4, 6)],
    'output_shape': [(10, 5), (3, 4)]
}))
class TestResizeImagesBackward(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(
            size=self.in_shape).astype(numpy.float32)
        output_shape_4d = self.in_shape[:2] + self.output_shape
        self.gy = numpy.random.uniform(
            size=output_shape_4d).astype(numpy.float32)
        self.ggx = numpy.random.uniform(
            size=self.in_shape).astype(numpy.float32)

    def check_backward(self, x, output_shape, gy):
        def f(x):
            return functions.resize_images(x, output_shape)

        gradient_check.check_backward(
            f, x, gy, dtype='d', atol=1e-2, rtol=1e-3, eps=1e-5)

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

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

    def check_double_backward(self, x, output_shape, gy, ggx):
        def f(x):
            y = functions.resize_images(x, output_shape)
            return y * y

        gradient_check.check_double_backward(f, x, gy, ggx)

    def test_doube_backward_cpu(self):
        self.check_double_backward(
            self.x, self.output_shape, self.gy, self.ggx)

    @attr.gpu
    def test_double_backward_gpu(self):
        self.check_double_backward(cuda.to_gpu(self.x), self.output_shape,
                                   cuda.to_gpu(self.gy), cuda.to_gpu(self.ggx))

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


<commit_before><commit_msg>More data in admin<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python3

"""
Here is the opening stanza of an important work of the English
 language:

    Burning 'em, if you ain't quick and nimble
    I go crazy when I hear a cymbal

Encrypt it, under the key "ICE", using repeating-key XOR.

In repeating-key XOR, you'll sequentially apply each byte of the key;
 the first byte of plaintext will be XOR'd against I, the next C, the
 next E, then I again for the 4th byte, and so on.

It should come out to:

0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a262263242727\
65272
a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e272\
82f

Encrypt a bunch of stuff using your repeating-key XOR function. Encrypt
 your mail. Encrypt your password file. Your .sig file. Get a feel for
 it. I promise, we aren't wasting your time with this.
"""

import argparse
import string
import sys
import math
import binascii

def encrypt(msg, key):
    msglen=len(msg)
    testmsg = ''.join(chr(ord(x) ^ ord(y))
        for x,y in zip(key*(math.ceil(msglen/len(key))+1),msg))
    return binascii.hexlify(testmsg.encode('utf-8')).decode('utf-8')

def main(message, key):
    print('Line: ' + message)
    print('Key: ' + key)
    ret = encrypt(message,key)
    if ret:
        print('Encrypt: ' + ret)
        return 0
    print('Error.')
    return -1

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Encrypts a message with repeating key XOR')
    parser.add_argument('-m', '--message', help='opt. message to encrypt',
                    default='')
    parser.add_argument('-k', '--key', help='opt. key',
                    default='ICE')
    args = parser.parse_args()
    if not args.message:
        args.message = 'Burning \'em, if you ain\'t quick and nimble\nI go crazy'
        args.message += ' when I hear a cymbal'
    sys.exit(main(args.message, args.key))
<commit_msg>fixed default string length to PEP-8 standards<commit_after>#!/usr/bin/python3

"""
Here is the opening stanza of an important work of the English
 language:

    Burning 'em, if you ain't quick and nimble
    I go crazy when I hear a cymbal

Encrypt it, under the key "ICE", using repeating-key XOR.

In repeating-key XOR, you'll sequentially apply each byte of the key;
 the first byte of plaintext will be XOR'd against I, the next C, the
 next E, then I again for the 4th byte, and so on.

It should come out to:

0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a262263242727\
65272
a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e272\
82f

Encrypt a bunch of stuff using your repeating-key XOR function. Encrypt
 your mail. Encrypt your password file. Your .sig file. Get a feel for
 it. I promise, we aren't wasting your time with this.
"""

import argparse
import string
import sys
import math
import binascii

def encrypt(msg, key):
    msglen=len(msg)
    testmsg = ''.join(chr(ord(x) ^ ord(y))
        for x,y in zip(key*(math.ceil(msglen/len(key))+1),msg))
    return binascii.hexlify(testmsg.encode('utf-8')).decode('utf-8')

def main(message, key):
    print('Line: ' + message)
    print('Key: ' + key)
    ret = encrypt(message,key)
    if ret:
        print('Encrypt: ' + ret)
        return 0
    print('Error.')
    return -1

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Encrypts a message with repeating key XOR')
    parser.add_argument('-m', '--message', help='opt. message to encrypt',
                    default='')
    parser.add_argument('-k', '--key', help='opt. key',
                    default='ICE')
    args = parser.parse_args()
    if not args.message:
        args.message = 'Burning \'em, if you ain\'t quick and nimble\nI go'
        args.message += ' crazy when I hear a cymbal'
    sys.exit(main(args.message, args.key))
<|endoftext|>