def cacheFiles(self): """ Fetches the files contained on the directory for posterior usage of them. This should be called on the worker side to work or the files wouldn't be the expected ones. """ import gnomevfs log.debug('vfsgnome', 'getting files for %s' % (self.path, )) retval = [] try: fileInfos = gnomevfs.open_directory(self.path) except gnomevfs.AccessDeniedError: raise AccessDeniedError if self.path != '/': retval.append( GnomeVFSDirectory(os.path.dirname(self.path), name='..')) for fileInfo in fileInfos: filename = fileInfo.name if filename.startswith('.'): continue if fileInfo.type == gnomevfs.FILE_TYPE_DIRECTORY: obj = GnomeVFSDirectory(os.path.join(self.path, fileInfo.name)) else: obj = GnomeVFSFile(self.path, fileInfo) retval.append(obj) log.log('vfsgnome', 'returning %r' % (retval, )) self._cachedFiles = retval
def cacheFiles(self): """ Fetches the files contained on the directory for posterior usage of them. This should be called on the worker side to work or the files wouldn't be the expected ones. """ import gnomevfs log.debug('vfsgnome', 'getting files for %s' % (self.path, )) retval = [] try: fileInfos = gnomevfs.open_directory(self.path) except gnomevfs.AccessDeniedError: raise AccessDeniedError if self.path != '/': retval.append(GnomeVFSDirectory(os.path.dirname(self.path), name='..')) for fileInfo in fileInfos: filename = fileInfo.name if filename.startswith('.'): continue if fileInfo.type == gnomevfs.FILE_TYPE_DIRECTORY: obj = GnomeVFSDirectory(os.path.join(self.path, fileInfo.name)) else: obj = GnomeVFSFile(self.path, fileInfo) retval.append(obj) log.log('vfsgnome', 'returning %r' % (retval, )) self._cachedFiles = retval
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)
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)
def load_package(self, name, filename, file=None): # this is only ever called the first time a package is imported log.log("packager", "load_package %s" % name) ret = ihooks.Hooks.load_package(self, name, filename, file) m = sys.modules[name] packagePaths = self.packager.getPathsForPackage(name) if not packagePaths: return ret # get full paths to the package paths = [os.path.join(path, name.replace(".", os.sep)) for path in packagePaths] for path in paths: if not path in m.__path__: log.log("packager", "adding path %s for package %s" % (path, name)) m.__path__.append(path) return ret
def getPid(type, name=None): """ Get the pid from the pid file in the run directory, using the given process type and process name for the filename. @returns: pid of the process, or None if not running or file not found. """ pidPath = _getPidPath(type, name) log.log('common', 'pidfile for %s %s is %s' % (type, name, pidPath)) if not os.path.exists(pidPath): return pidFile = open(pidPath, 'r') pid = pidFile.readline() pidFile.close() if not pid or int(pid) == 0: return return int(pid)
def load_package(self, name, filename, file=None): # this is only ever called the first time a package is imported log.log('packager', 'load_package %s' % name) ret = ihooks.Hooks.load_package(self, name, filename, file) m = sys.modules[name] packagePaths = self.packager.getPathsForPackage(name) if not packagePaths: return ret # get full paths to the package paths = [ os.path.join(path, name.replace('.', os.sep)) for path in packagePaths ] for path in paths: if not path in m.__path__: log.log('packager', 'adding path %s for package %s' % (path, name)) m.__path__.append(path) return ret
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))
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))
class GIODirectory(Copyable, RemoteCopy): """I am object implementing L{IDirectory} on top of GIO, see L{IDirectory} for more information. """ implements(IDirectory) def __init__(self, path, name=None): import gio if not os.path.exists(path): self.path = '/' elif not os.path.isdir(path): raise NotDirectoryError() else: self.path = os.path.abspath(path) gfile = gio.File(self.path) if name is None: name = gfile.get_basename() self.filename = name self.iconNames = self._getIconNames(gfile) def _getIconNames(self, gFile): gFileInfo = gFile.query_info('standard::icon') gIcon = gFileInfo.get_icon() return gIcon.get_names() # IFile def getPath(self): return self.path # IDirectory def getFiles(self): return succeed(self._cachedFiles) def cacheFiles(self): """ Fetches the files contained on the directory for posterior usage of them. This should be called on the worker side to work or the files wouldn't be the expected ones. """ import gio log.debug('vfsgio', 'getting files for %s' % (self.path, )) retval = [] gfile = gio.File(os.path.abspath(self.path)) try: gfileinfos = gfile.enumerate_children('standard::*') except gobject.GError, e: if (e.domain == gio.ERROR and e.code == gio.ERROR_PERMISSION_DENIED): raise AccessDeniedError raise if self.path != '/': retval.append(GIODirectory(os.path.dirname(self.path), name='..')) for gfileinfo in gfileinfos: filename = gfileinfo.get_name() if filename.startswith('.') and filename != '..': continue if gfileinfo.get_file_type() == gio.FILE_TYPE_DIRECTORY: obj = GIODirectory(os.path.join(self.path, gfileinfo.get_name())) else: obj = GIOFile(self.path, gfileinfo) retval.append(obj) log.log('vfsgio', 'returning %r' % (retval, )) self._cachedFiles = retval