add a URL that can be used to download the current version of a plugin

This commit is contained in:
Kevin Dangoor 2010-03-31 15:40:31 -04:00
Родитель fe1055e07b
Коммит 9775d8731d
4 изменённых файлов: 72 добавлений и 13 удалений

Просмотреть файл

@ -1076,7 +1076,6 @@ def register_test_plugins(request, response):
raise FileNotFound("Test plugins are only in development environment")
return _plugin_response(response, c.test_plugin_path)
@expose(r'^/plugin/script/(?P<plugin_location>[^/]+)/(?P<plugin_name>[^/]+)/(?P<path>.*)', 'GET', auth=False)
def load_script(request, response):
response.content_type = "text/javascript"
@ -1175,6 +1174,44 @@ def plugin_gallery(request, response):
result.append(data)
return _respond_json(response, result)
class FileIterable(object):
def __init__(self, filename):
self.filename = filename
def __iter__(self):
return FileIterator(self.filename)
class FileIterator(object):
chunk_size = 4096
def __init__(self, filename):
self.filename = filename
self.fileobj = open(self.filename, 'rb')
def __iter__(self):
return self
def next(self):
chunk = self.fileobj.read(self.chunk_size)
if not chunk:
raise StopIteration
return chunk
@expose(r'^/plugin/download/(?P<plugin_name>[^/]+)/current/$', "GET", auth=False)
def download_plugin(request, response):
plugin_name = request.kwargs['plugin_name']
plugin = GalleryPlugin.get_plugin(plugin_name)
if plugin is None:
raise FileNotFound("Plugin %s is not in the gallery" % (plugin_name))
location = plugin.get_path()
if location.endswith(".zip"):
response.content_type = "application/zip"
else:
response.content_type = "text/javascript"
response.app_iter = FileIterable(location)
response.content_length = os.path.getsize(location)
response.last_modified = os.path.getmtime(location)
response.etag = '%s-%s-%s' % (response.last_modified,
response.content_length, hash(location))
return response()
def _wrap_script(plugin_name, script_path, script_text):
if script_path:
module_name = os.path.splitext(script_path)[0]

Просмотреть файл

@ -810,3 +810,21 @@ class GalleryPlugin(Base):
s = _get_session()
return s.query(cls).order_by('name').all()
def get_path(self, version=None):
"""Retrieves the path to the plugin for a particular
version. If the version is not provided, use the
current version."""
if version is None:
version = self.version
gallery_root = config.c.gallery_root
plugin_dir = gallery_root / self.name
location = plugin_dir / ("%s-%s.zip" % (self.name, version))
if not location.exists():
location = plugin_dir / (version + ".js")
if not location.exists():
raise FileNotFound("Unable to find the plugin files for '%s' version %s"
% (self.name, self.version))
return location

Просмотреть файл

@ -359,16 +359,7 @@ def _collect_dependencies(main_plugin):
return result
def _perform_installation(user, plugin):
version = plugin.version
gallery_root = config.c.gallery_root
plugin_dir = gallery_root / plugin.name
location = plugin_dir / ("%s-%s.zip" % (plugin.name, version))
if not location.exists():
location = plugin_dir / (version + ".js")
if not location.exists():
raise PluginError("Unable to find the plugin files for '%s' version %s"
% (plugin.name, plugin.version))
location = plugin.get_path()
project = get_project(user, user, "BespinSettings")
@ -388,7 +379,7 @@ def _perform_installation(user, plugin):
def install_plugin_from_gallery(user, plugin_name):
plugin = GalleryPlugin.get_plugin(plugin_name)
if not plugin:
raise PluginError('Cannot find plugin "%s" in the gallery' % (plugin_name))
raise FileNotFound('Cannot find plugin "%s" in the gallery' % (plugin_name))
deps = _collect_dependencies(plugin)

Просмотреть файл

@ -501,3 +501,16 @@ def test_error_message_when_uploading_plugin_without_enough_metadata():
response = app.post("/plugin/upload/single_file_plugin1", status=400)
print response.body
assert response.body == "Errors in plugin metadata: ['description is required', 'version is required', 'licenses is required']"
def test_download_a_plugin():
_init_data()
plugins.save_to_gallery(macgyver, plugindir / "plugin1")
plugins.save_to_gallery(macgyver, plugindir / "single_file_plugin3.js")
response = app.get("/plugin/download/plugin1/current/")
assert response.content_type == "application/zip"
response = app.get("/plugin/download/single_file_plugin3/current/")
assert response.content_type == "text/javascript"
response = app.get("/plugin/download/doesnotexist/current/", status=404)