From 6bef4843be1c2e3c33441d5395524ceb192dd950 Mon Sep 17 00:00:00 2001 From: Alessio Sergi Date: Sun, 25 May 2014 18:45:45 +0200 Subject: [PATCH] Fix plugins path Unhardcode plugins path. Use plugins path properly: temporarily add plugins path to system path and then restore it. --- glances/core/glances_globals.py | 15 ++++++++---- glances/core/glances_stats.py | 42 +++++++++++++++------------------ 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/glances/core/glances_globals.py b/glances/core/glances_globals.py index 732cd6b3..3291af7e 100644 --- a/glances/core/glances_globals.py +++ b/glances/core/glances_globals.py @@ -42,11 +42,6 @@ if psutil_version < psutil_min_version: print('psutil 2.0 or higher is needed. Glances cannot start.') sys.exit(1) -# Path definitions -work_path = os.path.realpath(os.path.dirname(__file__)) -appname_path = os.path.split(sys.argv[0])[0] -sys_prefix = os.path.realpath(os.path.dirname(appname_path)) - # PY3? is_py3 = sys.version_info >= (3, 3) @@ -57,6 +52,16 @@ is_linux = sys.platform.startswith('linux') is_mac = sys.platform.startswith('darwin') is_windows = sys.platform.startswith('win') +# Path definitions +work_path = os.path.realpath(os.path.dirname(__file__)) +appname_path = os.path.split(sys.argv[0])[0] +sys_prefix = os.path.realpath(os.path.dirname(appname_path)) + +# Set the plugins path +plugins_path = os.path.realpath(os.path.join(work_path, '..', 'plugins')) +sys_path = sys.path[:] +sys.path.insert(0, plugins_path) + # i18n gettext_domain = __appname__ i18n_path = os.path.realpath(os.path.join(work_path, '..', '..', 'i18n')) diff --git a/glances/core/glances_stats.py b/glances/core/glances_stats.py index ab759206..0e6961ee 100644 --- a/glances/core/glances_stats.py +++ b/glances/core/glances_stats.py @@ -21,6 +21,8 @@ import collections import os import sys +from glances.core.glances_globals import plugins_path, sys_path + class GlancesStats(object): """ @@ -68,24 +70,21 @@ class GlancesStats(object): """ Load all plugins in the "plugins" folder """ - - # Set the plugins' path - plug_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../plugins") - sys.path.insert(0, plug_dir) - header = "glances_" - for plug in os.listdir(plug_dir): - if (plug.startswith(header) and - plug.endswith(".py") and - plug != (header + "plugin.py")): + for item in os.listdir(plugins_path): + if (item.startswith(header) and + item.endswith(".py") and + item != (header + "plugin.py")): # Import the plugin - m = __import__(os.path.basename(plug)[:-3]) + plugin = __import__(os.path.basename(item)[:-3]) # Add the plugin to the dictionary # The key is the plugin name # for example, the file glances_xxx.py # generate self._plugins_list["xxx"] = ... - plugname = os.path.basename(plug)[len(header):-3].lower() - self._plugins[plugname] = m.Plugin(args=args) + plugin_name = os.path.basename(item)[len(header):-3].lower() + self._plugins[plugin_name] = plugin.Plugin(args=args) + # Restoring system path + sys.path = sys_path def getAllPlugins(self): """ @@ -197,23 +196,20 @@ class GlancesStatsClient(GlancesStats): def set_plugins(self, input_plugins): """ - Set the plugin list accoring to the Glances' server + Set the plugin list according to the Glances server """ - - # Set the plugins' path - plug_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../plugins") - sys.path.insert(0, plug_dir) - header = "glances_" - for plug in input_plugins: + for item in input_plugins: # Import the plugin - m = __import__(header + plug) - # Add the plugin to the dictionnary + plugin = __import__(header + item) + # Add the plugin to the dictionary # The key is the plugin name # for example, the file glances_xxx.py # generate self._plugins_list["xxx"] = ... - # print "DEBUG: Init %s plugin" % plug - self._plugins[plug] = m.Plugin() + # print "DEBUG: Init %s plugin" % item + self._plugins[item] = plugin.Plugin() + # Restoring system path + sys.path = sys_path class GlancesStatsClientSNMP(GlancesStats):