<commit_before>"""Database models used by django-reversion."""

from __future__ import unicode_literals

import warnings
from functools import partial

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.conf import settings
from django.core import serializers
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, IntegrityError
from django.db.models.signals import pre_save, post_save
from django.dispatch.dispatcher import Signal, _make_id
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_text, python_2_unicode_compatible

def safe_revert(versions):
    """
    Attempts to revert the given models contained in the give versions.

    This method will attempt to resolve dependencies between the versions to revert
    them in the correct order to avoid database integrity errors.
    """
    unreverted_versions = []
    for version in versions:
        try:
            version.revert()
        except (IntegrityError, ObjectDoesNotExist):
            unreverted_versions.append(version)
    if len(unreverted_versions) == len(versions):
        raise RevertError("Could not revert revision, due to database integrity errors.")
    if unreverted_versions:
        safe_revert(unreverted_versions)

class RevertError(Exception):

    """Exception thrown when something goes wrong with reverting a model."""

UserModel = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')

@python_2_unicode_compatible
class Revision(models.Model):

    """A group of related object versions."""

    manager_slug = models.CharField(
        max_length = 200,
        db_index = True,
        default = "default",
    )

    date_created = models.DateTimeField(auto_now_add=True,
                                        verbose_name=_("date created"),
                                        help_text="The date and time this revision was created.")

    user = models.ForeignKey(UserModel,
                             blank=True,
                             null=True,
                             verbose_name=_("user"),
                             help_text="The user who created this revision.")

    comment = models.TextField(blank=True,
                               verbose_name=_("comment"),
                               help_text="A text comment on this revision.")

    def revert(self, delete=False):
        """Reverts all objects in this revision."""
        version_set = self.version_set.all()
        # Optionally delete objects no longer in the current revision.
        if delete:
            # Get a dict of all objects in this revision.
            old_revision = {}
            for version in version_set:
                try:
                    obj = version.object
                except ContentType.objects.get_for_id(version.content_type_id).model_class().DoesNotExist:
                    pass
                else:
                    old_revision[obj] = version
            # Calculate the set of all objects that are in the revision now.
            from reversion.revisions import RevisionManager
            current_revision = RevisionManager.get_manager(self.manager_slug)._follow_relationships(obj for obj in old_revision.keys() if obj is not None)
            # Delete objects that are no longer in the current revision.
            for item in current_revision:
                if item in old_revision:
                    if old_revision[item].type == VERSION_DELETE:
                        item.delete()
                else:
                    item.delete()
        # Attempt to revert all revisions.
        safe_revert([version for version in version_set if version.type != VERSION_DELETE])

    def __str__(self):
        """Returns a unicode representation."""
        return ", ".join(force_text(version) for version in self.version_set.all())

# Version types.

VERSION_ADD = 0
VERSION_CHANGE = 1
VERSION_DELETE = 2

VERSION_TYPE_CHOICES = (
    (VERSION_ADD, "Addition"),
    (VERSION_CHANGE, "Change"),
    (VERSION_DELETE, "Deletion"),
)

def has_int_pk(model):
    """Tests whether the given model has an integer primary key."""
    pk = model._meta.pk
    return (
        (
            isinstance(pk, (models.IntegerField, models.AutoField)) and
            not isinstance(pk, models.BigIntegerField)
        ) or (
            isinstance(pk, models.ForeignKey) and has_int_pk(pk.rel.to)
        )
    )

@python_2_unicode_compatible
class Version(models.Model):

    """A saved version of a database model."""

    revision = models.ForeignKey(Revision,
                                 help_text="The revision that contains this version.")

    object_id = models.TextField(help_text="Primary key of the model under version control.")

    object_id_int = models.IntegerField(
        blank = True,
        null = True,
        db_index = True,
        help_text = "An indexed, integer version of the stored model's primary key, used for faster lookups.",
    )

    content_type = models.ForeignKey(ContentType,
                                     help_text="Content type of the model under version control.")

    # A link to the current instance, not the version stored in this Version!
    object = generic.GenericForeignKey()

    format = models.CharField(max_length=255,
                              help_text="The serialization format used by this model.")

    serialized_data = models.TextField(help_text="The serialized form of this version of the model.")

    object_repr = models.TextField(help_text="A string representation of the object.")

    @property
    def object_version(self):
        """The stored version of the model."""
        data = self.serialized_data
        data = force_text(data.encode("utf8"))
        return list(serializers.deserialize(self.format, data, ignorenonexistent=True))[0]

    type = models.PositiveSmallIntegerField(choices=VERSION_TYPE_CHOICES, db_index=True)

    @property
    def field_dict(self):
        """
        A dictionary mapping field names to field values in this version
        of the model.

        This method will follow parent links, if present.
        """
        if not hasattr(self, "_field_dict_cache"):
            object_version = self.object_version
            obj = object_version.object
            result = {}
            for field in obj._meta.fields:
                result[field.name] = field.value_from_object(obj)
            result.update(object_version.m2m_data)
            # Add parent data.
            for parent_class, field in obj._meta.parents.items():
                content_type = ContentType.objects.get_for_model(parent_class)
                if field:
                    parent_id = force_text(getattr(obj, field.attname))
                else:
                    parent_id = obj.pk
                try:
                    parent_version = Version.objects.get(revision__id=self.revision_id,
                                                         content_type=content_type,
                                                         object_id=parent_id)
                except Version.DoesNotExist:
                    pass
                else:
                    result.update(parent_version.field_dict)
            setattr(self, "_field_dict_cache", result)
        return getattr(self, "_field_dict_cache")

    def revert(self):
        """Recovers the model in this version."""
        self.object_version.save()

    def __str__(self):
        """Returns a unicode representation."""
        return self.object_repr

# Version management signals.
pre_revision_commit = Signal(providing_args=["instances", "revision", "versions"])
post_revision_commit = Signal(providing_args=["instances", "revision", "versions"])

def check_for_receivers(sender, sending_signal, **kwargs):
    """Checks that no other signal receivers have been connected."""
    if len(sending_signal._live_receivers(_make_id(sender))) > 1:
        warnings.warn("pre_save and post_save signals will not longer be sent for Revision and Version models in django-reversion 1.8. Please use the pre_revision_commit and post_revision_commit signals instead.")

check_for_pre_save_receivers = partial(check_for_receivers, sending_signal=pre_save)
check_for_post_save_receivers = partial(check_for_receivers, sending_signal=post_save)

pre_save.connect(check_for_pre_save_receivers, sender=Revision)
pre_save.connect(check_for_pre_save_receivers, sender=Version)
post_save.connect(check_for_post_save_receivers, sender=Revision)
post_save.connect(check_for_post_save_receivers, sender=Version)
<commit_msg>Update check_for_receivers for django 1.6.<commit_after>"""Database models used by django-reversion."""

from __future__ import unicode_literals

import warnings
from functools import partial

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.conf import settings
from django.core import serializers
from django.core.exceptions import ObjectDoesNotExist
from django.db import models, IntegrityError
from django.db.models.signals import pre_save, post_save
from django.dispatch.dispatcher import Signal
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_text, python_2_unicode_compatible

def safe_revert(versions):
    """
    Attempts to revert the given models contained in the give versions.

    This method will attempt to resolve dependencies between the versions to revert
    them in the correct order to avoid database integrity errors.
    """
    unreverted_versions = []
    for version in versions:
        try:
            version.revert()
        except (IntegrityError, ObjectDoesNotExist):
            unreverted_versions.append(version)
    if len(unreverted_versions) == len(versions):
        raise RevertError("Could not revert revision, due to database integrity errors.")
    if unreverted_versions:
        safe_revert(unreverted_versions)

class RevertError(Exception):

    """Exception thrown when something goes wrong with reverting a model."""

UserModel = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')

@python_2_unicode_compatible
class Revision(models.Model):

    """A group of related object versions."""

    manager_slug = models.CharField(
        max_length = 200,
        db_index = True,
        default = "default",
    )

    date_created = models.DateTimeField(auto_now_add=True,
                                        verbose_name=_("date created"),
                                        help_text="The date and time this revision was created.")

    user = models.ForeignKey(UserModel,
                             blank=True,
                             null=True,
                             verbose_name=_("user"),
                             help_text="The user who created this revision.")

    comment = models.TextField(blank=True,
                               verbose_name=_("comment"),
                               help_text="A text comment on this revision.")

    def revert(self, delete=False):
        """Reverts all objects in this revision."""
        version_set = self.version_set.all()
        # Optionally delete objects no longer in the current revision.
        if delete:
            # Get a dict of all objects in this revision.
            old_revision = {}
            for version in version_set:
                try:
                    obj = version.object
                except ContentType.objects.get_for_id(version.content_type_id).model_class().DoesNotExist:
                    pass
                else:
                    old_revision[obj] = version
            # Calculate the set of all objects that are in the revision now.
            from reversion.revisions import RevisionManager
            current_revision = RevisionManager.get_manager(self.manager_slug)._follow_relationships(obj for obj in old_revision.keys() if obj is not None)
            # Delete objects that are no longer in the current revision.
            for item in current_revision:
                if item in old_revision:
                    if old_revision[item].type == VERSION_DELETE:
                        item.delete()
                else:
                    item.delete()
        # Attempt to revert all revisions.
        safe_revert([version for version in version_set if version.type != VERSION_DELETE])

    def __str__(self):
        """Returns a unicode representation."""
        return ", ".join(force_text(version) for version in self.version_set.all())

# Version types.

VERSION_ADD = 0
VERSION_CHANGE = 1
VERSION_DELETE = 2

VERSION_TYPE_CHOICES = (
    (VERSION_ADD, "Addition"),
    (VERSION_CHANGE, "Change"),
    (VERSION_DELETE, "Deletion"),
)

def has_int_pk(model):
    """Tests whether the given model has an integer primary key."""
    pk = model._meta.pk
    return (
        (
            isinstance(pk, (models.IntegerField, models.AutoField)) and
            not isinstance(pk, models.BigIntegerField)
        ) or (
            isinstance(pk, models.ForeignKey) and has_int_pk(pk.rel.to)
        )
    )

