<commit_before>"""
Unit tests for the sumatra.recordstore package
"""

import unittest2 as unittest
import os
import sys
from datetime import datetime
from django.core import management

from sumatra.records import Record
from sumatra.programs import register_executable, Executable
from sumatra.recordstore import shelve_store, django_store, http_store
from sumatra.versioncontrol import vcs_list
import sumatra.launch
import sumatra.datastore
import sumatra.parameters
try:
    import json
except ImportError:
    import simplejson as json
import urlparse

originals = []
django_store1 = None
django_store2 = None

class MockExecutable(Executable):
    name = "a.out"
    path = "/usr/local/bin/a.out"
    version = "999"
    options = "-v"
    def __init__(self, *args, **kwargs):
        pass
register_executable(MockExecutable, "a.out", "/usr/local/bin/a.out", [])

class MockRepository(object):
    url = "http://svn.example.com/"
    type = "MockRepository"
    def __init__(self, *args, **kwargs):
        pass
vcs_list.append(sys.modules[__name__])

def tearDownModule():
    vcs_list.remove(sys.modules[__name__])

class MockLaunchMode(object):
    type = "SerialLaunchMode"
    def __getstate__(self):
        return {}

class MockDataStore(object):
    type = "FileSystemDataStore"
    def __init__(self, **parameters):
        pass
    def __getstate__(self):
        return {'root': "/tmp"}
    def copy(self):
        return self

class MockDependency(object):
    name = "some_module"
    path = "/usr/lib/python/some_module.py"
    version = "1.0"
    diff = ""
    module = "python"

class MockPlatformInformation(object):
    architecture_bits = 32
    architecture_linkage = ""
    machine = ""
    network_name = ""
    ip_addr = "192.168.0.0"
    processor = ""
    release = ""
    system_name = ""
    version = ""

class MockParameterSet(object):
    def __init__(self, initialiser):
        pass
    def as_dict(self):
        return {}
    def pop(self, k, d):
        return None

class MockRecord(object):

    def __init__(self, label):
        self.label = label
        self.timestamp = datetime.now() #datetime(1901, 6, 1, 12, 0, 0)
        self.reason = "because"
        self.duration = 7543.2
        self.outcome = None
        self.stdout_stderr = "ok"
        self.main_file = "test"
        self.version = "99863a9dc5f"
        self.output_data = []
        self.executable = MockExecutable()
        self.repository = MockRepository()
        self.launch_mode = MockLaunchMode()
        self.datastore = MockDataStore()
        self.input_datastore = MockDataStore()
        self.parameters = MockParameterSet({})
        self.tags = set([])
        self.dependencies = [MockDependency(), MockDependency()]
        self.platforms = [MockPlatformInformation()]
        self.diff = ""
        self.user = "michaelpalin"
        self.input_data = []
        self.script_arguments = "arg1 arg2"

    def __eq__(self, other):
        return self.label == other.label and self.duration == other.duration

class MockProject(object):
    name = "TestProject"

def clean_up():
    if os.path.exists("test_record_store.db"):
        os.remove("test_record_store.db")

def setup():
    global django_store1, django_store2
    clean_up()
    sumatra.launch.MockLaunchMode = MockLaunchMode
    sumatra.datastore.MockDataStore = MockDataStore
    sumatra.parameters.MockParameterSet = MockParameterSet
    if django_store1 is None:
        django_store1 = django_store.DjangoRecordStore(db_file="test.db")
    if django_store2 is None:
        django_store2 = django_store.DjangoRecordStore(db_file="test2.db")
    django_store.db_config.configure()

def teardown():
    del sumatra.launch.MockLaunchMode
    del sumatra.datastore.MockDataStore
    del sumatra.parameters.MockParameterSet
    for filename in (django_store1._db_file, django_store2._db_file):
        if os.path.exists(filename):
            os.remove(filename)

class BaseTestRecordStore(object):

    def tearDown(self):
        django_store1.delete_all()
        django_store2.delete_all()
        for filename in ("test_record_store2",):
            if os.path.exists(filename):
                os.remove(filename)

    def add_some_records(self):
        r1 = MockRecord("record1")
        r2 = MockRecord("record2")
        r3 = MockRecord("record3")
        for r in r1, r2, r3:
            #print "saving record %s" % r.label
            self.store.save(self.project.name, r)

    def add_some_tags(self):
        r1 = MockRecord("record1")
        r3 = MockRecord("record3")
        r1.tags.add("tag1")
        r1.tags.add("tag2")
        r3.tags.add("tag1")
        self.store.save(self.project.name, r1)
        self.store.save(self.project.name, r3)

    def test_create_record_store_should_not_produce_errors(self):
        pass

    def test_save_should_not_produce_errors(self):
        self.add_some_records()

    def test_get(self):
        self.add_some_records()
        r = self.store.get(self.project.name, "record1")
        assert isinstance(r, (MockRecord, Record)), type(r)
        assert r.label == "record1", r.label

    def test_get_nonexistent_record_raises_KeyError(self):
        self.assertRaises(KeyError, self.store.get, self.project.name, "foo")

    def test_list_without_tags_should_return_all_records(self):
        self.add_some_records()
        records = self.store.list(self.project.name)
        assert isinstance(records, list), type(records)
        self.assertEqual(len(records), 3)

    def test_list_for_tags_should_filter_records_appropriately(self):
        self.add_some_records()
        self.add_some_tags()
        records = self.store.list(self.project.name, "tag1")
        self.assertEqual(len(records), 2)

    def test_delete_removes_record(self):
        self.add_some_records()
        key = "record1"
        self.store.delete(self.project.name, key)
        self.assertRaises(KeyError, self.store.get, self.project.name, key)

    def test_delete_by_tag(self):
        self.add_some_records()
        self.assertEqual(len(self.store.list(self.project.name)), 3)
        self.add_some_tags()
        r = self.store.get(self.project.name, "record1")
        self.assertEqual(r.tags, set(['tag1', 'tag2']))
        n = self.store.delete_by_tag(self.project.name, "tag1")
        self.assertEqual(n, 2)
        self.assertEqual(len(self.store.list(self.project.name)), 1)
        self.assertRaises(KeyError, self.store.get, self.project.name, "record1")

    def test_delete_nonexistent_label(self):
        self.add_some_records()
        self.assertRaises(Exception, # could be KeyError or DoesNotExist
                          self.store.delete,
                          self.project.name,
                          "notarecord")
        # should emit warning but not exception

    def test_str(self):
        #this test is pointless, just to increase coverage
        assert isinstance(str(self.store), basestring)

    def test_most_recent(self):
        self.add_some_records()
        self.assertEqual(self.store.most_recent(self.project.name), "record3")
        self.store.delete(self.project.name, "record3")
        self.assertEqual(self.store.most_recent(self.project.name), "record2")

    def test_sync_with_shelve_store(self):
        self.add_some_records()
        other_store = shelve_store.ShelveRecordStore(shelf_name="test_record_store2")
        self.assertEqual(len(other_store.list(self.project.name)), 0)
        self.store.sync(other_store, self.project.name)
        self.assertEqual(sorted(rec.label for rec in self.store.list(self.project.name)),
                         sorted(rec.label for rec in other_store.list(self.project.name)))

    def test_sync_with_django_store(self):
        other_store = django_store2
        self.add_some_records()
        self.assertEqual(len(other_store.list(self.project.name)), 0)
        self.store.sync(other_store, self.project.name)
        self.assertEqual(sorted(rec.label for rec in self.store.list(self.project.name)),
                         sorted(rec.label for rec in other_store.list(self.project.name)))

class TestShelveRecordStore(unittest.TestCase, BaseTestRecordStore):

    def setUp(self):
        self.store = shelve_store.ShelveRecordStore(shelf_name="test_record_store")
        self.project = MockProject()

    def tearDown(self):
        BaseTestRecordStore.tearDown(self)
        os.remove("test_record_store")

    def test_record_store_is_pickleable(self):
        import pickle
        self.add_some_records()
        s = pickle.dumps(self.store)
        del self.store
        unpickled = pickle.loads(s)
        assert unpickled._shelf_name == "test_record_store"
        assert os.path.exists(unpickled._shelf_name)

class TestDjangoRecordStore(unittest.TestCase, BaseTestRecordStore):

    def setUp(self):
        self.store = django_store1
        self.project = MockProject()

    def tearDown(self):
        BaseTestRecordStore.tearDown(self)

    def __del__(self):
        clean_up()

    def test_record_store_is_pickleable(self):
        import pickle
        self.add_some_records()
        s = pickle.dumps(self.store)
        del self.store
        django_store.db_config.configured = False
        django_store.db_config._settings['DATABASES'] = {}
        unpickled = pickle.loads(s)
        #assert unpickled._shelf_name == "test_record_store"
        #assert os.path.exists(unpickled._shelf_name)

class MockResponse(object):
    def __init__(self, status):
        self.status = status

def check_record(record):
    # this is a rather basic test. Should also check the keys of the
    # subsidiary dicts, and the types of the values
    assert set(record.keys()) == set(["executable", "parameters", "repository",
                                      "tags", "main_file", "label", "platforms",
                                      "reason", "version", "user", "launch_mode",
                                      "timestamp", "duration", "diff",
                                      "datastore", "outcome", "output_data",
                                      "dependencies", "input_data",
                                      "script_arguments", "stdout_stderr",
                                      "input_datastore"])

class MockCredentials(object):
        credentials = [['domain', 'username', 'password']]

class MockHttp(object):
    def __init__(self, *args):
        self.records = {}
        self.debug = False
        self.last_record = None
        self.credentials = MockCredentials()
    def add_credentials(self, *args, **kwargs):
        pass
    def request(self, uri, method="GET", body=None, headers=None, **kwargs):
        u = urlparse.urlparse(uri)
        parts = u.path.split("/")[1:-1]
        if self.debug:
            print "\n<<<<<", uri, u.path, len(parts), method, body, headers, u.params, u.query
        if len(parts) == 2: # record uri
            if method == "PUT":
                record = json.loads(body)
                check_record(record)
                self.records[parts[1]] = record
                content = ""
                status = 200
                self.last_record = record
            elif method == "GET":
                label = parts[1]
                if label == "last":
                    content = json.dumps(self.last_record)
                else:
                    content = json.dumps(self.records[label])
                status = 200
            elif method == "DELETE":
                self.records.pop(parts[1])
                most_recent = u""
                for record in self.records.itervalues():
                    if record["timestamp"] > most_recent:
                        most_recent = record["timestamp"]
                        self.last_record = record
                content = ""
                status = 204
        elif len(parts) == 1: # project uri
            if u.query:
                tags = u.query.split("=")[1].split(",")
                records = set([])
                for tag in tags:
                    records = records.union(["%s://%s/%s/%s/" % (u.scheme, u.netloc, parts[0],
                                               path) for path in self.records.keys() if tag in self.records[path]['tags']])
                records = list(records)
            else:
                records = ["%s://%s/%s/%s/" % (u.scheme, u.netloc, parts[0],
                                                    path) for path in self.records.keys()]
            content = json.dumps({"records": records})
            status = 200
        elif len(parts) == 3: # tagged records uri
            if method == "DELETE":
                tag = parts[2]
                n = 0
                for key, record in self.records.items():
                    if tag in record["tags"]:
                        self.records.pop(key)
                        n += 1
                status = 200
                content = str(n)
        if self.debug:
            print ">>>>>", status, content
        return MockResponse(status), content

class MockHttpLib(object):

    @staticmethod
    def Http(*args):
        return MockHttp(*args)

class TestHttpRecordStore(unittest.TestCase, BaseTestRecordStore):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)
        self.real_httplib = http_store.httplib2
        http_store.httplib2 = MockHttpLib()

    def __del__(self):
        http_store.httplib2 = self.real_httplib

    def setUp(self):
        self.store = http_store.HttpRecordStore("http://127.0.0.1:8000/", "testuser", "z6Ty49HY")
        self.project = MockProject()

    def test_record_store_is_pickleable(self):
        import pickle
        self.add_some_records()
        s = pickle.dumps(self.store)
        del self.store
        unpickled = pickle.loads(s)

