<commit_before>import ast

class Type(object):
	def __eq__(self, other):
		return self.__class__ == other.__class__

class ReprId(object):

	def __hash__(self):
		return hash(repr(self))

	def __eq__(self, other):
		return repr(self) == repr(other)

	def __ne__(self, other):
		return not self.__eq__(other)

class base(ReprId):

	byval = False
	attribs = {}
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%' + self.name

	def __repr__(self):
		return '<type: %s>' % self.__class__.__name__

class trait(ReprId):

	byval = False
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%%%s.wrap' % self.name

	def __repr__(self):
		return '<trait: %s>' % self.name

class concrete(base):
	pass

class template(base):

	@property
	def ir(self):
		raise TypeError('not a concrete type')

	def __repr__(self):
		return '<template: %s>' % self.__class__.__name__

	def __getitem__(self, params):

		params = params if isinstance(params, tuple) else (params,)
		name = '%s[%s]' % (self.name, ', '.join(p.name for p in params))
		internal = name.replace('$', '_').replace('.', '_')
		cls = ALL[(self.name, params)] = type(internal, (concrete,), {
			'ir': '%' + self.name + '$' + '.'.join(t.name for t in params),
			'name': name,
			'methods': {},
			'attribs': {},
		})

		trans = {k: v for (k, v) in zip(self.params, params)}
		for k, v in self.attribs.iteritems():
			if isinstance(v[1], Stub):
				cls.attribs[k] = v[0], trans[v[1].name]
			elif isinstance(v[1], WRAPPERS) and isinstance(v[1].over, Stub):
				cls.attribs[k] = v[0], v[1].__class__(trans[v[1].over.name])
			else:
				cls.attribs[k] = v[0], v[1]

		return cls()

class void(base):
	ir = 'void'
	byval = True