@python_2_unicode_compatible
class Version(models.Model):

    """A saved version of a database model."""

    revision = models.ForeignKey(Revision,
                                 help_text="The revision that contains this version.")

    object_id = models.TextField(help_text="Primary key of the model under version control.")

    object_id_int = models.IntegerField(
        blank = True,
        null = True,
        db_index = True,
        help_text = "An indexed, integer version of the stored model's primary key, used for faster lookups.",
    )

    content_type = models.ForeignKey(ContentType,
                                     help_text="Content type of the model under version control.")

    # A link to the current instance, not the version stored in this Version!
    object = generic.GenericForeignKey()

    format = models.CharField(max_length=255,
                              help_text="The serialization format used by this model.")

    serialized_data = models.TextField(help_text="The serialized form of this version of the model.")

    object_repr = models.TextField(help_text="A string representation of the object.")

    @property
    def object_version(self):
        """The stored version of the model."""
        data = self.serialized_data
        data = force_text(data.encode("utf8"))
        return list(serializers.deserialize(self.format, data, ignorenonexistent=True))[0]

    type = models.PositiveSmallIntegerField(choices=VERSION_TYPE_CHOICES, db_index=True)

    @property
    def field_dict(self):
        """
        A dictionary mapping field names to field values in this version
        of the model.

        This method will follow parent links, if present.
        """
        if not hasattr(self, "_field_dict_cache"):
            object_version = self.object_version
            obj = object_version.object
            result = {}
            for field in obj._meta.fields:
                result[field.name] = field.value_from_object(obj)
            result.update(object_version.m2m_data)
            # Add parent data.
            for parent_class, field in obj._meta.parents.items():
                content_type = ContentType.objects.get_for_model(parent_class)
                if field:
                    parent_id = force_text(getattr(obj, field.attname))
                else:
                    parent_id = obj.pk
                try:
                    parent_version = Version.objects.get(revision__id=self.revision_id,
                                                         content_type=content_type,
                                                         object_id=parent_id)
                except Version.DoesNotExist:
                    pass
                else:
                    result.update(parent_version.field_dict)
            setattr(self, "_field_dict_cache", result)
        return getattr(self, "_field_dict_cache")

    def revert(self):
        """Recovers the model in this version."""
        self.object_version.save()

    def __str__(self):
        """Returns a unicode representation."""
        return self.object_repr

# Version management signals.
pre_revision_commit = Signal(providing_args=["instances", "revision", "versions"])
post_revision_commit = Signal(providing_args=["instances", "revision", "versions"])

def check_for_receivers(sender, sending_signal, **kwargs):
    """Checks that no other signal receivers have been connected."""
    if len(sending_signal._live_receivers(sender)) > 1:
        warnings.warn("pre_save and post_save signals will not longer be sent for Revision and Version models in django-reversion 1.8. Please use the pre_revision_commit and post_revision_commit signals instead.")

check_for_pre_save_receivers = partial(check_for_receivers, sending_signal=pre_save)
check_for_post_save_receivers = partial(check_for_receivers, sending_signal=post_save)

pre_save.connect(check_for_pre_save_receivers, sender=Revision)
pre_save.connect(check_for_pre_save_receivers, sender=Version)
post_save.connect(check_for_post_save_receivers, sender=Revision)
post_save.connect(check_for_post_save_receivers, sender=Version)
<|endoftext|>


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

########################################################################################################
########################################################################################################
########################################################################################################

### IMPORTS ############################################################################################

from __future__ import print_function
import os
import sys
import argparse

import modu.bottle as bottle
import modu.slurm as slurm

app = bottle.Bottle()

### ARG PARSING ########################################################################################

parser = argparse.ArgumentParser()
parser.add_argument('-a', help="Host address", action="store", dest="a", required=True)
parser.add_argument('-p', help="Port number", action="store", dest="p", required=True)
parser.add_argument('-d', help="Dev mode", action="store_true", required=False)
parser.add_argument('-r', help="Live reloading", action="store_true", required=False)

args = parser.parse_args()

# GLOBAL ENVIRONMENT VARIABLES
ENV = argparse.Namespace()
ENV.HOST = args.a
ENV.PORT = args.p
ENV.DEV = True if args.d else False
ENV.RELOAD = True if args.r else False
ENV.ROOT = os.path.dirname(os.path.realpath(__file__))

### APACHE #############################################################################################

os.chdir(ENV.ROOT)
sys.path.insert(1, ENV.ROOT)

### FOR CSS READING IN TEMPLATES #######################################################################

bottle.SimpleTemplate.defaults["url"] = bottle.url

### STATIC ROUTING ########################################################################################

# CSS
@app.get('/css/<filename:re:.*\.css(.map)?>')
def stylesheets(filename):
    return bottle.static_file(filename, root='static/css')

# JAVASCRIPT
@app.get('/js/<filename:re:.*\.js(.map)?>')
def javascripts(filename):
    return bottle.static_file(filename, root='./static/js')

# IMAGES
@app.get('/img/<filename:re:.*\.(jpg|png|gif|ico)>')
def images(filename):
    return bottle.static_file(filename, root='static/img')

# FONTS
@app.get('/fonts/<filename:re:.*\.(eot|ttf|woff|woff2|svg)>')
def fonts(filename):
    return bottle.static_file(filename, root='static/fonts')

@app.error(404)
def error404(error):
    return 'Nothing here, sorry'

########################################################################################################
########################################################################################################
########################################################################################################
#
# ^^^ FUNCTIONS, SETTINGS ^^^
#
# vvv ROUTES vvv
#
########################################################################################################
##################################### SLURM ROUTES START ###############################################
########################################################################################################

@app.get('/')
@app.get('/slurm')
def slurm_nodes():
    # get and delete anchor cookie
    anchor = bottle.request.cookies.get("anchor", -1)
    bottle.response.delete_cookie("anchor")

    # get and delete requested node cookie
    requested = bottle.request.cookies.get("requested", "")
    bottle.response.delete_cookie("requested")

    # dict of (state --> obj) pairs
    states = slurm.Slurm.getNonEmptyStates()

    # if a specific node was requested, get the scontrol info for that node
    node = slurm.Slurm.Node(requested)

    #################################################

    return bottle.template('slurm', anchor=anchor, states=states, node=node)

########################################################################################################
########################################################################################################

@app.post('/node')
def scontrol_show_node():
    anchor = bottle.request.forms.get('anchor') or -1
    bottle.response.set_cookie("anchor", anchor)

    requested = bottle.request.forms.get('node') or -1
    bottle.response.set_cookie("requested", requested)

    bottle.redirect('/slurm#' + anchor)

########################################################################################################
########################################################################################################

@app.post('/search')
def search_for_node():
    # get requested node and parse out the number
    requested = slurm.Slurm.normalizeNodeName(bottle.request.forms.get('search'))

    # set the cookie to be used in /slurm
    bottle.response.set_cookie("requested", requested)

    bottle.redirect("/slurm")

########################################################################################################
##################################### SLURM ROUTES END #################################################
########################################################################################################

print("* * * * * * * * * * * * * * * * * * * * * * * * * * * ")
print("APP RUNNING FROM : {project_dir}".format(project_dir=ENV.ROOT))
print("HOST ADDRESS     : {hostAddr}".format(hostAddr=ENV.HOST))
print("HOST PORT        : {hostPort}".format(hostPort=ENV.PORT))
print("DEBUG            : {devMode}".format(devMode=ENV.DEV))
print("LIVE RELOAD      : {liveReload}".format(liveReload=ENV.RELOAD))
print("* * * * * * * * * * * * * * * * * * * * * * * * * * * ")

app.run(host=ENV.HOST, port=ENV.PORT, debug=ENV.DEV, reloader=ENV.RELOAD)

########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
<commit_msg>fixed routing bug<commit_after>#!/usr/bin/env python

########################################################################################################
########################################################################################################
########################################################################################################

### IMPORTS ############################################################################################

from __future__ import print_function
import os
import sys
import argparse

import modu.bottle as bottle
import modu.slurm as slurm

app = bottle.Bottle()

### ARG PARSING ########################################################################################

parser = argparse.ArgumentParser()
parser.add_argument('-a', help="Host address", action="store", dest="a", required=True)
parser.add_argument('-p', help="Port number", action="store", dest="p", required=True)
parser.add_argument('-d', help="Dev mode", action="store_true", required=False)
parser.add_argument('-r', help="Live reloading", action="store_true", required=False)

args = parser.parse_args()

# GLOBAL ENVIRONMENT VARIABLES
ENV = argparse.Namespace()
ENV.HOST = args.a
ENV.PORT = args.p
ENV.DEV = True if args.d else False
ENV.RELOAD = True if args.r else False
ENV.ROOT = os.path.dirname(os.path.realpath(__file__))

### APACHE #############################################################################################

os.chdir(ENV.ROOT)
sys.path.insert(1, ENV.ROOT)

### FOR CSS READING IN TEMPLATES #######################################################################

bottle.SimpleTemplate.defaults["url"] = bottle.url

### STATIC ROUTING ########################################################################################

# CSS
@app.get('/css/<filename:re:.*\.css(.map)?>')
def stylesheets(filename):
    return bottle.static_file(filename, root='static/css')

# JAVASCRIPT
@app.get('/js/<filename:re:.*\.js(.map)?>')
def javascripts(filename):
    return bottle.static_file(filename, root='static/js')

# IMAGES
@app.get('/img/<filename:re:.*\.(jpg|png|gif|ico)>')
def images(filename):
    return bottle.static_file(filename, root='static/img')

# FONTS
@app.get('/fonts/<filename:re:.*\.(eot|ttf|woff|woff2|svg)>')
def fonts(filename):
    return bottle.static_file(filename, root='static/fonts')

@app.error(404)
def error404(error):
    return 'Nothing here, sorry'

########################################################################################################
########################################################################################################
########################################################################################################
#
# ^^^ FUNCTIONS, SETTINGS ^^^
#
# vvv ROUTES vvv
#
########################################################################################################
##################################### SLURM ROUTES START ###############################################
########################################################################################################

@app.get('/')
@app.get('/slurm')
def slurm_nodes():
    # get and delete anchor cookie
    anchor = bottle.request.cookies.get("anchor", -1)
    bottle.response.delete_cookie("anchor")

    # get and delete requested node cookie
    requested = bottle.request.cookies.get("requested", "")
    bottle.response.delete_cookie("requested")

    # dict of (state --> obj) pairs
    states = slurm.Slurm.getNonEmptyStates()

    # if a specific node was requested, get the scontrol info for that node
    node = slurm.Slurm.Node(requested)

    #################################################

    return bottle.template('slurm', anchor=anchor, states=states, node=node)

########################################################################################################
########################################################################################################

@app.post('/node')
def scontrol_show_node():
    anchor = bottle.request.forms.get('anchor') or -1
    bottle.response.set_cookie("anchor", anchor)

    requested = bottle.request.forms.get('node') or -1
    bottle.response.set_cookie("requested", requested)

    bottle.redirect('/slurm#' + anchor)

########################################################################################################
########################################################################################################

@app.post('/search')
def search_for_node():
    # get requested node and parse out the number
    requested = slurm.Slurm.normalizeNodeName(bottle.request.forms.get('search'))

    # set the cookie to be used in /slurm
    bottle.response.set_cookie("requested", requested)

    bottle.redirect("/slurm")

########################################################################################################
##################################### SLURM ROUTES END #################################################
########################################################################################################

print("* * * * * * * * * * * * * * * * * * * * * * * * * * * ")
print("APP RUNNING FROM : {project_dir}".format(project_dir=ENV.ROOT))
print("HOST ADDRESS     : {hostAddr}".format(hostAddr=ENV.HOST))
print("HOST PORT        : {hostPort}".format(hostPort=ENV.PORT))
print("DEBUG            : {devMode}".format(devMode=ENV.DEV))
print("LIVE RELOAD      : {liveReload}".format(liveReload=ENV.RELOAD))
print("* * * * * * * * * * * * * * * * * * * * * * * * * * * ")

