<commit_before>"""
Utility function to facilitate testing.
"""

import os
import sys

__all__ = ['assert_equal', 'assert_almost_equal','assert_approx_equal',
           'assert_array_equal', 'assert_array_less',
           'assert_array_almost_equal', 'jiffies', 'memusage', 'rand',
           'runstring']

def rand(*args):
    """Returns an array of random numbers with the given shape.

    This only uses the standard library, so it is useful for testing purposes.
    """
    import random
    from numpy.core import zeros, float64
    results = zeros(args, float64)
    f = results.flat
    for i in range(len(f)):
        f[i] = random.random()
    return results

if sys.platform[:5]=='linux':
    def jiffies(_proc_pid_stat = '/proc/%s/stat'%(os.getpid()),
                _load_time=[]):
        """ Return number of jiffies (1/100ths of a second) that this
    process has been scheduled in user mode. See man 5 proc. """
        import time
        if not _load_time:
            _load_time.append(time.time())
        try:
            f=open(_proc_pid_stat,'r')
            l = f.readline().split(' ')
            f.close()
            return int(l[13])
        except:
            return int(100*(time.time()-_load_time[0]))

    def memusage(_proc_pid_stat = '/proc/%s/stat'%(os.getpid())):
        """ Return virtual memory size in bytes of the running python.
        """
        try:
            f=open(_proc_pid_stat,'r')
            l = f.readline().split(' ')
            f.close()
            return int(l[22])
        except:
            return
else:
    # os.getpid is not in all platforms available.
    # Using time is safe but inaccurate, especially when process
    # was suspended or sleeping.
    def jiffies(_load_time=[]):
        """ Return number of jiffies (1/100ths of a second) that this
    process has been scheduled in user mode. [Emulation with time.time]. """
        import time
        if not _load_time:
            _load_time.append(time.time())
        return int(100*(time.time()-_load_time[0]))
    def memusage():
        """ Return memory usage of running python. [Not implemented]"""
        raise NotImplementedError

if os.name=='nt' and sys.version[:3] > '2.3':
    # Code "stolen" from enthought/debug/memusage.py
    def GetPerformanceAttributes(object, counter, instance = None,
                                 inum=-1, format = None, machine=None):
        # NOTE: Many counters require 2 samples to give accurate results,
        # including "% Processor Time" (as by definition, at any instant, a
        # thread's CPU usage is either 0 or 100).  To read counters like this,
        # you should copy this function, but keep the counter open, and call
        # CollectQueryData() each time you need to know.
        # See http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp
        # My older explanation for this was that the "AddCounter" process forced
        # the CPU to 100%, but the above makes more sense :)
        import win32pdh
        if format is None: format = win32pdh.PDH_FMT_LONG
        path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) )
        hq = win32pdh.OpenQuery()
        try:
            hc = win32pdh.AddCounter(hq, path)
            try:
                win32pdh.CollectQueryData(hq)
                type, val = win32pdh.GetFormattedCounterValue(hc, format)
                return val
            finally:
                win32pdh.RemoveCounter(hc)
        finally:
            win32pdh.CloseQuery(hq)

        def memusage(processName="python", instance=0):
            # from win32pdhutil, part of the win32all package
            import win32pdh
            return GetPerformanceAttributes("Process", "Virtual Bytes",
                                            processName, instance,
                                            win32pdh.PDH_FMT_LONG, None)

def build_err_msg(actual, desired, err_msg, header='Items are not equal:',
                  verbose=True):
    msg = ['\n' + header]
    if err_msg:
        if err_msg.find('\n') == -1 and len(err_msg) < 79-len(header):
            msg = [msg[0] + ' ' + err_msg]
        else:
            msg.append(err_msg)
    if verbose:
        try:
            rd = repr(desired)
        except:
            rd = '[repr failed]'
        rd = rd[:100]
        msg.append(' DESIRED: ' + rd)
        try:
            ra = repr(actual)
        except:
            ra = '[repr failed]'
        rd = ra[:100]
        msg.append(' ACTUAL: ' + ra)
    return '\n'.join(msg)

def assert_equal(actual,desired,err_msg='',verbose=True):
    """ Raise an assertion if two items are not
        equal.  I think this should be part of unittest.py
    """
    if isinstance(desired, dict):
        assert isinstance(actual, dict),`type(actual)`
        assert_equal(len(actual),len(desired),err_msg,verbose)
        for k,i in desired.items():
            assert actual.has_key(k),`k`
            assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k,err_msg), verbose)
        return
    if isinstance(desired, list) and isinstance(actual, list):
        assert_equal(len(actual),len(desired),err_msg,verbose)
        for k in range(len(desired)):
            assert_equal(actual[k], desired[k], 'item=%r\n%s' % (k,err_msg), verbose)
        return
    from numpy.core import ndarray
    if isinstance(actual, ndarray) or isinstance(desired, ndarray):
        return assert_array_equal(actual, desired, err_msg)
    msg = build_err_msg(actual, desired, err_msg, verbose=verbose)
    assert desired == actual, msg

def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True):
    """ Raise an assertion if two items are not equal.

    I think this should be part of unittest.py

    The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal)
    """
    from numpy.core import ndarray
    if isinstance(actual, ndarray) or isinstance(desired, ndarray):
        return assert_array_almost_equal(actual, desired, decimal, err_msg)
    msg = build_err_msg(actual, desired, err_msg, verbose=verbose)
    assert round(abs(desired - actual),decimal) == 0, msg

def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True):
    """ Raise an assertion if two items are not
        equal.  I think this should be part of unittest.py
        Approximately equal is defined as the number of significant digits
        correct
    """
    import math
    actual, desired = map(float, (actual, desired))
    if desired==actual:
        return
    # Normalized the numbers to be in range (-10.0,10.0)
    scale = float(pow(10,math.floor(math.log10(0.5*(abs(desired)+abs(actual))))))
    try:
        sc_desired = desired/scale
    except ZeroDivisionError:
        sc_desired = 0.0
    try:
        sc_actual = actual/scale
    except ZeroDivisionError:
        sc_actual = 0.0
    msg = build_err_msg(actual, desired, err_msg,
                header='Items are not equal to %d significant digits:' %
                                 significant,
                verbose=verbose)
    assert math.fabs(sc_desired - sc_actual) < pow(10.,-1*significant), msg

def assert_array_equal(x,y,err_msg=''):
    from numpy.core import asarray, alltrue, equal, shape, ravel, array2string
    x,y = asarray(x), asarray(y)
    msg = '\nArrays are not equal'
    try:
        assert 0 in [len(shape(x)),len(shape(y))] \
               or (len(shape(x))==len(shape(y)) and \
                   alltrue(equal(shape(x),shape(y)))),\
                   msg + ' (shapes %s, %s mismatch):\n\t' \
                   % (shape(x),shape(y)) + err_msg
        reduced = ravel(equal(x,y))
        cond = alltrue(reduced)
        if not cond:
            s1 = array2string(x,precision=16)
            s2 = array2string(y,precision=16)
            if len(s1)>120: s1 = s1[:120] + '...'
            if len(s2)>120: s2 = s2[:120] + '...'
            match = 100-100.0*reduced.tolist().count(1)/len(reduced)
            msg = msg + ' (mismatch %s%%):\n\tArray 1: %s\n\tArray 2: %s' % (match,s1,s2)
        assert cond,\
               msg + '\n\t' + err_msg
    except ValueError:
        raise ValueError, msg

def assert_array_almost_equal(x,y,decimal=6,err_msg=''):
    from numpy.core import asarray, alltrue, equal, shape, ravel,\
         array2string, less_equal, around
    x = asarray(x)
    y = asarray(y)
    msg = '\nArrays are not almost equal'
    try:
        cond = alltrue(equal(shape(x),shape(y)))
        if not cond:
            msg = msg + ' (shapes mismatch):\n\t'\
                  'Shape of array 1: %s\n\tShape of array 2: %s' % (shape(x),shape(y))
        assert cond, msg + '\n\t' + err_msg
        reduced = ravel(equal(less_equal(around(abs(x-y),decimal),10.0**(-decimal)),1))
        cond = alltrue(reduced)
        if not cond:
            s1 = array2string(x,precision=decimal+1)
            s2 = array2string(y,precision=decimal+1)
            if len(s1)>120: s1 = s1[:120] + '...'
            if len(s2)>120: s2 = s2[:120] + '...'
            match = 100-100.0*reduced.tolist().count(1)/len(reduced)
            msg = msg + ' (mismatch %s%%):\n\tArray 1: %s\n\tArray 2: %s' % (match,s1,s2)
        assert cond,\
               msg + '\n\t' + err_msg
    except ValueError:
        print sys.exc_value
        print shape(x),shape(y)
        print x, y
        raise ValueError, 'arrays are not almost equal'

def assert_array_less(x,y,err_msg=''):
    from numpy.core import asarray, alltrue, less, equal, shape, ravel, array2string
    x,y = asarray(x), asarray(y)
    msg = '\nArrays are not less-ordered'
    try:
        assert alltrue(equal(shape(x),shape(y))),\
               msg + ' (shapes mismatch):\n\t' + err_msg
        reduced = ravel(less(x,y))
        cond = alltrue(reduced)
        if not cond:
            s1 = array2string(x,precision=16)
            s2 = array2string(y,precision=16)
            if len(s1)>120: s1 = s1[:120] + '...'
            if len(s2)>120: s2 = s2[:120] + '...'
            match = 100-100.0*reduced.tolist().count(1)/len(reduced)
            msg = msg + ' (mismatch %s%%):\n\tArray 1: %s\n\tArray 2: %s' % (match,s1,s2)
        assert cond,\
               msg + '\n\t' + err_msg
    except ValueError:
        print shape(x),shape(y)
        raise ValueError, 'arrays are not less-ordered'

def runstring(astr, dict):
    exec astr in dict
<commit_msg>Rework numpy.testing.utils. This tightens up equality tests a bit; some tests in numpy an scipy fail.<commit_after>"""
Utility function to facilitate testing.
"""

import os
import sys
import operator

__all__ = ['assert_equal', 'assert_almost_equal','assert_approx_equal',
           'assert_array_equal', 'assert_array_less',
           'assert_array_almost_equal', 'jiffies', 'memusage', 'rand',
           'runstring']

def rand(*args):
    """Returns an array of random numbers with the given shape.

    This only uses the standard library, so it is useful for testing purposes.
    """
    import random
    from numpy.core import zeros, float64
    results = zeros(args, float64)
    f = results.flat
    for i in range(len(f)):
        f[i] = random.random()
    return results

if sys.platform[:5]=='linux':
    def jiffies(_proc_pid_stat = '/proc/%s/stat'%(os.getpid()),
                _load_time=[]):
        """ Return number of jiffies (1/100ths of a second) that this
    process has been scheduled in user mode. See man 5 proc. """
        import time
        if not _load_time:
            _load_time.append(time.time())
        try:
            f=open(_proc_pid_stat,'r')
            l = f.readline().split(' ')
            f.close()
            return int(l[13])
        except:
            return int(100*(time.time()-_load_time[0]))

    def memusage(_proc_pid_stat = '/proc/%s/stat'%(os.getpid())):
        """ Return virtual memory size in bytes of the running python.
        """
        try:
            f=open(_proc_pid_stat,'r')
            l = f.readline().split(' ')
            f.close()
            return int(l[22])
        except:
            return
else:
    # os.getpid is not in all platforms available.
    # Using time is safe but inaccurate, especially when process
    # was suspended or sleeping.
    def jiffies(_load_time=[]):
        """ Return number of jiffies (1/100ths of a second) that this
    process has been scheduled in user mode. [Emulation with time.time]. """
        import time
        if not _load_time:
            _load_time.append(time.time())
        return int(100*(time.time()-_load_time[0]))
    def memusage():
        """ Return memory usage of running python. [Not implemented]"""
        raise NotImplementedError

if os.name=='nt' and sys.version[:3] > '2.3':
    # Code "stolen" from enthought/debug/memusage.py
    def GetPerformanceAttributes(object, counter, instance = None,
                                 inum=-1, format = None, machine=None):
        # NOTE: Many counters require 2 samples to give accurate results,
        # including "% Processor Time" (as by definition, at any instant, a
        # thread's CPU usage is either 0 or 100).  To read counters like this,
        # you should copy this function, but keep the counter open, and call
        # CollectQueryData() each time you need to know.
        # See http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp
        # My older explanation for this was that the "AddCounter" process forced
        # the CPU to 100%, but the above makes more sense :)
        import win32pdh
        if format is None: format = win32pdh.PDH_FMT_LONG
        path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) )
        hq = win32pdh.OpenQuery()
        try:
            hc = win32pdh.AddCounter(hq, path)
            try:
                win32pdh.CollectQueryData(hq)
                type, val = win32pdh.GetFormattedCounterValue(hc, format)
                return val
            finally:
                win32pdh.RemoveCounter(hc)
        finally:
            win32pdh.CloseQuery(hq)

        def memusage(processName="python", instance=0):
            # from win32pdhutil, part of the win32all package
            import win32pdh
            return GetPerformanceAttributes("Process", "Virtual Bytes",
                                            processName, instance,
                                            win32pdh.PDH_FMT_LONG, None)

def build_err_msg(arrays, err_msg, header='Items are not equal:',
                  verbose=True,
                  names=('ACTUAL', 'DESIRED')):
    msg = ['\n' + header]
    if err_msg:
        if err_msg.find('\n') == -1 and len(err_msg) < 79-len(header):
            msg = [msg[0] + ' ' + err_msg]
        else:
            msg.append(err_msg)
    if verbose:
        for i, a in enumerate(arrays):
            try:
                r = repr(a)
            except:
                r = '[repr failed]'
            r = r[:100]
            msg.append(' %s: %s' % (names[i], r))
    return '\n'.join(msg)

def assert_equal(actual,desired,err_msg='',verbose=True):
    """ Raise an assertion if two items are not
        equal.  I think this should be part of unittest.py
    """
    if isinstance(desired, dict):
        assert isinstance(actual, dict),`type(actual)`
        assert_equal(len(actual),len(desired),err_msg,verbose)
        for k,i in desired.items():
            assert actual.has_key(k),`k`
            assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k,err_msg), verbose)
        return
    if isinstance(desired, list) and isinstance(actual, list):
        assert_equal(len(actual),len(desired),err_msg,verbose)
        for k in range(len(desired)):
            assert_equal(actual[k], desired[k], 'item=%r\n%s' % (k,err_msg), verbose)
        return
    from numpy.core import ndarray
    if isinstance(actual, ndarray) or isinstance(desired, ndarray):
        return assert_array_equal(actual, desired, err_msg)
    msg = build_err_msg([actual, desired], err_msg, verbose=verbose)
    assert desired == actual, msg

def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True):
    """ Raise an assertion if two items are not equal.

    I think this should be part of unittest.py

    The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal)
    """
    from numpy.core import ndarray
    if isinstance(actual, ndarray) or isinstance(desired, ndarray):
        return assert_array_almost_equal(actual, desired, decimal, err_msg)
    msg = build_err_msg([actual, desired], err_msg, verbose=verbose)
    assert round(abs(desired - actual),decimal) == 0, msg

def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True):
    """ Raise an assertion if two items are not
        equal.  I think this should be part of unittest.py
        Approximately equal is defined as the number of significant digits
        correct
    """
    import math
    actual, desired = map(float, (actual, desired))
    if desired==actual:
        return
    # Normalized the numbers to be in range (-10.0,10.0)
    scale = float(pow(10,math.floor(math.log10(0.5*(abs(desired)+abs(actual))))))
    try:
        sc_desired = desired/scale
    except ZeroDivisionError:
        sc_desired = 0.0
    try:
        sc_actual = actual/scale
    except ZeroDivisionError:
        sc_actual = 0.0
    msg = build_err_msg([actual, desired], err_msg,
                header='Items are not equal to %d significant digits:' %
                                 significant,
                verbose=verbose)
    assert math.fabs(sc_desired - sc_actual) < pow(10.,-1*significant), msg

def assert_array_compare(comparision, x, y, err_msg='', verbose=True,
                         header=''):
    from numpy.core import asarray
    x = asarray(x)
    y = asarray(y)
    try:
        cond = (x.shape==() or y.shape==()) or x.shape == y.shape
        if not cond:
            msg = build_err_msg([x, y],
                                err_msg
                                + '\n(shapes %s, %s mismatch)' % (x.shape,
                                                                  y.shape),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            assert cond, msg
        reduced = comparision(x, y).ravel()
        cond = reduced.all()
        if not cond:
            match = 100-100.0*reduced.tolist().count(1)/len(reduced)
            msg = build_err_msg([x, y],
                                err_msg
                                + '\n(mismatch %s%%)' % (match,),
                                verbose=verbose, header=header,
                                names=('x', 'y'))
            assert cond, msg
    except ValueError:
        msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
                            names=('x', 'y'))
        raise ValueError(msg)

def assert_array_equal(x, y, err_msg='', verbose=True):
    assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
                         verbose=verbose, header='Arrays are not equal')

def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True):
    from numpy.core import around
    def compare(x, y):
        return around(abs(x-y),decimal) <= 10.0**(-decimal)
    assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose,
                         header='Arrays are not almost equal')

def assert_array_less(x, y, err_msg='', verbose=True):
    assert_array_compare(operator.__lt__, x, y, err_msg=err_msg,
                         verbose=verbose,
                         header='Arrays are not less-ordered')

def runstring(astr, dict):
    exec astr in dict
<|endoftext|>


<commit_before>"""f2py Tool

Tool-specific initialization for f2py.

There normally shouldn't be any need to import this module directly.
It will usually be imported through the generic SCons.Tool.Tool()
selection method.

"""

import os.path
import re

import SCons.Action
import SCons.Defaults
import SCons.Scanner
import SCons.Tool
import SCons.Util
import SCons.Node

# XXX: this whole thing needs cleaning !

def _f2pySuffixEmitter(env, source):
    return '$F2PYCFILESUFFIX'

#_reModule = re.compile(r'%module\s+(.+)')

def _mangle_fortranobject(targetname, filename):
    basename = os.path.splitext(os.path.basename(targetname))[0]
    return '%s_%s' % (basename, filename)

def _f2pyEmitter(target, source, env):
    build_dir = os.path.dirname(str(target[0]))
    target.append(SCons.Node.FS.default_fs.Entry(
        os.path.join(build_dir, _mangle_fortranobject(str(target[0]), 'fortranobject.c'))))
    if _is_pyf(str(source[0])):
        basename = os.path.splitext(os.path.basename(str(target[0])))
        basename = basename[0]
        basename = basename.split('module')[0]
        target.append(SCons.Node.FS.default_fs.Entry(
            os.path.join(build_dir, '%s-f2pywrappers.f' % basename)))
    return (target, source)

def _is_pyf(source_file):
    return os.path.splitext(source_file)[1] == '.pyf'

def _pyf2c(target, source, env):
    import numpy.f2py
    import shutil

    # We need filenames from source/target for path handling
    target_file_names = [str(i) for i in target]
    source_file_names = [str(i) for i in source]

    # Get source files necessary for f2py generated modules
    d = os.path.dirname(numpy.f2py.__file__)
    source_c = os.path.join(d,'src','fortranobject.c')

    # Copy source files for f2py generated modules in the build dir
    build_dir = os.path.dirname(target_file_names[0])

    # XXX: blah
    if build_dir == '':
        build_dir = '.'

    try:
        shutil.copy(source_c, os.path.join(build_dir,
               _mangle_fortranobject(target_file_names[0], 'fortranobject.c')))
    except IOError, e:
        msg = "Error while copying fortran source files (error was %s)" % str(e)
        raise IOError(msg)

    basename = os.path.basename(str(target[0]).split('module')[0])

    # XXX: handle F2PYOPTIONS being a string instead of a list
    if _is_pyf(source_file_names[0]):
        # XXX: scons has a way to force buidler to only use one source file
        if len(source_file_names) > 1:
            raise NotImplementedError("FIXME: multiple source files")

        wrapper = os.path.join(build_dir, '%s-f2pywrappers.f' % basename)

        cmd = env['F2PYOPTIONS'] + [source_file_names[0], '--build-dir', build_dir]
        st = numpy.f2py.run_main(cmd)

        if not os.path.exists(wrapper):
            f = open(wrapper, 'w')
            f.close()
    else:
        cmd = env['F2PYOPTIONS'] + source_file_names + ['--build-dir', build_dir]
        # fortran files, we need to give the module name
        cmd.extend(['--lower', '-m', basename])
        st = numpy.f2py.run_main(cmd)

    return 0

