<commit_before>import sys, os
import unittest

from nose.plugins.skip import SkipTest
from numpy.testing import assert_array_almost_equal

cwd = os.path.abspath(os.path.split(__file__)[0])
sys.path.insert(0, os.path.join(cwd, '..'))

from dataviews.views import Layout, GridLayout
from dataviews import DataOverlay,  DataStack,  Annotation, Curve, Histogram
from dataviews import TableStack, Table
from dataviews import SheetOverlay, SheetStack, SheetView, Points, Contours
from dataviews import CoordinateGrid, DataGrid

from dataviews.options import StyleOpts, PlotOpts, ChannelOpts

from IPython.display import HTML, SVG

class ViewTestCase(unittest.TestCase):
    """
    The class implements comparisons between View objects for the
    purposes of testing. The most important attribute that needs to be
    compared is the data attribute as this contains the raw data held
    by the View object.
    """
    def __init__(self, *args, **kwargs):
        super(ViewTestCase, self).__init__(*args, **kwargs)
        # General view classes
        self.addTypeEqualityFunc(GridLayout,   self.compare_gridlayout)
        self.addTypeEqualityFunc(Layout,       self.compare_layouts)
        self.addTypeEqualityFunc(Annotation,   self.compare_annotations)
        # DataLayers
        self.addTypeEqualityFunc(DataOverlay,  self.compare_dataoverlays)
        self.addTypeEqualityFunc(DataStack,    self.compare_datastack)
        self.addTypeEqualityFunc(Curve,        self.compare_curve)
        self.addTypeEqualityFunc(Histogram,    self.compare_histogram)
        # Tables
        self.addTypeEqualityFunc(TableStack,   self.compare_tablestack)
        self.addTypeEqualityFunc(Table,        self.compare_tables)
        # SheetLayers
        self.addTypeEqualityFunc(SheetOverlay, self.compare_sheetoverlays)
        self.addTypeEqualityFunc(SheetStack,   self.compare_sheetstack)
        self.addTypeEqualityFunc(SheetView,    self.compare_sheetviews)
        self.addTypeEqualityFunc(Contours,     self.compare_contours)
        self.addTypeEqualityFunc(Points,       self.compare_points)
        # CoordinateGrid and DataGrid
        self.addTypeEqualityFunc(CoordinateGrid, self.compare_coordgrids)
        self.addTypeEqualityFunc(DataGrid,       self.compare_datagrids)
        # Option objects
        self.addTypeEqualityFunc(StyleOpts, self.compare_opts)
        self.addTypeEqualityFunc(PlotOpts, self.compare_opts)
        self.addTypeEqualityFunc(ChannelOpts, self.compare_channelopts)

    def compare_opts(self, opt1, opt2, msg):
        self.assertEqual(opt1.items, opt2.items)

    def compare_channelopts(self, opt1, opt2, msg):
        self.assertEqual(opt1.mode, opt2.mode)
        self.assertEqual(opt1.pattern, opt2.pattern)
        self.assertEqual(opt1.patter, opt2.pattern)

    #================#
    # Helper methods #
    #================#

    def compare_arrays(self, arr1, arr2, name):
        try:
            assert_array_almost_equal(arr1, arr2)
        except AssertionError as e:
            raise self.failureException(name+': '+str(e))

    def bounds_check(self, view1, view2):
        if view1.bounds.lbrt() != view2.bounds.lbrt():
            raise self.failureException("BoundingBoxes are mismatched.")

    def compare_stack(self, view1, view2, msg):

        if view1.ndims != view2.ndims:
            raise self.failureException("Stacks have different numbers of dimensions.")

        if view1.dimension_labels != view2.dimension_labels:
            raise self.failureException("Stacks have different dimension labels.")

        if len(view1.keys()) != len(view2.keys()):
            raise self.failureException("Stacks have different numbers of keys.")

        if set(view1.keys()) != set(view2.keys()):
            raise self.failureException("Stacks have different sets of keys.")

        for el1, el2 in zip(view1, view2):
            self.assertEqual(el1,el2)

    #=================#
    # Generic classes #
    #=================#

    def compare_gridlayout(self, view1, view2, msg):
        if len(view1) != len(view2):
            raise self.failureException("GridLayouts have different sizes.")

        if set(view1.keys()) != set(view2.keys()):
            raise self.failureException("GridLayouts have different keys.")

        for el1, el2 in zip(view1, view2):
            self.assertEqual(el1,el2)

    def compare_layouts(self, view1, view2, msg):
        for el1, el2 in zip(view1, view1):
            self.assertEqual(el1, el2)

    def compare_annotations(self, view1, view2, msg):
        if set(view1.data) != set(view2.data):
            raise self.failureException("Annotations contain different sets of annotations.")

    #============#
    # DataLayers #
    #============#

    def compare_datastack(self, view1, view2, msg):
        self.compare_stack(view1, view2, msg)

    def compare_dataoverlays(self, view1, view2, msg):
        if len(view1) != len(view2):
            raise self.failureException("DataOverlays have different lengths.")

        for (layer1, layer2) in zip(view1, view2):
            self.assertEqual(layer1, layer2)

    def compare_curve(self, view1, view2, msg):
        if view1.cyclic_range != view2.cyclic_range:
            raise self.failureException("Curves do not have matching cyclic_range.")
        self.compare_arrays(view1.data, view2.data, 'Curve data')

    def compare_histogram(self, view1, view2, msg):

        if view1.cyclic_range != view2.cyclic_range:
            raise self.failureException("Histograms do not have matching cyclic_range.")

        self.compare_arrays(view1.edges, view2.edges, "Histogram edges")
        self.compare_arrays(view1.values, view2.values, "Histogram values")

    #========#
    # Tables #
    #========#

    def compare_tablestack(self, view1, view2, msg):
        self.compare_stack(view1, view2, msg)

    def compare_tables(self, view1, view2, msg):

        if view1.rows != view2.rows:
            raise self.failureException("Tables have different numbers of rows.")

        if view1.cols != view2.cols:
            raise self.failureException("Tables have different numbers of columns.")

        if view1.heading_map != view2.heading_map:
            raise self.failureException("Tables have different headings.")

        for heading in view1.heading_values():
            self.assertEqual(view1[heading], view2[heading])

    #=============#
    # SheetLayers #
    #=============#

    def compare_sheetstack(self, view1, view2, msg):
        self.bounds_check(view1,view2)
        self.compare_stack(view1, view2, msg)

    def compare_sheetoverlays(self, view1, view2, msg):
        if len(view1) != len(view2):
            raise self.failureException("SheetOverlays have different lengths.")

        self.bounds_check(view1, view2)

        for (layer1, layer2) in zip(view1, view2):
            self.assertEqual(layer1, layer2)

    def compare_sheetviews(self, view1, view2, msg):
        self.compare_arrays(view1.data, view2.data, 'SheetView')
        self.bounds_check(view1,view2)

    def compare_contours(self, view1, view2, msg):
        self.bounds_check(view1, view2)

        if len(view1) != len(view2):
            raise self.failureException("Contours do not have a matching number of contours.")

        for c1, c2 in zip(view1.data, view2.data):
            self.compare_arrays(c1, c2, 'Contour data')

    def compare_points(self, view1, view2, msg):
        self.bounds_check(view1, view2)

        if len(view1) != len(view2):
            raise self.failureException("Points objects have different numbers of points.")

        self.compare_arrays(view1.data, view2.data, 'Points data')

    #=======#
    # Grids #
    #=======#

    def compare_grids(self, view1, view2, name ):

        if len(view1.keys()) != len(view2.keys()):
            raise self.failureException("%ss have different numbers of items." % name)

        if set(view1.keys()) != set(view2.keys()):
            raise self.failureException("%ss have different keys." % name)

        if len(view1) != len(view2):
            raise self.failureException("%ss have different depths." % name)

        for el1, el2 in zip(view1, view2):
            self.assertEqual(el1, el2)

    def compare_coordgrids(self, view1, view2, msg):
        self.bounds_check(view1, view2)
        self.compare_grids(view1, view2, 'CoordinateGrid')

    def compare_datagrids(self, view1, view2, msg):
        self.compare_grids(view1, view2, 'DataGrid')

class IPTestCase(ViewTestCase):
    """
    This class extends ViewTestCase to handle IPython specific objects.
    """

    def setUp(self):
        super(IPTestCase, self).setUp()
        try:
            import IPython
            self.ip = IPython.InteractiveShell()
            if self.ip is None:
                raise TypeError()
        except Exception:
                raise SkipTest("IPython could not be started")

        self.addTypeEqualityFunc(HTML, self.skip_comparison)
        self.addTypeEqualityFunc(SVG,  self.skip_comparison)

    def skip_comparison(self, obj1, obj2, msg):
        pass

    def get_object(self, name):
        obj = self.ip._object_find(name).obj
        if obj is None:
            raise self.failureException("Could not find object %s" % name)
        return obj

    def cell(self, line):
        "Run an IPython cell"
        self.ip.run_cell(line, silent=True)

    def cell_magic(self, *args, **kwargs):
        "Run an IPython cell magic"
        self.ip.run_cell_magic(*args, **kwargs)

    def line_magic(self, *args, **kwargs):
        "Run an IPython line magic"
        self.ip.run_line_magic(*args, **kwargs)
<commit_msg>Added dimension comparisons to utils and improved method ordering<commit_after>import sys, os
import unittest

from nose.plugins.skip import SkipTest
from numpy.testing import assert_array_almost_equal

cwd = os.path.abspath(os.path.split(__file__)[0])
sys.path.insert(0, os.path.join(cwd, '..'))

from dataviews.views import Layout, GridLayout
from dataviews import DataOverlay,  DataStack,  Annotation, Curve, Histogram
from dataviews import TableStack, Table
from dataviews import SheetOverlay, SheetStack, SheetView, Points, Contours
from dataviews import CoordinateGrid, DataGrid

from dataviews import Dimension
from dataviews.options import StyleOpts, PlotOpts, ChannelOpts

from IPython.display import HTML, SVG

