Beispiel #1
0
def main(args):
    parser = _createParser()

    log.debug('manager', 'Parsing arguments (%r)' % ', '.join(args))
    options, args = parser.parse_args(args)

    # Force options down configure's throat
    for d in ['logdir', 'rundir']:
        o = getattr(options, d, None)
        if o:
            log.debug('manager', 'Setting configure.%s to %s' % (d, o))
            setattr(configure, d, o)

    # parse planet config file
    if len(args) <= 1:
        log.warning('manager', 'Please specify a planet configuration file')
        sys.stderr.write("Please specify a planet configuration file.\n")
        return 1

    planetFile = args[1]
    try:
        cfg = config.ManagerConfigParser(planetFile)
    except IOError, e:
        sys.stderr.write("ERROR: Could not read configuration from '%s':\n" %
            planetFile)
        sys.stderr.write("ERROR: %s\n" % e.strerror)
        return 1
Beispiel #2
0
def main(args):
    parser = _createParser()

    log.debug("manager", "Parsing arguments (%r)" % ", ".join(args))
    options, args = parser.parse_args(args)

    # Force options down configure's throat
    for d in ["logdir", "rundir"]:
        o = getattr(options, d, None)
        if o:
            log.debug("manager", "Setting configure.%s to %s" % (d, o))
            setattr(configure, d, o)

    # parse planet config file
    if len(args) <= 1:
        log.warning("manager", "Please specify a planet configuration file")
        sys.stderr.write("Please specify a planet configuration file.\n")
        return 1

    planetFile = args[1]
    try:
        cfg = config.ManagerConfigParser(planetFile)
    except IOError, e:
        sys.stderr.write("ERROR: Could not read configuration from '%s':\n" % planetFile)
        sys.stderr.write("ERROR: %s\n" % e.strerror)
        return 1
Beispiel #3
0
def getRecentConnections():
    """
    Fetches a list of recently used connections
    @returns: recently used connections
    @rtype: list of L{RecentConnection}
    """

    def _parseConnection(filename):
        tree = minidom.parse(filename)
        state = {}
        for childNode in tree.documentElement.childNodes:
            if (childNode.nodeType != Node.TEXT_NODE and
                childNode.nodeType != Node.COMMENT_NODE):
                state[childNode.nodeName] = childNode.childNodes[0].wholeText
        state['port'] = int(state['port'])
        state['use_insecure'] = (state['use_insecure'] != '0')
        authenticator = Authenticator(username=state['user'],
                                      password=state['passwd'])
        return PBConnectionInfo(state['host'], state['port'],
                                not state['use_insecure'],
                                authenticator)

    recentFilenames = _getRecentFilenames()
    recentConnections = []
    for filename in sorted(recentFilenames, reverse=True):
        try:
            state = _parseConnection(filename)
            recentConnections.append(
                RecentConnection(str(state),
                                 filename=filename,
                                 info=state))
        except Exception, e:
            log.warning('connections', 'Error parsing %s: %r', filename, e)
Beispiel #4
0
    def _cbRequestAvatarId(self, validKey, credentials):
        if not validKey:
            return failure.Failure(UnauthorizedLogin())
        if not credentials.signature:
            return failure.Failure(error.ValidPublicKey())
        else:
            try:
                if conch.version.major < 10:
                    pubKey = keys.getPublicKeyObject(data=credentials.blob)
                    if keys.verifySignature(pubKey, credentials.signature,
                                            credentials.sigData):
                        return credentials.username
                else:
                    pubKey = keys.Key.fromString(credentials.blob)
                    if pubKey.verify(credentials.signature,
                        credentials.sigData):
                        return credentials.username

            except: # any error should be treated as a failed login
                f = failure.Failure()
                log.warning('manhole',
                    'error checking signature on creds %r: %r',
                        credentials, log.getFailureMessage(f))
                return f
        return failure.Failure(UnauthorizedLogin())
Beispiel #5
0
    def _cbRequestAvatarId(self, validKey, credentials):
        if not validKey:
            return failure.Failure(UnauthorizedLogin())
        if not credentials.signature:
            return failure.Failure(error.ValidPublicKey())
        else:
            try:
                if conch.version.major < 10:
                    pubKey = keys.getPublicKeyObject(data=credentials.blob)
                    if keys.verifySignature(pubKey, credentials.signature,
                                            credentials.sigData):
                        return credentials.username
                else:
                    pubKey = keys.Key.fromString(credentials.blob)
                    if pubKey.verify(credentials.signature,
                                     credentials.sigData):
                        return credentials.username

            except:  # any error should be treated as a failed login
                f = failure.Failure()
                log.warning('manhole',
                            'error checking signature on creds %r: %r',
                            credentials, log.getFailureMessage(f))
                return f
        return failure.Failure(UnauthorizedLogin())
Beispiel #6
0
 def read(self):
     try:
         tree = minidom.parse(self._filename)
         node = tree.getElementsByTagName('gtk-admin-state')[0]
         for elem in node.childNodes:
             self._values[elem.nodeName] = elem.firstChild.data
     except (IOError, IndexError, ExpatError), e:
         log.warning('Cannot read gtl-admin-state %s',
                     log.getExceptionMessage(e))