if __name__ == '__main__':
    setup()
    unittest.main()
    teardown()
<commit_msg>Fixed spurious error in unit tests<commit_after>"""
Unit tests for the sumatra.recordstore package
"""

import unittest2 as unittest
import os
import sys
from datetime import datetime
from django.core import management

from sumatra.records import Record
from sumatra.programs import register_executable, Executable
from sumatra.recordstore import shelve_store, django_store, http_store
from sumatra.versioncontrol import vcs_list
import sumatra.launch
import sumatra.datastore
import sumatra.parameters
try:
    import json
except ImportError:
    import simplejson as json
import urlparse

originals = []
django_store1 = None
django_store2 = None

class MockExecutable(Executable):
    name = "a.out"
    path = "/usr/local/bin/a.out"
    version = "999"
    options = "-v"
    def __init__(self, *args, **kwargs):
        pass
register_executable(MockExecutable, "a.out", "/usr/local/bin/a.out", [])

class MockRepository(object):
    url = "http://svn.example.com/"
    type = "MockRepository"
    def __init__(self, *args, **kwargs):
        pass

class MockLaunchMode(object):
    type = "SerialLaunchMode"
    def __getstate__(self):
        return {}

class MockDataStore(object):
    type = "FileSystemDataStore"
    def __init__(self, **parameters):
        pass
    def __getstate__(self):
        return {'root': "/tmp"}
    def copy(self):
        return self

class MockDependency(object):
    name = "some_module"
    path = "/usr/lib/python/some_module.py"
    version = "1.0"
    diff = ""
    module = "python"

class MockPlatformInformation(object):
    architecture_bits = 32
    architecture_linkage = ""
    machine = ""
    network_name = ""
    ip_addr = "192.168.0.0"
    processor = ""
    release = ""
    system_name = ""
    version = ""

class MockParameterSet(object):
    def __init__(self, initialiser):
        pass
    def as_dict(self):
        return {}
    def pop(self, k, d):
        return None

class MockRecord(object):

    def __init__(self, label):
        self.label = label
        self.timestamp = datetime.now() #datetime(1901, 6, 1, 12, 0, 0)
        self.reason = "because"
        self.duration = 7543.2
        self.outcome = None
        self.stdout_stderr = "ok"
        self.main_file = "test"
        self.version = "99863a9dc5f"
        self.output_data = []
        self.executable = MockExecutable()
        self.repository = MockRepository()
        self.launch_mode = MockLaunchMode()
        self.datastore = MockDataStore()
        self.input_datastore = MockDataStore()
        self.parameters = MockParameterSet({})
        self.tags = set([])
        self.dependencies = [MockDependency(), MockDependency()]
        self.platforms = [MockPlatformInformation()]
        self.diff = ""
        self.user = "michaelpalin"
        self.input_data = []
        self.script_arguments = "arg1 arg2"

    def __eq__(self, other):
        return self.label == other.label and self.duration == other.duration

class MockProject(object):
    name = "TestProject"

def clean_up():
    if os.path.exists("test_record_store.db"):
        os.remove("test_record_store.db")

def setup():
    global django_store1, django_store2
    clean_up()
    sumatra.launch.MockLaunchMode = MockLaunchMode
    sumatra.datastore.MockDataStore = MockDataStore
    sumatra.parameters.MockParameterSet = MockParameterSet
    if django_store1 is None:
        django_store1 = django_store.DjangoRecordStore(db_file="test.db")
    if django_store2 is None:
        django_store2 = django_store.DjangoRecordStore(db_file="test2.db")
    django_store.db_config.configure()
    vcs_list.append(sys.modules[__name__])

def teardown():
    del sumatra.launch.MockLaunchMode
    del sumatra.datastore.MockDataStore
    del sumatra.parameters.MockParameterSet
    for filename in (django_store1._db_file, django_store2._db_file):
        if os.path.exists(filename):
            os.remove(filename)
    vcs_list.remove(sys.modules[__name__])

class BaseTestRecordStore(object):

    def tearDown(self):
        django_store1.delete_all()
        django_store2.delete_all()
        for filename in ("test_record_store2",):
            if os.path.exists(filename):
                os.remove(filename)

    def add_some_records(self):
        r1 = MockRecord("record1")
        r2 = MockRecord("record2")
        r3 = MockRecord("record3")
        for r in r1, r2, r3:
            #print "saving record %s" % r.label
            self.store.save(self.project.name, r)

    def add_some_tags(self):
        r1 = MockRecord("record1")
        r3 = MockRecord("record3")
        r1.tags.add("tag1")
        r1.tags.add("tag2")
        r3.tags.add("tag1")
        self.store.save(self.project.name, r1)
        self.store.save(self.project.name, r3)

    def test_create_record_store_should_not_produce_errors(self):
        pass

    def test_save_should_not_produce_errors(self):
        self.add_some_records()

    def test_get(self):
        self.add_some_records()
        r = self.store.get(self.project.name, "record1")
        assert isinstance(r, (MockRecord, Record)), type(r)
        assert r.label == "record1", r.label

    def test_get_nonexistent_record_raises_KeyError(self):
        self.assertRaises(KeyError, self.store.get, self.project.name, "foo")

    def test_list_without_tags_should_return_all_records(self):
        self.add_some_records()
        records = self.store.list(self.project.name)
        assert isinstance(records, list), type(records)
        self.assertEqual(len(records), 3)

    def test_list_for_tags_should_filter_records_appropriately(self):
        self.add_some_records()
        self.add_some_tags()
        records = self.store.list(self.project.name, "tag1")
        self.assertEqual(len(records), 2)

    def test_delete_removes_record(self):
        self.add_some_records()
        key = "record1"
        self.store.delete(self.project.name, key)
        self.assertRaises(KeyError, self.store.get, self.project.name, key)

    def test_delete_by_tag(self):
        self.add_some_records()
        self.assertEqual(len(self.store.list(self.project.name)), 3)
        self.add_some_tags()
        r = self.store.get(self.project.name, "record1")
        self.assertEqual(r.tags, set(['tag1', 'tag2']))
        n = self.store.delete_by_tag(self.project.name, "tag1")
        self.assertEqual(n, 2)
        self.assertEqual(len(self.store.list(self.project.name)), 1)
        self.assertRaises(KeyError, self.store.get, self.project.name, "record1")

    def test_delete_nonexistent_label(self):
        self.add_some_records()
        self.assertRaises(Exception, # could be KeyError or DoesNotExist
                          self.store.delete,
                          self.project.name,
                          "notarecord")
        # should emit warning but not exception

    def test_str(self):
        #this test is pointless, just to increase coverage
        assert isinstance(str(self.store), basestring)

    def test_most_recent(self):
        self.add_some_records()
        self.assertEqual(self.store.most_recent(self.project.name), "record3")
        self.store.delete(self.project.name, "record3")
        self.assertEqual(self.store.most_recent(self.project.name), "record2")

    def test_sync_with_shelve_store(self):
        self.add_some_records()
        other_store = shelve_store.ShelveRecordStore(shelf_name="test_record_store2")
        self.assertEqual(len(other_store.list(self.project.name)), 0)
        self.store.sync(other_store, self.project.name)
        self.assertEqual(sorted(rec.label for rec in self.store.list(self.project.name)),
                         sorted(rec.label for rec in other_store.list(self.project.name)))

    def test_sync_with_django_store(self):
        other_store = django_store2
        self.add_some_records()
        self.assertEqual(len(other_store.list(self.project.name)), 0)
        self.store.sync(other_store, self.project.name)
        self.assertEqual(sorted(rec.label for rec in self.store.list(self.project.name)),
                         sorted(rec.label for rec in other_store.list(self.project.name)))

class TestShelveRecordStore(unittest.TestCase, BaseTestRecordStore):

    def setUp(self):
        self.store = shelve_store.ShelveRecordStore(shelf_name="test_record_store")
        self.project = MockProject()

    def tearDown(self):
        BaseTestRecordStore.tearDown(self)
        os.remove("test_record_store")

    def test_record_store_is_pickleable(self):
        import pickle
        self.add_some_records()
        s = pickle.dumps(self.store)
        del self.store
        unpickled = pickle.loads(s)
        assert unpickled._shelf_name == "test_record_store"
        assert os.path.exists(unpickled._shelf_name)

class TestDjangoRecordStore(unittest.TestCase, BaseTestRecordStore):

    def setUp(self):
        self.store = django_store1
        self.project = MockProject()

    def tearDown(self):
        BaseTestRecordStore.tearDown(self)

    def __del__(self):
        clean_up()

    def test_record_store_is_pickleable(self):
        import pickle
        self.add_some_records()
        s = pickle.dumps(self.store)
        del self.store
        django_store.db_config.configured = False
        django_store.db_config._settings['DATABASES'] = {}
        unpickled = pickle.loads(s)
        #assert unpickled._shelf_name == "test_record_store"
        #assert os.path.exists(unpickled._shelf_name)

class MockResponse(object):
    def __init__(self, status):
        self.status = status

def check_record(record):
    # this is a rather basic test. Should also check the keys of the
    # subsidiary dicts, and the types of the values
    assert set(record.keys()) == set(["executable", "parameters", "repository",
                                      "tags", "main_file", "label", "platforms",
                                      "reason", "version", "user", "launch_mode",
                                      "timestamp", "duration", "diff",
                                      "datastore", "outcome", "output_data",
                                      "dependencies", "input_data",
                                      "script_arguments", "stdout_stderr",
                                      "input_datastore"])

class MockCredentials(object):
        credentials = [['domain', 'username', 'password']]

class MockHttp(object):
    def __init__(self, *args):
        self.records = {}
        self.debug = False
        self.last_record = None
        self.credentials = MockCredentials()
    def add_credentials(self, *args, **kwargs):
        pass
    def request(self, uri, method="GET", body=None, headers=None, **kwargs):
        u = urlparse.urlparse(uri)
        parts = u.path.split("/")[1:-1]
        if self.debug:
            print "\n<<<<<", uri, u.path, len(parts), method, body, headers, u.params, u.query
        if len(parts) == 2: # record uri
            if method == "PUT":
                record = json.loads(body)
                check_record(record)
                self.records[parts[1]] = record
                content = ""
                status = 200
                self.last_record = record
            elif method == "GET":
                label = parts[1]
                if label == "last":
                    content = json.dumps(self.last_record)
                else:
                    content = json.dumps(self.records[label])
                status = 200
            elif method == "DELETE":
                self.records.pop(parts[1])
                most_recent = u""
                for record in self.records.itervalues():
                    if record["timestamp"] > most_recent:
                        most_recent = record["timestamp"]
                        self.last_record = record
                content = ""
                status = 204
        elif len(parts) == 1: # project uri
            if u.query:
                tags = u.query.split("=")[1].split(",")
                records = set([])
                for tag in tags:
                    records = records.union(["%s://%s/%s/%s/" % (u.scheme, u.netloc, parts[0],
                                               path) for path in self.records.keys() if tag in self.records[path]['tags']])
                records = list(records)
            else:
                records = ["%s://%s/%s/%s/" % (u.scheme, u.netloc, parts[0],
                                                    path) for path in self.records.keys()]
            content = json.dumps({"records": records})
            status = 200
        elif len(parts) == 3: # tagged records uri
            if method == "DELETE":
                tag = parts[2]
                n = 0
                for key, record in self.records.items():
                    if tag in record["tags"]:
                        self.records.pop(key)
                        n += 1
                status = 200
                content = str(n)
        if self.debug:
            print ">>>>>", status, content
        return MockResponse(status), content

class MockHttpLib(object):

    @staticmethod
    def Http(*args):
        return MockHttp(*args)

class TestHttpRecordStore(unittest.TestCase, BaseTestRecordStore):

    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)
        self.real_httplib = http_store.httplib2
        http_store.httplib2 = MockHttpLib()

    def __del__(self):
        http_store.httplib2 = self.real_httplib

    def setUp(self):
        self.store = http_store.HttpRecordStore("http://127.0.0.1:8000/", "testuser", "z6Ty49HY")
        self.project = MockProject()

    def test_record_store_is_pickleable(self):
        import pickle
        self.add_some_records()
        s = pickle.dumps(self.store)
        del self.store
        unpickled = pickle.loads(s)