app.run(host=ENV.HOST, port=ENV.PORT, debug=ENV.DEV, reloader=ENV.RELOAD)

########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
########################################################################################################
<|endoftext|>


<commit_before>#!/usr/bin/env python
''' main code here '''

import sensorState
import rover
import pixyTracker
import time
import signal
import rgb
import bump
import wifiswitch
import screenlogger

QuitFlag = False

# Ctrl-c handler
def ctrlC(signal, frame):
    global Quitflag
    PixySensor.quit()
    Rover.Stop()
    QuitFlag = True

signal.signal(signal.SIGINT,ctrlC)

# Initialize rover
Rover = rover.Rover()
RotateSpeed = 3200

#Initialize LED
Led = rgb.RGBController()

#Initialize Bump Sensor
Bump = bump.BumpSensor("BumpSensor")
Bump.start(None)

#Initialize Wifi Kill Switch
KillSwitch = wifiswitch.WifiSwitch("KillSwitch", host_ip = "172.20.10.1")
#KillSwitch.start()

# Initialize Pixy
PixySensor = pixyTracker.pixyController('pixy')
PixySensor.start(None)
PixyTimeCounter = 0

# Initialize WayPoint Sensr (GPS, COMPAS)
WayPointSensor = sensorState.WaypointSensor("WaypointSensor")
WayPointSensor.start()
HeadingDiff = 7

# Initia; State
MachineState = "WayPoint"

# Initialize screen logger message
ScreenLog = screenlogger.ScreenLogger(WayPointSensor.logToScreen, verbose = True)

ScreenLog.Log("Robot Maria is Ready!")

while not QuitFlag:
    time.sleep(0.1) # We will get data every 100 ms

    if True: #KillSwitch.getReading().value:
        if MachineState == "Pixy":
            #Get data from pixy and rotate till we are facing the cone.
            ConeAngle = PixySensor.getReading()
            #print "Cone Angle:"+str(ConeAngle)
            #print "Bump State:"+str(Bump.getReading().value)
            if Bump.getReading().value == 1:
                ScreenLog.Log("Bumped Cone")
                Led.allOff()
                Led.turnOn('teal')
                Rover.Stop()
                Rover.Stop()
                Rover.Reverse(RotateSpeed)
                time.sleep(3)
                Rover.Stop()
                Rover.RotateRight(RotateSpeed)
                time.sleep(3)
                Rover.Stop()
                Rover.Forward(RotateSpeed)
                time.sleep(3)
                Rover.Stop()
                Rover.RotateLeft(RotateSpeed)
                time.sleep(3)
                Rover.Stop()
                MachineState = "WayPoint"
                WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
            else:
                Led.allOff()
                Led.turnOn('purple')
                if ConeAngle.value == None:
                    Rover.RotateRight(RotateSpeed)
                    PixyTimeCounter += 1
                    if PixyTimeCounter > 50:
                        MachineState = "WayPoint"
                        WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
                elif ConeAngle.value > 2:
                    Rover.RotateRight(RotateSpeed)
                    print "Rotate Right"
                    ScreenLog.Log("I see Cone")
                elif ConeAngle.value < -2:
                    print "Rotate Left"
                    Rover.RotateLeft(RotateSpeed)
                    ScreenLog.Log("I see Cone")
                else:
                    Rover.Forward(RotateSpeed)
                    print "Forward, Pixy Residual Angle:"+str(ConeAngle)

        elif MachineState == "WayPoint":
           WayPointData = WayPointSensor.getReading().value
           if Bump.getReading().value == 1:
              ScreenLog.Log("Bumped into something")
              Led.allOff()
              Led.turnOn('teal')
			  # Time to avoid the obstacle by reversing,
			  # turning right moving forward and turning back left again
              Rover.Stop()
              Rover.Reverse(RotateSpeed)
              time.sleep(3)
              Rover.Stop()
              Rover.RotateRight(RotateSpeed)
              time.sleep(3)
              Rover.Stop()
              Rover.Forward(RotateSpeed)
              time.sleep(3)
              Rover.Stop()
              Rover.RotateLeft(RotateSpeed)
              time.sleep(3)
              Rover.Stop()

           elif WayPointData != None:
               if WayPointData["nextWaypoint"] == 0:
                   ScreenLog.Log("Reached last way point.")
                   Led.allOff()
                   Led.turnOn('yellow')
                   Rover.Stop()
               else:
                   if (WayPointData["currentHorizontalAccuracy"]+2 < 2) and (WayPointData["distanceToNextWaypoint"] < (WayPointData["currentHorizontalAccuracy"]+2)):
                       #print "We are close to cone, switch to Pixy. Distance: "+str(WayPointData["distanceToNextWaypoint"])
                       ScreenLog.Log("Reached Waypoint: "+str(WayPointData["nextWaypoint"]))
                       ScreenLog.Log("Close to Cone, switching to Pixy Mode.")
                       Rover.Stop()
                       Led.allOff()
                       Led.turnOn('green')
                       time.sleep(0.5)
                       if WayPointData["nextWaypointWeight"] == 1:
                           MachineState = "Pixy"
                           PixyTimeCounter = 0
                       else:
                           WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
                   elif WayPointData["distanceToNextWaypoint"] <= 2:
                       #print "We are close to cone, switch to Pixy. Distance: "+str(WayPointData["distanceToNextWaypoint"])
                       ScreenLog.Log("Reached Waypoint: "+str(WayPointData["nextWaypoint"]))
                       ScreenLog.Log("Close to Cone, switching to Pixy Mode.")
                       Rover.Stop()
                       Led.allOff()
                       Led.turnOn('green')
                       time.sleep(0.5)
                       if WayPointData["nextWaypointWeight"] == 1:
                           MachineState = "Pixy"
                           PixyTimeCounter = 0
                       else:
                           WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
                   else:
                       Led.allOff()
                       Led.turnOn('blue')
                       if WayPointData["currentHorizontalAccuracy"] < 5000:#20:
                           AngleDiff = WayPointData["headingToNextWaypoint"] - WayPointData["direction"]
                           if (AngleDiff < -180):
                               AngleDiff += 360
                           elif (AngleDiff > 180):
                               AngleDiff -= 360
                           #print "AngleDiff: "+str(AngleDiff)
                           # Allow a small degree of diviation before taking action
                           if abs(AngleDiff) > HeadingDiff:
                               if AngleDiff > 0:
                                   Rover.RotateRight(RotateSpeed)
                                   print "ROTATE RIGHT"
                               else:
                                   Rover.RotateLeft(RotateSpeed)
                                   print "ROTATE LEFT"
                           else:
                                   Rover.Forward(RotateSpeed)
                                   print "FORWARD"
           else:
               #print "Waypoint data is None"
               ScreenLog.Log("WayPoint data is None")
               Led.allOff()
               Led.turnOn('red')
    else:
        ScreenLog.Log("Kill Switch enabled.")
        Led.allOff()
        Led.turnOn('red')
<commit_msg>updating the obstacle avoidance to allow the motors to ramp up when moving<commit_after>#!/usr/bin/env python
''' main code here '''

import sensorState
import rover
import pixyTracker
import time
import signal
import rgb
import bump
import wifiswitch
import screenlogger

QuitFlag = False

# Ctrl-c handler
def ctrlC(signal, frame):
    global Quitflag
    PixySensor.quit()
    Rover.Stop()
    QuitFlag = True

signal.signal(signal.SIGINT,ctrlC)

# Initialize rover
Rover = rover.Rover()
RotateSpeed = 3200

#Initialize LED
Led = rgb.RGBController()

#Initialize Bump Sensor
Bump = bump.BumpSensor("BumpSensor")
Bump.start(None)

#Initialize Wifi Kill Switch
KillSwitch = wifiswitch.WifiSwitch("KillSwitch", host_ip = "172.20.10.1")
#KillSwitch.start()

# Initialize Pixy
PixySensor = pixyTracker.pixyController('pixy')
PixySensor.start(None)
PixyTimeCounter = 0

# Initialize WayPoint Sensr (GPS, COMPAS)
WayPointSensor = sensorState.WaypointSensor("WaypointSensor")
WayPointSensor.start()
HeadingDiff = 7

# constants for ramping motors
FromStop = 13
WhileMoving = 5

# Initia; State
MachineState = "WayPoint"

# Initialize screen logger message
ScreenLog = screenlogger.ScreenLogger(WayPointSensor.logToScreen, verbose = True)

ScreenLog.Log("Robot Maria is Ready!")