Beispiel #7
0
 def read(self):
     try:
         tree = minidom.parse(self._filename)
         node = tree.getElementsByTagName('gtk-admin-state')[0]
         for elem in node.childNodes:
             self._values[elem.nodeName] = elem.firstChild.data
     except (IOError, IndexError, ExpatError), e:
         log.warning('Cannot read gtl-admin-state %s',
                     log.getExceptionMessage(e))
Beispiel #8
0
def handleGStreamerDeviceError(failure, device, mid=None):
    """
    Handle common GStreamer GstErrors or other.
    Return a message or None.
    """
    if not mid:
        log.warning('check',
            'handleGStreamerDeviceError: no message id specified')

    m = None

    if failure.check(errors.GStreamerGstError):
        source, gerror, debug = failure.value.args
        log.debug('check',
            'GStreamer GError: %s (domain %s, code %d, debug %s)' % (
                gerror.message, gerror.domain, gerror.code, debug))

        if gerror.domain == "gst-resource-error-quark":
            if gerror.code == int(Gst.RESOURCE_ERROR_OPEN_READ):
                m = messages.Error(T_(
                    N_("Could not open device '%s' for reading.  "
                       "Check permissions on the device."), device), mid=mid)
                documentation.messageAddFixBadPermissions(m)
            if gerror.code == int(Gst.RESOURCE_ERROR_OPEN_WRITE):
                m = messages.Error(T_(
                    N_("Could not open device '%s' for writing.  "
                       "Check permissions on the device."), device), mid=mid)
                documentation.messageAddFixBadPermissions(m)
            elif gerror.code == int(Gst.RESOURCE_ERROR_OPEN_READ_WRITE):
                m = messages.Error(T_(
                    N_("Could not open device '%s'.  "
                       "Check permissions on the device."), device), mid=mid)
                documentation.messageAddFixBadPermissions(m)
            elif gerror.code == int(Gst.RESOURCE_ERROR_BUSY):
                m = messages.Error(T_(
                    N_("Device '%s' is already in use."), device), mid=mid)
            elif gerror.code == int(Gst.RESOURCE_ERROR_SETTINGS):
                m = messages.Error(T_(
                    N_("Device '%s' did not accept the requested settings."),
                    device),
                    debug="%s\n%s" % (gerror.message, debug), mid=mid)

        # fallback GStreamer GstError handling
        if not m:
            m = messages.Error(T_(N_("Internal unhandled GStreamer error.")),
                debug="%s\n%s: %d\n%s" % (
                    gerror.message, gerror.domain, gerror.code, debug),
                               mid=mid)
    elif failure.check(errors.GStreamerError):
        m = messages.Error(T_(N_("Internal GStreamer error.")),
            debug=debugFailure(failure), mid=mid)
    log.debug('check', 'handleGStreamerError: returning %r' % m)
    return m
Beispiel #9
0
def handleGStreamerDeviceError(failure, device, mid=None):
    """
    Handle common GStreamer GstErrors or other.
    Return a message or None.
    """
    if not mid:
        log.warning('check',
            'handleGStreamerDeviceError: no message id specified')

    m = None

    if failure.check(errors.GStreamerGstError):
        source, gerror, debug = failure.value.args
        log.debug('check',
            'GStreamer GError: %s (domain %s, code %d, debug %s)' % (
                gerror.message, gerror.domain, gerror.code, debug))

        if gerror.domain == "gst-resource-error-quark":
            if gerror.code == int(gst.RESOURCE_ERROR_OPEN_READ):
                m = messages.Error(T_(
                    N_("Could not open device '%s' for reading.  "
                       "Check permissions on the device."), device), mid=mid)
                documentation.messageAddFixBadPermissions(m)
            if gerror.code == int(gst.RESOURCE_ERROR_OPEN_WRITE):
                m = messages.Error(T_(
                    N_("Could not open device '%s' for writing.  "
                       "Check permissions on the device."), device), mid=mid)
                documentation.messageAddFixBadPermissions(m)
            elif gerror.code == int(gst.RESOURCE_ERROR_OPEN_READ_WRITE):
                m = messages.Error(T_(
                    N_("Could not open device '%s'.  "
                       "Check permissions on the device."), device), mid=mid)
                documentation.messageAddFixBadPermissions(m)
            elif gerror.code == int(gst.RESOURCE_ERROR_BUSY):
                m = messages.Error(T_(
                    N_("Device '%s' is already in use."), device), mid=mid)
            elif gerror.code == int(gst.RESOURCE_ERROR_SETTINGS):
                m = messages.Error(T_(
                    N_("Device '%s' did not accept the requested settings."),
                    device),
                    debug="%s\n%s" % (gerror.message, debug), mid=mid)

        # fallback GStreamer GstError handling
        if not m:
            m = messages.Error(T_(N_("Internal unhandled GStreamer error.")),
                debug="%s\n%s: %d\n%s" % (
                    gerror.message, gerror.domain, gerror.code, debug),
                               mid=mid)
    elif failure.check(errors.GStreamerError):
        m = messages.Error(T_(N_("Internal GStreamer error.")),
            debug=debugFailure(failure), mid=mid)
    log.debug('check', 'handleGStreamerError: returning %r' % m)
    return m