def generate(env):
    """Add Builders and construction variables for swig to an Environment."""
    import numpy.f2py
    d = os.path.dirname(numpy.f2py.__file__)

    c_file, cxx_file = SCons.Tool.createCFileBuilders(env)

    c_file.suffix['.pyf'] = _f2pySuffixEmitter

    c_file.add_action('.pyf', SCons.Action.Action(_pyf2c))
    c_file.add_emitter('.pyf', _f2pyEmitter)

    env['F2PYOPTIONS']      = SCons.Util.CLVar('')
    env['F2PYBUILDDIR']     = ''
    env['F2PYCFILESUFFIX']  = 'module$CFILESUFFIX'
    env['F2PYINCLUDEDIR']   = os.path.join(d, 'src')

    # XXX: adding a scanner using c_file.add_scanner does not work...
    expr = '(<)include_file=(\S+)>'
    scanner = SCons.Scanner.ClassicCPP("F2PYScan", ".pyf", "F2PYPATH", expr)
    env.Append(SCANNERS = scanner)

    env['BUILDERS']['F2py'] = SCons.Builder.Builder(action = _pyf2c,
                                                    emitter = _f2pyEmitter,
                                                    suffix = _f2pySuffixEmitter)

_MINC = re.compile(r'<include_file=(\S+)>')
def _pyf_scanner(node, env, path):
    print "================== SCANNING ===================="
    cnt = node.get_contents()
    return _parse(cnt)

def _parse(lines):
    """Return list of included files in .pyf from include_file directive."""
    dep = []
    for line in lines:
        m = _MINC.search(line)
        if m:
            dep.append(m.group(1))
    return dep

def exists(env):
    try:
        import numpy.f2py
        st = 1
    except ImportError, e:
        print "Warning : f2py tool not found, error was %s" % e
        st = 0

    return st
<commit_msg>Add a method to get the module name of a .pyf file<commit_after>"""f2py Tool

Tool-specific initialization for f2py.

There normally shouldn't be any need to import this module directly.
It will usually be imported through the generic SCons.Tool.Tool()
selection method.

"""

import os.path
import re

import SCons.Action
import SCons.Defaults
import SCons.Scanner
import SCons.Tool
import SCons.Util
import SCons.Node

# XXX: this whole thing needs cleaning !

def _f2pySuffixEmitter(env, source):
    return '$F2PYCFILESUFFIX'

#_reModule = re.compile(r'%module\s+(.+)')

def _mangle_fortranobject(targetname, filename):
    basename = os.path.splitext(os.path.basename(targetname))[0]
    return '%s_%s' % (basename, filename)

# XXX: Taken from numpy/distutils/command/build_src
_f2py_module_name_match = re.compile(r'\s*python\s*module\s*(?P<name>[\w_]+)',
                                re.I).match
_f2py_user_module_name_match = re.compile(r'\s*python\s*module\s*(?P<name>[\w_]*?'\
                                     '__user__[\w_]*)',re.I).match

def get_f2py_modulename(source):
    name = None
    f = open(source)
    f_readlines = getattr(f,'xreadlines',f.readlines)
    for line in f_readlines():
        m = _f2py_module_name_match(line)
        if m:
            if _f2py_user_module_name_match(line): # skip *__user__* names
                continue
            name = m.group('name')
            break
    f.close()
    return name
# End of code taken from numpy/distutils/command/build_src

def get_f2py_modulename_from_node(node):
    # XXX: is srcnode() call the right method ? We cannot just use the node
    # itself, because it may not still exists in the FS (because of VariantDir,
    # etc...)
    src = node.srcnode()
    return get_f2py_modulename(str(src))

def _f2pyEmitter(target, source, env):
    build_dir = os.path.dirname(str(target[0]))
    target.append(SCons.Node.FS.default_fs.Entry(
        os.path.join(build_dir, _mangle_fortranobject(str(target[0]), 'fortranobject.c'))))
    if _is_pyf(str(source[0])):
        basename = os.path.splitext(os.path.basename(str(target[0])))
        basename = basename[0]
        basename = basename.split('module')[0]
        target.append(SCons.Node.FS.default_fs.Entry(
            os.path.join(build_dir, '%s-f2pywrappers.f' % basename)))
    return (target, source)

def _is_pyf(source_file):
    return os.path.splitext(source_file)[1] == '.pyf'

def _pyf2c(target, source, env):
    import numpy.f2py
    import shutil

    # We need filenames from source/target for path handling
    target_file_names = [str(i) for i in target]
    source_file_names = [str(i) for i in source]

    # Get source files necessary for f2py generated modules
    d = os.path.dirname(numpy.f2py.__file__)
    source_c = os.path.join(d,'src','fortranobject.c')

    # Copy source files for f2py generated modules in the build dir
    build_dir = os.path.dirname(target_file_names[0])

    # XXX: blah
    if build_dir == '':
        build_dir = '.'

    try:
        shutil.copy(source_c, os.path.join(build_dir,
               _mangle_fortranobject(target_file_names[0], 'fortranobject.c')))
    except IOError, e:
        msg = "Error while copying fortran source files (error was %s)" % str(e)
        raise IOError(msg)

    basename = os.path.basename(str(target[0]).split('module')[0])

    # XXX: handle F2PYOPTIONS being a string instead of a list
    if _is_pyf(source_file_names[0]):
        # XXX: scons has a way to force buidler to only use one source file
        if len(source_file_names) > 1:
            raise NotImplementedError("FIXME: multiple source files")

        wrapper = os.path.join(build_dir, '%s-f2pywrappers.f' % basename)

        cmd = env['F2PYOPTIONS'] + [source_file_names[0], '--build-dir', build_dir]
        st = numpy.f2py.run_main(cmd)

        if not os.path.exists(wrapper):
            f = open(wrapper, 'w')
            f.close()
    else:
        cmd = env['F2PYOPTIONS'] + source_file_names + ['--build-dir', build_dir]
        # fortran files, we need to give the module name
        cmd.extend(['--lower', '-m', basename])
        st = numpy.f2py.run_main(cmd)

    return 0

def generate(env):
    """Add Builders and construction variables for swig to an Environment."""
    import numpy.f2py
    d = os.path.dirname(numpy.f2py.__file__)

    c_file, cxx_file = SCons.Tool.createCFileBuilders(env)

    c_file.suffix['.pyf'] = _f2pySuffixEmitter

    c_file.add_action('.pyf', SCons.Action.Action(_pyf2c))
    c_file.add_emitter('.pyf', _f2pyEmitter)

    env['F2PYOPTIONS']      = SCons.Util.CLVar('')
    env['F2PYBUILDDIR']     = ''
    env['F2PYCFILESUFFIX']  = 'module$CFILESUFFIX'
    env['F2PYINCLUDEDIR']   = os.path.join(d, 'src')

    # XXX: adding a scanner using c_file.add_scanner does not work...
    expr = '(<)include_file=(\S+)>'
    scanner = SCons.Scanner.ClassicCPP("F2PYScan", ".pyf", "F2PYPATH", expr)
    env.Append(SCANNERS = scanner)

    env['BUILDERS']['F2py'] = SCons.Builder.Builder(action = _pyf2c,
                                                    emitter = _f2pyEmitter,
                                                    suffix = _f2pySuffixEmitter)

_MINC = re.compile(r'<include_file=(\S+)>')
def _pyf_scanner(node, env, path):
    print "================== SCANNING ===================="
    cnt = node.get_contents()
    return _parse(cnt)

def _parse(lines):
    """Return list of included files in .pyf from include_file directive."""
    dep = []
    for line in lines:
        m = _MINC.search(line)
        if m:
            dep.append(m.group(1))
    return dep

def exists(env):
    try:
        import numpy.f2py
        st = 1
    except ImportError, e:
        print "Warning : f2py tool not found, error was %s" % e
        st = 0

    return st
<|endoftext|>


<commit_before>from quaternions import compare_two_sets_of_quaternions,global_quaternion_rotation_between_two_sets
from intensities import fourier_shell_correlation
<commit_msg>updated __init__.py<commit_after>from quaternions import compare_two_sets_of_quaternions,global_quaternion_rotation_between_two_sets
from intensities import fourier_shell_correlation
from utils import rotation
<|endoftext|>


<commit_before>import json
import sys
import imp
import importlib

from google.protobuf import json_format
from google.protobuf import descriptor_pb2

class ProtoParseException(Exception):
    pass

class ParseException(Exception):
    pass

def dynamic_client_getter(stubs, service_name):
    stub = stubs.get(service_name)
    if stub:
        if stub.client_import_type == 'path':
            if stub.client_module_name in sys.modules.keys():
                md = importlib.import_module(stub.client_module_name)
            else:
                md = imp.load_source(
                    stub.client_module_name,
                    stub.client_module_path)
        else:
            md = importlib.import_module(stub.client_module)
        cl = getattr(md, stub.get_client_func)
        return cl()

def get_service_identify(stubs, service_name):
    stub = stubs.get(service_name)
    if stub:
        if stub.server_pb2_import_type == 'path':
            if stub.server_pb2_module_name in sys.modules.keys():
                md = importlib.import_module(stub.server_pb2_module_name)
            else:
                md = imp.load_source(
                    stub.server_pb2_module_name,
                    stub.server_pb2_module_path)
        else:
            md = importlib.import_module(stub.server_pb2_module)
        return get_service_identify_from_pb_file(stubs, md)

