def run_upgrade_hooks(self): """Executes registered functions to upgrade config version to the latest""" if not self._upgrade_hooks: return if self._version >= PICARD_VERSION: if self._version > PICARD_VERSION: print("Warning: config file %s was created by a more recent " "version of Picard (current is %s)" % (version_to_string( self._version), version_to_string(PICARD_VERSION))) return # sort upgrade hooks by version self._upgrade_hooks.sort(key=itemgetter("to")) for hook in self._upgrade_hooks: if self._version < hook['to']: try: hook['func'](*hook['args']) except Exception as e: raise ConfigUpgradeError( "Error during config upgrade from version %s to %s " "using %s(): %s" % (version_to_string( self._version), version_to_string( hook['to']), hook['func'].__name__, e)) else: hook['done'] = True self._version = hook['to'] self._write_version() else: # hook is not applicable, mark as done hook['done'] = True if all(map(itemgetter("done"), self._upgrade_hooks)): # all hooks were executed, ensure config is marked with latest version self._version = PICARD_VERSION self._write_version()
def load_plugin(self, name, plugindir): try: info = imp.find_module(name, [plugindir]) except ImportError: log.error("Failed loading plugin %r", name) return None plugin = None try: index = None for i, p in enumerate(self.plugins): if name == p.module_name: log.warning("Module %r conflict: unregistering previously" \ " loaded %r version %s from %r", p.module_name, p.name, p.version, p.file) _unregister_module_extensions(name) index = i break plugin_module = imp.load_module(_PLUGIN_MODULE_PREFIX + name, *info) plugin = PluginWrapper(plugin_module, plugindir, file=info[1]) versions = [ version_from_string(v) for v in list(plugin.api_versions) ] compatible_versions = list(set(versions) & self._api_versions) if compatible_versions: log.debug( "Loading plugin %r version %s, compatible with API: %s", plugin.name, plugin.version, ", ".join([ version_to_string(v, short=True) for v in sorted(compatible_versions) ])) plugin.compatible = True setattr(picard.plugins, name, plugin_module) if index: self.plugins[index] = plugin else: self.plugins.append(plugin) else: log.warning("Plugin '%s' from '%s' is not compatible" " with this version of Picard." % (plugin.name, plugin.file)) except VersionError as e: log.error("Plugin %r has an invalid API version string : %s", name, e) except: log.error("Plugin %r : %s", name, traceback.format_exc()) if info[0] is not None: info[0].close() return plugin
def test_version_conversion(self): versions = ( ((1, 1, 0, 'final', 0), '1.1.0.final0'), ((0, 0, 1, 'dev', 1), '0.0.1.dev1'), ((1, 1, 0, 'dev', 0), '1.1.0.dev0'), ((999, 999, 999, 'dev', 999), '999.999.999.dev999'), ((1, 1, 2, 'alpha', 2), '1.1.2.alpha2'), ((1, 1, 2, 'beta', 2), '1.1.2.beta2'), ((1, 1, 2, 'rc', 2), '1.1.2.rc2'), ) for l, s in versions: self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s))
def test_version_conversion_short(self): versions = ( ((1, 1, 0, 'final', 0), '1.1'), ((1, 1, 1, 'final', 0), '1.1.1'), ((0, 0, 1, 'dev', 1), '0.0.1.dev1'), ((1, 1, 0, 'dev', 0), '1.1.0.dev0'), ((1, 1, 2, 'alpha', 2), '1.1.2a2'), ((1, 1, 2, 'beta', 2), '1.1.2b2'), ((1, 1, 2, 'rc', 2), '1.1.2rc2'), ) for l, s in versions: self.assertEqual(version_to_string(l, short=True), s) self.assertEqual(l, version_from_string(s))
def run_upgrade_hooks(self): """Executes registered functions to upgrade config version to the latest""" if not self._upgrade_hooks: return if self._version >= PICARD_VERSION: if self._version > PICARD_VERSION: print("Warning: config file %s was created by a more recent " "version of Picard (current is %s)" % ( version_to_string(self._version), version_to_string(PICARD_VERSION) )) return for version in sorted(self._upgrade_hooks): hook = self._upgrade_hooks[version] if self._version < version: try: hook['func'](*hook['args']) except: import traceback raise ConfigUpgradeError( "Error during config upgrade from version %s to %s " "using %s():\n%s" % ( version_to_string(self._version), version_to_string(version), hook['func'].__name__, traceback.format_exc() )) else: hook['done'] = True self._version = version self._write_version() else: # hook is not applicable, mark as done hook['done'] = True if all(map(itemgetter("done"), self._upgrade_hooks.values())): # all hooks were executed, ensure config is marked with latest version self._version = PICARD_VERSION self._write_version()
def run_upgrade_hooks(self): """Executes registered functions to upgrade config version to the latest""" if not self._upgrade_hooks: return if self._version >= PICARD_VERSION: if self._version > PICARD_VERSION: print("Warning: config file %s was created by a more recent " "version of Picard (current is %s)" % ( version_to_string(self._version), version_to_string(PICARD_VERSION) )) return # sort upgrade hooks by version self._upgrade_hooks.sort(key=itemgetter("to")) for hook in self._upgrade_hooks: if self._version < hook['to']: try: hook['func'](*hook['args']) except Exception as e: raise ConfigUpgradeError( "Error during config upgrade from version %s to %s " "using %s(): %s" % ( version_to_string(self._version), version_to_string(hook['to']), hook['func'].__name__, e )) else: hook['done'] = True self._version = hook['to'] self._write_version() else: # hook is not applicable, mark as done hook['done'] = True if all(map(itemgetter("done"), self._upgrade_hooks)): # all hooks were executed, ensure config is marked with latest version self._version = PICARD_VERSION self._write_version()
def load_plugin(self, name, plugindir): try: info = imp.find_module(name, [plugindir]) except ImportError: log.error("Failed loading plugin %r", name) return None plugin = None try: index = None for i, p in enumerate(self.plugins): if name == p.module_name: log.warning("Module %r conflict: unregistering previously" \ " loaded %r version %s from %r", p.module_name, p.name, p.version, p.file) _unregister_module_extensions(name) index = i break plugin_module = imp.load_module(_PLUGIN_MODULE_PREFIX + name, *info) plugin = PluginWrapper(plugin_module, plugindir, file=info[1]) versions = [version_from_string(v) for v in list(plugin.api_versions)] compatible_versions = list(set(versions) & self._api_versions) if compatible_versions: log.debug("Loading plugin %r version %s, compatible with API: %s", plugin.name, plugin.version, ", ".join([version_to_string(v, short=True) for v in sorted(compatible_versions)])) plugin.compatible = True setattr(picard.plugins, name, plugin_module) if index is not None: self.plugins[index] = plugin else: self.plugins.append(plugin) else: log.warning("Plugin '%s' from '%s' is not compatible" " with this version of Picard." % (plugin.name, plugin.file)) except VersionError as e: log.error("Plugin %r has an invalid API version string : %s", name, e) except: log.error("Plugin %r : %s", name, traceback.format_exc()) if info[0] is not None: info[0].close() return plugin
def run_upgrade_hooks(self, outputfunc=None): """Executes registered functions to upgrade config version to the latest""" if not self._upgrade_hooks: return if self._version >= PICARD_VERSION: if self._version > PICARD_VERSION: print("Warning: config file %s was created by a more recent " "version of Picard (current is %s)" % ( version_to_string(self._version), version_to_string(PICARD_VERSION) )) return for version in sorted(self._upgrade_hooks): hook = self._upgrade_hooks[version] if self._version < version: try: if outputfunc and hook['func'].__doc__: outputfunc("Config upgrade %s -> %s: %s" % ( version_to_string(self._version), version_to_string(version), hook['func'].__doc__.strip())) hook['func'](self, *hook['args']) except BaseException: import traceback raise ConfigUpgradeError( "Error during config upgrade from version %s to %s " "using %s():\n%s" % ( version_to_string(self._version), version_to_string(version), hook['func'].__name__, traceback.format_exc() )) else: hook['done'] = True self._version = version self._write_version() else: # hook is not applicable, mark as done hook['done'] = True if all(map(itemgetter("done"), self._upgrade_hooks.values())): # all hooks were executed, ensure config is marked with latest version self._version = PICARD_VERSION self._write_version()
def test_version_conv_3(self): l, s = (1, 1, 0, 'dev', 0), '1.1.0.dev0' r = '1.1.0.dev0' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s)) self.assertEqual(l, version_from_string(r))
def test_version_conv_2(self): l, s = (1, 1, 0, 'final', 0), '1.1.0.final0' r = '1.1.0.final0' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s)) self.assertEqual(l, version_from_string(r))
def test_version_conv_1(self): l, s = (0, 0, 1, 'dev', 1), '0.0.1.dev1' r = '0.0.1.dev1' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s)) self.assertEqual(l, version_from_string(r))
def _write_version(self): self.application["version"] = version_to_string(self._version) self.sync()
def test_version_conv_18(self): l, s = (1, 1, 0, 'final', 0), '1.1' self.assertEqual(version_to_string(l, short=True), s) self.assertEqual(l, version_from_string(s))
def test_version_conv_5(self): l, s = (999, 999, 999, 'dev', 999), '999.999.999dev999' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s))
def test_version_conv_10(self): l, s = (1, 1, 0, 'dev', 0), '1.1.0.dev0' self.assertEqual(version_to_string(l, short=True), s)
def test_version_conv_1(self): l, s = (0, 0, 1, 'dev', 1), '0.0.1dev1' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s))
def test_version_conv_2(self): l, s = (1, 1, 0, 'final', 0), '1.1.0final0' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s))
def test_version_conv_11(self): l, s = ('1', '1', '0', 'dev', '0'), '1.1.0dev0' self.assertEqual(version_to_string(l), s)
def test_version_conv_10(self): l, s = (1, 1, 0, 'dev', 0), '1.1.0dev0' self.assertEqual(version_to_string(l, short=True), s)
def test_version_conv_9(self): l, s = (1, 1, 0, 'final', 1), '1.1' self.assertEqual(version_to_string(l, short=True), s)
def test_version_conv_5(self): l, s = (999, 999, 999, 'dev', 999), '999.999.999.dev999' r = '999.999.999dev999' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s)) self.assertEqual(l, version_from_string(r))
def _load_plugin_from_directory(self, name, plugindir): module_file = None (zip_importer, module_name, manifest_data) = zip_import(os.path.join(plugindir, name + '.zip')) if zip_importer: name = module_name if not zip_importer.find_module(name): error = _("Failed loading zipped plugin %r") % name self.plugin_error(name, error) return None module_pathname = zip_importer.get_filename(name) else: try: info = imp.find_module(name, [plugindir]) module_file = info[0] module_pathname = info[1] except ImportError: error = _("Failed loading plugin %r") % name self.plugin_error(name, error) return None plugin = None try: existing_plugin, existing_plugin_index = self._get_plugin_index_by_name(name) if existing_plugin: log.warning("Module %r conflict: unregistering previously" " loaded %r version %s from %r", existing_plugin.module_name, existing_plugin.name, existing_plugin.version, existing_plugin.file) _unregister_module_extensions(name) full_module_name = _PLUGIN_MODULE_PREFIX + name if zip_importer: plugin_module = zip_importer.load_module(full_module_name) else: plugin_module = imp.load_module(full_module_name, *info) plugin = PluginWrapper(plugin_module, plugindir, file=module_pathname, manifest_data=manifest_data) compatible_versions = _compatible_api_versions(plugin.api_versions) if compatible_versions: log.debug("Loading plugin %r version %s, compatible with API: %s", plugin.name, plugin.version, ", ".join([version_to_string(v, short=True) for v in sorted(compatible_versions)])) plugin.compatible = True setattr(picard.plugins, name, plugin_module) if existing_plugin: self.plugins[existing_plugin_index] = plugin else: self.plugins.append(plugin) else: error = _("Plugin '%s' from '%s' is not compatible with this " "version of Picard.") % (plugin.name, plugin.file) self.plugin_error(plugin.name, error, log_func=log.warning) except VersionError as e: error = _("Plugin %r has an invalid API version string : %s") % (name, e) self.plugin_error(name, error) except BaseException: error = _("Plugin %r : %s") % (name, traceback.format_exc()) self.plugin_error(name, error) if module_file is not None: module_file.close() return plugin
def _load_plugin_from_directory(self, name, plugindir): module_file = None (zip_importer, module_name, manifest_data) = zip_import(os.path.join(plugindir, name + '.zip')) if zip_importer: name = module_name if not zip_importer.find_module(name): error = _("Failed loading zipped plugin %r") % name self.plugin_error(name, error) return None module_pathname = zip_importer.get_filename(name) else: try: info = imp.find_module(name, [plugindir]) module_file = info[0] module_pathname = info[1] except ImportError: error = _("Failed loading plugin %r") % name self.plugin_error(name, error) return None plugin = None try: existing_plugin, existing_plugin_index = self._get_plugin_index_by_name( name) if existing_plugin: log.warning( "Module %r conflict: unregistering previously" " loaded %r version %s from %r", existing_plugin.module_name, existing_plugin.name, existing_plugin.version, existing_plugin.file) _unregister_module_extensions(name) full_module_name = _PLUGIN_MODULE_PREFIX + name if zip_importer: plugin_module = zip_importer.load_module(full_module_name) else: plugin_module = imp.load_module(full_module_name, *info) plugin = PluginWrapper(plugin_module, plugindir, file=module_pathname, manifest_data=manifest_data) compatible_versions = _compatible_api_versions(plugin.api_versions) if compatible_versions: log.debug( "Loading plugin %r version %s, compatible with API: %s", plugin.name, plugin.version, ", ".join([ version_to_string(v, short=True) for v in sorted(compatible_versions) ])) plugin.compatible = True setattr(picard.plugins, name, plugin_module) if existing_plugin: self.plugins[existing_plugin_index] = plugin else: self.plugins.append(plugin) else: error = _("Plugin '%s' from '%s' is not compatible with this " "version of Picard.") % (plugin.name, plugin.file) self.plugin_error(plugin.name, error, log_func=log.warning) except VersionError as e: error = _("Plugin %r has an invalid API version string : %s") % ( name, e) self.plugin_error(name, error) except BaseException: error = _("Plugin %r : %s") % (name, traceback.format_exc()) self.plugin_error(name, error) if module_file is not None: module_file.close() return plugin
def test_version_conv_11(self): l, s = ('1', '1', '0', 'dev', '0'), '1.1.0.dev0' self.assertEqual(version_to_string(l), s)
def test_version_conv_3(self): l, s = (1, 1, 0, 'dev', 0), '1.1.0dev0' self.assertEqual(version_to_string(l), s) self.assertEqual(l, version_from_string(s))
def load_plugin(self, name, plugindir): module_file = None (importer, module_name, manifest_data) = zip_import(os.path.join(plugindir, name)) if importer: name = module_name if not importer.find_module(name): error = _("Failed loading zipped plugin %r") % name self.plugin_error(name, error) return None module_pathname = importer.get_filename(name) else: try: info = imp.find_module(name, [plugindir]) module_file = info[0] module_pathname = info[1] except ImportError: error = _("Failed loading plugin %r") % name self.plugin_error(name, error) return None plugin = None try: index = None for i, p in enumerate(self.plugins): if name == p.module_name: log.warning( "Module %r conflict: unregistering previously" " loaded %r version %s from %r", p.module_name, p.name, p.version, p.file) _unregister_module_extensions(name) index = i break if not importer: plugin_module = imp.load_module(_PLUGIN_MODULE_PREFIX + name, *info) else: plugin_module = importer.load_module(_PLUGIN_MODULE_PREFIX + name) plugin = PluginWrapper(plugin_module, plugindir, file=module_pathname, manifest_data=manifest_data) versions = [ version_from_string(v) for v in list(plugin.api_versions) ] compatible_versions = list(set(versions) & self._api_versions) if compatible_versions: log.debug( "Loading plugin %r version %s, compatible with API: %s", plugin.name, plugin.version, ", ".join([ version_to_string(v, short=True) for v in sorted(compatible_versions) ])) plugin.compatible = True setattr(picard.plugins, name, plugin_module) if index is not None: self.plugins[index] = plugin else: self.plugins.append(plugin) else: error = _("Plugin '%s' from '%s' is not compatible with this " "version of Picard.") % (plugin.name, plugin.file) self.plugin_error(plugin.name, error, log_func=log.warning) except VersionError as e: error = _("Plugin %r has an invalid API version string : %s") % ( name, e) self.plugin_error(name, error) except: error = _("Plugin %r : %s") % (name, traceback.format_exc()) self.plugin_error(name, error) if module_file is not None: module_file.close() return plugin