<commit_before>import sublime
import sublime_plugin
import re
import json
from xml.dom.minidom import *
from os.path import basename

class BaseIndentCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
        self.language = self.get_language()

    def get_language(self):
        syntax = self.view.settings().get('syntax')
        language = basename(syntax).replace('.tmLanguage', '').lower() if syntax != None else "plain text"
        return language

    def check_enabled(self, lang):
        return True

    def is_enabled(self):
        """
        Enables or disables the 'indent' command.
        Command will be disabled if there are currently no text selections and current file is not 'XML' or 'Plain Text'.
        This helps clarify to the user about when the command can be executed, especially useful for UI controls.
        """
        if self.view == None:
            return False

        return self.check_enabled(self.get_language())

    def run(self, edit):
        """
        Main plugin logic for the 'indent' command.
        """
        view = self.view
        regions = view.sel()
        # if there are more than 1 region or region one and it's not empty
        if len(regions) > 1 or not regions[0].empty():
                for region in view.sel():
                    if not region.empty():
                        s = view.substr(region)
                        s = self.indent(s)
                        view.replace(edit, region, s)
        else:   #format all text
                alltextreg = sublime.Region(0, view.size())
                s = view.substr(alltextreg)
                s = self.indent(s)
                view.replace(edit, alltextreg, s)

class AutoIndentCommand(BaseIndentCommand):
    def get_text_type(self, s):
        language =  self.language
        if language == 'xml':
            return 'xml'
        if language == 'json':
            return 'json'
        if language == 'plain text':
            if (s[0] == '<'):
                return 'xml'
            else:
                return 'json'

        return 'notsupported'

    def indent(self, s):
        text_type = self.get_text_type(s)
        if text_type == 'xml':
            command = IndentXmlCommand(self.view)
        if text_type == 'json':
            command = IndentJsonCommand(self.view)
        if text_type == 'notsupported':
            return s

        return command.indent(s)

    def check_enabled(self, lang):
        return True

class IndentXmlCommand(BaseIndentCommand):
    def indent(self, s):
        # convert to utf
        s = s.encode("utf-8")
        xmlheader = re.compile(b"<\?.*\?>").match(s)
        # convert to plain string without indents and spaces
        s = re.compile(b'>\s+([^\s])', re.DOTALL).sub(b'>\g<1>', s)
        # replace tags to convince minidom process cdata as text
        s = s.replace(b'<![CDATA[', b'%CDATAESTART%').replace(b']]>', b'%CDATAEEND%')
        s = parseString(s).toprettyxml()
        # remove line breaks
        s = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL).sub('>\g<1></', s)
        # restore cdata
        s = s.replace('%CDATAESTART%', '<![CDATA[').replace('%CDATAEEND%', ']]>')
        # remove xml header
        s = s.replace("<?xml version=\"1.0\" ?>", "").strip()
        if xmlheader:
                s = xmlheader.group() + "\n" + s
        return s

    def check_enabled(self, language):
        return ((language == "xml") or (language == "plain text"))

class IndentJsonCommand(BaseIndentCommand):
    def check_enabled(self, language):
        return ((language == "json") or (language == "plain text"))

    def indent(self, s):
        parsed = json.loads(s)
        return json.dumps(parsed, sort_keys=True, indent=4, separators=(',', ': '))<commit_msg>File type deteciton fixed<commit_after>import sublime
import sublime_plugin
import re
import json
from xml.dom.minidom import *
from os.path import basename

class BaseIndentCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view
        self.language = self.get_language()

    def get_language(self):
        syntax = self.view.settings().get('syntax')
        language = basename(syntax).replace('.tmLanguage', '').lower() if syntax != None else "plain text"
        return language

    def check_enabled(self, lang):
        return True

    def is_enabled(self):
        """
        Enables or disables the 'indent' command.
        Command will be disabled if there are currently no text selections and current file is not 'XML' or 'Plain Text'.
        This helps clarify to the user about when the command can be executed, especially useful for UI controls.
        """
        if self.view == None:
            return False

        return self.check_enabled(self.get_language())

    def run(self, edit):
        """
        Main plugin logic for the 'indent' command.
        """
        view = self.view
        regions = view.sel()
        # if there are more than 1 region or region one and it's not empty
        if len(regions) > 1 or not regions[0].empty():
                for region in view.sel():
                    if not region.empty():
                        s = view.substr(region)
                        s = self.indent(s)
                        view.replace(edit, region, s)
        else:   #format all text
                alltextreg = sublime.Region(0, view.size())
                s = view.substr(alltextreg)
                s = self.indent(s)
                view.replace(edit, alltextreg, s)

class AutoIndentCommand(BaseIndentCommand):
    def get_text_type(self, s):
        language =  self.language
        if language == 'xml':
            return 'xml'
        if language == 'json':
            return 'json'
        if language == 'plain text':
            if s[0] == '<':
                return 'xml'
            if s[0] == '{':
                return 'json'

        return 'notsupported'

    def indent(self, s):
        text_type = self.get_text_type(s)
        if text_type == 'xml':
            command = IndentXmlCommand(self.view)
        if text_type == 'json':
            command = IndentJsonCommand(self.view)
        if text_type == 'notsupported':
            return s

        return command.indent(s)

    def check_enabled(self, lang):
        return True

class IndentXmlCommand(BaseIndentCommand):
    def indent(self, s):
        # convert to utf
        s = s.encode("utf-8")
        xmlheader = re.compile(b"<\?.*\?>").match(s)
        # convert to plain string without indents and spaces
        s = re.compile(b'>\s+([^\s])', re.DOTALL).sub(b'>\g<1>', s)
        # replace tags to convince minidom process cdata as text
        s = s.replace(b'<![CDATA[', b'%CDATAESTART%').replace(b']]>', b'%CDATAEEND%')
        s = parseString(s).toprettyxml()
        # remove line breaks
        s = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL).sub('>\g<1></', s)
        # restore cdata
        s = s.replace('%CDATAESTART%', '<![CDATA[').replace('%CDATAEEND%', ']]>')
        # remove xml header
        s = s.replace("<?xml version=\"1.0\" ?>", "").strip()
        if xmlheader:
                s = xmlheader.group() + "\n" + s
        return s

    def check_enabled(self, language):
        return ((language == "xml") or (language == "plain text"))

class IndentJsonCommand(BaseIndentCommand):
    def check_enabled(self, language):
        return ((language == "json") or (language == "plain text"))

    def indent(self, s):
        parsed = json.loads(s)
        return json.dumps(parsed, sort_keys=True, indent=4, separators=(',', ': '))<|endoftext|>


<commit_before>import tensorflow as tf
import numpy as np

def model():

#change this value later
in_dim = 20
out_dim = 100
n_samples = 29000
batch_size = 124
num_features = 50
num_classes = 2
num_iter = 1000

#define placeholder for our input
X = tf.placeholder(tf.float32, shape=(batch_size, num_features))
Y = tf.placeholder(tf.float32, shape=(batch_size, num_classes))
#drop_p = tf.placeholder(tf.float32)
#first hidden layer
with tf.variable_scope("hidden1") as scope:
	w = tf.get_variable("weights",[in_dim out_dim], initializer=tf.random_normal_initializer())
	b = tf.get_variable("bias",[out_dim], initializer=tf.constant_initializer(0.0))
	hidden1 = tf.nn.relu(tf.nn.bias_add(tf.matmul(X, w), b), name=scope.name)
	#h1_drop = tf.nn.dropout(hidden1, drop_p)

#second hidden layer
with tf.variable_scope("hidden2") as scope:
	w = tf.get_variable("weights", [out_dim num_classes], initializer=tf.random_normal_initializer())
	b = tf.get_variable("bias",[num_classes], initializer=tf.constant_initializer(0.0))
	hidden2 = tf.nn.relu(tf.nn.bias_add(tf.matmul(hidden1, w), b), name=scope.name)
	#h2_drop = tf.nn.dropout(hidden2, drop_p)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(hidden2, Y))
train_op = tf.train.AdamOptimizer().minimize(loss)
predict_op = tf.argmax(hidden2, 1)

with tf.Session() as sess:
	sess.run(tf.initialize_all_variables())
	#gradient descent loop for num_iter steps
	for _ in range(num_iter):

		#need to write code to get x and y data where x is all the features and y is one hot labels
		#X_data is our features, still need to write the code to get all the features
		for start, end in zip(range(0, len(X_data), batch_size), range(batch_size, len(X_data)+1, batch_size)):
			_, loss_val = sess.run(train_op, feed_dict={X: X_data[start:end], Y: y_data[start:end]})

		#to predict scores
		#print(i, np.mean(np.argmax(y_test, axis=1) == sess.run(predict_op, feed_dict={X: X_test})))
<commit_msg>Delete nn.py<commit_after><|endoftext|>


<commit_before>import json
from twython import Twython

from config import (consumer_key, consumer_secret, access_token, access_token_secret)

def filter_sources(tweet):
	### TK

	pass

def find_tweets(twitter, place_id):
	### Return tweets from a certain place id

	#`count` param defaults to 15, maximum of 100. using 3 for now to test
	results = twitter.search(q = 'place:'+place_id, count = '3')

	#turn results in json string
	tweets = json.loads( json.dumps(results) )

	print "Tweets:"
	for i in range(len(tweets['statuses'])):
		tweet_id = tweets['statuses'][i]['id_str']

		#retrieve tweet based on tweet_id
		t = twitter.lookup_status(id = tweet_id)
		tweet = json.loads( json.dumps(t) )

		#sample parse for tweet output
		print tweet[0]['user']['screen_name']+': '+tweet[0]['text']

		#this is where we're going to filter these tweets then return
		#filter_sources(tweet)

		#pydrive?

def find_place(location):
	### Determine place id based on querying location

	#configure twython connection
	twitter = Twython(consumer_key, consumer_secret, access_token, access_token_secret)

	results = twitter.search_geo(query = location, max_results = '3')

	#turn results into json string, then turn json string into parsable dictionary
	locations = json.loads( json.dumps(results) )

	#results return different location possibilities based on the location you enter
	#we need the user to tell us which location is the right one to use
	print "Results based on your provided location:"
	for i in range(len(locations['result']['places'])):
		print str(i+1)+'. ', locations['result']['places'][i]['full_name']
		### TODO: option for "other"

	place_number = input('Please enter the location you want to use: ')
	place_number = place_number - 1 #adjust for array counting

	place_id = locations['result']['places'][place_number]['id']

	find_tweets(twitter, place_id)