def get_service_identify_from_pb_file(stubs, pb2):
    method_mapping = dict()
    p = descriptor_pb2.FileDescriptorProto()
    pb2.DESCRIPTOR.CopyToProto(p)
    for s in p.service:
        for m in s.method:
            if not method_mapping.get(m.name):
                method_mapping[m.name] = dict()
            for k, v in m.options._fields.items():
                if hasattr(v, '_fields'):
                    for i, j in v._fields.items():
                        method_mapping[m.name][i.name] = j
    return method_mapping

def search_method_option_in_service(stubs, service, call):
    idt = get_service_identify(stubs, service)
    if not idt:
        raise ProtoParseException('not registed service')
    options = dict()

    for k, v in idt.items():
        if str(k) == call:
            options = v
            break

    if options:
        return options
    raise ProtoParseException('no such a call')

def pb2json(pb):
    return json.loads(json_format.MessageToJson(pb))

def json2pb(pb, js):
    if isinstance(js, dict):
        js = json.dumps(js)
    return json_format.Parse(js, pb)
<commit_msg>MessageToJson add including_default_value_fields<commit_after>import json
import sys
import imp
import importlib

from google.protobuf import json_format
from google.protobuf import descriptor_pb2

class ProtoParseException(Exception):
    pass

class ParseException(Exception):
    pass

def dynamic_client_getter(stubs, service_name):
    stub = stubs.get(service_name)
    if stub:
        if stub.client_import_type == 'path':
            if stub.client_module_name in sys.modules.keys():
                md = importlib.import_module(stub.client_module_name)
            else:
                md = imp.load_source(
                    stub.client_module_name,
                    stub.client_module_path)
        else:
            md = importlib.import_module(stub.client_module)
        cl = getattr(md, stub.get_client_func)
        return cl()

def get_service_identify(stubs, service_name):
    stub = stubs.get(service_name)
    if stub:
        if stub.server_pb2_import_type == 'path':
            if stub.server_pb2_module_name in sys.modules.keys():
                md = importlib.import_module(stub.server_pb2_module_name)
            else:
                md = imp.load_source(
                    stub.server_pb2_module_name,
                    stub.server_pb2_module_path)
        else:
            md = importlib.import_module(stub.server_pb2_module)
        return get_service_identify_from_pb_file(stubs, md)

def get_service_identify_from_pb_file(stubs, pb2):
    method_mapping = dict()
    p = descriptor_pb2.FileDescriptorProto()
    pb2.DESCRIPTOR.CopyToProto(p)
    for s in p.service:
        for m in s.method:
            if not method_mapping.get(m.name):
                method_mapping[m.name] = dict()
            for k, v in m.options._fields.items():
                if hasattr(v, '_fields'):
                    for i, j in v._fields.items():
                        method_mapping[m.name][i.name] = j
    return method_mapping

def search_method_option_in_service(stubs, service, call):
    idt = get_service_identify(stubs, service)
    if not idt:
        raise ProtoParseException('not registed service')
    options = dict()

    for k, v in idt.items():
        if str(k) == call:
            options = v
            break

    if options:
        return options
    raise ProtoParseException('no such a call')

def pb2json(pb):
    return json.loads(json_format.MessageToJson(pb, including_default_value_fields=True))

def json2pb(pb, js):
    if isinstance(js, dict):
        js = json.dumps(js)
    return json_format.Parse(js, pb)
<|endoftext|>


<commit_before># Copyright (c) 2014, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

import inspect
import stix
from cybox.core import Observables, Observable
import cybox.utils.nsparser as cybox_nsparser

class NamespaceParser(object):
    def __init__(self):
        pass

    def _get_observable_namespaces(self, obs):
        '''Returns namespaces used within a CybOX Observable'''
        cybox_parser = cybox_nsparser.NamespaceParser([obs.to_obj()])
        d = cybox_parser.get_namespace_dict()
        d['http://cybox.mitre.org/default_vocabularies-2'] = 'cyboxVocabs' # this is just added by default in python-cybox
        return [ns for ns in d.iterkeys()]

    def get_namespaces(self, entity):
        all_namespaces = set()

        if not (isinstance(entity, stix.Entity) or isinstance(entity, Observable)):
            raise ValueError("Must provide an instance of stix.Entity or cybox.core.Observable")

        entity.nsparser_touched = True
        if isinstance(entity, Observable):
            all_namespaces.update(self._get_observable_namespaces(entity))
        elif isinstance(entity, Observables):
            for child in self._get_children(entity):
                all_namespaces.update(self.get_namespaces(child))
        elif hasattr(entity, "_namespace"):
            all_namespaces.add(entity._namespace)

            for child in self._get_children(entity):
                if not hasattr(child, "nsparser_touched"):
                    if hasattr(child, "_namespace") or isinstance(child, Observable):
                        all_namespaces.update(self.get_namespaces(child))

        del entity.nsparser_touched
        return all_namespaces

    def _get_children(self, entity):
        for (name, obj) in inspect.getmembers(entity):
            if isinstance(obj, stix.Entity):
                yield obj
            elif isinstance(obj, Observable):
                yield obj
            elif isinstance(obj, Observables):
                for obs in obj.observables:
                    yield obs
            elif isinstance(obj, list):
                for item in obj:
                    if isinstance(item, stix.Entity) or isinstance(item, Observable):
                        yield item

    def get_namespace_schemalocation_dict(self, entity):
        d = {}
        ns_set = self.get_namespaces(entity)
        for ns in ns_set:
            if ns in XML_NAMESPACES:
                continue
            elif ns in STIX_NS_TO_SCHEMALOCATION:
                schemalocation = STIX_NS_TO_SCHEMALOCATION[ns]
                d[ns] = schemalocation
            elif ns in EXT_NS_TO_SCHEMALOCATION:
                schemalocation = EXT_NS_TO_SCHEMALOCATION[ns]
                d[ns] = schemalocation
            elif ns.startswith("http://cybox.mitre.org"):
                for cybox_ns_tup in cybox_nsparser.NS_LIST:
                    if cybox_ns_tup[0] == ns:
                        d[ns] = cybox_ns_tup[2]
            else:
                print "! Cannot map %s to a schemalocation." % (ns)

        return d

    def _get_xmlns_str(self, ns_dict):
        return "\n\t".join(['xmlns:%s="%s"' % (alias,ns) for ns,alias in sorted(ns_dict.iteritems())])

    def _get_schemaloc_str(self, schemaloc_dict):
        schemaloc_str_start = 'xsi:schemaLocation="\n\t'
        schemaloc_str_end = '"'
        schemaloc_str_content = "\n\t".join(["%s %s" % (ns, loc) for ns,loc in sorted(schemaloc_dict.iteritems())])
        return schemaloc_str_start + schemaloc_str_content + schemaloc_str_end

    def get_namespace_def_str(self, ns_dict, schemaloc_dict):
        return "\n\t" + self._get_xmlns_str(ns_dict) + "\n\t" + self._get_schemaloc_str(schemaloc_dict)

XML_NAMESPACES = {'http://www.w3.org/2001/XMLSchema-instance' : 'xsi',
                  'http://www.w3.org/2001/XMLSchema' : 'xs',
                  'http://www.w3.org/1999/xlink' : 'xlink',
                  'http://www.w3.org/2000/09/xmldsig#': 'ds'}

# Schema locations for namespaces defined by the STIX language
STIX_NS_TO_SCHEMALOCATION = {
         'http://stix.mitre.org/stix-1': 'http://stix.mitre.org/XMLSchema/core/1.0.1/stix_core.xsd',
         'http://stix.mitre.org/common-1': 'http://stix.mitre.org/XMLSchema/common/1.0.1/stix_common.xsd',
         'http://data-marking.mitre.org/Marking-1': 'http://stix.mitre.org/XMLSchema/data_marking/1.0.1/data_marking.xsd',
         'http://data-marking.mitre.org/extensions/MarkingStructure#Simple-1': 'http://stix.mitre.org/XMLSchema/extensions/marking/simple_marking/1.0.1/simple_marking.xsd',
         'http://data-marking.mitre.org/extensions/MarkingStructure#TLP-1': 'http://stix.mitre.org/XMLSchema/extensions/marking/tlp/1.0.1/tlp.xsd',
         'http://stix.mitre.org/Campaign-1': 'http://stix.mitre.org/XMLSchema/campaign/1.0.1/campaign.xsd',
         'http://stix.mitre.org/CourseOfAction-1': 'http://stix.mitre.org/XMLSchema/course_of_action/1.0.1/course_of_action.xsd',
         'http://stix.mitre.org/ExploitTarget-1': 'http://stix.mitre.org/XMLSchema/exploit_target/1.0.1/exploit_target.xsd',
         'http://stix.mitre.org/Incident-1': 'http://stix.mitre.org/XMLSchema/incident/1.0.1/incident.xsd',
         'http://stix.mitre.org/Indicator-2': 'http://stix.mitre.org/XMLSchema/indicator/2.0.1/indicator.xsd',
         'http://stix.mitre.org/TTP-1': 'http://stix.mitre.org/XMLSchema/ttp/1.0.1/ttp.xsd',
         'http://stix.mitre.org/ThreatActor-1': 'http://stix.mitre.org/XMLSchema/threat_actor/1.0.1/threat_actor.xsd',
         'http://stix.mitre.org/default_vocabularies-1': 'http://stix.mitre.org/XMLSchema/default_vocabularies/1.0.1/stix_default_vocabularies.xsd',
         'http://stix.mitre.org/extensions/AP#CAPEC2.6-1': 'http://stix.mitre.org/XMLSchema/extensions/attack_pattern/capec_2.6.1/1.0.1/capec_2.6.1.xsd',
         'http://stix.mitre.org/extensions/Address#CIQAddress3.0-1': 'http://stix.mitre.org/XMLSchema/extensions/address/ciq_address_3.0/1.0.1/ciq_address_3.0.xsd',
         'http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1': 'http://stix.mitre.org/XMLSchema/extensions/identity/ciq_identity_3.0/1.0.1/ciq_identity_3.0.xsd',
         'http://stix.mitre.org/extensions/Malware#MAEC4.0-1': 'http://stix.mitre.org/XMLSchema/extensions/malware/maec_4.0.1/1.0.1/maec_4.0.1.xsd',
         'http://stix.mitre.org/extensions/StructuredCOA#Generic-1': 'http://stix.mitre.org/XMLSchema/extensions/structured_coa/generic/1.0.1/generic.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#Generic-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/generic/1.0.1/generic.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#OVAL5.10-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/oval_5.10/1.0.1/oval_5.10.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#OpenIOC2010-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/open_ioc_2010/1.0.1/open_ioc_2010.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#Snort-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/snort/1.0.1/snort.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#YARA-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/yara/1.0.1/yara.xsd',
         'http://stix.mitre.org/extensions/Vulnerability#CVRF-1': 'http://stix.mitre.org/XMLSchema/extensions/vulnerability/cvrf_1.1/1.0.1/cvrf_1.1.xsd'}