class ViewTestCase(unittest.TestCase):
    """
    The class implements comparisons between View objects for the
    purposes of testing. The most important attribute that needs to be
    compared is the data attribute as this contains the raw data held
    by the View object.
    """
    def __init__(self, *args, **kwargs):
        super(ViewTestCase, self).__init__(*args, **kwargs)
        # General view classes
        self.addTypeEqualityFunc(GridLayout,   self.compare_gridlayout)
        self.addTypeEqualityFunc(Layout,       self.compare_layouts)
        self.addTypeEqualityFunc(Annotation,   self.compare_annotations)
        # DataLayers
        self.addTypeEqualityFunc(DataOverlay,  self.compare_dataoverlays)
        self.addTypeEqualityFunc(DataStack,    self.compare_datastack)
        self.addTypeEqualityFunc(Curve,        self.compare_curve)
        self.addTypeEqualityFunc(Histogram,    self.compare_histogram)
        # Tables
        self.addTypeEqualityFunc(TableStack,   self.compare_tablestack)
        self.addTypeEqualityFunc(Table,        self.compare_tables)
        # SheetLayers
        self.addTypeEqualityFunc(SheetOverlay, self.compare_sheetoverlays)
        self.addTypeEqualityFunc(SheetStack,   self.compare_sheetstack)
        self.addTypeEqualityFunc(SheetView,    self.compare_sheetviews)
        self.addTypeEqualityFunc(Contours,     self.compare_contours)
        self.addTypeEqualityFunc(Points,       self.compare_points)
        # CoordinateGrid and DataGrid
        self.addTypeEqualityFunc(CoordinateGrid, self.compare_coordgrids)
        self.addTypeEqualityFunc(DataGrid,       self.compare_datagrids)
        # Option objects
        self.addTypeEqualityFunc(StyleOpts, self.compare_opts)
        self.addTypeEqualityFunc(PlotOpts, self.compare_opts)
        self.addTypeEqualityFunc(ChannelOpts, self.compare_channelopts)
        # Dimension objects
        self.addTypeEqualityFunc(Dimension, self.compare_dims)

    #================#
    # Helper methods #
    #================#

    def compare_arrays(self, arr1, arr2, name):
        try:
            assert_array_almost_equal(arr1, arr2)
        except AssertionError as e:
            raise self.failureException(name+': '+str(e))

    def bounds_check(self, view1, view2):
        if view1.bounds.lbrt() != view2.bounds.lbrt():
            raise self.failureException("BoundingBoxes are mismatched.")

    def compare_stack(self, view1, view2, msg):

        if view1.ndims != view2.ndims:
            raise self.failureException("Stacks have different numbers of dimensions.")

        if view1.dimension_labels != view2.dimension_labels:
            raise self.failureException("Stacks have different dimension labels.")

        if len(view1.keys()) != len(view2.keys()):
            raise self.failureException("Stacks have different numbers of keys.")

        if set(view1.keys()) != set(view2.keys()):
            raise self.failureException("Stacks have different sets of keys.")

        for el1, el2 in zip(view1, view2):
            self.assertEqual(el1,el2)

    #=================#
    # Generic classes #
    #=================#

    def compare_gridlayout(self, view1, view2, msg):
        if len(view1) != len(view2):
            raise self.failureException("GridLayouts have different sizes.")

        if set(view1.keys()) != set(view2.keys()):
            raise self.failureException("GridLayouts have different keys.")

        for el1, el2 in zip(view1, view2):
            self.assertEqual(el1,el2)

    def compare_layouts(self, view1, view2, msg):
        for el1, el2 in zip(view1, view1):
            self.assertEqual(el1, el2)

    def compare_annotations(self, view1, view2, msg):
        if set(view1.data) != set(view2.data):
            raise self.failureException("Annotations contain different sets of annotations.")

    #============#
    # DataLayers #
    #============#

    def compare_datastack(self, view1, view2, msg):
        self.compare_stack(view1, view2, msg)

    def compare_dataoverlays(self, view1, view2, msg):
        if len(view1) != len(view2):
            raise self.failureException("DataOverlays have different lengths.")

        for (layer1, layer2) in zip(view1, view2):
            self.assertEqual(layer1, layer2)

    def compare_curve(self, view1, view2, msg):
        if view1.cyclic_range != view2.cyclic_range:
            raise self.failureException("Curves do not have matching cyclic_range.")
        self.compare_arrays(view1.data, view2.data, 'Curve data')

    def compare_histogram(self, view1, view2, msg):

        if view1.cyclic_range != view2.cyclic_range:
            raise self.failureException("Histograms do not have matching cyclic_range.")

        self.compare_arrays(view1.edges, view2.edges, "Histogram edges")
        self.compare_arrays(view1.values, view2.values, "Histogram values")

    #========#
    # Tables #
    #========#

    def compare_tablestack(self, view1, view2, msg):
        self.compare_stack(view1, view2, msg)

    def compare_tables(self, view1, view2, msg):

        if view1.rows != view2.rows:
            raise self.failureException("Tables have different numbers of rows.")

        if view1.cols != view2.cols:
            raise self.failureException("Tables have different numbers of columns.")

        if view1.heading_map != view2.heading_map:
            raise self.failureException("Tables have different headings.")

        for heading in view1.heading_values():
            self.assertEqual(view1[heading], view2[heading])

    #=============#
    # SheetLayers #
    #=============#

    def compare_sheetstack(self, view1, view2, msg):
        self.bounds_check(view1,view2)
        self.compare_stack(view1, view2, msg)

    def compare_sheetoverlays(self, view1, view2, msg):
        if len(view1) != len(view2):
            raise self.failureException("SheetOverlays have different lengths.")

        self.bounds_check(view1, view2)

        for (layer1, layer2) in zip(view1, view2):
            self.assertEqual(layer1, layer2)

    def compare_sheetviews(self, view1, view2, msg):
        self.compare_arrays(view1.data, view2.data, 'SheetView')
        self.bounds_check(view1,view2)

    def compare_contours(self, view1, view2, msg):
        self.bounds_check(view1, view2)

        if len(view1) != len(view2):
            raise self.failureException("Contours do not have a matching number of contours.")

        for c1, c2 in zip(view1.data, view2.data):
            self.compare_arrays(c1, c2, 'Contour data')

    def compare_points(self, view1, view2, msg):
        self.bounds_check(view1, view2)

        if len(view1) != len(view2):
            raise self.failureException("Points objects have different numbers of points.")

        self.compare_arrays(view1.data, view2.data, 'Points data')

    #=======#
    # Grids #
    #=======#

    def compare_grids(self, view1, view2, name ):

        if len(view1.keys()) != len(view2.keys()):
            raise self.failureException("%ss have different numbers of items." % name)

        if set(view1.keys()) != set(view2.keys()):
            raise self.failureException("%ss have different keys." % name)

        if len(view1) != len(view2):
            raise self.failureException("%ss have different depths." % name)

        for el1, el2 in zip(view1, view2):
            self.assertEqual(el1, el2)

    def compare_coordgrids(self, view1, view2, msg):
        self.bounds_check(view1, view2)
        self.compare_grids(view1, view2, 'CoordinateGrid')

    def compare_datagrids(self, view1, view2, msg):
        self.compare_grids(view1, view2, 'DataGrid')

    #=========#
    # Options #
    #=========#

    def compare_opts(self, opt1, opt2, msg):
        self.assertEqual(opt1.items, opt2.items)

    def compare_channelopts(self, opt1, opt2, msg):
        self.assertEqual(opt1.mode, opt2.mode)
        self.assertEqual(opt1.pattern, opt2.pattern)
        self.assertEqual(opt1.patter, opt2.pattern)

    #============#
    # Dimensions #
    #============#

    def compare_dims(self, dim1, dim2, msg):
        if dim1.name != dim2.name:
            raise self.failureException("Dimension names are mismatched.")
        if dim1.cyclic != dim1.cyclic:
            raise self.failureException("Dimension cyclic declarations mismatched.")
        if dim1.range != dim1.range:
            raise self.failureException("Dimension ranges mismatched.")
        if dim1.type != dim1.type:
            raise self.failureException("Dimension type declarations mismatched.")
        if dim1.unit != dim1.unit:
            raise self.failureException("Dimension unit declarations mismatched.")
        if dim1.format_string != dim1.format_string:
            raise self.failureException("Dimension format string declarations mismatched.")

class IPTestCase(ViewTestCase):
    """
    This class extends ViewTestCase to handle IPython specific objects.
    """

    def setUp(self):
        super(IPTestCase, self).setUp()
        try:
            import IPython
            self.ip = IPython.InteractiveShell()
            if self.ip is None:
                raise TypeError()
        except Exception:
                raise SkipTest("IPython could not be started")

        self.addTypeEqualityFunc(HTML, self.skip_comparison)
        self.addTypeEqualityFunc(SVG,  self.skip_comparison)

    def skip_comparison(self, obj1, obj2, msg):
        pass

    def get_object(self, name):
        obj = self.ip._object_find(name).obj
        if obj is None:
            raise self.failureException("Could not find object %s" % name)
        return obj

    def cell(self, line):
        "Run an IPython cell"
        self.ip.run_cell(line, silent=True)

    def cell_magic(self, *args, **kwargs):
        "Run an IPython cell magic"
        self.ip.run_cell_magic(*args, **kwargs)

    def line_magic(self, *args, **kwargs):
        "Run an IPython line magic"
        self.ip.run_line_magic(*args, **kwargs)
<|endoftext|>


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

import contextlib

from oslo.config import cfg
from oslotest import base as test_base
from oslotest import moxstubout
import six

if six.PY3:
    @contextlib.contextmanager
    def nested(*contexts):
        with contextlib.ExitStack() as stack:
            for c in contexts:
                stack.enter_context(c)
            yield
else:
    nested = contextlib.nested

class BaseTestCase(test_base.BaseTestCase):
    def setUp(self, conf=cfg.CONF):
        super(BaseTestCase, self).setUp()
        moxfixture = self.useFixture(moxstubout.MoxStubout())
        self.mox = moxfixture.mox
        self.stubs = moxfixture.stubs
        self.conf = conf
        self.addCleanup(self.conf.reset)
<commit_msg>Fix nested() for py3<commit_after># Copyright 2010-2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import contextlib

from oslo.config import cfg
from oslotest import base as test_base
from oslotest import moxstubout
import six

if six.PY3:
    @contextlib.contextmanager
    def nested(*contexts):
        with contextlib.ExitStack() as stack:
            yield [stack.enter_context(c) for c in contexts]
else:
    nested = contextlib.nested

class BaseTestCase(test_base.BaseTestCase):
    def setUp(self, conf=cfg.CONF):
        super(BaseTestCase, self).setUp()
        moxfixture = self.useFixture(moxstubout.MoxStubout())
        self.mox = moxfixture.mox
        self.stubs = moxfixture.stubs
        self.conf = conf
        self.addCleanup(self.conf.reset)
<|endoftext|>


<commit_before>import os
import sys
import time2

try:
    import pygtk
    pygtk.require('2.0')
except:
    pass

import gtk

def refresh_gui(delay=0):
    while gtk.events_pending():
        gtk.main_iteration_do(block=False)
    time.sleep(delay)

dir = os.path.dirname(__file__)
if not dir in sys.path:
    sys.path.insert(0, os.path.join(dir))

from kiwi.environ import environ
environ.add_resource('glade', dir)
<commit_msg>test pyflakes break<commit_after>import os
import sys
import time3

try:
    import pygtk
    pygtk.require('2.0')
except:
    pass

import gtk

def refresh_gui(delay=0):
    while gtk.events_pending():
        gtk.main_iteration_do(block=False)
    time.sleep(delay)

dir = os.path.dirname(__file__)
if not dir in sys.path:
    sys.path.insert(0, os.path.join(dir))

from kiwi.environ import environ
environ.add_resource('glade', dir)
<|endoftext|>


<commit_before># Copyright 2016-2017 Capital One Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import fnmatch
import json
import os
import shutil
import zipfile
from datetime import datetime, timedelta, tzinfo
from distutils.util import strtobool

import boto3
import placebo
from botocore.response import StreamingBody
from placebo import pill
from six import StringIO

from c7n.testing import CustodianTestCore

###########################################################################
# BEGIN PLACEBO MONKEY PATCH
#
# Placebo is effectively abandoned upstream, since mitch went back to work at AWS, irony...
# These monkeypatch patches represent fixes on trunk of that repo that have not been released
# into an extant version, we carry them here. We can drop this when this issue is resolved
#
# https://github.com/garnaat/placebo/issues/63
#
# License - Apache 2.0
# Copyright (c) 2015 Mitch Garnaat

class UTC(tzinfo):
    """UTC"""

    def utcoffset(self, dt):
        return timedelta(0)

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return timedelta(0)

utc = UTC()

def deserialize(obj):
    """Convert JSON dicts back into objects."""
    # Be careful of shallow copy here
    target = dict(obj)
    class_name = None
    if "__class__" in target:
        class_name = target.pop("__class__")
    if "__module__" in obj:
        obj.pop("__module__")
    # Use getattr(module, class_name) for custom types if needed
    if class_name == "datetime":
        return datetime(tzinfo=utc, **target)
    if class_name == "StreamingBody":
        return StringIO(target["body"])
    # Return unrecognized structures as-is
    return obj

def serialize(obj):
    """Convert objects into JSON structures."""
    # Record class and module information for deserialization
    result = {"__class__": obj.__class__.__name__}
    try:
        result["__module__"] = obj.__module__
    except AttributeError:
        pass

    # Convert objects to dictionary representation based on type
    if isinstance(obj, datetime):
        result["year"] = obj.year
        result["month"] = obj.month
        result["day"] = obj.day
        result["hour"] = obj.hour
        result["minute"] = obj.minute
        result["second"] = obj.second
        result["microsecond"] = obj.microsecond
        return result
    if isinstance(obj, StreamingBody):
        result["body"] = obj.read()
        obj._raw_stream = StringIO(result["body"])
        obj._amount_read = 0
        return result
    if isinstance(obj, bytes):
        return obj.decode('utf8')

    # Raise a TypeError if the object isn't recognized
    raise TypeError("Type not serializable")

pill.FakeHttpResponse.raw = None
placebo.pill.serialize = serialize
placebo.pill.deserialize = deserialize

# END PLACEBO MONKEY
##########################################################################

class BluePill(pill.Pill):

    def playback(self):
        super(BluePill, self).playback()
        self._avail = self.get_available()

    def get_available(self):
        return set(
            [
                os.path.join(self.data_path, n)
                for n in fnmatch.filter(os.listdir(self.data_path), "*.json")
            ]
        )

    def get_next_file_path(self, service, operation):
        fn = super(BluePill, self).get_next_file_path(service, operation)
        # couple of double use cases
        if fn in self._avail:
            self._avail.remove(fn)
        else:
            print("\ndouble use %s\n" % fn)
        return fn

    def stop(self):
        result = super(BluePill, self).stop()
        if self._avail:
            print("Unused json files \n %s" % ("\n".join(sorted(self._avail))))
        return result

class ZippedPill(pill.Pill):

    def __init__(self, path, prefix=None, debug=False):
        super(ZippedPill, self).__init__(prefix, debug)
        self.path = path
        self._used = set()
        self.archive = None

    def playback(self):
        self.archive = zipfile.ZipFile(self.path, "r")
        self._files = set(self.archive.namelist())
        return super(ZippedPill, self).playback()

    def record(self):
        self.archive = zipfile.ZipFile(self.path, "a", zipfile.ZIP_DEFLATED)
        self._files = set()

        files = set([n for n in self.archive.namelist() if n.startswith(self.prefix)])

        if not files:
            return super(ZippedPill, self).record()

        # We can't update files in a zip, so copy
        self.archive.close()
        os.rename(self.path, "%s.tmp" % self.path)
        src = zipfile.ZipFile("%s.tmp" % self.path, "r")

        self.archive = zipfile.ZipFile(self.path, "w", zipfile.ZIP_DEFLATED)

        for n in src.namelist():
            if n in files:
                continue
            self.archive.writestr(n, src.read(n))
        os.remove("%s.tmp" % self.path)
        return super(ZippedPill, self).record()

    def stop(self):
        super(ZippedPill, self).stop()
        if self.archive:
            self.archive.close()

    def save_response(self, service, operation, response_data, http_response=200):

        filepath = self.get_new_file_path(service, operation)
        pill.LOG.debug("save_response: path=%s", filepath)
        json_data = {"status_code": http_response, "data": response_data}
        self.archive.writestr(
            filepath,
            json.dumps(json_data, indent=4, default=pill.serialize),
            zipfile.ZIP_DEFLATED,
        )
        self._files.add(filepath)

    def load_response(self, service, operation):
        response_file = self.get_next_file_path(service, operation)
        self._used.add(response_file)
        pill.LOG.debug("load_responses: %s", response_file)
        response_data = json.loads(
            self.archive.read(response_file), object_hook=pill.deserialize
        )
        return (
            pill.FakeHttpResponse(response_data["status_code"]), response_data["data"]
        )

    def get_new_file_path(self, service, operation):
        base_name = "{0}.{1}".format(service, operation)
        if self.prefix:
            base_name = "{0}.{1}".format(self.prefix, base_name)
        pill.LOG.debug("get_new_file_path: %s", base_name)
        index = 0
        glob_pattern = os.path.join(self._data_path, base_name + "*")

        for file_path in fnmatch.filter(self._files, glob_pattern):
            file_name = os.path.basename(file_path)
            m = self.filename_re.match(file_name)
            if m:
                i = int(m.group("index"))
                if i > index:
                    index = i
        index += 1
        return os.path.join(self._data_path, "{0}_{1}.json".format(base_name, index))

    def get_next_file_path(self, service, operation):
        base_name = "{0}.{1}".format(service, operation)
        if self.prefix:
            base_name = "{0}.{1}".format(self.prefix, base_name)
        pill.LOG.debug("get_next_file_path: %s", base_name)
        next_file = None
        while next_file is None:
            index = self._index.setdefault(base_name, 1)
            fn = os.path.join(self._data_path, base_name + "_{0}.json".format(index))
            fn = fn.replace('\\', '/')
            if fn in self._files:
                next_file = fn
                self._index[base_name] += 1
                self._files.add(fn)
            elif index != 1:
                self._index[base_name] = 1
            else:
                # we are looking for the first index and it's not here
                raise IOError("response file ({0}) not found".format(fn))
        return fn

def attach(session, data_path, prefix=None, debug=False):
    pill = ZippedPill(data_path, prefix=prefix, debug=debug)
    pill.attach(session, prefix)
    return pill

