on master help sign in
[mod] create static directory for each external plugin
commite45a269ecbb3a80b61d706e5dc7075e250f5afa9 authorAdam Tauber <asciimoo@gmail.com> dateWed 22 Jul 2020, 19:02 (6 years ago) parent81e9c75
1 file changed+30−8
searx/plugins/__init__.py+30−8
searx/plugins/__init__.py+30−8
@@ -17,7 +17,8 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
1717
1818from hashlib import sha256
1919from importlib import import_module
20−from os.path import abspath, basename, dirname, exists, join
20+from os import listdir, mkdir, remove
21+from os.path import abspath, basename, dirname, exists, isdir, join
2122from shutil import copyfile
2223from sys import exit, version_info
2324from traceback import print_exc
@@ -105,34 +106,55 @@ def load_external_plugins(plugin_names):
105106 return plugins
106107
107108
108−def check_resource(base_path, resource_path, name, dir_prefix):
109+def check_resource(base_path, resource_path, name, target_dir, plugin_dir):
109110 dep_path = join(base_path, resource_path)
110111 file_name = basename(dep_path)
111− resource_name = '{0}_{1}'.format('_'.join(name.split()), file_name)
112− resource_path = join(static_path, 'plugins', dir_prefix, resource_name)
112+ resource_path = join(target_dir, file_name)
113113 if not exists(resource_path) or sha_sum(dep_path) != sha_sum(resource_path):
114114 try:
115115 copyfile(dep_path, resource_path)
116116 except:
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))
118118 exit(3)
119119
120120 # returning with the web path of the resource
121− return join('plugins', dir_prefix, resource_name)
121+ return join('plugins', plugin_dir, file_name)
122122
123123
124124def 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):
128+ try:
129+ mkdir(target_dir)
130+ except:
131+ logger.critical('failed to create resource directory {0} for plugin {1}'.format(target_dir, name))
132+ exit(3)
133+
134+ resources = []
135+
125136 if hasattr(pkg, 'js_dependencies'):
137+ resources.extend(map(basename, pkg.js_dependencies))
126138 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)
128140 for x in pkg.js_dependencies
129141 ])
130142 if hasattr(pkg, 'css_dependencies'):
143+ resources.extend(map(basename, pkg.css_dependencies))
131144 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)
133146 for x in pkg.css_dependencies
134147 ])
135148
149+ for f in listdir(target_dir):
150+ if basename(f) not in resources:
151+ resource_path = join(target_dir, basename(f))
152+ try:
153+ remove(resource_path)
154+ except:
155+ logger.critical('failed to remove unused resource file {0} for plugin {1}'.format(resource_path, name))
156+ exit(3)
157+
136158
137159def sha_sum(filename):
138160 with open(filename, "rb") as f: