<commit_before>#!/usr/bin/env python
#
# Copyright (c) 2014-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import tempfile
import subprocess
import xml.etree.ElementTree as ET
import getopt
import shutil
from . import metadata
from .simple_puller import SimplePuller
import glob
import zipfile
from . import common

from sets import Set
from os.path import join
from os.path import abspath

ROOT_SCREENSHOT_DIR = '/sdcard/screenshots'
OLD_ROOT_SCREENSHOT_DIR = '/data/data/'

def usage():
    print >>sys.stderr, "usage: ./scripts/screenshot_tests/pull_screenshots com.facebook.apk.name.tests [--generate-png]"
    return

def generate_html(dir):
    root = ET.parse(join(dir, 'metadata.xml')).getroot()
    alternate = False
    index_html = abspath(join(dir, "index.html"))
    with open(index_html, "w") as html:
        html.write('<!DOCTYPE html>')
        html.write('')
        html.write('')
        html.write('')
        html.write('')
        html.write('')
        html.write('<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />')
        html.write('<link rel="stylesheet" href="default.css">')
        html.write('')

        html.write('')
        for screenshot in root.iter('screenshot'):
            alternate = not alternate
            html.write('
' % ('alternate' if alternate else ''))
            html.write('
%s
' % (screenshot.find('name').text))
            html.write('<button class="view_dump" data-name="%s">Dump view hierarchy</button>' % (screenshot.find('name').text))

            extras = screenshot.find('extras')
            if extras is not None:
                str = ""
                for node in extras:
                    if node.text is not None:
                        str = str + "*****" + node.tag + "*****\n\n" + node.text + "\n\n\n"
                if str != "":
                    extra_html = '<button class="extra" data="%s">Extra info</button>' % str
                    html.write(extra_html.encode('utf-8').strip())

            description = screenshot.find('description')
            if description is not None:
                html.write('
%s
' % description.text)

            error = screenshot.find('error')
            if error is not None:
                html.write('
%s
' % error.text)
            else:
                write_image(dir, html, screenshot)

            html.write('
')

        html.write('')
        return index_html

def write_image(dir, html, screenshot):
    html.write('
')
    for y in xrange(int(screenshot.find('tile_height').text)):
        html.write('
')
        for x in xrange(int(screenshot.find('tile_width').text)):
            html.write('')
            image_file = "./" + common.get_image_file_name(screenshot.find('name').text, x, y)

            if os.path.exists(join(dir, image_file)):
                html.write('' % image_file)

            html.write('	')
        html.write('
')
    html.write('
')

def test_for_wkhtmltoimage():
    if subprocess.call(['which', 'wkhtmltoimage']) != 0:
        raise RuntimeError("""Could not find wkhtmltoimage in your path, we need this for generating pngs
Download an appropriate version from:
    http://wkhtmltopdf.org/downloads.html""")

def generate_png(path_to_html, path_to_png):
    test_for_wkhtmltoimage()
    subprocess.check_call(['wkhtmltoimage', path_to_html, path_to_png], stdout=sys.stdout)

def copy_assets(destination):
    """Copy static assets required for rendering the HTML"""
    _copy_asset("default.css", destination)
    _copy_asset("default.js", destination)
    _copy_asset("background.png", destination)

def _copy_asset(filename, destination):
    thisdir = os.path.dirname(__file__)
    _copy_file(abspath(join(thisdir, filename)), join(destination, filename))

def _copy_file(src, dest):
    if os.path.exists(src):
        shutil.copyfile(src, dest)
    else:
        _copy_via_zip(src, None, dest)

def _copy_via_zip(src_zip, zip_path, dest):
    if os.path.exists(src_zip):
        zip = zipfile.ZipFile(src_zip)
        input = zip.open(zip_path, 'r')
        with open(dest, 'w') as output:
            output.write(input.read())
    else:
        # walk up the tree
        head, tail = os.path.split(src_zip)

        _copy_via_zip(head, tail if not zip_path else (tail + "/" + zip_path), dest)

def pull_metadata(package, dir, adb_puller):
    metadata_file = '%s/%s/screenshots-default/metadata.xml' % (ROOT_SCREENSHOT_DIR, package)
    old_metadata_file = '%s/%s/app_screenshots-default/metadata.xml' % (OLD_ROOT_SCREENSHOT_DIR, package)

    if adb_puller.remote_file_exists(metadata_file):
        adb_puller.pull(metadata_file, join(dir, 'metadata.xml'))
    elif adb_puller.remote_file_exists(old_metadata_file):
        adb_puller.pull(old_metadata_file, join(dir, 'metadata.xml'))
    else:
        create_empty_metadata_file(dir)

def create_empty_metadata_file(dir):
    with open(join(dir, 'metadata.xml'), 'w') as out:
        out.write("""<?xml version="1.0" encoding="UTF-8"?>
<screenshots>
</screenshots>""")

def pull_images(dir, adb_puller):
    root = ET.parse(join(dir, 'metadata.xml')).getroot()
    for s in root.iter('screenshot'):
        filename_nodes = s.findall('absolute_file_name')
        for filename_node in filename_nodes:
            adb_puller.pull(filename_node.text, join(dir, os.path.basename(filename_node.text)))
        dump_node = s.find('view_hierarchy')
        if dump_node is not None:
            adb_puller.pull(dump_node.text, join(dir, os.path.basename(dump_node.text)))

def pull_all(package, dir, adb_puller):
    pull_metadata(package, dir, adb_puller=adb_puller)
    pull_images(dir, adb_puller=adb_puller)

def pull_filtered(package, dir, adb_puller, filter_name_regex=None):
    pull_metadata(package, dir, adb_puller=adb_puller)
    metadata.filter_screenshots(join(dir, 'metadata.xml'), name_regex=filter_name_regex)
    pull_images(dir, adb_puller=adb_puller)

def pull_screenshots(process,
                     adb_puller,
                     temp_dir=None,
                     filter_name_regex=None,
                     opt_generate_png=None):
    temp_dir = temp_dir or tempfile.mkdtemp(prefix='screenshots')
    copy_assets(temp_dir)

    pull_filtered(process, adb_puller=adb_puller, dir=temp_dir, filter_name_regex=filter_name_regex)
    path_to_html = generate_html(temp_dir)

    if opt_generate_png:
        generate_png(path_to_html, opt_generate_png)
        shutil.rmtree(temp_dir)
    else:
        print('Open the following url in a browser to view the results: ')
        print('  file://%s' % path_to_html)

def _check_output(args, **kwargs):
    with open(os.devnull) as f:
        kwargs['stderr'] = f
        return subprocess.check_output(args, **kwargs)

def parse_package_line(line):
    """The line looks like this:
    package: name='com.facebook.testing.tests' versionCode='1' versionName=''"""

    for word in line.split():
        if word.startswith("name='"):
            return word[len("name='"):-1]

def get_aapt_bin():
    """Find the binary for aapt from $ANDROID_SDK"""
    android_sdk = os.environ.get('ANDROID_SDK') or os.environ.get('ANDROID_HOME')
    build_tools = os.path.join(android_sdk, 'build-tools')

    all = list(glob.glob(os.path.join(build_tools, '*/aapt')))
    bad = list(glob.glob(os.path.join(build_tools, 'android-*/aapt')))
    good = list(Set(all) - Set(bad))

    good.sort()
    bad.sort()

    if len(good) == 0:
        return bad[-1]

    return good[-1]

def get_package(apk):
    output = _check_output([get_aapt_bin(), 'dump', 'badging', apk], stderr=os.devnull)
    for line in output.split('\n'):
        if line.startswith('package:'):
            return parse_package_line(line)

def main(argv):
    try:
        opt_list, rest_args = getopt.gnu_getopt(
            argv[1:],
            "eds:",
            ["generate-png=", "filter-name-regex=", "apk", "record=", "verify="])
    except getopt.GetoptError, err:
        usage()
        return 2

    if len(rest_args) != 1:
        usage()
        return 2

    process = rest_args[0]  # something like com.facebook.places.tests

    opts = dict(opt_list)

    if "--apk" in opts:
        # treat process as an apk instead
        process = get_package(process)

    puller_args = []
    if "-e" in opts:
        puller_args.append("-e")

    if "-d" in opts:
        puller_args.append("-d")

    if "-s" in opts:
        puller_args += ["-s", opts["-s"]]

    return pull_screenshots(process,
                            filter_name_regex=opts.get('--filter-name-regex'),
                            opt_generate_png=opts.get('--generate-png'),
                            record=opts.get('--record'),
                            verify=opts.get('--verify'),
                            adb_puller=SimplePuller(puller_args))

if __name__ == '__main__':
    sys.exit(main(sys.argv))
<commit_msg>pull screenshots<commit_after>#!/usr/bin/env python
#
# Copyright (c) 2014-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import tempfile
import subprocess
import xml.etree.ElementTree as ET
import getopt
import shutil
from . import metadata
from .simple_puller import SimplePuller
import glob
import zipfile
from . import common

from sets import Set
from os.path import join
from os.path import abspath

ROOT_SCREENSHOT_DIR = '/sdcard/screenshots'
OLD_ROOT_SCREENSHOT_DIR = '/data/data/'

def usage():
    print >>sys.stderr, "usage: ./scripts/screenshot_tests/pull_screenshots com.facebook.apk.name.tests [--generate-png]"
    return

def generate_html(dir):
    root = ET.parse(join(dir, 'metadata.xml')).getroot()
    alternate = False
    index_html = abspath(join(dir, "index.html"))
    with open(index_html, "w") as html:
        html.write('<!DOCTYPE html>')
        html.write('')
        html.write('')
        html.write('')
        html.write('')
        html.write('')
        html.write('<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />')
        html.write('<link rel="stylesheet" href="default.css">')
        html.write('')

        html.write('')
        for screenshot in root.iter('screenshot'):
            alternate = not alternate
            html.write('
' % ('alternate' if alternate else ''))
            html.write('
%s
' % (screenshot.find('name').text))
            html.write('<button class="view_dump" data-name="%s">Dump view hierarchy</button>' % (screenshot.find('name').text))

            extras = screenshot.find('extras')
            if extras is not None:
                str = ""
                for node in extras:
                    if node.text is not None:
                        str = str + "*****" + node.tag + "*****\n\n" + node.text + "\n\n\n"
                if str != "":
                    extra_html = '<button class="extra" data="%s">Extra info</button>' % str
                    html.write(extra_html.encode('utf-8').strip())

            description = screenshot.find('description')
            if description is not None:
                html.write('
%s
' % description.text)

            error = screenshot.find('error')
            if error is not None:
                html.write('
%s
' % error.text)
            else:
                write_image(dir, html, screenshot)

            html.write('
')

        html.write('')
        return index_html

def write_image(dir, html, screenshot):
    html.write('
')
    for y in xrange(int(screenshot.find('tile_height').text)):
        html.write('
')
        for x in xrange(int(screenshot.find('tile_width').text)):
            html.write('')
            image_file = "./" + common.get_image_file_name(screenshot.find('name').text, x, y)

            if os.path.exists(join(dir, image_file)):
                html.write('' % image_file)

            html.write('	')
        html.write('
')
    html.write('
')

def test_for_wkhtmltoimage():
    if subprocess.call(['which', 'wkhtmltoimage']) != 0:
        raise RuntimeError("""Could not find wkhtmltoimage in your path, we need this for generating pngs
Download an appropriate version from:
    http://wkhtmltopdf.org/downloads.html""")

def generate_png(path_to_html, path_to_png):
    test_for_wkhtmltoimage()
    subprocess.check_call(['wkhtmltoimage', path_to_html, path_to_png], stdout=sys.stdout)

def copy_assets(destination):
    """Copy static assets required for rendering the HTML"""
    _copy_asset("default.css", destination)
    _copy_asset("default.js", destination)
    _copy_asset("background.png", destination)

def _copy_asset(filename, destination):
    thisdir = os.path.dirname(__file__)
    _copy_file(abspath(join(thisdir, filename)), join(destination, filename))

def _copy_file(src, dest):
    if os.path.exists(src):
        shutil.copyfile(src, dest)
    else:
        _copy_via_zip(src, None, dest)

def _copy_via_zip(src_zip, zip_path, dest):
    if os.path.exists(src_zip):
        zip = zipfile.ZipFile(src_zip)
        input = zip.open(zip_path, 'r')
        with open(dest, 'w') as output:
            output.write(input.read())
    else:
        # walk up the tree
        head, tail = os.path.split(src_zip)

        _copy_via_zip(head, tail if not zip_path else (tail + "/" + zip_path), dest)

def pull_metadata(package, dir, adb_puller):
    metadata_file = '%s/%s/screenshots-default/metadata.xml' % (ROOT_SCREENSHOT_DIR, package)
    old_metadata_file = '%s/%s/app_screenshots-default/metadata.xml' % (OLD_ROOT_SCREENSHOT_DIR, package)

    if adb_puller.remote_file_exists(metadata_file):
        adb_puller.pull(metadata_file, join(dir, 'metadata.xml'))
    elif adb_puller.remote_file_exists(old_metadata_file):
        adb_puller.pull(old_metadata_file, join(dir, 'metadata.xml'))
    else:
        create_empty_metadata_file(dir)

def create_empty_metadata_file(dir):
    with open(join(dir, 'metadata.xml'), 'w') as out:
        out.write("""<?xml version="1.0" encoding="UTF-8"?>
<screenshots>
</screenshots>""")

def pull_images(dir, adb_puller):
    root = ET.parse(join(dir, 'metadata.xml')).getroot()
    for s in root.iter('screenshot'):
        filename_nodes = s.findall('absolute_file_name')
        for filename_node in filename_nodes:
            adb_puller.pull(filename_node.text, join(dir, os.path.basename(filename_node.text)))
        dump_node = s.find('view_hierarchy')
        if dump_node is not None:
            adb_puller.pull(dump_node.text, join(dir, os.path.basename(dump_node.text)))

def pull_all(package, dir, adb_puller):
    pull_metadata(package, dir, adb_puller=adb_puller)
    pull_images(dir, adb_puller=adb_puller)

def pull_filtered(package, dir, adb_puller, filter_name_regex=None):
    pull_metadata(package, dir, adb_puller=adb_puller)
    metadata.filter_screenshots(join(dir, 'metadata.xml'), name_regex=filter_name_regex)
    pull_images(dir, adb_puller=adb_puller)

def pull_screenshots(process,
                     adb_puller,
                     temp_dir=None,
                     filter_name_regex=None,
                     record=None,
                     verify=None,
                     opt_generate_png=None):
    temp_dir = temp_dir or tempfile.mkdtemp(prefix='screenshots')
    copy_assets(temp_dir)

    pull_filtered(process, adb_puller=adb_puller, dir=temp_dir, filter_name_regex=filter_name_regex)
    path_to_html = generate_html(temp_dir)

    if record or verify:
        # don't import this early, since we need PIL to import this
        from .recorder import Record
        recorder = Record(temp_dir, record or verify)
        if verify:
            recorder.verify()
        else:
            recorder.record()

    if opt_generate_png:
        generate_png(path_to_html, opt_generate_png)
        shutil.rmtree(temp_dir)
    else:
        print('Open the following url in a browser to view the results: ')
        print('  file://%s' % path_to_html)

def _check_output(args, **kwargs):
    with open(os.devnull) as f:
        kwargs['stderr'] = f
        return subprocess.check_output(args, **kwargs)

def parse_package_line(line):
    """The line looks like this:
    package: name='com.facebook.testing.tests' versionCode='1' versionName=''"""

    for word in line.split():
        if word.startswith("name='"):
            return word[len("name='"):-1]

def get_aapt_bin():
    """Find the binary for aapt from $ANDROID_SDK"""
    android_sdk = os.environ.get('ANDROID_SDK') or os.environ.get('ANDROID_HOME')
    build_tools = os.path.join(android_sdk, 'build-tools')

    all = list(glob.glob(os.path.join(build_tools, '*/aapt')))
    bad = list(glob.glob(os.path.join(build_tools, 'android-*/aapt')))
    good = list(Set(all) - Set(bad))

    good.sort()
    bad.sort()

    if len(good) == 0:
        return bad[-1]

    return good[-1]

def get_package(apk):
    output = _check_output([get_aapt_bin(), 'dump', 'badging', apk], stderr=os.devnull)
    for line in output.split('\n'):
        if line.startswith('package:'):
            return parse_package_line(line)

def main(argv):
    try:
        opt_list, rest_args = getopt.gnu_getopt(
            argv[1:],
            "eds:",
            ["generate-png=", "filter-name-regex=", "apk", "record=", "verify="])
    except getopt.GetoptError, err:
        usage()
        return 2

    if len(rest_args) != 1:
        usage()
        return 2

    process = rest_args[0]  # something like com.facebook.places.tests

    opts = dict(opt_list)

    if "--apk" in opts:
        # treat process as an apk instead
        process = get_package(process)

    puller_args = []
    if "-e" in opts:
        puller_args.append("-e")

    if "-d" in opts:
        puller_args.append("-d")

    if "-s" in opts:
        puller_args += ["-s", opts["-s"]]

    return pull_screenshots(process,
                            filter_name_regex=opts.get('--filter-name-regex'),
                            opt_generate_png=opts.get('--generate-png'),
                            record=opts.get('--record'),
                            verify=opts.get('--verify'),
                            adb_puller=SimplePuller(puller_args))

if __name__ == '__main__':
    sys.exit(main(sys.argv))
<|endoftext|>


<commit_before># Copyright 2007 Andrey Golovizin
#
# This file is part of pybtex.
#
# pybtex is free software; you can redistribute it and/or modify
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# pybtex is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pybtex; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

"""Built-in functions for BibTeX interpreter.

CAUTION: functions should PUSH results, not RETURN
"""

class Builtin(object):
    def __init__(self, f):
        self.f = f
    def execute(self, interpreter):
        self.f(interpreter)

def builtin(f):
    return Builtin(f)

@builtin
def operator_assign(i):
    var = i.pop()
    value = i.pop()
    var.set(value)

@builtin
def operator_plus(i):
    arg1 = i.pop()
    arg2 = i.pop()
    i.push(arg1 + arg2)

@builtin
def operator_more(i):
    arg1 = i.pop()
    arg2 = i.pop()
    if arg2 > arg1:
        i.push(1)
    else:
        i.push(0)

@builtin
def empty(i):
    #FIXME error checking
    s = i.pop()
    if s and not s.isspace():
        i.push(1)
    else:
        i.push(0)

@builtin
def if_(i):
    f1 = i.pop()
    f2 = i.pop()
    p = i.pop()
    if p > 0:
        f1.execute(i)
    else:
        f2.execute(i)

@builtin
def int_to_str(i):
    i.push(str(i.pop()))

@builtin
def preamble(i):
    #FIXME stub
    i.push('')

@builtin
def skip(i):
    pass

@builtin
def width(i):
    #FIXME need to investigate bibtex' source
    s = i.pop()
    i.push(len(s))

builtins = {
        '>': operator_more,
        '+': operator_plus,
        ':=': operator_assign,
        'empty$': empty,
        'if$': if_,
        'int.to.str$': int_to_str,
        'preamble$': preamble,
        'skip$': skip,
        'width$': width,
}
<commit_msg>implemented some meaningful __repr__ for builtins<commit_after># Copyright 2007 Andrey Golovizin
#
# This file is part of pybtex.
#
# pybtex is free software; you can redistribute it and/or modify
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# pybtex is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pybtex; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

"""Built-in functions for BibTeX interpreter.

CAUTION: functions should PUSH results, not RETURN
"""

class Builtin(object):
    def __init__(self, f):
        self.f = f
    def execute(self, interpreter):
        self.f(interpreter)
    def __repr__(self):
        return '<builtin %s>' % self.f.__name__

def builtin(f):
    return Builtin(f)

@builtin
def operator_assign(i):
    var = i.pop()
    value = i.pop()
    var.set(value)

@builtin
def operator_plus(i):
    arg1 = i.pop()
    arg2 = i.pop()
    i.push(arg1 + arg2)

@builtin
def operator_more(i):
    arg1 = i.pop()
    arg2 = i.pop()
    if arg2 > arg1:
        i.push(1)
    else:
        i.push(0)

@builtin
def empty(i):
    #FIXME error checking
    s = i.pop()
    if s and not s.isspace():
        i.push(1)
    else:
        i.push(0)

@builtin
def if_(i):
    f1 = i.pop()
    f2 = i.pop()
    p = i.pop()
    if p > 0:
        f1.execute(i)
    else:
        f2.execute(i)

@builtin
def int_to_str(i):
    i.push(str(i.pop()))

@builtin
def preamble(i):
    #FIXME stub
    i.push('')

@builtin
def skip(i):
    pass

@builtin
def width(i):
    #FIXME need to investigate bibtex' source
    s = i.pop()
    i.push(len(s))

builtins = {
        '>': operator_more,
        '+': operator_plus,
        ':=': operator_assign,
        'empty$': empty,
        'if$': if_,
        'int.to.str$': int_to_str,
        'preamble$': preamble,
        'skip$': skip,
        'width$': width,
}
<|endoftext|>


<commit_before># Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.

import configparser #To read config files.
import io #To output config files to string.

import UM.VersionUpgrade #To indicate that a file is of the wrong format.

##  Creates a new preferences instance by parsing a serialised preferences file
#   in version 1 of the file format.
#
#   \param serialised The serialised form of a preferences file in version 1.
#   \return A representation of those preferences, or None if the file format is
#   incorrect.
def importFrom(serialised):
    try:
        return Preferences(serialised)
    except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
        return None

##  A representation of preferences files as intermediary form for conversion
#   from one format to the other.
class Preferences:
    ##  Reads version 2 of the preferences file format, storing it in memory.
    #
    #   \param serialised A serialised version 2 preferences file.
    def __init__(self, serialised):
        self._config = configparser.ConfigParser(interpolation = None)
        self._config.read_string(serialised)

        #Checking file correctness.
        if not self._config.has_section("general"):
            raise UM.VersionUpgrade.FormatException("No \"general\" section.")
        if not self._config.has_option("general", "version"):
            raise UM.VersionUpgrade.FormatException("No \"version\" in \"general\" section.")
        if int(self._config.get("general", "version")) != 2: # Explicitly hard-code version 2, since if this number changes the programmer MUST change this entire function.
            raise UM.VersionUpgrade.InvalidVersionException("The version of this preferences file is wrong. It must be 2.")
        if self._config.has_option("general", "name"): #This is probably a machine instance.
            raise UM.VersionUpgrade.FormatException("There is a \"name\" field in this configuration file. I suspect it is not a preferences file.")

    ##  Serialises these preferences as a preferences file of version 3.
    #
    #   This is where the actual translation happens.
    #
    #   \return A serialised version of a preferences file in version 3.
    def export(self):
        #Reset the cura/categories_expanded property since it works differently now.
        if self._config.has_section("cura") and self._config.has_option("cura", "categories_expanded"):
            self._config.remove_option("cura", "categories_expanded")

        #Translate the setting names in the visible settings.
        if self._config.has_section("machines") and self._config.has_option("machines", "setting_visibility"):
            visible_settings = self._config.get("machines", "setting_visibility")
            visible_settings = visible_settings.split(",")
            import VersionUpgrade21to22 #Import here to prevent a circular dependency.
            VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettingNames(visible_settings)
            visible_settings = ",".join(visible_settings)
            self._config.set("machines", "setting_visibility", value = visible_settings)

        #Update the version number itself.
        self._config.set("general", "version", value = "3")

        #Output the result as a string.
        output = io.StringIO()
        self._config.write(output)
        return output.getvalue()<commit_msg>Translate active machine setting<commit_after># Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.

import configparser #To read config files.
import io #To output config files to string.

import UM.VersionUpgrade #To indicate that a file is of the wrong format.

##  Creates a new preferences instance by parsing a serialised preferences file
#   in version 1 of the file format.
#
#   \param serialised The serialised form of a preferences file in version 1.
#   \return A representation of those preferences, or None if the file format is
#   incorrect.
def importFrom(serialised):
    try:
        return Preferences(serialised)
    except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
        return None

##  A representation of preferences files as intermediary form for conversion
#   from one format to the other.
class Preferences:
    ##  Reads version 2 of the preferences file format, storing it in memory.
    #
    #   \param serialised A serialised version 2 preferences file.
    def __init__(self, serialised):
        self._config = configparser.ConfigParser(interpolation = None)
        self._config.read_string(serialised)

        #Checking file correctness.
        if not self._config.has_section("general"):
            raise UM.VersionUpgrade.FormatException("No \"general\" section.")
        if not self._config.has_option("general", "version"):
            raise UM.VersionUpgrade.FormatException("No \"version\" in \"general\" section.")
        if int(self._config.get("general", "version")) != 2: # Explicitly hard-code version 2, since if this number changes the programmer MUST change this entire function.
            raise UM.VersionUpgrade.InvalidVersionException("The version of this preferences file is wrong. It must be 2.")
        if self._config.has_option("general", "name"): #This is probably a machine instance.
            raise UM.VersionUpgrade.FormatException("There is a \"name\" field in this configuration file. I suspect it is not a preferences file.")

    ##  Serialises these preferences as a preferences file of version 3.
    #
    #   This is where the actual translation happens.
    #
    #   \return A serialised version of a preferences file in version 3.
    def export(self):
        #Reset the cura/categories_expanded property since it works differently now.
        if self._config.has_section("cura") and self._config.has_option("cura", "categories_expanded"):
            self._config.remove_option("cura", "categories_expanded")

        #Translate the setting names in the visible settings.
        if self._config.has_section("machines") and self._config.has_option("machines", "setting_visibility"):
            visible_settings = self._config.get("machines", "setting_visibility")
            visible_settings = visible_settings.split(",")
            import VersionUpgrade21to22 #Import here to prevent a circular dependency.
            VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettingNames(visible_settings)
            visible_settings = ",".join(visible_settings)
            self._config.set("machines", "setting_visibility", value = visible_settings)

        #Translate the active_instance key.
        if self._config.has_section("machines") and self._config.has_option("machines", "active_instance"):
            active_machine = self._config.get("machines", "active_instance")
            self._config.remove_option("machines", "active_instance")
            self._config.set("cura", "active_machine", active_machine)

        #Update the version number itself.
        self._config.set("general", "version", value = "3")

        #Output the result as a string.
        output = io.StringIO()
        self._config.write(output)
        return output.getvalue()<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================
import transaction

from zeo_connector import transaction_manager
from zeo_connector.examples import DatabaseHandler

class CacheHandler(DatabaseHandler):
    """
    """
    def __init__(self, conf_path, project_key):
        super(self.__class__, self).__init__(
            conf_path=conf_path,
            project_key=project_key
        )

        # read the proper index
        self.cache_key = "cache"
        self.users = self._get_key_or_create(self.cache_key)

    def add_upload_request(metadata, bds_id):
        pass

    def get_upload_request():
        pass
<commit_msg>#22: Added sketch of the CacheDB functionality.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
# Imports =====================================================================
import time
from functools import total_ordering

import transaction
from persistent import Persistent

from zeo_connector import transaction_manager
from zeo_connector.examples import DatabaseHandler

from BalancedDiscStorage import BalancedDiscStorage

from .. import settings

# Functions & classes =========================================================
@total_ordering
class UploadRequest(Persistent):
    def __init__(self, metadata, bds_id):
        self.metadata = metadata
        self.bds_id = bds_id
        self.created = time.time()

    def get_file_obj(self):
        bds = BalancedDiscStorage(settings.WEB_CACHE)

        with transaction.manager:
            return open(bds.file_path_from_hash(self.bds_id), "rb")

    def remove_file(self):
        bds = BalancedDiscStorage(settings.WEB_CACHE)

        bds.delete_by_hash(self.bds_id)

    def __eq__(self, obj):
        return self.bds_id == obj.bds_id

    def __lt__(self, obj):
        return float(self.created).__lt__(obj.created)

class CacheHandler(DatabaseHandler):
    """
    """
    def __init__(self, conf_path, project_key):
        super(self.__class__, self).__init__(
            conf_path=conf_path,
            project_key=project_key
        )

        # read the proper index
        self.cache_key = "cache"
        self.cache = self._get_key_or_create(self.cache_key)

    @transaction_manager
    def add_upload_request(self, metadata, bds_id):
        self.cache[bds_id] = UploadRequest(metadata, bds_id)

    @transaction_manager
    def has_upload_request(self):
        return len(self.cache.keys()) > 0

    @transaction_manager
    def get_upload_request(self):
        if not self.cache.keys():
            raise ValueError("There is no cached upload request.")

        oldest = min(self.cache.values(), key=lambda x: x.created)

        return oldest

    def pop_upload_requst(self):
        if not self.cache.keys():
            return None

        oldest = min(self.cache.values(), key=lambda x: x.created)

        del self.cache[oldest.bds_id]

        return oldest
<|endoftext|>


<commit_before>##############################################################################
#
# Copyright (c) 2013 Rijk Stofberg
# All Rights Reserved.
#
# This software is subject to the provisions of the MIT License (MIT),
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
##############################################################################

from sqlalchemy import (
    Column,
    Sequence,
    Integer,
    Text,
    )

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    )

from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()

class Order(Base):
    __tablename__ = 'order'
    id = Column(Integer,
                Sequence('order_seq_id', optional=True),
                primary_key=True,)
    description = Column(Text)
    value = Column(Integer)
    external_reference_number = Column(Text, unique=True)
    status = Column(Text)

    def __init__(self, description, value, external_reference_number=''):
        self.description = description
        self.value = value
        self.external_reference_number = external_reference_number
        self.status = 'order_pending'

    def formatted_order_number(self):
        """ Format the id by prepending the string 'order_' and left padding the
            actual id up to 4 characters with '0'.
            e.g. An order with the id '1' will be formatted:
            'order_0001'
        """
        if self.id is None:
            return ''
        return 'order_{id:04}'.format(id=self.id)

    def as_dict(self):
        return dict(
            description = self.description,
            value = self.value,
            display_value = self.format_value(),
            order_number = self.formatted_order_number(),
            external_reference_number = self.external_reference_number,
        )

    def format_value(self):
        return 'R {0:0.2f}'.format(self.value/100.00)

    @classmethod
    def by_id(self, id):
        order = DBSession.query(Order).filter(Order.id == id)
        return order.first()

    @classmethod
    def by_external_reference_number(self, external_reference_number):
        orders = DBSession.query(Order).filter(
            Order.external_reference_number == external_reference_number)
        return orders.first()

    @classmethod
    def next_order_number(self, order_number):
        results = DBSession.execute('select max("id") from "order"')
        return results.first()[0] +1

class Product(Base):
    __tablename__ = 'product'
    id = Column(Integer,
                Sequence('product_seq_id', optional=True),
                primary_key=True,)
    description = Column(Text)
    price = Column(Integer)

    def __init__(self, description, price):
        self.description = description
        self.price = price

    def format_price(self):
        return 'R {0:0.2f}'.format(self.price/100.00)

    @classmethod
    def by_id(self, id):
        order = DBSession.query(Product).filter(Order.id == id)
        return order.first()
<commit_msg>Fix bug in look by id<commit_after>##############################################################################
#
# Copyright (c) 2013 Rijk Stofberg
# All Rights Reserved.
#
# This software is subject to the provisions of the MIT License (MIT),
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
##############################################################################

from sqlalchemy import (
    Column,
    Sequence,
    Integer,
    Text,
    )

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    )

from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()

class Order(Base):
    __tablename__ = 'order'
    id = Column(Integer,
                Sequence('order_seq_id', optional=True),
                primary_key=True,)
    description = Column(Text)
    value = Column(Integer)
    external_reference_number = Column(Text, unique=True)
    status = Column(Text)

    def __init__(self, description, value, external_reference_number=''):
        self.description = description
        self.value = value
        self.external_reference_number = external_reference_number
        self.status = 'order_pending'

    def formatted_order_number(self):
        """ Format the id by prepending the string 'order_' and left padding the
            actual id up to 4 characters with '0'.
            e.g. An order with the id '1' will be formatted:
            'order_0001'
        """
        if self.id is None:
            return ''
        return 'order_{id:04}'.format(id=self.id)

    def as_dict(self):
        return dict(
            description = self.description,
            value = self.value,
            display_value = self.format_value(),
            order_number = self.formatted_order_number(),
            external_reference_number = self.external_reference_number,
        )

    def format_value(self):
        return 'R {0:0.2f}'.format(self.value/100.00)

    @classmethod
    def by_id(self, id):
        order = DBSession.query(Order).filter(Order.id == id)
        return order.first()

    @classmethod
    def by_external_reference_number(self, external_reference_number):
        orders = DBSession.query(Order).filter(
            Order.external_reference_number == external_reference_number)
        return orders.first()

    @classmethod
    def next_order_number(self, order_number):
        results = DBSession.execute('select max("id") from "order"')
        return results.first()[0] +1

class Product(Base):
    __tablename__ = 'product'
    id = Column(Integer,
                Sequence('product_seq_id', optional=True),
                primary_key=True,)
    description = Column(Text)
    price = Column(Integer)

    def __init__(self, description, price):
        self.description = description
        self.price = price

    def format_price(self):
        return 'R {0:0.2f}'.format(self.price/100.00)

    @classmethod
    def by_id(self, id):
        order = DBSession.query(Product).filter(Product.id == id)
        return order.first()
<|endoftext|>


<commit_before>import pytest
import os
from pycket.expand import expand, to_ast
from pycket.interpreter import *
from pycket.values import *
from pycket.prims import *

stdlib_fn = os.path.join(os.path.dirname(os.path.dirname(__file__)), "stdlib.sch")

def run_fix(p,v):
    e = expand(p)
    ast = to_ast(e)
    val = interpret_one(ast)
    assert isinstance(val, W_Fixnum)
    assert val.value == v

def run(p,v):
    e = expand(p)
    ast = to_ast(e)
    val = interpret_one(ast)
    assert equal_loop(val,v)

with file(stdlib_fn) as f:
     stdlib = f.read()

def run_top(p,v=None):
    pp = "(let () \n %s \n %s\n %s)"%(stdlib, p, "" if v else "(void)")
    e = expand(pp)
    ast = to_ast(e)
    val = interpret([ast])
    if v:
        assert equal_loop(val,v)

def test_constant():
    prog = "1"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 1

def test_read_err ():
    with pytest.raises(Exception):
        expand ("(")
    with pytest.raises(Exception):
        expand ("1 2")
    with pytest.raises(Exception):
        expand ("(1 2) 3")

def test_plus():
    prog = "(+ 2 3)"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 5

def test_thunk():
    prog = "((lambda () 1))"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 1

def test_thunk2():
    prog = "((lambda () 1 2))"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 2

def test_call():
    prog = "((lambda (x) (+ x 1)) 2)"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 3

def test_curry():
    prog = "(((lambda (y) (lambda (x) (+ x y))) 2) 3)"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 5

def test_arith():
    run_fix("(+ 1 2)", 3)
    run_fix("(* 1 2)", 2)
    run_fix("(- 1 2)", -1)
    run_fix("(* -1 2)", -2)

def test_letrec():
    run_fix("(letrec ([x 1]) x)", 1)
    run_fix("(letrec ([x 1] [y 2]) y)", 2)
    run_fix("(letrec ([x 1] [y 2]) (+ x y))", 3)
    run_fix("(let ([x 0]) (letrec ([x 1] [y x]) (+ x y)))", 2)
    run_fix("(letrec ([x (lambda (z) x)]) 2)", 2)

def test_let():
    run_fix("(let () 1)", 1)
    run_fix("(let ([x 1]) x)", 1)
    run_fix("(let ([x 1] [y 2]) y)", 2)
    run_fix("(let ([x 1] [y 2]) (+ x y))", 3)
    run_fix("(let ([x 0]) (let ([x 1] [y x]) (+ x y)))", 1)

def test_fac():
    run_fix("(letrec ([fac (lambda (n) (if (= n 0) 1 (* n (fac (- n 1)))))]) (fac 5))", 120)

def test_fib():
    run_fix("(letrec ([fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 2))", 2)
    run_fix("(letrec ([fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 3))", 3)

def test_void():
    run ("(void)", w_void)
    run ("(void 1)", w_void)
    run ("(void 2 3 #true)", w_void)

def test_cons():
    run_fix ("(car (cons 1 2))", 1)
    run_fix ("(cdr (cons 1 2))", 2)

def test_set_car():
    run_fix ("(letrec ([x (cons 1 2)]) (set-car! x 3) (car x))", 3)
    run_fix ("(letrec ([x (cons 1 2)]) (set-cdr! x 3) (cdr x))", 3)

def test_set_bang():
    run("((lambda (x) (set! x #t) x) 1)", w_true)
    run("(letrec([x 0]) ((lambda (x) (set! x #t) x) 1))", w_true)

def test_bools():
    run ("#t", w_true)
    run ("#true", w_true)
    run ("#T", w_true)
    run ("#f", w_false)
    run ("#false", w_false)
    run ("#F", w_false)
    run ("true", w_true)
    run ("false", w_false)

def test_lists():
    run ("null", w_null)
    run ("(list)", w_null)
    run ("(list #t)", to_list ([w_true]))

def test_fib():
    Y = """
  (lambda (f)
    ((lambda (x) (x x))
     (lambda (g)
       (f (lambda (z) ((g g) z))))))
"""
    fac = """
    (lambda (f)
      (lambda (x)
        (if (< x 2)
            1
            (* x (f (- x 1))))))
 """

    fib = """
    (lambda (f)
      (lambda (x)
        (if (< x 2)
            x
            (+ (f (- x 1)) (f (- x 2))))))
"""
    run_fix("((%s %s) 2)"%(Y,fib), 1)
    run_fix("((%s %s) 2)"%(Y,fac), 2)

def test_vararg():
    run_fix ("((lambda x (car x)) 1)", 1)
    run_fix ("((lambda (a . x) a) 1)", 1)
    run ("((lambda (a . x) x) 1)", w_null)

def test_callcc():
    run_fix ("(call/cc (lambda (k) 1))", 1)
    run_fix ("(+ 1 (call/cc (lambda (k) 1)))", 2)
    run_fix ("(+ 1 (call/cc (lambda (k) (k 1))))", 2)
    run_fix ("(+ 1 (call/cc (lambda (k) (+ 5 (k 1)))))", 2)

def test_define():
    run_top("(define x 1) x", W_Fixnum(1))
<commit_msg>a bug in -<commit_after>import pytest
import os
from pycket.expand import expand, to_ast
from pycket.interpreter import *
from pycket.values import *
from pycket.prims import *

stdlib_fn = os.path.join(os.path.dirname(os.path.dirname(__file__)), "stdlib.sch")

def run_fix(p,v):
    e = expand(p)
    ast = to_ast(e)
    val = interpret_one(ast)
    assert isinstance(val, W_Fixnum)
    assert val.value == v

def run(p,v):
    e = expand(p)
    ast = to_ast(e)
    val = interpret_one(ast)
    assert equal_loop(val,v)

with file(stdlib_fn) as f:
     stdlib = f.read()

def run_top(p,v=None):
    pp = "(let () \n %s \n %s\n %s)"%(stdlib, p, "" if v else "(void)")
    e = expand(pp)
    ast = to_ast(e)
    val = interpret([ast])
    if v:
        assert equal_loop(val,v)

def test_constant():
    prog = "1"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 1

def test_read_err ():
    with pytest.raises(Exception):
        expand ("(")
    with pytest.raises(Exception):
        expand ("1 2")
    with pytest.raises(Exception):
        expand ("(1 2) 3")

def test_plus():
    prog = "(+ 2 3)"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 5

def test_thunk():
    prog = "((lambda () 1))"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 1

def test_thunk2():
    prog = "((lambda () 1 2))"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 2

def test_call():
    prog = "((lambda (x) (+ x 1)) 2)"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 3

def test_curry():
    prog = "(((lambda (y) (lambda (x) (+ x y))) 2) 3)"
    val = interpret_one(to_ast(expand(prog)))
    assert isinstance(val, W_Fixnum)
    assert val.value == 5

def test_arith():
    run_fix("(+ 1 2)", 3)
    run_fix("(* 1 2)", 2)
    run_fix("(- 1 2)", -1)
    run_fix("(* -1 2)", -2)

@pytest.mark.xfail
def test_arith_minus_one_arg_bug():
    run_fix("(- 1)", -1)

def test_letrec():
    run_fix("(letrec ([x 1]) x)", 1)
    run_fix("(letrec ([x 1] [y 2]) y)", 2)
    run_fix("(letrec ([x 1] [y 2]) (+ x y))", 3)
    run_fix("(let ([x 0]) (letrec ([x 1] [y x]) (+ x y)))", 2)
    run_fix("(letrec ([x (lambda (z) x)]) 2)", 2)

def test_let():
    run_fix("(let () 1)", 1)
    run_fix("(let ([x 1]) x)", 1)
    run_fix("(let ([x 1] [y 2]) y)", 2)
    run_fix("(let ([x 1] [y 2]) (+ x y))", 3)
    run_fix("(let ([x 0]) (let ([x 1] [y x]) (+ x y)))", 1)

def test_fac():
    run_fix("(letrec ([fac (lambda (n) (if (= n 0) 1 (* n (fac (- n 1)))))]) (fac 5))", 120)

def test_fib():
    run_fix("(letrec ([fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 2))", 2)
    run_fix("(letrec ([fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 3))", 3)

def test_void():
    run ("(void)", w_void)
    run ("(void 1)", w_void)
    run ("(void 2 3 #true)", w_void)

def test_cons():
    run_fix ("(car (cons 1 2))", 1)
    run_fix ("(cdr (cons 1 2))", 2)

def test_set_car():
    run_fix ("(letrec ([x (cons 1 2)]) (set-car! x 3) (car x))", 3)
    run_fix ("(letrec ([x (cons 1 2)]) (set-cdr! x 3) (cdr x))", 3)

def test_set_bang():
    run("((lambda (x) (set! x #t) x) 1)", w_true)
    run("(letrec([x 0]) ((lambda (x) (set! x #t) x) 1))", w_true)

def test_bools():
    run ("#t", w_true)
    run ("#true", w_true)
    run ("#T", w_true)
    run ("#f", w_false)
    run ("#false", w_false)
    run ("#F", w_false)
    run ("true", w_true)
    run ("false", w_false)

def test_lists():
    run ("null", w_null)
    run ("(list)", w_null)
    run ("(list #t)", to_list ([w_true]))

def test_fib():
    Y = """
  (lambda (f)
    ((lambda (x) (x x))
     (lambda (g)
       (f (lambda (z) ((g g) z))))))
"""
    fac = """
    (lambda (f)
      (lambda (x)
        (if (< x 2)
            1
            (* x (f (- x 1))))))
 """

    fib = """
    (lambda (f)
      (lambda (x)
        (if (< x 2)
            x
            (+ (f (- x 1)) (f (- x 2))))))
"""
    run_fix("((%s %s) 2)"%(Y,fib), 1)
    run_fix("((%s %s) 2)"%(Y,fac), 2)

def test_vararg():
    run_fix ("((lambda x (car x)) 1)", 1)
    run_fix ("((lambda (a . x) a) 1)", 1)
    run ("((lambda (a . x) x) 1)", w_null)

def test_callcc():
    run_fix ("(call/cc (lambda (k) 1))", 1)
    run_fix ("(+ 1 (call/cc (lambda (k) 1)))", 2)
    run_fix ("(+ 1 (call/cc (lambda (k) (k 1))))", 2)
    run_fix ("(+ 1 (call/cc (lambda (k) (+ 5 (k 1)))))", 2)

def test_define():
    run_top("(define x 1) x", W_Fixnum(1))
<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Assorted test helpers
#
import pytest
import os
from tempfile import NamedTemporaryFile, gettempdir
from pycket.expand import expand, JsonLoader, expand_string, ModTable, parse_module
from pycket.pycket_json import loads
from pycket.interpreter import *
from pycket.env import ToplevelEnv
from pycket import values

from pycket.racket_entry import initiate_boot_sequence, namespace_require_kernel, read_eval_print_string, get_primitive
from pycket.prims.linklet import *
from pycket.test.utils import *
from pycket.config import get_testing_config

#
# basic runners
#

# This is where all the work happens

delete_temp_files = True

##############################
## NEW Pycket test helpers
##############################

instantiate_linklet = get_primitive("instantiate-linklet")

# This is where all the work happens

if pytest.config.load_expander:
    # get the expander
    print("Loading and initializing the expander")
    initiate_boot_sequence(None, [], False)
    # load the '#%kernel
    print("(namespace-require '#%%kernel)")
    namespace_require_kernel(None)

def run_sexp(body_sexp_str, v=None, just_return=False, extra="", equal_huh=False):
    linkl_str = "(linklet () () %s %s)" % (extra, body_sexp_str)
    l = make_linklet(linkl_str)
    result, _ = eval(l, empty_target(), just_return=just_return)

    if just_return:
        return result

    return check_result(result, v, equal_huh)

def run_string(expr_str, v=None, just_return=False, equal_huh=False):
    # FIXME : removing \n is not ideal, as the test itself may have one
    expr_str = expr_str.replace('\n', '') # remove the newlines added by the multi line doctest
    expr_str = "(begin %s)" % expr_str
    result = read_eval_print_string(expr_str, None, return_val=True)

    # FIXME: check for multiple results
    assert isinstance(result, W_Object)
    if just_return:
        return result

    return check_result(result, v, equal_huh)

def run_expr_result(expr_str):
    return run_expr(expr_str, just_return=True)

def run_expr(expr_str, v=None, just_return=False, extra="", equal_huh=False):
    expr_str = extra + expr_str
    if pytest.config.load_expander:
        return run_string(expr_str, v, just_return, equal_huh=equal_huh)
    else:
        return run_sexp(expr_str, v, just_return, equal_huh=equal_huh)

def check_result(result, expected, equal_huh=False):
    if equal_huh:
        assert result.equal(expected)
        return result

    if isinstance(result, W_Fixnum) or isinstance(result, W_Flonum) or isinstance(result, W_Bignum):
        check = result.value
    elif isinstance(result, values_string.W_String):
        check = result.as_str_utf8()
    elif result is w_true:
        check = True
    elif result is w_false:
        check = False
    elif isinstance(result, W_HashTable):
        check = result
    else:
        raise Exception("I don't know this type yet : %s -- actual value: %s" % (result, result.tostring()))

    assert check == expected

    return result

### Linklet Utils ###

def eval(linkl, target, imports=[], just_return=False):
    result = instantiate_linklet.call_interpret([linkl, to_list(imports), target, w_false], get_testing_config())
    return result, target

def eval_fixnum(linkl, target, imports=[], just_return=False):
    r, t = eval(linkl, target, imports=imports, just_return=just_return)
    if isinstance(r, W_Fixnum):
        r = r.value
    # o/w don't care it's not used
    return r, t

def get_var_val(inst, id_str):
    # for getting uninterned symbols
    for k,v in inst.vars.iteritems():
        if id_str == k.tostring():
            return k, v.get_value_direct()
    raise Exception("Can't find the variable : %s in instance : %s" % (id_str, inst.tostring()))

def variables(inst):
    return get_instance_variable_names(inst) # W_Cons

def defines(inst, name_str):
    return inst.has_var(W_Symbol.make(name_str))

def get_val(inst, name_str):
    return inst.lookup_var_value(values.W_Symbol.make(name_str)).get_val()

def check_val(inst, var_str, val):
    return get_val(inst, var_str).value == val

def inst(linkl, imports=[], target=None):
    if not target:
        target = w_false

    return instantiate_linklet.call_interpret([linkl, to_list(imports), target, w_false], get_testing_config())

def empty_target(l_name="test_empty_instance"):
    # creates an empty target
    return make_instance("(linklet () ())", l_name=l_name)

def make_instance(linkl_str, imports=[], l_name="test_linklet_sexp"):
    instance = inst(make_linklet(linkl_str, l_name), imports)
    return instance

def make_linklet(linkl_str, l_name="test_linklet_sexp"):
    #"(linklet () (x) (define-values (x) 4))"
    linkl_sexp = string_to_sexp(linkl_str)
    try:
        do_compile_linklet(linkl_sexp, values.W_Symbol.make(l_name), w_false, w_false, w_false, ToplevelEnv(), NilCont())
    except Done, e:
        l = e.values # W_Linklet
        return l
    raise Exception("do_compile_linklet didn't raised a Done exception")

##############################
## OLD Pycket test helpers
##############################

def run_ast(ast):
    env = ToplevelEnv()
    env.globalconfig.load(ast)
    mod = interpret_module(ast, env)
    return mod

def expand_from_bytecode(m, srcloc):
    with NamedTemporaryFile(delete=delete_temp_files) as f:
        f.write(m.encode("utf-8"))
        f.seek(0)
        os.rename(f.name, f.name+'.rkt')
        s = expand_string(m.encode("utf-8"), reuse=False, srcloc=srcloc, byte_option=True, tmp_file_name=f.name)
        os.rename(f.name+'.rkt', f.name) # for automatic deletion to find it

        if delete_temp_files: # removing the compiled/*.dep && *.zo as well
            subs = f.name.split("/")
            parent = subs[:-1]
            parent.append('compiled')
            file_name = subs[-1]
            byteCodesPrefix = "/".join(parent) + "/" + file_name + "_rkt"
            os.remove(byteCodesPrefix + ".zo")
            os.remove(byteCodesPrefix + ".dep")

    return s

def run_mod(m, stdlib=False, srcloc=True):
    assert not stdlib

    if not pytest.config.byte_option:
        ast = parse_module(expand_string(m, srcloc=srcloc))
    else:
        ast = parse_module(expand_from_bytecode(m, srcloc), bytecode_expand=True)

    return run_ast(ast)

def format_pycket_mod(s, stdlib=False, extra=""):
    # pycket handles the stdlib, various requires
    str = "#lang pycket%s\n%s\n%s"%(" #:stdlib" if stdlib else "", extra, s)
    return str

def run_mod_defs(m, extra="",stdlib=False, srcloc=True):
    str = format_pycket_mod(m, extra=extra, stdlib=stdlib)
    mod = run_mod(str, srcloc=srcloc)
    return mod

def run_mod_expr(e, v=None, stdlib=False, wrap=False, extra="", srcloc=False):
    # this (let () e) wrapping is needed if e is `(begin (define x 1) x)`, for example
    # FIXME: this should get moved into a language
    expr = "(let () %s)"%e if wrap else e
    defn = "(define #%%pycket-expr %s)"%expr
    mod = run_mod_defs(defn, stdlib=stdlib, extra=extra, srcloc=srcloc)
    ov = mod.defs[values.W_Symbol.make("#%pycket-expr")]
    if v:
        assert ov.equal(v)
    return ov

def execute(p, stdlib=False, extra=""):
    return run_mod_expr(p, stdlib=stdlib, extra=extra)

def run_fix(p, v=None, stdlib=False, extra="", equal_huh=False):
    new = pytest.config.new_pycket
    if new:
        return run_expr(p, v, equal_huh=equal_huh)
    ov = run_mod_expr(p,stdlib=stdlib, extra=extra)
    assert isinstance(ov, values.W_Fixnum)
    if v is not None:
        assert ov.value == v
    return ov.value

def run_flo(p, v=None, stdlib=False, extra=""):
    ov = run_mod_expr(p,stdlib=stdlib, extra=extra)
    assert isinstance(ov, values.W_Flonum)
    if v is not None:
        assert ov.value == v
    return ov.value

def run(p, v=None, stdlib=False, extra=""):
    if pytest.config.new_pycket:
        return run_expr_result(p)
    return run_mod_expr(p,v=v,stdlib=stdlib, extra=extra)

def run_top(p, v=None, stdlib=False, extra=""):
    return run_mod_expr(p,v=v,stdlib=stdlib, wrap=True, extra=extra)

def run_values(p, stdlib=False):
    e = "(call-with-values (lambda () %s) list)"%p
    v = run_mod_expr(e, stdlib=stdlib)
    return values.from_list(v)

def run_std(c, v):
    return run_top(c, v, stdlib=True)

def run_file(fname, *replacements, **kwargs):
    modname = os.path.join(os.path.dirname(__file__), fname)
    ast = parse_file(fname, *replacements, **kwargs)
    env = ToplevelEnv()
    env.globalconfig.load(ast)
    env.module_env.add_module(modname, ast)
    return interpret_module(ast, env)

def parse_file(fname, *replacements, **kwargs):
    abspath = kwargs.get("abspath", False)
    if not abspath:
        fname = os.path.join(os.path.dirname(__file__), fname)

    if kwargs.get("inplace", False):
        assert not replacements
        if not pytest.config.byte_option:
            reader = JsonLoader(bytecode_expand=False)
        else:
            reader = JsonLoader(bytecode_expand=True)
        ast = reader.expand_to_ast(fname)
        return ast

    with file(fname) as f:
        s = f.read()
    for replace, with_ in replacements:
        assert s.count(replace) == 1
        s = s.replace(replace, with_)
    s = s.decode("utf-8")

    if not pytest.config.byte_option:
        s = expand_string(s)
        ast = parse_module(s)
    else:
        s = expand_from_bytecode(s, True)
        ast = parse_module(s, bytecode_expand=True)

    return ast

#
# Combined checking
#
def check_all(*snippets_returning_true, **kwargs):
    code = []
    tail = []
    for i, snippet in enumerate(snippets_returning_true):
        code.append("  " * i + "(if %s" % snippet)
        tail.append("  " * (i + 1) + "%s)" % i)
    code.append("  " * (i + 1) + "#t")
    code = "\n".join(code) + "\n" + "\n".join(reversed(tail))
    print code
    res = execute(code, extra=kwargs.get("extra", ""))
    if res is not values.w_true:
        assert 0, "%s returned a non-true value" % snippets_returning_true[res.value]

def check_none(*snippets_returning_false, **kwargs):
    code = []
    tail = []
    for i, snippet in enumerate(snippets_returning_false):
        code.append("  " * i + "(if %s %s" % (snippet, i))
        tail.append("  " * (i + 1) + ")")
    code.append("  " * (i + 1) + "#t")
    code = "\n".join(code) + "\n" + "\n".join(reversed(tail))
    print code
    res = execute(code, extra=kwargs.get("extra", ""))
    if res is not values.w_true:
        assert 0, "%s returned a true value" % snippets_returning_false[res.value]

def check_equal(*pairs_of_equal_stuff, **kwargs):
    code = []
    tail = []
    assert len(pairs_of_equal_stuff) % 2 == 0
    ind = 0
    for i in range(len(pairs_of_equal_stuff) // 2):
        a = pairs_of_equal_stuff[i * 2]
        b = pairs_of_equal_stuff[i * 2 + 1]
        ind += 1
        if isinstance(a, list):
            code.append("  " * ind + "(let ()")
            ind += 1
            code.extend(["  " * ind + x for x in a[:-1]])
            code.append( "  " * ind + ("(if (equal? %s %s)" % (a[-1], b)))
            ind += 1
            tail.append("  " * ind + "%s))" % i)
        else:
            snippet = "(equal? %s %s)" % (a, b)
            code.append("  " * ind + "(if %s" % snippet)
            tail.append("  " * (ind + 1) + "%s)" % i)
    code.append("  " * (ind + 1) + "#t")
    code = "\n".join(code) + "\n" + "\n".join(reversed(tail))
    try:
        print code
    except UnicodeEncodeError:
        print code.encode("ascii", 'replace')

    extra = kwargs.get("extra", "")
    res = execute(code, extra=extra)
    if res is not values.w_true:
        src = pairs_of_equal_stuff[res.value * 2]
        if isinstance(src, list):
            src = "(let ()\n" + "\n".join(src) + ")"
        wrong = execute(src, extra=extra)
        assert 0, u"%s is %s, which is different from %s" % (
            pairs_of_equal_stuff[res.value * 2], wrong.tostring(),
            pairs_of_equal_stuff[res.value * 2 + 1])
<commit_msg>imports for the new pycket testing<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Assorted test helpers
#
import pytest
import os
from tempfile import NamedTemporaryFile, gettempdir
from pycket.expand import expand, JsonLoader, expand_string, ModTable, parse_module
from pycket.pycket_json import loads
from pycket.interpreter import *
from pycket.env import ToplevelEnv
from pycket import values
from pycket.error import SchemeException
from pycket.cont import continuation
from pycket.values import to_list, w_false, w_true, W_Fixnum, W_Object, W_Flonum, W_Void, w_null
from pycket.hash.base import W_HashTable
from pycket.racket_entry import initiate_boot_sequence, namespace_require_kernel, read_eval_print_string, get_primitive
from pycket.prims.linklet import *
from pycket.test.utils import *
from pycket.config import get_testing_config

#
# basic runners
#

# This is where all the work happens

delete_temp_files = True

##############################
## NEW Pycket test helpers
##############################

instantiate_linklet = get_primitive("instantiate-linklet")

# This is where all the work happens

if pytest.config.load_expander:
    # get the expander
    print("Loading and initializing the expander")
    initiate_boot_sequence(None, [], False)
    # load the '#%kernel
    print("(namespace-require '#%%kernel)")
    namespace_require_kernel(None)

def run_sexp(body_sexp_str, v=None, just_return=False, extra="", equal_huh=False):
    linkl_str = "(linklet () () %s %s)" % (extra, body_sexp_str)
    l = make_linklet(linkl_str)
    result, _ = eval(l, empty_target(), just_return=just_return)

    if just_return:
        return result

    return check_result(result, v, equal_huh)

def run_string(expr_str, v=None, just_return=False, equal_huh=False):
    # FIXME : removing \n is not ideal, as the test itself may have one
    expr_str = expr_str.replace('\n', '') # remove the newlines added by the multi line doctest
    expr_str = "(begin %s)" % expr_str
    result = read_eval_print_string(expr_str, None, return_val=True)

    # FIXME: check for multiple results
    assert isinstance(result, W_Object)
    if just_return:
        return result

    return check_result(result, v, equal_huh)

def run_expr_result(expr_str):
    return run_expr(expr_str, just_return=True)

def run_expr(expr_str, v=None, just_return=False, extra="", equal_huh=False):
    expr_str = extra + expr_str
    if pytest.config.load_expander:
        return run_string(expr_str, v, just_return, equal_huh=equal_huh)
    else:
        return run_sexp(expr_str, v, just_return, equal_huh=equal_huh)

def check_result(result, expected, equal_huh=False):
    if equal_huh:
        assert result.equal(expected)
        return result

    if isinstance(result, W_Fixnum) or isinstance(result, W_Flonum) or isinstance(result, W_Bignum):
        check = result.value
    elif isinstance(result, values_string.W_String):
        check = result.as_str_utf8()
    elif result is w_true:
        check = True
    elif result is w_false:
        check = False
    elif isinstance(result, W_HashTable):
        check = result
    else:
        raise Exception("I don't know this type yet : %s -- actual value: %s" % (result, result.tostring()))

    assert check == expected

    return result

### Linklet Utils ###

def eval(linkl, target, imports=[], just_return=False):
    result = instantiate_linklet.call_interpret([linkl, to_list(imports), target, w_false], get_testing_config())
    return result, target

def eval_fixnum(linkl, target, imports=[], just_return=False):
    r, t = eval(linkl, target, imports=imports, just_return=just_return)
    if isinstance(r, W_Fixnum):
        r = r.value
    # o/w don't care it's not used
    return r, t

def get_var_val(inst, id_str):
    # for getting uninterned symbols
    for k,v in inst.vars.iteritems():
        if id_str == k.tostring():
            return k, v.get_value_direct()
    raise Exception("Can't find the variable : %s in instance : %s" % (id_str, inst.tostring()))

def variables(inst):
    return get_instance_variable_names(inst) # W_Cons

def defines(inst, name_str):
    return inst.has_var(W_Symbol.make(name_str))

def get_val(inst, name_str):
    return inst.lookup_var_value(values.W_Symbol.make(name_str)).get_val()

def check_val(inst, var_str, val):
    return get_val(inst, var_str).value == val

def inst(linkl, imports=[], target=None):
    if not target:
        target = w_false

    return instantiate_linklet.call_interpret([linkl, to_list(imports), target, w_false], get_testing_config())

def empty_target(l_name="test_empty_instance"):
    # creates an empty target
    return make_instance("(linklet () ())", l_name=l_name)

def make_instance(linkl_str, imports=[], l_name="test_linklet_sexp"):
    instance = inst(make_linklet(linkl_str, l_name), imports)
    return instance

def make_linklet(linkl_str, l_name="test_linklet_sexp"):
    #"(linklet () (x) (define-values (x) 4))"
    linkl_sexp = string_to_sexp(linkl_str)
    try:
        do_compile_linklet(linkl_sexp, values.W_Symbol.make(l_name), w_false, w_false, w_false, ToplevelEnv(), NilCont())
    except Done, e:
        l = e.values # W_Linklet
        return l
    raise Exception("do_compile_linklet didn't raised a Done exception")

##############################
## OLD Pycket test helpers
##############################

def run_ast(ast):
    env = ToplevelEnv()
    env.globalconfig.load(ast)
    mod = interpret_module(ast, env)
    return mod

def expand_from_bytecode(m, srcloc):
    with NamedTemporaryFile(delete=delete_temp_files) as f:
        f.write(m.encode("utf-8"))
        f.seek(0)
        os.rename(f.name, f.name+'.rkt')
        s = expand_string(m.encode("utf-8"), reuse=False, srcloc=srcloc, byte_option=True, tmp_file_name=f.name)
        os.rename(f.name+'.rkt', f.name) # for automatic deletion to find it

        if delete_temp_files: # removing the compiled/*.dep && *.zo as well
            subs = f.name.split("/")
            parent = subs[:-1]
            parent.append('compiled')
            file_name = subs[-1]
            byteCodesPrefix = "/".join(parent) + "/" + file_name + "_rkt"
            os.remove(byteCodesPrefix + ".zo")
            os.remove(byteCodesPrefix + ".dep")

    return s

def run_mod(m, stdlib=False, srcloc=True):
    assert not stdlib

    if not pytest.config.byte_option:
        ast = parse_module(expand_string(m, srcloc=srcloc))
    else:
        ast = parse_module(expand_from_bytecode(m, srcloc), bytecode_expand=True)

    return run_ast(ast)

def format_pycket_mod(s, stdlib=False, extra=""):
    # pycket handles the stdlib, various requires
    str = "#lang pycket%s\n%s\n%s"%(" #:stdlib" if stdlib else "", extra, s)
    return str

def run_mod_defs(m, extra="",stdlib=False, srcloc=True):
    str = format_pycket_mod(m, extra=extra, stdlib=stdlib)
    mod = run_mod(str, srcloc=srcloc)
    return mod

def run_mod_expr(e, v=None, stdlib=False, wrap=False, extra="", srcloc=False):
    # this (let () e) wrapping is needed if e is `(begin (define x 1) x)`, for example
    # FIXME: this should get moved into a language
    expr = "(let () %s)"%e if wrap else e
    defn = "(define #%%pycket-expr %s)"%expr
    mod = run_mod_defs(defn, stdlib=stdlib, extra=extra, srcloc=srcloc)
    ov = mod.defs[values.W_Symbol.make("#%pycket-expr")]
    if v:
        assert ov.equal(v)
    return ov

def execute(p, stdlib=False, extra=""):
    return run_mod_expr(p, stdlib=stdlib, extra=extra)

def run_fix(p, v=None, stdlib=False, extra="", equal_huh=False):
    new = pytest.config.new_pycket
    if new:
        return run_expr(p, v, equal_huh=equal_huh)
    ov = run_mod_expr(p,stdlib=stdlib, extra=extra)
    assert isinstance(ov, values.W_Fixnum)
    if v is not None:
        assert ov.value == v
    return ov.value

def run_flo(p, v=None, stdlib=False, extra=""):
    ov = run_mod_expr(p,stdlib=stdlib, extra=extra)
    assert isinstance(ov, values.W_Flonum)
    if v is not None:
        assert ov.value == v
    return ov.value

def run(p, v=None, stdlib=False, extra=""):
    if pytest.config.new_pycket:
        return run_expr_result(p)
    return run_mod_expr(p,v=v,stdlib=stdlib, extra=extra)

def run_top(p, v=None, stdlib=False, extra=""):
    return run_mod_expr(p,v=v,stdlib=stdlib, wrap=True, extra=extra)

def run_values(p, stdlib=False):
    e = "(call-with-values (lambda () %s) list)"%p
    v = run_mod_expr(e, stdlib=stdlib)
    return values.from_list(v)

def run_std(c, v):
    return run_top(c, v, stdlib=True)

def run_file(fname, *replacements, **kwargs):
    modname = os.path.join(os.path.dirname(__file__), fname)
    ast = parse_file(fname, *replacements, **kwargs)
    env = ToplevelEnv()
    env.globalconfig.load(ast)
    env.module_env.add_module(modname, ast)
    return interpret_module(ast, env)

def parse_file(fname, *replacements, **kwargs):
    abspath = kwargs.get("abspath", False)
    if not abspath:
        fname = os.path.join(os.path.dirname(__file__), fname)

    if kwargs.get("inplace", False):
        assert not replacements
        if not pytest.config.byte_option:
            reader = JsonLoader(bytecode_expand=False)
        else:
            reader = JsonLoader(bytecode_expand=True)
        ast = reader.expand_to_ast(fname)
        return ast

    with file(fname) as f:
        s = f.read()
    for replace, with_ in replacements:
        assert s.count(replace) == 1
        s = s.replace(replace, with_)
    s = s.decode("utf-8")

    if not pytest.config.byte_option:
        s = expand_string(s)
        ast = parse_module(s)
    else:
        s = expand_from_bytecode(s, True)
        ast = parse_module(s, bytecode_expand=True)

    return ast

#
# Combined checking
#
def check_all(*snippets_returning_true, **kwargs):
    code = []
    tail = []
    for i, snippet in enumerate(snippets_returning_true):
        code.append("  " * i + "(if %s" % snippet)
        tail.append("  " * (i + 1) + "%s)" % i)
    code.append("  " * (i + 1) + "#t")
    code = "\n".join(code) + "\n" + "\n".join(reversed(tail))
    print code
    res = execute(code, extra=kwargs.get("extra", ""))
    if res is not values.w_true:
        assert 0, "%s returned a non-true value" % snippets_returning_true[res.value]

def check_none(*snippets_returning_false, **kwargs):
    code = []
    tail = []
    for i, snippet in enumerate(snippets_returning_false):
        code.append("  " * i + "(if %s %s" % (snippet, i))
        tail.append("  " * (i + 1) + ")")
    code.append("  " * (i + 1) + "#t")
    code = "\n".join(code) + "\n" + "\n".join(reversed(tail))
    print code
    res = execute(code, extra=kwargs.get("extra", ""))
    if res is not values.w_true:
        assert 0, "%s returned a true value" % snippets_returning_false[res.value]

def check_equal(*pairs_of_equal_stuff, **kwargs):
    code = []
    tail = []
    assert len(pairs_of_equal_stuff) % 2 == 0
    ind = 0
    for i in range(len(pairs_of_equal_stuff) // 2):
        a = pairs_of_equal_stuff[i * 2]
        b = pairs_of_equal_stuff[i * 2 + 1]
        ind += 1
        if isinstance(a, list):
            code.append("  " * ind + "(let ()")
            ind += 1
            code.extend(["  " * ind + x for x in a[:-1]])
            code.append( "  " * ind + ("(if (equal? %s %s)" % (a[-1], b)))
            ind += 1
            tail.append("  " * ind + "%s))" % i)
        else:
            snippet = "(equal? %s %s)" % (a, b)
            code.append("  " * ind + "(if %s" % snippet)
            tail.append("  " * (ind + 1) + "%s)" % i)
    code.append("  " * (ind + 1) + "#t")
    code = "\n".join(code) + "\n" + "\n".join(reversed(tail))
    try:
        print code
    except UnicodeEncodeError:
        print code.encode("ascii", 'replace')

    extra = kwargs.get("extra", "")
    res = execute(code, extra=extra)
    if res is not values.w_true:
        src = pairs_of_equal_stuff[res.value * 2]
        if isinstance(src, list):
            src = "(let ()\n" + "\n".join(src) + ")"
        wrong = execute(src, extra=extra)
        assert 0, u"%s is %s, which is different from %s" % (
            pairs_of_equal_stuff[res.value * 2], wrong.tostring(),
            pairs_of_equal_stuff[res.value * 2 + 1])
<|endoftext|>


<commit_before>### -*- coding: utf-8 -*-
###
### © 2012 Krux Digital, Inc.
### Author: Paul Lathrop <paul@krux.com>
###

"""AWS functions for pysecurity-groups."""

from operator import concat

### The code which reads the boto configuration files only runs when you
### import boto, so even though we aren't using the module, we need to import
### it.
import boto
from boto.ec2 import connect_to_region

from util import expand_sources, rule_dict

def policy(config):
    """
    For each region from CONFIG, fetch the security groups and rules. Return
    them as rule dicts.
    """
    regions = [region.strip()
               for region in config.get('CONFIG', 'regions').split(',')]
    return reduce(concat, [rules(region) for region in regions])

def rules(region):
    """
    Given a REGION, return the live rules for that REGION.
    """
    groups = connect_to_region(region).get_all_security_groups()
    return [dict([('region', region)] + rule.items())
            for rule in reduce(concat, [expand_rules(group)
                                        for group in groups])]

def expand_rules(group):
    """
    Given a GROUP, return a list of rules for that group, with each rule fully
    expanded.
    """
    return reduce(concat, [expand_rule(group, rule) for rule in group.rules])

def expand_rule(group, rule):
    """
    Given a group and a rule, return a list of expanded rules, with a rule for
    each distinct source or port in a list of ports.
    """
    if rule.from_port == rule.to_port:
        ports_or_types = rule.from_port
    else:
        ports_or_types = (rule.from_port, rule.to_port)
    return expand_sources(rule_dict(parse_grants(rule.grants),
                                    group.name,
                                    rule.ip_protocol,
                                    ports_or_types))

def parse_grants(grants):
    """
    Translate the EC2 GRANTS into our internal format.
    """
    return concat([grant.cidr_ip for grant in grants
                   if grant.cidr_ip is not None],
                  [grant.name for grant in grants
                   if grant.name is not None])

def groups(region):
    """
    Given a REGION, return the set of all security groups defined in that REGION.
    """
    return set([group.name for group in
                connect_to_region(region).get_all_security_groups()])
<commit_msg>Fix edge case for groups with no rules.<commit_after>### -*- coding: utf-8 -*-
###
### © 2012 Krux Digital, Inc.
### Author: Paul Lathrop <paul@krux.com>
###

"""AWS functions for pysecurity-groups."""

from operator import concat

### The code which reads the boto configuration files only runs when you
### import boto, so even though we aren't using the module, we need to import
### it.
import boto
from boto.ec2 import connect_to_region

from util import expand_sources, rule_dict

def policy(config):
    """
    For each region from CONFIG, fetch the security groups and rules. Return
    them as rule dicts.
    """
    regions = [region.strip()
               for region in config.get('CONFIG', 'regions').split(',')]
    return reduce(concat, [rules(region) for region in regions])

def rules(region):
    """
    Given a REGION, return the live rules for that REGION.
    """
    groups = connect_to_region(region).get_all_security_groups()
    return [dict([('region', region)] + rule.items())
            for rule in reduce(concat, [expand_rules(group)
                                        for group in groups])]

def expand_rules(group):
    """
    Given a GROUP, return a list of rules for that group, with each rule fully
    expanded.
    """
    return reduce(concat, [expand_rule(group, rule) for rule in group.rules],
                  [])

def expand_rule(group, rule):
    """
    Given a group and a rule, return a list of expanded rules, with a rule for
    each distinct source or port in a list of ports.
    """
    if rule.from_port == rule.to_port:
        ports_or_types = rule.from_port
    else:
        ports_or_types = (rule.from_port, rule.to_port)
    return expand_sources(rule_dict(parse_grants(rule.grants),
                                    group.name,
                                    rule.ip_protocol,
                                    ports_or_types))

def parse_grants(grants):
    """
    Translate the EC2 GRANTS into our internal format.
    """
    return concat([grant.cidr_ip for grant in grants
                   if grant.cidr_ip is not None],
                  [grant.name for grant in grants
                   if grant.name is not None])

def groups(region):
    """
    Given a REGION, return the set of all security groups defined in that REGION.
    """
    return set([group.name for group in
                connect_to_region(region).get_all_security_groups()])
<|endoftext|>


<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4
#
#    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
#

import util
import verify
import nose
from nose.plugins.attrib import attr

@attr(speed='slow')
@attr(tag=['func', 'wordpress'])
def test_template():
    try:
        template = 'WordPress_Single_Instance.template'

        func_utils = util.FuncUtils()

        func_utils.prepare_jeos('F17', 'x86_64', 'cfntools')
        func_utils.create_stack(template, 'F17')
        func_utils.check_cfntools()
        func_utils.wait_for_provisioning()
        func_utils.check_user_data(template)

        ssh = func_utils.get_ssh_client()

        # ensure wordpress was installed by checking for expected
        # configuration file over ssh
        wp_file = '/etc/wordpress/wp-config.php'
        stdin, stdout, sterr = ssh.exec_command('ls ' + wp_file)
        result = stdout.readlines().pop().rstrip()
        assert result == wp_file
        print "Wordpress installation detected"

        # Verify the output URL parses as expected, ie check that
        # the wordpress installation is operational
        stack_url = func_utils.get_stack_output("WebsiteURL")
        print "Got stack output WebsiteURL=%s, verifying" % stack_url
        ver = verify.VerifyStack()
        assert True == ver.verify_wordpress(stack_url)

    finally:
        func_utils.cleanup()
<commit_msg>heat tests : convert functional test to testcase class<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4
#
#    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
#

import util
import verify
import nose
from nose.plugins.attrib import attr
import unittest

@attr(speed='slow')
@attr(tag=['func', 'wordpress'])
class WordPressFunctionalTest(unittest.TestCase):
    def setUp(self):
        template = 'WordPress_Single_Instance.template'

        self.func_utils = util.FuncUtils()

        self.func_utils.prepare_jeos('F17', 'x86_64', 'cfntools')
        self.func_utils.create_stack(template, 'F17')
        self.func_utils.check_cfntools()
        self.func_utils.wait_for_provisioning()
        self.func_utils.check_user_data(template)

        self.ssh = self.func_utils.get_ssh_client()

    def test_instance(self):
        # ensure wordpress was installed by checking for expected
        # configuration file over ssh
        wp_file = '/etc/wordpress/wp-config.php'
        stdin, stdout, sterr = self.ssh.exec_command('ls ' + wp_file)
        result = stdout.readlines().pop().rstrip()
        assert result == wp_file
        print "Wordpress installation detected"

        # Verify the output URL parses as expected, ie check that
        # the wordpress installation is operational
        stack_url = self.func_utils.get_stack_output("WebsiteURL")
        print "Got stack output WebsiteURL=%s, verifying" % stack_url
        ver = verify.VerifyStack()
        assert True == ver.verify_wordpress(stack_url)

        self.func_utils.cleanup()
<|endoftext|>


<commit_before>import rx.subjects as subjects

from pysellus.stock_integrations import terminal

""" { test_name: [ registered_integrations ] } """
registered_integrations = {}

""" { integration_name: rx.subjects.Subject } """
integration_to_subject = {}

def on_failure(*integration_names):
    """
    on_failure :: [String] -> (fn -> fn)

    Decorator that maps the given function to the supplied integration names tuple

    Usage:
    @on_failure('integration')
    def foo():
        ...

    Receives a tuple of strings representing an integration (i.e. 'terminal', 'slack'),
    and returns a decorator function.

    This decorator function takes a function, and, for each string in the original tuple,
    creates a Subject object (see _get_integration). It then maps it against the
    given function name, {string: rx.Subject}.

    Finally, returns the original function

    """
    def decorator_of_setup_function(setup_function):
        registered_integrations[setup_function.__name__] = [
            _get_integration(integration_name_)
            for integration_name_ in integration_names
        ]

        return setup_function

    return decorator_of_setup_function

def _get_integration(integration_name):
    """
    _get_integration :: String -> rx.Subject

    Get the Subject object mapped to the given integration name

    If it doesn't exist, create it (see create) and store it in integration_to_subject,
    mapped against the given name {string: rx.Subject}

    """
    if integration_name not in integration_to_subject:
        integration_to_subject[integration_name] = _create(integration_name)

    return integration_to_subject[integration_name]

# TODO: Search for the integration name somewhere and get the functions
def _create(integration_name):
    """
    create :: String -> rx.Subject

    Given an integration name, create a new rx.Subject mapped to it

    If a mapped subject already exists, return it.

    In any case, the returned Subject has already been subscribed by all interested functions,
    so one can send a message to it without fear that the message will be lost

    """
    if integration_name == 'terminal':
        if integration_name not in integration_to_subject:
            terminal_integration = terminal.TerminalIntegration()
            integration_to_subject[integration_name] = terminal_integration.get_subject()

        return integration_to_subject[integration_name]

def notify_element(test_name, element_payload):
    _notify_integrations(test_name, element_payload)

def notify_error(test_name, element_payload):
    _notify_integrations(test_name, element_payload, error=True)

def _notify_integrations(test_name, message, error=False):
    """
    _notify_integrations :: String -> Any -> Boolean -> IO

    Given a function name, and a Payload object, send the payload to the appropiate subject.

    Check the function name in `registered_integrations` and get all mapped subjects,
    and send the payload to all of them.

    If the error flag is set to true, send the message as an error

    """
    for integration in registered_integrations[test_name]:
        if error:
            integration.on_error(message)
        else:
            integration.on_next(message)
<commit_msg>Simplify _create integration logic<commit_after>import rx.subjects as subjects

from pysellus.stock_integrations import terminal

""" { test_name: [ registered_integrations ] } """
registered_integrations = {}

""" { integration_name: rx.subjects.Subject } """
integration_to_subject = {}

def on_failure(*integration_names):
    """
    on_failure :: [String] -> (fn -> fn)

    Decorator that maps the given function to the supplied integration names tuple

    Usage:
    @on_failure('integration')
    def foo():
        ...

    Receives a tuple of strings representing an integration (i.e. 'terminal', 'slack'),
    and returns a decorator function.

    This decorator function takes a function, and, for each string in the original tuple,
    creates a Subject object (see _get_integration). It then maps it against the
    given function name, {string: rx.Subject}.

    Finally, returns the original function

    """
    def decorator_of_setup_function(setup_function):
        registered_integrations[setup_function.__name__] = [
            _get_integration(integration_name_)
            for integration_name_ in integration_names
        ]

        return setup_function

    return decorator_of_setup_function

def _get_integration(integration_name):
    """
    _get_integration :: String -> rx.Subject

    Get the Subject object mapped to the given integration name

    If it doesn't exist, create it (see create) and store it in integration_to_subject,
    mapped against the given name {string: rx.Subject}

    """
    if integration_name not in integration_to_subject:
        integration_to_subject[integration_name] = _create(integration_name)

    return integration_to_subject[integration_name]

# TODO: Search for the integration name somewhere and get the functions
def _create(integration_name):
    """
    create :: String -> rx.Subject

    Given an integration name, create a new rx.Subject mapped to it

    In any case, the returned Subject has already been subscribed by all interested functions,
    so one can send a message to it without fear that the message will be lost

    """
    subject = None
    if integration_name == 'terminal':
        subject = terminal.TerminalIntegration().get_subject()

    return subject

def notify_element(test_name, element_payload):
    _notify_integrations(test_name, element_payload)

def notify_error(test_name, element_payload):
    _notify_integrations(test_name, element_payload, error=True)

def _notify_integrations(test_name, message, error=False):
    """
    _notify_integrations :: String -> Any -> Boolean -> IO

    Given a function name, and a Payload object, send the payload to the appropiate subject.

    Check the function name in `registered_integrations` and get all mapped subjects,
    and send the payload to all of them.

    If the error flag is set to true, send the message as an error

    """
    for integration in registered_integrations[test_name]:
        if error:
            integration.on_error(message)
        else:
            integration.on_next(message)
<|endoftext|>