while not QuitFlag:
    time.sleep(0.1) # We will get data every 100 ms

    if True: #KillSwitch.getReading().value:
        if MachineState == "Pixy":
            #Get data from pixy and rotate till we are facing the cone.
            ConeAngle = PixySensor.getReading()
            #print "Cone Angle:"+str(ConeAngle)
            #print "Bump State:"+str(Bump.getReading().value)
            if Bump.getReading().value == 1:
                ScreenLog.Log("Bumped Cone")
                Led.allOff()
                Led.turnOn('teal')
                Rover.Stop()
                for i in range(0,FromStop):
                    Rover.Reverse(RotateSpeed)
                    time.sleep(0.1)
                Rover.Stop()
                for i in range(0,FromStop):
                    Rover.RotateRight(RotateSpeed)
                    time.sleep(0.1)
                for i in range(0,WhileMoving):
                    Rover.Forward(RotateSpeed)
                    time.sleep(0.1)
                for i in range(0,WhileMoving):
                    Rover.RotateLeft(RotateSpeed)
                    time.sleep(0.1)
                Rover.Stop()
                MachineState = "WayPoint"
                WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
            else:
                Led.allOff()
                Led.turnOn('purple')
                if ConeAngle.value == None:
                    Rover.RotateRight(RotateSpeed)
                    PixyTimeCounter += 1
                    if PixyTimeCounter > 50:
                        MachineState = "WayPoint"
                        WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
                elif ConeAngle.value > 2:
                    Rover.RotateRight(RotateSpeed)
                    print "Rotate Right"
                    ScreenLog.Log("I see Cone")
                elif ConeAngle.value < -2:
                    print "Rotate Left"
                    Rover.RotateLeft(RotateSpeed)
                    ScreenLog.Log("I see Cone")
                else:
                    Rover.Forward(RotateSpeed)
                    print "Forward, Pixy Residual Angle:"+str(ConeAngle)

        elif MachineState == "WayPoint":
           WayPointData = WayPointSensor.getReading().value
           if Bump.getReading().value == 1:
              ScreenLog.Log("Bumped into something")
              Led.allOff()
              Led.turnOn('teal')
              # Time to avoid the obstacle by reversing,
              # turning right moving forward and turning back left again
              Rover.Stop()
              for i in range(0,FromStop):
                  Rover.Reverse(RotateSpeed)
                  time.sleep(0.1)
              Rover.Stop()
              for i in range(0,FromStop):
                  Rover.RotateRight(RotateSpeed)
                  time.sleep(0.1)
              for i in range(0,WhileMoving):
                  Rover.Forward(RotateSpeed)
                  time.sleep(0.1)
              for i in range(0,WhileMoving):
                  Rover.RotateLeft(RotateSpeed)
                  time.sleep(0.1)
              Rover.Stop()

           elif WayPointData != None:
               if WayPointData["nextWaypoint"] == 0:
                   ScreenLog.Log("Reached last way point.")
                   Led.allOff()
                   Led.turnOn('yellow')
                   Rover.Stop()
               else:
                   if (WayPointData["currentHorizontalAccuracy"]+2 < 2) and (WayPointData["distanceToNextWaypoint"] < (WayPointData["currentHorizontalAccuracy"]+2)):
                       #print "We are close to cone, switch to Pixy. Distance: "+str(WayPointData["distanceToNextWaypoint"])
                       ScreenLog.Log("Reached Waypoint: "+str(WayPointData["nextWaypoint"]))
                       ScreenLog.Log("Close to Cone, switching to Pixy Mode.")
                       Rover.Stop()
                       Led.allOff()
                       Led.turnOn('green')
                       time.sleep(0.5)
                       if WayPointData["nextWaypointWeight"] == 1:
                           MachineState = "Pixy"
                           PixyTimeCounter = 0
                       else:
                           WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
                   elif WayPointData["distanceToNextWaypoint"] <= 2:
                       #print "We are close to cone, switch to Pixy. Distance: "+str(WayPointData["distanceToNextWaypoint"])
                       ScreenLog.Log("Reached Waypoint: "+str(WayPointData["nextWaypoint"]))
                       ScreenLog.Log("Close to Cone, switching to Pixy Mode.")
                       Rover.Stop()
                       Led.allOff()
                       Led.turnOn('green')
                       time.sleep(0.5)
                       if WayPointData["nextWaypointWeight"] == 1:
                           MachineState = "Pixy"
                           PixyTimeCounter = 0
                       else:
                           WayPointSensor.setNextWaypoint(WayPointData["nextWaypoint"]+1)
                   else:
                       Led.allOff()
                       Led.turnOn('blue')
                       if WayPointData["currentHorizontalAccuracy"] < 5000:#20:
                           AngleDiff = WayPointData["headingToNextWaypoint"] - WayPointData["direction"]
                           if (AngleDiff < -180):
                               AngleDiff += 360
                           elif (AngleDiff > 180):
                               AngleDiff -= 360
                           #print "AngleDiff: "+str(AngleDiff)
                           # Allow a small degree of diviation before taking action
                           if abs(AngleDiff) > HeadingDiff:
                               if AngleDiff > 0:
                                   Rover.RotateRight(RotateSpeed)
                                   print "ROTATE RIGHT"
                               else:
                                   Rover.RotateLeft(RotateSpeed)
                                   print "ROTATE LEFT"
                           else:
                                   Rover.Forward(RotateSpeed)
                                   print "FORWARD"
           else:
               #print "Waypoint data is None"
               ScreenLog.Log("WayPoint data is None")
               Led.allOff()
               Led.turnOn('red')
    else:
        ScreenLog.Log("Kill Switch enabled.")
        Led.allOff()
        Led.turnOn('red')
<|endoftext|>


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

# Author Tully Foote/tfoote@willowgarage.com, Ken Conley/kwc@willowgarage.com

"""
rosdep library and command-line tool
"""

from __future__ import print_function

__version__ = '0.10.21'

import sys

try:
    from .installers import InstallerContext, Installer, \
            PackageManagerInstaller
    from .core import RosdepInternalError, InstallFailed, UnsupportedOs, \
            InvalidData, DownloadFailure
    from .model import RosdepDatabase, RosdepDatabaseEntry
    from .lookup import RosdepDefinition, RosdepView, RosdepLookup, \
            ResolutionError
    from .loader import RosdepLoader
except ImportError:
    # soft-fail if rospkg not available.  Need this backoff behavior
    # in order to support setting __version__ in the module.
    print("failed to load symbols, rosdep will not function properly",
            file=sys.stderr)

# don't let import error take down code as when attempting to compute version number
try:
    from .rospkg_loader import RosPkgLoader
except ImportError:
    print("Cannot import rospkg, rosdep will not function properly",
            file=sys.stderr)

def create_default_installer_context(verbose=False):
    from .platforms import arch
    from .platforms import cygwin
    from .platforms import debian
    from .platforms import gentoo
    from .platforms import opensuse
    from .platforms import osx
    from .platforms import pip
    from .platforms import gem
    from .platforms import redhat
    from .platforms import source

    platform_mods = [arch, cygwin, debian, gentoo, opensuse, osx, redhat]
    installer_mods = [source, pip, gem] + platform_mods

    context = InstallerContext()
    context.set_verbose(verbose)

    # setup installers
    for m in installer_mods:
        if verbose:
            print("registering installers for %s"%(m.__name__))
        m.register_installers(context)

    # setup platforms
    for m in platform_mods:
        if verbose:
            print("registering platforms for %s"%(m.__name__))
        m.register_platforms(context)

    return context

#TODO: this was partially abstracted from main() for another library,
# but it turned out to be unnecessary. Not sure it's worth maintaining
# separately, especially in the top-level module.
def get_default_installer(installer_context=None, verbose=False):
    """
    Based on the active OS and installer context configuration, get
    the installer to use and the necessary configuration state
    (installer keys, OS name/version).

    :returns: installer, installer_keys, default_key, os_name, os_version.
    """
    if installer_context is None:
        installer_context = create_default_installer_context(verbose=verbose)

    os_name, os_version = installer_context.get_os_name_and_version()
    try:
        installer_keys = installer_context.get_os_installer_keys(os_name)
        default_key = installer_context.get_default_os_installer_key(os_name)
    except KeyError:
        raise UnsupportedOs(os_name, installer_context.get_os_keys())
    installer = installer_context.get_installer(default_key)
    return installer, installer_keys, default_key, os_name, os_version

__all__ = ['InstallerContext', 'Installer', 'PackageManagerInstaller',
        'RosdepInternalError', 'InstallFailed', 'UnsupportedOs', 'InvalidData',
        'DownloadFailure',
        'RosdepDatabase', 'RosdepDatabaseEntry',
        'RosdepDefinition', 'RosdepView', 'RosdepLookup', 'ResolutionError',
        'RosdepLoader', 'RosPkgLoader',
        'get_default_installer',
        'create_default_installer_context',
        ]
<commit_msg>0.10.22<commit_after># Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Willow Garage, Inc. nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# Author Tully Foote/tfoote@willowgarage.com, Ken Conley/kwc@willowgarage.com

"""
rosdep library and command-line tool
"""

from __future__ import print_function

__version__ = '0.10.22'

import sys

try:
    from .installers import InstallerContext, Installer, \
            PackageManagerInstaller
    from .core import RosdepInternalError, InstallFailed, UnsupportedOs, \
            InvalidData, DownloadFailure
    from .model import RosdepDatabase, RosdepDatabaseEntry
    from .lookup import RosdepDefinition, RosdepView, RosdepLookup, \
            ResolutionError
    from .loader import RosdepLoader
except ImportError:
    # soft-fail if rospkg not available.  Need this backoff behavior
    # in order to support setting __version__ in the module.
    print("failed to load symbols, rosdep will not function properly",
            file=sys.stderr)

# don't let import error take down code as when attempting to compute version number
try:
    from .rospkg_loader import RosPkgLoader
except ImportError:
    print("Cannot import rospkg, rosdep will not function properly",
            file=sys.stderr)

def create_default_installer_context(verbose=False):
    from .platforms import arch
    from .platforms import cygwin
    from .platforms import debian
    from .platforms import gentoo
    from .platforms import opensuse
    from .platforms import osx
    from .platforms import pip
    from .platforms import gem
    from .platforms import redhat
    from .platforms import source

    platform_mods = [arch, cygwin, debian, gentoo, opensuse, osx, redhat]
    installer_mods = [source, pip, gem] + platform_mods

    context = InstallerContext()
    context.set_verbose(verbose)

    # setup installers
    for m in installer_mods:
        if verbose:
            print("registering installers for %s"%(m.__name__))
        m.register_installers(context)

    # setup platforms
    for m in platform_mods:
        if verbose:
            print("registering platforms for %s"%(m.__name__))
        m.register_platforms(context)

    return context

#TODO: this was partially abstracted from main() for another library,
# but it turned out to be unnecessary. Not sure it's worth maintaining
# separately, especially in the top-level module.
def get_default_installer(installer_context=None, verbose=False):
    """
    Based on the active OS and installer context configuration, get
    the installer to use and the necessary configuration state
    (installer keys, OS name/version).

    :returns: installer, installer_keys, default_key, os_name, os_version.
    """
    if installer_context is None:
        installer_context = create_default_installer_context(verbose=verbose)

    os_name, os_version = installer_context.get_os_name_and_version()
    try:
        installer_keys = installer_context.get_os_installer_keys(os_name)
        default_key = installer_context.get_default_os_installer_key(os_name)
    except KeyError:
        raise UnsupportedOs(os_name, installer_context.get_os_keys())
    installer = installer_context.get_installer(default_key)
    return installer, installer_keys, default_key, os_name, os_version

__all__ = ['InstallerContext', 'Installer', 'PackageManagerInstaller',
        'RosdepInternalError', 'InstallFailed', 'UnsupportedOs', 'InvalidData',
        'DownloadFailure',
        'RosdepDatabase', 'RosdepDatabaseEntry',
        'RosdepDefinition', 'RosdepView', 'RosdepLookup', 'ResolutionError',
        'RosdepLoader', 'RosPkgLoader',
        'get_default_installer',
        'create_default_installer_context',
        ]
<|endoftext|>


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

"""
CI build script
(C) 2017 Jack Lloyd
Botan is released under the Simplified BSD License (see license.txt)
"""

import os
import platform
import subprocess
import sys
import time
import optparse # pylint: disable=deprecated-module

def get_concurrency():
    """
    Get default concurrency level of build
    """
    def_concurrency = 2

    try:
        import multiprocessing
        return max(def_concurrency, multiprocessing.cpu_count())
    except ImportError:
        return def_concurrency

def getenv_or_die(var):
    """
    Like it says...
    """
    val = os.getenv(var)
    if val is None:
        raise Exception('Required variable %s not set in environment' % (var))
    return val

