virtualenv/res/lib/lib_create_env.py
changeset 1 40d6b5e3dcd7
child 4 238f3cd543f2
equal deleted inserted replaced
0:368be7c741c4 1:40d6b5e3dcd7
       
     1 import sys
       
     2 import os
       
     3 import os.path
       
     4 import shutil
       
     5 import tarfile
       
     6 import zipfile
       
     7 import urllib
       
     8 import platform
       
     9 import patch
       
    10 
       
    11 join = os.path.join
       
    12 system_str = platform.system()
       
    13 
       
    14 
       
    15 URLS = {
       
    16     #'': {'setup': '', 'url':'', 'local':''},
       
    17     'DISTRIBUTE': {'setup': 'distribute', 'url':'http://pypi.python.org/packages/source/d/distribute/distribute-0.6.14.tar.gz', 'local':"distribute-0.6.14.tar.gz"},
       
    18     'DJANGO': {'setup': 'django', 'url': 'http://www.djangoproject.com/download/1.3/tarball/', 'local':"Django-1.3.tar.gz"},
       
    19     'DJANGO-EXTENSIONS': { 'setup': 'django-extensions', 'url':'https://github.com/django-extensions/django-extensions/tarball/0.6', 'local':"django-extensions-0.6.tar.gz"},
       
    20     'DJANGO-REGISTRATION': { 'setup': 'django-registration', 'url':'http://bitbucket.org/ubernostrum/django-registration/get/tip.tar.gz', 'local':"django-registration.tar.gz"},
       
    21     'DJANGO-TAGGING': { 'setup': 'django-tagging', 'url':'http://django-tagging.googlecode.com/files/django-tagging-0.3.1.tar.gz', 'local':"django-tagging-0.3.1.tar.gz"},
       
    22     'DJANGO-PISTON': { 'setup': 'django-piston', 'url':"django-piston-0.2.2-modified.tar.gz", 'local':"django-piston-0.2.2-modified.tar.gz"},
       
    23     'OAUTH2': { 'setup': 'python-oauth2', 'url':"http://pypi.python.org/packages/source/o/oauth2/oauth2-1.5.168.tar.gz", 'local':"oauth2-1.5.167.tar.gz"},
       
    24     'HTTPLIB2': { 'setup': 'python-httplib2', 'url':'http://httplib2.googlecode.com/files/httplib2-0.6.0.tar.gz', 'local':"httplib2-0.6.0.tar.gz"},
       
    25     'DJANGO-OAUTH-PLUS': { 'setup': 'django-oauth-plus', 'url':'http://bitbucket.org/david/django-oauth-plus/get/f314f018e473.gz', 'local':"django-oauth-plus.tar.gz"},
       
    26     'MYSQL': { 'setup': 'mysql-python', 'url': 'http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download', 'local':"MySQL-python-1.2.3.tar.gz"},
       
    27     'SETUPTOOLS-HG': { 'setup':'mercurial_hg', 'url':'http://pypi.python.org/packages/source/s/setuptools_hg/setuptools_hg-0.2.tar.gz', 'local':"setuptools_hg-0.2.tar.gz"},
       
    28     'MERCURIAL': {'setup':'mercurial', 'url':'http://pypi.python.org/packages/source/d/mercurial/mercurial-1.7.5.tar.gz', 'local':"mercurial-1.7.5.tar.gz"},
       
    29     'OPENID': {'setup':'openid', 'url':'http://pypi.python.org/packages/source/p/python-openid/python-openid-2.2.5.tar.gz', 'local':"python-openid-2.2.5.tar.gz"},
       
    30     'DJANGO_OPENID_CONSUMER': {'setup':'django_openid_consumer', 'url':'http://pypi.python.org/packages/source/d/django-openid-consumer/django-openid-consumer-0.1.1.tar.gz', 'local':"django-openid-consumer-0.1.1.tar.gz"},
       
    31     'SOCIAL_AUTH': {'setup':'social_auth', 'url':'https://github.com/omab/django-social-auth/tarball/v0.3.10', 'local':"omab-django-social-auth-v0.3.10-modified.tar.gz"},
       
    32     'SOUTH': { 'setup': 'South', 'url':'http://www.aeracode.org/releases/south/south-0.7.3.tar.gz', 'local':"south-0.7.3.tar.gz"},
       
    33 }
       
    34 
       
    35 if system_str == 'Windows':
       
    36     URLS.update({
       
    37         'PSYCOPG2': {'setup': 'psycopg2','url': 'psycopg2-2.0.14.win32-py2.6-pg8.4.3-release.zip', 'local':"psycopg2-2.0.14.win32-py2.6-pg8.4.3-release.zip"},
       
    38         'JCC': {'setup': 'jcc', 'url': 'http://pylucene-win32-binary.googlecode.com/files/JCC-2.6-py2.6-win32.egg', 'local':"JCC-2.6-py2.6-win32.egg"},
       
    39         'PYLUCENE': {'setup': 'pylucene', 'url': 'http://pylucene-win32-binary.googlecode.com/files/lucene-3.0.2-py2.6-win32.egg', 'local':"lucene-3.0.2-py2.6-win32.egg"},
       
    40         'PIL': {'setup': 'pil', 'url': 'http://effbot.org/media/downloads/PIL-1.1.7.win32-py2.6.exe', 'local':"PIL-1.1.7.win32-py2.6.exe"},
       
    41         'LXML': {'setup': 'lxml', 'url': 'http://pypi.python.org/packages/2.6/l/lxml/lxml-2.2.2-py2.6-win32.egg', 'local':"lxml-2.2.2-py2.6-win32.egg"}
       
    42     })
       
    43 else:
       
    44     URLS.update({
       
    45         'PSYCOPG2': {'setup': 'psycopg2','url': 'http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.tar.gz', 'local':"psycopg2-2.4.tar.gz"},
       
    46         'PYLUCENE': {'setup': 'pylucene', 'url': 'http://apache.crihan.fr/dist//lucene/pylucene/pylucene-3.1.0-1-src.tar.gz', 'local':"pylucene-3.1.0-1-src.tar.gz"},
       
    47         'PIL': {'setup': 'pil', 'url': 'http://effbot.org/downloads/Imaging-1.1.7.tar.gz', 'local':"Imaging-1.1.7.tar.gz"},
       
    48         'LXML': {'setup': 'lxml', 'url':"lxml-2.3.tar.bz2", 'local':"lxml-2.3.tar.bz2"}
       
    49     })
       
    50 
       
    51 
       
    52 
       
    53 class ResourcesEnv(object):
       
    54 
       
    55     def __init__(self, src_base, urls, normal_installs):
       
    56         self.src_base = src_base
       
    57         self.URLS = {}
       
    58         self.__init_url(urls)
       
    59         self.NORMAL_INSTALL = normal_installs
       
    60 
       
    61     def get_src_base_path(self, fpath):
       
    62         return os.path.abspath(os.path.join(self.src_base, fpath)).replace("\\","/")
       
    63     
       
    64     def __add_package_def(self, key, setup, url, local):
       
    65         self.URLS[key] = {'setup':setup, 'url':url, 'local':self.get_src_base_path(local)}
       
    66 
       
    67     def __init_url(self, urls):
       
    68         for key, url_dict in urls.items():
       
    69             url = url_dict['url']
       
    70             if not url.startswith("http://"):
       
    71                 url = self.get_src_base_path(url)
       
    72             self.__add_package_def(key, url_dict["setup"], url, url_dict["local"])
       
    73 
       
    74 def ensure_dir(dir, logger):
       
    75     if not os.path.exists(dir):
       
    76         logger.notify('Creating directory %s' % dir)
       
    77         os.makedirs(dir)
       
    78 
       
    79 def extend_parser(parser):    
       
    80     parser.add_option(
       
    81         '--index-url',
       
    82         metavar='INDEX_URL',
       
    83         dest='index_url',
       
    84         default='http://pypi.python.org/simple/',
       
    85         help='base URL of Python Package Index')
       
    86     parser.add_option(
       
    87         '--type-install',
       
    88         metavar='type_install',
       
    89         dest='type_install',
       
    90         default='local',
       
    91         help='type install : local, url, setup')
       
    92     parser.add_option(
       
    93         '--ignore-packages',
       
    94         metavar='ignore_packages',
       
    95         dest='ignore_packages',
       
    96         default=None,
       
    97         help='list of comma separated keys for package to ignore')
       
    98 
       
    99 def adjust_options(options, args):
       
   100     pass
       
   101 
       
   102 
       
   103 def install_pylucene(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop):
       
   104     
       
   105     logger.notify("Get Pylucene from %s " % res_env.URLS['PYLUCENE'][res_source_key])
       
   106     pylucene_src = os.path.join(src_dir,"pylucene.tar.gz")
       
   107     if res_source_key == 'local':
       
   108         shutil.copy(res_env.URLS['PYLUCENE'][res_source_key], pylucene_src)
       
   109     else:
       
   110         urllib.urlretrieve(res_env.URLS['PYLUCENE'][res_source_key], pylucene_src)
       
   111     tf = tarfile.open(pylucene_src,'r:gz')
       
   112     pylucene_base_path = os.path.join(src_dir,"pylucene") 
       
   113     logger.notify("Extract Pylucene to %s " % pylucene_base_path)
       
   114     tf.extractall(pylucene_base_path)
       
   115     tf.close()
       
   116     
       
   117     pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0])
       
   118     jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc"))
       
   119     
       
   120     #install jcc
       
   121 
       
   122     #patch for linux
       
   123     if system_str == 'Linux' :
       
   124         olddir = os.getcwd()
       
   125         setuptools_path = os.path.join(lib_dir, 'site-packages', 'setuptools')
       
   126         if os.path.exists(setuptools_path) and os.path.isdir(setuptools_path):
       
   127             patch_dest_path = os.path.join(lib_dir, 'site-packages')
       
   128         else:
       
   129             patch_dest_path = os.path.join(lib_dir,'site-packages','setuptools-0.6c11-py%s.%s.egg' % (sys.version_info[0], sys.version_info[1]))
       
   130             if os.path.isfile(patch_dest_path):
       
   131                 # must unzip egg
       
   132                 # rename file and etract all
       
   133                 shutil.move(patch_dest_path, patch_dest_path + ".zip")
       
   134                 zf = zipfile.ZipFile(patch_dest_path + ".zip",'r')
       
   135                 zf.extractall(patch_dest_path)
       
   136                 os.remove(patch_dest_path + ".zip")
       
   137         logger.notify("Patch jcc : %s " % (patch_dest_path))
       
   138         os.chdir(patch_dest_path)
       
   139         p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
       
   140         p.apply()
       
   141         os.chdir(olddir)
       
   142 
       
   143     logger.notify("Install jcc")
       
   144     call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
       
   145                     cwd=jcc_src_path,
       
   146                     filter_stdout=filter_python_develop,
       
   147                     show_stdout=True)
       
   148     #install pylucene
       
   149     
       
   150     logger.notify("Install pylucene")
       
   151     #modify makefile
       
   152     makefile_path = os.path.join(pylucene_src_path,"Makefile")
       
   153     logger.notify("Modify makefile %s " % makefile_path)
       
   154     shutil.move( makefile_path, makefile_path+"~" )
       
   155 
       
   156     destination= open( makefile_path, "w" )
       
   157     source= open( makefile_path+"~", "r" )
       
   158     destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\n")
       
   159     destination.write("ANT=ant\n")
       
   160     destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\n")
       
   161     
       
   162     if system_str == "Darwin":
       
   163         if sys.version_info >= (2,6):
       
   164             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   165         else:
       
   166             destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\n")
       
   167         destination.write("NUM_FILES=2\n")
       
   168     elif system_str == "Windows":
       
   169         destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   170         destination.write("NUM_FILES=2\n")
       
   171     else:
       
   172         if sys.version_info >= (2,6) and sys.version_info <= (2,7):
       
   173             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared\n")
       
   174         else:
       
   175             destination.write("JCC=$(PYTHON) -m jcc --shared\n")
       
   176         destination.write("NUM_FILES=2\n")
       
   177     for line in source:
       
   178         destination.write( line )
       
   179     source.close()
       
   180     destination.close()
       
   181     os.remove(makefile_path+"~" )
       
   182 
       
   183     logger.notify("pylucene make")
       
   184     call_subprocess(['make'],
       
   185                     cwd=os.path.abspath(pylucene_src_path),
       
   186                     filter_stdout=filter_python_develop,
       
   187                     show_stdout=True)
       
   188 
       
   189     logger.notify("pylucene make install")
       
   190     call_subprocess(['make', 'install'],
       
   191                     cwd=os.path.abspath(pylucene_src_path),
       
   192                     filter_stdout=filter_python_develop,
       
   193                     show_stdout=True)
       
   194     
       
   195 
       
   196 def install_psycopg2(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop):
       
   197     psycopg2_src = os.path.join(src_dir,"psycopg2.zip")
       
   198     shutil.copy(res_env.URLS['PSYCOPG2'][res_source_key], psycopg2_src)
       
   199     #extract psycopg2
       
   200     zf = zipfile.ZipFile(psycopg2_src)
       
   201     psycopg2_base_path = os.path.join(src_dir,"psycopg2")
       
   202     zf.extractall(psycopg2_base_path)
       
   203     zf.close()
       
   204     
       
   205     psycopg2_src_path = os.path.join(psycopg2_base_path, os.listdir(psycopg2_base_path)[0])
       
   206     shutil.copytree(os.path.join(psycopg2_src_path, 'psycopg2'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'psycopg2')))
       
   207     shutil.copy(os.path.join(psycopg2_src_path, 'psycopg2-2.0.14-py2.6.egg-info'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'site-packages')))
       
   208 
       
   209 
       
   210 
       
   211 def lib_generate_install_methods(path_locations, src_base, Logger, call_subprocess, normal_installs, urls=None):
       
   212     
       
   213     all_urls = URLS.copy()
       
   214     if urls is not None:
       
   215         all_urls.update(urls)
       
   216         
       
   217     res_env = ResourcesEnv(src_base, all_urls, normal_installs)
       
   218 
       
   219     def filter_python_develop(line):
       
   220         if not line.strip():
       
   221             return Logger.DEBUG
       
   222         for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ',
       
   223                        'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ',
       
   224                        'creating ', 'Copying ']:
       
   225             if line.startswith(prefix):
       
   226                 return Logger.DEBUG
       
   227         return Logger.NOTIFY
       
   228     
       
   229     
       
   230     def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess):
       
   231         logger.notify("Install %s from %s with %s" % (key,res_env.URLS[key][res_source_key],method))
       
   232         if method == 'pip':
       
   233             if sys.platform == 'win32':
       
   234                 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', '-E', os.path.abspath(home_dir), res_env.URLS[key][res_source_key]]
       
   235             else:
       
   236                 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), res_env.URLS[key][res_source_key]]
       
   237             if option_str :
       
   238                 args.insert(4,option_str)
       
   239             call_subprocess(args,
       
   240                     cwd=os.path.abspath(tmp_dir),
       
   241                     filter_stdout=filter_python_develop,
       
   242                     show_stdout=True,
       
   243                     extra_env=extra_env)
       
   244         else:
       
   245             if sys.platform == 'win32':
       
   246                 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   247             else:
       
   248                 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   249             if option_str :
       
   250                 args.insert(1,option_str)
       
   251             call_subprocess(args,
       
   252                     cwd=os.path.abspath(tmp_dir),
       
   253                     filter_stdout=filter_python_develop,
       
   254                     show_stdout=True,
       
   255                     extra_env=extra_env)            
       
   256  
       
   257     
       
   258     def after_install(options, home_dir):
       
   259         
       
   260         global logger
       
   261         
       
   262         verbosity = options.verbose - options.quiet
       
   263         logger = Logger([(Logger.level_for_integer(2-verbosity), sys.stdout)])
       
   264 
       
   265         
       
   266         home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
       
   267         base_dir = os.path.dirname(home_dir)
       
   268         src_dir = os.path.join(home_dir, 'src')
       
   269         tmp_dir = os.path.join(home_dir, 'tmp')
       
   270         ensure_dir(src_dir, logger)
       
   271         ensure_dir(tmp_dir, logger)
       
   272         system_str = platform.system()
       
   273         
       
   274         res_source_key = options.type_install
       
   275         
       
   276         ignore_packages = []
       
   277         
       
   278         if options.ignore_packages :
       
   279             ignore_packages = options.ignore_packages.split(",")
       
   280         
       
   281         logger.indent += 2
       
   282         try:    
       
   283             for key, method, option_str, extra_env in res_env.NORMAL_INSTALL:
       
   284                 if key not in ignore_packages:
       
   285                     if callable(method):
       
   286                         method(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop)
       
   287                     else:
       
   288                         normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess)
       
   289                             
       
   290             logger.notify("Clear source dir")
       
   291             shutil.rmtree(src_dir)
       
   292     
       
   293         finally:
       
   294             logger.indent -= 2
       
   295         script_dir = join(base_dir, bin_dir)
       
   296         logger.notify('Run "%s Package" to install new packages that provide builds'
       
   297                       % join(script_dir, 'easy_install'))
       
   298     
       
   299 
       
   300     return adjust_options, extend_parser, after_install