home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / xbmc-9.11.exe / plugins / Programs / SVN Repo Installer / installerAPI / xbmcplugin_actions.py < prev    next >
Encoding:
Python Source  |  2009-11-03  |  6.1 KB  |  177 lines

  1. """
  2.     SVN Repo Installer - Actions
  3. """
  4.  
  5. import sys, os
  6. import os.path
  7. import re
  8. import xbmc, xbmcgui
  9. from xbmcplugin_lib import *
  10. from shutil import rmtree, copytree
  11.  
  12. __plugin__ = sys.modules["__main__"].__plugin__
  13. __date__ = '19-06-2009'
  14. log("Module: %s Dated: %s loaded!" % (__name__, __date__))
  15.  
  16. class Main:
  17.  
  18.     INSTALLED_ITEMS_FILENAME = os.path.join( os.getcwd(), "installed_items.dat" )
  19.     
  20.     def __init__( self ):
  21.         log( "%s started!" % self.__class__ )
  22.         try:
  23.             self._parse_argv()
  24.             if ( self.args.has_key("delete") ):
  25.                 self.delete_update_item()
  26. #            elif ( self.args.has_key("update") ):
  27. #                self.update()
  28.             elif ( self.args.has_key("self_update") ):
  29.                 self.self_update()
  30.         except Exception, e:
  31.             xbmcgui.Dialog().ok(__plugin__ + " ERROR!", str(e))
  32.  
  33.     ########################################################################################################################
  34.     def _parse_argv(self):
  35.         # call Info() with our formatted argv to create the self.args object
  36.         exec "self.args = Info(%s)" % ( unquote_plus( sys.argv[ 2 ][ 1 : ].replace( "&", ", " ) ), )
  37.  
  38.     ########################################################################################################################
  39.     def delete_update_item(self):
  40.         log("delete_update_item()")
  41.  
  42.         if xbmcgui.Dialog().yesno(__plugin__, self.args.title,"", "", xbmc.getLocalizedString( 30020 ), xbmc.getLocalizedString( 30022 )):    # Delete, Skip
  43.             items = loadFileObj(self.INSTALLED_ITEMS_FILENAME)
  44.             if items is None and self.args.has_key( "delete_from_list" ):
  45.                 items = {'filepath': self.args.delete}
  46.             filepath = self.args.delete
  47.             # find addon from installed list
  48.             for i, item in enumerate(items):
  49.                 if item['filepath'] == filepath or self.args.has_key( "delete_from_list" ):
  50.                     log("addon details: %s" % item)
  51.                     # make backup - if not deleting a backup copy
  52.                     if ".backup" not in filepath:
  53.                         backupPath, category = self.makeBackup(filepath)
  54.                         removeList = False
  55.                     else:
  56.                         category = self.parseCategory(filepath)
  57.                         backupPath = filepath
  58.                         removeList = True
  59.                     if backupPath and category:
  60.                         # remove addon dir tree
  61.                         try:
  62.                             rmtree(filepath)
  63.                             log("dir deleted: " + filepath)
  64.  
  65.                             # update list with new filepath, or remove from list if delting backup
  66.                             if removeList:
  67.                                 del items[i]
  68.                             else:
  69.                                 # update filepath to indicated Deleted
  70.                                 items[i]['filepath'] = backupPath
  71.                             if not self.args.has_key( "delete_from_list" ):
  72.                                 saveFileObj(self.INSTALLED_ITEMS_FILENAME, items)
  73.                             if not removeList:
  74.                                 xbmcgui.Dialog().ok(__plugin__, xbmc.getLocalizedString( 30018 ), xbmc.getLocalizedString( 30004 ), category)
  75.                             else:
  76.                                 xbmcgui.Dialog().ok(__plugin__, xbmc.getLocalizedString( 30018 ), category)     # no backup
  77.                             # force list refresh
  78.                             xbmc.executebuiltin('Container.Refresh')
  79.                         except:
  80.                             handleException("delete_update_item()")
  81.                     break
  82.  
  83.     #####################################################################################################
  84.     def parseCategory(self, filepath):
  85.         try:
  86.             cat = re.search("(plugins.*|scripts.*)$",  filepath, re.IGNORECASE).group(1)
  87.             cat = cat.replace("\\", "/")
  88.         except:
  89.             cat = ""
  90.         log("parseCategory() cat=%s" % cat)
  91.         return cat
  92.  
  93.     ########################################################################################################################
  94.     def makeBackup( self, installedPath ):
  95.         """ copy addon to <addon_category>/.backups """
  96.         log("> makeBackup() installedPath=%s" % installedPath)
  97.  
  98.         try:
  99.             dialog = xbmcgui.DialogProgress()
  100.             dialog.create( __plugin__, "Making Backup ..." )
  101.             category = ""
  102.             if installedPath[-1] in ('\\','/'):
  103.                 installedPath = installedPath[:-1]
  104.  
  105.             # extract rootpath , addon name
  106.             matches = re.search("(.*)[\\\/](.*?)$", installedPath)
  107.             rootpath = matches.group(1)
  108.             name = matches.group(2)
  109.             log("rootpath=%s name=%s" % (rootpath, name))
  110.  
  111.             # create root backup path
  112.             backupPath = os.path.join(rootpath, ".backups")
  113.             log("backupPath=%s" % backupPath)
  114.             # make root backup dir
  115.             try:
  116.                 os.makedirs(backupPath)
  117.                 log("created dir " + backupPath )
  118.             except: pass
  119.  
  120.             # remove any existing backup
  121.             try:
  122.                 backupPath = os.path.join(backupPath, name)
  123.                 rmtree( backupPath, ignore_errors=True )        
  124.                 log("removed existing dir " + backupPath)
  125.             except: pass
  126.  
  127.             # copy to backup path
  128.             log("copytree %s -> %s" % (installedPath, backupPath))
  129.             copytree(installedPath, backupPath)
  130.             log("copytree success")
  131.  
  132.             # extract category in .backups
  133.             category = re.search("(plugins.*|scripts.*)$", backupPath).group(1)
  134.             # check file exists in backup
  135.             if not os.path.exists(os.path.join(backupPath, "default.py")):
  136.                 xbmcgui.Dialog().ok(__plugin__, "Make backup failed!", category)
  137.                 backupPath = ""
  138.         except:
  139.             handleException("makeBackup()")
  140.             backupPath = ""
  141.         dialog.close()
  142.         log("< makeBackup() backupPath=%s category=%s" % (backupPath, category))
  143.         return (backupPath, category)
  144.  
  145.     ########################################################################################################################
  146.     def _parseCategoryPath(self):
  147.         # extract category in .backups
  148.         category = re.search("(plugins.*|scripts.*)$", backupPath).group(1)
  149.  
  150.     ########################################################################################################################
  151.     def self_update(self):
  152.         log("> self_update()")
  153.  
  154.         # make backup
  155.         filepath= xbmc.translatePath("special://home/plugins/programs/" + __plugin__)
  156.         backupPath, category = self.makeBackup(filepath)
  157.  
  158.         # launch download from backup
  159.         if backupPath and category:
  160.             # remove self_update arg from downloader url
  161.             url_args = sys.argv[2].replace("self_update=True&", "")
  162.             # create path to module in backups
  163.             filepath= "plugin://programs/.backups/" + __plugin__
  164.             path = '%s%s' % ( filepath, url_args, )
  165.  
  166.             # run module from backup
  167.             command = 'XBMC.RunPlugin(%s)' % path
  168.             log(command)
  169.             xbmc.executebuiltin(command)
  170.  
  171.         log("< self_update()")
  172.  
  173.     
  174. if ( __name__ == "__main__" ):
  175.     Main()
  176.  
  177.