def get_plugin_instance(plugin): try: source_path = os.path.join(plugin.path, plugin.script) cache.load_module(plugin.name, source_path) instance = cache.get_plugin(plugin.name)() return instance except: logger.error(traceback.format_exc()) raise Exception("Error loading plugin module from %s" % source_path)
def setUpModule(): plugins = [] path = os.path.join(os.path.dirname(__file__),'plugins') path = os.path.normpath(os.path.abspath(path)) assert os.path.exists(path) for (dirpath, dirnames, filenames) in os.walk(path): if '.svn' in dirpath: continue for name in dirnames: if name == '.svn': continue launchsh = os.path.join(dirpath, name, 'launch.sh') if os.path.exists(launchsh): log.info("Plugin [launch]: '%s'",launchsh) plugins.append( (name, launchsh) ) continue # Find all instances of PluginName/PluginName.py plugindef = os.path.join(dirpath, name, name + '.py') if os.path.exists(plugindef): log.info("Plugin [class]: '%s'",plugindef) #sys.path.append(os.path.join(dirpath, name)) plugins.append( (name, plugindef) ) continue log.debug("Path (No Plugins): '%s/%s'",dirpath, name) assert plugins cache.set_installed_modules(plugins) for (name, script) in plugins: ret = cache.load_module(name, script) assert ret return True
def setUpModule(): plugins = [] path = os.path.join(os.path.dirname(__file__), 'plugins') path = os.path.normpath(os.path.abspath(path)) assert os.path.exists(path) for (dirpath, dirnames, filenames) in os.walk(path): if '.svn' in dirpath: continue for name in dirnames: if name == '.svn': continue launchsh = os.path.join(dirpath, name, 'launch.sh') if os.path.exists(launchsh): log.info("Plugin [launch]: '%s'", launchsh) plugins.append((name, launchsh)) continue # Find all instances of PluginName/PluginName.py plugindef = os.path.join(dirpath, name, name + '.py') if os.path.exists(plugindef): log.info("Plugin [class]: '%s'", plugindef) #sys.path.append(os.path.join(dirpath, name)) plugins.append((name, plugindef)) continue log.debug("Path (No Plugins): '%s/%s'", dirpath, name) assert plugins cache.set_installed_modules(plugins) for (name, script) in plugins: ret = cache.load_module(name, script) assert ret return True
def get_info_from_script(name, script, context=None): from ion.plugin.loader import cache from ion.plugin.info import PluginInfo mod = cache.load_module(name, script) cls = cache.get_plugin(name) if not cls: return None plugin = cls() ## TODO plugin = cls(**context) info = PluginInfo.from_instance(plugin) return info
def get_info_from_script(name, script, context=None, add_to_store=True): from ion.plugin.loader import cache from ion.plugin.info import PluginInfo mod = cache.load_module(name, script, add_to_store) # if the load module command returned a None object, then lets raise an exception based on what happened if not mod: if name in cache.module_errors: raise cache.module_errors[name] else: raise Exception("Error loading the module.") cls = cache.get_plugin(name) if not cls: raise Exception("The python module loaded but no classes which extend 'IonPlugin' registered themselves with the framework.") plugin = cls() return PluginInfo.from_instance(plugin)
def get_info_from_script(name, script, context=None, add_to_store=True): from ion.plugin.loader import cache from ion.plugin.info import PluginInfo mod = cache.load_module(name, script, add_to_store) # if the load module command returned a None object, then lets raise an exception based on what happened if not mod: if name in cache.module_errors: raise cache.module_errors[name] else: raise Exception("Error loading the module.") cls = cache.get_plugin(name) if not cls: raise Exception( "The python module loaded but no classes which extend 'IonPlugin' registered themselves with the framework." ) plugin = cls() return PluginInfo.from_instance(plugin)
def unzipPlugin(zipfile, logger=None): if not logger: logger = logging.getLogger(__name__) ## Extract plugin to scratch folder. When complete, move to final location. plugin_path, ext = os.path.splitext(zipfile) plugin_name = os.path.basename(plugin_path) # ZIP file must named with plugin name - fragile # FIXME - handle (1) additions (common for multiple downloads via browser) # FIXME - handle version string in ZIP archive name scratch_path = os.path.join(settings.PLUGIN_PATH, "scratch", "install-temp", plugin_name) (prefix, files) = extract_zip(zipfile, scratch_path, auto_prefix=True, logger=logger) if prefix: plugin_name = os.path.basename(prefix) plugin_temp_home = os.path.join(scratch_path, prefix) try: # Convert script into PluginClass, get info by introspection from iondb.plugins.manager import pluginmanager script, islaunch = pluginmanager.find_pluginscript( plugin_temp_home, plugin_name) logger.debug("Got script: %s", script) from ion.plugin.loader import cache ret = cache.load_module(plugin_name, script) cls = cache.get_plugin(plugin_name) p = cls() final_name = p.name # what the plugin calls itself, regardless of ZIP file name logger.info("Plugin calls itself: '%s'", final_name) except: logger.exception("Unable to interrogate plugin name from: '%s'", zipfile) final_name = plugin_name #move to the plugin dir # New extract_zip removes prefix from extracted files. # But still writes to file_name try: final_install_dir = os.path.join(settings.PLUGIN_PATH, final_name) if os.path.exists(final_install_dir) and (final_install_dir != settings.PLUGIN_PATH): logger.info("Deleting old copy of plugin at '%s'", final_install_dir) delete_that_folder( final_install_dir, "Error Deleting old copy of plugin at '%s'" % final_install_dir) parent_folder = os.path.dirname(final_install_dir) if not os.path.exists(parent_folder): logger.info("Creating path for plugin '%s' for '%s'", parent_folder, final_install_dir) os.makedirs(parent_folder, 0555) logger.info( "Moving plugin from temp extract folder '%s' to final location: '%s'", plugin_temp_home, final_install_dir) shutil.move(plugin_temp_home, final_install_dir) delete_that_folder(scratch_path, "Deleting plugin install scratch folder") except (IOError, OSError): logger.exception( "Failed to move plugin from temp extract folder '%s' to final location: '%s'", plugin_temp_home, final_install_dir) raise # Now that it has been downloaded, # convert pre-plugin into real db plugin object try: from iondb.plugins.manager import pluginmanager (new_plugin, updated) = pluginmanager.install(final_name, final_install_dir) except ValueError: logger.exception("Failed to install plugin") #delete_that_folder(final_install_dir) return { "plugin": final_name, "path": final_install_dir, "files": files, }
def unzipPlugin(zipfile, logger=None): if not logger: logger = logging.getLogger(__name__) ## Extract plugin to scratch folder. When complete, move to final location. plugin_path, ext = os.path.splitext(zipfile) plugin_name = os.path.basename(plugin_path) # ZIP file must named with plugin name - fragile # FIXME - handle (1) additions (common for multiple downloads via browser) # FIXME - handle version string in ZIP archive name scratch_path = os.path.join(settings.PLUGIN_PATH,"scratch","install-temp",plugin_name) (prefix, files) = extract_zip(zipfile, scratch_path, auto_prefix=True, logger=logger) if prefix: plugin_name = os.path.basename(prefix) plugin_temp_home = os.path.join(scratch_path, prefix) try: # Convert script into PluginClass, get info by introspection from iondb.plugins.manager import pluginmanager script, islaunch = pluginmanager.find_pluginscript(plugin_temp_home, plugin_name) logger.debug("Got script: %s", script) from ion.plugin.loader import cache ret = cache.load_module(plugin_name, script) cls = cache.get_plugin(plugin_name) p = cls() final_name = p.name # what the plugin calls itself, regardless of ZIP file name logger.info("Plugin calls itself: '%s'", final_name) except: logger.exception("Unable to interrogate plugin name from: '%s'", zipfile) final_name = plugin_name #move to the plugin dir # New extract_zip removes prefix from extracted files. # But still writes to file_name try: final_install_dir = os.path.join(settings.PLUGIN_PATH, final_name) if os.path.exists(final_install_dir) and (final_install_dir != settings.PLUGIN_PATH): logger.info("Deleting old copy of plugin at '%s'", final_install_dir) delete_that_folder(final_install_dir, "Error Deleting old copy of plugin at '%s'" % final_install_dir) parent_folder = os.path.dirname(final_install_dir) if not os.path.exists(parent_folder): logger.info("Creating path for plugin '%s' for '%s'", parent_folder, final_install_dir) os.makedirs(parent_folder, 0555) logger.info("Moving plugin from temp extract folder '%s' to final location: '%s'", plugin_temp_home, final_install_dir) shutil.move(plugin_temp_home, final_install_dir) delete_that_folder(scratch_path, "Deleting plugin install scratch folder") except (IOError, OSError): logger.exception("Failed to move plugin from temp extract folder '%s' to final location: '%s'", plugin_temp_home, final_install_dir) raise # Now that it has been downloaded, # convert pre-plugin into real db plugin object try: from iondb.plugins.manager import pluginmanager (new_plugin, updated) = pluginmanager.install(final_name, final_install_dir) except ValueError: logger.exception("Failed to install plugin") #delete_that_folder(final_install_dir) return { "plugin": final_name, "path": final_install_dir, "files": files, }