<commit_msg>start reliable-sources function<commit_after>import json
from twython import Twython

from config import (consumer_key, consumer_secret, access_token, access_token_secret)

def filter_sources(tweet):
	### Method to determind if sources are "reliable"

	return True

def find_tweets(twitter, place_id):
	### Return tweets from a certain place id

	#`count` param defaults to 15, maximum of 100. using 3 for now to test
	results = twitter.search(q = 'place:'+place_id, count = '3')

	#turn results in json string
	tweets = json.loads( json.dumps(results) )

	print "Tweets:"
	for i in range(len(tweets['statuses'])):
		tweet_id = tweets['statuses'][i]['id_str']

		#retrieve tweet based on tweet_id
		t = twitter.lookup_status(id = tweet_id)
		tweet = json.loads( json.dumps(t) )

		#this is where we're going to filter these tweets then return
		reliable = filter_sources(tweet)

		if reliable == True:
			#sample parse for tweet output
			print tweet[0]['user']['screen_name']+': '+tweet[0]['text']

		#pydrive?

def find_place(location):
	### Determine place id based on querying location

	#configure twython connection
	twitter = Twython(consumer_key, consumer_secret, access_token, access_token_secret)

	results = twitter.search_geo(query = location, max_results = '3')

	#turn results into json string, then turn json string into parsable dictionary
	locations = json.loads( json.dumps(results) )

	#results return different location possibilities based on the location you enter
	#we need the user to tell us which location is the right one to use
	print "Results based on your provided location:"
	for i in range(len(locations['result']['places'])):
		print str(i+1)+'. ', locations['result']['places'][i]['full_name']
		### TODO: option for "other"

	place_number = input('Please enter the location you want to use: ')
	place_number = place_number - 1 #adjust for array counting

	place_id = locations['result']['places'][place_number]['id']

	find_tweets(twitter, place_id)

<|endoftext|>


<commit_before>################################### LICENSE ####################################
#                            Copyright 2016 Morphux                            #
#                                                                              #
#        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.                         #
################################################################################

##
# glibc_p2.py
# Created: 13/12/2016
# By: Louis Solofrizzo <louis@morphux.org>
##

import      os

class   Glibc_P2:

    conf_lst = {}
    e = False
    root_dir = ""

    def     init(self, c_lst, ex, root_dir):
        self.conf_lst = c_lst
        self.e = ex
        self.root_dir = root_dir
        self.config = {
            "name": "glibc", # Name of the package
            "version": "2.24", # Version of the package
            "size": 1400, # Size of the installed package (MB)
            "archive": "glibc-2.24.tar.xz", # Archive name
            "SBU": 17, # SBU (Compilation time)
            "tmp_install": False, # Is this package part of the temporary install
            "next": "toolchain", # Next package to install
            "urls": [ # Url to download the package. The first one must be morphux servers
                "https://install.morphux.org/packages/glibc-2.24.tar.xz"
            ]
        }
        return self.config

    def     before(self):
        self.e(["patch", "-Np1", "-i", "../glibc-2.24-fhs-1.patch"], ignore=True)
        res = self.e(["mkdir", "-vp", "build"])
        os.chdir("build")
        return res

    def     configure(self):
        return self.e(["../configure",
                "--prefix=/usr",
                "--enable-kernel=2.6.32",
                "--enable-obsolete-rpc"
            ])

    def     make(self):
        return self.e(["make", "-j", self.conf_lst["cpus"]])

    def     install(self):
        self.e(["touch", "/etc/ld"])
        return self.e(["make", "install"])

    def     after(self):
        locales = [
            ("cs_CZ", "UTF-8", "cs_CZ.UTF-8"),
            ("de_DE", "ISO-8859-1", "de_DE"),
            ("de_DE@euro", "ISO-8859-15", "de_DE@euro"),
            ("de_DE", "UTF-8", "de_DE.UTF-8"),
            ("en_GB", "UTF-8", "en_GB.UTF-8"),
            ("en_HK", "ISO-8859-1", "en_HK"),
            ("en_PH", "ISO-8859-1", "en_PH"),
            ("en_US", "ISO-8859-1", "en_US"),
            ("en_US", "UTF-8", "en_US.UTF-8"),
            ("ex_MX", "ISO-8859-1", "es_MX"),
            ("fa_IR", "UTF-8", "fa_IR"),
            ("fr_FR", "ISO-8859-1", "fr_FR"),
            ("fr_FR@euro", "ISO-8859-15", "fr_FR@euro"),
            ("fr_FR", "UTF-8", "fr_FR.UTF-8"),
            ("it_IT", "ISO-8859-1", "it_IT"),
            ("it_IT", "UTF-8", "it_IT.UTF-8"),
            ("jp_JP", "EUC-JP", "ja_JP"),
            ("ru_RU", "KOI8-R", "ru_RU.KOI8-R"),
            ("ru_RU", "UTF-8", "ru_RU.UTF-8"),
            ("tr_TR", "UTF-8", "tr_TR.UTF-8"),
            ("zh_CN", "GB18030", "zh_CN.GB18030")
        ]
        zoneinfo="/usr/share/zoneinfo"
        locale_base = "America/New_York"

        self.e(["cp", "-v", "../nscd/nscd.conf", "/etc/nscd.conf"])
        self.e(["mkdir", "-pv", "/var/cache/nscd"])
        self.e(["mkdir", "-pv", "/usr/lib/locale"])

        for l in locales:
            self.e(["localedef", "-i", l[0], "-f", l[1], l[2]])

        self.e(["tar", "xf", "../../tzdata2016f.tar.gz"])
        self.e(["mkdir", "-pv", zoneinfo + "/{posix,right}"], shell=True)

        self.e(["for tz in etcetera southamerica northamerica europe africa antarctica \
                    asia australasia backward pacificnew systemv; do \
                zic -L /dev/null \
                -d", zoneinfo,
                "-y 'sh yearistype.sh' ${tz} \
                zic -L /dev/null \
                -d", zoneinfo, "/posix -y 'sh yearistype.sh' ${tz} \
                zic -L leapseconds -d ", zoneinfo,"/right -y 'sh yearistype.sh' ${tz} \
            done"
            ], shell=True)
        self.e(["cp", "-v", "zone.tab", "zone1970.tab", "iso3166.tab", zoneinfo])
        self.e(["zic", "-d", zoneinfo, "-p", "America/New_York"])
        self.e(["mkdir", "-pv", "/etc/ld.so.conf.d"])
        return self.e(["cp", "-v", "/usr/share/zoneinfo/" + locale_base, "/etc/localtime"])
<commit_msg>Fix(Glibc P2): Wrong names for locales<commit_after>################################### LICENSE ####################################
#                            Copyright 2016 Morphux                            #
#                                                                              #
#        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.                         #
################################################################################

##
# glibc_p2.py
# Created: 13/12/2016
# By: Louis Solofrizzo <louis@morphux.org>
##

import      os

class   Glibc_P2:

    conf_lst = {}
    e = False
    root_dir = ""

    def     init(self, c_lst, ex, root_dir):
        self.conf_lst = c_lst
        self.e = ex
        self.root_dir = root_dir
        self.config = {
            "name": "glibc", # Name of the package
            "version": "2.24", # Version of the package
            "size": 1400, # Size of the installed package (MB)
            "archive": "glibc-2.24.tar.xz", # Archive name
            "SBU": 17, # SBU (Compilation time)
            "tmp_install": False, # Is this package part of the temporary install
            "next": "toolchain", # Next package to install
            "urls": [ # Url to download the package. The first one must be morphux servers
                "https://install.morphux.org/packages/glibc-2.24.tar.xz"
            ]
        }
        return self.config

    def     before(self):
        self.e(["patch", "-Np1", "-i", "../glibc-2.24-fhs-1.patch"], ignore=True)
        res = self.e(["mkdir", "-vp", "build"])
        os.chdir("build")
        return res

    def     configure(self):
        return self.e(["../configure",
                "--prefix=/usr",
                "--enable-kernel=2.6.32",
                "--enable-obsolete-rpc"
            ])

    def     make(self):
        return self.e(["make", "-j", self.conf_lst["cpus"]])

    def     install(self):
        self.e(["touch", "/etc/ld"])
        return self.e(["make", "install"])

    def     after(self):
        locales = [
            ("cs_CZ", "UTF-8", "cs_CZ.UTF-8"),
            ("de_DE", "ISO-8859-1", "de_DE"),
            ("de_DE@euro", "ISO-8859-15", "de_DE@euro"),
            ("de_DE", "UTF-8", "de_DE.UTF-8"),
            ("en_GB", "UTF-8", "en_GB.UTF-8"),
            ("en_HK", "ISO-8859-1", "en_HK"),
            ("en_PH", "ISO-8859-1", "en_PH"),
            ("en_US", "ISO-8859-1", "en_US"),
            ("en_US", "UTF-8", "en_US.UTF-8"),
            ("es_MX", "ISO-8859-1", "es_MX"),
            ("fa_IR", "UTF-8", "fa_IR"),
            ("fr_FR", "ISO-8859-1", "fr_FR"),
            ("fr_FR@euro", "ISO-8859-15", "fr_FR@euro"),
            ("fr_FR", "UTF-8", "fr_FR.UTF-8"),
            ("it_IT", "ISO-8859-1", "it_IT"),
            ("it_IT", "UTF-8", "it_IT.UTF-8"),
            ("ja_JP", "EUC-JP", "ja_JP"),
            ("ru_RU", "KOI8-R", "ru_RU.KOI8-R"),
            ("ru_RU", "UTF-8", "ru_RU.UTF-8"),
            ("tr_TR", "UTF-8", "tr_TR.UTF-8"),
            ("zh_CN", "GB18030", "zh_CN.GB18030")
        ]
        zoneinfo="/usr/share/zoneinfo"
        locale_base = "America/New_York"

        self.e(["cp", "-v", "../nscd/nscd.conf", "/etc/nscd.conf"])
        self.e(["mkdir", "-pv", "/var/cache/nscd"])
        self.e(["mkdir", "-pv", "/usr/lib/locale"])

        for l in locales:
            self.e(["localedef", "-i", l[0], "-f", l[1], l[2]])

        self.e(["tar", "xf", "../../tzdata2016f.tar.gz"])
        self.e(["mkdir", "-pv", zoneinfo + "/{posix,right}"], shell=True)

        self.e(["for tz in etcetera southamerica northamerica europe africa antarctica \
                    asia australasia backward pacificnew systemv; do \
                zic -L /dev/null \
                -d", zoneinfo,
                "-y 'sh yearistype.sh' ${tz} \
                zic -L /dev/null \
                -d", zoneinfo, "/posix -y 'sh yearistype.sh' ${tz} \
                zic -L leapseconds -d ", zoneinfo,"/right -y 'sh yearistype.sh' ${tz} \
            done"
            ], shell=True)
        self.e(["cp", "-v", "zone.tab", "zone1970.tab", "iso3166.tab", zoneinfo])
        self.e(["zic", "-d", zoneinfo, "-p", "America/New_York"])
        self.e(["mkdir", "-pv", "/etc/ld.so.conf.d"])
        return self.e(["cp", "-v", "/usr/share/zoneinfo/" + locale_base, "/etc/localtime"])
