Changeset 5755

Show
Ignore:
Timestamp:
11/23/08 15:13:35 (2 months ago)
Author:
chrisz
Message:

Updated ez_setup and adjusted tgsetup accordingly. Also made tgsetup print a meaningful error message if setuptools cannot be downloaded or installed automatically.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0/tools/tgsetup.py

    r5753 r5755  
    2929# this is the default version of setuptools, not TG 
    3030DEFAULT_VERSION = "0.6c9" 
    31 DEFAULT_URL     = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3] 
     31DEFAULT_URL     = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] 
    3232 
    3333md5_data = { 
     
    5959    'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', 
    6060    'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', 
     61    'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', 
    6162    'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', 
    6263    'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', 
     
    6869 
    6970import sys, os 
     71try: from hashlib import md5 
     72except ImportError: from md5 import md5 
    7073 
    7174# TGBEGIN 
    7275win32 = """ 
    73 The recommended Python for TurboGears is Python 2.5.2. You can download 
    74 it here: 
     76The recommended Python for TurboGears 1.0 is Python 2.5.2. 
     77You can download it here: 
    7578 
    7679http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi 
     
    7881 
    7982darwin = """ 
    80 The recommended Python for TurboGears is Python 2.5.2. You can download 
    81 a Universal build here: 
     83The recommended Python for TurboGears 1.0 is Python 2.5.2. 
     84You can download a Universal build here: 
    8285 
    8386http://www.python.org/ftp/python/2.5.2/python-2.5.2-macosx.dmg 
     
    8588 
    8689other = """ 
    87 The recommended Python for TurboGears is Python 2.5.2. This version of 
    88 Python is often available in your operating system's native package 
    89 format (via apt-get or yum, for instance). You can also easily build 
    90 Python from source on Unix-like systems. Here is the source download 
    91 link for Python: 
     90The recommended Python for TurboGears 1.0 is Python 2.5.2. 
     91This version of Python is often available in your operating system's 
     92native package format (via apt-get or yum, for instance). 
     93You can also easily build Python from source on Unix-like systems. 
     94Here is the source download link for Python: 
    9295 
    9396http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tar.bz2 
     
    139142 
    140143def check_python(): 
    141     if sys.version_info < (2,3): 
    142         print "Python 2.3 or higher is required to run TurboGears 1.0.x.
     144    if sys.version_info < (2, 3): 
     145        print "Python 2.3 or higher is required to run TurboGears 1.0.
    143146 
    144147        if not get_python(): 
    145148            sys.exit(1) 
    146149 
    147     elif sys.version_info < (2,4): 
    148         print "TurboGears 1.0.x supports Python 2.3, but 2.4 is strongly recommended." 
     150    elif sys.version_info < (2, 4): 
     151        print "TurboGears 1.0 supports Python 2.3, but 2.4 is strongly recommended." 
    149152 
    150153        if not get_python(): 
     
    154157                print "You can rerun the installer once you have an updated Python." 
    155158                sys.exit(1) 
     159 
     160    elif sys.version_info >= (2, 6): 
     161        print "Python 2.6 or higher is not supported by TurboGears 1.0." 
     162 
     163        if not get_python(): 
     164            sys.exit(1) 
    156165 
    157166def check_directory(): 
     
    193202    print "Beginning setuptools/EasyInstall installation and TurboGears " \ 
    194203          "download\n" 
    195  
    196204    if len(sys.argv) > 1: 
    197205        args = sys.argv[1:] 
     
    268276def _validate_md5(egg_name, data): 
    269277    if egg_name in md5_data: 
    270         from md5 import md5 
    271278        digest = md5(data).hexdigest() 
    272279        if digest != md5_data[egg_name]: 
    273280            print >>sys.stderr, ( 
    274                 "md5 validation of %s failed! (Possible download problem?)" 
     281                "md5 validation of %s failed! (Possible download problem?)" 
    275282                % egg_name 
    276283            ) 
    277284            sys.exit(2) 
    278285    return data 
    279  
    280286 
    281287def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 
     
    292298    an attempt to abort the calling script. 
    293299    """ 
    294     try: 
    295         import setuptools 
    296         if setuptools.__version__ == '0.0.1': 
    297             print >>sys.stderr, ( 
    298             "You have an obsolete version of setuptools installed.  Please\n" 
    299             "remove it from your system entirely before rerunning this script." 
    300             ) 
    301             sys.exit(2) 
    302     except ImportError: 
     300    was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules 
     301    def do_download(): 
    303302        egg = download_setuptools(version, download_base, to_dir, download_delay) 
    304303        sys.path.insert(0, egg) 
    305304        import setuptools; setuptools.bootstrap_install_from = egg 
    306  
    307     import pkg_resources 
    308305    try: 
    309         pkg_resources.require("setuptools>="+version) 
    310  
     306        import pkg_resources 
     307    except ImportError: 
     308        return do_download() 
     309    try: 
     310        pkg_resources.require("setuptools>="+version); return 
    311311    except pkg_resources.VersionConflict, e: 
    312         # XXX could we install in a subprocess here? 
    313         print >>sys.stderr, ( 
     312        if was_imported: 
     313            print >>sys.stderr, ( 
    314314            "The required version of setuptools (>=%s) is not available, and\n" 
    315315            "can't be installed while this script is running. Please install\n" 
    316             " a more recent version first.\n\n(Currently using %r)" 
    317         ) % (version, e.args[0]) 
    318         sys.exit(2) 
    319  
    320 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 
    321     to_dir=os.curdir, delay = 15): 
     316            " a more recent version first, using 'easy_install -U setuptools'." 
     317            "\n\n(Currently using %r)" 
     318            ) % (version, e.args[0]) 
     319            sys.exit(2) 
     320        else: 
     321            del pkg_resources, sys.modules['pkg_resources']    # reload ok 
     322            return do_download() 
     323    except pkg_resources.DistributionNotFound: 
     324        return do_download() 
     325 
     326def download_setuptools( 
     327    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, 
     328    delay = 15 
     329): 
    322330    """Download setuptools from a specified location and return its filename 
    323331 
     
    365373def main(argv, version=DEFAULT_VERSION): 
    366374    """Install or upgrade setuptools and EasyInstall""" 
    367  
     375    #TGBEGIN 
    368376    try: 
    369         import setuptools 
    370  
    371     except ImportError: 
    372         egg = None 
    373377        try: 
    374             egg = download_setuptools(version, delay=0) 
    375             sys.path.insert(0,egg) 
     378            import setuptools 
     379        except ImportError: 
     380            egg = None 
     381            try: 
     382                egg = download_setuptools(version, delay=0) 
     383                sys.path.insert(0,egg) 
     384                from setuptools.command.easy_install import main 
     385                return main(list(argv)+[egg])   # we're done here 
     386            finally: 
     387                if egg and os.path.exists(egg): 
     388                    os.unlink(egg) 
     389    except Exception, e: 
     390        print >>sys.stderr, ( 
     391        "Could not download and install setupools:\n%s\n" 
     392        "Please try installing setuptools manually." 
     393        % (str(e) or e.__class__.__name__)) 
     394        sys.exit(2) 
     395    if setuptools.__version__ == '0.0.1': 
     396        print >>sys.stderr, ( 
     397        "You have an obsolete version of setuptools installed.  Please\n" 
     398        "remove it from your system entirely before rerunning this script." 
     399        ) 
     400        sys.exit(2) 
     401    #TGEND 
     402 
     403    req = "setuptools>=" + version 
     404    import pkg_resources 
     405    try: 
     406        pkg_resources.require(req) 
     407    except pkg_resources.VersionConflict: 
     408        try: 
    376409            from setuptools.command.easy_install import main 
    377410            #TGBEGIN 
    378411            install_command() 
    379412            #TGEND 
    380             return main(list(argv)+[egg])   # we're done here 
    381         finally: 
    382             if egg and os.path.exists(egg): 
    383                 os.unlink(egg) 
    384     else: 
    385         if setuptools.__version__ == '0.0.1': 
    386             # tell the user to uninstall obsolete version 
    387             use_setuptools(version) 
    388  
    389     req = "setuptools>=" + version 
    390     import pkg_resources 
    391     try: 
    392         pkg_resources.require(req) 
    393  
    394     except pkg_resources.VersionConflict: 
    395         try: 
    396             from setuptools.command.easy_install import main 
    397413        except ImportError: 
    398414            from easy_install import main 
    399         #TGBEGIN 
    400         install_command() 
    401         #TGEND 
    402415        main(list(argv)+[download_setuptools(delay=0)]) 
    403416        sys.exit(0) # try to force an exit 
    404417    else: 
    405         from setuptools.command.easy_install import main 
    406         #TGBEGIN 
    407         install_command() 
    408         #TGEND 
    409         main(argv) 
    410  
     418        if argv: 
     419            from setuptools.command.easy_install import main 
     420            #TGBEGIN 
     421            install_command() 
     422            #TGEND 
     423            main(argv) 
     424        else: 
     425            print "Setuptools version",version,"or greater has been installed." 
     426            print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' 
    411427 
    412428def update_md5(filenames): 
     
    414430 
    415431    import re 
    416     from md5 import md5 
    417432 
    418433    for name in filenames: 
     
    440455    f.close() 
    441456 
    442  
    443457if __name__=='__main__': 
    444458    if len(sys.argv) > 2 and sys.argv[1] == '--md5update': 
    445459        update_md5(sys.argv[2:]) 
    446  
    447460    else: 
     461        #TGBEGIN 
    448462        tg_main() 
     463        #TGEND 
  • branches/1.1/tools/tgsetup.py

    r5754 r5755  
    44from distutils import log 
    55 
     6# this is the TG version 
    67TGVERSION = "1.1b1" 
    78TGDOWNLOAD = "http://www.turbogears.org/download/" 
     
    2324""" 
    2425import sys 
     26# this is the default version of setuptools, not TG 
    2527DEFAULT_VERSION = "0.6c9" 
    26 DEFAULT_URL     = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3] 
     28DEFAULT_URL     = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] 
    2729 
    2830md5_data = { 
     
    5456    'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', 
    5557    'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', 
     58    'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', 
    5659    'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', 
    5760    'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', 
     
    6366 
    6467import sys, os 
     68try: from hashlib import md5 
     69except ImportError: from md5 import md5 
    6570 
    6671# TGBEGIN 
    6772win32 = """ 
    68 The recommended Python for TurboGears is Python 2.5.2. You can download 
    69 it here: 
     73The recommended Python for TurboGears 1.1 is Python 2.5.2. 
     74You can download it here: 
    7075 
    7176http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi 
     
    7378 
    7479darwin = """ 
    75 The recommended Python for TurboGears is Python 2.5.2. You can download 
    76 a Universal build here: 
     80The recommended Python for TurboGears 1.1 is Python 2.5.2. 
     81You can download a Universal build here: 
    7782 
    7883http://www.python.org/ftp/python/2.5.2/python-2.5.2-macosx.dmg 
     
    8085 
    8186other = """ 
    82 The recommended Python for TurboGears is Python 2.5.2. This version of 
    83 Python is often available in your operating system's native package 
    84 format (via apt-get or yum, for instance). You can also easily build 
    85 Python from source on Unix-like systems. Here is the source download 
    86 link for Python: 
     87The recommended Python for TurboGears 1.1 is Python 2.5.2. 
     88This version of Python is often available in your operating system's 
     89native package format (via apt-get or yum, for instance). 
     90You can also easily build Python from source on Unix-like systems. 
     91Here is the source download link for Python: 
    8792 
    8893http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tar.bz2 
     
    120125 
    121126def check_python(): 
    122     if sys.version_info < (2,4): 
    123         print "Python 2.4 or higher is required to run TurboGears >= 1.1." 
     127    if sys.version_info < (2, 4): 
     128        print "Python 2.4 or higher is required to run TurboGears 1.1." 
    124129 
    125130        if not get_python(): 
     
    163168    print "Beginning setuptools/EasyInstall installation and TurboGears " \ 
    164169          "download\n" 
    165  
    166170    if len(sys.argv) > 1: 
    167171        args = sys.argv[1:] 
     
    199203        def finalize_options(self): 
    200204            if self.future: 
     205                #self.args.append("TurboGears[future,testtools] >= %s" % TGVERSION) 
    201206                self.args.append("TurboGears[future] == %s" % TGVERSION) 
    202207            else: 
    203208                self.args.append("TurboGears == %s" % TGVERSION) 
    204  
    205209            if self.upgrade is None: 
    206210                self.upgrade = True 
     
    238242def _validate_md5(egg_name, data): 
    239243    if egg_name in md5_data: 
    240         from md5 import md5 
    241244        digest = md5(data).hexdigest() 
    242245        if digest != md5_data[egg_name]: 
    243246            print >>sys.stderr, ( 
    244                 "md5 validation of %s failed! (Possible download problem?)" 
     247                "md5 validation of %s failed! (Possible download problem?)" 
    245248                % egg_name 
    246249            ) 
    247250            sys.exit(2) 
    248251    return data 
    249  
    250252 
    251253def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 
     
    262264    an attempt to abort the calling script. 
    263265    """ 
    264     try: 
    265         import setuptools 
    266         if setuptools.__version__ == '0.0.1': 
    267             print >>sys.stderr, ( 
    268             "You have an obsolete version of setuptools installed.  Please\n" 
    269             "remove it from your system entirely before rerunning this script." 
    270             ) 
    271             sys.exit(2) 
    272     except ImportError: 
     266    was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules 
     267    def do_download(): 
    273268        egg = download_setuptools(version, download_base, to_dir, download_delay) 
    274269        sys.path.insert(0, egg) 
    275270        import setuptools; setuptools.bootstrap_install_from = egg 
    276  
    277     import pkg_resources 
    278271    try: 
    279         pkg_resources.require("setuptools>="+version) 
    280  
     272        import pkg_resources 
     273    except ImportError: 
     274        return do_download() 
     275    try: 
     276        pkg_resources.require("setuptools>="+version); return 
    281277    except pkg_resources.VersionConflict, e: 
    282         # XXX could we install in a subprocess here? 
    283         print >>sys.stderr, ( 
     278        if was_imported: 
     279            print >>sys.stderr, ( 
    284280            "The required version of setuptools (>=%s) is not available, and\n" 
    285281            "can't be installed while this script is running. Please install\n" 
    286             " a more recent version first.\n\n(Currently using %r)" 
    287         ) % (version, e.args[0]) 
    288         sys.exit(2) 
    289  
    290 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 
    291     to_dir=os.curdir, delay = 15): 
     282            " a more recent version first, using 'easy_install -U setuptools'." 
     283            "\n\n(Currently using %r)" 
     284            ) % (version, e.args[0]) 
     285            sys.exit(2) 
     286        else: 
     287            del pkg_resources, sys.modules['pkg_resources']    # reload ok 
     288            return do_download() 
     289    except pkg_resources.DistributionNotFound: 
     290        return do_download() 
     291 
     292def download_setuptools( 
     293    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, 
     294    delay = 15 
     295): 
    292296    """Download setuptools from a specified location and return its filename 
    293297 
     
    335339def main(argv, version=DEFAULT_VERSION): 
    336340    """Install or upgrade setuptools and EasyInstall""" 
    337  
     341    #TGBEGIN 
    338342    try: 
    339         import setuptools 
    340  
    341     except ImportError: 
    342         egg = None 
    343343        try: 
    344             egg = download_setuptools(version, delay=0) 
    345             sys.path.insert(0,egg) 
     344            import setuptools 
     345        except ImportError: 
     346            egg = None 
     347            try: 
     348                egg = download_setuptools(version, delay=0) 
     349                sys.path.insert(0,egg) 
     350                from setuptools.command.easy_install import main 
     351                return main(list(argv)+[egg])   # we're done here 
     352            finally: 
     353                if egg and os.path.exists(egg): 
     354                    os.unlink(egg) 
     355    except Exception, e: 
     356        print >>sys.stderr, ( 
     357        "Could not download and install setupools:\n%s\n" 
     358        "Please try installing setuptools manually." 
     359        % (str(e) or e.__class__.__name__)) 
     360        sys.exit(2) 
     361    if setuptools.__version__ == '0.0.1': 
     362        print >>sys.stderr, ( 
     363        "You have an obsolete version of setuptools installed.  Please\n" 
     364        "remove it from your system entirely before rerunning this script." 
     365        ) 
     366        sys.exit(2) 
     367    #TGEND 
     368 
     369    req = "setuptools>=" + version 
     370    import pkg_resources 
     371    try: 
     372        pkg_resources.require(req) 
     373    except pkg_resources.VersionConflict: 
     374        try: 
    346375            from setuptools.command.easy_install import main 
    347376            #TGBEGIN 
    348377            install_command() 
    349378            #TGEND 
    350             return main(list(argv)+[egg])   # we're done here 
    351         finally: 
    352             if egg and os.path.exists(egg): 
    353                 os.unlink(egg) 
    354     else: 
    355         if setuptools.__version__ == '0.0.1': 
    356             # tell the user to uninstall obsolete version 
    357             use_setuptools(version) 
    358  
    359     req = "setuptools>=" + version 
    360     import pkg_resources 
    361     try: 
    362         pkg_resources.require(req) 
    363  
    364     except pkg_resources.VersionConflict: 
    365         try: 
    366             from setuptools.command.easy_install import main 
    367379        except ImportError: 
    368380            from easy_install import main 
    369         #TGBEGIN 
    370         install_command() 
    371         #TGEND 
    372381        main(list(argv)+[download_setuptools(delay=0)]) 
    373382        sys.exit(0) # try to force an exit 
    374383    else: 
    375         from setuptools.command.easy_install import main 
    376         #TGBEGIN 
    377         install_command() 
    378         #TGEND 
    379         main(argv) 
    380  
     384        if argv: 
     385            from setuptools.command.easy_install import main 
     386            #TGBEGIN 
     387            install_command() 
     388            #TGEND 
     389            main(argv) 
     390        else: 
     391            print "Setuptools version",version,"or greater has been installed." 
     392            print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' 
    381393 
    382394def update_md5(filenames): 
     
    384396 
    385397    import re 
    386     from md5 import md5 
    387398 
    388399    for name in filenames: 
     
    410421    f.close() 
    411422 
    412  
    413423if __name__=='__main__': 
    414424    if len(sys.argv) > 2 and sys.argv[1] == '--md5update': 
    415425        update_md5(sys.argv[2:]) 
    416  
    417426    else: 
     427        #TGBEGIN 
    418428        tg_main() 
     429        #TGEND 
  • branches/1.5/tools/tgsetup.py

    r5482 r5755  
    44from distutils import log 
    55 
    6 TGVERSION = "1.1b1" 
     6# this is the TG version 
     7TGVERSION = "1.5a1" 
    78TGDOWNLOAD = "http://www.turbogears.org/download/" 
    89 
     
    2324""" 
    2425import sys 
    25 DEFAULT_VERSION = "0.6c8" 
    26 DEFAULT_URL     = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3] 
     26# this is the default version of setuptools, not TG 
     27DEFAULT_VERSION = "0.6c9" 
     28DEFAULT_URL     = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] 
    2729 
    2830md5_data = { 
     
    5456    'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', 
    5557    'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', 
     58    'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', 
    5659    'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', 
    5760    'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', 
     61    'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03', 
     62    'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a', 
     63    'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6', 
     64    'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a', 
    5865} 
    5966 
    6067import sys, os 
     68try: from hashlib import md5 
     69except ImportError: from md5 import md5 
    6170 
    6271# TGBEGIN 
    6372win32 = """ 
    64 The recommended Python for TurboGears is Python 2.5.2. You can download 
    65 it here: 
     73The recommended Python for TurboGears 1.5 is Python 2.5.2. 
     74You can download it here: 
    6675 
    6776http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi 
     
    6978 
    7079darwin = """ 
    71 The recommended Python for TurboGears is Python 2.5.2. You can download 
    72 a Universal build here: 
     80The recommended Python for TurboGears 1.5 is Python 2.5.2. 
     81You can download a Universal build here: 
    7382 
    7483http://www.python.org/ftp/python/2.5.2/python-2.5.2-macosx.dmg 
     
    7685 
    7786other = """ 
    78 The recommended Python for TurboGears is Python 2.5.2. This version of 
    79 Python is often available in your operating system's native package 
    80 format (via apt-get or yum, for instance). You can also easily build 
    81 Python from source on Unix-like systems. Here is the source download 
    82 link for Python: 
     87The recommended Python for TurboGears 1.5 is Python 2.5.2. 
     88This version of Python is often available in your operating system's 
     89native package format (via apt-get or yum, for instance). 
     90You can also easily build Python from source on Unix-like systems. 
     91Here is the source download link for Python: 
    8392 
    8493http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tar.bz2 
     
    116125 
    117126def check_python(): 
    118     if sys.version_info < (2,4): 
    119         print "Python 2.4 or higher is required to run TurboGears >= 1.1." 
     127    if sys.version_info < (2, 4): 
     128        print "Python 2.4 or higher is required to run TurboGears 1.5." 
    120129 
    121130        if not get_python(): 
     
    159168    print "Beginning setuptools/EasyInstall installation and TurboGears " \ 
    160169          "download\n" 
    161  
    162170    if len(sys.argv) > 1: 
    163171        args = sys.argv[1:] 
     
    195203        def finalize_options(self): 
    196204            if self.future: 
     205                #self.args.append("TurboGears[future,testtools] >= %s" % TGVERSION) 
    197206                self.args.append("TurboGears[future] == %s" % TGVERSION) 
    198207            else: 
    199208                self.args.append("TurboGears == %s" % TGVERSION) 
    200  
    201209            if self.upgrade is None: 
    202210                self.upgrade = True 
     
    234242def _validate_md5(egg_name, data): 
    235243    if egg_name in md5_data: 
    236         from md5 import md5 
    237244        digest = md5(data).hexdigest() 
    238245        if digest != md5_data[egg_name]: 
    239246            print >>sys.stderr, ( 
    240                 "md5 validation of %s failed! (Possible download problem?)" 
     247                "md5 validation of %s failed! (Possible download problem?)" 
    241248                % egg_name 
    242249            ) 
    243250            sys.exit(2) 
    244251    return data 
    245  
    246252 
    247253def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 
     
    258264    an attempt to abort the calling script. 
    259265    """ 
    260     try: 
    261         import setuptools 
    262         if setuptools.__version__ == '0.0.1': 
    263             print >>sys.stderr, ( 
    264             "You have an obsolete version of setuptools installed.  Please\n" 
    265             "remove it from your system entirely before rerunning this script." 
    266             ) 
    267             sys.exit(2) 
    268     except ImportError: 
     266    was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules 
     267    def do_download(): 
    269268        egg = download_setuptools(version, download_base, to_dir, download_delay) 
    270269        sys.path.insert(0, egg) 
    271270        import setuptools; setuptools.bootstrap_install_from = egg 
    272  
    273     import pkg_resources 
    274271    try: 
    275         pkg_resources.require("setuptools>="+version) 
    276  
     272        import pkg_resources 
     273    except ImportError: 
     274        return do_download() 
     275    try: 
     276        pkg_resources.require("setuptools>="+version); return 
    277277    except pkg_resources.VersionConflict, e: 
    278         # XXX could we install in a subprocess here? 
    279         print >>sys.stderr, ( 
     278        if was_imported: 
     279            print >>sys.stderr, ( 
    280280            "The required version of setuptools (>=%s) is not available, and\n" 
    281281            "can't be installed while this script is running. Please install\n" 
    282             " a more recent version first.\n\n(Currently using %r)" 
    283         ) % (version, e.args[0]) 
    284         sys.exit(2) 
    285  
    286 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 
    287     to_dir=os.curdir, delay = 15): 
     282            " a more recent version first, using 'easy_install -U setuptools'." 
     283            "\n\n(Currently using %r)" 
     284            ) % (version, e.args[0]) 
     285            sys.exit(2) 
     286        else: 
     287            del pkg_resources, sys.modules['pkg_resources']    # reload ok 
     288            return do_download() 
     289    except pkg_resources.DistributionNotFound: 
     290        return do_download() 
     291 
     292def download_setuptools( 
     293    version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, 
     294    delay = 15 
     295): 
    288296    """Download setuptools from a specified location and return its filename 
    289297 
     
    331339def main(argv, version=DEFAULT_VERSION): 
    332340    """Install or upgrade setuptools and EasyInstall""" 
    333  
     341    #TGBEGIN 
    334342    try: 
    335         import setuptools 
    336  
    337     except ImportError: 
    338         egg = None 
    339343        try: 
    340             egg = download_setuptools(version, delay=0) 
    341             sys.path.insert(0,egg) 
     344            import setuptools 
     345        except ImportError: 
     346            egg = None 
     347            try: 
     348                egg = download_setuptools(version, delay=0) 
     349                sys.path.insert(0,egg) 
     350                from setuptools.command.easy_install import main 
     351                return main(list(argv)+[egg])   # we're done here 
     352            finally: 
     353                if egg and os.path.exists(egg): 
     354                    os.unlink(egg) 
     355    except Exception, e: 
     356        print >>sys.stderr, ( 
     357        "Could not download and install setupools:\n%s\n" 
     358        "Please try installing setuptools manually." 
     359        % (str(e) or e.__class__.__name__)) 
     360        sys.exit(2) 
     361    if setuptools.__version__ == '0.0.1': 
     362        print >>sys.stderr, ( 
     363        "You have an obsolete version of setuptools installed.  Please\n" 
     364        "remove it from your system entirely before rerunning this script." 
     365        ) 
     366        sys.exit(2) 
     367    #TGEND 
     368 
     369    req = "setuptools>=" + version 
     370    import pkg_resources 
     371    try: 
     372        pkg_resources.require(req) 
     373    except pkg_resources.VersionConflict: 
     374        try: 
    342375            from setuptools.command.easy_install import main 
    343376            #TGBEGIN 
    344377            install_command() 
    345378            #TGEND 
    346             return main(list(argv)+[egg])   # we're done here 
    347         finally: 
    348             if egg and os.path.exists(egg): 
    349                 os.unlink(egg) 
    350     else: 
    351         if setuptools.__version__ == '0.0.1': 
    352             # tell the user to uninstall obsolete version 
    353             use_setuptools(version) 
    354  
    355     req = "setuptools>=" + version 
    356     import pkg_resources 
    357     try: 
    358         pkg_resources.require(req) 
    359  
    360     except pkg_resources.VersionConflict: 
    361         try: 
    362             from setuptools.command.easy_install import main 
    363379        except ImportError: 
    364380            from easy_install import main 
    365         #TGBEGIN 
    366         install_command() 
    367         #TGEND 
    368381        main(list(argv)+[download_setuptools(delay=0)]) 
    369382        sys.exit(0) # try to force an exit 
    370383    else: 
    371         from setuptools.command.easy_install import main 
    372         #TGBEGIN 
    373         install_command() 
    374         #TGEND 
    375         main(argv) 
    376  
     384        if argv: 
     385            from setuptools.command.easy_install import main 
     386            #TGBEGIN 
     387            install_command() 
     388            #TGEND 
     389            main(argv) 
     390        else: 
     391            print "Setuptools version",version,"or greater has been installed." 
     392            print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' 
    377393 
    378394def update_md5(filenames): 
     
    380396 
    381397    import re 
    382     from md5 import md5 
    383398 
    384399    for name in filenames: 
     
    406421    f.close() 
    407422 
    408  
    409423if __name__=='__main__': 
    410424    if len(sys.argv) > 2 and sys.argv[1] == '--md5update': 
    411425        update_md5(sys.argv[2:]) 
    412  
    413426    else: 
     427        #TGBEGIN 
    414428        tg_main() 
     429        #TGEND