def determine_flags(target, target_os, target_cc, cc_bin, use_ccache, root_dir):
    # pylint: disable=too-many-branches,too-many-statements,too-many-arguments

    """
    Return the configure.py flags as well as make/test running prefixes
    """
    is_cross_target = target.startswith('cross-')

    if target_os not in ['linux', 'osx']:
        print('Error unknown OS %s' % (target_os))
        return 1

    if is_cross_target:
        if target_os == 'osx':
            target_os = 'ios'
        elif target == 'cross-win32':
            target_os = 'mingw'

    make_prefix = []
    test_prefix = []
    test_cmd = [os.path.join(root_dir, 'botan-test')]

    fast_tests = ['block', 'aead', 'hash', 'stream', 'mac', 'modes',
                  'hmac_drbg', 'hmac_drbg_unit',
                  'tls', 'ffi',
                  'rsa_sign', 'rsa_verify', 'dh_kat', 'ecdsa_sign', 'curve25519_scalar',
                  'simd_32', 'os_utils', 'util', 'util_dates']

    flags = ['--prefix=/tmp/botan-install', '--cc=%s' % (target_cc), '--os=%s' % (target_os)]

    if target in ['static', 'mini-static', 'fuzzers'] or target_os in ['ios', 'mingw']:
        flags += ['--disable-shared']

    if target in ['mini-static', 'mini-shared']:
        flags += ['--minimized-build', '--enable-modules=dev_random,system_rng,sha2_32,sha2_64,aes']

    if target == 'shared':
        # Arbitrarily test amalgamation on shared obj builds
        flags += ['--amalgamation']

    if target in ['bsi', 'nist']:
        flags += ['--module-policy=%s' % (target)]

    if target == 'docs':
        flags += ['--with-doxygen', '--with-sphinx']
        test_cmd = None

    if target in ['coverage', 'sonar']:
        flags += ['--with-coverage-info']
    if target == 'valgrind':
        flags += ['--with-valgrind']
        test_prefix = ['valgrind', '--error-exitcode=9', '-v']
        test_cmd += fast_tests

    if target in ['fuzzers', 'coverage', 'valgrind']:
        flags += ['--with-debug-info']
    if target in ['fuzzers', 'coverage']:
        flags += ['--build-fuzzers=test']
    if target in ['fuzzers', 'sanitizer']:
        flags += ['--with-sanitizers']
    if target in ['valgrind', 'sanitizer', 'fuzzers']:
        flags += ['--disable-modules=locking_allocator']

    if target == 'parallel':
        if 'cc' == 'gcc':
            flags += ['--with-cilkplus']
        else:
            flags += ['--with-openmp']

    if target == 'sonar':
        make_prefix = [os.path.join(root_dir, 'build-wrapper-linux-x86/build-wrapper-linux-x86-64'),
                       '--out-dir', 'bw-outputs']
        test_cmd = ['sonar-scanner',
                    '-Dproject.settings=%s' % (os.path.join(root_dir, 'src', 'build-data', 'sonar-project.properties')),
                    '-Dsonar.login=%s' % (getenv_or_die('SONAR_TOKEN'))]

    if is_cross_target:
        if target_os == 'ios':
            make_prefix = ['xcrun', '--sdk', 'iphoneos']
            test_cmd = None
            if target == 'cross-arm32':
                flags += ['--cpu=armv7', '--cc-abi-flags=-arch armv7 -arch armv7s -stdlib=libc++']
            elif target == 'cross-arm64':
                flags += ['--cpu=armv8-a', '--cc-abi-flags=-arch arm64 -stdlib=libc++']
            else:
                raise Exception("Unknown cross target '%s' for iOS" % (target))
        elif target == 'cross-win32':
            flags += ['--cpu=x86_32', '--cc-abi-flags=-static']
            cc_bin = 'i686-w64-mingw32-g++'
            test_cmd = [os.path.join(root_dir, 'botan-test.exe')]
            # No runtime prefix required for Wine
        else:
            # Build everything but restrict what is run
            test_cmd += fast_tests

            if target == 'cross-arm32':
                flags += ['--cpu=armv7']
                cc_bin = 'arm-linux-gnueabihf-g++'
                test_prefix = ['qemu-arm', '-L', '/usr/arm-linux-gnueabihf/']
            elif target == 'cross-arm64':
                flags += ['--cpu=armv8-a']
                cc_bin = 'aarch64-linux-gnu-g++'
                test_prefix = ['qemu-aarch64', '-L', '/usr/aarch64-linux-gnu/']
            elif target == 'cross-ppc32':
                flags += ['--cpu=ppc32']
                cc_bin = 'powerpc-linux-gnu-g++'
                test_prefix = ['qemu-ppc', '-L', '/usr/powerpc-linux-gnu/']
            elif target == 'cross-ppc64':
                flags += ['--cpu=ppc64', '--with-endian=little']
                cc_bin = 'powerpc64le-linux-gnu-g++'
                test_prefix = ['qemu-ppc64le', '-L', '/usr/powerpc64le-linux-gnu/']
            else:
                raise Exception("Unknown cross target '%s' for Linux" % (target))
    else:
        # Flags specific to native targets
        flags += ['--with-bzip2', '--with-lzma', '--with-sqlite', '--with-zlib']

        if target_os == 'osx':
            # Test Boost on OS X
            flags += ['--with-boost']
        elif target not in ['sanitizer', 'valgrind', 'mini-shared', 'mini-static']:
            # Avoid OpenSSL when using dynamic checkers, or on OS X where it sporadically
            # is not installed on the CI image
            flags += ['--with-openssl']

        if target == 'coverage':
            flags += ['--with-tpm']
            test_cmd += ['--run-long-tests', '--run-online-tests']

            softhsm_lib = '/tmp/softhsm/lib/softhsm/libsofthsm2.so'
            if os.access(softhsm_lib, os.R_OK):
                test_cmd += ['--pkcs11-lib=%s' % (softhsm_lib)]

    flags += ['--cc-bin=%s%s' % ('ccache ' if use_ccache else '', cc_bin)]

    if test_cmd is None:
        run_test_command = None
    else:
        run_test_command = test_prefix + test_cmd

    return flags, run_test_command, make_prefix

def run_cmd(cmd, root_dir):
    """
    Execute a command, die if it failed
    """
    print("Running '%s':\n" % (' '.join(cmd)))
    sys.stdout.flush()

    start = time.time()

    sub_env = os.environ.copy()
    sub_env['LD_LIBRARY_PATH'] = root_dir
    proc = subprocess.Popen(cmd, close_fds=True, env=sub_env)
    proc.communicate()

    time_taken = int(time.time() - start)

    if time_taken > 2:
        print("Ran for %d seconds" % (time_taken))

    if proc.returncode != 0:
        print("Command failed with error code %d" % (proc.returncode))
        sys.exit(proc.returncode)

def parse_args(args):
    """
    Parse arguments
    """
    parser = optparse.OptionParser()

    parser.add_option('--os', default=platform.system().lower(),
                      help='Set the target os (default %default)')
    parser.add_option('--cc', default='gcc',
                      help='Set the target compiler type (default %default)')
    parser.add_option('--cc-bin', default=None,
                      help='Set path to compiler')
    parser.add_option('--root-dir', metavar='D', default='.',
                      help='Set directory to execute from (default %default)')

    parser.add_option('--branch', metavar='B', default=None,
                      help='Specify branch being built')

    parser.add_option('--add-travis-folds', action='store_true', default=False,
                      help='Add fold markers for Travis UI')

    parser.add_option('--dry-run', action='store_true', default=False,
                      help='Just show commands to be executed')
    parser.add_option('--build-jobs', metavar='J', default=get_concurrency(),
                      help='Set number of jobs to run in parallel (default %default)')

    parser.add_option('--with-ccache', dest='use_ccache', action='store_true', default=None,
                      help='Enable using ccache')
    parser.add_option('--without-ccache', dest='use_ccache', action='store_false',
                      help='Disable using ccache')

    parser.add_option('--with-python3', dest='use_python3', action='store_true', default=None,
                      help='Enable using python3')
    parser.add_option('--without-python3', dest='use_python3', action='store_false',
                      help='Disable using python3')

    return parser.parse_args(args)

def have_prog(prog):
    """
    Check if some named program exists in the path
    """
    for path in os.environ['PATH'].split(os.pathsep):
        exe_file = os.path.join(path, prog)
        if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
            return True

def main(args=None):
    # pylint: disable=too-many-branches,too-many-statements
    """
    Parse options, do the things
    """
    (options, args) = parse_args(args or sys.argv)

    if len(args) != 2:
        print('Usage: %s [options] target' % (args[0]))
        return 1

    if options.use_ccache is None:
        options.use_ccache = have_prog('ccache')

    use_python2 = have_prog('python2')

    if options.use_python3 is None:
        use_python3 = have_prog('python3')
    else:
        use_python3 = options.use_python3

    if options.cc_bin is None:
        if options.cc == 'gcc':
            options.cc_bin = 'g++'
        elif options.cc == 'clang':
            options.cc_bin = 'clang++'
        else:
            print('Error unknown compiler %s' % (options.cc))
            return 1

    target = args[1]

    root_dir = options.root_dir

    if os.access(root_dir, os.R_OK) != True:
        raise Exception('Bad root dir setting, dir %s not readable', root_dir)

    cmds = []

    if target == 'lint':

        if not use_python2 and not use_python3:
            raise Exception('No python interpreters found cannot lint')

        py_scripts = [
            'configure.py',
            'src/python/botan2.py',
            'src/scripts/ci_build.py',
            'src/scripts/install.py',
            'src/scripts/python_unittests.py',
            'src/scripts/python_unittests_unix.py']

        for target in py_scripts:
            target_path = os.path.join(root_dir, target)

            if use_python2:
                # Some disabled rules specific to Python2
                # superfluous-parens: needed for Python3 compatible print statements
                # too-many-locals: variable counting differs from pylint3
                py2_flags = '--disable=superfluous-parens,too-many-locals'
                cmds.append(['python2', '-m', 'pylint', py2_flags, target_path])

            if use_python3:
                cmds.append(['python3', '-m', 'pylint', target_path])

    else:
        config_flags, run_test_command, make_prefix = determine_flags(
            target, options.os, options.cc, options.cc_bin, options.use_ccache, root_dir)

        cmds.append([os.path.join(root_dir, 'configure.py')] + config_flags)

        if target == 'docs':
            cmds.append(['make', '-C', root_dir, 'docs'])
        else:
            if options.use_ccache:
                cmds.append(['ccache', '--show-stats'])

            make_targets = ['libs', 'cli', 'tests']
            if target in ['coverage', 'fuzzers']:
                make_targets += ['fuzzers', 'fuzzer_corpus_zip']

            cmds.append(make_prefix +
                        ['make', '-j', str(options.build_jobs), '-C', root_dir] +
                        make_targets)

            if options.use_ccache:
                cmds.append(['ccache', '--show-stats'])

        if run_test_command != None:
            cmds.append(run_test_command)

        if target in ['coverage', 'fuzzers']:
            cmds.append([os.path.join(root_dir, 'src/scripts/test_fuzzers.py'),
                         os.path.join(root_dir, 'fuzzer_corpus'),
                         os.path.join(root_dir, 'build/fuzzer')])

        if target in ['static', 'shared']:
            cmds.append([os.path.join(root_dir, 'src/scripts/cli_tests.py'),
                         os.path.join(root_dir, 'botan')])

        if target in ['shared', 'coverage']:

            if use_python2:
                cmds.append(['python2', os.path.join(root_dir, 'src/python/botan2.py')])
            if use_python3:
                cmds.append(['python3', os.path.join(root_dir, 'src/python/botan2.py')])

        if target != 'docs':
            cmds.append(['make', '-C', root_dir, 'install'])

        if target in ['coverage']:

            if not have_prog('lcov'):
                print('Error: lcov not found in PATH (%s)' % (os.getenv('PATH')))
                return 1

            if not have_prog('gcov'):
                print('Error: gcov not found in PATH (%s)' % (os.getenv('PATH')))
                return 1

            cmds.append(['lcov', '--capture', '--directory', options.root_dir, '--output-file', 'coverage.info.raw'])
            cmds.append(['lcov', '--remove', 'coverage.info.raw', '/usr/*', '--output-file', 'coverage.info'])
            cmds.append(['lcov', '--list', 'coverage.info'])

            if have_prog('coverage'):
                cmds.append(['coverage', 'run', '--branch', os.path.join(root_dir, 'src/python/botan2.py')])

            if have_prog('codecov'):
                # If codecov exists assume we are on Travis and report to codecov.io
                cmds.append(['codecov'])
            else:
                # Otherwise generate a local HTML report
                cmds.append(['genhtml', 'coverage.info', '--output-directory', 'lcov-out'])

    for cmd in cmds:
        if options.dry_run:
            print('$ ' + ' '.join(cmd))
        else:
            run_cmd(cmd, root_dir)

    return 0

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>Revert f16c3df4fc4ed<commit_after>#!/usr/bin/env python