<|endoftext|>


<commit_before>import psutil, os, re, sys, signal, subprocess, time, threading, traceback

from .actorlib import send, receive

from ..eval.Interpreter import Interpreter

from ..visitor.Parser import parseFromString

from ..actors.console import Console

debug = False

def infanticide(pid):
    try:
      parent = psutil.Process(pid)
    except psutil.NoSuchProcess:
      return
    children = parent.children(recursive=True)
    if debug:
        sys.stderr.write('The children of {0} are {1}\n'.format(pid, children))
    for p in children:
        os.kill(p.pid, signal.SIGKILL)
        if debug:
            sys.stderr.write('Sent signal {1} to {0}\n'.format(p.pid, signal.SIGKILL))

def sighandler(signum, frame):
    if debug:
        sys.stderr.write('Signal handler called with signal {0}\n'.format(signum))
        sys.stderr.flush()
    infanticide(os.getpid())
    sys.exit()

def main():
    if len(sys.argv) == 1:
        launch("noname")
    elif len(sys.argv) == 2:
        launch(sys.argv[1])
    else:
        launch(sys.argv[1], sys.argv[2])
    return 0

class Main(object):

    retries = 1

    launchConsole = False

    plambda_pattern = re.compile('^\s*\(.*\)\s*$')

    handlers = []

    # a singleton instance
    myself = None

    def __init__(self, name, filename=None):
        """Creates an plambda Actor object with the given name.
        """
        self.name = name
        self.interpreter = Interpreter()
        self.filename = filename
        signal.signal(signal.SIGINT, sighandler)
        if Main.myself is None:
            Main.myself = self
        else:
            raise Exception("plambda.actor.pyactor.Main should have a singleton instance!")
        if self.filename is not None:
            sys.stderr.write('Loading {0}\n'.format(self.filename))
            self.interpreter.load(self.filename)

    def run(self):
        """The main thread ever ready to launch the console.
        """
        fails = 0

        self.oracle = threading.Thread(target=oracle, args=(self, ))
        self.oracle.daemon = True
        self.oracle.start()

        while True:
            time.sleep(1)
            #FIXME: better do this with a message now that we have handlers.
            if Main.launchConsole:
                Main.launchConsole = False
                self.console = Console(self.interpreter)
                self.console.mainloop()

        return 0

def add_handler(closure):
    Main.handlers.append(closure)

def remove_handler(closure):
    Main.handlers.remove(closure)

def notify(message):
    if debug:
        sys.stderr.write('{0}\n'.format(message))
        sys.stderr.flush()

def oracle(actor):
    """The Read Eval Message Loop.
    """
    fails = 0

    while True:
        incoming = receive()
        if incoming is None:
            fails += 1
            if fails > Main.retries:
                return 0
            else:
                continue
        (sender, msg) = incoming
        thread = threading.Thread(target=eval, args=(actor.interpreter, sender, msg))
        #thread needs to be a daemon so that the actor itself can die in peace.
        thread.daemon = True
        thread.start()

def eval(interpreter, sender, message):
    """Evaluates the message in the given interpreter.

    If the message appears to be an PLambda expression iit is simply
    dispatched to the interpreter to evaluate. Otherwise it is processed
    by any custom handlers that have been installed.

    Custom handlers are of the form:

    (define handler (lambda (sender message)  ... ))

    and are installed by

    (import 'plambda.actors.pyactor')
    (apply plambda.actors.pyactor.add_handler handler)

    and removed analgously

    (apply plambda.actors.pyactor.remove_handler handler)

    """
    try:
        if Main.plambda_pattern.match(message):
            val = interpreter.evaluateString(message)
            notify('eval: {0} evaluated to {1}'.format(message, val))
        else:
            sender = sender.strip()
            message = message.strip()
            notify('looking for handlers for: "{0}" from {1}'.format(message, sender))
            for handler in Main.handlers:
                if handler.applyClosure(sender, message):
                    notify('found a handler for: "{0}" from sender {1}'.format(message, sender))
                    break

    except Exception as e:
        sys.stderr.write('plambda.actors.pyactor.Main exception: {0}\n'.format(e))
        if debug:
            traceback.print_exc(file=sys.stderr)

def launch(name, file=None):
    main = Main(name, file)
    main.run()
    return 0

<commit_msg>Clean uo.<commit_after>import psutil, os, re, sys, signal, subprocess, time, threading, traceback

from .actorlib import send, receive

from ..eval.Interpreter import Interpreter

from ..visitor.Parser import parseFromString

from ..actors.console import Console

debug = False

def infanticide(pid):
    try:
      parent = psutil.Process(pid)
    except psutil.NoSuchProcess:
      return
    children = parent.children(recursive=True)
    if debug:
        sys.stderr.write('The children of {0} are {1}\n'.format(pid, children))
    for p in children:
        os.kill(p.pid, signal.SIGKILL)
        if debug:
            sys.stderr.write('Sent signal {1} to {0}\n'.format(p.pid, signal.SIGKILL))

def sighandler(signum, frame):
    if debug:
        sys.stderr.write('Signal handler called with signal {0}\n'.format(signum))
        sys.stderr.flush()
    infanticide(os.getpid())
    sys.exit()

def main():
    if len(sys.argv) == 1:
        launch("noname")
    elif len(sys.argv) == 2:
        launch(sys.argv[1])
    else:
        launch(sys.argv[1], sys.argv[2])
    return 0

class Main(object):

    retries = 1

    launchConsole = False

    plambda_pattern = re.compile('^\s*\(.*\)\s*$')

    handlers = []

    # a singleton instance
    myself = None

    def __init__(self, name, filename=None):
        """Creates an plambda Actor object with the given name.
        """
        self.name = name
        self.interpreter = Interpreter()
        self.filename = filename
        signal.signal(signal.SIGINT, sighandler)
        if Main.myself is None:
            Main.myself = self
        else:
            raise Exception("plambda.actor.pyactor.Main should have a singleton instance!")
        if self.filename is not None:
            notify('Loading {0}\n'.format(self.filename))
            self.interpreter.load(self.filename)

    def run(self):
        """The main thread ever ready to launch the console.
        """
        fails = 0

        self.oracle = threading.Thread(target=oracle, args=(self, ))
        self.oracle.daemon = True
        self.oracle.start()

        while True:
            time.sleep(1)
            #FIXME: better do this with a message now that we have handlers.
            if Main.launchConsole:
                Main.launchConsole = False
                self.console = Console(self.interpreter)
                self.console.mainloop()

        return 0

def add_handler(closure):
    Main.handlers.append(closure)

def remove_handler(closure):
    Main.handlers.remove(closure)

def notify(message):
    if debug:
        sys.stderr.write('{0}\n'.format(message))
        sys.stderr.flush()

def oracle(actor):
    """The Read Eval Message Loop.
    """
    fails = 0

    while True:
        incoming = receive()
        if incoming is None:
            fails += 1
            if fails > Main.retries:
                return 0
            else:
                continue
        (sender, msg) = incoming
        thread = threading.Thread(target=eval, args=(actor.interpreter, sender, msg))
        #thread needs to be a daemon so that the actor itself can die in peace.
        thread.daemon = True
        thread.start()

def eval(interpreter, sender, message):
    """Evaluates the message in the given interpreter.

    If the message appears to be an PLambda expression iit is simply
    dispatched to the interpreter to evaluate. Otherwise it is processed
    by any custom handlers that have been installed.

    Custom handlers are of the form:

    (define handler (lambda (sender message)  ... ))

    and are installed by

    (import 'plambda.actors.pyactor')
    (apply plambda.actors.pyactor.add_handler handler)

    and removed analgously

    (apply plambda.actors.pyactor.remove_handler handler)

    """
    try:
        if Main.plambda_pattern.match(message):
            val = interpreter.evaluateString(message)
            notify('eval: {0} evaluated to {1}'.format(message, val))
        else:
            sender = sender.strip()
            message = message.strip()
            notify('looking for handlers for: "{0}" from {1}'.format(message, sender))
            for handler in Main.handlers:
                if handler.applyClosure(sender, message):
                    notify('found a handler for: "{0}" from sender {1}'.format(message, sender))
                    break

    except Exception as e:
        sys.stderr.write('plambda.actors.pyactor.Main exception: {0}\n'.format(e))
        if debug:
            traceback.print_exc(file=sys.stderr)

def launch(name, file=None):
    main = Main(name, file)
    main.run()
    return 0

<|endoftext|>


<commit_before>#%% Setup.
from matplotlib import pyplot as plt
from scipy.ndimage import zoom

from eva.models.gated_pixelcnn import GatedPixelCNN

from eva.util.mutil import generate

INPUTS = {
    'cifar10' : (32, 32, 3),
    'mnist_rgb' : (28, 28, 3),
    'mnist' : (28, 28, 1)
}

#%% Args.
# Input.
TYPE = 'mnist_rgb'

# Model.
FILTERS = 126
BLOCKS = 1
CLASSES = 10

# Generation.
BATCH = 10
LATENT = None

# Parse model.
INPUT = INPUTS[TYPE]
M = GatedPixelCNN(INPUT, FILTERS, BLOCKS, LATENT)
M.load_weights('model.h5')

#%% Choice (Probabilistic).
batch = generate(M, LATENT, BATCH, deterministic=False)
for pic in batch:
    plt.imshow(pic, interpolation='nearest')
    plt.show()

#%% ArgMax (Deterministic)
batch = generate(M, LATENT, BATCH, deterministic=True)
for pic in batch:
    plt.imshow(pic, interpolation='nearest')
    plt.show()
<commit_msg>Remove uneeded classes field in inference<commit_after>#%% Setup.
from matplotlib import pyplot as plt
from scipy.ndimage import zoom

from eva.models.gated_pixelcnn import GatedPixelCNN

from eva.util.mutil import generate

INPUTS = {
    'cifar10' : (32, 32, 3),
    'mnist_rgb' : (28, 28, 3),
    'mnist' : (28, 28, 1)
}

#%% Args.
# Input.
TYPE = 'mnist_rgb'

# Model.
FILTERS = 126
BLOCKS = 1

# Generation.
BATCH = 10
LATENT = None

# Parse model.
INPUT = INPUTS[TYPE]
M = GatedPixelCNN(INPUT, FILTERS, BLOCKS, LATENT)
M.load_weights('model.h5')

#%% Choice (Probabilistic).
batch = generate(M, LATENT, BATCH, deterministic=False)
for pic in batch:
    plt.imshow(pic, interpolation='nearest')
    plt.show()

#%% ArgMax (Deterministic)
batch = generate(M, LATENT, BATCH, deterministic=True)
for pic in batch:
    plt.imshow(pic, interpolation='nearest')
    plt.show()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    flask_cachecontrol
    ~~~~~~~~~~~~~~~~~~

    A light-weight library to conveniently set Cache-Control
    headers on the response. Decorate view functions with
    cache_for, cache, or dont_cache decorators. Makes use of
    Flask response.cache_control.

    This extension does not provide any caching of its own. Its sole
    purpose is to set Cache-Control and related HTTP headers on the
    response, so that clients, intermediary proxies or reverse proxies
    in your jurisdiction which evaluate Cache-Control headers, such as
    Varnish Cache, do the caching for you.

    :copyright: (c) 2015 by Thomas Wiebe.
    :license: BSD, see LICENSE for more details.
"""

from .cache import cache, cache_for, dont_cache, FlaskCacheControl
from .error import FlaskCacheControlError, CacheControlAttributeInvalidError

__version__ = '0.1.2'
<commit_msg>Bump version<commit_after># -*- coding: utf-8 -*-
"""
    flask_cachecontrol
    ~~~~~~~~~~~~~~~~~~

    A light-weight library to conveniently set Cache-Control
    headers on the response. Decorate view functions with
    cache_for, cache, or dont_cache decorators. Makes use of
    Flask response.cache_control.

    This extension does not provide any caching of its own. Its sole
    purpose is to set Cache-Control and related HTTP headers on the
    response, so that clients, intermediary proxies or reverse proxies
    in your jurisdiction which evaluate Cache-Control headers, such as
    Varnish Cache, do the caching for you.

    :copyright: (c) 2015 by Thomas Wiebe.
    :license: BSD, see LICENSE for more details.
"""

from .cache import cache, cache_for, dont_cache, FlaskCacheControl
from .error import FlaskCacheControlError, CacheControlAttributeInvalidError

__version__ = '0.1.3'
<|endoftext|>


<commit_before>from __future__ import unicode_literals
from ben10.foundation.platform_ import Platform, UnknownPlatform
import os
import platform
import pytest
import sys

#===================================================================================================
# Test
#===================================================================================================
class Test(object):

    def testPlatform(self):
        p = Platform('win', '32')
        assert p.name == 'win'
        assert p.bits == '32'
        assert p.debug == False
        assert unicode(p) == 'win32'
        assert p.AsString(False) == 'win32'
        assert p.AsString(True) == 'win32'
        assert p.GetSimplePlatform() == 'i686.win32'
        assert p.GetBaseName() == 'win32'
        assert p.GetLongName() == 'Windows 32-bit'
        assert p.GetPlatformFlavour() == 'windows'
        assert p.GetMneumonic() == 'w32'

        p = Platform('win', '64', True)
        assert p.name == 'win'
        assert p.bits == '64'
        assert p.debug == True
        assert unicode(p) == 'win64d'
        assert p.AsString(False) == 'win64d'
        assert p.AsString(True) == 'win64'
        assert p.GetSimplePlatform() == 'amd64.win32'
        assert p.GetBaseName() == 'win64'
        assert p.GetLongName() == 'Windows 64-bit DEBUG'
        assert p.GetPlatformFlavour() == 'windows'
        assert p.GetMneumonic() == 'w64'

        with pytest.raises(ValueError):
            p = Platform('INVALID', '32')

        with pytest.raises(ValueError):
            p = Platform('win', 'INVALID')

        p = Platform('win', '32')
        p.name = 'INVALID'
        with pytest.raises(UnknownPlatform):
            p.GetSimplePlatform()

        with pytest.raises(UnknownPlatform):
            p.GetPlatformFlavour()

        with pytest.raises(UnknownPlatform):
            p.GetLongName()

        assert p.GetCurrentFlavour() == p.GetCurrentPlatform().GetPlatformFlavour()

    def testGetValidPlatforms(self):
        assert set(Platform.GetValidPlatforms()) == {
            'darwin32',
            'darwin32d',
            'darwin64',
            'darwin64d',
            'debian32',
            'debian32d',
            'debian64',
            'debian64d',
            'redhat32',
            'redhat32d',
            'redhat64',
            'redhat64d',
            'ubuntu32',
            'ubuntu32d',
            'ubuntu64',
            'ubuntu64d',
            'win32',
            'win32d',
            'win64',
            'win64d',
        }

    def testCreate(self):
        assert unicode(Platform.Create('win32')) == 'win32'
        assert unicode(Platform.Create('i686.win32')) == 'win32'
        assert unicode(Platform.Create(None)) == unicode(Platform.GetCurrentPlatform())

        plat = Platform.GetCurrentPlatform()
        assert Platform.Create(plat) is plat

        p = Platform.CreateFromString('win32')
        assert unicode(p) == 'win32'

        p = Platform.CreateFromSimplePlatform('i686.win32')
        assert unicode(p) == 'win32'

        with pytest.raises(UnknownPlatform):
            Platform.Create(123)

        with pytest.raises(UnknownPlatform):
            Platform.CreateFromSimplePlatform('UNKNOWN')

    def testGetCurrentPlatform(self, monkeypatch):
        '''
        This is a white box test, but I found it necessary to full coverage.
        '''
        monkeypatch.setattr(sys, 'platform', 'win32')
        monkeypatch.setattr(platform, 'python_compiler', lambda:'WINDOWS')
        assert unicode(Platform.GetCurrentPlatform()) == 'win32'
        assert unicode(Platform.GetDefaultPlatform()) == 'win32'

        monkeypatch.setattr(platform, 'python_compiler', lambda:'AMD64')
        assert unicode(Platform.GetCurrentPlatform()) == 'win64'
        assert unicode(Platform.GetDefaultPlatform()) == 'win32'

        monkeypatch.setattr(sys, 'platform', 'darwin')
        assert unicode(Platform.GetCurrentPlatform()) == 'darwin64'
        assert unicode(Platform.GetDefaultPlatform()) == 'darwin64'

        monkeypatch.setattr(sys, 'platform', 'linux2')
        monkeypatch.setattr(platform, 'dist', lambda:['fedora'])
        monkeypatch.setattr(platform, 'machine', lambda:'x86_64')
        assert unicode(Platform.GetCurrentPlatform()) == 'redhat64'
        assert unicode(Platform.GetDefaultPlatform()) == 'redhat64'

    def testGetOSPlatform(self, monkeypatch):
        monkeypatch.setattr(sys, 'platform', 'win32')
        monkeypatch.setattr(platform, 'python_compiler', lambda:'WINDOWS')

        monkeypatch.setattr(os, 'environ', {})
        assert unicode(Platform.GetOSPlatform()) == 'win32'

        monkeypatch.setattr(os, 'environ', {'PROGRAMFILES(X86)':''})
        assert unicode(Platform.GetOSPlatform()) == 'win64'

        monkeypatch.setattr(sys, 'platform', 'linux2')
        monkeypatch.setattr(platform, 'dist', lambda:['fedora'])
        monkeypatch.setattr(platform, 'machine', lambda:'x86_64')
        assert unicode(Platform.GetOSPlatform()) == 'redhat64'

    def testFlags(self, monkeypatch):
        assert Platform.GetAllFlags() == {
            'windows', 'linux', 'darwin',
            'win32', 'win32d', 'win64', 'win64d',
            'redhat32', 'redhat32d', 'redhat64', 'redhat64d',
            'darwin32', 'darwin32d', 'darwin64', 'darwin64d',
            'debian64', 'debian32', 'debian64d', 'debian32d',
            'ubuntu64', 'ubuntu32', 'ubuntu32d', 'ubuntu64d',
        }

        platform = Platform.Create('win32')
        assert platform.GetFlags() == {'windows', 'win32'}

        platform = Platform.Create('win32d')
        assert platform.GetFlags() == {'windows', 'win32', 'win32d', 'debug'}

        platform = Platform.Create('redhat64')
        assert platform.GetFlags() == {'linux', 'redhat64'}
<commit_msg>Fixed platform tests after centos64 was added.<commit_after>from __future__ import unicode_literals
from ben10.foundation.platform_ import Platform, UnknownPlatform
import os
import platform
import pytest
import sys

#===================================================================================================
# Test
#===================================================================================================
class Test(object):

    def testPlatform(self):
        p = Platform('win', '32')
        assert p.name == 'win'
        assert p.bits == '32'
        assert p.debug == False
        assert unicode(p) == 'win32'
        assert p.AsString(False) == 'win32'
        assert p.AsString(True) == 'win32'
        assert p.GetSimplePlatform() == 'i686.win32'
        assert p.GetBaseName() == 'win32'
        assert p.GetLongName() == 'Windows 32-bit'
        assert p.GetPlatformFlavour() == 'windows'
        assert p.GetMneumonic() == 'w32'

        p = Platform('win', '64', True)
        assert p.name == 'win'
        assert p.bits == '64'
        assert p.debug == True
        assert unicode(p) == 'win64d'
        assert p.AsString(False) == 'win64d'
        assert p.AsString(True) == 'win64'
        assert p.GetSimplePlatform() == 'amd64.win32'
        assert p.GetBaseName() == 'win64'
        assert p.GetLongName() == 'Windows 64-bit DEBUG'
        assert p.GetPlatformFlavour() == 'windows'
        assert p.GetMneumonic() == 'w64'

        with pytest.raises(ValueError):
            p = Platform('INVALID', '32')

        with pytest.raises(ValueError):
            p = Platform('win', 'INVALID')

        p = Platform('win', '32')
        p.name = 'INVALID'
        with pytest.raises(UnknownPlatform):
            p.GetSimplePlatform()

        with pytest.raises(UnknownPlatform):
            p.GetPlatformFlavour()

        with pytest.raises(UnknownPlatform):
            p.GetLongName()

        assert p.GetCurrentFlavour() == p.GetCurrentPlatform().GetPlatformFlavour()

    def testGetValidPlatforms(self):
        assert set(Platform.GetValidPlatforms()) == {
            'centos32',
            'centos32d',
            'centos64',
            'centos64d',
            'darwin32',
            'darwin32d',
            'darwin64',
            'darwin64d',
            'debian32',
            'debian32d',
            'debian64',
            'debian64d',
            'redhat32',
            'redhat32d',
            'redhat64',
            'redhat64d',
            'ubuntu32',
            'ubuntu32d',
            'ubuntu64',
            'ubuntu64d',
            'win32',
            'win32d',
            'win64',
            'win64d',
        }

    def testCreate(self):
        assert unicode(Platform.Create('win32')) == 'win32'
        assert unicode(Platform.Create('i686.win32')) == 'win32'
        assert unicode(Platform.Create(None)) == unicode(Platform.GetCurrentPlatform())

        plat = Platform.GetCurrentPlatform()
        assert Platform.Create(plat) is plat

        p = Platform.CreateFromString('win32')
        assert unicode(p) == 'win32'

        p = Platform.CreateFromSimplePlatform('i686.win32')
        assert unicode(p) == 'win32'

        with pytest.raises(UnknownPlatform):
            Platform.Create(123)

        with pytest.raises(UnknownPlatform):
            Platform.CreateFromSimplePlatform('UNKNOWN')

    def testGetCurrentPlatform(self, monkeypatch):
        '''
        This is a white box test, but I found it necessary to full coverage.
        '''
        monkeypatch.setattr(sys, 'platform', 'win32')
        monkeypatch.setattr(platform, 'python_compiler', lambda:'WINDOWS')
        assert unicode(Platform.GetCurrentPlatform()) == 'win32'
        assert unicode(Platform.GetDefaultPlatform()) == 'win32'

        monkeypatch.setattr(platform, 'python_compiler', lambda:'AMD64')
        assert unicode(Platform.GetCurrentPlatform()) == 'win64'
        assert unicode(Platform.GetDefaultPlatform()) == 'win32'

        monkeypatch.setattr(sys, 'platform', 'darwin')
        assert unicode(Platform.GetCurrentPlatform()) == 'darwin64'
        assert unicode(Platform.GetDefaultPlatform()) == 'darwin64'

        monkeypatch.setattr(sys, 'platform', 'linux2')
        monkeypatch.setattr(platform, 'dist', lambda:['fedora'])
        monkeypatch.setattr(platform, 'machine', lambda:'x86_64')
        assert unicode(Platform.GetCurrentPlatform()) == 'redhat64'
        assert unicode(Platform.GetDefaultPlatform()) == 'redhat64'

    def testGetOSPlatform(self, monkeypatch):
        monkeypatch.setattr(sys, 'platform', 'win32')
        monkeypatch.setattr(platform, 'python_compiler', lambda:'WINDOWS')

        monkeypatch.setattr(os, 'environ', {})
        assert unicode(Platform.GetOSPlatform()) == 'win32'

        monkeypatch.setattr(os, 'environ', {'PROGRAMFILES(X86)':''})
        assert unicode(Platform.GetOSPlatform()) == 'win64'

        monkeypatch.setattr(sys, 'platform', 'linux2')
        monkeypatch.setattr(platform, 'dist', lambda:['fedora'])
        monkeypatch.setattr(platform, 'machine', lambda:'x86_64')
        assert unicode(Platform.GetOSPlatform()) == 'redhat64'

    def testFlags(self, monkeypatch):
        assert Platform.GetAllFlags() == {
            'windows', 'linux', 'darwin',
            'win32', 'win32d', 'win64', 'win64d',
            'centos64', 'centos32', 'centos32d', 'centos64d',
            'darwin32', 'darwin32d', 'darwin64', 'darwin64d',
            'debian64', 'debian32', 'debian64d', 'debian32d',
            'redhat32', 'redhat32d', 'redhat64', 'redhat64d',
            'ubuntu64', 'ubuntu32', 'ubuntu32d', 'ubuntu64d',
        }

        platform = Platform.Create('win32')
        assert platform.GetFlags() == {'windows', 'win32'}

        platform = Platform.Create('win32d')
        assert platform.GetFlags() == {'windows', 'win32', 'win32d', 'debug'}

        platform = Platform.Create('redhat64')
        assert platform.GetFlags() == {'linux', 'redhat64'}

        platform = Platform.Create('centos64')
        assert platform.GetFlags() == {'linux', 'centos64'}
<|endoftext|>


<commit_before>import itertools

from future import utils
if utils.PY2:
    from __builtin__ import max as _builtin_max, min as _builtin_min
else:
    from builtins import max as _builtin_max, min as _builtin_min

_SENTINEL = object()

def newmin(*args, **kwargs):
    return new_min_max(_builtin_min, *args, **kwargs)

def newmax(*args, **kwargs):
    return new_min_max(_builtin_max, *args, **kwargs)

def new_min_max(_builtin_func, *args, **kwargs):
    """
    To support the argument "default" introduced in python 3.4 for min and max
    :param _builtin_func: builtin min or builtin max
    :param args:
    :param kwargs:
    :return: returns the min or max based on the arguments passed
    """

    for key, _ in kwargs.items():
        if key not in set(['key', 'default']):
            raise TypeError('Illegal argument %s', key)

    if len(args) == 0:
        raise TypeError

    if len(args) != 1 and kwargs.get('default', _SENTINEL) is not _SENTINEL:
        raise TypeError

    if len(args) == 1:
        iterator = iter(args[0])
        try:
            first = next(iterator)
        except StopIteration:
            if kwargs.get('default', _SENTINEL) is not _SENTINEL:
                return kwargs.get('default')
            else:
                raise ValueError('iterable is an empty sequence')
        else:
            iterator = itertools.chain([first], iterator)
        if kwargs.get('key') is not None:
            return _builtin_func(iterator, key=kwargs.get('key'))
        else:
            return _builtin_func(iterator)

    if len(args) > 1:
        if kwargs.get('key') is not None:
            return _builtin_func(args, key=kwargs.get('key'))
        else:
            return _builtin_func(args)
<commit_msg>Make new min/max error message consistent builtin error message<commit_after>import itertools

from future import utils
if utils.PY2:
    from __builtin__ import max as _builtin_max, min as _builtin_min
else:
    from builtins import max as _builtin_max, min as _builtin_min

_SENTINEL = object()

def newmin(*args, **kwargs):
    return new_min_max(_builtin_min, *args, **kwargs)

def newmax(*args, **kwargs):
    return new_min_max(_builtin_max, *args, **kwargs)

def new_min_max(_builtin_func, *args, **kwargs):
    """
    To support the argument "default" introduced in python 3.4 for min and max
    :param _builtin_func: builtin min or builtin max
    :param args:
    :param kwargs:
    :return: returns the min or max based on the arguments passed
    """

    for key, _ in kwargs.items():
        if key not in set(['key', 'default']):
            raise TypeError('Illegal argument %s', key)

    if len(args) == 0:
        raise TypeError

    if len(args) != 1 and kwargs.get('default', _SENTINEL) is not _SENTINEL:
        raise TypeError

    if len(args) == 1:
        iterator = iter(args[0])
        try:
            first = next(iterator)
        except StopIteration:
            if kwargs.get('default', _SENTINEL) is not _SENTINEL:
                return kwargs.get('default')
            else:
                raise ValueError('{}() arg is an empty sequence'.format(_builtin_func.__name__))
        else:
            iterator = itertools.chain([first], iterator)
        if kwargs.get('key') is not None:
            return _builtin_func(iterator, key=kwargs.get('key'))
        else:
            return _builtin_func(iterator)

    if len(args) > 1:
        if kwargs.get('key') is not None:
            return _builtin_func(args, key=kwargs.get('key'))
        else:
            return _builtin_func(args)
<|endoftext|>


<commit_before>from abc import ABCMeta, abstractmethod, abstractproperty
from pliers.transformers import Transformer, CollectionStimMixin
from six import with_metaclass
from pliers.utils import memory
import importlib
from types import GeneratorType
from pliers import config
import pliers

class Converter(with_metaclass(ABCMeta, Transformer)):
    ''' Base class for Converters.'''

    def __init__(self):
        super(Converter, self).__init__()
        if config.cache_converters:
            self.transform = memory.cache(self.transform)

    @abstractmethod
    def _convert(self, stim):
        pass

    @abstractproperty
    def _output_type(self):
        pass

    def _transform(self, stim, *args, **kwargs):
        new_stim = self._convert(stim, *args, **kwargs)
        if isinstance(new_stim, (list, tuple, GeneratorType)):
            return new_stim
        if new_stim.name is None:
            new_stim.name = stim.name
        else:
            new_stim.name = stim.name + '->' + new_stim.name
        if isinstance(new_stim, CollectionStimMixin):
            for s in new_stim:
                if s.name is None:
                    s.name = stim.name
        return new_stim

def get_converter(in_type, out_type, *args, **kwargs):
    ''' Scans the list of available Converters and returns an instantiation
    of the first one whose input and output types match those passed in.
    Args:
        in_type (type): The type of input the converter must have.
        out_type (type): The type of output the converter must have.
        args, kwargs: Optional positional and keyword arguments to pass onto
            matching Converter's initializer.
    '''
    convs = pliers.converters.__all__

    # If config includes default converters for this combination, try them first
    conv_str = '%s->%s' % (in_type.__name__, out_type.__name__)
    if conv_str in config.default_converters:
        convs = list(config.default_converters[conv_str]) + convs

    for name in convs:
        cls = getattr(pliers.converters, name)
        if not issubclass(cls, Converter):
            continue

        if cls._input_type == in_type and cls._output_type == out_type and cls.available:
            try:
                conv = cls(*args, **kwargs)
                return conv
            except ValueError:
                # Important for API converters
                pass

    return None
<commit_msg>stop renaming stims post-conversion until we figure out a better approach<commit_after>from abc import ABCMeta, abstractmethod, abstractproperty
from pliers.transformers import Transformer, CollectionStimMixin
from six import with_metaclass
from pliers.utils import memory
import importlib
from types import GeneratorType
from pliers import config
import pliers

class Converter(with_metaclass(ABCMeta, Transformer)):
    ''' Base class for Converters.'''

    def __init__(self):
        super(Converter, self).__init__()
        if config.cache_converters:
            self.transform = memory.cache(self.transform)

    @abstractmethod
    def _convert(self, stim):
        pass

    @abstractproperty
    def _output_type(self):
        pass

    def _transform(self, stim, *args, **kwargs):
        return self._convert(stim, *args, **kwargs)

def get_converter(in_type, out_type, *args, **kwargs):
    ''' Scans the list of available Converters and returns an instantiation
    of the first one whose input and output types match those passed in.
    Args:
        in_type (type): The type of input the converter must have.
        out_type (type): The type of output the converter must have.
        args, kwargs: Optional positional and keyword arguments to pass onto
            matching Converter's initializer.
    '''
    convs = pliers.converters.__all__

    # If config includes default converters for this combination, try them first
    conv_str = '%s->%s' % (in_type.__name__, out_type.__name__)
    if conv_str in config.default_converters:
        convs = list(config.default_converters[conv_str]) + convs

    for name in convs:
        cls = getattr(pliers.converters, name)
        if not issubclass(cls, Converter):
            continue

        if cls._input_type == in_type and cls._output_type == out_type and cls.available:
            try:
                conv = cls(*args, **kwargs)
                return conv
            except ValueError:
                # Important for API converters
                pass

    return None
<|endoftext|>


<commit_before>"""Miscellaneous conversion classes."""

from pliers.extractors import ExtractorResult
from pliers.stimuli import SeriesStim
from .base import Converter

class ExtractorResultToSeriesConverter(Converter):
    """Converts an ExtractorResult instance to a list of SeriesStims."""

    _input_type = ExtractorResult
    _output_type = SeriesStim

    def _convert(self, result):
        df = result.to_df(timing=False, metadata=False, object_id=False)
        n_rows = df.shape[0]
        stims = []
        for i in range(n_rows):
            data = df.iloc[i, :]
            onset = result.onset[i] if result.onset is not None else None
            dur = result.duration[i] if result.duration is not None else None
            order = result.order[i] if result.order is not None else i
            st = SeriesStim(data, onset=onset, duration=dur, order=order)
            stims.append(st)
        return stims
<commit_msg>Apply suggestions from code review<commit_after>"""Miscellaneous conversion classes."""

from pliers.extractors import ExtractorResult
from pliers.stimuli import SeriesStim
from .base import Converter

class ExtractorResultToSeriesConverter(Converter):
    """Converts an ExtractorResult instance to a list of SeriesStims."""

    _input_type = ExtractorResult
    _output_type = SeriesStim

    def _convert(self, result):
        df = result.to_df(timing=False, metadata=False, object_id=False)
        stims = []
        for i, data in df.iterrows():
            onset = result.onset[i] if result.onset is not None else None
            dur = result.duration[i] if result.duration is not None else None
            order = result.order[i] if result.order is not None else i
            st = SeriesStim(data, onset=onset, duration=dur, order=order)
            stims.append(st)
        return stims
<|endoftext|>


<commit_before>import sys
import os

UPSTART = """description "VM-Series-Helper"
author "techbizdev@paloaltonetworks.com"

