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