Fix bugs in init and plugin upload filenames

This commit is contained in:
Tulir Asokan 2018-11-01 12:28:56 +02:00
parent 767885cec7
commit 28d7731e70
4 changed files with 15 additions and 7 deletions

View File

@ -58,7 +58,7 @@ loop = asyncio.get_event_loop()
init_db(db_session)
clients = init_client(loop)
init_plugin_instance_class(db_session, config)
init_plugin_instance_class(db_session, config, loop)
management_api = init_management(config, loop)
server = MaubotServer(config, management_api, loop)

View File

@ -129,7 +129,10 @@ class PluginInstance:
return
self.log.debug("Stopping plugin instance...")
self.running = False
try:
await self.plugin.stop()
except Exception:
self.log.exception("Failed to stop instance")
self.plugin = None
@classmethod

View File

@ -151,7 +151,7 @@ class ZippedPluginLoader(PluginLoader):
def _get_importer(self, reset_cache: bool = False) -> zipimporter:
try:
if not self._importer:
if not self._importer or self._importer.archive != self.path:
self._importer = zipimporter(self.path)
if reset_cache:
self._importer.reset_cache()

View File

@ -15,8 +15,10 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from aiohttp import web
from io import BytesIO
from time import time
import traceback
import os.path
import re
from ...loader import PluginLoader, ZippedPluginLoader, MaubotZipImportError
from .responses import (ErrPluginNotFound, ErrPluginInUse, plugin_import_error,
@ -81,11 +83,14 @@ async def upload_new_plugin(content: bytes, pid: str, version: str) -> web.Respo
async def upload_replacement_plugin(plugin: ZippedPluginLoader, content: bytes, new_version: str
) -> web.Response:
dirname = os.path.dirname(plugin.path)
filename = os.path.basename(plugin.path)
if plugin.version in filename:
filename = filename.replace(plugin.version, new_version)
old_filename = os.path.basename(plugin.path)
if plugin.version in old_filename:
filename = old_filename.replace(plugin.version, new_version)
if filename == old_filename:
filename = re.sub(f"{re.escape(plugin.version)}(-ts[0-9]+)?",
f"{new_version}-ts{int(time())}", old_filename)
else:
filename = filename.rstrip(".mbp")
filename = old_filename.rstrip(".mbp")
filename = f"{filename}-v{new_version}.mbp"
path = os.path.join(dirname, filename)
with open(path, "wb") as p: