sbin/virtualenv/create_python_env.py
changeset 1 eb9188f2ee4f
child 2 3e2968de0994
equal deleted inserted replaced
0:85b071fb75b2 1:eb9188f2ee4f
       
     1 """
       
     2 Call this like ``python create_python_env.py``; it will
       
     3 refresh the project-boot.py script
       
     4 
       
     5 -prerequisite:
       
     6 
       
     7 - virtualenv
       
     8 - distribute
       
     9 - psycopg2 requires the PostgreSQL libpq libraries and the pg_config utility
       
    10 
       
    11 - python project-boot.py --distribute --no-site-packages --index-url=http://pypi.websushi.org/ --clear --type-install=local <path_to_venv>
       
    12 - For Linux :
       
    13 python project-boot.py --unzip-setuptools --no-site-packages --index-url=http://pypi.websushi.org/ --clear --type-install=local <path_to_venv>
       
    14 
       
    15 """
       
    16 
       
    17 import os
       
    18 import subprocess
       
    19 import re
       
    20 import sys
       
    21 
       
    22 
       
    23 here = os.path.dirname(os.path.abspath(__file__))
       
    24 base_dir = here
       
    25 script_name = os.path.join(base_dir, 'project-boot.py')
       
    26 
       
    27 import virtualenv
       
    28 
       
    29 # things to install
       
    30 # - psycopg2 -> pip
       
    31 # - PIL -> pip
       
    32 # - pyxml -> pip
       
    33 # - 4Suite-xml - easy_install ftp://ftp.4suite.org/pub/4Suite/4Suite-XML-1.0.2.tar.bz2
       
    34 # - pylucene  - script
       
    35 
       
    36 src_base = os.path.join(here,"res","src")
       
    37 lib_path = os.path.abspath(os.path.join(here,"res","lib"))
       
    38 patch_path = os.path.abspath(os.path.join(here,"res","patch"))
       
    39 
       
    40 EXTRA_TEXT  = "URLS = { \n"
       
    41 
       
    42 EXTRA_TEXT += "    'DISTRIBUTE' : { 'setup': 'distribute', 'url': 'http://pypi.python.org/packages/source/d/distribute/distribute-0.6.13.tar.gz', 'local': '"+ os.path.abspath(os.path.join(src_base,"distribute-0.6.13.tar.gz"))+"'},\n"
       
    43 EXTRA_TEXT += "    'PSYCOPG2' : { 'setup': 'psycopg2','url': 'http://initd.org/pub/software/psycopg/psycopg2-2.2.1.tar.gz', 'local': '"+ os.path.abspath(os.path.join(src_base,"psycopg2-2.2.1.tar.gz"))+"'},\n"
       
    44 EXTRA_TEXT += "    'MYSQL' : { 'setup': 'mysql-python', 'url': 'http://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz/download', 'local' : '"+ os.path.abspath(os.path.join(src_base,"MySQL-python-1.2.3c1.tar.gz"))+"'},\n"
       
    45 EXTRA_TEXT += "    'FOURSUITE_XML' : { 'setup': '4Suite-XML', 'url': 'ftp://ftp.4suite.org/pub/4Suite/4Suite-XML-1.0.2.tar.bz2', 'local': '"+ os.path.abspath(os.path.join(src_base,"4Suite-XML-1.0.2.tar.bz2"))+"'},\n"
       
    46 EXTRA_TEXT += "    'PYLUCENE' : { 'setup': 'http://apache.crihan.fr/dist/lucene/pylucene/pylucene-3.0.1-1-src.tar.gz', 'url': 'http://apache.crihan.fr/dist/lucene/pylucene/pylucene-3.0.1-1-src.tar.gz', 'local': '"+ os.path.abspath(os.path.join(src_base,"pylucene-3.0.1-1-src.tar.gz"))+"'},\n"
       
    47 EXTRA_TEXT += "    'PIL' : { 'setup': 'pil', 'url': 'http://effbot.org/downloads/Imaging-1.1.7.tar.gz', 'local': '"+ os.path.abspath(os.path.join(src_base,"Imaging-1.1.7.tar.gz"))+"'},\n"
       
    48 EXTRA_TEXT += "    'PYXML' : { 'setup': 'http://sourceforge.net/projects/pyxml/files/pyxml/0.8.4/PyXML-0.8.4.tar.gz/download', 'url': 'http://sourceforge.net/projects/pyxml/files/pyxml/0.8.4/PyXML-0.8.4.tar.gz/download', 'local': '"+ os.path.abspath(os.path.join(src_base,"PyXML-0.8.4.tar.gz"))+"', 'patch': '"+os.path.join(patch_path,"pyxml.patch")+"'},\n"
       
    49 EXTRA_TEXT += "    'DJANGO' : { 'setup': 'django', 'url': 'http://www.djangoproject.com/download/1.2.3/tarball/', 'local': '"+ os.path.abspath(os.path.join(src_base,"Django-1.2.3.tar.gz"))+"'},\n"
       
    50 EXTRA_TEXT += "    'JOGGING' : { 'setup': 'jogging', 'url': 'http://github.com/zain/jogging/tarball/v0.2.2', 'local': '"+ os.path.abspath(os.path.join(src_base,"jogging-0.2.2.tar.gz"))+"'},\n"
       
    51 EXTRA_TEXT += "    'DJANGO-EXTENSIONS' : { 'setup': 'django-extensions', 'url':'http://django-command-extensions.googlecode.com/files/django-extensions-0.4.1.tar.gz', 'local':'"+ os.path.abspath(os.path.join(src_base,"django-extensions-0.4.1.tar.gz"))+"' },\n"
       
    52 EXTRA_TEXT += "    'DJANGO-REGISTRATION' : { 'setup': 'django-registration', 'url':'http://bitbucket.org/ubernostrum/django-registration/get/tip.tar.gz', 'local':'"+ os.path.abspath(os.path.join(src_base,"django-registration.tar.gz"))+"' },\n"
       
    53 EXTRA_TEXT += "    'DJANGO-TAGGING' : { 'setup': 'django-tagging', 'url':'http://django-tagging.googlecode.com/files/django-tagging-0.3.1.tar.gz', 'local':'"+ os.path.abspath(os.path.join(src_base,"django-tagging-0.3.1.tar.gz"))+"' },\n"
       
    54 EXTRA_TEXT += "    'LXML' : { 'setup': 'lxml', 'url': '"+ os.path.abspath(os.path.join(src_base,"lxml_2.2.7.tar.gz"))+"', 'local': '"+ os.path.abspath(os.path.join(src_base,"lxml-2.2.7.tar.gz"))+"'},\n"
       
    55 EXTRA_TEXT += "}\n"
       
    56 
       
    57 EXTRA_TEXT += "import sys\n"
       
    58 EXTRA_TEXT += "sys.path.append('"+lib_path+"')\n"
       
    59 
       
    60 EXTRA_TEXT += """
       
    61 
       
    62 import shutil
       
    63 import tarfile
       
    64 import urllib
       
    65 import platform
       
    66 import patch
       
    67 
       
    68 
       
    69 INDEX_URL = 'http://pypi.python.org/simple/'
       
    70 
       
    71 
       
    72 def extend_parser(parser):
       
    73     parser.add_option(
       
    74         '--index-url',
       
    75         metavar='INDEX_URL',
       
    76         dest='index_url',
       
    77         default='',
       
    78         help='base URL of Python Package Index')
       
    79     parser.add_option(
       
    80         '--type-install',
       
    81         metavar='type_install',
       
    82         dest='type_install',
       
    83         default='local',
       
    84         help='type install : local, url, setup')
       
    85     parser.add_option(
       
    86         '--ignore-packages',
       
    87         metavar='ignore_packages',
       
    88         dest='ignore_packages',
       
    89         default=None,
       
    90         help='list of comma separated keys for package to ignore')
       
    91 
       
    92 
       
    93 
       
    94 def adjust_options(options, args):
       
    95     pass
       
    96 
       
    97 
       
    98 def after_install(options, home_dir):
       
    99     home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
       
   100     base_dir = os.path.dirname(home_dir)
       
   101     src_dir = join(home_dir, 'src')
       
   102     tmp_dir = join(home_dir, 'tmp')
       
   103     ensure_dir(src_dir)
       
   104     ensure_dir(tmp_dir)
       
   105     system_str = platform.system()
       
   106     
       
   107     res_source_key = options.type_install
       
   108     
       
   109     ignore_packages = []
       
   110     
       
   111     if options.ignore_packages :
       
   112         ignore_packages = options.ignore_packages.split(",")
       
   113     
       
   114     logger.indent += 2
       
   115     try:
       
   116         
       
   117         if 'PYLUCENE' not in ignore_packages:
       
   118             #get pylucene
       
   119             logger.notify("Get Pylucene from %s " % URLS['PYLUCENE'][res_source_key])
       
   120             pylucene_src = os.path.join(src_dir,"pylucene.tar.gz")
       
   121             urllib.urlretrieve(URLS['PYLUCENE'][res_source_key], pylucene_src)
       
   122             tf = tarfile.open(pylucene_src,'r:gz')
       
   123             pylucene_base_path = os.path.join(src_dir,"pylucene") 
       
   124             logger.notify("Extract Pylucene to %s " % pylucene_base_path)
       
   125             tf.extractall(pylucene_base_path)
       
   126             tf.close()
       
   127             
       
   128             pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0])
       
   129             jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc"))
       
   130             
       
   131             #install jcc
       
   132     
       
   133             #patch for linux
       
   134             if system_str == 'Linux' :
       
   135                 olddir = os.getcwd()
       
   136                 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')
       
   137                 if os.path.isfile(patch_dest_path):
       
   138                     # must unzip egg
       
   139                     # rename file and etract all
       
   140                     shutil.move(patch_dest_path, patch_dest_path + ".zip")
       
   141                     zf = zipfile.ZipFile(patch_dest_path + ".zip",'r')
       
   142                     zf.extractall(patch_dest_path)
       
   143                     os.remove(patch_dest_path + ".zip")
       
   144                 logger.notify("Patch jcc : %s " % (patch_dest_path))
       
   145                 os.chdir(patch_dest_path)
       
   146                 p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
       
   147                 p.apply()
       
   148                 os.chdir(olddir)
       
   149     
       
   150             logger.notify("Install jcc")
       
   151             call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
       
   152                             cwd=jcc_src_path,
       
   153                             filter_stdout=filter_python_develop,
       
   154                             show_stdout=True)
       
   155             #install pylucene
       
   156             
       
   157             logger.notify("Install pylucene")
       
   158             #modify makefile
       
   159             makefile_path = os.path.join(pylucene_src_path,"Makefile")
       
   160             logger.notify("Modify makefile %s " % makefile_path)
       
   161             shutil.move( makefile_path, makefile_path+"~" )
       
   162     
       
   163             destination= open( makefile_path, "w" )
       
   164             source= open( makefile_path+"~", "r" )
       
   165             destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\\n")
       
   166             destination.write("ANT=ant\\n")
       
   167             destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\\n")
       
   168             
       
   169             if system_str == "Darwin":
       
   170                 if sys.version_info >= (2,6):
       
   171                     destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n")
       
   172                 else:
       
   173                     destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\\n")
       
   174                 destination.write("NUM_FILES=2\\n")
       
   175             elif system_str == "Windows":
       
   176                 destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n")
       
   177                 destination.write("NUM_FILES=2\\n")
       
   178             else:
       
   179                 if sys.version_info >= (2,6) and sys.version_info < (2,7):
       
   180                     destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared\\n")
       
   181                 else:
       
   182                     destination.write("JCC=$(PYTHON) -m jcc --shared\\n")
       
   183                 destination.write("NUM_FILES=2\\n")
       
   184             for line in source:
       
   185                 destination.write( line )
       
   186             source.close()
       
   187             destination.close()
       
   188             os.remove(makefile_path+"~" )
       
   189     
       
   190             logger.notify("pylucene make")
       
   191             call_subprocess(['make'],
       
   192                             cwd=os.path.abspath(pylucene_src_path),
       
   193                             filter_stdout=filter_python_develop,
       
   194                             show_stdout=True)
       
   195     
       
   196             logger.notify("pylucene make install")
       
   197             call_subprocess(['make', 'install'],
       
   198                             cwd=os.path.abspath(pylucene_src_path),
       
   199                             filter_stdout=filter_python_develop,
       
   200                             show_stdout=True)
       
   201 
       
   202         if system_str == 'Linux'  and 'DISTRIBUTE' not in ignore_packages:
       
   203             normal_install('DISTRIBUTE', 'pip', None, None, res_source_key, home_dir, tmp_dir)
       
   204 
       
   205         if 'PYXML' not in ignore_packages:
       
   206             logger.notify("PyXML install : %s " % URLS['PYXML'][res_source_key])
       
   207             if sys.version_info >= (2,6):
       
   208                 logger.notify("PyXML -> python version >= 2.6 : patching")
       
   209                 pyxml_src = os.path.join(src_dir,"pyxml.tar.gz")
       
   210                 urllib.urlretrieve(URLS['PYXML'][res_source_key], pyxml_src)
       
   211                 logger.notify("PyXML -> python version >= 2.6 : extract archive")
       
   212                 tf = tarfile.open(pyxml_src,'r:gz')
       
   213                 pyxml_base_path = os.path.join(src_dir,"pyxml") 
       
   214                 tf.extractall(pyxml_base_path)
       
   215                 tf.close()
       
   216     
       
   217                 #patch
       
   218                 pyxml_version = os.listdir(pyxml_base_path)[0]            
       
   219                 pyxml_path = os.path.join(pyxml_base_path, pyxml_version)
       
   220                 olddir = os.getcwd()
       
   221                 os.chdir(pyxml_path)
       
   222                 logger.notify("PyXML -> python version >= 2.6 : do patch %s : %s " % (pyxml_path, URLS['PYXML']['patch']))
       
   223                 p = patch.fromfile(URLS['PYXML']['patch'])
       
   224                 p.apply()
       
   225                 os.chdir(olddir)
       
   226                 logger.notify("PyXML -> python version >= 2.6 : install")
       
   227                 call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), '--build='+os.path.abspath(pyxml_base_path), '--no-download', pyxml_version],
       
   228                         cwd=os.path.abspath(tmp_dir),
       
   229                         filter_stdout=filter_python_develop,
       
   230                         show_stdout=True)
       
   231             else:
       
   232                 call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS['PYXML'][res_source_key]],
       
   233                         cwd=os.path.abspath(tmp_dir),
       
   234                         filter_stdout=filter_python_develop,
       
   235                         show_stdout=True)
       
   236         
       
   237         
       
   238         NORMAL_INSTALL = [ #(key,method, option_str, extra_env)
       
   239             #('LXML', 'easy_install', None, {'STATIC_DEPS': 'true'}),
       
   240             ('PSYCOPG2', 'pip', None, None),
       
   241             ('MYSQL', 'pip', None, None),
       
   242             ('PIL', 'pip', None, None), 
       
   243             ('FOURSUITE_XML','easy_install', None, None), 
       
   244             ('DJANGO','pip', None, None),
       
   245             ('JOGGING','pip', None, None),
       
   246             ('DJANGO-EXTENSIONS', 'pip', None, None),
       
   247             ('DJANGO-REGISTRATION', 'easy_install', '-Z', None),
       
   248             ('DJANGO-TAGGING', 'pip', None, None),
       
   249             ]
       
   250             
       
   251         if system_str == "Darwin":
       
   252             NORMAL_INSTALL.append(('LXML', 'easy_install', None, {'STATIC_DEPS': 'true'}))
       
   253         else:
       
   254             NORMAL_INSTALL.append(('LXML', 'easy_install', None, None))
       
   255         
       
   256             
       
   257         for key, method, option_str, extra_env in NORMAL_INSTALL:
       
   258             if key not in ignore_packages:
       
   259                 normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir)
       
   260                         
       
   261         logger.notify("Clear source dir")
       
   262         shutil.rmtree(src_dir)
       
   263 
       
   264     finally:
       
   265         logger.indent -= 2
       
   266     script_dir = join(base_dir, 'bin')
       
   267     logger.notify('Run "%s Package" to install new packages that provide builds'
       
   268                   % join(script_dir, 'easy_install'))
       
   269 
       
   270 
       
   271 def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir):
       
   272     logger.notify("Install %s from %s with %s" % (key,URLS[key][res_source_key],method))
       
   273     if method == 'pip':
       
   274         args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS[key][res_source_key]]
       
   275         if option_str :
       
   276             args.insert(4,option_str)
       
   277         call_subprocess(args,
       
   278                 cwd=os.path.abspath(tmp_dir),
       
   279                 filter_stdout=filter_python_develop,
       
   280                 show_stdout=True,
       
   281                 extra_env=extra_env)
       
   282     else:
       
   283         args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), URLS[key][res_source_key]]
       
   284         if option_str :
       
   285             args.insert(1,option_str)
       
   286         call_subprocess(args,
       
   287                 cwd=os.path.abspath(tmp_dir),
       
   288                 filter_stdout=filter_python_develop,
       
   289                 show_stdout=True,
       
   290                 extra_env=extra_env)
       
   291     
       
   292 
       
   293 def ensure_dir(dir):
       
   294     if not os.path.exists(dir):
       
   295         logger.notify('Creating directory %s' % dir)
       
   296         os.makedirs(dir)
       
   297 
       
   298 def filter_python_develop(line):
       
   299     if not line.strip():
       
   300         return Logger.DEBUG
       
   301     for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ',
       
   302                    'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ',
       
   303                    'creating ', 'Copying ']:
       
   304         if line.startswith(prefix):
       
   305             return Logger.DEBUG
       
   306     return Logger.NOTIFY
       
   307 """
       
   308 
       
   309 def main():
       
   310     python_version = ".".join(map(str,sys.version_info[0:2]))
       
   311     text = virtualenv.create_bootstrap_script(EXTRA_TEXT, python_version=python_version)
       
   312     if os.path.exists(script_name):
       
   313         f = open(script_name)
       
   314         cur_text = f.read()
       
   315         f.close()
       
   316     else:
       
   317         cur_text = ''
       
   318     print 'Updating %s' % script_name
       
   319     if cur_text == 'text':
       
   320         print 'No update'
       
   321     else:
       
   322         print 'Script changed; updating...'
       
   323         f = open(script_name, 'w')
       
   324         f.write(text)
       
   325         f.close()
       
   326 
       
   327 if __name__ == '__main__':
       
   328     main()
       
   329