Beispiel #10
0
 def save(self):
     try:
         f = open(self._filename, 'w')
         doc = minidom.Document()
         root = doc.createElement('gtk-admin-state')
         doc.appendChild(root)
         for key, value in self._values.iteritems():
             self._append(doc, root, key, value)
         doc.writexml(f)
         f.close()
     except IOError, e:
         log.warning('Cannot find gtk-admin-state: %s',
                     log.getExceptionMessage(e))
Beispiel #11
0
 def _notifyListeners(self, index, *args):
     # notify our local listeners; compute set of procs first, so as
     # to allow the listeners set to change during the calls
     self._ensureListeners()
     for proc in [tup[index] for tup in self._listeners.values()]:
         if proc:
             try:
                 proc(self, *args)
             except Exception, e:
                 # These are all programming errors
                 log.warning("stateremotecache",
                             'Exception in StateCache handler: %s',
                             log.getExceptionMessage(e))
Beispiel #12
0
 def _notifyListeners(self, index, *args):
     # notify our local listeners; compute set of procs first, so as
     # to allow the listeners set to change during the calls
     self._ensureListeners()
     for proc in [tup[index] for tup in self._listeners.values()]:
         if proc:
             try:
                 proc(self, *args)
             except Exception, e:
                 # These are all programming errors
                 log.warning("stateremotecache",
                             'Exception in StateCache handler: %s',
                             log.getExceptionMessage(e))
Beispiel #13
0
 def setMood(self, moodValue):
     if self._jobState and moodValue != moods.sad.value:
         log.warning('componentstate', 'cannot set component mood to '
                     'something other than sad when we have a '
                     'jobState -- fix your code!')
     elif moodValue == self.get('mood'):
         log.log('componentstate', '%s already in mood %d',
                 self.get('name'), moodValue)
     else:
         log.debug('componentstate',
                   'manager sets mood of %s from %s to %d',
                   self.get('name'), self.get('mood'), moodValue)
         self.set('mood', moodValue)
Beispiel #14
0
 def save(self):
     try:
         f = open(self._filename, 'w')
         doc = minidom.Document()
         root = doc.createElement('gtk-admin-state')
         doc.appendChild(root)
         for key, value in self._values.iteritems():
             self._append(doc, root, key, value)
         doc.writexml(f)
         f.close()
     except IOError, e:
         log.warning('Cannot find gtk-admin-state: %s',
                     log.getExceptionMessage(e))
Beispiel #15
0
    def emit(self, signalName, *args):
        self.__ensureSignals()
        if signalName not in self.__signals__:
            raise ValueError('Emitting unknown signal %s' % signalName)

        connections = self.__signalConnections
        for name, proc, pargs, pkwargs in connections.values():
            if name == signalName:
                try:
                    proc(self, *(args + pargs), **pkwargs)
                except Exception, e:
                    log.warning("signalmixin", "Exception calling "
                                "signal handler %r: %s", proc,
                                log.getExceptionMessage(e))
Beispiel #16
0
 def run(self):
     while self._running:
         sessions = self.plug._sessions.keys()
         for session in sessions:
             try:
                 session.doServe()
             except Exception, e:
                 log.warning("Error during async file serving: %s",
                             log.getExceptionMessage(e))
             try:
                 session.doCopy()
             except Exception, e:
                 log.warning("Error during file copy: %s",
                             log.getExceptionMessage(e))
Beispiel #17
0
 def run(self):
     while self._running:
         sessions = self.plug._sessions.keys()
         for session in sessions:
             try:
                 session.doServe()
             except Exception, e:
                 log.warning("Error during async file serving: %s",
                             log.getExceptionMessage(e))
             try:
                 session.doCopy()
             except Exception, e:
                 log.warning("Error during file copy: %s",
                             log.getExceptionMessage(e))
Beispiel #18
0
    def emit(self, signalName, *args):
        self.__ensureSignals()
        if signalName not in self.__signals__:
            raise ValueError('Emitting unknown signal %s' % signalName)

        connections = self.__signalConnections
        for name, proc, pargs, pkwargs in connections.values():
            if name == signalName:
                try:
                    proc(self, *(args + pargs), **pkwargs)
                except Exception, e:
                    log.warning("signalmixin", "Exception calling "
                                "signal handler %r: %s", proc,
                                log.getExceptionMessage(e))
Beispiel #19
0
def getRecentConnections():
    """
    Fetches a list of recently used connections
    @returns: recently used connections
    @rtype: list of L{RecentConnection}
    """

    recentFilenames = _getRecentFilenames()
    recentConnections = []
    for filename in sorted(recentFilenames, reverse=True):
        try:
            state = _parseSingleConnectionFile(filename)
            recentConnections.append(RecentConnection(str(state), filename=filename, info=state))
        except Exception, e:
            log.warning("connections", "Error parsing %s: %r", filename, e)