# Schema locations for namespaces not defined by STIX, but hosted on the STIX website
EXT_NS_TO_SCHEMALOCATION = {'urn:oasis:names:tc:ciq:xpil:3' : 'http://stix.mitre.org/XMLSchema/external/oasis_ciq_3.0/xPIL.xsd',
                            'urn:oasis:names:tc:ciq:xnl:3' : 'http://stix.mitre.org/XMLSchema/external/oasis_ciq_3.0/xNL.xsd'}

# Default namespace->alias mappings. These can be overriden by user-provided dictionaries on export
DEFAULT_STIX_NS_TO_PREFIX = {
     'http://data-marking.mitre.org/Marking-1': 'marking',
     'http://data-marking.mitre.org/extensions/MarkingStructure#TLP-1': 'tlpMarking',
     'http://data-marking.mitre.org/extensions/MarkingStructure#Simple-1': 'simpleMarking',
     'http://stix.mitre.org/Campaign-1': 'campaign',
     'http://stix.mitre.org/CourseOfAction-1': 'coa',
     'http://stix.mitre.org/ExploitTarget-1': 'et',
     'http://stix.mitre.org/Incident-1': 'incident',
     'http://stix.mitre.org/Indicator-2': 'indicator',
     'http://stix.mitre.org/TTP-1': 'ttp',
     'http://stix.mitre.org/ThreatActor-1': 'ta',
     'http://stix.mitre.org/common-1': 'stixCommon',
     'http://stix.mitre.org/default_vocabularies-1': 'stixVocabs',
     'http://stix.mitre.org/extensions/AP#CAPEC2.6-1': 'capecInstance',
     'http://stix.mitre.org/extensions/Address#CIQAddress3.0-1': 'ciqAddress',
     'http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1': 'ciqIdentity',
     'http://stix.mitre.org/extensions/Malware#MAEC4.0-1': 'maecInstance',
     'http://stix.mitre.org/extensions/StructuredCOA#Generic-1': 'genericStructuredCOA',
     'http://stix.mitre.org/extensions/TestMechanism#Generic-1': 'genericTM',
     'http://stix.mitre.org/extensions/TestMechanism#OVAL5.10-1': 'ovalTM',
     'http://stix.mitre.org/extensions/TestMechanism#OpenIOC2010-1': 'openiocTM',
     'http://stix.mitre.org/extensions/TestMechanism#Snort-1': 'snortTM',
     'http://stix.mitre.org/extensions/TestMechanism#YARA-1': 'yaraTM',
     'http://stix.mitre.org/extensions/Vulnerability#CVRF-1': 'cvrfVuln',
     'http://stix.mitre.org/stix-1': 'stix',
     'http://www.icasi.org/CVRF/schema/common/1.1': 'cvrf-common',
     'http://www.icasi.org/CVRF/schema/cvrf/1.1': 'cvrf',
     'http://www.icasi.org/CVRF/schema/prod/1.1': 'prod',
     'http://www.icasi.org/CVRF/schema/vuln/1.1': 'vuln',
     'http://www.w3.org/1999/xlink': 'xlink',
     'http://www.w3.org/2000/09/xmldsig#': 'ds',
     'http://www.w3.org/2001/XMLSchema': 'xs',
     'http://www.w3.org/2001/XMLSchema-instance': 'xsi'}

DEFAULT_EXT_TO_PREFIX = {'urn:oasis:names:tc:ciq:xpil:3' : 'xpil',
                         'urn:oasis:names:tc:ciq:xnl:3' : 'xnl'}
<commit_msg>Ensure proper CybOX namespaces are output<commit_after># Copyright (c) 2014, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

import inspect
import stix
import cybox
from cybox.core import Observables, Observable
import cybox.utils.nsparser as cybox_nsparser

class NamespaceParser(object):
    def __init__(self):
        pass

    def _get_observable_namespaces(self, obs):
        '''Returns namespaces used within a CybOX Observable'''
        cybox_parser = cybox_nsparser.NamespaceParser([obs.to_obj()])
        d = cybox_parser.get_namespace_dict()
        d['http://cybox.mitre.org/default_vocabularies-2'] = 'cyboxVocabs' # this is just added by default in python-cybox
        return [ns for ns in d.iterkeys()]

    def get_namespaces(self, entity):
        all_namespaces = set()

        if not isinstance(entity, (stix.Entity, cybox.Entity)):
            raise ValueError("Must provide an instance of stix.Entity or cybox.core.Observable")

        entity.nsparser_touched = True
        if isinstance(entity, Observable):
            all_namespaces.update(self._get_observable_namespaces(entity))
        elif isinstance(entity, Observables):
            for child in self._get_children(entity):
                all_namespaces.update(self.get_namespaces(child))
        elif hasattr(entity, "_namespace"):
            all_namespaces.add(entity._namespace)

            for child in self._get_children(entity):
                if not hasattr(child, "nsparser_touched"):
                    if hasattr(child, "_namespace") or isinstance(child, Observable):
                        all_namespaces.update(self.get_namespaces(child))

        del entity.nsparser_touched
        return all_namespaces

    def _get_children(self, entity):
        for (name, obj) in inspect.getmembers(entity):
            if isinstance(obj, Observables):
                for obs in obj.observables:
                    yield obs
            elif isinstance(obj, (stix.Entity, cybox.Entity)):
                yield obj
            elif isinstance(obj, list):
                for item in obj:
                    if isinstance(item, stix.Entity) or isinstance(item, Observable):
                        yield item

    def get_namespace_schemalocation_dict(self, entity):
        d = {}
        ns_set = self.get_namespaces(entity)
        for ns in ns_set:
            if ns in XML_NAMESPACES:
                continue
            elif ns in STIX_NS_TO_SCHEMALOCATION:
                schemalocation = STIX_NS_TO_SCHEMALOCATION[ns]
                d[ns] = schemalocation
            elif ns in EXT_NS_TO_SCHEMALOCATION:
                schemalocation = EXT_NS_TO_SCHEMALOCATION[ns]
                d[ns] = schemalocation
            elif ns.startswith("http://cybox.mitre.org"):
                for cybox_ns_tup in cybox_nsparser.NS_LIST:
                    if cybox_ns_tup[0] == ns:
                        d[ns] = cybox_ns_tup[2]
            else:
                print "! Cannot map %s to a schemalocation." % (ns)

        return d

    def _get_xmlns_str(self, ns_dict):
        return "\n\t".join(['xmlns:%s="%s"' % (alias,ns) for ns,alias in sorted(ns_dict.iteritems())])

    def _get_schemaloc_str(self, schemaloc_dict):
        schemaloc_str_start = 'xsi:schemaLocation="\n\t'
        schemaloc_str_end = '"'
        schemaloc_str_content = "\n\t".join(["%s %s" % (ns, loc) for ns,loc in sorted(schemaloc_dict.iteritems())])
        return schemaloc_str_start + schemaloc_str_content + schemaloc_str_end

    def get_namespace_def_str(self, ns_dict, schemaloc_dict):
        return "\n\t" + self._get_xmlns_str(ns_dict) + "\n\t" + self._get_schemaloc_str(schemaloc_dict)

XML_NAMESPACES = {'http://www.w3.org/2001/XMLSchema-instance' : 'xsi',
                  'http://www.w3.org/2001/XMLSchema' : 'xs',
                  'http://www.w3.org/1999/xlink' : 'xlink',
                  'http://www.w3.org/2000/09/xmldsig#': 'ds'}

