def test_create_eggs_cache(self, get_default_store): original_eggs_cache = self._manager._eggs_cache try: get_default_store.return_value = self.store PluginEgg(store=self.store, plugin_name=u'foobar', egg_content=b'lorem', egg_md5sum=u'e194544df936c31ebf9b4c2d4a6ef213') PluginEgg(store=self.store, plugin_name=u'foo', egg_content=b'ipsum', egg_md5sum=u'2b3bd636ec90eb39c3c171dd831b8c30') PluginEgg(store=self.store, plugin_name=u'bar', egg_content=b'lorem ipsum', egg_md5sum=u'5f084b7281515082703bd903708c977a') self._manager._create_eggs_cache() plugins_dir = self._manager._eggs_cache with open(os.path.join(plugins_dir, 'foobar.egg')) as f: self.assertEqual(f.read(), 'lorem') with open(os.path.join(plugins_dir, 'foo.egg')) as f: self.assertEqual(f.read(), 'ipsum') with open(os.path.join(plugins_dir, 'bar.egg')) as f: self.assertEqual(f.read(), 'lorem ipsum') self.assertEqual(set(self._manager.egg_plugins_names), {'foobar', 'foo', 'bar'}) finally: self._manager._eggs_cache = original_eggs_cache
def _insert_egg(self, plugin_name, filename): from stoqlib.database.runtime import new_store from stoqlib.domain.plugin import PluginEgg from stoqlib.lib.fileutils import md5sum_for_filename print('Inserting plugin egg %s %s' % (plugin_name, filename)) md5sum = str(md5sum_for_filename(filename)) with open(filename, 'rb') as f: with new_store() as store: plugin_egg = store.find(PluginEgg, plugin_name=plugin_name).one() if plugin_egg is None: plugin_egg = PluginEgg( store=store, plugin_name=plugin_name, ) plugin_egg.egg_content = f.read() plugin_egg.egg_md5sum = md5sum
def _insert_egg(self, plugin_name, filename): from stoqlib.database.runtime import new_store from stoqlib.domain.plugin import PluginEgg from stoqlib.lib.fileutils import md5sum_for_filename print('Inserting plugin egg %s %s' % (plugin_name, filename)) md5sum = str(md5sum_for_filename(filename)) with open(filename, 'rb') as f: with new_store() as store: plugin_egg = store.find(PluginEgg, plugin_name=plugin_name).one() if plugin_egg is None: plugin_egg = PluginEgg( store=store, plugin_name=plugin_name, ) plugin_egg.egg_content = f.read() plugin_egg.egg_md5sum = md5sum
def test_create_eggs_cache(self, get_default_store, get_application_dir): temp_dir = tempfile.mkdtemp() try: get_default_store.return_value = self.store get_application_dir.return_value = temp_dir plugins_dir = os.path.join(temp_dir, 'plugins') os.makedirs(plugins_dir) with open(os.path.join(plugins_dir, 'foobar.egg'), 'wb') as f: f.write('wrong_content') with open(os.path.join(plugins_dir, 'foo.egg'), 'wb') as f: f.write('foo_content') PluginEgg(store=self.store, plugin_name=u'foobar', egg_content='right_content', egg_md5sum=u'e194544df936c31ebf9b4c2d4a6ef213') PluginEgg(store=self.store, plugin_name=u'foo', egg_content='will_not_be_overwritten', egg_md5sum=u'2b3bd636ec90eb39c3c171dd831b8c30') PluginEgg(store=self.store, plugin_name=u'bar', egg_content='bar_content', egg_md5sum=u'5f084b7281515082703bd903708c977a') self._manager._create_eggs_cache() # foobar didn't match md5 so it should have been overwritten with open(os.path.join(plugins_dir, 'foobar.egg')) as f: self.assertEqual(f.read(), 'right_content') # foo matched so it should be untouched with open(os.path.join(plugins_dir, 'foo.egg')) as f: self.assertEqual(f.read(), 'foo_content') # bar didn't exist so it should have been created with open(os.path.join(plugins_dir, 'bar.egg')) as f: self.assertEqual(f.read(), 'bar_content') self.assertEqual(set(self._manager.egg_plugins_names), {'foobar', 'foo', 'bar'}) finally: shutil.rmtree(temp_dir, ignore_errors=True)
def download_plugin(self, plugin_name): """Download a plugin from webservice :param plugin_name: the name of the plugin to download :returns: a deferred """ from stoqlib.lib.webservice import WebService default_store = get_default_store() existing_egg = default_store.find(PluginEgg, plugin_name=plugin_name).one() md5sum = existing_egg and existing_egg.egg_md5sum webapi = WebService() r = webapi.download_plugin(plugin_name, md5sum=md5sum) try: response = r.get_response() except Exception as e: return False, _("Failed to do the request: %s" % (e, )) code = response.status_code if code == 204: msg = _("No update needed. The plugin is already up to date.") log.info(msg) return True, msg if code != 200: return_messages = { 400: _("Plugin not available for this stoq version"), 401: _("The instance is not authorized to download the plugin"), 404: _("Plugin does not exist"), 405: _("This instance has not acquired the specified plugin"), } msg = return_messages.get(code, str(code)) log.warning(msg) return False, msg content = response.content md5sum = unicode(hashlib.md5(content).hexdigest()) with new_store() as store: existing_egg = store.find(PluginEgg, plugin_name=plugin_name).one() if existing_egg is not None: existing_egg.egg_content = content existing_egg.egg_md5sum = md5sum else: PluginEgg( store=store, plugin_name=plugin_name, egg_md5sum=md5sum, egg_content=content, ) self._reload() return True, _("Plugin download successful")
def download_plugin(self, plugin_name, channel=None): """Download a plugin from webservice :param plugin_name: the name of the plugin to download :param channel: the channel the plugin belongs :returns: a deferred """ from stoqlib.lib.webservice import WebService default_store = get_default_store() existing_egg = default_store.find(PluginEgg, plugin_name=plugin_name).one() md5sum = existing_egg and existing_egg.egg_md5sum webapi = WebService() r = webapi.download_plugin(plugin_name, md5sum=md5sum, channel=channel) try: response = r.get_response() except Exception as e: return False, _("Failed to do the request: %s" % (e, )) code = response.status_code if code == 204: msg = _("No update needed. The plugin is already up to date.") log.info(msg) return True, msg if code != 200: return_messages = { 400: _("Plugin not available for this stoq version"), 401: _("The instance is not authorized to download the plugin"), 404: _("Plugin does not exist"), 405: _("This instance has not acquired the specified plugin"), } msg = return_messages.get(code, str(code)) log.warning(msg) return False, msg try: with io.BytesIO() as f: f.write(response.content) with ZipFile(f) as egg: if egg.testzip() is not None: raise BadZipfile md5sum = hashlib.md5(f.getvalue()).hexdigest() with new_store() as store: existing_egg = store.find(PluginEgg, plugin_name=plugin_name).one() if existing_egg is not None: existing_egg.egg_content = f.getvalue() existing_egg.egg_md5sum = md5sum else: PluginEgg( store=store, plugin_name=plugin_name, egg_md5sum=md5sum, egg_content=f.getvalue(), ) except BadZipfile: return False, _("The downloaded plugin is corrupted") self._reload() return True, _("Plugin download successful")