Beispiel #20
0
 def _cbRequestAvatarId(self, validKey, credentials):
     if not validKey:
         return failure.Failure(UnauthorizedLogin())
     if not credentials.signature:
         return failure.Failure(error.ValidPublicKey())
     else:
         try:
             pubKey = keys.getPublicKeyObject(data=credentials.blob)
             if keys.verifySignature(pubKey, credentials.signature, credentials.sigData):
                 return credentials.username
         except:  # any error should be treated as a failed login
             f = failure.Failure()
             log.warning("error checking signature: %r", credentials)
             return f
     return failure.Failure(UnauthorizedLogin())
Beispiel #21
0
 def _cbRequestAvatarId(self, validKey, credentials):
     if not validKey:
         return failure.Failure(UnauthorizedLogin())
     if not credentials.signature:
         return failure.Failure(error.ValidPublicKey())
     else:
         try:
             pubKey = keys.getPublicKeyObject(data = credentials.blob)
             if keys.verifySignature(pubKey, credentials.signature,
                                     credentials.sigData):
                 return credentials.username
         except: # any error should be treated as a failed login
             f = failure.Failure()
             log.warning('error checking signature: %r', credentials)
             return f
     return failure.Failure(UnauthorizedLogin())
Beispiel #22
0
def getDefaultConnections():
    """
    Fetches a list of default connections.

    @returns: default connections
    @rtype: list of L{ConnectionInfo}
    """

    filename = xdg.config_read_path('connections')
    if not filename:
        return []

    try:
        return _parseMultipleConnectionsFile(filename)
    except Exception, e:
        log.warning('connections', 'Error parsing %s: %r', filename, e)
Beispiel #23
0
def getRecentConnections():
    """
    Fetches a list of recently used connections
    @returns: recently used connections
    @rtype: list of L{RecentConnection}
    """

    recentFilenames = _getRecentFilenames()
    recentConnections = []
    for filename in sorted(recentFilenames, reverse=True):
        try:
            state = _parseSingleConnectionFile(filename)
            recentConnections.append(
                RecentConnection(str(state), filename=filename, info=state))
        except Exception, e:
            log.warning('connections', 'Error parsing %s: %r', filename, e)
Beispiel #24
0
def getDefaultConnections():
    """
    Fetches a list of default connections.

    @returns: default connections
    @rtype: list of L{ConnectionInfo}
    """

    filename = xdg.config_read_path('connections')
    if not filename:
        return []

    try:
        return _parseMultipleConnectionsFile(filename)
    except Exception, e:
        log.warning('connections', 'Error parsing %s: %r', filename, e)
Beispiel #25
0
def reloadFlumotion():
    """Properly reload all flumotion-related modules currently loaded."""
    needs_reload = lambda name: name.startswith('flumotion')
    for name in filter(needs_reload, sys.modules.keys()):
        if not name in sys.modules:
            log.warning("reload", "hm, %s disappeared from the modules" % name)
            continue
        module = sys.modules[name]
        if not module:
            log.log("reload", "hm, module '%s' == None" % name)
            continue
        log.log("reload", "rebuilding %s" % module)
        try:
            rebuild(module, doLog=0)
        except SyntaxError, msg:
            from flumotion.common import errors
            raise errors.ReloadSyntaxError(msg)
Beispiel #26
0
def reloadFlumotion():
    """Properly reload all flumotion-related modules currently loaded."""
    needs_reload = lambda name: name.startswith('flumotion')
    for name in filter(needs_reload, sys.modules.keys()):
        if not name in sys.modules:
            log.warning("reload", "hm, %s disappeared from the modules" % name)
            continue
        module = sys.modules[name]
        if not module:
            log.log("reload", "hm, module '%s' == None" % name)
            continue
        log.log("reload", "rebuilding %s" % module)
        try:
            rebuild(module, doLog=0)
        except SyntaxError, msg:
            from flumotion.common import errors
            raise errors.ReloadSyntaxError(msg)
Beispiel #27
0
def get(project, attribute, default=None):
    """
    Get an attribute from a project's module.
    """
    log.debug('project', 'Getting attribute %s from project %s',
        attribute, project)

    # short-circuit flumotion, the core project
    if project == 'flumotion':
        return getattr(configure, attribute, default)

    moduleName = "flumotion.project.%s" % project
    try:
        exec("import %s" % moduleName)
    except ImportError, e:
        log.warning('project', 'Could not load project %s: %s',
            project, log.getExceptionMessage(e))
        raise errors.NoProjectError(moduleName)