# Schema locations for namespaces defined by the STIX language
STIX_NS_TO_SCHEMALOCATION = {
         'http://stix.mitre.org/stix-1': 'http://stix.mitre.org/XMLSchema/core/1.0.1/stix_core.xsd',
         'http://stix.mitre.org/common-1': 'http://stix.mitre.org/XMLSchema/common/1.0.1/stix_common.xsd',
         'http://data-marking.mitre.org/Marking-1': 'http://stix.mitre.org/XMLSchema/data_marking/1.0.1/data_marking.xsd',
         'http://data-marking.mitre.org/extensions/MarkingStructure#Simple-1': 'http://stix.mitre.org/XMLSchema/extensions/marking/simple_marking/1.0.1/simple_marking.xsd',
         'http://data-marking.mitre.org/extensions/MarkingStructure#TLP-1': 'http://stix.mitre.org/XMLSchema/extensions/marking/tlp/1.0.1/tlp.xsd',
         'http://stix.mitre.org/Campaign-1': 'http://stix.mitre.org/XMLSchema/campaign/1.0.1/campaign.xsd',
         'http://stix.mitre.org/CourseOfAction-1': 'http://stix.mitre.org/XMLSchema/course_of_action/1.0.1/course_of_action.xsd',
         'http://stix.mitre.org/ExploitTarget-1': 'http://stix.mitre.org/XMLSchema/exploit_target/1.0.1/exploit_target.xsd',
         'http://stix.mitre.org/Incident-1': 'http://stix.mitre.org/XMLSchema/incident/1.0.1/incident.xsd',
         'http://stix.mitre.org/Indicator-2': 'http://stix.mitre.org/XMLSchema/indicator/2.0.1/indicator.xsd',
         'http://stix.mitre.org/TTP-1': 'http://stix.mitre.org/XMLSchema/ttp/1.0.1/ttp.xsd',
         'http://stix.mitre.org/ThreatActor-1': 'http://stix.mitre.org/XMLSchema/threat_actor/1.0.1/threat_actor.xsd',
         'http://stix.mitre.org/default_vocabularies-1': 'http://stix.mitre.org/XMLSchema/default_vocabularies/1.0.1/stix_default_vocabularies.xsd',
         'http://stix.mitre.org/extensions/AP#CAPEC2.6-1': 'http://stix.mitre.org/XMLSchema/extensions/attack_pattern/capec_2.6.1/1.0.1/capec_2.6.1.xsd',
         'http://stix.mitre.org/extensions/Address#CIQAddress3.0-1': 'http://stix.mitre.org/XMLSchema/extensions/address/ciq_address_3.0/1.0.1/ciq_address_3.0.xsd',
         'http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1': 'http://stix.mitre.org/XMLSchema/extensions/identity/ciq_identity_3.0/1.0.1/ciq_identity_3.0.xsd',
         'http://stix.mitre.org/extensions/Malware#MAEC4.0-1': 'http://stix.mitre.org/XMLSchema/extensions/malware/maec_4.0.1/1.0.1/maec_4.0.1.xsd',
         'http://stix.mitre.org/extensions/StructuredCOA#Generic-1': 'http://stix.mitre.org/XMLSchema/extensions/structured_coa/generic/1.0.1/generic.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#Generic-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/generic/1.0.1/generic.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#OVAL5.10-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/oval_5.10/1.0.1/oval_5.10.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#OpenIOC2010-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/open_ioc_2010/1.0.1/open_ioc_2010.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#Snort-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/snort/1.0.1/snort.xsd',
         'http://stix.mitre.org/extensions/TestMechanism#YARA-1': 'http://stix.mitre.org/XMLSchema/extensions/test_mechanism/yara/1.0.1/yara.xsd',
         'http://stix.mitre.org/extensions/Vulnerability#CVRF-1': 'http://stix.mitre.org/XMLSchema/extensions/vulnerability/cvrf_1.1/1.0.1/cvrf_1.1.xsd'}

# Schema locations for namespaces not defined by STIX, but hosted on the STIX website
EXT_NS_TO_SCHEMALOCATION = {'urn:oasis:names:tc:ciq:xpil:3' : 'http://stix.mitre.org/XMLSchema/external/oasis_ciq_3.0/xPIL.xsd',
                            'urn:oasis:names:tc:ciq:xnl:3' : 'http://stix.mitre.org/XMLSchema/external/oasis_ciq_3.0/xNL.xsd'}

# Default namespace->alias mappings. These can be overriden by user-provided dictionaries on export
DEFAULT_STIX_NS_TO_PREFIX = {
     'http://data-marking.mitre.org/Marking-1': 'marking',
     'http://data-marking.mitre.org/extensions/MarkingStructure#TLP-1': 'tlpMarking',
     'http://data-marking.mitre.org/extensions/MarkingStructure#Simple-1': 'simpleMarking',
     'http://stix.mitre.org/Campaign-1': 'campaign',
     'http://stix.mitre.org/CourseOfAction-1': 'coa',
     'http://stix.mitre.org/ExploitTarget-1': 'et',
     'http://stix.mitre.org/Incident-1': 'incident',
     'http://stix.mitre.org/Indicator-2': 'indicator',
     'http://stix.mitre.org/TTP-1': 'ttp',
     'http://stix.mitre.org/ThreatActor-1': 'ta',
     'http://stix.mitre.org/common-1': 'stixCommon',
     'http://stix.mitre.org/default_vocabularies-1': 'stixVocabs',
     'http://stix.mitre.org/extensions/AP#CAPEC2.6-1': 'capecInstance',
     'http://stix.mitre.org/extensions/Address#CIQAddress3.0-1': 'ciqAddress',
     'http://stix.mitre.org/extensions/Identity#CIQIdentity3.0-1': 'ciqIdentity',
     'http://stix.mitre.org/extensions/Malware#MAEC4.0-1': 'maecInstance',
     'http://stix.mitre.org/extensions/StructuredCOA#Generic-1': 'genericStructuredCOA',
     'http://stix.mitre.org/extensions/TestMechanism#Generic-1': 'genericTM',
     'http://stix.mitre.org/extensions/TestMechanism#OVAL5.10-1': 'ovalTM',
     'http://stix.mitre.org/extensions/TestMechanism#OpenIOC2010-1': 'openiocTM',
     'http://stix.mitre.org/extensions/TestMechanism#Snort-1': 'snortTM',
     'http://stix.mitre.org/extensions/TestMechanism#YARA-1': 'yaraTM',
     'http://stix.mitre.org/extensions/Vulnerability#CVRF-1': 'cvrfVuln',
     'http://stix.mitre.org/stix-1': 'stix',
     'http://www.icasi.org/CVRF/schema/common/1.1': 'cvrf-common',
     'http://www.icasi.org/CVRF/schema/cvrf/1.1': 'cvrf',
     'http://www.icasi.org/CVRF/schema/prod/1.1': 'prod',
     'http://www.icasi.org/CVRF/schema/vuln/1.1': 'vuln',
     'http://www.w3.org/1999/xlink': 'xlink',
     'http://www.w3.org/2000/09/xmldsig#': 'ds',
     'http://www.w3.org/2001/XMLSchema': 'xs',
     'http://www.w3.org/2001/XMLSchema-instance': 'xsi'}

DEFAULT_EXT_TO_PREFIX = {'urn:oasis:names:tc:ciq:xpil:3' : 'xpil',
                         'urn:oasis:names:tc:ciq:xnl:3' : 'xnl'}
<|endoftext|>


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

"""
    obelisk-cardiograph
    Monitor obelisk servers' heartbeat.

    Based on "Pubsub envelope subscriber" example from zguide
    Author: Guillaume Aubert (gaubert) <guillaume(dot)aubert(at)gmail(dot)com>

"""
import zmq

def main():
    """ main method """

    # Prepare our context and publisher
    context    = zmq.Context()
    subscriber = context.socket(zmq.SUB)
    subscriber.connect("tcp://79.98.29.93:9092")
    subscriber.setsockopt(zmq.SUBSCRIBE, b"B")

    print("Entering main loop.")
    while True:
        # Read envelope with address
        [address, contents] = subscriber.recv_multipart()
        print("[%s] %s" % (address, contents))

    # We never get here but clean up anyhow
    subscriber.close()
    context.term()

if __name__ == "__main__":
    main()
<commit_msg>Minimal working version.<commit_after>#!/usr/bin/env python

"""
    obelisk-cardiograph
    Monitor obelisk servers' heartbeat.

    Based on "Pubsub envelope subscriber" example from zguide
    Author: Guillaume Aubert (gaubert) <guillaume(dot)aubert(at)gmail(dot)com>

"""
import zmq

def main():
    """ main method """

    serverip = '79.98.29.93'
    serverport = '9092'

    ctx = zmq.Context()
    s = ctx.socket(zmq.SUB)
    s.connect('tcp://' + serverip + ':' + serverport)
    s.setsockopt(zmq.SUBSCRIBE, b'')    # subscribe to everything

    print("Entering main loop.")
    while True:
        reply = s.recv()
        reply = reply[::-1] # obelisk sent little-endian
        data = ':'.join(hex(x)[2:] for x in reply)
        print(serverip, data)

    # We never get here but clean up anyhow
    s.close()
    ctx.term()

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2010, Pall Sigurdsson <palli@opensource.is>
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This script 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.shortcuts import render_to_response, redirect
from django.core import serializers
from django.http import HttpResponse, HttpResponseServerError
from django.utils import simplejson
from django.core.context_processors import csrf

import sys

from pynag.Model import *
from pynag import Model
from forms import *

def home(request):
    return redirect('adagios')

## DEPRECATED for list_objects
def list_object(request, object_type):
    c = {}
    return render_to_response('hosts.html', c)

## Deprecated for list_objects
def list_contacts(request):
    c = {}
    c['contacts'] = Contact.objects.all

    return render_to_response('configurator/list_contacts.html', c)

def list_objects( request, object_type=None, display_these_objects=None ):
    """ Finds Pynag objects and returns them in a pretty list. search filter can be applied via querystring

    Arguments:
        object_type(str) : Only find pynag object of this type
        display_these_objects([ObjectDefinition]) : Instead of searching, simply return these objects

    """
    c = {}                      # This hash is sent to our template
    c['messages'] = m = []      # If we want to get any messages or errors accross
    c['objects'] = [] # Our pynag objects are collected in here
    c['object_type'] = object_type
    # Validate any potential search terms sent via querystring
    search = {}
    for k,v in request.GET.items():
        k,v = str(k), str(v)
        if k == 'object_type':
            object_type = v
        if v == 'None':
            search[k] = None
        else:
            search[k] = v

    # If a non-existent object_type is requested, lets display a warning
    # Model.string_to_class contains a hash map that convert string value to its respective class definition
    Pynag = Model.string_to_class.get(object_type, Model.ObjectDefinition)

    # Lets decide if we want to get everything or apply a filter
    if display_these_objects is not None:
        c['objects'] = display_these_objects
    elif len(search) == 0:
        c['objects'] = Pynag.objects.all
    else:
        c['objects'] = Pynag.objects.filter(**search)

    return render_to_response('list_objects.html', c)

def list_object_types(request):
    ''' Collects statistics about pynag objects and returns to template '''
    c = {}
    c['object_types'] = []
    for name,Class in Model.string_to_class.items():
        if name != None:
            active = inactive = 0
            all_instances = Class.objects.all
            for i in all_instances:
                if i['register'] == "0":
                    inactive += 1
                else:
                    active += 1
            c['object_types'].append( (name, active, inactive) )
    return render_to_response('list_object_types.html', c)