class PillTest(CustodianTestCore):

    archive_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "placebo_data.zip"
    )

    placebo_dir = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "data", "placebo"
    )

    output_dir = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "data", "output"
    )

    recording = False

    def cleanUp(self):
        self.pill = None

    def record_flight_data(self, test_case, zdata=False, augment=False):
        self.recording = True
        test_dir = os.path.join(self.placebo_dir, test_case)
        if not (zdata or augment):
            if os.path.exists(test_dir):
                shutil.rmtree(test_dir)
            os.makedirs(test_dir)

        session = boto3.Session()
        default_region = session.region_name
        if not zdata:
            pill = placebo.attach(session, test_dir)
        else:
            pill = attach(session, self.archive_path, test_case, debug=True)

        pill.record()
        self.pill = pill
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)

        class FakeFactory:

            def __call__(fake, region=None, assume=None):
                new_session = None
                # slightly experimental for test recording, using
                # cross account assumes, note this will record sts
                # assume role api calls creds into test data, they will
                # go stale, but its best to modify before commiting.
                # Disabled by default.
                if 0 and (assume is not False and fake.assume_role):
                    client = session.client('sts')
                    creds = client.assume_role(
                        RoleArn=fake.assume_role,
                        RoleSessionName='CustodianTest')['Credentials']
                    new_session = boto3.Session(
                        aws_access_key_id=creds['AccessKeyId'],
                        aws_secret_access_key=creds['SecretAccessKey'],
                        aws_session_token=creds['SessionToken'],
                        region_name=region or fake.region or default_region)
                elif region and region != default_region:
                    new_session = boto3.Session(region_name=region)

                if new_session:
                    assert not zdata
                    new_pill = placebo.attach(new_session, test_dir, debug=True)
                    new_pill.record()
                    self.addCleanup(new_pill.stop)
                    return new_session
                return session

        return FakeFactory()

    def replay_flight_data(self, test_case, zdata=False, region=None):
        """
        The `region` argument is to allow functional tests to override the
        default region. It is unused when replaying stored data.
        """

        if strtobool(os.environ.get('C7N_FUNCTIONAL', 'no')):
            self.recording = True
            return lambda region=region, assume=None: boto3.Session(region_name=region)

        if not zdata:
            test_dir = os.path.join(self.placebo_dir, test_case)
            if not os.path.exists(test_dir):
                raise RuntimeError("Invalid Test Dir for flight data %s" % test_dir)

        session = boto3.Session()
        if not zdata:
            pill = placebo.attach(session, test_dir)
            # pill = BluePill()
            # pill.attach(session, test_dir)
        else:
            pill = attach(session, self.archive_path, test_case, False)

        pill.playback()
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)
        return lambda region=None, assume=None: session
<commit_msg>ci - aws test recording - auto anonymize and slim (#5561)<commit_after># Copyright 2016-2017 Capital One Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import fnmatch
import json
import os
import shutil
import zipfile
import re
from datetime import datetime, timedelta, tzinfo
from distutils.util import strtobool

import boto3
import placebo
from botocore.response import StreamingBody
from placebo import pill
from six import StringIO

from c7n.testing import CustodianTestCore

###########################################################################
# BEGIN PLACEBO MONKEY PATCH
#
# Placebo is effectively abandoned upstream, since mitch went back to work at AWS, irony...
# These monkeypatch patches represent fixes on trunk of that repo that have not been released
# into an extant version, we carry them here. We can drop this when this issue is resolved
#
# https://github.com/garnaat/placebo/issues/63
#
# License - Apache 2.0
# Copyright (c) 2015 Mitch Garnaat

class UTC(tzinfo):
    """UTC"""

    def utcoffset(self, dt):
        return timedelta(0)

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return timedelta(0)

utc = UTC()

def deserialize(obj):
    """Convert JSON dicts back into objects."""
    # Be careful of shallow copy here
    target = dict(obj)
    class_name = None
    if "__class__" in target:
        class_name = target.pop("__class__")
    if "__module__" in obj:
        obj.pop("__module__")
    # Use getattr(module, class_name) for custom types if needed
    if class_name == "datetime":
        return datetime(tzinfo=utc, **target)
    if class_name == "StreamingBody":
        return StringIO(target["body"])
    # Return unrecognized structures as-is
    return obj

def serialize(obj):
    """Convert objects into JSON structures."""
    # Record class and module information for deserialization
    result = {"__class__": obj.__class__.__name__}
    try:
        result["__module__"] = obj.__module__
    except AttributeError:
        pass

    # Convert objects to dictionary representation based on type
    if isinstance(obj, datetime):
        result["year"] = obj.year
        result["month"] = obj.month
        result["day"] = obj.day
        result["hour"] = obj.hour
        result["minute"] = obj.minute
        result["second"] = obj.second
        result["microsecond"] = obj.microsecond
        return result
    if isinstance(obj, StreamingBody):
        result["body"] = obj.read()
        obj._raw_stream = StringIO(result["body"])
        obj._amount_read = 0
        return result
    if isinstance(obj, bytes):
        return obj.decode('utf8')

    # Raise a TypeError if the object isn't recognized
    raise TypeError("Type not serializable")

pill.FakeHttpResponse.raw = None
placebo.pill.serialize = serialize
placebo.pill.deserialize = deserialize

# END PLACEBO MONKEY
##########################################################################

class BluePill(pill.Pill):

    def playback(self):
        super(BluePill, self).playback()
        self._avail = self.get_available()

    def get_available(self):
        return set(
            [
                os.path.join(self.data_path, n)
                for n in fnmatch.filter(os.listdir(self.data_path), "*.json")
            ]
        )

    def get_next_file_path(self, service, operation):
        fn = super(BluePill, self).get_next_file_path(service, operation)
        # couple of double use cases
        if fn in self._avail:
            self._avail.remove(fn)
        else:
            print("\ndouble use %s\n" % fn)
        return fn

    def stop(self):
        result = super(BluePill, self).stop()
        if self._avail:
            print("Unused json files \n %s" % ("\n".join(sorted(self._avail))))
        return result

class ZippedPill(pill.Pill):

    def __init__(self, path, prefix=None, debug=False):
        super(ZippedPill, self).__init__(prefix, debug)
        self.path = path
        self._used = set()
        self.archive = None

    def playback(self):
        self.archive = zipfile.ZipFile(self.path, "r")
        self._files = set(self.archive.namelist())
        return super(ZippedPill, self).playback()

    def record(self):
        self.archive = zipfile.ZipFile(self.path, "a", zipfile.ZIP_DEFLATED)
        self._files = set()

        files = set([n for n in self.archive.namelist() if n.startswith(self.prefix)])

        if not files:
            return super(ZippedPill, self).record()

        # We can't update files in a zip, so copy
        self.archive.close()
        os.rename(self.path, "%s.tmp" % self.path)
        src = zipfile.ZipFile("%s.tmp" % self.path, "r")

        self.archive = zipfile.ZipFile(self.path, "w", zipfile.ZIP_DEFLATED)

        for n in src.namelist():
            if n in files:
                continue
            self.archive.writestr(n, src.read(n))
        os.remove("%s.tmp" % self.path)
        return super(ZippedPill, self).record()

    def stop(self):
        super(ZippedPill, self).stop()
        if self.archive:
            self.archive.close()

    def save_response(self, service, operation, response_data, http_response=200):

        filepath = self.get_new_file_path(service, operation)
        pill.LOG.debug("save_response: path=%s", filepath)
        json_data = {"status_code": http_response, "data": response_data}
        self.archive.writestr(
            filepath,
            json.dumps(json_data, indent=4, default=pill.serialize),
            zipfile.ZIP_DEFLATED,
        )
        self._files.add(filepath)

    def load_response(self, service, operation):
        response_file = self.get_next_file_path(service, operation)
        self._used.add(response_file)
        pill.LOG.debug("load_responses: %s", response_file)
        response_data = json.loads(
            self.archive.read(response_file), object_hook=pill.deserialize
        )
        return (
            pill.FakeHttpResponse(response_data["status_code"]), response_data["data"]
        )

    def get_new_file_path(self, service, operation):
        base_name = "{0}.{1}".format(service, operation)
        if self.prefix:
            base_name = "{0}.{1}".format(self.prefix, base_name)
        pill.LOG.debug("get_new_file_path: %s", base_name)
        index = 0
        glob_pattern = os.path.join(self._data_path, base_name + "*")

        for file_path in fnmatch.filter(self._files, glob_pattern):
            file_name = os.path.basename(file_path)
            m = self.filename_re.match(file_name)
            if m:
                i = int(m.group("index"))
                if i > index:
                    index = i
        index += 1
        return os.path.join(self._data_path, "{0}_{1}.json".format(base_name, index))

    def get_next_file_path(self, service, operation):
        base_name = "{0}.{1}".format(service, operation)
        if self.prefix:
            base_name = "{0}.{1}".format(self.prefix, base_name)
        pill.LOG.debug("get_next_file_path: %s", base_name)
        next_file = None
        while next_file is None:
            index = self._index.setdefault(base_name, 1)
            fn = os.path.join(self._data_path, base_name + "_{0}.json".format(index))
            fn = fn.replace('\\', '/')
            if fn in self._files:
                next_file = fn
                self._index[base_name] += 1
                self._files.add(fn)
            elif index != 1:
                self._index[base_name] = 1
            else:
                # we are looking for the first index and it's not here
                raise IOError("response file ({0}) not found".format(fn))
        return fn

def attach(session, data_path, prefix=None, debug=False):
    pill = ZippedPill(data_path, prefix=prefix, debug=debug)
    pill.attach(session, prefix)
    return pill

class RedPill(pill.Pill):
    def datetime_converter(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()

    def save_response(self, service, operation, response_data,
                    http_response=200):
        """
        Override to sanitize response metadata and account_ids
        """
        if 'ResponseMetadata' in response_data:
            response_data['ResponseMetadata'] = {}

        response_data = json.dumps(response_data, default=self.datetime_converter)
        response_data = re.sub("\d{12}", "123456789123", response_data)  # noqa
        response_data = json.loads(response_data)

        super(RedPill, self).save_response(service, operation, response_data,
                    http_response)

class PillTest(CustodianTestCore):

    archive_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "placebo_data.zip"
    )

    placebo_dir = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "data", "placebo"
    )

    output_dir = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "data", "output"
    )

    recording = False

    def cleanUp(self):
        self.pill = None

    def record_flight_data(self, test_case, zdata=False, augment=False):
        self.recording = True
        test_dir = os.path.join(self.placebo_dir, test_case)
        if not (zdata or augment):
            if os.path.exists(test_dir):
                shutil.rmtree(test_dir)
            os.makedirs(test_dir)

        session = boto3.Session()
        default_region = session.region_name
        if not zdata:
            pill = RedPill()
            pill.attach(session, test_dir)
        else:
            pill = attach(session, self.archive_path, test_case, debug=True)

        pill.record()
        self.pill = pill
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)

        class FakeFactory:

            def __call__(fake, region=None, assume=None):
                new_session = None
                # slightly experimental for test recording, using
                # cross account assumes, note this will record sts
                # assume role api calls creds into test data, they will
                # go stale, but its best to modify before commiting.
                # Disabled by default.
                if 0 and (assume is not False and fake.assume_role):
                    client = session.client('sts')
                    creds = client.assume_role(
                        RoleArn=fake.assume_role,
                        RoleSessionName='CustodianTest')['Credentials']
                    new_session = boto3.Session(
                        aws_access_key_id=creds['AccessKeyId'],
                        aws_secret_access_key=creds['SecretAccessKey'],
                        aws_session_token=creds['SessionToken'],
                        region_name=region or fake.region or default_region)
                elif region and region != default_region:
                    new_session = boto3.Session(region_name=region)

                if new_session:
                    assert not zdata
                    new_pill = placebo.attach(new_session, test_dir, debug=True)
                    new_pill.record()
                    self.addCleanup(new_pill.stop)
                    return new_session
                return session

        return FakeFactory()

    def replay_flight_data(self, test_case, zdata=False, region=None):
        """
        The `region` argument is to allow functional tests to override the
        default region. It is unused when replaying stored data.
        """

        if strtobool(os.environ.get('C7N_FUNCTIONAL', 'no')):
            self.recording = True
            return lambda region=region, assume=None: boto3.Session(region_name=region)

        if not zdata:
            test_dir = os.path.join(self.placebo_dir, test_case)
            if not os.path.exists(test_dir):
                raise RuntimeError("Invalid Test Dir for flight data %s" % test_dir)

        session = boto3.Session()
        if not zdata:
            pill = placebo.attach(session, test_dir)
            # pill = BluePill()
            # pill.attach(session, test_dir)
        else:
            pill = attach(session, self.archive_path, test_case, False)

        pill.playback()
        self.addCleanup(pill.stop)
        self.addCleanup(self.cleanUp)
        return lambda region=None, assume=None: session