if __name__ == '__main__':
    setup()
    unittest.main()
    teardown()
<|endoftext|>


<commit_before>import os, re, sim, sys, time

def die(message):
    print('Error: %s.' % message)
    sys.exit(1)

output = sim.config.output_dir
period = 1e6 * sim.util.Time.NS

benchmark = os.getenv('BENCHMARK_NAME')
if not benchmark: die('cannot identify the benchmark')

mcpat_bin = os.path.join(os.getenv('TOOLS_ROOT'), 'mcpat.py')
if not os.path.exists(mcpat_bin): die('cannot find mcpat.py')

bullet_bin = os.path.join(os.getenv('BULLET_ROOT'), 'bin', 'bullet')
if not os.path.exists(bullet_bin): die('cannot find bullet')

results = os.getenv('RESULTS_ROOT')
if not results: die('cannot idenitfy the results directory')

redis_bin = 'redis-cli'
server = '127.0.0.1:6379'
queue = 'bullet'

sqlite_bin = 'sqlite3'
database = os.path.join(results, '%s.sqlite3' % benchmark)
table = re.sub(r'-', '_', benchmark)

class Bullet:
    def setup(self, args):
        reset()
        bullet_start()
        self.t_last = 0
        sim.util.Every(period, self.periodic, roi_only = True)

    def periodic(self, time, elapsed):
        report('Time %.2f ms, elapsed %.2f ms' % (
            time / sim.util.Time.MS, elapsed / sim.util.Time.MS
        ))
        self.process(time)

    def hook_sim_end(self):
        self.process(sim.stats.get('performance_model', 0, 'elapsed_time'))
        bullet_stop()

    def process(self, time):
        time = coarse(time)
        sim.stats.write(str(time))
        self.compute_power(self.t_last, time)
        self.t_last = time

    def compute_power(self, t0, t1):
        filebase = os.path.join(output, 'power-%s-%s-%s' % (t0, t1, t1 - t0))
        bullet_send(filebase, t0, t1)

def reset():
    run('%s DEL %s' % (redis_bin, queue))
    run('%s %s "DROP TABLE IF EXISTS %s;"' % (sqlite_bin, database, table))

def coarse(time):
    return long(long(time) / sim.util.Time.NS)

def bullet_send(filebase, t0, t1):
    if t0 == 0: t0 = 'roi-begin'
    prepare = "%s -o %s -d %s --partial=%s:%s" % (mcpat_bin, filebase, output, t0, t1)
    enqueue = "(%s RPUSH %s %s > /dev/null)" % (redis_bin, queue, filebase + '.xml')
    run('unset PYTHONHOME && %s && %s &' % (prepare, enqueue))

def bullet_start():
    run('%s -s %s -q %s -c -d %s -t %s &' % (
        bullet_bin, server, queue, database, table
    ))

def bullet_stop():
    run('%s RPUSH %s bullet:halt > /dev/null' % (redis_bin, queue))

def report(message):
    print('-------> [%-15s] %s' % (benchmark, message))

def run(command):
    if os.system(command) != 0: die('failed to run `%s`' % command)

sim.util.register(Bullet())
<commit_msg>Record area apart from power<commit_after>import os, sim, sys, time

def die(message):
    print('Error: %s.' % message)
    sys.exit(1)

output = sim.config.output_dir
period = 1e6 * sim.util.Time.NS

benchmark = os.getenv('BENCHMARK_NAME')
if not benchmark: die('cannot identify the benchmark')

mcpat_bin = os.path.join(os.getenv('TOOLS_ROOT'), 'mcpat.py')
if not os.path.exists(mcpat_bin): die('cannot find mcpat.py')

bullet_bin = os.path.join(os.getenv('BULLET_ROOT'), 'bin', 'bullet')
if not os.path.exists(bullet_bin): die('cannot find bullet')

results = os.getenv('RESULTS_ROOT')
if not results: die('cannot idenitfy the results directory')

redis_bin = 'redis-cli'
server = '127.0.0.1:6379'
queue = 'bullet'

sqlite_bin = 'sqlite3'
database = os.path.join(results, '%s.sqlite3' % benchmark)
area_table = 'area'
power_table = 'power'

class Bullet:
    def setup(self, args):
        reset()
        self.t_last = None
        bullet_power_start()
        sim.util.Every(period, self.periodic, roi_only = True)

    def periodic(self, t, _):
        report('%.2f ms' % (t / sim.util.Time.MS))
        t = coarse(t)
        sim.stats.write(str(t))
        if self.t_last == None:
            self.compute_area(t)
        else:
            self.compute_power(self.t_last, t)
        self.t_last = t

    def hook_sim_end(self):
        bullet_power_stop()

    def compute_area(self, t):
        filebase = os.path.join(output, 'area')
        bullet_area_send(filebase, t)

    def compute_power(self, t0, t1):
        filebase = os.path.join(output, 'power-%s-%s' % (t0, t1))
        bullet_power_send(filebase, t0, t1)

def reset():
    run('%s DEL %s > /dev/null' % (redis_bin, queue))
    run('%s %s "DROP TABLE IF EXISTS %s;"' % (sqlite_bin, database, area_table))
    run('%s %s "DROP TABLE IF EXISTS %s;"' % (sqlite_bin, database, power_table))

def coarse(time):
    return long(long(time) / sim.util.Time.NS)

def bullet_area_send(filebase, t):
    run('unset PYTHONHOME && %s -o %s -d %s --partial=%s:%s' % (
      mcpat_bin, filebase, output, 'start', t
    ))
    run('%s area --server %s --caching --config %s --database %s --table %s &' % (
        bullet_bin, server, filebase + '.xml', database, area_table
    ))

def bullet_power_send(filebase, t0, t1):
    run('unset PYTHONHOME && %s -o %s -d %s --partial=%s:%s' % (
      mcpat_bin, filebase, output, t0, t1
    ))
    run('%s RPUSH %s %s > /dev/null &' % (
      redis_bin, queue, filebase + '.xml'
    ))

def bullet_power_start():
    run('%s power --server %s --queue %s --caching --database %s --table %s &' % (
        bullet_bin, server, queue, database, power_table
    ))

def bullet_power_stop():
    run('%s RPUSH %s bullet:halt > /dev/null' % (redis_bin, queue))

def report(message):
    print('-------> [%-15s] %s' % (benchmark, message))

def run(command):
    if os.system(command) != 0: die('failed to run `%s`' % command)

sim.util.register(Bullet())
<|endoftext|>


<commit_before>from django.db import models
from django.utils.translation import ugettext, ugettext_lazy as _

from invoice.models import Customer

from django.conf import settings
from decimal import Decimal
import datetime
from functools import partial

#--------------------------------------------------------------------------------
# Measures units constants.
# note: other UNIT_ could be Gigabytes, Terabytes

UNIT_MONTHS = 'months'
UNIT_HOURS = 'hours'
UNIT_SECONDS = 'seconds'
UNIT_CHOICES = (
    #(UNIT_SECONDS, _('Seconds')),
    (UNIT_MONTHS, _('Months')),
    (UNIT_HOURS, _('Hours')),
)

# Conversion table map. Use "partial" because we cannot lambda (":") in dict values

CONVERSION_UNIT_MAP = {
    (UNIT_HOURS, UNIT_MONTHS): partial(lambda x : x/720),
    (UNIT_SECONDS, UNIT_MONTHS): partial(lambda x : x/2592000),
}
#--------------------------------------------------------------------------------

class Service(models.Model):
    """
    Kind of periodic service offered. I.e:

    * Quota annuale associativa
    * Quota annuale mailing-list

    but it could be also:

    * N gigabytes of traffic

    """

    abbreviation = models.CharField(max_length=32,verbose_name=_("abbreviation"))
    name = models.CharField(max_length=256, db_index=True,verbose_name=_("name"))
    description = models.TextField(verbose_name=_("description"))
    period = models.IntegerField(null=True, blank=True,
        help_text=_('indicator of a period by raw units.'),verbose_name=_("period")
    )
    period_deadline_modifier=models.IntegerField(null=True,blank=True,help_text=_('indicator to modify the periodic payement deadline'),verbose_name=_("period deadline modifier"))
    period_unit_raw = models.CharField(max_length=16, default=UNIT_HOURS, choices=UNIT_CHOICES, verbose_name=_("raw unit"))
    period_unit_display = models.CharField(max_length=16,
        blank=True, help_text=_('display measure unit for period'),
        choices=UNIT_CHOICES, default=UNIT_MONTHS,verbose_name=_("period measure of unit")
    )
    period_unit_source = models.CharField(max_length=32, default="epoch_now",verbose_name=_("period start"))

    amount = models.DecimalField(max_digits=12, decimal_places=2, verbose_name=_("amount"))
    default_vat_percent = models.DecimalField(max_digits=3, decimal_places=2,
        default=Decimal(str(settings.DEFAULT_VAT_PERCENT)), verbose_name=_("default vat percentage")
    )

    def __unicode__(self):
        return self.name

class ServiceSubscription(models.Model):
    """
    Map services offered to customers specifying subscription period,
    and some attributes specific to this subscription like discount or
    custom vat_percent.

    It is used by ServiceSubscriptionPayments as template for payment.
    """

    customer = models.ForeignKey(Customer)
    service = models.ForeignKey(Service)

    vat_percent = models.DecimalField(max_digits=3, decimal_places=2,
        default=Decimal(str(settings.DEFAULT_VAT_PERCENT))
    )

    discount = models.DecimalField(_("discount"), default=0, max_digits=3, decimal_places=2)

    invoice_period = models.IntegerField(null=True, blank=True,
        help_text=_('how many period lasts before creating an invoice?'),
        default=1
    )

    subscribed_on = models.DateTimeField(null=True)
    subscribed_until = models.DateTimeField(null=True)

    note = models.TextField(blank=True)

    created_on = models.DateTimeField(auto_now_add=True)
    last_update_on = models.DateTimeField(auto_now=True)

    # last paid on and last_paid_for
    # could be also properties get by ServiceSubscriptionPayments model
    last_paid_on = models.DateTimeField(null=True)
    last_paid_for = models.IntegerField(help_text=_("For what has he paid? (incremental value)"))

    class Meta:
        unique_together = (('customer', 'service', 'subscribed_on'),)

    @property
    def next_payment_due(self):
        #raise NotImplementedError("TBD")
        difference = (datetime.datetime.now() - self.last_paid_on.replace(tzinfo=None)).total_seconds()
        if self.service.period_unit_raw == UNIT_MONTHS:
            return (difference / 2592000) > (self.last_paid_for + self.service.period_deadline_modifier)
        elif self.service.period_unit_raw == UNIT_HOURS:
            return (difference / 3600) > (self.last_paid_for + self.service.period_deadline_modifier)
        elif self.service.period_unit_raw == UNIT_SECONDS:
            return difference  > (self.last_paid_for + self.service.period_deadline_modifier)
        raise NotImplementedError("TBD")

class ServiceSubscriptionPayments(models.Model):
    """
    """

    customer = models.ForeignKey(Customer)
    service = models.ForeignKey(Service)

    amount = models.DecimalField(max_digits=12, decimal_places=2)
    vat_percent = models.DecimalField(max_digits=3, decimal_places=2,
        default=Decimal(str(settings.DEFAULT_VAT_PERCENT))
    )

    discount = models.DecimalField(_("discount"), default=0, max_digits=3, decimal_places=2)

    paid_on = models.DateTimeField(auto_now_add=True, help_text=_('When has it been paid?'))
    paid_for = models.IntegerField(help_text=_("For what has he paid? (incremental value). Leave 0 to use default"))

    note = models.TextField(blank=True)

    class Meta:
        unique_together = (('customer', 'service', 'paid_for'),)

    @property
    def paid_for_display(self):
        unit_raw = self.service.period_unit_raw
        unit_display = self.service.period_unit_display
        return CONVERSION_UNIT_MAP[(unit_raw, unit_display)](self.paid_for)

<commit_msg>Modified the attributes of some Service fields<commit_after>from django.db import models
from django.utils.translation import ugettext, ugettext_lazy as _

from invoice.models import Customer

