diff setup.py @ 235:7a2ef5fe4e8d

distutils: added preinstall_check hack + some tuning while testing
author Goffi <goffi@goffi.org>
date Fri, 07 Jan 2011 03:47:24 +0100
parents 6a37bc1b0b8c
children aae816738314
line wrap: on
line diff
--- a/setup.py	Thu Jan 06 19:17:09 2011 +0100
+++ b/setup.py	Fri Jan 07 03:47:24 2011 +0100
@@ -16,6 +16,16 @@
 class MercurialException(Exception):
     pass
 
+def module_installed(module_name):
+    """Try to import muc from custom wokkel build
+    @param module_name: name of the module to test
+    @return: True if successful"""
+    try:
+        __import__(module_name)
+    except:
+        return False
+    return True
+
 class custom_install(install):
     
     def custom_auto_options(self):
@@ -48,29 +58,22 @@
             mode = ((os.stat(dest_name)[ST_MODE]) | 0555) & 07777
             os.chmod(dest_name, mode)
 
-    def _custom_wokkel_installed(self):
-        """Try to import muc from custom wokkel build
-        @return: True if successful"""
-        try:
-            __import__('wokkel.muc')
-        except:
-            return False
-        return True
     
     def custom_wokkel_requirement(self):
         """Test if the custom wokkel is present, else get and build it"""
-        if not self._custom_wokkel_installed():
-            print ('Custom wokkel is not present, building it')
+        if not module_installed('wokkel.muc'):
+            sys.stdout.write ('Custom wokkel is not present, building it\n')
             import tempfile
             ori_path = os.getcwd()
             work_path = tempfile.mkdtemp()
             os.chdir(work_path)
+            hg_path = subprocess.Popen('which hg', stdout=subprocess.PIPE, shell=True).communicate()[0][:-1]
             #we are now in a temp dir, we can get the repositories
-            commands = ['/usr/bin/hg clone http://hg.ik.nu/wokkel wokkel',
+            commands = ['%s clone http://hg.ik.nu/wokkel wokkel' % hg_path,
                         'cd wokkel',
-                        '/usr/bin/hg pull -f http://hg.ik.nu/ralphm/wokkel-muc-client-support-24-2',
-                        '/usr/bin/hg merge wokkel-muc-client-support-24',
-                        '/usr/bin/hg commit -m "Merged wokkel\'s MUC branch"',
+                        '%s pull -f http://hg.ik.nu/ralphm/wokkel-muc-client-support-24-2' % hg_path,
+                        '%s merge wokkel-muc-client-support-24' % hg_path,
+                        '%s commit -u "SàT installation script" -m "Merged wokkel\'s MUC branch"' % hg_path,
                         '%s setup.py install' % sys.executable]
             for command in commands:
                 if command.startswith('cd '):
@@ -79,22 +82,22 @@
                     ret = subprocess.call(command, stdout=open('/dev/null', 'w'), shell=True)
                     if ret!=0:
                         os.chdir(ori_path)
-                        print ("ERROR while building/installing custom wokkel")
-                        print ('Error happened when executing [%s]' % command)
-                        print ('tmpdir is [%s]' % work_path)
+                        sys.stderr.write ("ERROR while building/installing custom wokkel\n")
+                        sys.stderr.write ('Error happened when executing [%s]\n' % command)
+                        sys.stderr.write ('tmpdir is [%s]\n' % work_path)
                         raise MercurialException
             os.chdir(ori_path)
-            print "Custom wokkel builded and installed, removing temporary files"
+            sys.stdout.write ("Custom wokkel builded and installed, removing temporary files\n")
             import shutil
             shutil.rmtree(work_path)
-            print "done"
+            sys.stdout.write ("done\n")
         else:
-            print "Custom wokkel already installed"
+            sys.stdout.write ("Custom wokkel already installed\n")
 
 
     def run(self):
         install.run(self)
-        print ('running post installation stuff')
+        sys.stdout.write ('running post installation stuff\n')
         self.sh_script_path = os.path.join(self.install_lib,'sat','sat.sh')
         self.primitivus_path = os.path.join(self.install_lib,'sat_frontends','primitivus')
         self.custom_auto_options()
@@ -102,6 +105,56 @@
         self.custom_wokkel_requirement()
 
 
+def preinstall_check():
+    """Check presence of problematic dependencies, and try to install them with package manager
+    This ugly stuff is necessary as distributions are not installed correctly with setuptools/distribute
+    Hope to remove this at some point"""
+
+    #modules_tocheck=['twisted', 'twisted.words', 'twisted.web', 'wx', 'urwid']
+    modules_tocheck=['wx'] #XXX: wx is the only one to really difficult to install
+
+    package = {'twisted':'python-twisted-core',
+               'twisted.words':'python-twisted-words',
+               'twisted.web':'python-twisted-web',
+               'wx':'python-wxgtk2.8',
+               'urwid':'python-urwid',
+               'mercurial':'mercurial'} #this dict map dependencies to packages names for debian distributions
+
+    sys.stdout.write ("Running pre-installation dependencies check\n")
+    
+    #which modules are not installed ?
+    modules_toinstall = filter(lambda mod:not module_installed(mod),modules_tocheck)
+    """#is mercurial available ?
+    hg_installed = subprocess.call('which hg', stdout=open('/dev/null', 'w'), shell=True) == 0
+    if not hg_installed:
+        modules_toinstall.append('mercurial')""" #hg can be installed from pypi
+    
+    if modules_toinstall:
+        #are we on a distribution using apt ?
+        apt_path = subprocess.Popen('which apt-get', stdout=subprocess.PIPE, shell=True).communicate()[0][:-1]
+        not_installed=set()
+        if apt_path:
+            #we have apt, we'll try to use it
+            for module_name in modules_toinstall:
+                package_name = package[module_name]
+                sys.stdout.write ("Installing %s\n" % package_name)
+                success = subprocess.call('%s -qy install %s' % (apt_path,package_name), shell=True) == 0
+                if not success:
+                    not_installed.add(module_name)
+        else:
+            not_installed=set(modules_toinstall)
+
+        if not_installed:
+            #some packages can't be automatically installed, we sys.stdout.write their name for manual installation
+            sys.stdout.write ("You should install the following dependencies with your distribution recommanded tool before installing %s:\n" % NAME)
+            for module_name in not_installed:
+                sys.stdout.write ("- %s (Debian name: %s)\n" % (module_name,package[module_name]))
+            sys.exit(2)
+            
+
+if sys.argv[1].lower() in ['egg_info','install']:
+    #we only check dependencies in egg_info or install is used
+    preinstall_check()
 
 setup(name=NAME,
       version='0.1.0',
@@ -131,7 +184,9 @@
                   ],
       scripts=['frontends/src/jp/jp', 'frontends/src/primitivus/primitivus', 'frontends/src/wix/wix'],
       zip_safe=False,
-      install_requires=['twisted', 'progressbar', 'wxPython', 'urwid', 'BeautifulSoup','Mercurial'],
+      install_requires=['twisted', 'progressbar', 'urwid', 'beautifulsoup', 'mercurial'],
       cmdclass=dict(install=custom_install),
       ) #XXX: The Mercurial dependecy is just here to build the custom wokkel (with MUC branch), it must be removed
       # and replace by wokkel as soon as MUC branch is officially available in wokkel main branch.
+      # wxpython doesn't work, it's managed with preinstall_check
+