<|endoftext|>


<commit_before>import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'edurepo.settings')

import sys
sys.path.append('.')

import datetime
from django.contrib.auth.models import User
from teachers.models import Entry, Teacher, TeacherClass

def process():
    teacher_data = ('ms.teacher@example.edu', 'testuser', 'Ms. Teacher', 'AL-G5-SCIENCE', '5th grade science',
                    ['AL-G5-SCIENCE-05A', 'AL-G5-SCIENCE-05A', 'AL-G5-SCIENCE-05B', 'AL-G5-SCIENCE-05D',
                     'AL-G5-SCIENCE-05D'])
    users = User.objects.filter(username=teacher_data[1])
    if users:
        u = users[0]
    else:
        print '%s does not exist, creating...' % teacher_data[1]
        u = User.objects.create_user(teacher_data[1], email=teacher_data[0])
    Teacher.objects.filter(email=teacher_data[0]).delete()
    t = Teacher(email=teacher_data[0], user=u, name=teacher_data[2])
    t.save()
    c = TeacherClass(teacher=t, course_id=teacher_data[3], name=teacher_data[4])
    c.save()

    today = datetime.date.today()
    cur_day = today - datetime.timedelta(days=today.weekday())
    for objective in teacher_data[5]:
        e = Entry(teacher=t, teacher_class=c, date=cur_day, objective=objective)
        e.save()
        cur_day += datetime.timedelta(days=1)

process()
<commit_msg>store base API URL in TeacherClass<commit_after>import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'edurepo.settings')

import sys
sys.path.append('.')

import datetime
from django.contrib.auth.models import User
from teachers.models import Entry, Teacher, TeacherClass

def process(args):
    if len(args) != 1:
        print >> sys.stderr, "one arg required: Base API URL"
        sys.exit(1)

    base_api_url = args[0]

    teacher_data = ('ms.teacher@example.edu', 'testuser', 'Ms. Teacher', 'AL-G5-SCIENCE', '5th grade science',
                    ['AL-G5-SCIENCE-05A', 'AL-G5-SCIENCE-05A', 'AL-G5-SCIENCE-05B', 'AL-G5-SCIENCE-05D',
                     'AL-G5-SCIENCE-05D'])
    users = User.objects.filter(username=teacher_data[1])
    if users:
        u = users[0]
    else:
        print '%s does not exist, creating...' % teacher_data[1]
        u = User.objects.create_user(teacher_data[1], email=teacher_data[0])
    Teacher.objects.filter(email=teacher_data[0]).delete()
    t = Teacher(email=teacher_data[0], user=u, name=teacher_data[2])
    t.save()
    c = TeacherClass(teacher=t, course_id=teacher_data[3], name=teacher_data[4],
                     repo_provider=base_api_url)
    c.save()

    today = datetime.date.today()
    cur_day = today - datetime.timedelta(days=today.weekday())
    for objective in teacher_data[5]:
        e = Entry(teacher=t, teacher_class=c, date=cur_day, objective=objective)
        e.save()
        cur_day += datetime.timedelta(days=1)

process(sys.argv[1:])
<|endoftext|>


<commit_before>import csv
from dataclasses import dataclass
import io
from ekklesia_common import md
from ekklesia_portal.datamodel import Proposition
from eliot import log_call, start_task, log_message

@dataclass
class TableRowOptionalFields:
    content: bool = True
    motivation: bool = True
    submitters: bool = False
    tags: bool = True

@log_call
def proposition_to_table_row(proposition: Proposition, origin: str,
        optional_fields: TableRowOptionalFields):

    row = {
        "Identifier": proposition.voting_identifier,
        "Title": proposition.title,
        "Origin": origin,
        "Category": proposition.ballot.proposition_type.name,
        "Motion block": proposition.ballot.name
    }

    if optional_fields.content:
        row["Text"] = md.convert(proposition.content)

    if optional_fields.motivation:
        row["Reason"] = md.convert(proposition.motivation)

    if optional_fields.tags:
        row["Tags"] = ",".join(t.name for t in proposition.tags)

    if optional_fields.submitters:
        submitter_names = [pm.member.name for pm in proposition.propositions_member if pm.submitter]
        row["Submitters"] = ",".join(submitter_names)

    return row

def propositions_to_csv(propositions, origin="", delimiter=";", optional_fields: TableRowOptionalFields=TableRowOptionalFields()):
    proposition_rows = [proposition_to_table_row(p, origin, optional_fields) for p in propositions]
    csv_out = io.StringIO()
    fieldnames = ['Identifier', 'Submitters', 'Title', 'Text', 'Reason', 'Category', 'Tags', 'Motion block', 'Origin']

    writer = csv.DictWriter(csv_out, fieldnames, delimiter=delimiter)
    writer.writeheader()
    writer.writerows(proposition_rows)

    return csv_out.getvalue()
<commit_msg>Quote all fields in CSV to avoid issues on import<commit_after>import csv
from dataclasses import dataclass
import io
from ekklesia_common import md
from ekklesia_portal.datamodel import Proposition
from eliot import log_call, start_task, log_message

@dataclass
class TableRowOptionalFields:
    content: bool = True
    motivation: bool = True
    submitters: bool = False
    tags: bool = True

@log_call
def proposition_to_table_row(proposition: Proposition, origin: str,
        optional_fields: TableRowOptionalFields):

    row = {
        "Identifier": proposition.voting_identifier,
        "Title": proposition.title,
        "Origin": origin,
        "Category": proposition.ballot.proposition_type.name,
        "Motion block": proposition.ballot.name
    }

    if optional_fields.content:
        row["Text"] = md.convert(proposition.content)

    if optional_fields.motivation:
        row["Reason"] = md.convert(proposition.motivation)

    if optional_fields.tags:
        row["Tags"] = ",".join(t.name for t in proposition.tags)

    if optional_fields.submitters:
        submitter_names = [pm.member.name for pm in proposition.propositions_member if pm.submitter]
        row["Submitters"] = ",".join(submitter_names)

    return row

def propositions_to_csv(propositions, origin="", delimiter=";", optional_fields: TableRowOptionalFields=TableRowOptionalFields()):
    proposition_rows = [proposition_to_table_row(p, origin, optional_fields) for p in propositions]
    csv_out = io.StringIO()
    fieldnames = ['Identifier', 'Submitters', 'Title', 'Text', 'Reason', 'Category', 'Tags', 'Motion block', 'Origin']

    writer = csv.DictWriter(csv_out, fieldnames, delimiter=delimiter, quoting=csv.QUOTE_ALL)
    writer.writeheader()
    writer.writerows(proposition_rows)

    return csv_out.getvalue()
<|endoftext|>


<commit_before>#!/usr/bin/env python
<commit_msg>Removed another unnecessary file.<commit_after><|endoftext|>


<commit_before>#!/usr/bin/env python
# Author: Donald Merand for Explo (http://www.explo.org)
# Some bits are from Adafruit (see README)

import logging, time, subprocess, os
import toml

import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855

# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
  return c * 9.0 / 5.0 + 32.0

# Grab config information from the log file (TOML format)
with open("/home/pi/raspberry-pi-temperature-monitor/config.toml") as conffile:
  conf = toml.loads(conffile.read())

# Set up logging if applicable
LOGFORMAT = "%(asctime)s\t%(message)s"
if conf.get('log_file'):
  if conf.get('log_level') == 'DEBUG':
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.DEBUG)
  else:
    # Default to INFO-level logging
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.INFO)
  print("Logging to {0}".format(conf['log_file']))
else:
  # Just log to STDOUT
  logging.basicConfig(level=logging.INFO, format=LOGFORMAT)

# Set log file rotate size
if conf.get('log_file_rotate_size'):
  log_file_rotate_size = conf['log_file_rotate_size']
else:
  log_file_rotate_size = 10000000 #10MB

# Set device name
if conf.get('device_name'):
  device_name = conf['device_name']
else:
  device_name = "Unknown Device"

# Set up the temperature poll rate
if conf.get('poll_rate'):
  poll_rate = conf['poll_rate']
else:
  # Default to 10 seconds if none is set
  poll_rate = 10

# Set our temperature sensor based on the desired connection method
if conf.get('pi').get('connect_method') == "HARDWARE":
  sensor = MAX31855.MAX31855(spi=SPI.SpiDev(conf['pi']['SPI_PORT'], conf['pi']['SPI_DEVICE']))