from django.conf import settings
from decimal import Decimal
import datetime
from functools import partial

#--------------------------------------------------------------------------------
# Measures units constants.
# note: other UNIT_ could be Gigabytes, Terabytes

UNIT_MONTHS = 'months'
UNIT_HOURS = 'hours'
UNIT_SECONDS = 'seconds'
UNIT_CHOICES = (
    #(UNIT_SECONDS, _('Seconds')),
    (UNIT_MONTHS, _('Months')),
    (UNIT_HOURS, _('Hours')),
)

# Conversion table map. Use "partial" because we cannot lambda (":") in dict values

CONVERSION_UNIT_MAP = {
    (UNIT_HOURS, UNIT_MONTHS): partial(lambda x : x/720),
    (UNIT_SECONDS, UNIT_MONTHS): partial(lambda x : x/2592000),
}
#--------------------------------------------------------------------------------

class Service(models.Model):
    """
    Kind of periodic service offered. I.e:

    * Quota annuale associativa
    * Quota annuale mailing-list

    but it could be also:

    * N gigabytes of traffic

    """

    abbreviation = models.CharField(max_length=32,verbose_name=_("abbreviation"))
    name = models.CharField(max_length=256, db_index=True,verbose_name=_("name"))
    description = models.TextField(verbose_name=_("description"))
    period = models.IntegerField(help_text=_('indicator of a period by raw units.'),
        verbose_name=_("period")
    )
    period_deadline_modifier=models.IntegerField(blank=True,
        help_text=_('indicator to modify the periodic payement deadline'),
        verbose_name=_("period deadline modifier")
    )
    period_unit_raw = models.CharField(max_length=16, default=UNIT_HOURS,
        choices=UNIT_CHOICES, verbose_name=_("raw unit")
    )
    period_unit_display = models.CharField(null=False,max_length=16,
        help_text=_('display measure unit for period'),
        choices=UNIT_CHOICES, default=UNIT_MONTHS,verbose_name=_("period measure of unit")
    )
    period_unit_source = models.CharField(max_length=32, default="epoch_now",verbose_name=_("period start"))

    amount = models.DecimalField(max_digits=12, decimal_places=2, verbose_name=_("amount"))
    default_vat_percent = models.DecimalField(max_digits=3, decimal_places=2,
        default=Decimal(str(settings.DEFAULT_VAT_PERCENT)), verbose_name=_("default vat percentage")
    )

    def __unicode__(self):
        return self.name

class ServiceSubscription(models.Model):
    """
    Map services offered to customers specifying subscription period,
    and some attributes specific to this subscription like discount or
    custom vat_percent.

    It is used by ServiceSubscriptionPayments as template for payment.
    """

    customer = models.ForeignKey(Customer)
    service = models.ForeignKey(Service)

    vat_percent = models.DecimalField(max_digits=3, decimal_places=2,
        default=Decimal(str(settings.DEFAULT_VAT_PERCENT))
    )

    discount = models.DecimalField(_("discount"), default=0, max_digits=3, decimal_places=2)

    invoice_period = models.IntegerField(null=True, blank=True,
        help_text=_('how many period lasts before creating an invoice?'),
        default=1
    )

    subscribed_on = models.DateTimeField(null=True)
    subscribed_until = models.DateTimeField(null=True)

    note = models.TextField(blank=True)

    created_on = models.DateTimeField(auto_now_add=True)
    last_update_on = models.DateTimeField(auto_now=True)

    # last paid on and last_paid_for
    # could be also properties get by ServiceSubscriptionPayments model
    last_paid_on = models.DateTimeField(null=True)
    last_paid_for = models.IntegerField(help_text=_("For what has he paid? (incremental value)"))

    class Meta:
        unique_together = (('customer', 'service', 'subscribed_on'),)

    @property
    def next_payment_due(self):
        #raise NotImplementedError("TBD")
        difference = (datetime.datetime.now() - self.last_paid_on.replace(tzinfo=None)).total_seconds()
        if self.service.period_unit_raw == UNIT_MONTHS:
            return (difference / 2592000) > (self.last_paid_for + self.service.period_deadline_modifier)
        elif self.service.period_unit_raw == UNIT_HOURS:
            return (difference / 3600) > (self.last_paid_for + self.service.period_deadline_modifier)
        elif self.service.period_unit_raw == UNIT_SECONDS:
            return difference  > (self.last_paid_for + self.service.period_deadline_modifier)
        raise NotImplementedError("TBD")

class ServiceSubscriptionPayments(models.Model):
    """
    """

    customer = models.ForeignKey(Customer)
    service = models.ForeignKey(Service)

    amount = models.DecimalField(max_digits=12, decimal_places=2)
    vat_percent = models.DecimalField(max_digits=3, decimal_places=2,
        default=Decimal(str(settings.DEFAULT_VAT_PERCENT))
    )

    discount = models.DecimalField(_("discount"), default=0, max_digits=3, decimal_places=2)

    paid_on = models.DateTimeField(auto_now_add=True, help_text=_('When has it been paid?'))
    paid_for = models.IntegerField(help_text=_("For what has he paid? (incremental value). Leave 0 to use default"))

    note = models.TextField(blank=True)

    class Meta:
        unique_together = (('customer', 'service', 'paid_for'),)

    @property
    def paid_for_display(self):
        unit_raw = self.service.period_unit_raw
        unit_display = self.service.period_unit_display
        return CONVERSION_UNIT_MAP[(unit_raw, unit_display)](self.paid_for)

<|endoftext|>


<commit_before># build for the iphone/ipad

# A lot of code here is just copy/pasted from the main SConstruct file.
# Refactor this all out.

from SCons.Environment import Environment
from SCons.Script import Exit
import os

def isPlatform(platform):
    import sys
    return platform in sys.platform

def isWindows():
    return isPlatform("win32")

def xterm_color(string, color):
    colors = {'none': "0",
              'black': "0;30",
              'red': "0;31",
              'green': "0;32",
              'brown': "0;33",
              'blue': "0;34",
              'purple': "0;35",
              'cyan': "0;36",
              'light-gray': "0;37",
              'dark-gray': "1:30",
              'light-red': "1;31",
              'light-green': "1;32",
              'yellow': "1;33",
              'light-blue': "1;34",
              'light-purple': "1;35",
              'light-cyan': "1;36",
              'white': "1;37"}
    return "\033[%sm%s\033[0m" % (colors[color], string)

# todo: figure out when we are on an xterm
def isXterm():
    # assume linux and osx are ok
    return not isWindows()

def colorize(string, color):
    if noColors():
        return string
    if isXterm():
        return xterm_color(string, color)
    return string

def dummyCheck(context):
    context.Result(1)
    return 1

def peg_to_cpp(target, source, env):
    import sys
    sys.path.append("src/mugen/parser")
    sys.path.append(".")
    import peg, re, cpp_generator
    name = source[0].name
    parser = peg.make_peg_parser(re.sub('\..*', '', name))
    fout = open(target[0].path, 'w')
    fout.write(cpp_generator.generate(parser(source[0].path)))
    fout.write('\n')
    fout.close()

def iphoneEnvironment():
    base = Environment(ENV = os.environ)
    gccversion = '4.2'
    iostarget = '4.3'
    osxtarget = '10.6'
    platform_phone = 'iPhoneOS'
    arch_phone = 'armv6'
    phone_bin_dir = '/Developer/Platforms/%s.platform/Developer/usr/bin' % platform_phone
    base.PrependENVPath('PATH', phone_bin_dir)
    sdkroot_phone = '/Developer/Platforms/%s.platform/Developer/SDKs/%s%s.sdk' % (platform_phone, platform_phone, iostarget)
    postfix = '-%s' % gccversion
    def setup(x, post):
        return '%s%s' % (x, post)
    # only the compiler can have different versions
    base['CC'] = setup('gcc', postfix)
    base['CXX'] = setup('g++', postfix)

    def stringify(array):
        str = ''
        for item in array:
            str += item + ' '
        return [str.split(" ")]

    cflags_phone = ['-std=gnu99', '-fobjc-legacy-dispatch', '-fobjc-abi-version=2']
    ccflags_phone = ['-arch %s' % arch_phone, '-pipe', '-mdynamic-no-pic', '-fvisibility=hidden', '-isysroot %s' % sdkroot_phone, '-mmacosx-version-min=%s' % osxtarget] + Split("""-g -O2 -gdwarf-2 -mthumb -Wall -Wmissing-prototypes -ffast-math -fno-strict-aliasing -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -gdwarf-2 -Wall -fno-strict-aliasing""")
    ldflags_phone = ['-arch %s' % arch_phone, '-isysroot %s' % sdkroot_phone, '-Wl,-dead_strip', '-mmacosx-version-min=%s' % osxtarget, '-Xlinker -objc_abi_version', '-Xlinker 2', '-Wl,-search_paths_first', '-Wl,-headerpad_max_install_names']
    #frameworks = Split("""OpenGLES CoreGraphics QuartzCore UIKit Foundation CoreFoundation OpenAL""")

    # ldflags_phone = ['-arch %s' % arch_phone, '-framework OpenGLES']
    defines_phone = Split("""__IPHONE_OS_VERSION_MIN_REQUIRED=40300""")
    cppflags = Split("")

    def setFlags(includes, cflags, ccflags, cppflags, ldflags, defines, env):
        env.Append(CPATH = includes)
        env.Append(CPPPATH = includes)
        env.Append(CFLAGS = cflags)
        env.Append(CCFLAGS = ccflags)
        env.Append(CPPFLAGS = cppflags)
        env.Append(LINKFLAGS = ldflags)
        env.Append(CPPDEFINES = defines)

    # base.Append(LIBS = ['allegro-phone'])
    #base.Append(FRAMEWORKS = frameworks)
    base['LIBS'] = ['allegro', 'allegro_acodec', 'allegro_audio', 'allegro_image', 'allegro_memfile', 'allegro_primitives', 'allegro_ttf', 'allegro_font']
    base.ParseConfig('misc/allegro-iphone/install/phone/bin/freetype-config --cflags --libs')
    base.Append(LIBPATH = ['#misc/allegro-iphone/install/phone/lib'])
    includes = [sdkroot_phone, '#misc/allegro-iphone/allegro/include/allegro5/']
    #  + ['%s/usr/include/c++/4.2.1' % sdkroot_phone] + ['%s/usr/include/c++/4.2.1/i686-apple-darwin10' % sdkroot_phone]
    setFlags(includes + ['%s/usr/include' % sdkroot_phone], stringify(cflags_phone), stringify(ccflags_phone), cppflags, stringify(ldflags_phone), defines_phone, base)

    return base

