Package yapsy :: Module AutoInstallPluginManager
[hide private]

Source Code for Module yapsy.AutoInstallPluginManager

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*- 
  3   
  4  """ 
  5  Defines plugin managers that can handle the installation of plugin 
  6  files into the right place. Then the end-user does not have to browse 
  7  to the plugin directory to install them. 
  8  """ 
  9   
 10  import sys, os 
 11  import logging 
 12  import ConfigParser 
 13  import shutil 
 14   
 15  from IPlugin import IPlugin 
 16   
 17   
 18  from PluginManager import PluginManager,PluginManagerDecorator 
 19  from PluginManager import PLUGIN_NAME_FORBIDEN_STRING 
 20   
 21   
 22   
23 -class AutoInstallPluginManager(PluginManagerDecorator):
24 """ 25 A plugin manager that also manages the installation of the plugin 26 files into the appropriate directory. 27 """ 28 29
30 - def __init__(self, 31 plugin_install_dir=None, 32 decorated_manager=None, 33 # The following args will only be used if we need to 34 # create a default PluginManager 35 categories_filter={"Default":IPlugin}, 36 directories_list=None, 37 plugin_info_ext="yapsy-plugin"):
38 """ 39 Create the plugin manager and set up the directory where to 40 install new plugins. 41 42 Arguments 43 44 ``plugin_install_dir`` 45 The directory where new plugins to be installed will be copied. 46 47 .. warning:: If ``plugin_install_dir`` does not correspond to 48 an element of the ``directories_list``, it is appended to 49 the later. 50 51 """ 52 # Create the base decorator class 53 PluginManagerDecorator.__init__(self, 54 decorated_manager, 55 categories_filter, 56 directories_list, 57 plugin_info_ext) 58 # set the directory for new plugins 59 self.plugins_places=[] 60 self.setInstallDir(plugin_install_dir)
61
62 - def setInstallDir(self,plugin_install_dir):
63 """ 64 Set the directory where to install new plugins. 65 """ 66 if not (plugin_install_dir in self.plugins_places): 67 self.plugins_places.append(plugin_install_dir) 68 self.install_dir = plugin_install_dir
69
70 - def getInstallDir(self):
71 """ 72 Return the directory where new plugins should be installed. 73 """ 74 return self.install_dir
75
76 - def install(self, directory, plugin_info_filename):
77 """ 78 Giving the plugin's info file (e.g. ``myplugin.yapsy-plugin``), 79 and the directory where it is located, get all the files that 80 define the plugin and copy them into the correct directory. 81 82 Return ``True`` if the installation is a success, ``False`` if 83 it is a failure. 84 """ 85 # start collecting essential info about the new plugin 86 plugin_info, config_parser = self._gatherCorePluginInfo(directory, plugin_info_filename) 87 # now determine the path of the file to execute, 88 # depending on wether the path indicated is a 89 # directory or a file 90 if not (os.path.exists(plugin_info.path) or os.path.exists(plugin_info.path+".py") ): 91 logging.warning("Could not find the plugin's implementation for %s." % plugin_info.name) 92 return False 93 if os.path.isdir(plugin_info.path): 94 try: 95 shutil.copytree(plugin_info.path, 96 os.path.join(self.install_dir,os.path.basename(plugin_info.path))) 97 shutil.copy(os.path.join(directory, plugin_info_filename), 98 self.install_dir) 99 except: 100 logging.error("Could not install plugin: %s." % plugin_info.name) 101 return False 102 else: 103 return True 104 elif os.path.isfile(plugin_info.path+".py"): 105 try: 106 shutil.copy(plugin_info.path+".py", 107 self.install_dir) 108 shutil.copy(os.path.join(directory, plugin_info_filename), 109 self.install_dir) 110 except: 111 logging.error("Could not install plugin: %s." % plugin_info.name) 112 return False 113 else: 114 return True 115 else: 116 return False
117