"""
CI build script
(C) 2017 Jack Lloyd
Botan is released under the Simplified BSD License (see license.txt)
"""

import os
import platform
import subprocess
import sys
import time
import optparse # pylint: disable=deprecated-module

def get_concurrency():
    """
    Get default concurrency level of build
    """
    def_concurrency = 2

    try:
        import multiprocessing
        return max(def_concurrency, multiprocessing.cpu_count())
    except ImportError:
        return def_concurrency

def getenv_or_die(var):
    """
    Like it says...
    """
    val = os.getenv(var)
    if val is None:
        raise Exception('Required variable %s not set in environment' % (var))
    return val

def determine_flags(target, target_os, target_cc, cc_bin, use_ccache, root_dir):
    # pylint: disable=too-many-branches,too-many-statements,too-many-arguments

    """
    Return the configure.py flags as well as make/test running prefixes
    """
    is_cross_target = target.startswith('cross-')

    if target_os not in ['linux', 'osx']:
        print('Error unknown OS %s' % (target_os))
        return 1

    if is_cross_target:
        if target_os == 'osx':
            target_os = 'ios'
        elif target == 'cross-win32':
            target_os = 'mingw'

    make_prefix = []
    test_prefix = []
    test_cmd = [os.path.join(root_dir, 'botan-test')]

    fast_tests = ['block', 'aead', 'hash', 'stream', 'mac', 'modes',
                  'hmac_drbg', 'hmac_drbg_unit',
                  'tls', 'ffi',
                  'rsa_sign', 'rsa_verify', 'dh_kat', 'ecdsa_sign', 'curve25519_scalar',
                  'simd_32', 'os_utils', 'util', 'util_dates']

    flags = ['--prefix=/tmp/botan-install', '--cc=%s' % (target_cc), '--os=%s' % (target_os)]

    if target in ['static', 'mini-static', 'fuzzers'] or target_os in ['ios', 'mingw']:
        flags += ['--disable-shared']

    if target in ['mini-static', 'mini-shared']:
        flags += ['--minimized-build', '--enable-modules=dev_random,system_rng,sha2_32,sha2_64,aes']

    if target == 'shared':
        # Arbitrarily test amalgamation on shared obj builds
        flags += ['--amalgamation']

    if target in ['bsi', 'nist']:
        flags += ['--module-policy=%s' % (target)]

    if target == 'docs':
        flags += ['--with-doxygen', '--with-sphinx']
        test_cmd = None

    if target == 'coverage':
        flags += ['--with-coverage-info']
    if target == 'valgrind':
        flags += ['--with-valgrind']
        test_prefix = ['valgrind', '--error-exitcode=9', '-v']
        test_cmd += fast_tests

    if target in ['fuzzers', 'coverage', 'valgrind']:
        flags += ['--with-debug-info']
    if target in ['fuzzers', 'coverage']:
        flags += ['--build-fuzzers=test']
    if target in ['fuzzers', 'sanitizer']:
        flags += ['--with-sanitizers']
    if target in ['valgrind', 'sanitizer', 'fuzzers']:
        flags += ['--disable-modules=locking_allocator']

    if target == 'parallel':
        if 'cc' == 'gcc':
            flags += ['--with-cilkplus']
        else:
            flags += ['--with-openmp']

    if target == 'sonar':
        make_prefix = [os.path.join(root_dir, 'build-wrapper-linux-x86/build-wrapper-linux-x86-64'),
                       '--out-dir', 'bw-outputs']
        test_cmd = ['sonar-scanner',
                    '-Dproject.settings=%s' % (os.path.join(root_dir, 'src', 'build-data', 'sonar-project.properties')),
                    '-Dsonar.login=%s' % (getenv_or_die('SONAR_TOKEN'))]

    if is_cross_target:
        if target_os == 'ios':
            make_prefix = ['xcrun', '--sdk', 'iphoneos']
            test_cmd = None
            if target == 'cross-arm32':
                flags += ['--cpu=armv7', '--cc-abi-flags=-arch armv7 -arch armv7s -stdlib=libc++']
            elif target == 'cross-arm64':
                flags += ['--cpu=armv8-a', '--cc-abi-flags=-arch arm64 -stdlib=libc++']
            else:
                raise Exception("Unknown cross target '%s' for iOS" % (target))
        elif target == 'cross-win32':
            flags += ['--cpu=x86_32', '--cc-abi-flags=-static']
            cc_bin = 'i686-w64-mingw32-g++'
            test_cmd = [os.path.join(root_dir, 'botan-test.exe')]
            # No runtime prefix required for Wine
        else:
            # Build everything but restrict what is run
            test_cmd += fast_tests

            if target == 'cross-arm32':
                flags += ['--cpu=armv7']
                cc_bin = 'arm-linux-gnueabihf-g++'
                test_prefix = ['qemu-arm', '-L', '/usr/arm-linux-gnueabihf/']
            elif target == 'cross-arm64':
                flags += ['--cpu=armv8-a']
                cc_bin = 'aarch64-linux-gnu-g++'
                test_prefix = ['qemu-aarch64', '-L', '/usr/aarch64-linux-gnu/']
            elif target == 'cross-ppc32':
                flags += ['--cpu=ppc32']
                cc_bin = 'powerpc-linux-gnu-g++'
                test_prefix = ['qemu-ppc', '-L', '/usr/powerpc-linux-gnu/']
            elif target == 'cross-ppc64':
                flags += ['--cpu=ppc64', '--with-endian=little']
                cc_bin = 'powerpc64le-linux-gnu-g++'
                test_prefix = ['qemu-ppc64le', '-L', '/usr/powerpc64le-linux-gnu/']
            else:
                raise Exception("Unknown cross target '%s' for Linux" % (target))
    else:
        # Flags specific to native targets
        flags += ['--with-bzip2', '--with-lzma', '--with-sqlite', '--with-zlib']

        if target_os == 'osx':
            # Test Boost on OS X
            flags += ['--with-boost']
        elif target not in ['sanitizer', 'valgrind', 'mini-shared', 'mini-static']:
            # Avoid OpenSSL when using dynamic checkers, or on OS X where it sporadically
            # is not installed on the CI image
            flags += ['--with-openssl']

        if target == 'coverage':
            flags += ['--with-tpm']
            test_cmd += ['--run-long-tests', '--run-online-tests']

            softhsm_lib = '/tmp/softhsm/lib/softhsm/libsofthsm2.so'
            if os.access(softhsm_lib, os.R_OK):
                test_cmd += ['--pkcs11-lib=%s' % (softhsm_lib)]

    flags += ['--cc-bin=%s%s' % ('ccache ' if use_ccache else '', cc_bin)]

    if test_cmd is None:
        run_test_command = None
    else:
        run_test_command = test_prefix + test_cmd

    return flags, run_test_command, make_prefix

def run_cmd(cmd, root_dir):
    """
    Execute a command, die if it failed
    """
    print("Running '%s':\n" % (' '.join(cmd)))
    sys.stdout.flush()

    start = time.time()

    sub_env = os.environ.copy()
    sub_env['LD_LIBRARY_PATH'] = root_dir
    proc = subprocess.Popen(cmd, close_fds=True, env=sub_env)
    proc.communicate()

    time_taken = int(time.time() - start)

    if time_taken > 2:
        print("Ran for %d seconds" % (time_taken))

    if proc.returncode != 0:
        print("Command failed with error code %d" % (proc.returncode))
        sys.exit(proc.returncode)

def parse_args(args):
    """
    Parse arguments
    """
    parser = optparse.OptionParser()

    parser.add_option('--os', default=platform.system().lower(),
                      help='Set the target os (default %default)')
    parser.add_option('--cc', default='gcc',
                      help='Set the target compiler type (default %default)')
    parser.add_option('--cc-bin', default=None,
                      help='Set path to compiler')
    parser.add_option('--root-dir', metavar='D', default='.',
                      help='Set directory to execute from (default %default)')

    parser.add_option('--branch', metavar='B', default=None,
                      help='Specify branch being built')

    parser.add_option('--add-travis-folds', action='store_true', default=False,
                      help='Add fold markers for Travis UI')

    parser.add_option('--dry-run', action='store_true', default=False,
                      help='Just show commands to be executed')
    parser.add_option('--build-jobs', metavar='J', default=get_concurrency(),
                      help='Set number of jobs to run in parallel (default %default)')

    parser.add_option('--with-ccache', dest='use_ccache', action='store_true', default=None,
                      help='Enable using ccache')
    parser.add_option('--without-ccache', dest='use_ccache', action='store_false',
                      help='Disable using ccache')

    parser.add_option('--with-python3', dest='use_python3', action='store_true', default=None,
                      help='Enable using python3')
    parser.add_option('--without-python3', dest='use_python3', action='store_false',
                      help='Disable using python3')

    return parser.parse_args(args)

def have_prog(prog):
    """
    Check if some named program exists in the path
    """
    for path in os.environ['PATH'].split(os.pathsep):
        exe_file = os.path.join(path, prog)
        if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
            return True