def simulatorEnvironment():
    base = Environment(ENV = os.environ)
    gccversion = '4.2'
    iostarget = '4.3'
    osxtarget = '10.6'
    platform_sim = 'iPhoneSimulator'
    arch_sim = 'i686'
    sim_bin_dir = '/Developer/Platforms/%s.platform/Developer/usr/bin' % platform_sim
    base.PrependENVPath('PATH', sim_bin_dir)
    sdkroot_sim = '/Developer/Platforms/%s.platform/Developer/SDKs/%s%s.sdk' % (platform_sim, platform_sim, iostarget)
    postfix = '-%s' % gccversion
    def setup(x, post):
        return '%s%s' % (x, post)
    # only the compiler can have different versions
    base['CC'] = setup('gcc', postfix)
    base['CXX'] = setup('g++', postfix)

    def stringify(array):
        str = ''
        for item in array:
            str += item + ' '
        return [str.split(" ")]

    cflags_sim = ['-std=gnu99', '-fobjc-legacy-dispatch', '-fobjc-abi-version=2']
    ccflags_sim = ['-arch %s' % arch_sim, '-pipe', '-mdynamic-no-pic', '-fvisibility=hidden', '-isysroot %s' % sdkroot_sim, '-mmacosx-version-min=%s' % osxtarget] + Split("""-g -O2 -gdwarf-2 -mthumb -Wall -Wmissing-prototypes -ffast-math -fno-strict-aliasing -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -gdwarf-2 -Wall -fno-strict-aliasing""")
    ldflags_sim = ['-arch %s' % arch_sim, '-isysroot %s' % sdkroot_sim, '-Wl,-dead_strip', '-mmacosx-version-min=%s' % osxtarget, '-Xlinker -objc_abi_version', '-Xlinker 2', '-Wl,-search_paths_first', '-Wl,-headerpad_max_install_names']
    frameworks = Split("""OpenGLES CoreGraphics QuartzCore UIKit Foundation CoreFoundation OpenAL""")

    # ldflags_sim = ['-arch %s' % arch_sim, '-framework OpenGLES']
    defines_sim = Split("""__IPHONE_OS_VERSION_MIN_REQUIRED=40300""")
    cppflags = Split("")

    def setFlags(includes, cflags, ccflags, cppflags, ldflags, defines, env):
        env.Append(CPATH = includes)
        env.Append(CPPPATH = includes)
        env.Append(CFLAGS = cflags)
        env.Append(CCFLAGS = ccflags)
        env.Append(CPPFLAGS = cppflags)
        env.Append(LINKFLAGS = ldflags)
        env.Append(CPPDEFINES = defines)

    # base.Append(LIBS = ['allegro-sim'])
    base.Append(FRAMEWORKS = frameworks)
    base['LIBS'] = ['allegro', 'allegro_acodec', 'allegro_audio', 'allegro_image', 'allegro_memfile', 'allegro_primitives', 'allegro_ttf', 'allegro_font']
    base.ParseConfig('misc/allegro-iphone/install/sim/bin/freetype-config --cflags --libs')
    base.Append(LIBPATH = ['#misc/allegro-iphone/install/sim/lib'])
    includes = [sdkroot_sim, '#misc/allegro-iphone/allegro/include/allegro5/']
    #  + ['%s/usr/include/c++/4.2.1' % sdkroot_sim] + ['%s/usr/include/c++/4.2.1/i686-apple-darwin10' % sdkroot_sim]
    setFlags(includes + ['%s/usr/include' % sdkroot_sim], stringify(cflags_sim), stringify(ccflags_sim), cppflags, stringify(ldflags_sim), defines_sim, base)

    return base

def buildForEnvironment(env, name):
    env['PEG_MAKE'] = 'Creating peg parser $TARGET'

    buildDir = 'build-allegro5-iphone-%s' % name

    # Generates a single .cpp file from a .peg description
    peg_builder = Builder(action = Action(peg_to_cpp, env['PEG_MAKE']),
                      suffix = '.cpp',
                      src_suffix = '.peg')

    env.Append(BUILDERS = {'Peg' : peg_builder})
    env['PAINTOWN_NETWORKING'] = False
    env['PAINTOWN_BACKEND'] = 'allegro5'
    env['PAINTOWN_BUILD_TESTS'] = False
    env['PAINTOWN_TESTS'] = {'CheckPython': dummyCheck}
    env['PAINTOWN_COLORIZE'] = colorize
    env['PAINTOWN_USE_PRX'] = False
    env.Append(CPPPATH = ["#%s" % buildDir, '#misc/allegro-iphone/install/%s/include' % name])
    env.Append(CPPDEFINES = ['USE_ALLEGRO5', 'IPHONE'])
    use = env
    shared = SConscript('src/SConscript', variant_dir = buildDir, exports = ['use'] );

#env = simulatorEnvironment()
#env['PEG_MAKE'] = 'Creating peg parser $TARGET'

#buildDir = 'build-allegro5-iphone-sim'

# Generates a single .cpp file from a .peg description
#peg_builder = Builder(action = Action(peg_to_cpp, env['PEG_MAKE']),
#                      suffix = '.cpp',
#                      src_suffix = '.peg')

#env.Append(BUILDERS = {'Peg' : peg_builder})
#env['PAINTOWN_NETWORKING'] = False
#env['PAINTOWN_BACKEND'] = 'allegro5'
#env['PAINTOWN_BUILD_TESTS'] = False
#env['PAINTOWN_TESTS'] = {'CheckPython': dummyCheck}
#env['PAINTOWN_COLORIZE'] = colorize
#env['PAINTOWN_USE_PRX'] = False
#env.Append(CPPPATH = ["#%s" % buildDir, '#misc/allegro-iphone/install/sim/include'])
#env.Append(CPPDEFINES = ['USE_ALLEGRO5', 'IPHONE'])
#use = env
#shared = SConscript('src/SConscript', variant_dir = buildDir, exports = ['use'] );

buildForEnvironment(simulatorEnvironment(), 'sim')
buildForEnvironment(iphoneEnvironment(), 'phone')<commit_msg>Add frameworks.<commit_after># build for the iphone/ipad

# A lot of code here is just copy/pasted from the main SConstruct file.
# Refactor this all out.

from SCons.Environment import Environment
from SCons.Script import Exit
import os

def isPlatform(platform):
    import sys
    return platform in sys.platform

def isWindows():
    return isPlatform("win32")

def xterm_color(string, color):
    colors = {'none': "0",
              'black': "0;30",
              'red': "0;31",
              'green': "0;32",
              'brown': "0;33",
              'blue': "0;34",
              'purple': "0;35",
              'cyan': "0;36",
              'light-gray': "0;37",
              'dark-gray': "1:30",
              'light-red': "1;31",
              'light-green': "1;32",
              'yellow': "1;33",
              'light-blue': "1;34",
              'light-purple': "1;35",
              'light-cyan': "1;36",
              'white': "1;37"}
    return "\033[%sm%s\033[0m" % (colors[color], string)

# todo: figure out when we are on an xterm
def isXterm():
    # assume linux and osx are ok
    return not isWindows()

def colorize(string, color):
    if noColors():
        return string
    if isXterm():
        return xterm_color(string, color)
    return string

def dummyCheck(context):
    context.Result(1)
    return 1

def peg_to_cpp(target, source, env):
    import sys
    sys.path.append("src/mugen/parser")
    sys.path.append(".")
    import peg, re, cpp_generator
    name = source[0].name
    parser = peg.make_peg_parser(re.sub('\..*', '', name))
    fout = open(target[0].path, 'w')
    fout.write(cpp_generator.generate(parser(source[0].path)))
    fout.write('\n')
    fout.close()

def iphoneEnvironment():
    base = Environment(ENV = os.environ)
    gccversion = '4.2'
    iostarget = '4.3'
    osxtarget = '10.5'
    platform_phone = 'iPhoneOS'
    arch_phone = 'armv6'
    phone_bin_dir = '/Developer/Platforms/%s.platform/Developer/usr/bin' % platform_phone
    base.PrependENVPath('PATH', phone_bin_dir)
    sdkroot_phone = '/Developer/Platforms/%s.platform/Developer/SDKs/%s%s.sdk' % (platform_phone, platform_phone, iostarget)
    postfix = '-%s' % gccversion
    def setup(x, post):
        return '%s%s' % (x, post)
    # only the compiler can have different versions
    base['CC'] = setup('gcc', postfix)
    base['CXX'] = setup('g++', postfix)

    def stringify(array):
        str = ''
        for item in array:
            str += item + ' '
        return [str.split(" ")]

    cflags_phone = ['-std=gnu99', '-fobjc-legacy-dispatch', '-fobjc-abi-version=2']
    ccflags_phone = ['-arch %s' % arch_phone, '-pipe', '-mdynamic-no-pic', '-fvisibility=hidden', '-isysroot %s' % sdkroot_phone] + Split("""-g -O2 -gdwarf-2 -mthumb -Wall -Wmissing-prototypes -ffast-math -fno-strict-aliasing -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -gdwarf-2 -Wall -fno-strict-aliasing""")
    ldflags_phone = ['-arch %s' % arch_phone, '-isysroot %s' % sdkroot_phone, '-Wl,-dead_strip', '-Xlinker -objc_abi_version', '-Xlinker 2', '-Wl,-search_paths_first', '-Wl,-headerpad_max_install_names']
    frameworks = Split("""OpenGLES CoreGraphics QuartzCore UIKit Foundation CoreFoundation OpenAL""")

    # ldflags_phone = ['-arch %s' % arch_phone, '-framework OpenGLES']
    defines_phone = Split("""__IPHONE_OS_VERSION_MIN_REQUIRED=40300""")
    cppflags = Split("")

    def setFlags(includes, cflags, ccflags, cppflags, ldflags, defines, env):
        env.Append(CPATH = includes)
        env.Append(CPPPATH = includes)
        env.Append(CFLAGS = cflags)
        env.Append(CCFLAGS = ccflags)
        env.Append(CPPFLAGS = cppflags)
        env.Append(LINKFLAGS = ldflags)
        env.Append(CPPDEFINES = defines)

    # base.Append(LIBS = ['allegro-phone'])
    base.Append(FRAMEWORKS = frameworks)
    base['LIBS'] = ['allegro', 'allegro_acodec', 'allegro_audio', 'allegro_image', 'allegro_memfile', 'allegro_primitives', 'allegro_ttf', 'allegro_font']
    base.ParseConfig('misc/allegro-iphone/install/phone/bin/freetype-config --cflags --libs')
    base.Append(LIBPATH = ['#misc/allegro-iphone/install/phone/lib'])
    includes = [sdkroot_phone, '#misc/allegro-iphone/allegro/include/allegro5/']
    #  + ['%s/usr/include/c++/4.2.1' % sdkroot_phone] + ['%s/usr/include/c++/4.2.1/i686-apple-darwin10' % sdkroot_phone]
    setFlags(includes + ['%s/usr/include' % sdkroot_phone], stringify(cflags_phone), stringify(ccflags_phone), cppflags, stringify(ldflags_phone), defines_phone, base)

    return base

def simulatorEnvironment():
    base = Environment(ENV = os.environ)
    gccversion = '4.2'
    iostarget = '4.3'
    osxtarget = '10.6'
    platform_sim = 'iPhoneSimulator'
    arch_sim = 'i686'
    sim_bin_dir = '/Developer/Platforms/%s.platform/Developer/usr/bin' % platform_sim
    base.PrependENVPath('PATH', sim_bin_dir)
    sdkroot_sim = '/Developer/Platforms/%s.platform/Developer/SDKs/%s%s.sdk' % (platform_sim, platform_sim, iostarget)
    postfix = '-%s' % gccversion
    def setup(x, post):
        return '%s%s' % (x, post)
    # only the compiler can have different versions
    base['CC'] = setup('gcc', postfix)
    base['CXX'] = setup('g++', postfix)

    def stringify(array):
        str = ''
        for item in array:
            str += item + ' '
        return [str.split(" ")]

    cflags_sim = ['-std=gnu99', '-fobjc-legacy-dispatch', '-fobjc-abi-version=2']
    ccflags_sim = ['-arch %s' % arch_sim, '-pipe', '-mdynamic-no-pic', '-fvisibility=hidden', '-isysroot %s' % sdkroot_sim, '-mmacosx-version-min=%s' % osxtarget] + Split("""-g -O2 -gdwarf-2 -mthumb -Wall -Wmissing-prototypes -ffast-math -fno-strict-aliasing -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -gdwarf-2 -Wall -fno-strict-aliasing""")
    ldflags_sim = ['-arch %s' % arch_sim, '-isysroot %s' % sdkroot_sim, '-Wl,-dead_strip', '-mmacosx-version-min=%s' % osxtarget, '-Xlinker -objc_abi_version', '-Xlinker 2', '-Wl,-search_paths_first', '-Wl,-headerpad_max_install_names']
    frameworks = Split("""OpenGLES CoreGraphics QuartzCore UIKit Foundation CoreFoundation OpenAL""")

    # ldflags_sim = ['-arch %s' % arch_sim, '-framework OpenGLES']
    defines_sim = Split("""__IPHONE_OS_VERSION_MIN_REQUIRED=40300""")
    cppflags = Split("")

    def setFlags(includes, cflags, ccflags, cppflags, ldflags, defines, env):
        env.Append(CPATH = includes)
        env.Append(CPPPATH = includes)
        env.Append(CFLAGS = cflags)
        env.Append(CCFLAGS = ccflags)
        env.Append(CPPFLAGS = cppflags)
        env.Append(LINKFLAGS = ldflags)
        env.Append(CPPDEFINES = defines)

    # base.Append(LIBS = ['allegro-sim'])
    base.Append(FRAMEWORKS = frameworks)
    base['LIBS'] = ['allegro', 'allegro_acodec', 'allegro_audio', 'allegro_image', 'allegro_memfile', 'allegro_primitives', 'allegro_ttf', 'allegro_font']
    base.ParseConfig('misc/allegro-iphone/install/sim/bin/freetype-config --cflags --libs')
    base.Append(LIBPATH = ['#misc/allegro-iphone/install/sim/lib'])
    includes = [sdkroot_sim, '#misc/allegro-iphone/allegro/include/allegro5/']
    #  + ['%s/usr/include/c++/4.2.1' % sdkroot_sim] + ['%s/usr/include/c++/4.2.1/i686-apple-darwin10' % sdkroot_sim]
    setFlags(includes + ['%s/usr/include' % sdkroot_sim], stringify(cflags_sim), stringify(ccflags_sim), cppflags, stringify(ldflags_sim), defines_sim, base)

    return base

