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