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