Beispiel #28
0
class SSHPublicKeyChecker(log.Loggable):
    try:
        credentialInterfaces = credentials.ISSHPrivateKey,
    except AttributeError:
        log.warning('manhole', 'ssh manhole unavailable (old twisted)')
        # you won't be able to log anything in
        credentialInterfaces = ()

    interface.implements(ICredentialsChecker)

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

    def requestAvatarId(self, credentials):
        d = defer.maybeDeferred(self.checkKey, credentials)
        d.addCallback(self._cbRequestAvatarId, credentials)
        d.addErrback(self._ebRequestAvatarId)
        return d

    def _cbRequestAvatarId(self, validKey, credentials):
        if not validKey:
            return failure.Failure(UnauthorizedLogin())
        if not credentials.signature:
            return failure.Failure(error.ValidPublicKey())
        else:
            try:
                pubKey = keys.getPublicKeyObject(data = credentials.blob)
                if keys.verifySignature(pubKey, credentials.signature,
                                        credentials.sigData):
                    return credentials.username
            except: # any error should be treated as a failed login
                f = failure.Failure()
                log.warning('error checking signature: %r', credentials)
                return f
        return failure.Failure(UnauthorizedLogin())

    def checkKey(self, credentials):
        filename = self.authorizedKeysFile
        if not os.path.exists(filename):
            return 0
        lines = open(filename).xreadlines()
        for l in lines:
            l2 = l.split()
            if len(l2) < 2:
                continue
            try:
                if base64.decodestring(l2[1]) == credentials.blob:
                    return 1
            except binascii.Error:
                continue
        return 0

    def _ebRequestAvatarId(self, f):
        if not f.check(UnauthorizedLogin, error.ValidPublicKey):
            log.warning('failed login %r', f)
            return failure.Failure(UnauthorizedLogin())
        return f
Beispiel #29
0
def get(project, attribute, default=None):
    """
    Get an attribute from a project's module.
    """
    log.debug('project', 'Getting attribute %s from project %s', attribute,
              project)

    # short-circuit flumotion, the core project
    if project == 'flumotion':
        return getattr(configure, attribute, default)

    moduleName = "flumotion.project.%s" % project
    try:
        exec("import %s" % moduleName)
    except ImportError, e:
        msg = ('Could not load project %s: %s' %
               (project, log.getExceptionMessage(e)))
        log.warning('project', msg)
        raise errors.NoProjectError(project, msg)
Beispiel #30
0
def upgradeAliases(conf):
    eaters = dict(conf.get('eater', {})) # a copy
    concat = lambda lists: reduce(list.__add__, lists, [])
    if not reduce(lambda x, y: y and isinstance(x, tuple),
                  concat(eaters.values()),
                  True):
        for eater in eaters:
            aliases = []
            feeders = eaters[eater]
            for i in range(len(feeders)):
                val = feeders[i]
                if isinstance(val, tuple):
                    feedId, alias = val
                    aliases.append(val[1])
                else:
                    feedId = val
                    alias = eater
                    while alias in aliases:
                        log.warning('config', "Duplicate alias %s for "
                                    "eater %s, uniquifying", alias, eater)
                        alias += '-bis'
                    aliases.append(alias)
                    feeders[i] = (feedId, val)
        conf['eater'] = eaters
Beispiel #31
0
def upgradeAliases(conf):
    eaters = dict(conf.get('eater', {}))  # a copy
    concat = lambda lists: reduce(list.__add__, lists, [])
    if not reduce(lambda x, y: y and isinstance(x, tuple),
                  concat(eaters.values()), True):
        for eater in eaters:
            aliases = []
            feeders = eaters[eater]
            for i in range(len(feeders)):
                val = feeders[i]
                if isinstance(val, tuple):
                    feedId, alias = val
                    aliases.append(val[1])
                else:
                    feedId = val
                    alias = eater
                    while alias in aliases:
                        log.warning(
                            'config', "Duplicate alias %s for "
                            "eater %s, uniquifying", alias, eater)
                        alias += '-bis'
                    aliases.append(alias)
                    feeders[i] = (feedId, val)
        conf['eater'] = eaters
Beispiel #32
0
    if not hasattr(module, methodName):
        raise err("module %s has no method named %s" %
                  (moduleName, methodName))

    log.debug('reflectcall', 'calling method %s.%s' % (moduleName, methodName))

    try:
        ret = getattr(module, methodName)(*args, **kwargs)
    except err:
        # already nicely formatted, so fall through
        log.debug('reflectcall', 'letting error fall through')
        raise
    except Exception, e:
        msg = log.getExceptionMessage(e)
        log.warning('reflectcall', msg)
        log.warning('reflectcall', 'raising error')
        raise err(msg)

    log.debug('reflectcall', 'returning %r' % ret)

    return ret


