@@ -17,7 +17,8 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
18from hashlib import sha256
18from hashlib import sha256
19from importlib import import_module
19from importlib import import_module
20+from os import listdir, mkdir, remove
20−from os.path import abspath, basename, dirname, exists, join
21+from os.path import abspath, basename, dirname, exists, isdir, join
21from shutil import copyfile
22from shutil import copyfile
22from sys import exit, version_info
23from sys import exit, version_info
23from traceback import print_exc
24from traceback import print_exc
@@ -105,34 +106,55 @@ def load_external_plugins(plugin_names):
105 return plugins
106 return plugins
108−def check_resource(base_path, resource_path, name, dir_prefix):
109+def check_resource(base_path, resource_path, name, target_dir, plugin_dir):
109 dep_path = join(base_path, resource_path)
110 dep_path = join(base_path, resource_path)
110 file_name = basename(dep_path)
111 file_name = basename(dep_path)
111− resource_name = '{0}_{1}'.format('_'.join(name.split()), file_name)
112+ resource_path = join(target_dir, file_name)
112− resource_path = join(static_path, 'plugins', dir_prefix, resource_name)
113 if not exists(resource_path) or sha_sum(dep_path) != sha_sum(resource_path):
113 if not exists(resource_path) or sha_sum(dep_path) != sha_sum(resource_path):
115 copyfile(dep_path, resource_path)
115 copyfile(dep_path, resource_path)
117− logger.critical('failed to copy plugin resource {0} for plugin {1}'.format(resource_name, name))
117+ logger.critical('failed to copy plugin resource {0} for plugin {1}'.format(file_name, name))
120 # returning with the web path of the resource
120 # returning with the web path of the resource
121− return join('plugins', dir_prefix, resource_name)
121+ return join('plugins', plugin_dir, file_name)
124def fix_package_resources(pkg, name):
124def fix_package_resources(pkg, name):
125+ plugin_dir = 'plugin_' + name
126+ target_dir = join(static_path, 'plugins', plugin_dir)
127+ if not isdir(target_dir):
131+ logger.critical('failed to create resource directory {0} for plugin {1}'.format(target_dir, name))
125 if hasattr(pkg, 'js_dependencies'):
136 if hasattr(pkg, 'js_dependencies'):
137+ resources.extend(map(basename, pkg.js_dependencies))
126 pkg.js_dependencies = tuple([
138 pkg.js_dependencies = tuple([
127− check_resource(pkg.__base_path, x, name, 'js')
139+ check_resource(pkg.__base_path, x, name, target_dir, plugin_dir)
128 for x in pkg.js_dependencies
140 for x in pkg.js_dependencies
130 if hasattr(pkg, 'css_dependencies'):
142 if hasattr(pkg, 'css_dependencies'):
143+ resources.extend(map(basename, pkg.css_dependencies))
131 pkg.css_dependencies = tuple([
144 pkg.css_dependencies = tuple([
132− check_resource(pkg.__base_path, x, name, 'css')
145+ check_resource(pkg.__base_path, x, name, target_dir, plugin_dir)
133 for x in pkg.css_dependencies
146 for x in pkg.css_dependencies
149+ for f in listdir(target_dir):
150+ if basename(f) not in resources:
151+ resource_path = join(target_dir, basename(f))
153+ remove(resource_path)
155+ logger.critical('failed to remove unused resource file {0} for plugin {1}'.format(resource_path, name))
137def sha_sum(filename):
159def sha_sum(filename):
138 with open(filename, "rb") as f:
160 with open(filename, "rb") as f: