]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxaddons/__init__.py
   1 #---------------------------------------------------------------------- 
   3 # Purpose:     Import logic and common functions for wxaddons module 
   5 # Author:      Kevin Ollivier 
   9 # Copyright:   (c) 2006 Kevin Ollivier 
  10 # Licence:     wxWindows license 
  11 #---------------------------------------------------------------------- 
  13 import sys
, os
, string
 
  18 # NB: For some reason that I haven't been able to track down, on Mac (at least) 
  19 # calling xmlrpc methods no longer works after the wx.App is started. Therefore,  
  20 # we grab the package URL even before prompting the user if they want to install 
  21 # the package in order for us to have the info we need before the wx.App is started. 
  23 domain 
= 'http://wxaddons.wxcommunity.com' 
  24 builtin_import 
= __builtin__
.__import
__ 
  29 config 
= wx
.Config("wxaddons") 
  30 if config
.Read("PerformChecks", "true") != "true": 
  33 if use_gui 
and not wx
.App
.IsDisplayAvailable(): 
  36 s 
= xmlrpclib
.Server('%s/xmlrpc-server.php' % domain
, verbose
=(debug 
== True))  
  38 def check_imports(check
): 
  40         config
.Write("PerformChecks", "true") 
  42         config
.Write("PerformChecks", "false") 
  44 def version_greater_than_or_equal(version1
, version2
): 
  46     Checks if version1 >= version2, returning true if so, 
  51     for index 
in range(0, len(version1
)-1): 
  52         if version1
[index
] > version2
[index
]: 
  55         elif version
[index
] < current_version
[index
]: 
  61 def prompt_install(name
, version
): 
  62     should_install 
= False 
  63     message 
= "The wxaddon %s is not installed, but was found on the wxaddons site. Would you like to download and install it?" % (name 
+ " " + version
) 
  65         app 
= wx
.PySimpleApp() 
  67         result 
= wx
.MessageBox(message
, "Install Dependency?", style
=wx
.YES_NO
) 
  71         result 
= raw_input(message 
+ " [y/n]") 
  72         if result
[0].lower() == "y": 
  77 def require_addon_version(name
, version
=[], canBeNewer
=True): 
  78     # Check the install receipt to see if we've got an appropriate version  
  79     config 
= wx
.Config("wxaddons-receipts") 
  81     if config
.HasGroup(name
): 
  83         current_version 
= config
.Read("version", "0.0").split(".") 
  85         needs_update 
= version_greater_than_or_equal(version
, current_version
) 
  88         comp_xml 
= s
.getComponent(name
) 
  92         comp 
= xmlrpclib
.loads(comp_xml
)[0][0] 
  93         comp_version 
= comp
["version"].split(".") 
  97             update_comp 
= version_greater_than_or_equal(comp_version
, version
) 
  99             update_comp 
= (version 
== comp_version
) 
 102             url 
= get_url(name
, version
) 
 103             should_install 
= prompt_install(name
, comp_version
) 
 106                 dl_and_install_addon(name
, comp_version
, url
) 
 108 def get_url(name
, version
): 
 110     release_xml 
= s
.getReleases(name
) 
 114     releases 
= xmlrpclib
.loads(release_xml
)[0][0] 
 115     for release 
in releases
: 
 116         if release
['version'] == version
: 
 121 def dl_and_install_addon(name
, version
, url
): 
 127         progress 
= wx
.ProgressDialog("Installing Dependency",  
 128             "Preparing to install the %s addon module." % name
,  
 130             style
=wx
.PD_APP_MODAL|wx
.PD_SMOOTH
) 
 132     message 
= "Downloading tarball..." 
 134     if use_gui
: progress
.Update(1, message
) 
 136     temp_archive
, headers 
= urllib
.urlretrieve(url
) 
 138     message 
= "Extracting files..." 
 140     if use_gui
: progress
.Update(2, message
) 
 142     tempdir 
= tempfile
.mkdtemp() 
 146     tar 
= tarfile
.open(temp_archive
, "r:gz") 
 153     message 
= "Installing %s" % name
 
 154     if use_gui
: progress
.Update(3, message
) 
 155     # TODO: Add support for modified PYTHONPATH? 
 156     # Also, do we need admin install support here? 
 157     retval 
= os
.system(sys
.executable 
+ " " + string
.join(("setup.py", "install"), " ")) 
 158     if use_gui
: progress
.Update(4) 
 161         message 
= "The %s addon was successfully installed." % name
 
 164             wx
.MessageBox(message
, "Installation Successful") 
 169     if use_gui
: progress
.Destroy() 
 172     shutil
.rmtree(tempdir
) 
 173     os
.remove(temp_archive
) 
 177 def import_hook(name
, globals=None, locals=None, fromlist
=None): 
 178     # Fast path: see if the module has already been imported. 
 180         return builtin_import(name
, globals, locals, fromlist
) 
 182         if name
.startswith("wxaddons"): 
 183             print "Querying %s for module." % domain
 
 185                 modname 
= name
.split(".")[1] 
 187                 comp_xml 
= s
.getComponent(modname
) 
 191                 comp 
= xmlrpclib
.loads(comp_xml
)[0][0]                 
 192                 url 
= get_url(comp
["name"], comp
["version"]) 
 193                 should_install 
= prompt_install(comp
["name"], comp
["version"]) 
 197                         success 
= dl_and_install_addon(comp
["name"], comp
["version"], url
) 
 210     import wxaddons
.persistence
 
 211     import wxaddons
.foo_bar
 
 215     __builtin__
.__import
__ = import_hook