def main(args=None):
    # pylint: disable=too-many-branches,too-many-statements
    """
    Parse options, do the things
    """
    (options, args) = parse_args(args or sys.argv)

    if len(args) != 2:
        print('Usage: %s [options] target' % (args[0]))
        return 1

    if options.use_ccache is None:
        options.use_ccache = have_prog('ccache')

    use_python2 = have_prog('python2')

    if options.use_python3 is None:
        use_python3 = have_prog('python3')
    else:
        use_python3 = options.use_python3

    if options.cc_bin is None:
        if options.cc == 'gcc':
            options.cc_bin = 'g++'
        elif options.cc == 'clang':
            options.cc_bin = 'clang++'
        else:
            print('Error unknown compiler %s' % (options.cc))
            return 1

    target = args[1]

    root_dir = options.root_dir

    if os.access(root_dir, os.R_OK) != True:
        raise Exception('Bad root dir setting, dir %s not readable', root_dir)

    cmds = []

    if target == 'lint':

        if not use_python2 and not use_python3:
            raise Exception('No python interpreters found cannot lint')

        py_scripts = [
            'configure.py',
            'src/python/botan2.py',
            'src/scripts/ci_build.py',
            'src/scripts/install.py',
            'src/scripts/python_unittests.py',
            'src/scripts/python_unittests_unix.py']

        for target in py_scripts:
            target_path = os.path.join(root_dir, target)

            if use_python2:
                # Some disabled rules specific to Python2
                # superfluous-parens: needed for Python3 compatible print statements
                # too-many-locals: variable counting differs from pylint3
                py2_flags = '--disable=superfluous-parens,too-many-locals'
                cmds.append(['python2', '-m', 'pylint', py2_flags, target_path])

            if use_python3:
                cmds.append(['python3', '-m', 'pylint', target_path])

    else:
        config_flags, run_test_command, make_prefix = determine_flags(
            target, options.os, options.cc, options.cc_bin, options.use_ccache, root_dir)

        cmds.append([os.path.join(root_dir, 'configure.py')] + config_flags)

        if target == 'docs':
            cmds.append(['make', '-C', root_dir, 'docs'])
        else:
            if options.use_ccache:
                cmds.append(['ccache', '--show-stats'])

            make_targets = ['libs', 'cli', 'tests']
            if target in ['coverage', 'fuzzers']:
                make_targets += ['fuzzers', 'fuzzer_corpus_zip']

            cmds.append(make_prefix +
                        ['make', '-j', str(options.build_jobs), '-C', root_dir] +
                        make_targets)

            if options.use_ccache:
                cmds.append(['ccache', '--show-stats'])

        if run_test_command != None:
            cmds.append(run_test_command)

        if target in ['coverage', 'fuzzers']:
            cmds.append([os.path.join(root_dir, 'src/scripts/test_fuzzers.py'),
                         os.path.join(root_dir, 'fuzzer_corpus'),
                         os.path.join(root_dir, 'build/fuzzer')])

        if target in ['static', 'shared']:
            cmds.append([os.path.join(root_dir, 'src/scripts/cli_tests.py'),
                         os.path.join(root_dir, 'botan')])

        if target in ['shared', 'coverage']:

            if use_python2:
                cmds.append(['python2', os.path.join(root_dir, 'src/python/botan2.py')])
            if use_python3:
                cmds.append(['python3', os.path.join(root_dir, 'src/python/botan2.py')])

        if target != 'docs':
            cmds.append(['make', '-C', root_dir, 'install'])

        if target in ['coverage']:

            if not have_prog('lcov'):
                print('Error: lcov not found in PATH (%s)' % (os.getenv('PATH')))
                return 1

            if not have_prog('gcov'):
                print('Error: gcov not found in PATH (%s)' % (os.getenv('PATH')))
                return 1

            cmds.append(['lcov', '--capture', '--directory', options.root_dir, '--output-file', 'coverage.info.raw'])
            cmds.append(['lcov', '--remove', 'coverage.info.raw', '/usr/*', '--output-file', 'coverage.info'])
            cmds.append(['lcov', '--list', 'coverage.info'])

            if have_prog('coverage'):
                cmds.append(['coverage', 'run', '--branch', os.path.join(root_dir, 'src/python/botan2.py')])

            if have_prog('codecov'):
                # If codecov exists assume we are on Travis and report to codecov.io
                cmds.append(['codecov'])
            else:
                # Otherwise generate a local HTML report
                cmds.append(['genhtml', 'coverage.info', '--output-directory', 'lcov-out'])

    for cmd in cmds:
        if options.dry_run:
            print('$ ' + ' '.join(cmd))
        else:
            run_cmd(cmd, root_dir)

    return 0

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


<commit_before>#!/usr/bin/python
# buildtest.py
#
# Tests important algorithms after build.
#
# Seth Lemons
# October 31, 2008

import timeit, sys

if len(sys.argv) > 1 and sys.argv[1] == "--help":
    print "buildtest.py [nthreads] [nbperthread] [weight] [costbound]"

if len(sys.argv) > 1:
    nthreads = str(sys.argv[1])
else:
    nthreads = "2"
if len(sys.argv) > 2:
    nbperthread = str(sys.argv[2])
else:
    nbperthread = "5"
if len(sys.argv) > 3:
    weight = str(sys.argv[3])
else:
    weight = "1.1"
if len(sys.argv) > 4:
    costbound = str(sys.argv[4])
else:
    costbound = "100"

algs = ["astar",
        #"idastar",
        #"bfs",
        #"costbounddfs-"+costbound,
        "kbfs-"+nthreads,
        "pastar-"+nthreads,
        #"psdd-"+nthreads+"-"+nbperthread,
        "dynpsdd-"+nthreads+"-"+nbperthread+"-"+weight,
        "pbnf-"+nthreads+"-"+nbperthread]

for alg in algs:
    print alg
    t = timeit.Timer('print Popen(["./search", alg], stdout=PIPE, stdin=infile).stdout.read()', 'from __main__ import alg; from subprocess import Popen,PIPE; infile = open("grid/jordan_unit_four-way_1200x2000_2")')
    print "%0.3f seconds\n\n" % t.timeit(number=1)

<commit_msg>tiny fix to test script<commit_after>#!/usr/bin/python
# buildtest.py
#
# Tests important algorithms after build.
#
# Seth Lemons
# October 31, 2008

import timeit, sys

if len(sys.argv) > 1 and sys.argv[1] == "--help":
    print "buildtest.py [nthreads] [nbperthread] [weight] [costbound]"
    exit(0)

if len(sys.argv) > 1:
    nthreads = str(sys.argv[1])
else:
    nthreads = "2"
if len(sys.argv) > 2:
    nbperthread = str(sys.argv[2])
else:
    nbperthread = "5"
if len(sys.argv) > 3:
    weight = str(sys.argv[3])
else:
    weight = "1.1"
if len(sys.argv) > 4:
    costbound = str(sys.argv[4])
else:
    costbound = "100"

algs = ["astar",
        #"idastar",
        #"bfs",
        #"costbounddfs-"+costbound,
        "kbfs-"+nthreads,
        "pastar-"+nthreads,
        #"psdd-"+nthreads+"-"+nbperthread,
        "dynpsdd-"+nthreads+"-"+nbperthread+"-"+weight,
        "pbnf-"+nthreads+"-"+nbperthread]

for alg in algs:
    print alg
    t = timeit.Timer('print Popen(["./search", alg], stdout=PIPE, stdin=infile).stdout.read()', 'from __main__ import alg; from subprocess import Popen,PIPE; infile = open("grid/jordan_unit_four-way_1200x2000_2")')
    print "%0.3f seconds\n\n" % t.timeit(number=1)

<|endoftext|>


<commit_before>import math

def bng(input_lat, input_lon):
    """
    Convert lat and long to BNG
    Expects two floats as input
    Returns a tuple of Easting, Northing floats
    Accurate to ~5m

    For testing purposes:
    input: 51.44533267, -0.32824866
    output: 516275.97337480687, 173142.1442810801

    Entirely Based on code by Hannah Fry (CASA):
    hannahfry.co.uk/2012/02/01/converting-british-national-grid-to-latitude-and-longitude-ii/

    """
    # Simple bounds checking
    if not all([-90 <= input_lat <= 90, -180 <= input_lon <= 180]):
        raise Exception(
            "input_lat should be between 0 and 90, input_lon should be between -180 and 80")
    # Convert input to degrees
    lat_1 = input_lat * math.pi / 180
    lon_1 = input_lon * math.pi / 180
    # The GSR80 semi-major and semi-minor axes used for WGS84 (m)
    a_1, b_1 = 6378137.000, 6356752.3141
    # The eccentricity (squared) of the GRS80 ellipsoid
    e2_1 = 1 - (b_1 * b_1) / (a_1 * a_1)
    # Transverse radius of curvature
    nu_1 = a_1 / math.sqrt(1 - e2_1 * math.sin(lat_1) ** 2)
    # Third spherical coordinate is 0, in this case
    H = 0
    x_1 = (nu_1 + H) * math.cos(lat_1) * math.cos(lon_1)
    y_1 = (nu_1 + H) * math.cos(lat_1) * math.sin(lon_1)
    z_1 = ((1 - e2_1) * nu_1 + H) * math.sin(lat_1)

    # Perform Helmert transform (to go between Airy 1830 (_1) and GRS80 (_2))
    s = 20.4894 * 10 ** -6
    # The translations along x,y,z axes respectively
    tx, ty, tz = -446.448, 125.157, -542.060
    # The rotations along x,y,z respectively, in seconds
    rxs, rys, rzs = -0.1502, -0.2470, -0.8421
    # In radians
    rx, ry, rz = \
        rxs * math.pi / (180 * 3600.),\
        rys * math.pi / (180 * 3600.),\
        rzs * math.pi / (180 * 3600.)
    x_2 = tx + (1 + s) * x_1 + (-rz) * y_1 + (ry) * z_1
    y_2 = ty + (rz) * x_1 + (1 + s) * y_1 + (-rx) * z_1
    z_2 = tz + (-ry) * x_1 + (rx) * y_1 + (1 + s) * z_1

    # The GSR80 semi-major and semi-minor axes used for WGS84 (m)
    a, b = 6377563.396, 6356256.909
    # The eccentricity of the Airy 1830 ellipsoid
    e2 = 1 - (b * b) / (a * a)
    p = math.sqrt(x_2 ** 2 + y_2 ** 2)
    # Initial value
    lat = math.atan2(z_2, (p * (1 - e2)))
    latold = 2 * math.pi

    # Latitude is obtained by an iterative procedure
    while abs(lat - latold) > 10 ** -16:
        lat, latold = latold, lat
        nu = a / math.sqrt(1 - e2 * math.sin(latold) ** 2)
        lat = math.atan2(z_2 + e2 * nu * math.sin(latold), p)

    lon = math.atan2(y_2, x_2)
    H = p / math.cos(lat) - nu
    # Scale factor on the central meridian
    F0 = 0.9996012717
    # Latitude of true origin (radians)
    lat0 = 49 * math.pi / 180
    # Longtitude of true origin and central meridian (radians)
    lon0 = -2 * math.pi / 180
    # Northing & easting of true origin (m)
    N0, E0 = -100000, 400000
    n = (a - b) / (a + b)
    # Meridional radius of curvature
    rho = a * F0 * (1-e2) * (1 - e2 * math.sin(lat) ** 2) ** (-1.5)
    eta2 = nu * F0 / rho - 1

    M1 = (1 + n + (5/4) * n ** 2 + (5 / 4) * n ** 3) \
        * (lat - lat0)
    M2 = (3 * n + 3 * n ** 2 + (21 / 8) * n ** 3) \
        * math.sin(lat - lat0) * math.cos(lat + lat0)
    M3 = ((15 / 8) * n ** 2 + (15 / 8) * n ** 3) \
        * math.sin(2 * (lat-lat0)) * math.cos(2 * (lat + lat0))
    M4 = (35 / 24) * n ** 3 * math.sin(3 * (lat - lat0)) \
        * math.cos(3 * (lat + lat0))

    M = b * F0 * (M1 - M2 + M3 - M4)

    I = M + N0
    II = nu * F0 * math.sin(lat) * \
        math.cos(lat) / 2
    III = nu * F0 * math.sin(lat) * \
        math.cos(lat) ** 3 * (5 - math.tan(lat) ** 2 + 9 * eta2) / 24
    IIIA = nu * F0 * math.sin(lat) * \
        math.cos(lat) ** 5 * (61 - 58 * math.tan(lat) ** 2 + math.tan(lat) ** 4) / 720
    IV = nu * F0 * math.cos(lat)
    V = nu * F0 * \
        math.cos(lat) ** 3 * (nu / rho - math.tan(lat) ** 2) / 6
    VI = nu * F0 * math.cos(lat) ** 5 * \
        (5 - 18 * math.tan(lat) ** 2 + math.tan(lat) ** 4 + 14 * eta2 - 58 * eta2 * math.tan(lat) ** 2) / 120

    N = I + II * (lon - lon0) \
        ** 2 + III * (lon - lon0) ** 4 + IIIA * (lon - lon0) ** 6
    E = E0 + IV * (lon - lon0) + V * (lon - lon0) \
        ** 3 + VI * (lon - lon0) ** 5
    return E, N