else:
  # Default to software SPI
  sensor = MAX31855.MAX31855(conf['pi']['CLK'], conf['pi']['CS'], conf['pi']['DO'])

# Grab our (wireless) IP address so that we can send a link to the webcam if
# the temp is too high/low
ip_command = "ifconfig | awk 'BEGIN{ORS=\"\"}/wlan0/{getline;gsub(/addr:/,\"\",$2);print $2}'"
my_ip = str(subprocess.check_output(ip_command, shell=True))

# We sometimes get false freezing measurements on startup. So we double-check
# for freezing temps
might_be_freezing = None

# Loop measurements every second.
while True:
  # Rotate log file
  if conf.get('log_file'):
    log_size = os.stat(conf.get('log_file')).st_size

    if log_size > log_file_rotate_size:
      # Store log in hipchat
      if conf.get('hipchat').get('token') and conf.get('hipchat').get('room'):
        subprocess.call(['/usr/local/bin/hipfile', "--token", conf['hipchat']['token'], "--room", conf['hipchat']['room'], "--path", conf.get('log_file'), "--message", "{0} Temperature Log".format(device_name)])
      # Empty log file
      with open(conf.get('log_file'), 'w'):
        pass

  # now do the temperature reading
  temp = sensor.readTempC()
  #internal = sensor.readInternalC()
  logging.info("{0:0.3F}*C\t{1:0.3F}*F".format(temp, c_to_f(temp)))

  if temp > conf['temp']['max']:
    warning_text = "{0} WARNING: {1} is hot, potential fire risk: {2:0.3F}*F. Webcam: http://{3}:8081".format(conf.get('hipchat').get('notify_names'), device_name, c_to_f(temp), my_ip)
    logging.warning(warning_text)
    if conf.get('hipchat').get('room'):
      subprocess.call(["/usr/local/bin/hipchat", "-r", conf['hipchat']['room'], warning_text])
    time.sleep(10) # wait 10 seconds
  if temp < conf['temp']['min']:
    if might_be_freezing:
      might_be_freezing = None
      warning_text = "{0} WARNING: {1} is being exposed to freezing temperatures: {2:0.3F}*F. Webcam: http://{3}:8081".format(conf.get('hipchat').get('notify_names'), device_name, c_to_f(temp), my_ip)
      logging.warning(warning_text)
      if conf.get('hipchat').get('room'):
        subprocess.call(["/usr/local/bin/hipchat", "-r", conf['hipchat']['room'], warning_text])
    else:
      might_be_freezing = True
    time.sleep(60 * 5) # wait 5 more minutes to be sure
  else:
    # Temperature is in normal range, wait regular polling amount + try again
    time.sleep(poll_rate)
<commit_msg>move param checking and log rotating into routines<commit_after>#!/usr/bin/env python
# Author: Donald Merand for Explo (http://www.explo.org)
# Some bits are from Adafruit (see README)

import logging, time, subprocess, os
import toml

import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855

# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
  return c * 9.0 / 5.0 + 32.0

# Grab config information from the log file (TOML format)
with open("/home/pi/raspberry-pi-temperature-monitor/config.toml") as conffile:
  conf = toml.loads(conffile.read())

# Check for a parameter + use it, otherwise set a default value
# Returns a string value, typically used like:
#   some_param = config_check('some_param', 'default_value')
def config_check(param, default):
  if conf.get(param):
    return conf.get(param)
  else:
    return default

# Set up logging if applicable
LOGFORMAT = "%(asctime)s\t%(message)s"
if conf.get('log_file'):
  log_file_rotate_size = config_check('log_file_rotate_size', 10000000)
  if conf.get('log_level') == 'DEBUG':
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.DEBUG)
  else:
    # Default to INFO-level logging
    logging.basicConfig(filename=conf['log_file'], format=LOGFORMAT, level=logging.INFO)
  print("Logging to {0}".format(conf['log_file']))
else:
  # Just log to STDOUT
  logging.basicConfig(level=logging.INFO, format=LOGFORMAT)

# Check if the log size is greater than our max allowable size.
# Send it to HipChat if it's too big, and clear it out
def rotate_log_file():
  if conf.get('log_file'):
    log_size = os.stat(conf.get('log_file')).st_size

    if log_size > log_file_rotate_size:
      # Store log in hipchat
      if conf.get('hipchat').get('token') and conf.get('hipchat').get('room'):
        subprocess.call(['/usr/local/bin/hipfile', "--token", conf['hipchat']['token'], "--room", conf['hipchat']['room'], "--path", conf.get('log_file'), "--message", "{0} Temperature Log".format(device_name)])
      # Empty log file
      with open(conf.get('log_file'), 'w'):
        pass

# Set some default values
device_name = config_check('device_name', 'Unknown Device')
# Make sure the temperature has been out of range since the last period, to
# avoid spurious one-off temperature readings causing broadcasts
last_temp = None
poll_rate = config_check('poll_rate', 10)
# Set our temperature sensor based on the desired connection method
if conf.get('pi').get('connect_method') == "HARDWARE":
  sensor = MAX31855.MAX31855(spi=SPI.SpiDev(conf['pi']['SPI_PORT'], conf['pi']['SPI_DEVICE']))
else:
  # Default to software SPI
  sensor = MAX31855.MAX31855(conf['pi']['CLK'], conf['pi']['CS'], conf['pi']['DO'])

# Grab our (wireless) IP address so that we can send a link to the webcam if
# the temp is too high/low
ip_command = "ifconfig | awk 'BEGIN{ORS=\"\"}/wlan0/{getline;gsub(/addr:/,\"\",$2);print $2}'"
my_ip = str(subprocess.check_output(ip_command, shell=True))

# Loop measurements every second.
while True:
  rotate_log_file

  # now do the temperature reading
  temp = sensor.readTempC()
  #internal = sensor.readInternalC()
  logging.info("{0:0.3F}*C\t{1:0.3F}*F".format(temp, c_to_f(temp)))

  if temp > conf['temp']['max']:
    if last_temp > conf['temp']['max']:
      warning_text = "{0} WARNING: {1} is hot, potential fire risk: {2:0.3F}*F. Webcam: http://{3}:8081".format(conf.get('hipchat').get('notify_names'), device_name, c_to_f(temp), my_ip)
      logging.warning(warning_text)
      if conf.get('hipchat').get('room'):
        subprocess.call(["/usr/local/bin/hipchat", "-r", conf['hipchat']['room'], warning_text])
    time.sleep(30) #seconds
  if temp < conf['temp']['min'] :
    if last_temp < conf['temp']['min']:
      warning_text = "{0} WARNING: {1} is being exposed to freezing temperatures: {2:0.3F}*F. Webcam: http://{3}:8081".format(conf.get('hipchat').get('notify_names'), device_name, c_to_f(temp), my_ip)
      logging.warning(warning_text)
      if conf.get('hipchat').get('room'):
        subprocess.call(["/usr/local/bin/hipchat", "-r", conf['hipchat']['room'], warning_text])
    time.sleep(60 * 5) # wait 5 more minutes in this case to be sure
  else:
    # Temperature is in normal range, store it, wait regular polling amount + try again
    last_temp = temp
    time.sleep(poll_rate)
<|endoftext|>


<commit_before>__version__ = "8.1.3"
__release__ = True
<commit_msg>Set version to v8.1.4 (#787)<commit_after>__version__ = "8.1.4"
__release__ = True
<|endoftext|>


<commit_before># inspired from:

# https://python-packaging-user-guide.readthedocs.org/en/latest/single_source_version/
# https://github.com/pypa/warehouse/blob/master/warehouse/__about__.py

__name__ = "thinc"
__version__ = "7.3.0"
__summary__ = "Practical Machine Learning for NLP"
__uri__ = "https://github.com/explosion/thinc"
__author__ = "Matthew Honnibal"
__email__ = "matt@explosion.ai"
__license__ = "MIT"
__title__ = "thinc"
__release__ = True
<commit_msg>Set version to 7.3.0.dev0<commit_after># inspired from:

# https://python-packaging-user-guide.readthedocs.org/en/latest/single_source_version/
# https://github.com/pypa/warehouse/blob/master/warehouse/__about__.py

__name__ = "thinc"
__version__ = "7.3.0.dev0"
__summary__ = "Practical Machine Learning for NLP"
__uri__ = "https://github.com/explosion/thinc"
__author__ = "Matthew Honnibal"
__email__ = "matt@explosion.ai"
__license__ = "MIT"
__title__ = "thinc"
__release__ = True
<|endoftext|>


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

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class TiebaItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

class ThreadItem(scrapy.Item):
    url = scrapy.Field()
    title = scrapy.Field()
    preview = scrapy.Field()
    author = scrapy.Field()
    tieba = scrapy.Field()
    date = scrapy.Field()
<commit_msg>add NoneItem, request in pipeline considered<commit_after># -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class TiebaItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

class ThreadItem(scrapy.Item):
    url = scrapy.Field()
    title = scrapy.Field()
    preview = scrapy.Field()
    author = scrapy.Field()
    tieba = scrapy.Field()
    date = scrapy.Field()

class NoneItem(scrapy.Item):
<|endoftext|>


<commit_before>#!/usr/bin/env python
from __future__ import print_function

import argparse
import os
import sys
import subprocess
import re

parser = argparse.ArgumentParser(description="""Extract version info from git and
generate a version header file. The working directory is expected to be
the root of Firmware.""")
parser.add_argument('filename', metavar='version.h', help='Header output file')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
                    help='Verbose output', default=False)
parser.add_argument('--validate', dest='validate', action='store_true',
                    help='Validate the tag format', default=False)

args = parser.parse_args()
filename = args.filename
verbose = args.verbose
validate = args.validate

try:
    fp_header = open(filename, 'r')
    old_header = fp_header.read()
except:
    old_header = ''

# Generate the header file content
header = """
/* Auto Magically Generated file */
/* Do not edit! */
#pragma once
"""

# PX4
git_tag = subprocess.check_output('git describe --always --tags --dirty'.split(),
                                  stderr=subprocess.STDOUT).decode('utf-8').strip()