def createComponent(moduleName, methodName, config):
    """
    @param moduleName: name of the module to create the component from
    @type  moduleName: string
    @param methodName: the factory method to use to create the component
    @type  methodName: string
    @param config: the component's config dict
Beispiel #33
0
    def registerPackagePath(self, packagePath, key, prefix=configure.PACKAGE):
        """
        Register a given path as a path that can be imported from.
        Used to support partition of bundled code or import code from various
        uninstalled location.

        sys.path will also be changed to include this, and remove references
        to older packagePath's for the same bundle.

        @param packagePath: path to add under which the module namespaces live,
                            (ending in an md5sum, for flumotion purposes)
        @type  packagePath: string
        @param key          a unique id for the package being registered
        @type  key:         string
        @param prefix:      prefix of the packages to be considered
        @type  prefix:      string
        """

        new = True
        packagePath = os.path.abspath(packagePath)
        if not os.path.exists(packagePath):
            log.warning('bundle',
                'registering a non-existing package path %s' % packagePath)

        self.log('registering packagePath %s' % packagePath)

        # check if a packagePath for this bundle was already registered
        if key in self._paths:
            oldPath = self._paths[key]
            if packagePath == oldPath:
                self.log('already registered %s for key %s' % (
                    packagePath, key))
                return
            new = False

        # Find the packages in the path and sort them,
        # the following algorithm only works if they're sorted.
        # By sorting the list we can ensure that a parent package
        # is always processed before one of its children
        if not os.path.isdir(packagePath):
            log.warning('bundle', 'package path not a dir: %s',
                        packagePath)
            packageNames = []
        else:
            packageNames = _findPackageCandidates(packagePath, prefix)

        if not packageNames:
            log.log('bundle',
                'packagePath %s does not have candidates starting with %s' %
                    (packagePath, prefix))
            return
        packageNames.sort()

        self.log('package candidates %r' % packageNames)

        if not new:
            # it already existed, and now it's a different path
            log.log('bundle',
                'replacing old path %s with new path %s for key %s' % (
                    oldPath, packagePath, key))

            if oldPath in sys.path:
                log.log('bundle',
                    'removing old packagePath %s from sys.path' % oldPath)
                sys.path.remove(oldPath)

            # clear this key from our name -> key cache
            for keys in self._packages.values():
                if key in keys:
                    keys.remove(key)

        self._paths[key] = packagePath

        # put packagePath at the top of sys.path if not in there
        if not packagePath in sys.path:
            self.log('adding packagePath %s to sys.path' % packagePath)
            sys.path.insert(0, packagePath)

        # update our name->keys cache
        for name in packageNames:
            if name not in self._packages:
                self._packages[name] = [key]
            else:
                self._packages[name].insert(0, key)

        self.log('packagePath %s has packageNames %r' % (
            packagePath, packageNames))
        # since we want sub-modules to be fixed up before parent packages,
        # we reverse the list
        packageNames.reverse()

        for packageName in packageNames:
            if packageName not in sys.modules:
                continue
            self.log('fixing up %s ...' % packageName)

            # the package is imported, so mess with __path__ and rebuild
            package = sys.modules.get(packageName)
            for path in package.__path__:
                if not new and path.startswith(oldPath):
                    self.log('%s.__path__ before remove %r' % (
                        packageName, package.__path__))
                    self.log('removing old %s from %s.__path__' % (
                        path, name))
                    package.__path__.remove(path)
                    self.log('%s.__path__ after remove %r' % (
                        packageName, package.__path__))

            # move the new path to the top
            # insert at front because FLU_REGISTRY_PATH paths should override
            # base components, and because subsequent reload() should prefer
            # the latest registered path
            newPath = os.path.join(packagePath,
                                   packageName.replace('.', os.sep))

            # if path already at position 0, everything's fine
            # if it's in there at another place, it needs to move to front
            # if not in there, it needs to be put in front
            if len(package.__path__) == 0:
                # FIXME: this seems to happen to e.g. flumotion.component.base
                # even when it was just rebuilt and had the __path__ set
                # can be triggered by choosing a admin_gtk depending on
                # the base admin_gtk where the base admin_gtk changes
                self.debug('WARN: package %s does not have __path__ values' % (
                    packageName))
            elif package.__path__[0] == newPath:
                self.log('path %s already at start of %s.__path__' % (
                    newPath, packageName))
                continue

            if newPath in package.__path__:
                package.__path__.remove(newPath)
                self.log('moving %s to front of %s.__path__' % (
                    newPath, packageName))
            else:
                self.log('inserting new %s into %s.__path__' % (
                    newPath, packageName))
            package.__path__.insert(0, newPath)

            # Rebuilding these packages just to get __path__ fixed in
            # seems not necessary - but re-enable it if it breaks
            # self.log('rebuilding package %s from paths %r' % (packageName,
            #     package.__path__))
            # rebuild.rebuild(package)
            # self.log('rebuilt package %s with paths %r' % (packageName,
            #     package.__path__))
            self.log('fixed up %s, __path__ %s ...' % (
                packageName, package.__path__))

        # now rebuild all non-package modules in this packagePath if this
        # is not a new package
        if not new:
            self.log('finding end module candidates')
            if not os.path.isdir(packagePath):
                log.warning('bundle', 'package path not a dir: %s',
                            path)
                moduleNames = []
            else:
                moduleNames = findEndModuleCandidates(packagePath, prefix)
            self.log('end module candidates to rebuild: %r' % moduleNames)
            for name in moduleNames:
                if name in sys.modules:
                    # fixme: isn't sys.modules[name] sufficient?
                    self.log("rebuilding non-package module %s" % name)
                    try:
                        module = reflect.namedAny(name)
                    except AttributeError:
                        log.warning('bundle',
                            "could not reflect non-package module %s" % name)
                        continue

                    if hasattr(module, '__path__'):
                        self.log('rebuilding module %s with paths %r' % (name,
                            module.__path__))
                    rebuild.rebuild(module)
                    #if paths:
                    #    module.__path__ = paths

        self.log('registered packagePath %s for key %s' % (packagePath, key))
Beispiel #34
0
    def registerPackagePath(self, packagePath, key, prefix=configure.PACKAGE):
        """
        Register a given path as a path that can be imported from.
        Used to support partition of bundled code or import code from various
        uninstalled location.

        sys.path will also be changed to include this, and remove references
        to older packagePath's for the same bundle.

        @param packagePath: path to add under which the module namespaces live,
                            (ending in an md5sum, for flumotion purposes)
        @type  packagePath: string
        @param key          a unique id for the package being registered
        @type  key:         string
        @param prefix:      prefix of the packages to be considered
        @type  prefix:      string
        """

        new = True
        packagePath = os.path.abspath(packagePath)
        if not os.path.exists(packagePath):
            log.warning(
                'bundle',
                'registering a non-existing package path %s' % packagePath)

        self.log('registering packagePath %s' % packagePath)

        # check if a packagePath for this bundle was already registered
        if key in self._paths:
            oldPath = self._paths[key]
            if packagePath == oldPath:
                self.log('already registered %s for key %s' %
                         (packagePath, key))
                return
            new = False

        # Find the packages in the path and sort them,
        # the following algorithm only works if they're sorted.
        # By sorting the list we can ensure that a parent package
        # is always processed before one of its children
        if not os.path.isdir(packagePath):
            log.warning('bundle', 'package path not a dir: %s', packagePath)
            packageNames = []
        else:
            packageNames = _findPackageCandidates(packagePath, prefix)

        if not packageNames:
            log.log(
                'bundle',
                'packagePath %s does not have candidates starting with %s' %
                (packagePath, prefix))
            return
        packageNames.sort()

        self.log('package candidates %r' % packageNames)

        if not new:
            # it already existed, and now it's a different path
            log.log(
                'bundle', 'replacing old path %s with new path %s for key %s' %
                (oldPath, packagePath, key))

            if oldPath in sys.path:
                log.log('bundle',
                        'removing old packagePath %s from sys.path' % oldPath)
                sys.path.remove(oldPath)

            # clear this key from our name -> key cache
            for keys in self._packages.values():
                if key in keys:
                    keys.remove(key)

        self._paths[key] = packagePath

        # put packagePath at the top of sys.path if not in there
        if not packagePath in sys.path:
            self.log('adding packagePath %s to sys.path' % packagePath)
            sys.path.insert(0, packagePath)

        # update our name->keys cache
        for name in packageNames:
            if name not in self._packages:
                self._packages[name] = [key]
            else:
                self._packages[name].insert(0, key)

        self.log('packagePath %s has packageNames %r' %
                 (packagePath, packageNames))
        # since we want sub-modules to be fixed up before parent packages,
        # we reverse the list
        packageNames.reverse()

        for packageName in packageNames:
            if packageName not in sys.modules:
                continue
            self.log('fixing up %s ...' % packageName)

            # the package is imported, so mess with __path__ and rebuild
            package = sys.modules.get(packageName)
            for path in package.__path__:
                if not new and path.startswith(oldPath):
                    self.log('%s.__path__ before remove %r' %
                             (packageName, package.__path__))
                    self.log('removing old %s from %s.__path__' % (path, name))
                    package.__path__.remove(path)
                    self.log('%s.__path__ after remove %r' %
                             (packageName, package.__path__))

            # move the new path to the top
            # insert at front because FLU_REGISTRY_PATH paths should override
            # base components, and because subsequent reload() should prefer
            # the latest registered path
            newPath = os.path.join(packagePath,
                                   packageName.replace('.', os.sep))

            # if path already at position 0, everything's fine
            # if it's in there at another place, it needs to move to front
            # if not in there, it needs to be put in front
            if len(package.__path__) == 0:
                # FIXME: this seems to happen to e.g. flumotion.component.base
                # even when it was just rebuilt and had the __path__ set
                # can be triggered by choosing a admin_gtk depending on
                # the base admin_gtk where the base admin_gtk changes
                self.debug('WARN: package %s does not have __path__ values' %
                           (packageName))
            elif package.__path__[0] == newPath:
                self.log('path %s already at start of %s.__path__' %
                         (newPath, packageName))
                continue

            if newPath in package.__path__:
                package.__path__.remove(newPath)
                self.log('moving %s to front of %s.__path__' %
                         (newPath, packageName))
            else:
                self.log('inserting new %s into %s.__path__' %
                         (newPath, packageName))
            package.__path__.insert(0, newPath)

            # Rebuilding these packages just to get __path__ fixed in
            # seems not necessary - but re-enable it if it breaks
            # self.log('rebuilding package %s from paths %r' % (packageName,
            #     package.__path__))
            # rebuild.rebuild(package)
            # self.log('rebuilt package %s with paths %r' % (packageName,
            #     package.__path__))
            self.log('fixed up %s, __path__ %s ...' %
                     (packageName, package.__path__))

        # now rebuild all non-package modules in this packagePath if this
        # is not a new package
        if not new:
            self.log('finding end module candidates')
            if not os.path.isdir(packagePath):
                log.warning('bundle', 'package path not a dir: %s', path)
                moduleNames = []
            else:
                moduleNames = findEndModuleCandidates(packagePath, prefix)
            self.log('end module candidates to rebuild: %r' % moduleNames)
            for name in moduleNames:
                if name in sys.modules:
                    # fixme: isn't sys.modules[name] sufficient?
                    self.log("rebuilding non-package module %s" % name)
                    try:
                        module = reflect.namedAny(name)
                    except AttributeError:
                        log.warning(
                            'bundle',
                            "could not reflect non-package module %s" % name)
                        continue

                    if hasattr(module, '__path__'):
                        self.log('rebuilding module %s with paths %r' %
                                 (name, module.__path__))
                    rebuild.rebuild(module)
                    #if paths:
                    #    module.__path__ = paths

        self.log('registered packagePath %s for key %s' % (packagePath, key))
Beispiel #35
0
 def _ebRequestAvatarId(self, f):
     if not f.check(UnauthorizedLogin, error.ValidPublicKey):
         log.warning('manhole', 'failed login: %r',
             log.getFailureMessage(f))
         return failure.Failure(UnauthorizedLogin())
     return f
Beispiel #36
0
 def _ebRequestAvatarId(self, f):
     if not f.check(UnauthorizedLogin, error.ValidPublicKey):
         log.warning('manhole', 'failed login: %r',
                     log.getFailureMessage(f))
         return failure.Failure(UnauthorizedLogin())
     return f
Beispiel #37
0
 def _ebRequestAvatarId(self, f):
     if not f.check(UnauthorizedLogin, error.ValidPublicKey):
         log.warning("failed login %r", f)
         return failure.Failure(UnauthorizedLogin())
     return f
Beispiel #38
0
    return [n[len('flumotion.project.'):] for n in modules]


def get(project, attribute, default=None):
    """
    Get an attribute from a project's module.
    """
    log.debug('project', 'Getting attribute %s from project %s', attribute,
              project)

    # short-circuit flumotion, the core project
    if project == 'flumotion':
        return getattr(configure, attribute, default)

    moduleName = "flumotion.project.%s" % project
    try:
        exec("import %s" % moduleName)
    except ImportError, e:
        msg = ('Could not load project %s: %s' %
               (project, log.getExceptionMessage(e)))
        log.warning('project', msg)
        raise errors.NoProjectError(project, msg)
    except SyntaxError, e:
        msg = ('Syntax error while loading project %s: %s' %
               (project, log.getExceptionMessage(e)))
        log.warning('project', msg)
        raise errors.NoProjectError(project, msg)
    m = sys.modules[moduleName]
    return getattr(m, attribute, default)
Beispiel #39
0
 def _ebRequestAvatarId(self, f):
     if not f.check(UnauthorizedLogin, error.ValidPublicKey):
         log.warning('failed login %r', f)
         return failure.Failure(UnauthorizedLogin())
     return f
Beispiel #40
0
    return [n[len('flumotion.project.'):] for n in modules]


def get(project, attribute, default=None):
    """
    Get an attribute from a project's module.
    """
    log.debug('project', 'Getting attribute %s from project %s',
        attribute, project)

    # short-circuit flumotion, the core project
    if project == 'flumotion':
        return getattr(configure, attribute, default)

    moduleName = "flumotion.project.%s" % project
    try:
        exec("import %s" % moduleName)
    except ImportError, e:
        msg = ('Could not load project %s: %s' %
               (project, log.getExceptionMessage(e)))
        log.warning('project', msg)
        raise errors.NoProjectError(project, msg)
    except SyntaxError, e:
        msg = ('Syntax error while loading project %s: %s' %
               (project, log.getExceptionMessage(e)))
        log.warning('project', msg)
        raise errors.NoProjectError(project, msg)
    m = sys.modules[moduleName]
    return getattr(m, attribute, default)
Beispiel #41
0
    if not hasattr(module, methodName):
        raise err("module %s has no method named %s"
                  % (moduleName, methodName))

    log.debug('reflectcall', 'calling method %s.%s'
              % (moduleName, methodName))

    try:
        ret = getattr(module, methodName)(*args, **kwargs)
    except err:
        # already nicely formatted, so fall through
        log.debug('reflectcall', 'letting error fall through')
        raise
    except Exception, e:
        msg = log.getExceptionMessage(e)
        log.warning('reflectcall', msg)
        log.warning('reflectcall', 'raising error')
        raise err(msg)

    log.debug('reflectcall', 'returning %r' % ret)

    return ret


def createComponent(moduleName, methodName, config):
    """
    @param moduleName: name of the module to create the component from
    @type  moduleName: string
    @param methodName: the factory method to use to create the component
    @type  methodName: string
    @param config: the component's config dict