start on runlevel [2345]
stop on runlevel [!2345]

env AWS_REGION=%(awsregion)s
env AWS_ACCESS_KEY_ID=%(awsaccesskey)s
env AWS_SECRET_ACCESS_KEY=%(awssecretaccesskey)s
env "STACKNAME=%(stackname)s"
env "AWS_SQS_URL=%(awssqsurl)s"

respawn

exec python %(helper)s >> /var/log/helper.log 2>&1
"""

def main(args):

    environment = {
        'awsregion': os.environ['AWS_REGION'],
        'awsaccesskey': os.environ['AWS_ACCESS_KEY_ID'],
        'awssecretaccesskey': os.environ['AWS_SECRET_ACCESS_KEY'],
        'stackname': os.environ['STACKNAME'],
        'awssqsurl': os.environ['AWS_SQS_URL'],
        'helper': os.path.join(os.path.dirname(os.path.realpath(__file__)), 'helper.py')
    }

    print 'installing upstart script'
    f = open('/etc/init/vmsh.conf', 'w+b')
    f.write(UPSTART%environment)
    f.close()

if __name__ == "__main__":
    main(sys.argv[1:])
<commit_msg>fixed installer<commit_after>import sys
import os

UPSTART = """description "VM-Series-Helper"
author "techbizdev@paloaltonetworks.com"

start on runlevel [2345]
stop on runlevel [!2345]

env AWS_REGION=%(awsregion)s
env AWS_ACCESS_KEY_ID=%(awsaccesskey)s
env AWS_SECRET_ACCESS_KEY=%(awssecretaccesskey)s
env "KEYNAME=%(keyname)s"
env "AWS_SQS_URL=%(awssqsurl)s"

respawn

