Example #1
0
def openDatFile(datpath):
    '''
    Open a file-like object using a pkg relative path.

    Example:

        fd = openDatFile('foopkg.barpkg/wootwoot.bin')

    Notes:

        * This API supports datfiles in the plain filesystem,
          embedded within pyz files, and datfiles included in
          mindmeld code bundles.
    '''
    pkgname, filename = datpath.split('/', 1)

    pkgmod = s_dyndeps.getDynMod(pkgname)

    loader = getattr(pkgmod, '__loader__', None)
    if isinstance(loader, s_mindmeld.MindMeld):
        return loader.openDatFile(datpath)

    # are we a regular file?
    pkgfile = os.path.abspath(pkgmod.__file__)
    if os.path.isfile(pkgfile):
        dirname = os.path.dirname(pkgfile)
        datname = os.path.join(dirname, filename)
        return open(datname, 'rb')
Example #2
0
def getModDef(name):
    '''
    Build a moddef tufo for the given module name.

    Example:

        moddef = getModDef('synapse.mindmeld')

    '''
    mod = s_dyndeps.getDynMod(name)
    if mod == None:
        return None

    modpath = abspath(mod.__file__)
    if modpath.endswith('.pyc'):
        modpath = modpath[:-1]

    modbase = os.path.basename(modpath)
    modinfo = _getModInfo(modbase)

    if modinfo != None:
        return tufo(name, path=modpath, **modinfo)

    # hrm...  what now smart guy?!?!
    if name in sys.builtin_module_names:
        return tufo(name, fmt='bin')
Example #3
0
def getModDef(name):
    '''
    Build a moddef tufo for the given module name.

    Example:

        moddef = getModDef('synapse.mindmeld')

    '''
    mod = s_dyndeps.getDynMod(name)
    if mod == None:
        return None

    modpath = getattr(mod, '__file__', None)
    if modpath == None:
        return None

    if modpath.endswith('.pyc'):
        modpath = modpath[:-1]

    modbase = os.path.basename(modpath)
    modinfo = _getModInfo(modbase)

    if modinfo != None:
        return tufo(name, path=modpath, **modinfo)

    # hrm...  what now smart guy?!?!
    if name in sys.builtin_module_names:
        return tufo(name, fmt='bin')
Example #4
0
    async def _loadDmonConf(self):

        # process per-conf elements...
        for name in self.conf.get('modules', ()):
            try:
                self.mods[name] = s_dyndeps.getDynMod(name)
            except Exception as e:
                logger.exception('dmon module error')

        lisn = self.conf.get('listen')
        if lisn is not None:
            self.addr = await self.listen(lisn)
Example #5
0
def openDatFile(datpath):
    '''
    Open a file-like object using a pkg relative path.

    Example:

        fd = openDatFile('foopkg.barpkg/wootwoot.bin')
    '''
    pkgname, filename = datpath.split('/', 1)

    pkgmod = s_dyndeps.getDynMod(pkgname)

    # are we a regular file?
    pkgfile = os.path.abspath(pkgmod.__file__)
    if os.path.isfile(pkgfile):
        dirname = os.path.dirname(pkgfile)
        datname = os.path.join(dirname, filename)
        return open(datname, 'rb')
Example #6
0
 def test_dyndeps_dynmod(self):
     self.assertIsNone( s_dyndeps.getDynMod('- -') )
     self.assertIsNotNone( s_dyndeps.getDynMod('sys') )