def view_object( request, object_id=None, object_type=None, shortname=None):
    ''' View details about one specific pynag object '''
    c = {}
    c.update(csrf(request))
    c['messages'] = m = []
    c['errors'] = []

    # Get our object
    if object_id != None:
        o = ObjectDefinition.objects.get_by_id(id=object_id)
    elif object_type != None and shortname != None:
        # TODO: if multiple objects are found, display a list
        otype = Model.string_to_class.get(object_type, Model.ObjectDefiniton)
        o = otype.objects.get_by_shortname(shortname)
    else:
        raise ValueError("Object not found")

    if request.method == 'POST':
        if request.POST.has_key('definition'):
            'Manual edit of the form'
            manual_edit = ManualEditObjectForm(data=request.POST, pynag_object=o)
            if manual_edit.is_valid():
                manual_edit.save()
                m.append("Object Saved manually to '%s'" % o['filename'])
            else:
                m.append( "Failed to save object")
        else:
            "this is the 'advanced_edit' form "
            c['form'] = PynagForm( pynag_object=o,initial=o._original_attributes, data=request.POST )
            if c['form'].is_valid():
                c['form'].save()
                m.append("Object Saved to %s" % o['filename'])
            else:
                c['errors'].append( "Problem reading form input")

    c['inherited_attributes'] = PynagForm(pynag_object=o, initial=o._original_attributes, show_defined_attributes=False,   show_inherited_attributes=True, show_undefined_attributes=False)
    c['defined_attributes'] = PynagForm(pynag_object=o, initial=o._original_attributes,   show_undefined_attributes=False, show_inherited_attributes=False)
    c['undefined_attributes'] = PynagForm(pynag_object=o, initial=o._original_attributes, show_defined_attributes=False,   show_inherited_attributes=False)
    c['form'] = PynagForm( pynag_object=o, initial=o._original_attributes )

    c['my_object'] = o
    c['manual_edit'] = ManualEditObjectForm(initial={'definition':o['meta']['raw_definition'], })
    if o['object_type'] == 'host':
        return _view_host(request, c)
    elif o['object_type'] == 'service':
        return _view_service(request, c)
    elif o['object_type'] == 'contact':
        return _view_contact(request, c)
    elif o['object_type'] == 'contactgroup':
        return _view_contactgroup(request, c)
    try: c['command_line'] = o.get_effective_command_line()
    except: pass
    try: c['object_macros'] = o.get_all_macros()
    except: pass
    try: c['effective_hostgroups'] = o.get_effective_hostgroups()
    except: pass
    try: c['effective_contacts'] = o.get_effective_contacts()
    except: pass
    try: c['effective_contactgroups'] = o.get_effective_contact_groups()
    except: pass
    try: c['effective_contactgroups'] = o.get_effective_contactgroups()
    except: pass
    try: c['effective_members'] = o.get_effective_members()
    except: pass
    return render_to_response('view_object.html', c)

def _view_contactgroup( request, c):
    ''' This is a helper function to view_object '''
    try: c['effective_members'] = c['my_object'].get_effective_members()
    except: pass
    return render_to_response('view_contactgroup.html', c)
def _view_contact( request, c):
    ''' This is a helper function to view_object '''
    try: c['effective_contactgroups'] = c['my_object'].get_effective_contactgroups()
    except: pass
    return render_to_response('view_contact.html', c)

def _view_service( request, c):
    ''' This is a helper function to view_object '''
    service = c['my_object']
    try: c['command_line'] = service.get_effective_command_line()
    except: c['errors'].append( "Configuration error while looking up command_line")

    try: c['effective_servicegroups'] = service.get_effective_servicegroups()
    except: c['errors'].append( "Configuration error while looking up servicegroups")

    try: c['effective_contacts'] = service.get_effective_contacts()
    except: c['errors'].append( "Configuration error while looking up contacts")

    try: c['effective_contactgroups'] = service.get_effective_contact_groups()
    except: c['errors'].append( "Configuration error while looking up contact_groups")

    try: c['object_macros'] = service.get_all_macros()
    except: c['errors'].append( "Configuration error while looking up macros")

    return render_to_response('view_service.html', c)
def _view_host( request, c):
    ''' This is a helper function to view_object '''
    host = c['my_object']
    if not c.has_key('errors'): c['errors'] = []

    try: c['effective_services'] = host.get_effective_services()
    except: c['errors'].append( "Configuration error while looking up services")

    try: c['command_line'] = host.get_effective_command_line()
    except: c['errors'].append( "Configuration error while looking up command_line")

    try: c['effective_hostgroups'] = host.get_effective_hostgroups()
    except: c['errors'].append( "Configuration error while looking up hostgroups")

    try: c['effective_contacts'] = host.get_effective_contacts()
    except: c['errors'].append( "Configuration error while looking up contacts")

    c['effective_contactgroups'] = host.get_effective_contact_groups()
    c['errors'].append( "Configuration error while looking up contact_groups")

    try: c['object_macros'] = host.get_all_macros()
    except: c['errors'].append( "Configuration error while looking up macros")

    return render_to_response('view_host.html', c)

def confighealth( request  ):
    c = {}
    c['messages'] = m = []
    c['objecthealth'] = s = {}
    c['booleans'] = {}
    services_no_description = Service.objects.filter(register="1", service_description=None)
    hosts_without_contacts = []
    hosts_with_invalid_contacts = []
    hosts_without_services =[]
    objects_with_invalid_parents = []
    services_without_contacts = []
    services_using_hostgroups = []
    services_without_icon_image = []
    for i in ObjectDefinition.objects.all:
        continue
        try:
            i.get_parents()
        except ValueError:
            objects_with_invalid_parents.append(i)
    for i in Host.objects.filter(register="1"):
            if i['contacts'] is None and i['contact_groups'] is None:
                hosts_without_contacts.append(i)
            if i.get_effective_services() == []:
                hosts_without_services.append(i)
    for i in Service.objects.filter(register="1"):
            if i['contacts'] is None and i['contact_groups'] is None:
                services_without_contacts.append(i)
            if i['hostgroups'] is not None:
                services_using_hostgroups.append(i)
            if i['icon_image'] is None:
                services_without_icon_image.append(i)
    c['booleans']['Nagios Service has been reloaded since last configuration change'] = not Model.config.needs_reload()
    c['booleans']['Adagios configuration cache is up-to-date'] = not Model.config.needs_reparse()
    import configurator.okconfig
    c['booleans']['OKConfig is installed and working'] = configurator.okconfig.is_valid()
    s['Services with no "service_description"'] = services_no_description
    s['Hosts without any contacts'] = hosts_without_contacts
    s['Services without any contacts'] = services_without_contacts
    s['Objects with invalid "use" attribute'] = objects_with_invalid_parents
    s['Services applied to hostgroups'] = services_using_hostgroups
    s['Services without a logo'] = services_without_icon_image
    s['Hosts without Service Checks'] = hosts_without_services
    if request.GET.has_key('show') and s.has_key( request.GET['show'] ):
        objects =  s[request.GET['show']]
        return list_objects(request,display_these_objects=objects )
    else:
        return render_to_response('suggestions.html', c)

def show_plugins(request):
    ''' Finds all command_line arguments, and shows missing plugins '''
    c = {}
    missing_plugins = []
    existing_plugins = []
    finished = []
    services = Service.objects.all
    common_interpreters = ['perl','python','sh','bash']
    for s in services:
        if not 'check_command' in s._defined_attributes: continue
        check_command = s.check_command.split('!')[0]
        if check_command in finished: continue
        finished.append( check_command )
        command_line = s.get_effective_command_line()
        if command_line is None: continue
        command_line = command_line.split()
        command_name = command_line.pop(0)
        if command_name in common_interpreters: command_name = command_line.pop(0)
        if os.path.exists(command_name):
            existing_plugins.append( (check_command, command_name) )
        else:
            missing_plugins.append( (check_command, command_name) )
    c['missing_plugins'] = missing_plugins
    c['existing_plugins'] = existing_plugins
    return render_to_response('show_plugins.html', c)

def view_parents(request):
    c = {}
    parents = {}
    hosts = Host.objects.all
    for h in hosts:
        for parent in h.get_parents():
            name = parent.name
            if not parents.has_key( name ):
                parents[ name ] = {'children':[], 'num_children':0, 'name':name}
                parents[ name ]
            parents[name]['children'].append ( h )
            parents[name]['num_children'] += 1
    c['parents'] = []
    for i in parents.keys():
        c['parents'].append( parents[i] )
    return render_to_response('parents.html', c)
def view_nagioscfg(request):
    c = {}
    c['filename'] = Model.config.cfg_file
    c['content'] = Model.config.maincfg_values
    c['content'].sort()
    return render_to_response('view_configfile.html', c)

 <commit_msg>Added GitEventHandler for automated git commits on changes<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2010, Pall Sigurdsson <palli@opensource.is>
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This script 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.shortcuts import render_to_response, redirect
from django.core import serializers
from django.http import HttpResponse, HttpResponseServerError
from django.utils import simplejson
from django.core.context_processors import csrf

import sys
from os.path import dirname

from pynag.Model import *
from pynag import Model
from pynag.Model import EventHandlers
from forms import *

try:
    # Hook up git event handler
    Model.eventhandlers.append(Model.EventHandlers.GitEventHandler(dirname(Model.cfg_file), 'adagios', 'tommi'))
except:
    pass

def home(request):
    return redirect('adagios')

## DEPRECATED for list_objects
def list_object(request, object_type):
    c = {}
    return render_to_response('hosts.html', c)

## Deprecated for list_objects
def list_contacts(request):
    c = {}
    c['contacts'] = Contact.objects.all

    return render_to_response('configurator/list_contacts.html', c)