def buildForEnvironment(env, name):
    env['PEG_MAKE'] = 'Creating peg parser $TARGET'

    buildDir = 'build-allegro5-iphone-%s' % name

    # Generates a single .cpp file from a .peg description
    peg_builder = Builder(action = Action(peg_to_cpp, env['PEG_MAKE']),
                      suffix = '.cpp',
                      src_suffix = '.peg')

    env.Append(BUILDERS = {'Peg' : peg_builder})
    env['PAINTOWN_NETWORKING'] = False
    env['PAINTOWN_BACKEND'] = 'allegro5'
    env['PAINTOWN_BUILD_TESTS'] = False
    env['PAINTOWN_TESTS'] = {'CheckPython': dummyCheck}
    env['PAINTOWN_COLORIZE'] = colorize
    env['PAINTOWN_USE_PRX'] = False
    env.Append(CPPPATH = ["#%s" % buildDir, '#misc/allegro-iphone/install/%s/include' % name])
    env.Append(CPPDEFINES = ['USE_ALLEGRO5', 'IPHONE'])
    use = env
    shared = SConscript('src/SConscript', variant_dir = buildDir, exports = ['use'] );

#env = simulatorEnvironment()
#env['PEG_MAKE'] = 'Creating peg parser $TARGET'

#buildDir = 'build-allegro5-iphone-sim'

# Generates a single .cpp file from a .peg description
#peg_builder = Builder(action = Action(peg_to_cpp, env['PEG_MAKE']),
#                      suffix = '.cpp',
#                      src_suffix = '.peg')

#env.Append(BUILDERS = {'Peg' : peg_builder})
#env['PAINTOWN_NETWORKING'] = False
#env['PAINTOWN_BACKEND'] = 'allegro5'
#env['PAINTOWN_BUILD_TESTS'] = False
#env['PAINTOWN_TESTS'] = {'CheckPython': dummyCheck}
#env['PAINTOWN_COLORIZE'] = colorize
#env['PAINTOWN_USE_PRX'] = False
#env.Append(CPPPATH = ["#%s" % buildDir, '#misc/allegro-iphone/install/sim/include'])
#env.Append(CPPDEFINES = ['USE_ALLEGRO5', 'IPHONE'])
#use = env
#shared = SConscript('src/SConscript', variant_dir = buildDir, exports = ['use'] );

buildForEnvironment(simulatorEnvironment(), 'sim')
buildForEnvironment(iphoneEnvironment(), 'phone')<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""Heroku specific settings. These are used to deploy opencomparison to
Heroku's platform.
"""

from postgresify import postgresify

from settings.base import *

CACHE_TIMEOUT = 60 * 60 * 24

########## DATABASES
DATABASES = postgresify()

INSTALLED_APPS += ['gunicorn']
LOCAL_INSTALLED_APPS = []

LAUNCHPAD_ACTIVE = False

# Analytics ID
URCHIN_ID = ""

# Email Settings
DEFAULT_FROM_EMAIL = \
        'Django Packages <djangopackages-noreply@djangopackages.com>'
EMAIL_SUBJECT_PREFIX = '[Django Packages] '

RESTRICT_PACKAGE_EDITORS = False
RESTRICT_GRID_EDITORS = False

ROOT_URLCONF = "app.urls"

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

SECRET_KEY = os.environ['SECRET_KEY']

GITHUB_API_SECRET = os.environ['GITHUB_API_SECRET']
GITHUB_APP_ID = os.environ['GITHUB_APP_ID']
SITE_TITLE = os.environ['SITE_TITLE']
FRAMEWORK_TITLE = os.environ['FRAMEWORK_TITLE']

PIWIK_CODE = """

"""

########## STORAGE CONFIGURATION

INSTALLED_APPS += ['storages', ]

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

AWS_QUERYSTRING_AUTH = False

AWS_HEADERS = {
    'Expires': 'Thu, 15 Apr 2020 20:00:00 GMT',
    'Cache-Control': 'max-age=86400',
}

# Boto requires subdomain formatting.
from S3 import CallingFormat
AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN

# Amazon S3 configuration.
if os.environ.has_key('S3_KEY'):
    AWS_ACCESS_KEY_ID = os.environ['S3_KEY']
    AWS_SECRET_ACCESS_KEY = os.environ['S3_SECRET']
else:
    AWS_ACCESS_KEY_ID = AWS_KEY
    AWS_SECRET_ACCESS_KEY = AWS_SECRET_KEY

AWS_STORAGE_BUCKET_NAME = 'opencomparison'

STATIC_URL = 'https://s3.amazonaws.com/opencomparison/'
MEDIA_URL = STATIC_URL
########## END STORAGE CONFIGURATION
<commit_msg>Adding a WSGI SERVER section header to settings.heroku for clarity.<commit_after># -*- coding: utf-8 -*-
"""Heroku specific settings. These are used to deploy opencomparison to
Heroku's platform.
"""

from postgresify import postgresify

from settings.base import *

CACHE_TIMEOUT = 60 * 60 * 24

########## DATABASES
DATABASES = postgresify()

########## WSGI SERVER
INSTALLED_APPS += ['gunicorn']

LOCAL_INSTALLED_APPS = []

LAUNCHPAD_ACTIVE = False

# Analytics ID
URCHIN_ID = ""

# Email Settings
DEFAULT_FROM_EMAIL = \
        'Django Packages <djangopackages-noreply@djangopackages.com>'
EMAIL_SUBJECT_PREFIX = '[Django Packages] '

RESTRICT_PACKAGE_EDITORS = False
RESTRICT_GRID_EDITORS = False

ROOT_URLCONF = "app.urls"

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

SECRET_KEY = os.environ['SECRET_KEY']

GITHUB_API_SECRET = os.environ['GITHUB_API_SECRET']
GITHUB_APP_ID = os.environ['GITHUB_APP_ID']
SITE_TITLE = os.environ['SITE_TITLE']
FRAMEWORK_TITLE = os.environ['FRAMEWORK_TITLE']

PIWIK_CODE = """

"""

########## STORAGE CONFIGURATION

INSTALLED_APPS += ['storages', ]

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

AWS_QUERYSTRING_AUTH = False

AWS_HEADERS = {
    'Expires': 'Thu, 15 Apr 2020 20:00:00 GMT',
    'Cache-Control': 'max-age=86400',
}

# Boto requires subdomain formatting.
from S3 import CallingFormat
AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN

# Amazon S3 configuration.
if os.environ.has_key('S3_KEY'):
    AWS_ACCESS_KEY_ID = os.environ['S3_KEY']
    AWS_SECRET_ACCESS_KEY = os.environ['S3_SECRET']
else:
    AWS_ACCESS_KEY_ID = AWS_KEY
    AWS_SECRET_ACCESS_KEY = AWS_SECRET_KEY

AWS_STORAGE_BUCKET_NAME = 'opencomparison'

STATIC_URL = 'https://s3.amazonaws.com/opencomparison/'
MEDIA_URL = STATIC_URL
########## END STORAGE CONFIGURATION
<|endoftext|>


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

# Utilitzeu això si voleu fer servir diverses instàncies a la mateixa
# maquina. Cada una necessita el seu directori temporal:
#
# tempfile.tempdir = os.environ['HOME'] + "/tmp"

settings = {

  # Usuaris SOA i GN6
  "username_soa" : "xxx",
  "password_soa" : "xxx",
  "username_gn6" : "xxx",
  "password_gn6" : "xxx",

  # Instància de GN6 on voleu crear els tiquets
  "domini": "999",

  # Equip resolutor per defecte amb què s'han de crear els tiquets
  "equip_resolutor_nous": "99999",

  # Usuari amb què es crearan els tiquets pels correus desconeguts
  # (únicament si teniu activat el FiltreNouExtern)
  "usuari_extern": "11763",

  # Valors amb què es crearan els tiquets dependent de les capçaleres
  # del correu rebut. A part de l'equipResolutor, es poden canviar
  # tots els paramatres documentats al servei SOA de creació de tiquets
  # (prioritat, tipus...)
  "valors_defecte": [
    {
      "order"    : ['Resent-To','To'],
      "match"    : "^webmaster@unitat\.upc\.edu$",
      "defaults" : {"equipResolutor": "11111"}
    },
    {
      "order"    : ['Resent-From','From'],
      "match"    : "^nom\.cognom@upc\.edu$",
      "defaults" : {"equipResolutor": "11113"}
    },
    {
      "order"    : ['Resent-From'],
      "match"    : "^nom@unitat\.upc\.edu$",
      "defaults" : {"equipResolutor": "11112"}
    },
    {
      "order"    : ['Subject'],
      "match"    : ".*URGENT",
      "defaults" : {"urgencia": "GRAVETAT_ALTA"}
    }
  ],

  # Filtres actius. També podem utilitzar:
  # - filtres.reply_reobrint.FiltreRepyReobrint (reobre tiquets tancats)
  # - filtres.nou_extern.FiltreNouExtern (obre tiquets de correus desconeguts)
  "filtres": [
    "filtres.reply.FiltreReply",
    "filtres.nou.FiltreNou"
  ],

  # Patró per detectar el número de tiquet
  "regex_reply": ".*?R[eEvV]:.*?\[Suport Unitat ([\d]+)\]",

  # Patró per detectar si es tracta d'un comentari privat
  # (també cal modificar la plantilla corresponent a GN6)
  "regex_privat": "(?i)\(comentari privat\)",

  # Correus addicionals propis de cada unitat que no es troben al servei
  # d'Identitat Digital UPC. Per cada correu cal indicar quin usuari li
  # correspon per crear el tiquet.
  "mails_addicionals": {
    "gestio.pfc@escola.upc.edu"  : "nom.cognom",
    "cap.estudis@escola.upc.edu" : "escola.cap.estudis",
  },

  # Patrons de diferents correus que equivalen a un mateix usuari
  # o bé a una referència del propi patró (per exemple, el correu
  # nom.cognom@upc.edu -> nom.cognom).
  "patrons_mail_addicionals": {
    "^root@([a-z0-9.\-]+\.)?unitat\.upc\.e(s|du)$" : "usuari.generic",
    "^(.*)@upc.edu$"                               : "%s",
    "^(.*)@upcnet.es$"                             : "%s"
  },

  # Correus dels que no s'ha de crear cap tiquet per la raó que sigui
  "mails_no_ticket":[
    "info.exemple@upc.edu"
  ],

  # Filtres d'adjunts que no s'han de processar (per exemple, les
  # signatures que contenen imatges adjuntes). Es poden filtrar
  # pel nom de l'adjunt o per l'emprempta digital en MD5.
  "filtrar_attachments_per_nom": [
    "paic\d+.jpg"
  ],

  "filtrar_attachments_per_hash": [
    "76f6a359e98f9e0effc214033373b9cf",
    "7526d0f3f7864090353c181158b218c3"
  ],

  # Fitxers de log i grau de detall
  "log_file"  : tempfile.gettempdir() + "/mailtoticket.log",
  "log_level" : logging.INFO,

  # Notificació dels informes d'error per correu
  "notificar_errors"      : True,
  "notificar_errors_from" : "mailtoticket@unitat.upc.edu",
  "notificar_errors_to"   : "nom.cognom@unitat.upc.edu",
  "servidor_mail"         : "relay.upc.edu"
}
<commit_msg>Passa el PEP8 per settings_sample.py<commit_after># -*- coding: utf-8 -*-
import logging
import tempfile
import os

# Utilitzeu això si voleu fer servir diverses instàncies a la mateixa
# maquina. Cada una necessita el seu directori temporal:
#
# tempfile.tempdir = os.environ['HOME'] + "/tmp"

settings = {

    # Usuaris SOA i GN6
    "username_soa": "xxx",
    "password_soa": "xxx",
    "username_gn6": "xxx",
    "password_gn6": "xxx",

    # Instància de GN6 on voleu crear els tiquets
    "domini": "999",

    # Equip resolutor per defecte amb què s'han de crear els tiquets
    "equip_resolutor_nous": "99999",

    # Usuari amb què es crearan els tiquets pels correus desconeguts
    # (únicament si teniu activat el FiltreNouExtern)
    "usuari_extern": "11763",

    # Valors amb què es crearan els tiquets dependent de les capçaleres
    # del correu rebut. A part de l'equipResolutor, es poden canviar
    # tots els paramatres documentats al servei SOA de creació de tiquets
    # (prioritat, tipus...)
    "valors_defecte": [
        {
            "order": ['Resent-To', 'To'],
            "match": "^webmaster@unitat\.upc\.edu$",
            "defaults": {"equipResolutor": "11111"}
        },
        {
            "order": ['Resent-From', 'From'],
            "match": "^nom\.cognom@upc\.edu$",
            "defaults": {"equipResolutor": "11113"}
        },
        {
            "order": ['Resent-From'],
            "match": "^nom@unitat\.upc\.edu$",
            "defaults": {"equipResolutor": "11112"}
        },
        {
            "order": ['Subject'],
            "match": ".*URGENT",
            "defaults": {"urgencia": "GRAVETAT_ALTA"}
        }
    ],

    # Filtres actius. També podem utilitzar:
    # - filtres.reply_reobrint.FiltreRepyReobrint (reobre tiquets tancats)
    # - filtres.nou_extern.FiltreNouExtern (obre tiquets de correus
    #   desconeguts)
    "filtres": [
        "filtres.reply.FiltreReply",
        "filtres.nou.FiltreNou"
    ],

    # Patró per detectar el número de tiquet
    "regex_reply": ".*?R[eEvV]:.*?\[Suport Unitat ([\d]+)\]",

    # Patró per detectar si es tracta d'un comentari privat
    # (també cal modificar la plantilla corresponent a GN6)
    "regex_privat": "(?i)\(comentari privat\)",

    # Correus addicionals propis de cada unitat que no es troben al servei
    # d'Identitat Digital UPC. Per cada correu cal indicar quin usuari li
    # correspon per crear el tiquet.
    "mails_addicionals": {
        "gestio.pfc@escola.upc.edu": "nom.cognom",
        "cap.estudis@escola.upc.edu": "escola.cap.estudis",
    },

    # Patrons de diferents correus que equivalen a un mateix usuari
    # o bé a una referència del propi patró (per exemple, el correu
    # nom.cognom@upc.edu -> nom.cognom).
    "patrons_mail_addicionals": {
        "^root@([a-z0-9.\-]+\.)?unitat\.upc\.e(s|du)$": "usuari.generic",
        "^(.*)@upc.edu$": "%s",
        "^(.*)@upcnet.es$": "%s"
    },

    # Correus dels que no s'ha de crear cap tiquet per la raó que sigui
    "mails_no_ticket": [
        "info.exemple@upc.edu"
    ],

    # Filtres d'adjunts que no s'han de processar (per exemple, les
    # signatures que contenen imatges adjuntes). Es poden filtrar
    # pel nom de l'adjunt o per l'emprempta digital en MD5.
    "filtrar_attachments_per_nom": [
        "paic\d+.jpg"
    ],

    "filtrar_attachments_per_hash": [
        "76f6a359e98f9e0effc214033373b9cf",
        "7526d0f3f7864090353c181158b218c3"
    ],

    # Fitxers de log i grau de detall
    "log_file": tempfile.gettempdir() + "/mailtoticket.log",
    "log_level": logging.INFO,

    # Notificació dels informes d'error per correu
    "notificar_errors": True,
    "notificar_errors_from": "mailtoticket@unitat.upc.edu",
    "notificar_errors_to": "nom.cognom@unitat.upc.edu",
    "servidor_mail": "relay.upc.edu"
}
<|endoftext|>


<commit_before>"""
Author: Marusa Zerjal, 2019 - 07 - 18

