virtualenv/res/lib/lib_create_env.py
changeset 68 b40657713336
child 70 8e3281bcebc9
equal deleted inserted replaced
67:30c1f3f3035e 68:b40657713336
       
     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.2.4/tarball/', 'local':"Django-1.2.4.tar.gz"},
       
    19     'JOGGING': {'setup': 'jogging', 'url': 'http://github.com/zain/jogging/tarball/v0.2.2', 'local':"jogging-0.2.2.tar.gz"},
       
    20     'DJANGO-EXTENSIONS': { 'setup': 'django-extensions', 'url':'https://github.com/django-extensions/django-extensions/tarball/0.6', 'local':"django-extensions-0.6.tar.gz"},
       
    21     'DJANGO-REGISTRATION': { 'setup': 'django-registration', 'url':'http://bitbucket.org/ubernostrum/django-registration/get/tip.tar.gz', 'local':"django-registration.tar.gz"},
       
    22     '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"},
       
    23     'DJANGO-PISTON': { 'setup': 'django-piston', 'url':"django-piston-0.2.2-modified.tar.gz", 'local':"django-piston-0.2.2-modified.tar.gz"},
       
    24     'OAUTH2': { 'setup': 'python-oauth2', 'url':"python-oauth2-1.2.1-modified.tar.gz", 'local':"python-oauth2-1.2.1-modified.tar.gz"},
       
    25     'HTTPLIB2': { 'setup': 'python-oauth2', 'url':'http://httplib2.googlecode.com/files/httplib2-0.6.0.tar.gz', 'local':"httplib2-0.6.0.tar.gz"},
       
    26     'DJANGO-OAUTH-PLUS': { 'setup': 'django-oauth-plus', 'url':'http://bitbucket.org/david/django-oauth-plus/get/f314f018e473.gz', 'local':"django-oauth-plus.tar.gz"},
       
    27     '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"},
       
    28     '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"},
       
    29     '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"},
       
    30 #added for blinkster
       
    31     'MODELTRANSLATION': {'setup':'modeltranslation', 'url':'http://code.google.com/p/django-modeltranslation/downloads/detail?name=django-modeltranslation-0.2.tar.gz', 'local':'django-modeltranslation-0.2.tar.gz'},
       
    32     'PHOTOLOGUE': {'setup': 'photologue', 'url':'http://django-photologue.googlecode.com/files/django-photologue-2.2.zip', 'local':'django-photologue-2.2.zip'},
       
    33 }
       
    34 
       
    35 if system_str == 'Windows':
       
    36     URLS.update({
       
    37         '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"},
       
    38         'JCC': {'setup': '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': '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.8-py2.6-win32.egg', 'local':"lxml-2.2.8-py2.6-win32.egg"}
       
    42     })
       
    43 else:
       
    44     URLS.update({
       
    45         '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"},
       
    46         '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"},
       
    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.2.8.tar.gz", 'local':"lxml-2.2.8.tar.gz"}
       
    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, 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         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')
       
   126         if os.path.isfile(patch_dest_path):
       
   127             # must unzip egg
       
   128             # rename file and etract all
       
   129             shutil.move(patch_dest_path, patch_dest_path + ".zip")
       
   130             zf = zipfile.ZipFile(patch_dest_path + ".zip",'r')
       
   131             zf.extractall(patch_dest_path)
       
   132             os.remove(patch_dest_path + ".zip")
       
   133         logger.notify("Patch jcc : %s " % (patch_dest_path))
       
   134         os.chdir(patch_dest_path)
       
   135         p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
       
   136         p.apply()
       
   137         os.chdir(olddir)
       
   138 
       
   139     logger.notify("Install jcc")
       
   140     call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
       
   141                     cwd=jcc_src_path,
       
   142                     filter_stdout=filter_python_develop,
       
   143                     show_stdout=True)
       
   144     #install pylucene
       
   145     
       
   146     logger.notify("Install pylucene")
       
   147     #modify makefile
       
   148     makefile_path = os.path.join(pylucene_src_path,"Makefile")
       
   149     logger.notify("Modify makefile %s " % makefile_path)
       
   150     shutil.move( makefile_path, makefile_path+"~" )
       
   151 
       
   152     destination= open( makefile_path, "w" )
       
   153     source= open( makefile_path+"~", "r" )
       
   154     destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\n")
       
   155     destination.write("ANT=ant\n")
       
   156     destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\n")
       
   157     
       
   158     if system_str == "Darwin":
       
   159         if sys.version_info >= (2,6):
       
   160             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   161         else:
       
   162             destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\n")
       
   163         destination.write("NUM_FILES=2\n")
       
   164     elif system_str == "Windows":
       
   165         destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   166         destination.write("NUM_FILES=2\n")
       
   167     else:
       
   168         if sys.version_info >= (2,6) and sys.version_info <= (2,7):
       
   169             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared\n")
       
   170         else:
       
   171             destination.write("JCC=$(PYTHON) -m jcc --shared\n")
       
   172         destination.write("NUM_FILES=2\n")
       
   173     for line in source:
       
   174         destination.write( line )
       
   175     source.close()
       
   176     destination.close()
       
   177     os.remove(makefile_path+"~" )
       
   178 
       
   179     logger.notify("pylucene make")
       
   180     call_subprocess(['make'],
       
   181                     cwd=os.path.abspath(pylucene_src_path),
       
   182                     filter_stdout=filter_python_develop,
       
   183                     show_stdout=True)
       
   184 
       
   185     logger.notify("pylucene make install")
       
   186     call_subprocess(['make', 'install'],
       
   187                     cwd=os.path.abspath(pylucene_src_path),
       
   188                     filter_stdout=filter_python_develop,
       
   189                     show_stdout=True)
       
   190     
       
   191 
       
   192 def install_psycopg2(option_str, extra_env, res_source_key, home_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop):
       
   193     psycopg2_src = os.path.join(src_dir,"psycopg2.zip")
       
   194     shutil.copy(res_env.URLS['PSYCOPG2'][res_source_key], psycopg2_src)
       
   195     #extract psycopg2
       
   196     zf = zipfile.ZipFile(psycopg2_src)
       
   197     psycopg2_base_path = os.path.join(src_dir,"psycopg2")
       
   198     zf.extractall(psycopg2_base_path)
       
   199     zf.close()
       
   200     
       
   201     psycopg2_src_path = os.path.join(psycopg2_base_path, os.listdir(psycopg2_base_path)[0])
       
   202     shutil.copytree(os.path.join(psycopg2_src_path, 'psycopg2'), os.path.abspath(os.path.join(home_dir, 'Lib', 'psycopg2')))
       
   203     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')))
       
   204 
       
   205 
       
   206 
       
   207 def lib_generate_install_methods(path_locations, src_base, Logger, call_subprocess, normal_installs, urls=None):
       
   208     
       
   209     all_urls = URLS.copy()
       
   210     if urls is not None:
       
   211         all_urls.update(urls)
       
   212         
       
   213     res_env = ResourcesEnv(src_base, all_urls, normal_installs)
       
   214 
       
   215     def filter_python_develop(line):
       
   216         if not line.strip():
       
   217             return Logger.DEBUG
       
   218         for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ',
       
   219                        'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ',
       
   220                        'creating ', 'Copying ']:
       
   221             if line.startswith(prefix):
       
   222                 return Logger.DEBUG
       
   223         return Logger.NOTIFY
       
   224     
       
   225     
       
   226     def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess):
       
   227         logger.notify("Install %s from %s with %s" % (key,res_env.URLS[key][res_source_key],method))
       
   228         if method == 'pip':
       
   229             if sys.platform == 'win32':
       
   230                 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]]
       
   231             else:
       
   232                 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]]
       
   233             if option_str :
       
   234                 args.insert(4,option_str)
       
   235             call_subprocess(args,
       
   236                     cwd=os.path.abspath(tmp_dir),
       
   237                     filter_stdout=filter_python_develop,
       
   238                     show_stdout=True,
       
   239                     extra_env=extra_env)
       
   240         else:
       
   241             if sys.platform == 'win32':
       
   242                 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   243             else:
       
   244                 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   245             if option_str :
       
   246                 args.insert(1,option_str)
       
   247             call_subprocess(args,
       
   248                     cwd=os.path.abspath(tmp_dir),
       
   249                     filter_stdout=filter_python_develop,
       
   250                     show_stdout=True,
       
   251                     extra_env=extra_env)            
       
   252  
       
   253     
       
   254     def after_install(options, home_dir):
       
   255         
       
   256         global logger
       
   257         
       
   258         verbosity = options.verbose - options.quiet
       
   259         logger = Logger([(Logger.level_for_integer(2-verbosity), sys.stdout)])
       
   260 
       
   261         
       
   262         home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
       
   263         base_dir = os.path.dirname(home_dir)
       
   264         src_dir = os.path.join(home_dir, 'src')
       
   265         tmp_dir = os.path.join(home_dir, 'tmp')
       
   266         ensure_dir(src_dir, logger)
       
   267         ensure_dir(tmp_dir, logger)
       
   268         system_str = platform.system()
       
   269         
       
   270         res_source_key = options.type_install
       
   271         
       
   272         ignore_packages = []
       
   273         
       
   274         if options.ignore_packages :
       
   275             ignore_packages = options.ignore_packages.split(",")
       
   276         
       
   277         logger.indent += 2
       
   278         try:    
       
   279             for key, method, option_str, extra_env in res_env.NORMAL_INSTALL:
       
   280                 if key not in ignore_packages:
       
   281                     if callable(method):
       
   282                         method(option_str, extra_env, res_source_key, home_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop)
       
   283                     else:
       
   284                         normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess)
       
   285                             
       
   286             logger.notify("Clear source dir")
       
   287             shutil.rmtree(src_dir)
       
   288     
       
   289         finally:
       
   290             logger.indent -= 2
       
   291         script_dir = join(base_dir, bin_dir)
       
   292         logger.notify('Run "%s Package" to install new packages that provide builds'
       
   293                       % join(script_dir, 'easy_install'))
       
   294     
       
   295 
       
   296     return adjust_options, extend_parser, after_install