if validate:
    if verbose:
        print("testing git tag: "+git_tag)
    # remove optional '-dirty' at the end
    git_tag_test = re.sub(r'-dirty$', '', git_tag)
    # remove optional -<num_commits>-g<commit_hash> at the end (in case we are not on a tagged commit)
    git_tag_test = re.sub(r'-[0-9]+-g[0-9a-fA-F]+$', '', git_tag_test)
    # now check the version format
    m = re.match(r'v([0-9]+)\.([0-9]+)\.[0-9]+(rc[0-9]+)?(-[0-9]+\.[0-9]+\.[0-9]+)?$', git_tag_test)
    if m:
        # format matches, check the major and minor numbers
        major = int(m.group(1))
        minor = int(m.group(2))
        if major < 1 or (major == 1 and minor < 9):
            print("")
            print("Error: PX4 version too low, expected at least v1.9.0")
            print("Check the git tag (current tag: '{:}')".format(git_tag_test))
            print("")
            sys.exit(1)
    else:
        print("")
        print("Error: the git tag '{:}' does not match the expected format.".format(git_tag_test))
        print("")
        print("The expected format is 'v<PX4 version>[-<custom version>]'")
        print("  <PX4 version>: v<major>.<minor>.<patch>[rc<rc>]")
        print("  <custom version>: <major>.<minor>.<patch>")
        print("Examples:")
        print("  v1.9.0rc3")
        print("  v1.9.0-1.0.0")
        print("See also https://dev.px4.io/master/en/setup/building_px4.html#firmware_version")
        print("")
        sys.exit(1)

git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      stderr=subprocess.STDOUT).decode('utf-8').strip()
try:
    git_branch_name = subprocess.check_output('git symbolic-ref -q --short HEAD'.split(),
                                          stderr=subprocess.STDOUT).decode('utf-8').strip()
except:
    git_branch_name = ''
git_version_short = git_version[0:16]

header += """
#define PX4_GIT_VERSION_STR  "{git_version}"
#define PX4_GIT_VERSION_BINARY 0x{git_version_short}
#define PX4_GIT_TAG_STR  "{git_tag}"
#define PX4_GIT_BRANCH_NAME  "{git_branch_name}"
""".format(git_tag=git_tag,
           git_version=git_version,
           git_version_short=git_version_short,
           git_branch_name=git_branch_name)

# ECL
if (os.path.exists('src/lib/ecl/.git')):
    ecl_git_tag = subprocess.check_output('git describe --always --tags --dirty'.split(),
                                  cwd='src/lib/ecl', stderr=subprocess.STDOUT).decode('utf-8')

    ecl_git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      cwd='src/lib/ecl', stderr=subprocess.STDOUT).decode('utf-8').strip()
    ecl_git_version_short = ecl_git_version[0:16]

    header += """
#define ECL_LIB_GIT_VERSION_STR  "{ecl_git_version}"
#define ECL_LIB_GIT_VERSION_BINARY 0x{ecl_git_version_short}
""".format(ecl_git_version=ecl_git_version,
           ecl_git_version_short=ecl_git_version_short)

# Mavlink
if (os.path.exists('mavlink/include/mavlink/v2.0/.git')):
    mavlink_git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      cwd='mavlink/include/mavlink/v2.0', stderr=subprocess.STDOUT).decode('utf-8').strip()
    mavlink_git_version_short = mavlink_git_version[0:16]

    header += """
#define MAVLINK_LIB_GIT_VERSION_STR  "{mavlink_git_version}"
#define MAVLINK_LIB_GIT_VERSION_BINARY 0x{mavlink_git_version_short}
""".format(mavlink_git_version=mavlink_git_version,
           mavlink_git_version_short=mavlink_git_version_short)

# NuttX
if (os.path.exists('platforms/nuttx/NuttX/nuttx/.git')):
    nuttx_git_tag = subprocess.check_output('git describe --always --tags --match nuttx-*  --dirty'.split(),
                                  cwd='platforms/nuttx/NuttX/nuttx', stderr=subprocess.STDOUT).decode('utf-8').strip().replace("nuttx-","v")
    nuttx_git_tag = re.sub('-.*','.0',nuttx_git_tag)
    nuttx_git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      cwd='platforms/nuttx/NuttX/nuttx', stderr=subprocess.STDOUT).decode('utf-8').strip()
    nuttx_git_version_short = nuttx_git_version[0:16]

    header += """
#define NUTTX_GIT_VERSION_STR  "{nuttx_git_version}"
#define NUTTX_GIT_VERSION_BINARY 0x{nuttx_git_version_short}
#define NUTTX_GIT_TAG_STR  "{nuttx_git_tag}"
""".format(nuttx_git_version=nuttx_git_version,
           nuttx_git_version_short=nuttx_git_version_short,
           nuttx_git_tag=nuttx_git_tag)

if old_header != header:
    if verbose:
        print('Updating header {}'.format(filename))
    fp_header = open(filename, 'w')
    fp_header.write(header)
<commit_msg>px_update_git_header.py: allow -beta/-rc in tag<commit_after>#!/usr/bin/env python
from __future__ import print_function

import argparse
import os
import sys
import subprocess
import re

parser = argparse.ArgumentParser(description="""Extract version info from git and
generate a version header file. The working directory is expected to be
the root of Firmware.""")
parser.add_argument('filename', metavar='version.h', help='Header output file')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
                    help='Verbose output', default=False)
parser.add_argument('--validate', dest='validate', action='store_true',
                    help='Validate the tag format', default=False)

args = parser.parse_args()
filename = args.filename
verbose = args.verbose
validate = args.validate

try:
    fp_header = open(filename, 'r')
    old_header = fp_header.read()
except:
    old_header = ''

# Generate the header file content
header = """
/* Auto Magically Generated file */
/* Do not edit! */
#pragma once
"""

# PX4
git_tag = subprocess.check_output('git describe --always --tags --dirty'.split(),
                                  stderr=subprocess.STDOUT).decode('utf-8').strip()
if validate:
    if verbose:
        print("testing git tag: "+git_tag)
    # remove optional '-dirty' at the end
    git_tag_test = re.sub(r'-dirty$', '', git_tag)
    # remove optional -<num_commits>-g<commit_hash> at the end (in case we are not on a tagged commit)
    git_tag_test = re.sub(r'-[0-9]+-g[0-9a-fA-F]+$', '', git_tag_test)
    # now check the version format
    m = re.match(r'v([0-9]+)\.([0-9]+)\.[0-9]+([-]?rc[0-9]+)?(-beta[0-9]+)?(-[0-9]+\.[0-9]+\.[0-9]+)?$', git_tag_test)
    if m:
        # format matches, check the major and minor numbers
        major = int(m.group(1))
        minor = int(m.group(2))
        if major < 1 or (major == 1 and minor < 9):
            print("")
            print("Error: PX4 version too low, expected at least v1.9.0")
            print("Check the git tag (current tag: '{:}')".format(git_tag_test))
            print("")
            sys.exit(1)
    else:
        print("")
        print("Error: the git tag '{:}' does not match the expected format.".format(git_tag_test))
        print("")
        print("The expected format is 'v<PX4 version>[-<custom version>]'")
        print("  <PX4 version>: v<major>.<minor>.<patch>[-rc<rc>|-beta<beta>]")
        print("  <custom version>: <major>.<minor>.<patch>")
        print("Examples:")
        print("  v1.9.0rc3 (deprecated)")
        print("  v1.9.0-rc3 (preferred)")
        print("  v1.9.0-beta1")
        print("  v1.9.0-1.0.0")
        print("See also https://dev.px4.io/master/en/setup/building_px4.html#firmware_version")
        print("")
        sys.exit(1)

git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      stderr=subprocess.STDOUT).decode('utf-8').strip()
try:
    git_branch_name = subprocess.check_output('git symbolic-ref -q --short HEAD'.split(),
                                          stderr=subprocess.STDOUT).decode('utf-8').strip()
except:
    git_branch_name = ''
git_version_short = git_version[0:16]

header += """
#define PX4_GIT_VERSION_STR  "{git_version}"
#define PX4_GIT_VERSION_BINARY 0x{git_version_short}
#define PX4_GIT_TAG_STR  "{git_tag}"
#define PX4_GIT_BRANCH_NAME  "{git_branch_name}"
""".format(git_tag=git_tag,
           git_version=git_version,
           git_version_short=git_version_short,
           git_branch_name=git_branch_name)

# ECL
if (os.path.exists('src/lib/ecl/.git')):
    ecl_git_tag = subprocess.check_output('git describe --always --tags --dirty'.split(),
                                  cwd='src/lib/ecl', stderr=subprocess.STDOUT).decode('utf-8')

    ecl_git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      cwd='src/lib/ecl', stderr=subprocess.STDOUT).decode('utf-8').strip()
    ecl_git_version_short = ecl_git_version[0:16]

    header += """
#define ECL_LIB_GIT_VERSION_STR  "{ecl_git_version}"
#define ECL_LIB_GIT_VERSION_BINARY 0x{ecl_git_version_short}
""".format(ecl_git_version=ecl_git_version,
           ecl_git_version_short=ecl_git_version_short)

# Mavlink
if (os.path.exists('mavlink/include/mavlink/v2.0/.git')):
    mavlink_git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      cwd='mavlink/include/mavlink/v2.0', stderr=subprocess.STDOUT).decode('utf-8').strip()
    mavlink_git_version_short = mavlink_git_version[0:16]

    header += """
#define MAVLINK_LIB_GIT_VERSION_STR  "{mavlink_git_version}"
#define MAVLINK_LIB_GIT_VERSION_BINARY 0x{mavlink_git_version_short}
""".format(mavlink_git_version=mavlink_git_version,
           mavlink_git_version_short=mavlink_git_version_short)