<commit_msg>Fix python conversion error message<commit_after>import math

def bng(input_lat, input_lon):
    """
    Convert lat and long to BNG
    Expects two floats as input
    Returns a tuple of Easting, Northing floats
    Accurate to ~5m

    For testing purposes:
    input: 51.44533267, -0.32824866
    output: 516275.97337480687, 173142.1442810801

    Entirely Based on code by Hannah Fry (CASA):
    hannahfry.co.uk/2012/02/01/converting-british-national-grid-to-latitude-and-longitude-ii/

    """
    # Simple bounds checking
    if not all([-90 <= input_lat <= 90, -180 <= input_lon <= 180]):
        raise Exception(
            "input_lat should be between -90 and 90, input_lon should be between -180 and 180")
    # Convert input to degrees
    lat_1 = input_lat * math.pi / 180
    lon_1 = input_lon * math.pi / 180
    # The GSR80 semi-major and semi-minor axes used for WGS84 (m)
    a_1, b_1 = 6378137.000, 6356752.3141
    # The eccentricity (squared) of the GRS80 ellipsoid
    e2_1 = 1 - (b_1 * b_1) / (a_1 * a_1)
    # Transverse radius of curvature
    nu_1 = a_1 / math.sqrt(1 - e2_1 * math.sin(lat_1) ** 2)
    # Third spherical coordinate is 0, in this case
    H = 0
    x_1 = (nu_1 + H) * math.cos(lat_1) * math.cos(lon_1)
    y_1 = (nu_1 + H) * math.cos(lat_1) * math.sin(lon_1)
    z_1 = ((1 - e2_1) * nu_1 + H) * math.sin(lat_1)

    # Perform Helmert transform (to go between Airy 1830 (_1) and GRS80 (_2))
    s = 20.4894 * 10 ** -6
    # The translations along x,y,z axes respectively
    tx, ty, tz = -446.448, 125.157, -542.060
    # The rotations along x,y,z respectively, in seconds
    rxs, rys, rzs = -0.1502, -0.2470, -0.8421
    # In radians
    rx, ry, rz = \
        rxs * math.pi / (180 * 3600.),\
        rys * math.pi / (180 * 3600.),\
        rzs * math.pi / (180 * 3600.)
    x_2 = tx + (1 + s) * x_1 + (-rz) * y_1 + (ry) * z_1
    y_2 = ty + (rz) * x_1 + (1 + s) * y_1 + (-rx) * z_1
    z_2 = tz + (-ry) * x_1 + (rx) * y_1 + (1 + s) * z_1

    # The GSR80 semi-major and semi-minor axes used for WGS84 (m)
    a, b = 6377563.396, 6356256.909
    # The eccentricity of the Airy 1830 ellipsoid
    e2 = 1 - (b * b) / (a * a)
    p = math.sqrt(x_2 ** 2 + y_2 ** 2)
    # Initial value
    lat = math.atan2(z_2, (p * (1 - e2)))
    latold = 2 * math.pi

    # Latitude is obtained by an iterative procedure
    while abs(lat - latold) > 10 ** -16:
        lat, latold = latold, lat
        nu = a / math.sqrt(1 - e2 * math.sin(latold) ** 2)
        lat = math.atan2(z_2 + e2 * nu * math.sin(latold), p)

    lon = math.atan2(y_2, x_2)
    H = p / math.cos(lat) - nu
    # Scale factor on the central meridian
    F0 = 0.9996012717
    # Latitude of true origin (radians)
    lat0 = 49 * math.pi / 180
    # Longtitude of true origin and central meridian (radians)
    lon0 = -2 * math.pi / 180
    # Northing & easting of true origin (m)
    N0, E0 = -100000, 400000
    n = (a - b) / (a + b)
    # Meridional radius of curvature
    rho = a * F0 * (1-e2) * (1 - e2 * math.sin(lat) ** 2) ** (-1.5)
    eta2 = nu * F0 / rho - 1

    M1 = (1 + n + (5/4) * n ** 2 + (5 / 4) * n ** 3) \
        * (lat - lat0)
    M2 = (3 * n + 3 * n ** 2 + (21 / 8) * n ** 3) \
        * math.sin(lat - lat0) * math.cos(lat + lat0)
    M3 = ((15 / 8) * n ** 2 + (15 / 8) * n ** 3) \
        * math.sin(2 * (lat-lat0)) * math.cos(2 * (lat + lat0))
    M4 = (35 / 24) * n ** 3 * math.sin(3 * (lat - lat0)) \
        * math.cos(3 * (lat + lat0))

    M = b * F0 * (M1 - M2 + M3 - M4)

    I = M + N0
    II = nu * F0 * math.sin(lat) * \
        math.cos(lat) / 2
    III = nu * F0 * math.sin(lat) * \
        math.cos(lat) ** 3 * (5 - math.tan(lat) ** 2 + 9 * eta2) / 24
    IIIA = nu * F0 * math.sin(lat) * \
        math.cos(lat) ** 5 * (61 - 58 * math.tan(lat) ** 2 + math.tan(lat) ** 4) / 720
    IV = nu * F0 * math.cos(lat)
    V = nu * F0 * \
        math.cos(lat) ** 3 * (nu / rho - math.tan(lat) ** 2) / 6
    VI = nu * F0 * math.cos(lat) ** 5 * \
        (5 - 18 * math.tan(lat) ** 2 + math.tan(lat) ** 4 + 14 * eta2 - 58 * eta2 * math.tan(lat) ** 2) / 120

    N = I + II * (lon - lon0) \
        ** 2 + III * (lon - lon0) ** 4 + IIIA * (lon - lon0) ** 6
    E = E0 + IV * (lon - lon0) + V * (lon - lon0) \
        ** 3 + VI * (lon - lon0) ** 5
    return E, N
<|endoftext|>


<commit_before>#!/usr/bin/python

'''
## What it does now

A command-line tool that takes a Markdown document, templates it as a plaintext email, tags its
links with Google Analytics parameters and copies its name to the clipboard.

## Doing

* Read in the contents of a markdown file

## Todo

* Wrap long text for plaintext emails
* Allow to specify if HTML email wanted (perhaps that should be the default)
* Tag conext with HTML temtplates read from files
'''

import argparse
import sys
import gtk
import re

def cmdline_parse():
	"""Parse the Command-Line arguments and return the options object"""

	# Further documentation at <http://docs.python.org/howto/argparse.html#id1>

	parser = argparse.ArgumentParser(
			description = 'Tag and template Markdown text and copy to clipboard.')
	parser.add_argument("filename", help="file in markdown format")
	return parser.parse_args()

def tag_urls(text):
	# Thanks to <http://stackoverflow.com/a/828458/248220>
	traffic_source = "bite_news"
	medium = "email"
	campaign = "newsletter-2000-01-01"
	tags = 'utm_source='+traffic_source+'&utm_medium='+medium+'&utm_campaign='+campaign;
	# urlfinder = re.compile('^(http:\/\/\S+)')
	urlfinder2 = re.compile('(http:\/\/\S+[^>) \.])')
	# text = urlfinder.sub(r'\1?'+tags, text)
	return urlfinder2.sub(r'\1?'+tags, text)

def template_plain_text(text):
	return "Hi, {!firstname_fix}\n\n"+text

def read_input(filename):
	file = open(filename)
	return file.read()

if __name__ == '__main__':
	cmdline_arguments = cmdline_parse()
	for_output = read_input(cmdline_arguments.filename)
	for_output = template_plain_text(for_output)
	for_output = tag_urls(for_output)

	print for_output
	gtk.Clipboard().set_text(for_output)
	gtk.Clipboard().store()<commit_msg>Only tag specified domain, my domain is be default<commit_after>#!/usr/bin/python

'''
## What it does now

A command-line tool that takes a Markdown document, templates it as a plaintext email, tags its
links with Google Analytics parameters and copies its name to the clipboard.

## Doing

* Only tag URLs matching a certain domain

## Todo

* Wrap long text for plaintext emails
* Allow to specify if HTML email wanted (perhaps that should be the default)
* Tag conext with HTML temtplates read from files
'''

import argparse
import sys
import gtk
import re

def cmdline_parse():
	"""Parse the Command-Line arguments and return the options object"""

	# Further documentation at <http://docs.python.org/howto/argparse.html#id1>

	parser = argparse.ArgumentParser(
			description = 'Tag and template Markdown text and copy to clipboard.')

	parser.add_argument('--tagdomain', help='domain which should be tagged with Google Analytics, without http://', default='www.bitesizeirishgaelic.com')
	parser.add_argument('filename', help="file in markdown format")
	return parser.parse_args()

def tag_urls(text):
	# Thanks to <http://stackoverflow.com/a/828458/248220>
	traffic_source = "bite_news"
	medium = "email"
	campaign = "newsletter-2000-01-01"
	tags = 'utm_source='+traffic_source+'&utm_medium='+medium+'&utm_campaign='+campaign;
	# urlfinder = re.compile('^(http:\/\/\S+)')
	# urlfinder2 = re.compile('(http:\/\/\S+[^>) \.])')
	urlfinder2 = re.compile('(http:\/\/www.bitesizeirishgaelic.com\S+[^>) \.])')
	# text = urlfinder.sub(r'\1?'+tags, text)
	return urlfinder2.sub(r'\1?'+tags, text)

def template_plain_text(text):
	return "Hi, {!firstname_fix}\n\n"+text

def read_input(filename):
	file = open(filename)
	return file.read()

if __name__ == '__main__':
	cmdline_arguments = cmdline_parse()
	for_output = read_input(cmdline_arguments.filename)
	for_output = template_plain_text(for_output)
	for_output = tag_urls(for_output)

	print for_output
	gtk.Clipboard().set_text(for_output)
	gtk.Clipboard().store()<|endoftext|>