"""

from multiprocessing import Pool
import contextlib
import time

import scipy
_SCIPY_VERSION= [int(v.split('rc')[0])
                 for v in scipy.__version__.split('.')]
if _SCIPY_VERSION[0] == 0 and _SCIPY_VERSION[1] < 10:
    from scipy.maxentropy import logsumexp
elif ((_SCIPY_VERSION[0] == 1 and _SCIPY_VERSION[1] >= 3) or
    _SCIPY_VERSION[0] > 1):
    from scipy.special import logsumexp
else:
    from scipy.misc import logsumexp

try:
    print('Using C implementation in expectmax')
    from _overlap import get_lnoverlaps
except:
    print("WARNING: Couldn't import C implementation, using slow pythonic overlap instead")
    logging.info("WARNING: Couldn't import C implementation, using slow pythonic overlap instead")
    from chronostar.likelihood import slow_get_lnoverlaps as get_lnoverlaps

class Bg_ols_cov_multiprocessing():
    def __init__(self, background_means, background_covs, nstars, star_covs, star_means):
        self.star_means = star_means
        self.star_covs = star_covs
        self.background_means = background_means
        self.background_covs = background_covs
        self.nstars = nstars

    def func(self, index):
        """
        Author: Marusa Zerjal, 2019 - 07 - 18

        Multiprocessing function should be pickable

        :param index:
        :return:
        """
        star_mean = self.star_means[index]
        star_cov = self.star_covs[index]
        print(star_mean, star_cov)
        try:
            #print('***********', nstars, star_cov, star_mean, background_covs, background_means)
            bg_lnol = get_lnoverlaps(star_cov, star_mean, self.background_covs,
                                     self.background_means, self.nstars)
            #print('intermediate', bg_lnol)
            # bg_lnol = np.log(np.sum(np.exp(bg_lnol))) # sum in linear space
            bg_lnol = logsumexp(bg_lnol) # sum in linear space

        # Do we really want to make exceptions here? If the sum fails then
        # there's something wrong with the data.
        except:
            # TC: Changed sign to negative (surely if it fails, we want it to
            # have a neglible background overlap?
            print('bg ln overlap failed, setting it to -inf')
            bg_lnol = -np.inf
        #bg_lnols.append(bg_lnol)
        #print(bg_lnol)
        #print('')
        return bg_lnol

    def compute_bg_ols(self):
        num_threads = 8
        start = time.time()
        # ~ with contextlib.closing( Pool(num_threads) ) as pool:
        #with Pool(num_threads) as pool:
        indices = range(len(star_means))
        print('indices', indices)
        with contextlib.closing(Pool(num_threads)) as pool:
            #results = pool.map(func, zip(star_means, star_covs))
            results = pool.map(self.func, indices)
        end = time.time()
        print(end - start, 'multiprocessing')
        print('results', results)
        return results<commit_msg>self.<commit_after>"""
Author: Marusa Zerjal, 2019 - 07 - 18