class anyint(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

class anyfloat(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

BASIC = {
	'bool': 'i1',
	'byte': 'i8',
	'i32': 'i32',
	'u32': 'i32',
	'int': 'i64',
	'uint': 'i64',
}

INTEGERS = {
	'byte': (False, 8),
	'i32': (True, 32),
	'u32': (False, 32),
	'int': (True, 64),
	'uint': (False, 64),
}

class module(base):
	def __init__(self, path=None):
		self.path = path
		self.functions = {}

class owner(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '$%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: $%s>' % (self.over.name)

class ref(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '&%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: &%s>' % (self.over.name)

class function(base):

	def __init__(self, rtype, formal):
		self.over = rtype, formal

	def __repr__(self):
		if not self.over[1] or isinstance(self.over[1][0], tuple):
			formal = ['%r' % i[1] for i in self.over[1]]
		else:
			formal = ['%r' % i for i in self.over[1]]
		return '<fun %r <- [%s]>' % (self.over[0], ', '.join(formal))

	@property
	def ir(self):
		raise NotImplementedError

def unwrap(t):
	while isinstance(t, WRAPPERS):
		t = t.over
	return t

def generic(t):
	return isinstance(unwrap(t), (anyint, anyfloat))

def compat(a, f):

	if isinstance(a, (tuple, list)) and isinstance(f, (tuple, list)):
		if len(a) != len(f):
			return False
		if f and f[-1] == VarArgs():
			return all(compat(i[0], i[1]) for i in zip(a, f[:-1]))
		return all(compat(i[0], i[1]) for i in zip(a, f))

	if a == f:
		return True
	elif isinstance(a, anyint) and f in INTS:
		return True
	elif isinstance(a, ref) and isinstance(f, owner):
		return False
	elif isinstance(a, WRAPPERS) or isinstance(f, WRAPPERS):
		return compat(unwrap(a), unwrap(f))
	elif a in UINTS and f in UINTS:
		return a.bits < f.bits
	elif isinstance(f, trait):

		for k, (x, rt, atypes) in f.methods.iteritems():

			if k not in a.methods:
				return False

			rc = compat(a.methods[k][1], rt)
			aargs = [i[1] for i in a.methods[k][2][1:]]
			fargs = [i[1] for i in atypes[1:]]
			ac = compat(aargs, fargs)
			if not rc or not ac:
				return False

		return True

	else:
		return False

class Stub(object):
	def __init__(self, name):
		self.name = name
	def __repr__(self):
		return '<%s(%r)>' % (self.__class__.__name__, self.name)

class VarArgs(base):
	@property
	def ir(self):
		return '...'

def get(t, stubs={}):
	if t is None:
		return void()
	elif t == '...':
		return VarArgs()
	elif isinstance(t, base):
		return t
	elif isinstance(t, basestring) and t[0] == '$':
		return owner(get(t[1:], stubs))
	elif isinstance(t, basestring) and t[0] == '&':
		return ref(get(t[1:], stubs))
	elif isinstance(t, basestring):
		return stubs[t] if t in stubs else ALL[t]()
	elif isinstance(t, ast.Name):
		return stubs[t.name] if t.name in stubs else ALL[t.name]()
	elif isinstance(t, ast.Elem):
		return ALL[t.obj.name](get(t.key, stubs))
	elif isinstance(t, ast.Owner):
		return owner(get(t.value, stubs))
	elif isinstance(t, ast.Ref):
		return ref(get(t.value, stubs))
	else:
		assert False, 'no type %s' % t

ALL = {}
for k in globals().keys():
	obj = globals()[k]
	if type(obj) == type and base in obj.__bases__:
		ALL[k] = globals()[k]

for k, cls in ALL.iteritems():
	for m, mdata in cls.methods.iteritems():
		rtype = get(mdata[1])
		atypes = [(n, get(t)) for (n, t) in mdata[2]]
		cls.methods[m] = (m[0], rtype, atypes)

SINTS = {anyint()}
UINTS = set()
INTS = set()
FLOATS = {anyfloat()}
WRAPPERS = owner, ref

def add(node):

	if isinstance(node, ast.Trait):
		parent = trait
	elif node.params:
		parent = template
	else:
		parent = base

	if isinstance(node, ast.Trait):
		fields = {'methods': {}}
	else:
		fields = {'methods': {}, 'attribs': {}}

	ALL[node.name.name] = type(node.name.name, (parent,), fields)

def fill(node):

	cls = ALL[node.name.name]
	if node.name.name in BASIC:
		cls.ir = BASIC[node.name.name]
		cls.byval = True

	stubs = {}
	if not isinstance(node, ast.Trait):
		cls.params = tuple(n.name for n in node.params)
		stubs = {n.name: Stub(n.name) for n in node.params}
		for i, (atype, name) in enumerate(node.attribs):
			cls.attribs[name.name] = i, get(atype, stubs)

	for method in node.methods:

		name = method.name.name
		irname = '%s.%s' % (node.name.name, name)
		rtype = void() if method.rtype is None else get(method.rtype, stubs)

		args = []
		for i, arg in enumerate(method.args):
			if not i and arg.name.name == 'self':
				wrapper = owner if name == '__del__' else ref
				args.append(('self', wrapper(get(node.name, stubs))))
			else:
				args.append((arg.name.name, get(arg.type, stubs)))

		cls.methods[name] = irname, rtype, args
		method.irname = irname

	obj = cls()
	if node.name.name in INTEGERS:

		cls.signed, cls.bits = INTEGERS[node.name.name]
		INTS.add(obj)
		if cls.signed:
			SINTS.add(obj)
		else:
			UINTS.add(obj)

		ALL['anyint'].methods.update(cls.methods)

	return obj
<commit_msg>Include anyint type in INTS.<commit_after>import ast

class Type(object):
	def __eq__(self, other):
		return self.__class__ == other.__class__

class ReprId(object):

	def __hash__(self):
		return hash(repr(self))

	def __eq__(self, other):
		return repr(self) == repr(other)

	def __ne__(self, other):
		return not self.__eq__(other)

class base(ReprId):

	byval = False
	attribs = {}
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%' + self.name

	def __repr__(self):
		return '<type: %s>' % self.__class__.__name__

class trait(ReprId):

	byval = False
	methods = {}
	type = Type()

	@property
	def name(self):
		return self.__class__.__name__

	@property
	def ir(self):
		return '%%%s.wrap' % self.name

	def __repr__(self):
		return '<trait: %s>' % self.name

class concrete(base):
	pass

class template(base):

	@property
	def ir(self):
		raise TypeError('not a concrete type')

	def __repr__(self):
		return '<template: %s>' % self.__class__.__name__

	def __getitem__(self, params):

		params = params if isinstance(params, tuple) else (params,)
		name = '%s[%s]' % (self.name, ', '.join(p.name for p in params))
		internal = name.replace('$', '_').replace('.', '_')
		cls = ALL[(self.name, params)] = type(internal, (concrete,), {
			'ir': '%' + self.name + '$' + '.'.join(t.name for t in params),
			'name': name,
			'methods': {},
			'attribs': {},
		})

		trans = {k: v for (k, v) in zip(self.params, params)}
		for k, v in self.attribs.iteritems():
			if isinstance(v[1], Stub):
				cls.attribs[k] = v[0], trans[v[1].name]
			elif isinstance(v[1], WRAPPERS) and isinstance(v[1].over, Stub):
				cls.attribs[k] = v[0], v[1].__class__(trans[v[1].over.name])
			else:
				cls.attribs[k] = v[0], v[1]

		return cls()

class void(base):
	ir = 'void'
	byval = True

class anyint(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

class anyfloat(base):
	@property
	def ir(self):
		raise TypeError('not a concrete type')

BASIC = {
	'bool': 'i1',
	'byte': 'i8',
	'i32': 'i32',
	'u32': 'i32',
	'int': 'i64',
	'uint': 'i64',
}

INTEGERS = {
	'byte': (False, 8),
	'i32': (True, 32),
	'u32': (False, 32),
	'int': (True, 64),
	'uint': (False, 64),
}

class module(base):
	def __init__(self, path=None):
		self.path = path
		self.functions = {}

class owner(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '$%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: $%s>' % (self.over.name)

class ref(base):

	def __init__(self, over):
		self.over = over

	@property
	def name(self):
		return '&%s' % self.over.name

	@property
	def ir(self):
		return self.over.ir + '*'

	def __repr__(self):
		return '<type: &%s>' % (self.over.name)

class function(base):

	def __init__(self, rtype, formal):
		self.over = rtype, formal

	def __repr__(self):
		if not self.over[1] or isinstance(self.over[1][0], tuple):
			formal = ['%r' % i[1] for i in self.over[1]]
		else:
			formal = ['%r' % i for i in self.over[1]]
		return '<fun %r <- [%s]>' % (self.over[0], ', '.join(formal))

	@property
	def ir(self):
		raise NotImplementedError

def unwrap(t):
	while isinstance(t, WRAPPERS):
		t = t.over
	return t

def generic(t):
	return isinstance(unwrap(t), (anyint, anyfloat))

def compat(a, f):

	if isinstance(a, (tuple, list)) and isinstance(f, (tuple, list)):
		if len(a) != len(f):
			return False
		if f and f[-1] == VarArgs():
			return all(compat(i[0], i[1]) for i in zip(a, f[:-1]))
		return all(compat(i[0], i[1]) for i in zip(a, f))

	if a == f:
		return True
	elif isinstance(a, anyint) and f in INTS:
		return True
	elif isinstance(a, ref) and isinstance(f, owner):
		return False
	elif isinstance(a, WRAPPERS) or isinstance(f, WRAPPERS):
		return compat(unwrap(a), unwrap(f))
	elif a in UINTS and f in UINTS:
		return a.bits < f.bits
	elif isinstance(f, trait):

		for k, (x, rt, atypes) in f.methods.iteritems():

			if k not in a.methods:
				return False

			rc = compat(a.methods[k][1], rt)
			aargs = [i[1] for i in a.methods[k][2][1:]]
			fargs = [i[1] for i in atypes[1:]]
			ac = compat(aargs, fargs)
			if not rc or not ac:
				return False

		return True

	else:
		return False

class Stub(object):
	def __init__(self, name):
		self.name = name
	def __repr__(self):
		return '<%s(%r)>' % (self.__class__.__name__, self.name)

class VarArgs(base):
	@property
	def ir(self):
		return '...'

def get(t, stubs={}):
	if t is None:
		return void()
	elif t == '...':
		return VarArgs()
	elif isinstance(t, base):
		return t
	elif isinstance(t, basestring) and t[0] == '$':
		return owner(get(t[1:], stubs))
	elif isinstance(t, basestring) and t[0] == '&':
		return ref(get(t[1:], stubs))
	elif isinstance(t, basestring):
		return stubs[t] if t in stubs else ALL[t]()
	elif isinstance(t, ast.Name):
		return stubs[t.name] if t.name in stubs else ALL[t.name]()
	elif isinstance(t, ast.Elem):
		return ALL[t.obj.name](get(t.key, stubs))
	elif isinstance(t, ast.Owner):
		return owner(get(t.value, stubs))
	elif isinstance(t, ast.Ref):
		return ref(get(t.value, stubs))
	else:
		assert False, 'no type %s' % t

ALL = {}
for k in globals().keys():
	obj = globals()[k]
	if type(obj) == type and base in obj.__bases__:
		ALL[k] = globals()[k]

for k, cls in ALL.iteritems():
	for m, mdata in cls.methods.iteritems():
		rtype = get(mdata[1])
		atypes = [(n, get(t)) for (n, t) in mdata[2]]
		cls.methods[m] = (m[0], rtype, atypes)

SINTS = {anyint()}
UINTS = set()
INTS = {anyint()}
FLOATS = {anyfloat()}
WRAPPERS = owner, ref

def add(node):

	if isinstance(node, ast.Trait):
		parent = trait
	elif node.params:
		parent = template
	else:
		parent = base

	if isinstance(node, ast.Trait):
		fields = {'methods': {}}
	else:
		fields = {'methods': {}, 'attribs': {}}

	ALL[node.name.name] = type(node.name.name, (parent,), fields)

def fill(node):

	cls = ALL[node.name.name]
	if node.name.name in BASIC:
		cls.ir = BASIC[node.name.name]
		cls.byval = True

	stubs = {}
	if not isinstance(node, ast.Trait):
		cls.params = tuple(n.name for n in node.params)
		stubs = {n.name: Stub(n.name) for n in node.params}
		for i, (atype, name) in enumerate(node.attribs):
			cls.attribs[name.name] = i, get(atype, stubs)

	for method in node.methods:

		name = method.name.name
		irname = '%s.%s' % (node.name.name, name)
		rtype = void() if method.rtype is None else get(method.rtype, stubs)

		args = []
		for i, arg in enumerate(method.args):
			if not i and arg.name.name == 'self':
				wrapper = owner if name == '__del__' else ref
				args.append(('self', wrapper(get(node.name, stubs))))
			else:
				args.append((arg.name.name, get(arg.type, stubs)))

		cls.methods[name] = irname, rtype, args
		method.irname = irname

	obj = cls()
	if node.name.name in INTEGERS:

		cls.signed, cls.bits = INTEGERS[node.name.name]
		INTS.add(obj)
		if cls.signed:
			SINTS.add(obj)
		else:
			UINTS.add(obj)

		ALL['anyint'].methods.update(cls.methods)

	return obj
<|endoftext|>


<commit_before>"""rupypy"""

from __future__ import absolute_import

import os
import sys

from pypy.rlib.objectmodel import specialize
from pypy.rlib.streamio import open_file_as_stream

from rupypy.error import RubyError
from rupypy.executioncontext import ExecutionContext
from rupypy.objspace import ObjectSpace
from rupypy.utils import format_traceback

@specialize.memo()
def getspace():
    return ObjectSpace()

def entry_point(argv):
    if len(argv) != 2:
        print __doc__
        return 1

    f = open_file_as_stream(argv[1])
    try:
        source = f.readall()
    finally:
        f.close()

    space = getspace()
    ec = ExecutionContext(space)
    try:
        space.execute(ec, source, filepath=argv[1])
    except RubyError as e:
        lines = format_traceback(space, e.w_value)
        for line in lines:
            os.write(2, line)
        return 1
    return 0

if __name__ == "__main__":
    entry_point(sys.argv)
<commit_msg>print the arguments on bad args for now<commit_after>from __future__ import absolute_import

import os
import sys

from pypy.rlib.objectmodel import specialize
from pypy.rlib.streamio import open_file_as_stream

from rupypy.error import RubyError
from rupypy.executioncontext import ExecutionContext
from rupypy.objspace import ObjectSpace
from rupypy.utils import format_traceback

@specialize.memo()
def getspace():
    return ObjectSpace()

def entry_point(argv):
    if len(argv) != 2:
        print "Usage: %s <program>" % argv[0]
        print "Arguments: %s" % argv
        return 1

    f = open_file_as_stream(argv[1])
    try:
        source = f.readall()
    finally:
        f.close()

    space = getspace()
    ec = ExecutionContext(space)
    try:
        space.execute(ec, source, filepath=argv[1])
    except RubyError as e:
        lines = format_traceback(space, e.w_value)
        for line in lines:
            os.write(2, line)
        return 1
    return 0

if __name__ == "__main__":
    entry_point(sys.argv)
<|endoftext|>


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

class S7Comm:
    def __init__(self, address):
        self._address = address
        self._s7obj = ctypes.CDLL("libs7comm.so")

        c_address = ctypes.create_string_buffer("10.0.3.9")
        self._s7conn = self._s7obj.s7comm_connect(c_address)

    def _readWord(self, db, num, value):
        ret = self._s7obj.s7comm_read_word(self._s7conn, db, num, ctypes.byref(value));

        if ret != 0:
            raise "Aiii"

    def readInt16(self, db, num):
        value = ctypes.c_int16()
        self._readWord(db, num, value)
        return value.value

    def readUInt16(self, db, num):
        value = ctypes.c_uint16()
        self._readWord(db, num, value)
        return value.value
<commit_msg>Simple write word functions<commit_after>#!/usr/bin/env python
import ctypes

class S7Comm:
    def __init__(self, address):
        self._address = address
        self._s7obj = ctypes.CDLL("libs7comm.so")

        c_address = ctypes.create_string_buffer("10.0.3.9")
        self._s7conn = self._s7obj.s7comm_connect(c_address)

    def _readWord(self, db, num, value):
        ret = self._s7obj.s7comm_read_word(self._s7conn, db, num, ctypes.byref(value));

        if ret != 0:
            raise "Aiii"

    def readInt16(self, db, num):
        value = ctypes.c_int16()
        self._readWord(db, num, value)
        return value.value

    def readUInt16(self, db, num):
        value = ctypes.c_uint16()
        self._readWord(db, num, value)
        return value.value

    def _writeWord(self, db, num, value):
        ret = self._s7obj.s7comm_write_word(self._s7conn, db, num, value)

        if ret != 0:
            raise "Aiii"

    def writeUIn16(self, db, num, value):
        self._writeWord(db, num, value)

    def writeInt16(self, db, num, value):
        self._writeWord(db, num, value)
<|endoftext|>


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

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
#     Gabes Jean, naparuba@gmail.com
#     Gerhard Lausser, Gerhard.Lausser@consol.de
#     Gregory Starck, g.starck@gmail.com
#     Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

import time
import logging
import sys
from logging.handlers import TimedRotatingFileHandler

from brok import Brok

def is_tty():
    # Look if we are in a tty or not
    if hasattr(sys.stdout, 'isatty'):
        return sys.stdout.isatty()
    return False

if is_tty():
    # Try to load the terminal color. Won't work under python 2.4
    try:
        from shinken.misc.termcolor import cprint
    except (SyntaxError, ImportError), exp:
        # Outch can't import a cprint, do a simple print
        def cprint(s, color='', end=''):
            print s
# Ok it's a daemon mode, if so, just print
else:
    def cprint(s, color='', end=''):
        print s

obj = None
name = None
local_log = None
human_timestamp_log = False

class Log:
    """Shinken logger class, wrapping access to Python logging standard library."""
    "Store the numeric value from python logging class"
    NOTSET   = logging.NOTSET
    DEBUG    = logging.DEBUG
    INFO     = logging.INFO
    WARNING  = logging.WARNING
    ERROR    = logging.ERROR
    CRITICAL = logging.CRITICAL

    def __init__(self):
        self._level = logging.NOTSET
        self.display_time = True
        self.display_level = True
        self.log_colors = {Log.WARNING:'yellow', Log.CRITICAL:'magenta', Log.ERROR:'red'}

    def load_obj(self, object, name_=None):
        """ We load the object where we will put log broks
        with the 'add' method
        """
        global obj
        global name
        obj = object
        name = name_

    @staticmethod
    def get_level_id(lvlName):
        """Convert a level name (string) to its integer value
           and vice-versa. Input a level and it will return a name.
           Raise KeyError when name or level not found
        """
        return logging._levelNames[lvlName]

    # We can have level as an int (logging.INFO) or a string INFO
    # if string, try to get the int value
    def get_level(self):
        return logging.getLogger().getEffectiveLevel()

    # We can have level as an int (logging.INFO) or a string INFO
    # if string, try to get the int value
    def set_level(self, level):
        if not isinstance(level, int):
            level = getattr(logging, level, None)
            if not level or not isinstance(level, int):
                raise TypeError('log level must be an integer')

        self._level = level
        logging.getLogger().setLevel(level)

    def set_display_time(self, b):
        self.display_time = b

    def set_display_level(self, b):
        self.display_level = b

    def debug(self, msg, *args, **kwargs):
        self._log(logging.DEBUG, msg, *args, **kwargs)

    def info(self, msg, *args, **kwargs):
        self._log(logging.INFO, msg, *args, **kwargs)

    def warning(self, msg, *args, **kwargs):
        self._log(logging.WARNING, msg, *args, **kwargs)

    def error(self, msg, *args, **kwargs):
        self._log(logging.ERROR, msg, *args, **kwargs)

    def critical(self, msg, *args, **kwargs):
        self._log(logging.CRITICAL, msg, *args, **kwargs)

    def log(self, message, format=None, print_it=True):
        """Old log method, kept for NAGIOS compatibility
        What strings should not use the new format ??"""
        self._log(logging.INFO, message, format, print_it, display_level=False)

    def _log(self, level, message, format=None, print_it=True, display_level=True):
        """We enter a log message, we format it, and we add the log brok"""
        global obj
        global name
        global local_log
        global human_timestamp_log

        # ignore messages when message level is lower than Log level
        if level < self._level:
            return

        # display the level only if we ask locally and globaly
        display_level = display_level & self.display_level

        # We format the log in UTF-8
        if isinstance(message, str):
            message = message.decode('UTF-8', 'replace')

        if format is None:
            lvlname = logging.getLevelName(level)

            if display_level:
                if self.display_time:
                    fmt = u'[%(date)s] %(level)-9s %(name)s%(msg)s\n'
                else:
                    fmt = u'%(level)-9s %(name)s%(msg)s\n'
            else:
                if self.display_time:
                    fmt = u'[%(date)s] %(name)s%(msg)s\n'
                else:
                    fmt = u'%(name)s%(msg)s\n'

            args = {
                'date': (human_timestamp_log and time.asctime()
                         or int(time.time())),
                'level': lvlname.capitalize()+' :',
                'name': name and ('[%s] ' % name) or '',
                'msg': message
            }
            s = fmt % args
        else:
            s = format % message

        if print_it and len(s) > 1:
            # Take a color so we can print if it's a TTY
            if is_tty():
                color = self.log_colors.get(level, None)
            else:
                color = None

            # Print to standard output.
            # If the daemon is launched with a non UTF8 shell
            # we can have problems in printing, work around it.
            try:
                cprint(s[:-1], color)
            except UnicodeEncodeError:
                print s.encode('ascii', 'ignore')

        # We create the brok and load the log message
        # DEBUG level logs are logged by the daemon locally
        # and must not be forwarded to other satellites, or risk overloading them.
        if level != logging.DEBUG and obj and hasattr(obj, 'add'):
            b = Brok('log', {'log': s})
            obj.add(b)

        # If local logging is enabled, log to the defined handler, file.
        if local_log is not None:
            logging.log(level, s.strip())

    def register_local_log(self, path, level=None):
        """The shinken logging wrapper can write to a local file if needed
        and return the file descriptor so we can avoid to
        close it.
        """
        global local_log

        if level is not None:
            self._level = level

        # Open the log and set to rotate once a day
        basic_log_handler = TimedRotatingFileHandler(path,
                                                     'midnight',
                                                     backupCount=5)
        basic_log_handler.setLevel(self._level)
        basic_log_formatter = logging.Formatter('%(asctime)s %(message)s')
        basic_log_handler.setFormatter(basic_log_formatter)
        logger = logging.getLogger()
        logger.addHandler(basic_log_handler)
        logger.setLevel(self._level)
        local_log = basic_log_handler

        # Return the file descriptor of this file
        return basic_log_handler.stream.fileno()

    def quit(self):
        """Close the local log file at program exit"""
        global local_log
        if local_log:
            self.debug("Closing %s local_log" % str(local_log))
            local_log.close()

    def set_human_format(self, on=True):
        """
        Set the output as human format.

        If the optional parameter `on` is False, the timestamps format
        will be reset to the default format.
        """
        global human_timestamp_log
        human_timestamp_log = bool(on)

logger = Log()

class __ConsoleLogger:
    """
    This wrapper class for logging and printing messages to stdout, too.

    :fixme: Implement this using an additional stream-handler, as soon
    as the logging system is based on the standard Python logging
    module.
    """
    def debug(self, msg, *args, **kwargs):
        self._log(Log.DEBUG, msg, *args, **kwargs)

    def info(self, msg, *args, **kwargs):
        kwargs.setdefault('display_level', False)
        self._log(Log.INFO, msg, *args, **kwargs)

    def warning(self, msg, *args, **kwargs):
        self._log(Log.WARNING, msg, *args, **kwargs)

    def error(self, msg, *args, **kwargs):
        self._log(Log.ERROR, msg, *args, **kwargs)

    def critical(self, msg, *args, **kwargs):
        self._log(Log.CRITICAL, msg, *args, **kwargs)

    def alert(self, msg, *args, **kwargs):
        kwargs.setdefault('display_level', False)
        self._log(Log.CRITICAL, msg, *args, **kwargs)

    def _log(self, *args, **kwargs):
        # if `print_it` is not passed as an argument, set it to `true`
        kwargs.setdefault('print_it', True)
        logger._log(*args, **kwargs)

console_logger = __ConsoleLogger()
<commit_msg>Enh : Start to merge logging PR<commit_after>#!/usr/bin/env python

# -*- coding: utf-8 -*-

# Copyright (C) 2009-2012:
#     Gabes Jean, naparuba@gmail.com
#     Gerhard Lausser, Gerhard.Lausser@consol.de
#     Gregory Starck, g.starck@gmail.com
#     Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken.  If not, see <http://www.gnu.org/licenses/>.

import time
import logging
import sys
from logging import Handler, Formatter, StreamHandler
from logging.handlers import TimedRotatingFileHandler

from brok import Brok

def is_tty():
    # Look if we are in a tty or not
    if hasattr(sys.stdout, 'isatty'):
        return sys.stdout.isatty()
    return False

if is_tty():
    # Try to load the terminal color. Won't work under python 2.4
    try:
        from shinken.misc.termcolor import cprint
    except (SyntaxError, ImportError), exp:
        # Outch can't import a cprint, do a simple print
        def cprint(s, color='', end=''):
            print s
# Ok it's a daemon mode, if so, just print
else:
    def cprint(s, color='', end=''):
        print s

obj = None
name = None
local_log = None
human_timestamp_log = False

class Log(logging.Logger):
    """Shinken logger class, wrapping access to Python logging standard library."""
    "Store the numeric value from python logging class"
    NOTSET   = logging.NOTSET
    DEBUG    = logging.DEBUG
    INFO     = logging.INFO
    WARNING  = logging.WARNING
    ERROR    = logging.ERROR
    CRITICAL = logging.CRITICAL

    def __init__(self, name='shinken', level=NOTSET):
        logging.Logger.__init__(self, name, level)
        self.display_time = True
        self.display_level = True
        self.log_colors = {Log.WARNING:'yellow', Log.CRITICAL:'magenta', Log.ERROR:'red'}

    def load_obj(self, object, name_=None):
        """ We load the object where we will put log broks
        with the 'add' method
        """
        global obj
        global name
        obj = object
        name = name_

    @staticmethod
    def get_level_id(lvlName):
        """Convert a level name (string) to its integer value
           and vice-versa. Input a level and it will return a name.
           Raise KeyError when name or level not found
        """
        return logging._levelNames[lvlName]

    # We can have level as an int (logging.INFO) or a string INFO
    # if string, try to get the int value
    def get_level(self):
        return logging.getLogger().getEffectiveLevel()

    # We can have level as an int (logging.INFO) or a string INFO
    # if string, try to get the int value
    def set_level(self, level):
        if not isinstance(level, int):
            level = getattr(logging, level, None)
            if not level or not isinstance(level, int):
                raise TypeError('log level must be an integer')

        self._level = level
        logging.getLogger().setLevel(level)

    def register_local_log(self, path, level=None):
        """The shinken logging wrapper can write to a local file if needed
        and return the file descriptor so we can avoid to
        close it.
        """
        global local_log

        if level is not None:
            self._level = level

        # Open the log and set to rotate once a day
        basic_log_handler = TimedRotatingFileHandler(path,
                                                     'midnight',
                                                     backupCount=5)
        basic_log_handler.setLevel(self._level)
        basic_log_formatter = logging.Formatter('%(asctime)s %(message)s')
        basic_log_handler.setFormatter(basic_log_formatter)
        logger = logging.getLogger()
        logger.addHandler(basic_log_handler)
        logger.setLevel(self._level)
        local_log = basic_log_handler

        # Return the file descriptor of this file
        return basic_log_handler.stream.fileno()

    def quit(self):
        """Close the local log file at program exit"""
        global local_log
        if local_log:
            self.debug("Closing %s local_log" % str(local_log))
            local_log.close()

    def set_human_format(self, on=True):
        """
        Set the output as human format.

        If the optional parameter `on` is False, the timestamps format
        will be reset to the default format.
        """
        global human_timestamp_log
        human_timestamp_log = bool(on)

logger = Log()

class __ConsoleLogger:
    """
    This wrapper class for logging and printing messages to stdout, too.

    :fixme: Implement this using an additional stream-handler, as soon
    as the logging system is based on the standard Python logging
    module.
    """
    def debug(self, msg, *args, **kwargs):
        self._log(Log.DEBUG, msg, *args, **kwargs)

    def info(self, msg, *args, **kwargs):
        kwargs.setdefault('display_level', False)
        self._log(Log.INFO, msg, *args, **kwargs)

    def warning(self, msg, *args, **kwargs):
        self._log(Log.WARNING, msg, *args, **kwargs)

    def error(self, msg, *args, **kwargs):
        self._log(Log.ERROR, msg, *args, **kwargs)

    def critical(self, msg, *args, **kwargs):
        self._log(Log.CRITICAL, msg, *args, **kwargs)

    def alert(self, msg, *args, **kwargs):
        kwargs.setdefault('display_level', False)
        self._log(Log.CRITICAL, msg, *args, **kwargs)

    def _log(self, *args, **kwargs):
        # if `print_it` is not passed as an argument, set it to `true`
        kwargs.setdefault('print_it', True)
        logger._log(*args, **kwargs)

console_logger = __ConsoleLogger()
<|endoftext|>


<commit_before>'''
This module contains all the stuff to make your way from python code to
a dynamic library, see __init__.py for exported interfaces.
'''
import sys
import os.path
import sysconfig
import shutil
import logging
logger = logging.getLogger(__name__)

from cxxgen import *
import ast
from middlend import refine
from backend import Cxx
from syntax import check_syntax
from passes import NormalizeIdentifiers
from openmp import GatherOMPData
from config import cfg
from passmanager import PassManager
from numpy import get_include
from typing import extract_constructed_types, pytype_to_ctype
from tables import pythran_ward, functions
from intrinsic import ConstExceptionIntr

from os import devnull
from subprocess import check_call, check_output, STDOUT, CalledProcessError
from tempfile import mkstemp, NamedTemporaryFile
import networkx as nx

def _format_cmdline(cmd):
    """No comma when printing a command line allows for copy/paste"""
    return "'" + "' '".join(cmd) + "'"

def _extract_all_constructed_types(v):
    return sorted(set(reduce(lambda x, y: x + y,
                            (extract_constructed_types(t) for t in v), [])),
                  key=len)

def _parse_optimization(optimization):
    '''Turns an optimization of the form
        my_optim
        my_package.my_optim
        into the associated symbol'''
    splitted = optimization.split('.')
    if len(splitted) == 1:
        splitted = ['pythran', 'optimizations'] + splitted
    return reduce(getattr, splitted[1:], __import__(splitted[0]))

def _python_cppflags():
    return ["-I" + sysconfig.get_config_var("INCLUDEPY")]

def _numpy_cppflags():
    return ["-I" + os.path.join(get_include(), 'numpy')]

def _pythran_cppflags():
    curr_dir = os.path.dirname(os.path.dirname(__file__))
    get = lambda *x: '-I' + os.path.join(curr_dir, *x)
    return [get('.'), get('pythran'), get('pythran', 'pythonic++')]

def _python_ldflags():
    return ["-L" + sysconfig.get_config_var("LIBPL"),
            "-lpython" + sysconfig.get_config_var('VERSION')]

def _get_temp(content, suffix=".cpp"):
    '''Get a temporary file for given content, default extension is .cpp
       It is user's responsability to delete when done.'''
    fd, fdpath = mkstemp(suffix)
    with os.fdopen(fd, "w") as cpp:
        cpp.write(content)
    return fd, fdpath

class HasArgument(ast.NodeVisitor):
    '''Checks if a given function has arguments'''
    def __init__(self, fname):
        self.fname = fname

    def visit_Module(self, node):
        for n in node.body:
            if type(n) is ast.FunctionDef and n.name == self.fname:
                return len(n.args.args) > 0
        return False

# PUBLIC INTERFACE STARTS HERE

class CompileError(Exception):
    """ Holds an exception when the C++ compilation failed"""

    def __init__(self, cmdline, output):
            self.cmdline = _format_cmdline(cmdline)
            self.output = output
            self._message = "\n".join(["Compile error!\n",
                                       "******** Command line was: ********",
                                       self.cmdline,
                                       "\n******** Output :  ********\n",
                                       self.output])
            super(CompileError, self).__init__(self._message)

def cxxflags():
    """The C++ flags to compile a Pythran generated cpp file"""
    return (cfg.get('user', 'cxxflags').split() +
            cfg.get('sys', 'cxxflags').split())

def cppflags():
    """The C++ flags to preprocess a Pythran generated cpp file"""
    return (_python_cppflags() +
            _numpy_cppflags() +
            _pythran_cppflags() +
            cfg.get('sys', 'cppflags').split() +
            cfg.get('user', 'cppflags').split())

def ldflags():
    """The linker flags to link a Pythran code into a shared library"""
    return (_python_ldflags() +
            cfg.get('sys', 'ldflags').split() +
            cfg.get('user', 'ldflags').split())

def generate_cxx(module_name, code, specs=None, optimizations=None):
    '''python + pythran spec -> c++ code
    returns a BoostPythonModule object

    '''
    pm = PassManager(module_name)
    # font end
    ir = ast.parse(code)

    # parse openmp directive
    pm.apply(GatherOMPData, ir)

    # avoid conflicts with cxx keywords
    renamings = pm.apply(NormalizeIdentifiers, ir)
    check_syntax(ir)

    # middle-end
    optimizations = (optimizations or
                     cfg.get('pythran', 'optimizations').split())
    optimizations = map(_parse_optimization, optimizations)
    refine(pm, ir, optimizations)

    # back-end
    content = pm.dump(Cxx, ir)

    if not specs:  # Match "None" AND empty specs
        class Generable:
            def __init__(self, content):
                self.content = content

            def generate(self):
                return "\n".join("\n".join(l for l in s.generate())
                                 for s in self.content)
        mod = Generable(content)
    else:
        # uniform typing
        for fname, signatures in specs.items():
            if not isinstance(signatures, tuple):
                specs[fname] = (signatures,)

        mod = BoostPythonModule(module_name)
        mod.use_private_namespace = False
        # very low value for max_arity leads to various bugs
        max_arity = max(4, max(max(map(len, s)) for s in specs.itervalues()))
        mod.add_to_preamble([Define("BOOST_PYTHON_MAX_ARITY", max_arity)])
        mod.add_to_preamble([Define("BOOST_SIMD_NO_STRICT_ALIASING", "1")])
        mod.add_to_preamble(content)
        mod.add_to_init([
            Statement('import_array()'),
            Statement('boost::python::implicitly_convertible<std::string,'
                      + 'pythonic::core::string>()')]
        )

        # add topologically sorted exceptions based on the inheritance hierarchy.
        # needed because otherwise boost python regiser_exception handlers do not
        # catch exception type in the right way (first valid exception is selected,
        # so inheritance has to be taken into account in the registration order)
        exceptions = nx.DiGraph()
        for function_name, v in functions.iteritems():
            for mname, symbol in v:
                if isinstance(symbol, ConstExceptionIntr):
                    exceptions.add_node(
                        getattr(sys.modules[mname], function_name))

        # add edges based on class relationships
        for n in exceptions:
            if n.__base__ in exceptions:
                exceptions.add_edge(n.__base__, n)

        sorted_exceptions = nx.topological_sort(exceptions)
        mod.add_to_init([
            Statement(
                'boost::python::register_exception_translator<' +
                'pythonic::core::%s>(&translate_%s)' %
                (n.__name__, n.__name__)) for n in sorted_exceptions])

        for function_name, signatures in specs.iteritems():
            internal_func_name = renamings.get(function_name,
                                               function_name)
            for sigid, signature in enumerate(signatures):
                numbered_function_name = "{0}{1}".format(internal_func_name,
                                                         sigid)
                arguments_types = [pytype_to_ctype(t) for t in signature]
                has_arguments = HasArgument(internal_func_name).visit(ir)
                arguments = ["a{0}".format(i)
                             for i in xrange(len(arguments_types))]
                name_fmt = pythran_ward + "{0}::{1}::type{2}"
                args_list = ", ".join(arguments_types)
                specialized_fname = name_fmt.format(module_name,
                                                    internal_func_name,
                                                    "<{0}>".format(args_list)
                                                    if has_arguments else "")
                result_type = ("typename std::remove_reference"
                               + "<typename {0}::result_type>::type".format(
                                 specialized_fname))
                mod.add_to_init(
                    [Statement("python_to_pythran<{0}>()".format(t))
                     for t in _extract_all_constructed_types(signature)])
                mod.add_to_init([Statement(
                    "pythran_to_python<{0}>()".format(result_type))])
                mod.add_function(
                    FunctionBody(
                        FunctionDeclaration(
                            Value(
                                result_type,
                                numbered_function_name),
                            [Value(t, a)
                             for t, a in zip(arguments_types, arguments)]),
                        Block([Statement("return {0}()({1})".format(
                            pythran_ward + '{0}::{1}'.format(
                                module_name, internal_func_name),
                            ', '.join(arguments)))])
                    ),
                    function_name
                )
    return mod

def compile_cxxfile(cxxfile, module_so=None, **kwargs):
    '''c++ file -> native module
    Return the filename of the produced shared library
    Raises CompileError on failure

    '''
    # FIXME: not sure about overriding the user defined compiler here...
    compiler = kwargs.get('cxx', cfg.get('user', 'cxx'))

    _cppflags = cppflags() + kwargs.get('cppflags', [])
    _cxxflags = cxxflags() + kwargs.get('cxxflags', [])
    _ldflags = ldflags() + kwargs.get('ldflags', [])

    # workaround g++-4.8 bug
    try:
        extra_flags = ['-ftrack-macro-expansion=0']
        check_call([compiler, cxxfile, '-E'] + extra_flags,
                     stdout=file(devnull), stderr=file(devnull))
        _cppflags.extend(extra_flags)
    except CalledProcessError:
        pass

    # Get output filename from input filename if not set
    module_so = module_so or (os.path.splitext(cxxfile)[0] + ".so")
    try:
        cmd = ([compiler, cxxfile]
               + _cppflags
               + _cxxflags
               + ["-shared", "-o", module_so]
               + _ldflags)
        logger.info("Command line: " + _format_cmdline(cmd))
        output = check_output(cmd, stderr=STDOUT)
    except CalledProcessError as e:
        raise CompileError(e.cmd, e.output)
    logger.info("Generated module: " + module_so)
    logger.info("Output: " + output)

    return module_so

def compile_cxxcode(cxxcode, module_so=None, keep_temp=False,
                    **kwargs):
    '''c++ code (string) -> temporary file -> native module.
    Returns the generated .so.

    '''

    # Get a temporary C++ file to compile
    fd, fdpath = _get_temp(cxxcode)
    module_so = compile_cxxfile(fdpath, module_so, **kwargs)
    if not keep_temp:
        # remove tempfile
        os.remove(fdpath)
    else:
        logger.warn("Keeping temporary generated file:" + fdpath)

    return module_so

def compile_pythrancode(module_name, pythrancode, specs=None,
                        opts=None, cpponly=False, module_so=None,
                        **kwargs):
    '''Pythran code (string) -> c++ code -> native module
    Returns the generated .so (or .cpp if `cpponly` is set to true).

    '''

    # Autodetect the Pythran spec if not given as parameter
    from spec import spec_parser
    specs = spec_parser(pythrancode) if specs is None else specs

    # Generate C++, get a BoostPythonModule object
    module = generate_cxx(module_name, pythrancode, specs, opts)

    if cpponly:
        # User wants only the C++ code
        _, output_file = _get_temp(str(module.generate()))
        if module_so:
            shutil.move(output_file, module_so)
            output_file = module_so
        logger.info("Generated C++ source file: " + output_file)
    else:
        # Compile to binary
        output_file = compile_cxxcode(str(module.generate()),
                                      module_so=module_so,
                                      **kwargs)

    return output_file

def compile_pythranfile(file_path, module_so=None, module_name=None,
                        cpponly=False, **kwargs):
    '''Pythran file -> c++ file -> native module
    Returns the generated .so (or .cpp if `cpponly` is set to true).

    '''
    if not module_so:
        # derive module name from input file name
        basedir, basename = os.path.split(file_path)
        module_name = module_name or os.path.splitext(basename)[0]

        # derive destination from file name
        module_so = os.path.join(basedir, module_name + ".so")
    else:
        # derive module name from destination module_so name
        _, basename = os.path.split(module_so)
        module_name = module_name or os.path.splitext(basename)[0]

    dl = compile_pythrancode(module_name, file(file_path).read(),
                             module_so=module_so, cpponly=cpponly, **kwargs)
    return module_so

def test_compile():
    '''Simple passthrough compile test.
    May raises CompileError Exception.

    '''
    module_so = compile_cxxcode("\n".join([
        "#define BOOST_PYTHON_MAX_ARITY 4",
        "#include <pythran/pythran.h>"
        ]))
    module_so and os.remove(module_so)
<commit_msg>PEP8 compliancy<commit_after>'''
This module contains all the stuff to make your way from python code to
a dynamic library, see __init__.py for exported interfaces.
'''
import sys
import os.path
import sysconfig
import shutil
import logging
logger = logging.getLogger(__name__)

from cxxgen import *
import ast
from middlend import refine
from backend import Cxx
from syntax import check_syntax
from passes import NormalizeIdentifiers
from openmp import GatherOMPData
from config import cfg
from passmanager import PassManager
from numpy import get_include
from typing import extract_constructed_types, pytype_to_ctype
from tables import pythran_ward, functions
from intrinsic import ConstExceptionIntr

from os import devnull
from subprocess import check_call, check_output, STDOUT, CalledProcessError
from tempfile import mkstemp, NamedTemporaryFile
import networkx as nx

def _format_cmdline(cmd):
    """No comma when printing a command line allows for copy/paste"""
    return "'" + "' '".join(cmd) + "'"

def _extract_all_constructed_types(v):
    return sorted(set(reduce(lambda x, y: x + y,
                            (extract_constructed_types(t) for t in v), [])),
                  key=len)

def _parse_optimization(optimization):
    '''Turns an optimization of the form
        my_optim
        my_package.my_optim
        into the associated symbol'''
    splitted = optimization.split('.')
    if len(splitted) == 1:
        splitted = ['pythran', 'optimizations'] + splitted
    return reduce(getattr, splitted[1:], __import__(splitted[0]))

def _python_cppflags():
    return ["-I" + sysconfig.get_config_var("INCLUDEPY")]

def _numpy_cppflags():
    return ["-I" + os.path.join(get_include(), 'numpy')]

def _pythran_cppflags():
    curr_dir = os.path.dirname(os.path.dirname(__file__))
    get = lambda *x: '-I' + os.path.join(curr_dir, *x)
    return [get('.'), get('pythran'), get('pythran', 'pythonic++')]

def _python_ldflags():
    return ["-L" + sysconfig.get_config_var("LIBPL"),
            "-lpython" + sysconfig.get_config_var('VERSION')]

def _get_temp(content, suffix=".cpp"):
    '''Get a temporary file for given content, default extension is .cpp
       It is user's responsability to delete when done.'''
    fd, fdpath = mkstemp(suffix)
    with os.fdopen(fd, "w") as cpp:
        cpp.write(content)
    return fd, fdpath

class HasArgument(ast.NodeVisitor):
    '''Checks if a given function has arguments'''
    def __init__(self, fname):
        self.fname = fname

    def visit_Module(self, node):
        for n in node.body:
            if type(n) is ast.FunctionDef and n.name == self.fname:
                return len(n.args.args) > 0
        return False

# PUBLIC INTERFACE STARTS HERE

class CompileError(Exception):
    """ Holds an exception when the C++ compilation failed"""

    def __init__(self, cmdline, output):
            self.cmdline = _format_cmdline(cmdline)
            self.output = output
            self._message = "\n".join(["Compile error!\n",
                                       "******** Command line was: ********",
                                       self.cmdline,
                                       "\n******** Output :  ********\n",
                                       self.output])
            super(CompileError, self).__init__(self._message)

def cxxflags():
    """The C++ flags to compile a Pythran generated cpp file"""
    return (cfg.get('user', 'cxxflags').split() +
            cfg.get('sys', 'cxxflags').split())

def cppflags():
    """The C++ flags to preprocess a Pythran generated cpp file"""
    return (_python_cppflags() +
            _numpy_cppflags() +
            _pythran_cppflags() +
            cfg.get('sys', 'cppflags').split() +
            cfg.get('user', 'cppflags').split())

def ldflags():
    """The linker flags to link a Pythran code into a shared library"""
    return (_python_ldflags() +
            cfg.get('sys', 'ldflags').split() +
            cfg.get('user', 'ldflags').split())

def generate_cxx(module_name, code, specs=None, optimizations=None):
    '''python + pythran spec -> c++ code
    returns a BoostPythonModule object

    '''
    pm = PassManager(module_name)
    # font end
    ir = ast.parse(code)

    # parse openmp directive
    pm.apply(GatherOMPData, ir)

    # avoid conflicts with cxx keywords
    renamings = pm.apply(NormalizeIdentifiers, ir)
    check_syntax(ir)

    # middle-end
    optimizations = (optimizations or
                     cfg.get('pythran', 'optimizations').split())
    optimizations = map(_parse_optimization, optimizations)
    refine(pm, ir, optimizations)

    # back-end
    content = pm.dump(Cxx, ir)

    if not specs:  # Match "None" AND empty specs
        class Generable:
            def __init__(self, content):
                self.content = content

            def generate(self):
                return "\n".join("\n".join(l for l in s.generate())
                                 for s in self.content)
        mod = Generable(content)
    else:
        # uniform typing
        for fname, signatures in specs.items():
            if not isinstance(signatures, tuple):
                specs[fname] = (signatures,)

        mod = BoostPythonModule(module_name)
        mod.use_private_namespace = False
        # very low value for max_arity leads to various bugs
        max_arity = max(4, max(max(map(len, s)) for s in specs.itervalues()))
        mod.add_to_preamble([Define("BOOST_PYTHON_MAX_ARITY", max_arity)])
        mod.add_to_preamble([Define("BOOST_SIMD_NO_STRICT_ALIASING", "1")])
        mod.add_to_preamble(content)
        mod.add_to_init([
            Statement('import_array()'),
            Statement('boost::python::implicitly_convertible<std::string,'
                      + 'pythonic::core::string>()')]
        )

        # topologically sorted exceptions based on the inheritance hierarchy.
        # needed because otherwise boost python register_exception handlers
        # do not catch exception type in the right way
        # (first valid exception is selected)
        # Inheritance has to be taken into account in the registration order.
        exceptions = nx.DiGraph()
        for function_name, v in functions.iteritems():
            for mname, symbol in v:
                if isinstance(symbol, ConstExceptionIntr):
                    exceptions.add_node(
                        getattr(sys.modules[mname], function_name))

        # add edges based on class relationships
        for n in exceptions:
            if n.__base__ in exceptions:
                exceptions.add_edge(n.__base__, n)

        sorted_exceptions = nx.topological_sort(exceptions)
        mod.add_to_init([
            Statement(
                'boost::python::register_exception_translator<' +
                'pythonic::core::%s>(&translate_%s)' %
                (n.__name__, n.__name__)) for n in sorted_exceptions])

        for function_name, signatures in specs.iteritems():
            internal_func_name = renamings.get(function_name,
                                               function_name)
            for sigid, signature in enumerate(signatures):
                numbered_function_name = "{0}{1}".format(internal_func_name,
                                                         sigid)
                arguments_types = [pytype_to_ctype(t) for t in signature]
                has_arguments = HasArgument(internal_func_name).visit(ir)
                arguments = ["a{0}".format(i)
                             for i in xrange(len(arguments_types))]
                name_fmt = pythran_ward + "{0}::{1}::type{2}"
                args_list = ", ".join(arguments_types)
                specialized_fname = name_fmt.format(module_name,
                                                    internal_func_name,
                                                    "<{0}>".format(args_list)
                                                    if has_arguments else "")
                result_type = ("typename std::remove_reference"
                               + "<typename {0}::result_type>::type".format(
                                 specialized_fname))
                mod.add_to_init(
                    [Statement("python_to_pythran<{0}>()".format(t))
                     for t in _extract_all_constructed_types(signature)])
                mod.add_to_init([Statement(
                    "pythran_to_python<{0}>()".format(result_type))])
                mod.add_function(
                    FunctionBody(
                        FunctionDeclaration(
                            Value(
                                result_type,
                                numbered_function_name),
                            [Value(t, a)
                             for t, a in zip(arguments_types, arguments)]),
                        Block([Statement("return {0}()({1})".format(
                            pythran_ward + '{0}::{1}'.format(
                                module_name, internal_func_name),
                            ', '.join(arguments)))])
                    ),
                    function_name
                )
    return mod

def compile_cxxfile(cxxfile, module_so=None, **kwargs):
    '''c++ file -> native module
    Return the filename of the produced shared library
    Raises CompileError on failure

    '''
    # FIXME: not sure about overriding the user defined compiler here...
    compiler = kwargs.get('cxx', cfg.get('user', 'cxx'))

    _cppflags = cppflags() + kwargs.get('cppflags', [])
    _cxxflags = cxxflags() + kwargs.get('cxxflags', [])
    _ldflags = ldflags() + kwargs.get('ldflags', [])

    # workaround g++-4.8 bug
    try:
        extra_flags = ['-ftrack-macro-expansion=0']
        check_call([compiler, cxxfile, '-E'] + extra_flags,
                     stdout=file(devnull), stderr=file(devnull))
        _cppflags.extend(extra_flags)
    except CalledProcessError:
        pass

    # Get output filename from input filename if not set
    module_so = module_so or (os.path.splitext(cxxfile)[0] + ".so")
    try:
        cmd = ([compiler, cxxfile]
               + _cppflags
               + _cxxflags
               + ["-shared", "-o", module_so]
               + _ldflags)
        logger.info("Command line: " + _format_cmdline(cmd))
        output = check_output(cmd, stderr=STDOUT)
    except CalledProcessError as e:
        raise CompileError(e.cmd, e.output)
    logger.info("Generated module: " + module_so)
    logger.info("Output: " + output)

    return module_so

def compile_cxxcode(cxxcode, module_so=None, keep_temp=False,
                    **kwargs):
    '''c++ code (string) -> temporary file -> native module.
    Returns the generated .so.

    '''

    # Get a temporary C++ file to compile
    fd, fdpath = _get_temp(cxxcode)
    module_so = compile_cxxfile(fdpath, module_so, **kwargs)
    if not keep_temp:
        # remove tempfile
        os.remove(fdpath)
    else:
        logger.warn("Keeping temporary generated file:" + fdpath)

    return module_so

def compile_pythrancode(module_name, pythrancode, specs=None,
                        opts=None, cpponly=False, module_so=None,
                        **kwargs):
    '''Pythran code (string) -> c++ code -> native module
    Returns the generated .so (or .cpp if `cpponly` is set to true).

    '''

    # Autodetect the Pythran spec if not given as parameter
    from spec import spec_parser
    specs = spec_parser(pythrancode) if specs is None else specs

    # Generate C++, get a BoostPythonModule object
    module = generate_cxx(module_name, pythrancode, specs, opts)

    if cpponly:
        # User wants only the C++ code
        _, output_file = _get_temp(str(module.generate()))
        if module_so:
            shutil.move(output_file, module_so)
            output_file = module_so
        logger.info("Generated C++ source file: " + output_file)
    else:
        # Compile to binary
        output_file = compile_cxxcode(str(module.generate()),
                                      module_so=module_so,
                                      **kwargs)

    return output_file

def compile_pythranfile(file_path, module_so=None, module_name=None,
                        cpponly=False, **kwargs):
    '''Pythran file -> c++ file -> native module
    Returns the generated .so (or .cpp if `cpponly` is set to true).

    '''
    if not module_so:
        # derive module name from input file name
        basedir, basename = os.path.split(file_path)
        module_name = module_name or os.path.splitext(basename)[0]

        # derive destination from file name
        module_so = os.path.join(basedir, module_name + ".so")
    else:
        # derive module name from destination module_so name
        _, basename = os.path.split(module_so)
        module_name = module_name or os.path.splitext(basename)[0]

    dl = compile_pythrancode(module_name, file(file_path).read(),
                             module_so=module_so, cpponly=cpponly, **kwargs)
    return module_so

def test_compile():
    '''Simple passthrough compile test.
    May raises CompileError Exception.

    '''
    module_so = compile_cxxcode("\n".join([
        "#define BOOST_PYTHON_MAX_ARITY 4",
        "#include <pythran/pythran.h>"
        ]))
    module_so and os.remove(module_so)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Copyright (c) 2014-2015 Daniel Kraft
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# Test the "getauxblock" merge-mining RPC interface.

# Add python-bitcoinrpc to module search path:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "python-bitcoinrpc"))

from bitcoinrpc.authproxy import JSONRPCException
from test_framework import BitcoinTestFramework
from util import assert_equal, Decimal

import auxpow

class GetAuxBlockTest (BitcoinTestFramework):

  def run_test (self):
    BitcoinTestFramework.run_test (self)

    # Generate a block so that we are not "downloading blocks".
    self.nodes[0].setgenerate (True, 1)

    # Compare basic data of getauxblock to getblocktemplate.
    auxblock = self.nodes[0].getauxblock ()
    blocktemplate = self.nodes[0].getblocktemplate ()
    assert_equal (auxblock['coinbasevalue'], blocktemplate['coinbasevalue'])
    assert_equal (auxblock['bits'], blocktemplate['bits'])
    assert_equal (auxblock['height'], blocktemplate['height'])
    assert_equal (auxblock['previousblockhash'], blocktemplate['previousblockhash'])

    # Compare target and take byte order into account.
    target = auxblock['_target']
    reversedTarget = auxpow.reverseHex (target)
    assert_equal (reversedTarget, blocktemplate['target'])

    # Verify data that can be found in another way.
    assert_equal (auxblock['chainid'], 1)
    assert_equal (auxblock['height'], self.nodes[0].getblockcount () + 1)
    assert_equal (auxblock['previousblockhash'], self.nodes[0].getblockhash (auxblock['height'] - 1))

    # Calling again should give the same block.
    auxblock2 = self.nodes[0].getauxblock ()
    assert_equal (auxblock2, auxblock)

    # If we receive a new block, the old hash will be replaced.
    self.sync_all ()
    self.nodes[1].setgenerate (True, 1)
    self.sync_all ()
    auxblock2 = self.nodes[0].getauxblock ()
    assert auxblock['hash'] != auxblock2['hash']
    try:
      self.nodes[0].getauxblock (auxblock['hash'], "x")
      raise AssertionError ("invalid block hash accepted")
    except JSONRPCException as exc:
      assert_equal (exc.error['code'], -8)

    # Invalid format for auxpow.
    try:
      self.nodes[0].getauxblock (auxblock2['hash'], "x")
      raise AssertionError ("malformed auxpow accepted")
    except JSONRPCException as exc:
      assert_equal (exc.error['code'], -1)

    # Invalidate the block again, send a transaction and query for the
    # auxblock to solve that contains the transaction.
    self.nodes[0].setgenerate (True, 1)
    addr = self.nodes[1].getnewaddress ()
    txid = self.nodes[0].sendtoaddress (addr, 1)
    self.sync_all ()
    assert_equal (self.nodes[1].getrawmempool (), [txid])
    auxblock = self.nodes[0].getauxblock ()
    blocktemplate = self.nodes[0].getblocktemplate ()
    target = blocktemplate['target']

    # Compute invalid auxpow.
    apow = auxpow.computeAuxpow (auxblock['hash'], target, False)
    res = self.nodes[0].getauxblock (auxblock['hash'], apow)
    assert not res

    # Compute and submit valid auxpow.
    apow = auxpow.computeAuxpow (auxblock['hash'], target, True)
    res = self.nodes[0].getauxblock (auxblock['hash'], apow)
    assert res

    # Make sure that the block is indeed accepted.
    self.sync_all ()
    assert_equal (self.nodes[1].getrawmempool (), [])
    height = self.nodes[1].getblockcount ()
    assert_equal (height, auxblock['height'])
    assert_equal (self.nodes[1].getblockhash (height), auxblock['hash'])

    # Call getblock and verify the auxpow field.
    data = self.nodes[1].getblock (auxblock['hash'])
    assert 'auxpow' in data
    auxJson = data['auxpow']
    assert_equal (auxJson['index'], 0)
    assert_equal (auxJson['parentblock'], apow[-160:])

    # Check that previous blocks don't have 'auxpow' in their getblock JSON.
    oldHash = self.nodes[1].getblockhash (100)
    data = self.nodes[1].getblock (oldHash)
    assert 'auxpow' not in data

    # Check that it paid correctly to the first node.
    t = self.nodes[0].listtransactions ("", 1)
    assert_equal (len (t), 1)
    t = t[0]
    assert_equal (t['category'], "immature")
    assert_equal (t['blockhash'], auxblock['hash'])
    assert t['generated']
    assert t['amount'] >= Decimal ("25")
    assert_equal (t['confirmations'], 1)

    # Verify the coinbase script.  Ensure that it includes the block height
    # to make the coinbase tx unique.  The expected block height is around
    # 200, so that the serialisation of the CScriptNum ends in an extra 00.
    # The vector has length 2, which makes up for 02XX00 as the serialised
    # height.  Check this.
    blk = self.nodes[1].getblock (auxblock['hash'])
    tx = self.nodes[1].getrawtransaction (blk['tx'][0], 1)
    coinbase = tx['vin'][0]['coinbase']
    assert_equal ("02%02x00" % auxblock['height'], coinbase[0 : 6])

if __name__ == '__main__':
  GetAuxBlockTest ().main ()
<commit_msg>Update getauxblock.py for new 'generate' RPC call.<commit_after>#!/usr/bin/env python
# Copyright (c) 2014-2015 Daniel Kraft
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# Test the "getauxblock" merge-mining RPC interface.

# Add python-bitcoinrpc to module search path:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "python-bitcoinrpc"))

from bitcoinrpc.authproxy import JSONRPCException
from test_framework import BitcoinTestFramework
from util import assert_equal, Decimal

import auxpow

class GetAuxBlockTest (BitcoinTestFramework):

  def run_test (self):
    BitcoinTestFramework.run_test (self)

    # Generate a block so that we are not "downloading blocks".
    self.nodes[0].generate (1)

    # Compare basic data of getauxblock to getblocktemplate.
    auxblock = self.nodes[0].getauxblock ()
    blocktemplate = self.nodes[0].getblocktemplate ()
    assert_equal (auxblock['coinbasevalue'], blocktemplate['coinbasevalue'])
    assert_equal (auxblock['bits'], blocktemplate['bits'])
    assert_equal (auxblock['height'], blocktemplate['height'])
    assert_equal (auxblock['previousblockhash'], blocktemplate['previousblockhash'])

    # Compare target and take byte order into account.
    target = auxblock['_target']
    reversedTarget = auxpow.reverseHex (target)
    assert_equal (reversedTarget, blocktemplate['target'])

    # Verify data that can be found in another way.
    assert_equal (auxblock['chainid'], 1)
    assert_equal (auxblock['height'], self.nodes[0].getblockcount () + 1)
    assert_equal (auxblock['previousblockhash'], self.nodes[0].getblockhash (auxblock['height'] - 1))

    # Calling again should give the same block.
    auxblock2 = self.nodes[0].getauxblock ()
    assert_equal (auxblock2, auxblock)

    # If we receive a new block, the old hash will be replaced.
    self.sync_all ()
    self.nodes[1].generate (1)
    self.sync_all ()
    auxblock2 = self.nodes[0].getauxblock ()
    assert auxblock['hash'] != auxblock2['hash']
    try:
      self.nodes[0].getauxblock (auxblock['hash'], "x")
      raise AssertionError ("invalid block hash accepted")
    except JSONRPCException as exc:
      assert_equal (exc.error['code'], -8)

    # Invalid format for auxpow.
    try:
      self.nodes[0].getauxblock (auxblock2['hash'], "x")
      raise AssertionError ("malformed auxpow accepted")
    except JSONRPCException as exc:
      assert_equal (exc.error['code'], -1)

    # Invalidate the block again, send a transaction and query for the
    # auxblock to solve that contains the transaction.
    self.nodes[0].generate (1)
    addr = self.nodes[1].getnewaddress ()
    txid = self.nodes[0].sendtoaddress (addr, 1)
    self.sync_all ()
    assert_equal (self.nodes[1].getrawmempool (), [txid])
    auxblock = self.nodes[0].getauxblock ()
    blocktemplate = self.nodes[0].getblocktemplate ()
    target = blocktemplate['target']

    # Compute invalid auxpow.
    apow = auxpow.computeAuxpow (auxblock['hash'], target, False)
    res = self.nodes[0].getauxblock (auxblock['hash'], apow)
    assert not res

    # Compute and submit valid auxpow.
    apow = auxpow.computeAuxpow (auxblock['hash'], target, True)
    res = self.nodes[0].getauxblock (auxblock['hash'], apow)
    assert res

    # Make sure that the block is indeed accepted.
    self.sync_all ()
    assert_equal (self.nodes[1].getrawmempool (), [])
    height = self.nodes[1].getblockcount ()
    assert_equal (height, auxblock['height'])
    assert_equal (self.nodes[1].getblockhash (height), auxblock['hash'])

    # Call getblock and verify the auxpow field.
    data = self.nodes[1].getblock (auxblock['hash'])
    assert 'auxpow' in data
    auxJson = data['auxpow']
    assert_equal (auxJson['index'], 0)
    assert_equal (auxJson['parentblock'], apow[-160:])

    # Check that previous blocks don't have 'auxpow' in their getblock JSON.
    oldHash = self.nodes[1].getblockhash (100)
    data = self.nodes[1].getblock (oldHash)
    assert 'auxpow' not in data

    # Check that it paid correctly to the first node.
    t = self.nodes[0].listtransactions ("", 1)
    assert_equal (len (t), 1)
    t = t[0]
    assert_equal (t['category'], "immature")
    assert_equal (t['blockhash'], auxblock['hash'])
    assert t['generated']
    assert t['amount'] >= Decimal ("25")
    assert_equal (t['confirmations'], 1)

    # Verify the coinbase script.  Ensure that it includes the block height
    # to make the coinbase tx unique.  The expected block height is around
    # 200, so that the serialisation of the CScriptNum ends in an extra 00.
    # The vector has length 2, which makes up for 02XX00 as the serialised
    # height.  Check this.
    blk = self.nodes[1].getblock (auxblock['hash'])
    tx = self.nodes[1].getrawtransaction (blk['tx'][0], 1)
    coinbase = tx['vin'][0]['coinbase']
    assert_equal ("02%02x00" % auxblock['height'], coinbase[0 : 6])

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


<commit_before>from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.contrib.sitemaps.views import sitemap
from django.contrib.staticfiles.views import serve
from django.views.i18n import JavaScriptCatalog, set_language
from graphene_django.views import GraphQLView

from .account.urls import urlpatterns as account_urls
from .cart.urls import urlpatterns as cart_urls
from .checkout.urls import urlpatterns as checkout_urls
from .core.sitemaps import sitemaps
from .core.urls import urlpatterns as core_urls
from .dashboard.urls import urlpatterns as dashboard_urls
from .data_feeds.urls import urlpatterns as feed_urls
from .graphql.api import schema
from .order.urls import urlpatterns as order_urls
from .page.urls import urlpatterns as page_urls
from .product.urls import urlpatterns as product_urls
from .search.urls import urlpatterns as search_urls

handler404 = 'saleor.core.views.handle_404'

urlpatterns = [
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
        name='django.contrib.sitemaps.views.sitemap'),
    url(r'^graphql', GraphQLView.as_view(
        schema=schema, graphiql=settings.DEBUG), name='api'),
    url(r'^i18n/$', set_language, name='set_language')]

new_urlpatterns = [
    url(r'^', include(core_urls)),
    url(r'^cart/', include((cart_urls, 'cart'), namespace='cart')),
    url(r'^checkout/',
        include((checkout_urls, 'checkout'), namespace='checkout')),
    url(r'^dashboard/',
        include((dashboard_urls, 'dashboard'), namespace='dashboard')),
    url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
    url(r'^order/', include((order_urls, 'order'), namespace='order')),
    url(r'^page/', include((page_urls, 'page'), namespace='page')),
    url(r'^products/',
        include((product_urls, 'product'), namespace='product')),
    url(r'^account/',
        include((account_urls, 'account'), namespace='account')),
    url(r'^feeds/',
        include((feed_urls, 'data_feeds'), namespace='data_feeds')),
    url(r'^search/', include((search_urls, 'search'), namespace='search')),
    url(r'', include('payments.urls')),
    url('', include('social_django.urls', namespace='social'))]

urlpatterns += i18n_patterns(*new_urlpatterns)

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += [
        url(r'^static/(?P<path>.*)$', serve)] + static(
            settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<commit_msg>Rename urlpatterns<commit_after>from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.contrib.sitemaps.views import sitemap
from django.contrib.staticfiles.views import serve
from django.views.i18n import JavaScriptCatalog, set_language
from graphene_django.views import GraphQLView

from .account.urls import urlpatterns as account_urls
from .cart.urls import urlpatterns as cart_urls
from .checkout.urls import urlpatterns as checkout_urls
from .core.sitemaps import sitemaps
from .core.urls import urlpatterns as core_urls
from .dashboard.urls import urlpatterns as dashboard_urls
from .data_feeds.urls import urlpatterns as feed_urls
from .graphql.api import schema
from .order.urls import urlpatterns as order_urls
from .page.urls import urlpatterns as page_urls
from .product.urls import urlpatterns as product_urls
from .search.urls import urlpatterns as search_urls

handler404 = 'saleor.core.views.handle_404'

non_translateable_urlpatterns = [
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
        name='django.contrib.sitemaps.views.sitemap'),
    url(r'^graphql', GraphQLView.as_view(
        schema=schema, graphiql=settings.DEBUG), name='api'),
    url(r'^i18n/$', set_language, name='set_language')]

translateable_urlpatterns = [
    url(r'^', include(core_urls)),
    url(r'^cart/', include((cart_urls, 'cart'), namespace='cart')),
    url(r'^checkout/',
        include((checkout_urls, 'checkout'), namespace='checkout')),
    url(r'^dashboard/',
        include((dashboard_urls, 'dashboard'), namespace='dashboard')),
    url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
    url(r'^order/', include((order_urls, 'order'), namespace='order')),
    url(r'^page/', include((page_urls, 'page'), namespace='page')),
    url(r'^products/',
        include((product_urls, 'product'), namespace='product')),
    url(r'^account/',
        include((account_urls, 'account'), namespace='account')),
    url(r'^feeds/',
        include((feed_urls, 'data_feeds'), namespace='data_feeds')),
    url(r'^search/', include((search_urls, 'search'), namespace='search')),
    url(r'', include('payments.urls')),
    url('', include('social_django.urls', namespace='social'))]

urlpatterns = non_translateable_urlpatterns + i18n_patterns(
    *translateable_urlpatterns)

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += [
        url(r'^static/(?P<path>.*)$', serve)] + static(
            settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<|endoftext|>