# NuttX
if (os.path.exists('platforms/nuttx/NuttX/nuttx/.git')):
    nuttx_git_tag = subprocess.check_output('git describe --always --tags --match nuttx-*  --dirty'.split(),
                                  cwd='platforms/nuttx/NuttX/nuttx', stderr=subprocess.STDOUT).decode('utf-8').strip().replace("nuttx-","v")
    nuttx_git_tag = re.sub('-.*','.0',nuttx_git_tag)
    nuttx_git_version = subprocess.check_output('git rev-parse --verify HEAD'.split(),
                                      cwd='platforms/nuttx/NuttX/nuttx', stderr=subprocess.STDOUT).decode('utf-8').strip()
    nuttx_git_version_short = nuttx_git_version[0:16]

    header += """
#define NUTTX_GIT_VERSION_STR  "{nuttx_git_version}"
#define NUTTX_GIT_VERSION_BINARY 0x{nuttx_git_version_short}
#define NUTTX_GIT_TAG_STR  "{nuttx_git_tag}"
""".format(nuttx_git_version=nuttx_git_version,
           nuttx_git_version_short=nuttx_git_version_short,
           nuttx_git_tag=nuttx_git_tag)

if old_header != header:
    if verbose:
        print('Updating header {}'.format(filename))
    fp_header = open(filename, 'w')
    fp_header.write(header)
<|endoftext|>


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

import os
import subprocess
import sys

# TODO: release/debug

root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
build_dir = os.path.join(root, 'out')

def build():
  if sys.platform != "win32":
      cmd = 'make -C %s' % build_dir
  else:
      cmd = 'tools\win_build.bat'

  print cmd
  sys.exit(subprocess.call(cmd, shell=True))

def test():
  luvit = os.path.join(root, 'out', 'Debug', 'luvit')
  test_dir = os.path.join(root, 'tests')
  old_cwd = os.getcwd()
  os.chdir(test_dir)
  cmd = '%s runner.lua' % luvit
  print cmd
  rc = subprocess.call(cmd, shell=True)
  os.chdir(old_cwd)
  sys.exit(rc)

commands = {
  'build': build,
  'test': test,
}

if len(sys.argv) != 2:
  print('Usage: build.py [%s]' % ', '.join(commands.keys()))
  sys.exit(1)

ins = sys.argv[1]
if commands.has_key(ins):
  print('Running %s' % ins)
  cmd = commands.get(ins)
  cmd()
<commit_msg>add invalid command<commit_after>#!/usr/bin/env python

import os
import subprocess
import sys

# TODO: release/debug

root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
build_dir = os.path.join(root, 'out')

def build():
  if sys.platform != "win32":
      cmd = 'make -C %s' % build_dir
  else:
      cmd = 'tools\win_build.bat'

  print cmd
  sys.exit(subprocess.call(cmd, shell=True))

def test():
  luvit = os.path.join(root, 'out', 'Debug', 'luvit')
  test_dir = os.path.join(root, 'tests')
  old_cwd = os.getcwd()
  os.chdir(test_dir)
  cmd = '%s runner.lua' % luvit
  print cmd
  rc = subprocess.call(cmd, shell=True)
  os.chdir(old_cwd)
  sys.exit(rc)

commands = {
  'build': build,
  'test': test,
}

def usage():
  print('Usage: build.py [%s]' % ', '.join(commands.keys()))
  sys.exit(1)

if len(sys.argv) != 2:
  usage()

ins = sys.argv[1]
if not commands.has_key(ins):
  print('Invalid command: %s' % ins)
  sys.exit(1)

print('Running %s' % ins)
cmd = commands.get(ins)
cmd()

<|endoftext|>


<commit_before>#!/usr/bin/python

import platform
import subprocess
import os
import sys

def Main():
    platformid = platform.system()
    env = os.environ.copy()
    if platformid == "Linux":
        env["ORACLE_OCI_HOME"] = (
            "%s/Downloads/instantclient_11_2/sdk" % (os.path.expanduser("~")))
        env["ORACLE_OCCI_LIB_HOME"] = (
            "%s/Downloads/instantclient_11_2" % (os.path.expanduser("~")))
        args = ['make', '-j', '8']
        extensionlibrary = "out/Debug/lib.target/liboracledart_extension.so"
    elif platformid == "Darwin":
        ORACLE_OCCI_LIB_HOME = (
            "%s/Downloads/instantclient_11_2-2" % (os.path.expanduser("~")))
        subprocess.call(
            ["ln",
             "%s/libocci.dylib.11.1 %s/libocci.dylib" %
             (ORACLE_OCCI_LIB_HOME, ORACLE_OCCI_LIB_HOME)])
        subprocess.call(
            ["ln",
             "%s/ORACLE_OCCI_LIB_HOME/libclntsh.dylib.11.1 "
             "%s/libclntsh.dylib" %
             (ORACLE_OCCI_LIB_HOME, ORACLE_OCCI_LIB_HOME)])
        env["ORACLE_OCI_HOME"] = (
            "%s/Downloads/instantclient_11_2/sdk" % (os.path.expanduser("~")))
        env["ORACLE_OCCI_LIB_HOME"] = ORACLE_OCCI_LIB_HOME
        args = ['xcodebuild',
                '-project', 'oracledart.xcodeproj',
                '-configuration', 'ReleaseIA32',
                'SYMROOT=%s/../dart/xcodebuild' % os.getcwd()]
        extensionlibrary = (
            "../dart/xcodebuild/ReleaseIA32/liboracledart_extension.dylib")
    elif platformid == "Windows" or platformid == "Microsoft":
        if not "JAVA_HOME" in env:
            print "JAVA_HOME is not set up"
            return 3
        env["ORACLE_OCI_HOME"] = (
            "\%userprofile\%\\downloads\\instantclient-sdk-nt-12.1.0.1.0\\instantclient_12_1\\sdk")
        args = ['devenv',
                'oracledart.sln',
                '/build',
                'ReleaseIA32']
        extensionlibrary = (
            "..\\dart\\build\\ReleaseIA32\\oracledart_extension.dll")
    else:
        print "Unsupported platform"
        return 1

    print " ".join(args)
    process = subprocess.Popen(args, stdin=None, env=env)
    process.wait()
    print "Completed with %d" % (process.returncode)
    if process.returncode == 0:
        if platformid == "Linux" or platformid == "Darwin":
            subprocess.call("cp %s ." % (extensionlibrary), shell=True)
        else:
            subprocess.call(["copy", extensionlibrary, "."], shell=True)
    return process.returncode

if __name__ == '__main__':
    sys.exit(Main())
<commit_msg>Fixed userprofile reference on windows<commit_after>#!/usr/bin/python

import platform
import subprocess
import os
import sys

def Main():
    platformid = platform.system()
    env = os.environ.copy()
    if platformid == "Linux":
        env["ORACLE_OCI_HOME"] = (
            "%s/Downloads/instantclient_11_2/sdk" % (os.path.expanduser("~")))
        env["ORACLE_OCCI_LIB_HOME"] = (
            "%s/Downloads/instantclient_11_2" % (os.path.expanduser("~")))
        args = ['make', '-j', '8']
        extensionlibrary = "out/Debug/lib.target/liboracledart_extension.so"
    elif platformid == "Darwin":
        ORACLE_OCCI_LIB_HOME = (
            "%s/Downloads/instantclient_11_2-2" % (os.path.expanduser("~")))
        subprocess.call(
            ["ln",
             "%s/libocci.dylib.11.1 %s/libocci.dylib" %
             (ORACLE_OCCI_LIB_HOME, ORACLE_OCCI_LIB_HOME)])
        subprocess.call(
            ["ln",
             "%s/ORACLE_OCCI_LIB_HOME/libclntsh.dylib.11.1 "
             "%s/libclntsh.dylib" %
             (ORACLE_OCCI_LIB_HOME, ORACLE_OCCI_LIB_HOME)])
        env["ORACLE_OCI_HOME"] = (
            "%s/Downloads/instantclient_11_2/sdk" % (os.path.expanduser("~")))
        env["ORACLE_OCCI_LIB_HOME"] = ORACLE_OCCI_LIB_HOME
        args = ['xcodebuild',
                '-project', 'oracledart.xcodeproj',
                '-configuration', 'ReleaseIA32',
                'SYMROOT=%s/../dart/xcodebuild' % os.getcwd()]
        extensionlibrary = (
            "../dart/xcodebuild/ReleaseIA32/liboracledart_extension.dylib")
    elif platformid == "Windows" or platformid == "Microsoft":
        if not "JAVA_HOME" in env:
            print "JAVA_HOME is not set up"
            return 3
        env["ORACLE_OCI_HOME"] = (
            "%s\\downloads\\instantclient-sdk-nt-12.1.0.1.0\\instantclient_12_1\\sdk" %
            (os.path.expanduser("~")))
        args = ['devenv',
                'oracledart.sln',
                '/build',
                'ReleaseIA32']
        extensionlibrary = (
            "..\\dart\\build\\ReleaseIA32\\oracledart_extension.dll")
    else:
        print "Unsupported platform"
        return 1

    print " ".join(args)
    process = subprocess.Popen(args, stdin=None, env=env)
    process.wait()
    print "Completed with %d" % (process.returncode)
    if process.returncode == 0:
        if platformid == "Linux" or platformid == "Darwin":
            subprocess.call("cp %s ." % (extensionlibrary), shell=True)
        else:
            subprocess.call(["copy", extensionlibrary, "."], shell=True)
    return process.returncode

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


<commit_before># -*- coding:utf-8 -*-
import os
import sys

def main():
    path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    build_path = os.path.join(path, 'build')

    languages = os.listdir(build_path)
    languages = [l for l in languages if l.endswith('.js') and not l.startswith('highlight.')]
    if len(sys.argv) > 1:
        languages = set(os.path.splitext(l)[0] for l in languages)
        languages = set(languages) & set(sys.argv[1:])
        languages = ['%s.js' % l for l in languages]

    f = open(os.path.join(build_path, 'highlight.pack.js'), 'w')
    f.write(open(os.path.join(build_path, 'highlight.js')).read())
    for language in languages:
        f.write(open(os.path.join(build_path, language)).read())
    f.close()

if __name__ == '__main__':
    main()<commit_msg>Удаление tools/build.py<commit_after><|endoftext|>