"""

from multiprocessing import Pool
import contextlib
import time

import scipy
_SCIPY_VERSION= [int(v.split('rc')[0])
                 for v in scipy.__version__.split('.')]
if _SCIPY_VERSION[0] == 0 and _SCIPY_VERSION[1] < 10:
    from scipy.maxentropy import logsumexp
elif ((_SCIPY_VERSION[0] == 1 and _SCIPY_VERSION[1] >= 3) or
    _SCIPY_VERSION[0] > 1):
    from scipy.special import logsumexp
else:
    from scipy.misc import logsumexp

try:
    print('Using C implementation in expectmax')
    from _overlap import get_lnoverlaps
except:
    print("WARNING: Couldn't import C implementation, using slow pythonic overlap instead")
    logging.info("WARNING: Couldn't import C implementation, using slow pythonic overlap instead")
    from chronostar.likelihood import slow_get_lnoverlaps as get_lnoverlaps

class Bg_ols_cov_multiprocessing():
    def __init__(self, background_means, background_covs, nstars, star_covs, star_means):
        self.star_means = star_means
        self.star_covs = star_covs
        self.background_means = background_means
        self.background_covs = background_covs
        self.nstars = nstars

    def func(self, index):
        """
        Author: Marusa Zerjal, 2019 - 07 - 18

        Multiprocessing function should be pickable

        :param index:
        :return:
        """
        star_mean = self.star_means[index]
        star_cov = self.star_covs[index]
        print(star_mean, star_cov)
        try:
            #print('***********', nstars, star_cov, star_mean, background_covs, background_means)
            bg_lnol = get_lnoverlaps(star_cov, star_mean, self.background_covs,
                                     self.background_means, self.nstars)
            #print('intermediate', bg_lnol)
            # bg_lnol = np.log(np.sum(np.exp(bg_lnol))) # sum in linear space
            bg_lnol = logsumexp(bg_lnol) # sum in linear space

        # Do we really want to make exceptions here? If the sum fails then
        # there's something wrong with the data.
        except:
            # TC: Changed sign to negative (surely if it fails, we want it to
            # have a neglible background overlap?
            print('bg ln overlap failed, setting it to -inf')
            bg_lnol = -np.inf
        #bg_lnols.append(bg_lnol)
        #print(bg_lnol)
        #print('')
        return bg_lnol

    def compute_bg_ols(self):
        num_threads = 8
        start = time.time()
        # ~ with contextlib.closing( Pool(num_threads) ) as pool:
        #with Pool(num_threads) as pool:
        indices = range(len(self.star_means))
        print('indices', indices)
        with contextlib.closing(Pool(num_threads)) as pool:
            #results = pool.map(func, zip(star_means, star_covs))
            results = pool.map(self.func, indices)
        end = time.time()
        print(end - start, 'multiprocessing')
        print('results', results)
        return results<|endoftext|>


<commit_before>def task_gen_logo():
    """generate versions of the logo in various sizes"""

    from PIL import Image

    def resize_logo(width):
        with Image.open('assets/logotype.png') as im:
            im = im.convert('L')
            height = im.height * width // im.width
            new = im.resize((width,height), resample=Image.LANCZOS)
            new.save('static/logotype-{}.png'.format(width), optimize=True)

    widths = (200, 400, 600, 800)
    for width in widths:
        yield dict(
                name=str(width),
                actions=[(resize_logo, (width,))],
                targets=[f'static/logotype-{width}.png'],
                file_dep=['assets/logotype.png'],
                clean=True,
            )

def task_copy_asset():
    import shutil
    assets = ('icon.png', 'logotype.png', 'settings.js')
    for asset in assets:
        yield dict(
                name=asset,
                actions=[(lambda asset: shutil.copy(f'assets/{asset}', f'static/{asset}'), (asset,))],
                targets=[f'static/{asset}'],
                file_dep=[f'assets/{asset}'],
                clean=True,
            )

def task_minify_css():
    """minify css"""

    from csscompressor import compress

    def minify():
        with open('assets/styles.css') as in_:
            with open('static/styles.css', 'w') as out:
                out.write(compress(in_.read()))

    return dict(
            actions=[minify],
            targets=['static/styles.css'],
            file_dep=['assets/styles.css'],
            clean=True,
        )

def task_compress_static():
    import brotli
    import gzip

    files = (
        'static/styles.css',
        'static/icon.png',
        'static/logotype.png',
        'static/version.js',
        'static/settings.js',
        ) + tuple((f'static/logotype-{width}.png' for width in (200, 400, 600, 800)))

    def compress_brotli(dependencies):
        for filename in dependencies:
            with open(filename, 'rb') as in_:
                with open(filename + '.br', 'wb') as out:
                    out.write(brotli.compress(in_.read()))
    def compress_gzip(dependencies):
        for filename in dependencies:
            with open(filename, 'rb') as in_:
                with gzip.open(filename + '.gz', 'wb') as out:
                    out.write(in_.read())

    for filename in files:
        yield dict(
                file_dep=(filename,),
                targets=(filename+'.br',),
                name=filename+'.br',
                actions=[compress_brotli],
                clean=True,
            )
        yield dict(
                file_dep=(filename,),
                targets=(filename+'.gz',),
                name=filename+'.gz',
                actions=[compress_gzip],
                clean=True,
            )

if __name__ == '__main__':
    import doit
    doit.run(globals())
<commit_msg>oops. remove version.js from doit<commit_after>def task_gen_logo():
    """generate versions of the logo in various sizes"""

    from PIL import Image

    def resize_logo(width):
        with Image.open('assets/logotype.png') as im:
            im = im.convert('L')
            height = im.height * width // im.width
            new = im.resize((width,height), resample=Image.LANCZOS)
            new.save('static/logotype-{}.png'.format(width), optimize=True)

    widths = (200, 400, 600, 800)
    for width in widths:
        yield dict(
                name=str(width),
                actions=[(resize_logo, (width,))],
                targets=[f'static/logotype-{width}.png'],
                file_dep=['assets/logotype.png'],
                clean=True,
            )

def task_copy_asset():
    import shutil
    assets = ('icon.png', 'logotype.png', 'settings.js')
    for asset in assets:
        yield dict(
                name=asset,
                actions=[(lambda asset: shutil.copy(f'assets/{asset}', f'static/{asset}'), (asset,))],
                targets=[f'static/{asset}'],
                file_dep=[f'assets/{asset}'],
                clean=True,
            )

def task_minify_css():
    """minify css"""

    from csscompressor import compress

    def minify():
        with open('assets/styles.css') as in_:
            with open('static/styles.css', 'w') as out:
                out.write(compress(in_.read()))

    return dict(
            actions=[minify],
            targets=['static/styles.css'],
            file_dep=['assets/styles.css'],
            clean=True,
        )

def task_compress_static():
    import brotli
    import gzip

    files = (
        'static/styles.css',
        'static/icon.png',
        'static/logotype.png',
        'static/settings.js',
        ) + tuple((f'static/logotype-{width}.png' for width in (200, 400, 600, 800)))

    def compress_brotli(dependencies):
        for filename in dependencies:
            with open(filename, 'rb') as in_:
                with open(filename + '.br', 'wb') as out:
                    out.write(brotli.compress(in_.read()))
    def compress_gzip(dependencies):
        for filename in dependencies:
            with open(filename, 'rb') as in_:
                with gzip.open(filename + '.gz', 'wb') as out:
                    out.write(in_.read())

    for filename in files:
        yield dict(
                file_dep=(filename,),
                targets=(filename+'.br',),
                name=filename+'.br',
                actions=[compress_brotli],
                clean=True,
            )
        yield dict(
                file_dep=(filename,),
                targets=(filename+'.gz',),
                name=filename+'.gz',
                actions=[compress_gzip],
                clean=True,
            )

if __name__ == '__main__':
    import doit
    doit.run(globals())
<|endoftext|>


<commit_before># coding: utf8

DOIT_CONFIG = {'default_tasks': []}

CITEULIKE_GROUP = 19073
BIBFILE = 'docs/pyfssa.bib'

def task_download_bib():
    """Download bibliography from CiteULike group"""

    return {
        'actions': [' '.join([
            'wget', '-O', BIBFILE,
            '"http://www.citeulike.org/bibtex/group/{}?incl_amazon=0&key_type=4"'.format(CITEULIKE_GROUP),
            ])],
        # 'file_dep': [CITEULIKE_COOKIES],
        'targets': [BIBFILE],
    }
<commit_msg>Add task to upload documentation to github pages<commit_after># coding: utf8

import os

DOIT_CONFIG = {'default_tasks': []}

CITEULIKE_GROUP = 19073
BIBFILE = 'docs/pyfssa.bib'

def task_download_bib():
    """Download bibliography from CiteULike group"""

    return {
        'actions': [' '.join([
            'wget', '-O', BIBFILE,
            '"http://www.citeulike.org/bibtex/group/{}?incl_amazon=0&key_type=4"'.format(CITEULIKE_GROUP),
            ])],
        # 'file_dep': [CITEULIKE_COOKIES],
        'targets': [BIBFILE],
    }

def task_upload_doc():
    """Upload built html documentation to GitHub pages"""

    return {
        'actions': [[
            'ghp-import',
            '-n',  # Include a .nojekyll file in the branch.
            '-p',  # Push the branch to origin/{branch} after committing.
            os.path.join('docs', '_build', 'html')
        ]],
    }
<|endoftext|>


<commit_before>from boolean import *

#predpostavljamo, da je formula podana v CNF obliki

def get_all_from_CNF(formula):
    if not isinstance(formula, And):
        return "formula ni podana v CNF obliki!"

def my_dpll(formula):
    valuation = dict()
    print(formula)
    if not isinstance(formula, And):
        return "formula ni podana v CNF obliki!"
    clauses = formula.terms
    print(clauses)
    #1. prazen seznam clausesov predstavlja vrednost T (satisfiable solution)
    if len(clauses)==0:
        return True
    #2. pregledamo, če je kateri izmed clausesov prazen, potem rešitve ni
    for clause in clauses:
        literals = clause.terms
        if len(literals) == 0:
            return False

def simplify_dpll(formula):
    pass

def simplify_unit_clause(formula, valuation):
    #TODO dodaj še spreminjanje spremenljivk glede na že znane vrednosti
    #TODO končaj program, če v slovar dodajaš drugačno vrednost, kot je že dodana
    print(formula)
    clauses = formula.terms
    for clause in clauses:
        if isinstance(clause, Or):
            literals = clause.terms
            if len(literals)==1:
                clauses.remove(clause)
                formula = And(*clauses)
                clause = literals[0]
                if isinstance(literals[0], Variable):
                    valuation[clause.x]=True
                elif isinstance(clause, Not):
                    if not isinstance(clause.x, Variable):
                        print("formula ni podana v CNF obliki!")
                    valuation[clause.x.x]=False
                simplify_unit_clause(formula, valuation)
                break
            else:
                continue
        elif isinstance(clause, Variable):
            valuation[clause.x]=True
            clauses.remove(clause)
            formula = And(*clauses)
            simplify_unit_clause(formula, valuation)
            break
        elif isinstance(clause, Not):
            if not isinstance(clause.x, Variable):
                print("formula ni podana v CNF obliki!")
            valuation[clause.x.x]=False
            clauses.remove(clause)
            formula = And(*clauses)
            simplify_unit_clause(formula, valuation)
            break
    return valuation

def simplify_pore_literal(formula, valuation):
    #mogoče pojdi čez seznam vseh spremenljivk in označi, če je spremenljivka (nastavi na true),
    #če je negacija (nastavi na false) in preglejuj če se kdaj seka --> odstrani iz seznama pure
    #pure literal. Na začetku nastavi na none
    pass

def simplify_by_literal(formula, l, tf):
    print(formula)
    clauses = formula.terms
    print(clauses)
    st_clauses = len(clauses)
    i = 0
    while i < st_clauses:
        clause = clauses[i]
        i = i+1
        if isinstance(clause, Or):
            literals = clause.terms
            if len(literals)==0:
                formula = F
                break
            elif tf:
                if l in literals:
                    clauses.remove(clause)
                    i, st_clauses = i-1, st_clauses-1
                if Not(l) in literals:
                    if len(literals)==1:
                        formula = F
                        break
                    else:
                        literals.remove(Not(l))
                        if len(literals)==1:
                            formula = F
                            break
            else:
                if l in literals:
                    if len(literals)==1:
                        formula = F
                        break
                    else:
                        literals.remove(l)
                        if len(literals)==1:
                            formula = F
                            break
                if Not(l) in literals:
                    clauses.remove(clause)
                    i, st_clauses = i-1, st_clauses-1
        elif isinstance(clause, Variable):
            if tf and clause==l:
                clauses.remove(clause)
                i, st_clauses = i-1, st_clauses-1
            elif clause == l:
                formula = F
                break
        elif isinstance(clause, Not):
            if tf and clause.x==l:
                formula = F
                break
            elif clause.x==l:
                clauses.remove(clause)
                i, st_clauses = i-1, st_clauses-1
    if st_clauses==0:
        return T
    else:
        return formula

<commit_msg>nekaj že dela :D<commit_after>from boolean import *

#predpostavljamo, da je formula podana v CNF obliki

def get_all_from_CNF(formula):
    if not isinstance(formula, And):
        return "formula ni podana v CNF obliki!"

def my_dpll(formula):
    valuation = dict()
    print(formula)
    if not isinstance(formula, And):
        return "formula ni podana v CNF obliki!"
    clauses = formula.terms
    print(clauses)
    #1. prazen seznam clausesov predstavlja vrednost T (satisfiable solution)
    if len(clauses)==0:
        return T
    #2. pregledamo, če je kateri izmed clausesov prazen, potem rešitve ni
    for clause in clauses:
        literals = clause.terms
        if len(literals) == 0:
            return F

def simplify_dpll(formula):
    pass

def simplify_unit_clause(formula, valuation):
    #TODO dodaj še spreminjanje spremenljivk glede na že znane vrednosti
    #TODO končaj program, če v slovar dodajaš drugačno vrednost, kot je že dodana
    clauses = formula.terms
    print(formula)
    st_clauses = len(clauses)
    i = 0
    while i < st_clauses:
        clause = clauses[i]
        i = i+1
        if isinstance(clause, Or):
            literals = clause.terms
            if len(literals)==1:
                clauses.remove(clause)
                clause = literals[0]
                if isinstance(clause, Variable):
                    valuation[clause.x]=True
                    simplify_by_literal(formula, clause.x, True)
                elif isinstance(clause, Not):
                    valuation[clause.x.x]=False
                    simplify_by_literal(formula, clause.x.x, False)
                simplify_unit_clause(formula, valuation)
                return valuation
        elif isinstance(clause, Variable):
            valuation[clause.x]=True
            clauses.remove(clause)
            simplify_by_literal(formula, clause.x, True)
            simplify_unit_clause(formula, valuation)
            return valuation
        elif isinstance(clause, Not):
            valuation[clause.x.x]=False
            clauses.remove(clause)
            i, st_clauses = i-1, st_clauses-1
            simplify_by_literal(formula, clause.x.x, False)
            simplify_unit_clause(formula, valuation)
            return valuation

def simplify_pore_literal(formula, valuation):
    #mogoče pojdi čez seznam vseh spremenljivk in označi, če je spremenljivka (nastavi na true),
    #če je negacija (nastavi na false) in preglejuj če se kdaj seka --> odstrani iz seznama pure
    #pure literal. Na začetku nastavi na none
    pass

def simplify_by_literal(formula, l, tf):
    print(formula)
    clauses = formula.terms
    st_clauses = len(clauses)
    i = 0
    while i < st_clauses:
        clause = clauses[i]
        i = i+1
        if isinstance(clause, Or):
            literals = clause.terms
            if len(literals)==0:
                formula = F
                break
            elif tf:
                if l in literals:
                    clauses.remove(clause)
                    i, st_clauses = i-1, st_clauses-1
                if Not(l) in literals:
                    if len(literals)==1:
                        formula = F
                        break
                    else:
                        literals.remove(Not(l))
                        if len(literals)==1:
                            formula = F
                            break
            else:
                if l in literals:
                    if len(literals)==1:
                        formula = F
                        break
                    else:
                        literals.remove(l)
                        if len(literals)==1:
                            formula = F
                            break
                if Not(l) in literals:
                    clauses.remove(clause)
                    i, st_clauses = i-1, st_clauses-1
        elif isinstance(clause, Variable):
            if tf and clause==l:
                clauses.remove(clause)
                i, st_clauses = i-1, st_clauses-1
            elif clause == l:
                formula = F
                break
        elif isinstance(clause, Not):
            if tf and clause.x==l:
                formula = F
                break
            elif clause.x==l:
                clauses.remove(clause)
                i, st_clauses = i-1, st_clauses-1
    if st_clauses==0:
        return T
    else:
        return formula

<|endoftext|>