Example #7
0
    def _loadDmonConf(self, conf):

        checkConfDict(conf)
        self.locs.update(conf.get('vars', {}))

        # handle forks first to prevent socket bind weirdness
        for name, subconf in conf.get('forks', ()):
            self._fireDmonFork(name, subconf)

        title = conf.get('title')
        if title is not None:
            s_thisplat.setProcName('dmon: %s' % title)

        # handle explicit module load requests
        for name, info in conf.get('modules', ()):
            modu = s_dyndeps.getDynMod(name)
            if modu is None:
                logger.warning('dmon mod not loaded: %s', name)

        # handle includes next
        for path in conf.get('includes', ()):
            fullpath = os.path.expanduser(path)
            try:
                self.loadDmonFile(fullpath)
            except Exception as e:
                raise Exception('Include Error (%s): %s' % (path, e))

        configs = conf.get('configs', {})

        for row in conf.get('ctors', ()):

            copts = {}  # ctor options
            if len(row) == 2:
                name, url = row
            elif len(row) == 3:
                name, url, copts = row
            else:
                raise Exception('Invalid ctor row: %r' % (row, ))

            if url.find('://') == -1:
                # this is a (name,dynfunc,config) formatted ctor...
                item = s_dyndeps.tryDynFunc(url, copts)
            else:
                item = self.dmoneval(url)

            self.locs[name] = item

            # check for a ctor opt that wants us to load a config dict by name
            cfgname = copts.get('config')
            if cfgname is not None:
                if not isinstance(item, s_config.Configable):
                    raise Exception('dmon ctor: %s does not support configs' %
                                    name)

                opts = configs.get(cfgname)
                if opts is None:
                    raise s_common.NoSuchConf(name=cfgname)

                item.setConfOpts(opts)

            # check for a ctor opt that wants us to "flatten" several configs in order
            cfgnames = copts.get('configs')
            if cfgnames is not None:

                if not isinstance(item, s_config.Configable):
                    raise Exception('dmon ctor: %s does not support configs' %
                                    name)

                opts = {}
                for cfgname in cfgnames:

                    cfgopts = configs.get(cfgname)
                    if cfgopts is None:
                        raise s_common.NoSuchConf(name=cfgname)

                    opts.update(cfgopts)

                item.setConfOpts(opts)

            # check for a match between config and ctor names
            opts = configs.get(name)
            if opts is not None:
                item.setConfOpts(opts)

            self.fire('dmon:conf:ctor', name=name, item=item)

        for busname, svcruns in conf.get('services', ()):

            svcbus = self.dmoneval(busname)
            if svcbus is None:
                raise s_common.NoSuchObj(name)

            for svcname, svcopts in svcruns:

                item = self.locs.get(svcname)
                if item is None:
                    raise s_common.NoSuchObj(svcname)

                svcname = svcopts.get('name', svcname)
                s_service.runSynSvc(svcname, item, svcbus, **svcopts)
Example #8
0
 def test_dyndeps_dynmod(self):
     self.assertIsNone(s_dyndeps.getDynMod('- -'))
     self.assertIsNotNone(s_dyndeps.getDynMod('sys'))
Example #9
0
 def test_dyndeps_dynmod(self):
     self.none(s_dyndeps.getDynMod('- -'))
     self.nn(s_dyndeps.getDynMod('sys'))
Example #10
0
import os

import synapse.compat as s_compat
import synapse.dyndeps as s_dyndeps
import synapse.lib.socket as s_socket

from synapse.links.common import *

paramiko = s_dyndeps.getDynMod('paramiko')


class SshRelay(LinkRelay):
    '''
    Implements the SSH link protocol for synapse.

    ssh://[user[:passwd]@]<host>[:port]/<name>?forward=<host:port>[&keyfile=<path>]

    '''
    proto = 'ssh'

    def _reqValidLink(self):

        if paramiko is None:
            raise Exception('paramiko module not installed')

        if self.link[1].get('port') is None:
            self.link[1]['port'] = 22

        host = self.link[1].get('host')
        if host is None:
            raise s_common.PropNotFound('host')
Example #11
0
import synapse.compat as s_compat
import synapse.dyndeps as s_dyndeps
import synapse.lib.socket as s_socket

from synapse.common import *
from synapse.links.common import *

paramiko = s_dyndeps.getDynMod('paramiko')

class SshRelay(LinkRelay):
    '''
    Implements the SSH link protocol for synapse.

    ssh://[user[:passwd]@]<host>[:port]/<name>?forward=<host:port>[&keyfile=<path>]

    '''
    proto = 'ssh'

    def _reqValidLink(self):

        if paramiko == None:
            raise Exception('paramiko module not installed')

        if self.link[1].get('port') == None:
            self.link[1]['port'] = 22

        host = self.link[1].get('host')
        if host == None:
            raise PropNotFound('host')

        fwdstr = self.link[1].get('forward')