exec python %(helper)s >> /var/log/helper.log 2>&1
"""

def main(args):

    environment = {
        'awsregion': os.environ['AWS_REGION'],
        'awsaccesskey': os.environ['AWS_ACCESS_KEY_ID'],
        'awssecretaccesskey': os.environ['AWS_SECRET_ACCESS_KEY'],
        'keyname': os.environ['KEYNAME'],
        'awssqsurl': os.environ['AWS_SQS_URL'],
        'helper': os.path.join(os.path.dirname(os.path.realpath(__file__)), 'helper.py')
    }

    print 'installing upstart script'
    f = open('/etc/init/vmsh.conf', 'w+b')
    f.write(UPSTART%environment)
    f.close()

if __name__ == "__main__":
    main(sys.argv[1:])
<|endoftext|>


<commit_before>import subprocess
import os

class Installer:

  ############################################################
  # install_software
  ############################################################
  def install_software(self):
    self.__install_server_software()
    self.__install_client_software()
  ############################################################
  # End install_software
  ############################################################

  ############################################################
  # __install_server_software
  ############################################################
  def __install_server_software(self):
    #######################################
    # Prerequisites
    #######################################
    self.__run_command("sudo apt-get update", True)
    self.__run_command("sudo apt-get upgrade", True)
    self.__run_command("sudo apt-get install build-essential libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev python-software-properties unzip git-core libcurl4-openssl-dev libbz2-dev libmysqlclient-dev mongodb-clients libreadline6-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev libgdbm-dev ncurses-dev automake libffi-dev htop libtool bison", True)

    self.__run_command("cp ../config/benchmark_profile ../../.bash_profile")

    #######################################
    # Languages
    #######################################

    #
    # Python
    #

    self.__run_command("curl http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz | tar xvz")
    self.__run_command("./configure", cwd="Python-2.7.3")
    self.__run_command("sudo make install", cwd="Python-2.7.3")
    self.__run_command("curl http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz | tar xvz")
    self.__run_command("sudo python setup.py install", cwd="setuptools-0.6c11")
    self.__run_command("curl http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz | tar xvz")
    self.__run_command("sudo python setup.py install", cwd="pip-1.1")
    self.__run_command("sudo pip install MySQL-python==1.2.4")
    self.__run_command("sudo pip install simplejson==3.0.7")

    #
    # nodejs
    #

    self.__run_command("curl http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-x64.tar.gz | tar xvz")

    #
    # Java
    #

    self.__run_command("sudo apt-get install openjdk-7-jdk", True)
    self.__run_command("sudo apt-get remove --purge openjdk-6-jre openjdk-6-jre-headless", True)

    #
    # Ruby/JRuby
    #

    self.__run_command("curl -L get.rvm.io | bash -s head")
    self.__run_command("echo rvm_auto_reload_flag=2 >> ~/.rvmrc")
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm install 2.0.0-p0"])
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm 2.0.0-p0 do gem install bundler"])
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm install jruby-1.7.3"])
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do gem install bundler"])

    # We need a newer version of jruby-rack
    self.__run_command("git clone git://github.com/jruby/jruby-rack.git")
    subprocess.call(["bash", "-c", "cd installs/jruby-rack && source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do bundle install"])
    subprocess.call(["bash", "-c", "cd installs/jruby-rack && source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do jruby -S bundle exec rake clean gem SKIP_SPECS=true"])
    subprocess.call(["bash", "-c", "cd installs/jruby-rack/target && source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do gem install jruby-rack-1.2.0.SNAPSHOT.gem"])

    #
    # go
    #

    self.__run_command("curl http://go.googlecode.com/files/go1.0.3.linux-amd64.tar.gz | tar xvz")

    #
    # php
    #

    self.__run_command("wget --trust-server-names http://www.php.net/get/php-5.4.13.tar.gz/from/us1.php.net/mirror")
    self.__run_command("tar xvf php-5.4.13.tar.gz")
    self.__run_command("./configure --with-pdo-mysql --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data", cwd="php-5.4.13")
    self.__run_command("make", cwd="php-5.4.13")
    self.__run_command("sudo make install", cwd="php-5.4.13")
    self.__run_command("sudo pecl install apc-beta", cwd="php-5.4.13")
    self.__run_command("sudo cp ../config/php.ini /usr/local/lib/php.ini")
    self.__run_command("sudo cp ../config/php-fpm.conf /usr/local/lib/php-fpm.conf")
    self.__run_command("rm php-5.4.13.tar.gz")

    #
    # Haskell
    #

    self.__run_command("sudo apt-get install ghc cabal-install", True)

    #######################################
    # Webservers
    #######################################

    #
    # Apache
    #

    self.__run_command("sudo apt-get install apache2 libapache2-mod-php5", True)
    self.__run_command("sudo mv /etc/apache2/apache2.conf /etc/apache2/apache2.conf.orig")
    self.__run_command("sudo sh -c \"cat ../config/apache2.conf > /etc/apache2/apache2.conf\"")
    self.__run_command("sudo mv /etc/apache2/ports.conf /etc/apache2/ports.conf.orig")
    self.__run_command("sudo sh -c \"cat ../config/ports.conf > /etc/apache2/ports.conf\"")
    self.__run_command("sudo /etc/init.d/apache2 stop")

    #
    # Nginx
    #
    self.__run_command("curl http://nginx.org/download/nginx-1.2.7.tar.gz | tar xvz")
    self.__run_command("./configure", cwd="nginx-1.2.7")
    self.__run_command("make", cwd="nginx-1.2.7")
    self.__run_command("sudo make install", cwd="nginx-1.2.7")

    #
    # Gunicorn
    #

    self.__run_command("sudo easy_install -U 'gunicorn==0.17.2'")
    self.__run_command("sudo easy_install -U 'eventlet==0.12.1'")
    self.__run_command("sudo pip install --upgrade 'gevent==0.13.8'")

    #
    # Resin
    #

    self.__run_command("sudo cp -r /usr/lib/jvm/java-1.7.0-openjdk-amd64/include /usr/lib/jvm/java-1.7.0-openjdk-amd64/jre/bin/")
    self.__run_command("curl http://www.caucho.com/download/resin-4.0.34.tar.gz | tar xvz")
    self.__run_command("./configure --prefix=`pwd`", cwd="resin-4.0.34")
    self.__run_command("make", cwd="resin-4.0.34")
    self.__run_command("make install", cwd="resin-4.0.34")
    self.__run_command("mv conf/resin.properties conf/resin.properties.orig", cwd="resin-4.0.34")
    self.__run_command("cat ../config/resin.properties > resin-4.0.34/conf/resin.properties")

    #
    # Passenger
    #

    self.__run_command("git clone https://github.com/FooBarWidget/passenger.git")
    self.__run_command("git checkout 65d36dbbadd399f65d81f5febadce9b0c6c1a430", cwd="passenger")
    subprocess.call(["bash", "-c", "cd installs/passenger && source ~/.rvm/scripts/'rvm' && rvm 2.0.0-p0 do gem build passenger.gemspec"])
    subprocess.call(["bash", "-c", "cd installs/passenger && source ~/.rvm/scripts/'rvm' && rvm 2.0.0-p0 do gem install passenger-3.9.5.rc3.gem"])

    ##############################
    # Tomcat
    # We don't use tomcat in our tests yet, but this is here to remind us of how we
    # installed the apr connector
    ##############################
    self.__run_command("curl http://apache.cs.utah.edu/tomcat/tomcat-7/v7.0.35/bin/apache-tomcat-7.0.35.tar.gz | tar xvz")
    #wget http://apache.cs.utah.edu/tomcat/tomcat-7/v7.0.35/bin/apache-tomcat-7.0.35.tar.gz
    #tar -xvzf apache-tomcat-7.0.35.tar.gz
    #rm apache-tomcat-7.0.35.tar.gz
    # use the native APR
    # http://evgeny-goldin.com/blog/ubuntu-installing-apr-tomcat/
    #wget http://apache.claz.org/apr/apr-1.4.6.tar.gz
    #tar xvf apr-1.4.6.tar.gz
    #cd apr-1.4.6
    #./configure
    #make
    #sudo make install
    #cd ..
    #rm apr-1.4.6.tar.gz
    #wget http://apache.tradebit.com/pub/tomcat/tomcat-connectors/native/1.1.24/source/tomcat-native-1.1.24-src.tar.gz
    #tar xvf tomcat-native-1.1.24-src.tar.gz
    #cd tomcat-native-1.1.24-src/jni/native
    #./configure --with-apr=/usr/local/apr
    #make
    #sudo make install
    #cd ../../..
    #rm tomcat-native-1.1.24-src.tar.gz

    ##############################################################
    #
    # Frameworks
    #
    ##############################################################

    ##############################
    # Django
    ##############################
    self.__run_command("curl http://www.djangoproject.com/m/releases/1.4/Django-1.4.tar.gz | tar xvz")
    self.__run_command("sudo rm -rf /usr/local/lib/python2.7/site-packages/django")
    self.__run_command("sudo python setup.py install", cwd="Django-1.4")
    self.__run_command("sudo easy_install -U 'ujson==1.30'")

    ##############################
    # Grails
    ##############################
    self.__run_command("wget http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.1.1.zip")
    self.__run_command("unzip -o grails-2.1.1.zip")
    self.__run_command("rm grails-2.1.1.zip")

    ##############################
    # Flask
    ##############################
    self.__run_command("sudo pip install flask flask-sqlalchemy")

    ##############################
    # Play
    ##############################
    self.__run_command("wget http://downloads.typesafe.com/play/2.1.0/play-2.1.0.zip")
    self.__run_command("unzip -o play-2.1.0.zip")
    self.__run_command("rm play-2.1.0.zip")

    ##############################
    # Vert.x
    ##############################
    self.__run_command("curl http://vertx.io/downloads/vert.x-1.3.1.final.tar.gz | tar xvz")

    ##############################
    # WebGO
    ##############################
    self.__run_command("go/bin/go get github.com/hoisie/web")

    ##############################
    # Yesod
    ##############################
    self.__run_command("cabal update")
    self.__run_command("cabal install yesod persistent-mysql")

    ##############################################################
    #
    # System Tools
    #
    ##############################################################

    ##############################
    # Maven
    ##############################
    self.__run_command("sudo apt-get install maven2", send_yes=True)

    ##############################
    # Leiningen
    ##############################
    self.__run_command("mkdir -p bin")
    self.__run_command("wget https://raw.github.com/technomancy/leiningen/stable/bin/lein")
    self.__run_command("mv lein bin/lein")
    self.__run_command("chmod +x bin/lein")
  ############################################################
  # End __install_server_software
  ############################################################

  ############################################################
  # __install_client_software
  ############################################################
  def __install_client_software(self):
    subprocess.call(self.benchmarker.sftp_string(batch_file="config/client_sftp_batch"), shell=True)

    remote_script = """

    ##############################
    # Prerequisites
    ##############################
    yes | sudo apt-get update
    yes | sudo apt-get install build-essential git libev-dev libpq-dev libreadline6-dev

    ##############################
    # MySQL
    ##############################
    sudo sh -c "echo mysql-server mysql-server/root_password_again select secret | debconf-set-selections"
    sudo sh -c "echo mysql-server mysql-server/root_password select secret | debconf-set-selections"

    yes | sudo apt-get install mysql-server

    # use the my.cnf file to overwrite /etc/mysql/my.cnf
    sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
    sudo mv my.cnf /etc/mysql/my.cnf
    sudo restart mysql

    # Insert data
    mysql -uroot -psecret < create.sql

    ##############################
    # Weighttp
    ##############################

    git clone git://git.lighttpd.net/weighttp
    cd weighttp
    ./waf configure
    ./waf build
    sudo ./waf install
    cd ~

    ##############################
    # wrk
    ##############################

    git clone https://github.com/wg/wrk.git
    cd wrk
    make
    sudo cp wrk /usr/local/bin
    cd ~

    ##############################
    # MongoDB
    ##############################
    yes | sudo apt-get install mongodb

    sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
    sudo mv mongodb.conf /etc/mongodb.conf
    sudo restart mongodb
    """

    p = subprocess.Popen(self.benchmarker.ssh_string.split(" "), stdin=subprocess.PIPE)
    p.communicate(remote_script)

  ############################################################
  # End __parse_results
  ############################################################

  ############################################################
  # __run_command
  ############################################################
  def __run_command(self, command, send_yes=False, cwd=None):
    try:
      cwd = os.path.join(self.install_dir, cwd)
    except AttributeError:
      cwd = self.install_dir

    if send_yes:
      subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, cwd=cwd).communicate("yes")
    else:
      subprocess.call(command, shell=True, cwd=cwd)
  ############################################################
  # End __run_command
  ############################################################

  ############################################################
  # __init__(benchmarker)
  ############################################################
  def __init__(self, benchmarker):
    self.benchmarker = benchmarker
    self.install_dir = "installs"

    try:
      os.mkdir(self.install_dir)
    except OSError:
      pass
  ############################################################
  # End __init__
  ############################################################
<commit_msg>removed necessity of user input on apc install<commit_after>import subprocess
import os

class Installer:

  ############################################################
  # install_software
  ############################################################
  def install_software(self):
    self.__install_server_software()
    self.__install_client_software()
  ############################################################
  # End install_software
  ############################################################

  ############################################################
  # __install_server_software
  ############################################################
  def __install_server_software(self):
    #######################################
    # Prerequisites
    #######################################
    self.__run_command("sudo apt-get update", True)
    self.__run_command("sudo apt-get upgrade", True)
    self.__run_command("sudo apt-get install build-essential libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev python-software-properties unzip git-core libcurl4-openssl-dev libbz2-dev libmysqlclient-dev mongodb-clients libreadline6-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev libgdbm-dev ncurses-dev automake libffi-dev htop libtool bison", True)

    self.__run_command("cp ../config/benchmark_profile ../../.bash_profile")

    #######################################
    # Languages
    #######################################

    #
    # Python
    #

    self.__run_command("curl http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz | tar xvz")
    self.__run_command("./configure", cwd="Python-2.7.3")
    self.__run_command("sudo make install", cwd="Python-2.7.3")
    self.__run_command("curl http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz | tar xvz")
    self.__run_command("sudo python setup.py install", cwd="setuptools-0.6c11")
    self.__run_command("curl http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz | tar xvz")
    self.__run_command("sudo python setup.py install", cwd="pip-1.1")
    self.__run_command("sudo pip install MySQL-python==1.2.4")
    self.__run_command("sudo pip install simplejson==3.0.7")

    #
    # nodejs
    #

    self.__run_command("curl http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-x64.tar.gz | tar xvz")

    #
    # Java
    #

    self.__run_command("sudo apt-get install openjdk-7-jdk", True)
    self.__run_command("sudo apt-get remove --purge openjdk-6-jre openjdk-6-jre-headless", True)

    #
    # Ruby/JRuby
    #

    self.__run_command("curl -L get.rvm.io | bash -s head")
    self.__run_command("echo rvm_auto_reload_flag=2 >> ~/.rvmrc")
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm install 2.0.0-p0"])
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm 2.0.0-p0 do gem install bundler"])
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm install jruby-1.7.3"])
    subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do gem install bundler"])

    # We need a newer version of jruby-rack
    self.__run_command("git clone git://github.com/jruby/jruby-rack.git")
    subprocess.call(["bash", "-c", "cd installs/jruby-rack && source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do bundle install"])
    subprocess.call(["bash", "-c", "cd installs/jruby-rack && source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do jruby -S bundle exec rake clean gem SKIP_SPECS=true"])
    subprocess.call(["bash", "-c", "cd installs/jruby-rack/target && source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do gem install jruby-rack-1.2.0.SNAPSHOT.gem"])

    #
    # go
    #

    self.__run_command("curl http://go.googlecode.com/files/go1.0.3.linux-amd64.tar.gz | tar xvz")

    #
    # php
    #

    self.__run_command("wget --trust-server-names http://www.php.net/get/php-5.4.13.tar.gz/from/us1.php.net/mirror")
    self.__run_command("tar xvf php-5.4.13.tar.gz")
    self.__run_command("./configure --with-pdo-mysql --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data", cwd="php-5.4.13")
    self.__run_command("make", cwd="php-5.4.13")
    self.__run_command("sudo make install", cwd="php-5.4.13")
    self.__run_command("printf \"\\n\" | sudo pecl install apc-beta", cwd="php-5.4.13")
    self.__run_command("sudo cp ../config/php.ini /usr/local/lib/php.ini")
    self.__run_command("sudo cp ../config/php-fpm.conf /usr/local/lib/php-fpm.conf")
    self.__run_command("rm php-5.4.13.tar.gz")

    #
    # Haskell
    #

    self.__run_command("sudo apt-get install ghc cabal-install", True)

    #######################################
    # Webservers
    #######################################

    #
    # Apache
    #

    self.__run_command("sudo apt-get install apache2 libapache2-mod-php5", True)
    self.__run_command("sudo mv /etc/apache2/apache2.conf /etc/apache2/apache2.conf.orig")
    self.__run_command("sudo sh -c \"cat ../config/apache2.conf > /etc/apache2/apache2.conf\"")
    self.__run_command("sudo mv /etc/apache2/ports.conf /etc/apache2/ports.conf.orig")
    self.__run_command("sudo sh -c \"cat ../config/ports.conf > /etc/apache2/ports.conf\"")
    self.__run_command("sudo /etc/init.d/apache2 stop")

    #
    # Nginx
    #
    self.__run_command("curl http://nginx.org/download/nginx-1.2.7.tar.gz | tar xvz")
    self.__run_command("./configure", cwd="nginx-1.2.7")
    self.__run_command("make", cwd="nginx-1.2.7")
    self.__run_command("sudo make install", cwd="nginx-1.2.7")

    #
    # Gunicorn
    #

    self.__run_command("sudo easy_install -U 'gunicorn==0.17.2'")
    self.__run_command("sudo easy_install -U 'eventlet==0.12.1'")
    self.__run_command("sudo pip install --upgrade 'gevent==0.13.8'")

    #
    # Resin
    #

    self.__run_command("sudo cp -r /usr/lib/jvm/java-1.7.0-openjdk-amd64/include /usr/lib/jvm/java-1.7.0-openjdk-amd64/jre/bin/")
    self.__run_command("curl http://www.caucho.com/download/resin-4.0.34.tar.gz | tar xvz")
    self.__run_command("./configure --prefix=`pwd`", cwd="resin-4.0.34")
    self.__run_command("make", cwd="resin-4.0.34")
    self.__run_command("make install", cwd="resin-4.0.34")
    self.__run_command("mv conf/resin.properties conf/resin.properties.orig", cwd="resin-4.0.34")
    self.__run_command("cat ../config/resin.properties > resin-4.0.34/conf/resin.properties")

    #
    # Passenger
    #

    self.__run_command("git clone https://github.com/FooBarWidget/passenger.git")
    self.__run_command("git checkout 65d36dbbadd399f65d81f5febadce9b0c6c1a430", cwd="passenger")
    subprocess.call(["bash", "-c", "cd installs/passenger && source ~/.rvm/scripts/'rvm' && rvm 2.0.0-p0 do gem build passenger.gemspec"])
    subprocess.call(["bash", "-c", "cd installs/passenger && source ~/.rvm/scripts/'rvm' && rvm 2.0.0-p0 do gem install passenger-3.9.5.rc3.gem"])

    ##############################
    # Tomcat
    # We don't use tomcat in our tests yet, but this is here to remind us of how we
    # installed the apr connector
    ##############################
    self.__run_command("curl http://apache.cs.utah.edu/tomcat/tomcat-7/v7.0.35/bin/apache-tomcat-7.0.35.tar.gz | tar xvz")
    #wget http://apache.cs.utah.edu/tomcat/tomcat-7/v7.0.35/bin/apache-tomcat-7.0.35.tar.gz
    #tar -xvzf apache-tomcat-7.0.35.tar.gz
    #rm apache-tomcat-7.0.35.tar.gz
    # use the native APR
    # http://evgeny-goldin.com/blog/ubuntu-installing-apr-tomcat/
    #wget http://apache.claz.org/apr/apr-1.4.6.tar.gz
    #tar xvf apr-1.4.6.tar.gz
    #cd apr-1.4.6
    #./configure
    #make
    #sudo make install
    #cd ..
    #rm apr-1.4.6.tar.gz
    #wget http://apache.tradebit.com/pub/tomcat/tomcat-connectors/native/1.1.24/source/tomcat-native-1.1.24-src.tar.gz
    #tar xvf tomcat-native-1.1.24-src.tar.gz
    #cd tomcat-native-1.1.24-src/jni/native
    #./configure --with-apr=/usr/local/apr
    #make
    #sudo make install
    #cd ../../..
    #rm tomcat-native-1.1.24-src.tar.gz

    ##############################################################
    #
    # Frameworks
    #
    ##############################################################

    ##############################
    # Django
    ##############################
    self.__run_command("curl http://www.djangoproject.com/m/releases/1.4/Django-1.4.tar.gz | tar xvz")
    self.__run_command("sudo rm -rf /usr/local/lib/python2.7/site-packages/django")
    self.__run_command("sudo python setup.py install", cwd="Django-1.4")
    self.__run_command("sudo easy_install -U 'ujson==1.30'")

    ##############################
    # Grails
    ##############################
    self.__run_command("wget http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.1.1.zip")
    self.__run_command("unzip -o grails-2.1.1.zip")
    self.__run_command("rm grails-2.1.1.zip")

    ##############################
    # Flask
    ##############################
    self.__run_command("sudo pip install flask flask-sqlalchemy")

    ##############################
    # Play
    ##############################
    self.__run_command("wget http://downloads.typesafe.com/play/2.1.0/play-2.1.0.zip")
    self.__run_command("unzip -o play-2.1.0.zip")
    self.__run_command("rm play-2.1.0.zip")

    ##############################
    # Vert.x
    ##############################
    self.__run_command("curl http://vertx.io/downloads/vert.x-1.3.1.final.tar.gz | tar xvz")

    ##############################
    # WebGO
    ##############################
    self.__run_command("go/bin/go get github.com/hoisie/web")

    ##############################
    # Yesod
    ##############################
    self.__run_command("cabal update")
    self.__run_command("cabal install yesod persistent-mysql")

    ##############################################################
    #
    # System Tools
    #
    ##############################################################

    ##############################
    # Maven
    ##############################
    self.__run_command("sudo apt-get install maven2", send_yes=True)

    ##############################
    # Leiningen
    ##############################
    self.__run_command("mkdir -p bin")
    self.__run_command("wget https://raw.github.com/technomancy/leiningen/stable/bin/lein")
    self.__run_command("mv lein bin/lein")
    self.__run_command("chmod +x bin/lein")
  ############################################################
  # End __install_server_software
  ############################################################

  ############################################################
  # __install_client_software
  ############################################################
  def __install_client_software(self):
    subprocess.call(self.benchmarker.sftp_string(batch_file="config/client_sftp_batch"), shell=True)

    remote_script = """

    ##############################
    # Prerequisites
    ##############################
    yes | sudo apt-get update
    yes | sudo apt-get install build-essential git libev-dev libpq-dev libreadline6-dev

    ##############################
    # MySQL
    ##############################
    sudo sh -c "echo mysql-server mysql-server/root_password_again select secret | debconf-set-selections"
    sudo sh -c "echo mysql-server mysql-server/root_password select secret | debconf-set-selections"

    yes | sudo apt-get install mysql-server

    # use the my.cnf file to overwrite /etc/mysql/my.cnf
    sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
    sudo mv my.cnf /etc/mysql/my.cnf
    sudo restart mysql

    # Insert data
    mysql -uroot -psecret < create.sql

    ##############################
    # Weighttp
    ##############################

    git clone git://git.lighttpd.net/weighttp
    cd weighttp
    ./waf configure
    ./waf build
    sudo ./waf install
    cd ~

    ##############################
    # wrk
    ##############################

    git clone https://github.com/wg/wrk.git
    cd wrk
    make
    sudo cp wrk /usr/local/bin
    cd ~

    ##############################
    # MongoDB
    ##############################
    yes | sudo apt-get install mongodb

    sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
    sudo mv mongodb.conf /etc/mongodb.conf
    sudo restart mongodb
    """

    p = subprocess.Popen(self.benchmarker.ssh_string.split(" "), stdin=subprocess.PIPE)
    p.communicate(remote_script)

  ############################################################
  # End __parse_results
  ############################################################

  ############################################################
  # __run_command
  ############################################################
  def __run_command(self, command, send_yes=False, cwd=None):
    try:
      cwd = os.path.join(self.install_dir, cwd)
    except AttributeError:
      cwd = self.install_dir

    if send_yes:
      subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, cwd=cwd).communicate("yes")
    else:
      subprocess.call(command, shell=True, cwd=cwd)
  ############################################################
  # End __run_command
  ############################################################

  ############################################################
  # __init__(benchmarker)
  ############################################################
  def __init__(self, benchmarker):
    self.benchmarker = benchmarker
    self.install_dir = "installs"

    try:
      os.mkdir(self.install_dir)
    except OSError:
      pass
  ############################################################
  # End __init__
  ############################################################
<|endoftext|>