virtualenv/res/lib/lib_create_env.py
changeset 16 e37a29d23c86
child 23 249fe0bbc817
equal deleted inserted replaced
5:9c9db6355381 16:e37a29d23c86
       
     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.4/tarball/', 'local':"Django-1.4.tar.gz"},
       
    19     'DJANGO-EXTENSIONS': { 'setup': 'django-extensions', 'url':'https://github.com/django-extensions/django-extensions/tarball/0.8', 'local':"django-extensions-0.8.tar.gz"},
       
    20     'DJANGO-REGISTRATION': { 'setup': 'django-registration', 'url':'https://bitbucket.org/ubernostrum/django-registration/get/v0.8.tar.gz', 'local':"django-registration-0.8.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':"https://bitbucket.org/jespern/django-piston/get/default.tar.gz", 'local':"django-piston.tar.gz"},
       
    23     'OAUTH2': { 'setup': 'python-oauth2', 'url':"https://github.com/simplegeo/python-oauth2/tarball/hudson-python-oauth2-211", 'local':"oauth2-1.5.211.tar.gz"},
       
    24     'HTTPLIB2': { 'setup': 'python-httplib2', 'url':'http://code.google.com/p/httplib2/downloads/detail?name=httplib2-0.7.4.tar.gz&can=2&q=', 'local':"httplib2-0.7.4.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     '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"},
       
    28     '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"},
       
    29     '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"},
       
    30     'SOUTH': { 'setup': 'South', 'url':'http://www.aeracode.org/releases/south/south-0.7.5.tar.gz', 'local':"south-0.7.5.tar.gz"},
       
    31     'DJANGO_GUARDIAN' : { 'setup': 'django-guardian', 'url':'http://pypi.python.org/packages/source/d/django-guardian/django-guardian-1.0.3.tar.gz', 'local':"django-guardian-1.0.3.tar.gz"},
       
    32     'SORL_THUMBNAIL' : { 'setup': 'sorl-thumbnail', 'url':'http://pypi.python.org/packages/source/s/sorl-thumbnail/sorl-thumbnail-11.12.tar.gz', 'local':"sorl-thumbnail-v10.12.1.tar.gz"},
       
    33     'LIBJPEG': {'setup': None, 'url':'jpegsrc.v8d.tar.gz', 'local':'jpegsrc.v8d.tar.gz'},
       
    34     'ZLIB': {'setup': None, 'url':'zlib-1.2.7.tar.gz', 'local':'zlib-1.2.7.tar.gz'},
       
    35     'PYCRYPTO': {'setup': 'pycrypto', 'url':'https://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz', 'local':'pycrypto-2.6.tar.gz'},
       
    36     'SSH': {'setup': 'ssh', 'url':'http://pypi.python.org/packages/source/s/ssh/ssh-1.7.14.tar.gz#md5=4cdd0549ef4699bd67b96264d3b21427', 'local':'ssh-1.7.14.tar.gz'},
       
    37     'FABRIC': {'setup': 'fabric', 'url':'https://github.com/fabric/fabric/tarball/1.4.2', 'local':'fabric-1.4.2.tar.gz'},
       
    38     'MERCURIAL': {'setup': 'mercurial', 'url':'http://mercurial.selenic.com/release/mercurial-2.2.2.tar.gz', 'local':'mercurial-2.2.2.tar.gz'},
       
    39 }
       
    40 
       
    41 if system_str == 'Windows':
       
    42     URLS.update({
       
    43         '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"},
       
    44         '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"},
       
    45         '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"},
       
    46         '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"},
       
    47         '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"}
       
    48     })
       
    49 else:
       
    50     URLS.update({
       
    51         'PSYCOPG2': {'setup': 'psycopg2','url': 'http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.5.tar.gz', 'local':"psycopg2-2.4.5.tar.gz"},
       
    52         'PYLUCENE': {'setup': 'pylucene', 'url': 'http://mirrors.ircam.fr/pub/apache/lucene/pylucene/pylucene-3.6.0-2-src.tar.gz', 'local':"pylucene-3.6.0-2-src.tar.gz"},
       
    53         'PIL': {'setup': 'pil', 'url': 'http://effbot.org/downloads/Imaging-1.1.7.tar.gz', 'local':"Imaging-1.1.7.tar.gz"},
       
    54         'LXML': {'setup': 'lxml', 'url':"lxml-2.3.4.tar.bz2", 'local':"lxml-2.3.4.tar.bz2"}
       
    55     })
       
    56 
       
    57 
       
    58 
       
    59 class ResourcesEnv(object):
       
    60 
       
    61     def __init__(self, src_base, urls, normal_installs):
       
    62         self.src_base = src_base
       
    63         self.URLS = {}
       
    64         self.__init_url(urls)
       
    65         self.NORMAL_INSTALL = normal_installs
       
    66 
       
    67     def get_src_base_path(self, fpath):
       
    68         return os.path.abspath(os.path.join(self.src_base, fpath)).replace("\\","/")
       
    69     
       
    70     def __add_package_def(self, key, setup, url, local):
       
    71         self.URLS[key] = {'setup':setup, 'url':url, 'local':self.get_src_base_path(local)}
       
    72 
       
    73     def __init_url(self, urls):
       
    74         for key, url_dict in urls.items():
       
    75             url = url_dict['url']
       
    76             if not url.startswith("http://"):
       
    77                 url = self.get_src_base_path(url)
       
    78             self.__add_package_def(key, url_dict["setup"], url, url_dict["local"])
       
    79 
       
    80 def ensure_dir(dir, logger):
       
    81     if not os.path.exists(dir):
       
    82         logger.notify('Creating directory %s' % dir)
       
    83         os.makedirs(dir)
       
    84 
       
    85 def extend_parser(parser):    
       
    86     parser.add_option(
       
    87         '--index-url',
       
    88         metavar='INDEX_URL',
       
    89         dest='index_url',
       
    90         default='http://pypi.python.org/simple/',
       
    91         help='base URL of Python Package Index')
       
    92     parser.add_option(
       
    93         '--type-install',
       
    94         metavar='type_install',
       
    95         dest='type_install',
       
    96         default='local',
       
    97         help='type install : local, url, setup')
       
    98     parser.add_option(
       
    99         '--ignore-packages',
       
   100         metavar='ignore_packages',
       
   101         dest='ignore_packages',
       
   102         default=None,
       
   103         help='list of comma separated keys for package to ignore')
       
   104 
       
   105 def adjust_options(options, args):
       
   106     pass
       
   107 
       
   108 
       
   109 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):
       
   110     
       
   111     logger.notify("Get Pylucene from %s " % res_env.URLS['PYLUCENE'][res_source_key])
       
   112     pylucene_src = os.path.join(src_dir,"pylucene.tar.gz")
       
   113     if res_source_key == 'local':
       
   114         shutil.copy(res_env.URLS['PYLUCENE'][res_source_key], pylucene_src)
       
   115     else:
       
   116         urllib.urlretrieve(res_env.URLS['PYLUCENE'][res_source_key], pylucene_src)
       
   117     tf = tarfile.open(pylucene_src,'r:gz')
       
   118     pylucene_base_path = os.path.join(src_dir,"pylucene") 
       
   119     logger.notify("Extract Pylucene to %s " % pylucene_base_path)
       
   120     tf.extractall(pylucene_base_path)
       
   121     tf.close()
       
   122     
       
   123     pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0])
       
   124     jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc"))
       
   125     
       
   126     #install jcc
       
   127 
       
   128     #patch for linux
       
   129     if system_str == 'Linux' :
       
   130         olddir = os.getcwd()
       
   131         setuptools_path = os.path.join(lib_dir, 'site-packages', 'setuptools')
       
   132         if os.path.exists(setuptools_path) and os.path.isdir(setuptools_path):
       
   133             patch_dest_path = os.path.join(lib_dir, 'site-packages')
       
   134         else:
       
   135             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]))
       
   136             if os.path.isfile(patch_dest_path):
       
   137                 # must unzip egg
       
   138                 # rename file and etract all
       
   139                 shutil.move(patch_dest_path, patch_dest_path + ".zip")
       
   140                 zf = zipfile.ZipFile(patch_dest_path + ".zip",'r')
       
   141                 zf.extractall(patch_dest_path)
       
   142                 os.remove(patch_dest_path + ".zip")
       
   143         logger.notify("Patch jcc : %s " % (patch_dest_path))
       
   144         os.chdir(patch_dest_path)
       
   145         p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
       
   146         p.apply()
       
   147         os.chdir(olddir)
       
   148 
       
   149     logger.notify("Install jcc")
       
   150     call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
       
   151                     cwd=jcc_src_path,
       
   152                     filter_stdout=filter_python_develop,
       
   153                     show_stdout=True)
       
   154     #install pylucene
       
   155     
       
   156     logger.notify("Install pylucene")
       
   157     #modify makefile
       
   158     makefile_path = os.path.join(pylucene_src_path,"Makefile")
       
   159     logger.notify("Modify makefile %s " % makefile_path)
       
   160     shutil.move( makefile_path, makefile_path+"~" )
       
   161 
       
   162     destination= open( makefile_path, "w" )
       
   163     source= open( makefile_path+"~", "r" )
       
   164     destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\n")
       
   165     destination.write("ANT=ant\n")
       
   166     destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\n")
       
   167     
       
   168     if system_str == "Darwin":
       
   169         if sys.version_info >= (2,6):
       
   170             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   171         else:
       
   172             destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\n")
       
   173         destination.write("NUM_FILES=2\n")
       
   174     elif system_str == "Windows":
       
   175         destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   176         destination.write("NUM_FILES=2\n")
       
   177     else:
       
   178         if sys.version_info >= (2,6) and sys.version_info <= (2,7):
       
   179             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared\n")
       
   180         else:
       
   181             destination.write("JCC=$(PYTHON) -m jcc --shared\n")
       
   182         destination.write("NUM_FILES=2\n")
       
   183     for line in source:
       
   184         destination.write( line )
       
   185     source.close()
       
   186     destination.close()
       
   187     os.remove(makefile_path+"~" )
       
   188 
       
   189     logger.notify("pylucene make")
       
   190     call_subprocess(['make'],
       
   191                     cwd=os.path.abspath(pylucene_src_path),
       
   192                     filter_stdout=filter_python_develop,
       
   193                     show_stdout=True)
       
   194 
       
   195     logger.notify("pylucene make install")
       
   196     call_subprocess(['make', 'install'],
       
   197                     cwd=os.path.abspath(pylucene_src_path),
       
   198                     filter_stdout=filter_python_develop,
       
   199                     show_stdout=True)
       
   200     
       
   201 
       
   202 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):
       
   203     psycopg2_src = os.path.join(src_dir,"psycopg2.zip")
       
   204     shutil.copy(res_env.URLS['PSYCOPG2'][res_source_key], psycopg2_src)
       
   205     #extract psycopg2
       
   206     zf = zipfile.ZipFile(psycopg2_src)
       
   207     psycopg2_base_path = os.path.join(src_dir,"psycopg2")
       
   208     zf.extractall(psycopg2_base_path)
       
   209     zf.close()
       
   210     
       
   211     psycopg2_src_path = os.path.join(psycopg2_base_path, os.listdir(psycopg2_base_path)[0])
       
   212     shutil.copytree(os.path.join(psycopg2_src_path, 'psycopg2'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'psycopg2')))
       
   213     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')))
       
   214 
       
   215 def gen_install_comp_lib(lib_name, lib_key, configure_options=[]):
       
   216     
       
   217     def install_lib(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop):
       
   218         lib_src = os.path.join(src_dir,lib_name+".tar.gz")
       
   219         shutil.copy(res_env.URLS[lib_key][res_source_key], lib_src)
       
   220         tf = tarfile.open(lib_src,'r:gz')
       
   221         lib_base_path = os.path.join(src_dir, lib_name) 
       
   222         logger.notify("Extract %s to %s " % (lib_name,lib_base_path))
       
   223         tf.extractall(lib_base_path)
       
   224         tf.close()
       
   225         
       
   226         lib_src_path = os.path.join(lib_base_path, os.listdir(lib_base_path)[0])
       
   227     
       
   228         logger.notify("libjpeg configure")
       
   229         call_subprocess(['configure', '--prefix='+os.path.abspath(home_dir)] + configure_options,
       
   230                         cwd=os.path.abspath(lib_src_path),
       
   231                         filter_stdout=filter_python_develop,
       
   232                         show_stdout=True)
       
   233         
       
   234         logger.notify("libjpeg make")
       
   235         call_subprocess(['make'],
       
   236                         cwd=os.path.abspath(lib_src_path),
       
   237                         filter_stdout=filter_python_develop,
       
   238                         show_stdout=True)
       
   239     
       
   240         logger.notify("zlib make install")
       
   241         call_subprocess(['make', 'install'],
       
   242                         cwd=os.path.abspath(lib_src_path),
       
   243                         filter_stdout=filter_python_develop,
       
   244                         show_stdout=True)
       
   245         return install_lib
       
   246 
       
   247 install_libjpeg = gen_install_comp_lib("libjpeg", "LIBJPEG", ['--enable-shared'])
       
   248 install_zlib = gen_install_comp_lib("zlib", "ZLIB", [])
       
   249 
       
   250 
       
   251     
       
   252     
       
   253 
       
   254 def lib_generate_install_methods(path_locations, src_base, Logger, call_subprocess, normal_installs, urls=None):
       
   255     
       
   256     all_urls = URLS.copy()
       
   257     if urls is not None:
       
   258         all_urls.update(urls)
       
   259         
       
   260     res_env = ResourcesEnv(src_base, all_urls, normal_installs)
       
   261 
       
   262     def filter_python_develop(line):
       
   263         if not line.strip():
       
   264             return Logger.DEBUG
       
   265         for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ',
       
   266                        'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ',
       
   267                        'creating ', 'Copying ']:
       
   268             if line.startswith(prefix):
       
   269                 return Logger.DEBUG
       
   270         return Logger.NOTIFY
       
   271     
       
   272     
       
   273     def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess):
       
   274         logger.notify("Install %s from %s with %s" % (key,res_env.URLS[key][res_source_key],method))
       
   275         if method == 'pip':
       
   276             if sys.platform == 'win32':
       
   277                 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', res_env.URLS[key][res_source_key]]
       
   278             else:
       
   279                 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', res_env.URLS[key][res_source_key]]
       
   280             if option_str :
       
   281                 args.insert(4,option_str)
       
   282             call_subprocess(args,
       
   283                     cwd=os.path.abspath(tmp_dir),
       
   284                     filter_stdout=filter_python_develop,
       
   285                     show_stdout=True,
       
   286                     extra_env=extra_env)
       
   287         else:
       
   288             if sys.platform == 'win32':
       
   289                 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   290             else:
       
   291                 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   292             if option_str :
       
   293                 args.insert(1,option_str)
       
   294             call_subprocess(args,
       
   295                     cwd=os.path.abspath(tmp_dir),
       
   296                     filter_stdout=filter_python_develop,
       
   297                     show_stdout=True,
       
   298                     extra_env=extra_env)            
       
   299  
       
   300     
       
   301     def after_install(options, home_dir):
       
   302         
       
   303         global logger
       
   304         
       
   305         verbosity = options.verbose - options.quiet
       
   306         logger = Logger([(Logger.level_for_integer(2-verbosity), sys.stdout)])
       
   307 
       
   308         
       
   309         home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
       
   310         base_dir = os.path.dirname(home_dir)
       
   311         src_dir = os.path.join(home_dir, 'src')
       
   312         tmp_dir = os.path.join(home_dir, 'tmp')
       
   313         ensure_dir(src_dir, logger)
       
   314         ensure_dir(tmp_dir, logger)
       
   315         system_str = platform.system()
       
   316         
       
   317         res_source_key = options.type_install
       
   318         
       
   319         ignore_packages = []
       
   320         
       
   321         if options.ignore_packages :
       
   322             ignore_packages = options.ignore_packages.split(",")
       
   323         
       
   324         logger.indent += 2
       
   325         try:    
       
   326             for key, method, option_str, extra_env in res_env.NORMAL_INSTALL:
       
   327                 if key not in ignore_packages:
       
   328                     if callable(method):
       
   329                         method(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop)
       
   330                     else:
       
   331                         normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess)
       
   332                             
       
   333             logger.notify("Clear source dir")
       
   334             shutil.rmtree(src_dir)
       
   335     
       
   336         finally:
       
   337             logger.indent -= 2
       
   338         script_dir = join(base_dir, bin_dir)
       
   339         logger.notify('Run "%s Package" to install new packages that provide builds'
       
   340                       % join(script_dir, 'easy_install'))
       
   341     
       
   342 
       
   343     return adjust_options, extend_parser, after_install