def list_objects( request, object_type=None, display_these_objects=None ):
    """ Finds Pynag objects and returns them in a pretty list. search filter can be applied via querystring

    Arguments:
        object_type(str) : Only find pynag object of this type
        display_these_objects([ObjectDefinition]) : Instead of searching, simply return these objects

    """
    c = {}                      # This hash is sent to our template
    c['messages'] = m = []      # If we want to get any messages or errors accross
    c['objects'] = [] # Our pynag objects are collected in here
    c['object_type'] = object_type
    # Validate any potential search terms sent via querystring
    search = {}
    for k,v in request.GET.items():
        k,v = str(k), str(v)
        if k == 'object_type':
            object_type = v
        if v == 'None':
            search[k] = None
        else:
            search[k] = v

    # If a non-existent object_type is requested, lets display a warning
    # Model.string_to_class contains a hash map that convert string value to its respective class definition
    Pynag = Model.string_to_class.get(object_type, Model.ObjectDefinition)

    # Lets decide if we want to get everything or apply a filter
    if display_these_objects is not None:
        c['objects'] = display_these_objects
    elif len(search) == 0:
        c['objects'] = Pynag.objects.all
    else:
        c['objects'] = Pynag.objects.filter(**search)

    return render_to_response('list_objects.html', c)

def list_object_types(request):
    ''' Collects statistics about pynag objects and returns to template '''
    c = {}
    c['object_types'] = []
    for name,Class in Model.string_to_class.items():
        if name != None:
            active = inactive = 0
            all_instances = Class.objects.all
            for i in all_instances:
                if i['register'] == "0":
                    inactive += 1
                else:
                    active += 1
            c['object_types'].append( (name, active, inactive) )
    return render_to_response('list_object_types.html', c)

def view_object( request, object_id=None, object_type=None, shortname=None):
    ''' View details about one specific pynag object '''
    c = {}
    c.update(csrf(request))
    c['messages'] = m = []
    c['errors'] = []

    # Get our object
    if object_id != None:
        o = ObjectDefinition.objects.get_by_id(id=object_id)
    elif object_type != None and shortname != None:
        # TODO: if multiple objects are found, display a list
        otype = Model.string_to_class.get(object_type, Model.ObjectDefiniton)
        o = otype.objects.get_by_shortname(shortname)
    else:
        raise ValueError("Object not found")

    if request.method == 'POST':
        if request.POST.has_key('definition'):
            'Manual edit of the form'
            manual_edit = ManualEditObjectForm(data=request.POST, pynag_object=o)
            if manual_edit.is_valid():
                manual_edit.save()
                m.append("Object Saved manually to '%s'" % o['filename'])
            else:
                m.append( "Failed to save object")
        else:
            "this is the 'advanced_edit' form "
            c['form'] = PynagForm( pynag_object=o,initial=o._original_attributes, data=request.POST )
            if c['form'].is_valid():
                c['form'].save()
                m.append("Object Saved to %s" % o['filename'])
            else:
                c['errors'].append( "Problem reading form input")

    c['inherited_attributes'] = PynagForm(pynag_object=o, initial=o._original_attributes, show_defined_attributes=False,   show_inherited_attributes=True, show_undefined_attributes=False)
    c['defined_attributes'] = PynagForm(pynag_object=o, initial=o._original_attributes,   show_undefined_attributes=False, show_inherited_attributes=False)
    c['undefined_attributes'] = PynagForm(pynag_object=o, initial=o._original_attributes, show_defined_attributes=False,   show_inherited_attributes=False)
    c['form'] = PynagForm( pynag_object=o, initial=o._original_attributes )

    c['my_object'] = o
    c['manual_edit'] = ManualEditObjectForm(initial={'definition':o['meta']['raw_definition'], })
    if o['object_type'] == 'host':
        return _view_host(request, c)
    elif o['object_type'] == 'service':
        return _view_service(request, c)
    elif o['object_type'] == 'contact':
        return _view_contact(request, c)
    elif o['object_type'] == 'contactgroup':
        return _view_contactgroup(request, c)
    try: c['command_line'] = o.get_effective_command_line()
    except: pass
    try: c['object_macros'] = o.get_all_macros()
    except: pass
    try: c['effective_hostgroups'] = o.get_effective_hostgroups()
    except: pass
    try: c['effective_contacts'] = o.get_effective_contacts()
    except: pass
    try: c['effective_contactgroups'] = o.get_effective_contact_groups()
    except: pass
    try: c['effective_contactgroups'] = o.get_effective_contactgroups()
    except: pass
    try: c['effective_members'] = o.get_effective_members()
    except: pass
    return render_to_response('view_object.html', c)

def _view_contactgroup( request, c):
    ''' This is a helper function to view_object '''
    try: c['effective_members'] = c['my_object'].get_effective_members()
    except: pass
    return render_to_response('view_contactgroup.html', c)
def _view_contact( request, c):
    ''' This is a helper function to view_object '''
    try: c['effective_contactgroups'] = c['my_object'].get_effective_contactgroups()
    except: pass
    return render_to_response('view_contact.html', c)

def _view_service( request, c):
    ''' This is a helper function to view_object '''
    service = c['my_object']
    try: c['command_line'] = service.get_effective_command_line()
    except: c['errors'].append( "Configuration error while looking up command_line")

    try: c['effective_servicegroups'] = service.get_effective_servicegroups()
    except: c['errors'].append( "Configuration error while looking up servicegroups")

    try: c['effective_contacts'] = service.get_effective_contacts()
    except: c['errors'].append( "Configuration error while looking up contacts")

    try: c['effective_contactgroups'] = service.get_effective_contact_groups()
    except: c['errors'].append( "Configuration error while looking up contact_groups")

    try: c['object_macros'] = service.get_all_macros()
    except: c['errors'].append( "Configuration error while looking up macros")

    return render_to_response('view_service.html', c)
def _view_host( request, c):
    ''' This is a helper function to view_object '''
    host = c['my_object']
    if not c.has_key('errors'): c['errors'] = []

    try: c['effective_services'] = host.get_effective_services()
    except: c['errors'].append( "Configuration error while looking up services")

    try: c['command_line'] = host.get_effective_command_line()
    except: c['errors'].append( "Configuration error while looking up command_line")

    try: c['effective_hostgroups'] = host.get_effective_hostgroups()
    except: c['errors'].append( "Configuration error while looking up hostgroups")

    try: c['effective_contacts'] = host.get_effective_contacts()
    except: c['errors'].append( "Configuration error while looking up contacts")

    c['effective_contactgroups'] = host.get_effective_contact_groups()
    c['errors'].append( "Configuration error while looking up contact_groups")

    try: c['object_macros'] = host.get_all_macros()
    except: c['errors'].append( "Configuration error while looking up macros")

    return render_to_response('view_host.html', c)

def confighealth( request  ):
    c = {}
    c['messages'] = m = []
    c['objecthealth'] = s = {}
    c['booleans'] = {}
    services_no_description = Service.objects.filter(register="1", service_description=None)
    hosts_without_contacts = []
    hosts_with_invalid_contacts = []
    hosts_without_services =[]
    objects_with_invalid_parents = []
    services_without_contacts = []
    services_using_hostgroups = []
    services_without_icon_image = []
    for i in ObjectDefinition.objects.all:
        continue
        try:
            i.get_parents()
        except ValueError:
            objects_with_invalid_parents.append(i)
    for i in Host.objects.filter(register="1"):
            if i['contacts'] is None and i['contact_groups'] is None:
                hosts_without_contacts.append(i)
            if i.get_effective_services() == []:
                hosts_without_services.append(i)
    for i in Service.objects.filter(register="1"):
            if i['contacts'] is None and i['contact_groups'] is None:
                services_without_contacts.append(i)
            if i['hostgroups'] is not None:
                services_using_hostgroups.append(i)
            if i['icon_image'] is None:
                services_without_icon_image.append(i)
    c['booleans']['Nagios Service has been reloaded since last configuration change'] = not Model.config.needs_reload()
    c['booleans']['Adagios configuration cache is up-to-date'] = not Model.config.needs_reparse()
    import configurator.okconfig
    c['booleans']['OKConfig is installed and working'] = configurator.okconfig.is_valid()
    s['Services with no "service_description"'] = services_no_description
    s['Hosts without any contacts'] = hosts_without_contacts
    s['Services without any contacts'] = services_without_contacts
    s['Objects with invalid "use" attribute'] = objects_with_invalid_parents
    s['Services applied to hostgroups'] = services_using_hostgroups
    s['Services without a logo'] = services_without_icon_image
    s['Hosts without Service Checks'] = hosts_without_services
    if request.GET.has_key('show') and s.has_key( request.GET['show'] ):
        objects =  s[request.GET['show']]
        return list_objects(request,display_these_objects=objects )
    else:
        return render_to_response('suggestions.html', c)

def show_plugins(request):
    ''' Finds all command_line arguments, and shows missing plugins '''
    c = {}
    missing_plugins = []
    existing_plugins = []
    finished = []
    services = Service.objects.all
    common_interpreters = ['perl','python','sh','bash']
    for s in services:
        if not 'check_command' in s._defined_attributes: continue
        check_command = s.check_command.split('!')[0]
        if check_command in finished: continue
        finished.append( check_command )
        command_line = s.get_effective_command_line()
        if command_line is None: continue
        command_line = command_line.split()
        command_name = command_line.pop(0)
        if command_name in common_interpreters: command_name = command_line.pop(0)
        if os.path.exists(command_name):
            existing_plugins.append( (check_command, command_name) )
        else:
            missing_plugins.append( (check_command, command_name) )
    c['missing_plugins'] = missing_plugins
    c['existing_plugins'] = existing_plugins
    return render_to_response('show_plugins.html', c)

def view_parents(request):
    c = {}
    parents = {}
    hosts = Host.objects.all
    for h in hosts:
        for parent in h.get_parents():
            name = parent.name
            if not parents.has_key( name ):
                parents[ name ] = {'children':[], 'num_children':0, 'name':name}
                parents[ name ]
            parents[name]['children'].append ( h )
            parents[name]['num_children'] += 1
    c['parents'] = []
    for i in parents.keys():
        c['parents'].append( parents[i] )
    return render_to_response('parents.html', c)
def view_nagioscfg(request):
    c = {}
    c['filename'] = Model.config.cfg_file
    c['content'] = Model.config.maincfg_values
    c['content'].sort()
    return render_to_response('view_configfile.html', c)

<|endoftext|>