Advertisement
Guest User

simple addon installer prototype1

a guest
Feb 11th, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. #!/usr/bin/python
  2. import xml.dom.minidom
  3. import os, sys, tarfile, urllib, hashlib, ctypes
  4.  
  5.  
  6. ADDON_DIR = '/tmp'
  7. if len(sys.argv) > 2: ADDON_DIR = sys.argv[-1]
  8. print('addon path', ADDON_DIR )
  9.  
  10. IS32BIT = (ctypes.sizeof(ctypes.c_void_p)==4)   #http://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode
  11. _linux_types = 'ubuntu debian fedora suse maemo'.split()
  12. def _guess_subplatform():
  13.     if os.name == 'posix':
  14.         sysname, nodename, release, version, machine = os.uname()
  15.         for linuxtype in _linux_types:
  16.             if linuxtype in version.lower(): return linuxtype
  17.         return 'ubuntu' # default to ubuntu
  18.     return ''
  19.  
  20.  
  21. def download( url, md5=None, path='' ):
  22.     print('downloading', url )
  23.     assert not path.startswith('../')   # do not allow outside of REXADDONS
  24.     data = urllib.urlopen( url ).read()
  25.     if md5: assert hashlib.md5sum( data ).hexdigest() == md5
  26.     tmp = '/tmp/%s' %os.path.split( url )[-1]
  27.     f = open( tmp, 'wb' ); f.write( data ); f.close()
  28.     if url.endswith('.tar.gz') or url.endswith('.tgz') or url.endswith('.tar.bz2') or url.endswith('.tar'):
  29.         print( 'untarring', tmp )
  30.         tar = tarfile.TarFile.open( tmp, 'r:*' )
  31.         fpath = os.path.join(ADDON_DIR,path)
  32.         if not os.path.isdir(fpath): os.makedirs( fpath )
  33.         print( 'extracting to:', fpath )
  34.         tar.extractall( path=fpath )
  35.  
  36. def install_addon( url, PATH='/tmp/addontest' ):
  37.     doc = xml.dom.minidom.parse( url )
  38.     platforms = doc.documentElement.getAttribute('platforms')
  39.     if not platforms: print('warning: this addon did not describe what platforms it can run on')
  40.     elif platforms == 'all': print('addon runs on all platforms')
  41.     else:
  42.         print('addon only supports the following platforms:')
  43.         for plat in platforms.split(','): print( plat )
  44.  
  45.     ## extract simple archives ##
  46.     for ar in doc.getElementsByTagName('archive'):
  47.         url = ar.getAttribute('url')
  48.         download( url, md5=ar.getAttribute('md5'), path=ar.getAttribute('path') )
  49.  
  50.     for library in doc.getElementsByTagName('library'):
  51.         libs = []
  52.         if sys.platform == 'linux2':
  53.             for lib in library.getElementsByTagName('linux'):
  54.                 if IS32BIT and lib.getAttribute('bits')=='32': libs.append( lib )
  55.                 elif not IS32BIT and lib.getAttribute('bits')=='64': libs.append( lib )
  56.         elif sys.platform.startswith('win'):
  57.             for lib in library.getElementsByTagName('windows'):
  58.                 if IS32BIT and lib.getAttribute('bits')=='32': libs.append( lib )
  59.                 elif not IS32BIT and lib.getAttribute('bits')=='64': libs.append( lib )
  60.         elif sys.platform == 'darwin':
  61.             for lib in library.getElementsByTagName('osx'):
  62.                 if IS32BIT and lib.getAttribute('bits')=='32': libs.append( lib )
  63.                 elif not IS32BIT and lib.getAttribute('bits')=='64': libs.append( lib )
  64.  
  65.         if not libs:
  66.             if library.getAttribute('required')=='True': print('error: this library is required %s' %library.getAttribute('name'))
  67.             print('no precompiled library for your OS')
  68.             sources = library.getElementsByTagName('source')
  69.             for src in sources:
  70.                 print('source is available here:')
  71.                 print( src.getAttribute('url') )
  72.                 print( src.getAttribute('notes') )
  73.  
  74.         for lib in libs:
  75.             url = lib.getAttribute('url')
  76.             download( url, md5=lib.getAttribute('md5'), path=lib.getAttribute('path') )
  77.  
  78.     configs = {}
  79.     for cfg in doc.getElementsByTagName('config')# embeded config files
  80.         name = cfg.getAttribute('name')
  81.         assert name
  82.         configs[ name ] = cfg.firstChild.nodeValue  # must be a text node
  83.         open( os.path.join( ADDON_DIR, name ), 'wb' ).write( cfg.firstChild.nodeValue )
  84.  
  85.     print configs
  86.     return configs  # installer-manager needs to keep track of ini-files to enable/disable the addon
  87.  
  88.  
  89. if __name__ == '__main__':
  90.     install_addon( sys.argv[1] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement