sbin/create_python_env.py
changeset 74 c18c0211348b
parent 73 acba3bceebf3
child 75 cd12d9d72ca2
equal deleted inserted replaced
73:acba3bceebf3 74:c18c0211348b
     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 - virtualenv --distribute --no-site-packages venv
       
    12 - python project-boot.py --distribute --no-site-packages --index-url=http://pypi.websushi.org/ --clear bvenv
       
    13 
       
    14 """
       
    15 
       
    16 import os
       
    17 import subprocess
       
    18 import re
       
    19 import sys
       
    20 
       
    21 
       
    22 here = os.path.dirname(os.path.abspath(__file__))
       
    23 base_dir = here
       
    24 script_name = os.path.join(base_dir, 'project-boot.py')
       
    25 
       
    26 import virtualenv
       
    27 
       
    28 # things to install
       
    29 # - psycopg2 -> pip
       
    30 # - PIL -> pip
       
    31 # - pyxml -> pip
       
    32 # - 4Suite-xml - easy_install ftp://ftp.4suite.org/pub/4Suite/4Suite-XML-1.0.2.tar.bz2
       
    33 # - pylucene  - script
       
    34 
       
    35 src_base = os.path.join(here,"res","src")
       
    36 lib_path = os.path.abspath(os.path.join(here,"res","lib"))
       
    37 patch_path = os.path.abspath(os.path.join(here,"res","patch"))
       
    38 
       
    39 EXTRA_TEXT  = "URLS = { \n"
       
    40 
       
    41 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"
       
    42 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"
       
    43 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"
       
    44 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"
       
    45 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"
       
    46 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"
       
    47 
       
    48 EXTRA_TEXT += "}\n"
       
    49 
       
    50 EXTRA_TEXT += "import sys\n"
       
    51 EXTRA_TEXT += "sys.path.append('"+lib_path+"')\n"
       
    52 
       
    53 EXTRA_TEXT += """
       
    54 
       
    55 import shutil
       
    56 import tarfile
       
    57 import urllib
       
    58 import platform
       
    59 import patch
       
    60 import zipfile
       
    61 from distutils.sysconfig import get_python_lib
       
    62 
       
    63 
       
    64 INDEX_URL = 'http://pypi.python.org/simple/'
       
    65 
       
    66 
       
    67 def extend_parser(parser):
       
    68     parser.add_option(
       
    69         '--index-url',
       
    70         metavar='INDEX_URL',
       
    71         dest='index_url',
       
    72         default='',
       
    73         help='base URL of Python Package Index')
       
    74     parser.add_option(
       
    75         '--type-install',
       
    76         metavar='type_install',
       
    77         dest='type_install',
       
    78         default='local',
       
    79         help='type install : local, url, setup')
       
    80 
       
    81 
       
    82 def adjust_options(options, args):
       
    83     pass
       
    84 
       
    85 
       
    86 def after_install(options, home_dir):
       
    87     home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
       
    88     base_dir = os.path.dirname(home_dir)
       
    89     src_dir = join(home_dir, 'src')
       
    90     tmp_dir = join(home_dir, 'tmp')
       
    91     ensure_dir(src_dir)
       
    92     ensure_dir(tmp_dir)
       
    93     system_str = platform.system()
       
    94     python_lib_dir = get_python_lib()
       
    95     
       
    96     res_source_key = options.type_install
       
    97     
       
    98     logger.indent += 2
       
    99     try:
       
   100         
       
   101         #get pylucene
       
   102         logger.notify("Get Pylucene from %s " % URLS['PYLUCENE'][res_source_key])
       
   103         pylucene_src = os.path.join(src_dir,"pylucene.tar.gz")
       
   104         urllib.urlretrieve(URLS['PYLUCENE'][res_source_key], pylucene_src)
       
   105         tf = tarfile.open(pylucene_src,'r:gz')
       
   106         pylucene_base_path = os.path.join(src_dir,"pylucene") 
       
   107         logger.notify("Extract Pylucene to %s " % pylucene_base_path)
       
   108         tf.extractall(pylucene_base_path)
       
   109         tf.close()
       
   110         
       
   111         pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0])
       
   112         jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc"))
       
   113         
       
   114         #install jcc
       
   115 
       
   116         #patch for linux
       
   117         if system_str == 'Linux' :
       
   118             olddir = os.getcwd()
       
   119             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')
       
   120             
       
   121             if os.path.isfile(patch_dest_path):
       
   122                 # must unzip egg
       
   123                 # rename file and etract all
       
   124                 shutil.move(patch_dest_path, patch_dest_path + ".zip")
       
   125                 zf = zipfile.ZipFile(patch_dest_path + ".zip",'r')
       
   126                 zf.extractall(patch_dest_path)
       
   127                 os.remove(patch_dest_path + ".zip")
       
   128             logger.notify("Patch jcc : %s " % (patch_dest_path))
       
   129             os.chdir(patch_dest_path)
       
   130             p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
       
   131             p.apply()
       
   132             os.chdir(olddir)
       
   133 
       
   134         logger.notify("Install jcc")
       
   135         call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
       
   136                         cwd=jcc_src_path,
       
   137                         filter_stdout=filter_python_develop,
       
   138                         show_stdout=True)
       
   139         #install pylucene
       
   140         
       
   141         logger.notify("Install pylucene")
       
   142         #modify makefile
       
   143         makefile_path = os.path.join(pylucene_src_path,"Makefile")
       
   144         logger.notify("Modify makefile %s " % makefile_path)
       
   145         shutil.move( makefile_path, makefile_path+"~" )
       
   146 
       
   147         destination= open( makefile_path, "w" )
       
   148         source= open( makefile_path+"~", "r" )
       
   149         destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\\n")
       
   150         destination.write("ANT=ant\\n")
       
   151         destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\\n")
       
   152         
       
   153         if system_str == "Darwin":
       
   154             if sys.version_info >= (2,6):
       
   155                 destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n")
       
   156             else:
       
   157                 destination.write("JCC=$(PYTHON) -m jcc --shared\\n")
       
   158             destination.write("NUM_FILES=2\\n")
       
   159         elif system_str == "Windows":
       
   160             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n")
       
   161             destination.write("NUM_FILES=2\\n")
       
   162         else:
       
   163             if sys.version_info >= (2,6) and sys.version_info < (2,7):
       
   164                 destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared\\n")
       
   165             else:
       
   166                 destination.write("JCC=$(PYTHON) -m jcc --shared\\n")
       
   167             destination.write("NUM_FILES=2\\n")
       
   168         for line in source:
       
   169             destination.write( line )
       
   170         source.close()
       
   171         destination.close()
       
   172         os.remove(makefile_path+"~" )
       
   173 
       
   174         logger.notify("pylucene make")
       
   175         call_subprocess(['make'],
       
   176                         cwd=os.path.abspath(pylucene_src_path),
       
   177                         filter_stdout=filter_python_develop,
       
   178                         show_stdout=True)
       
   179 
       
   180         logger.notify("pylucene make install")
       
   181         call_subprocess(['make', 'install'],
       
   182                         cwd=os.path.abspath(pylucene_src_path),
       
   183                         filter_stdout=filter_python_develop,
       
   184                         show_stdout=True)
       
   185 
       
   186         logger.notify("PyXML install : %s " % URLS['PYXML'][res_source_key])
       
   187         if sys.version_info >= (2,6):
       
   188             logger.notify("PyXML -> python version >= 2.6 : patching")
       
   189             pyxml_src = os.path.join(src_dir,"pyxml.tar.gz")
       
   190             urllib.urlretrieve(URLS['PYXML'][res_source_key], pyxml_src)
       
   191             logger.notify("PyXML -> python version >= 2.6 : extract archive")
       
   192             tf = tarfile.open(pyxml_src,'r:gz')
       
   193             pyxml_base_path = os.path.join(src_dir,"pyxml") 
       
   194             tf.extractall(pyxml_base_path)
       
   195             tf.close()
       
   196 
       
   197             #patch
       
   198             pyxml_version = os.listdir(pyxml_base_path)[0]            
       
   199             pyxml_path = os.path.join(pyxml_base_path, pyxml_version)
       
   200             olddir = os.getcwd()
       
   201             os.chdir(pyxml_path)
       
   202             logger.notify("PyXML -> python version >= 2.6 : do patch %s : %s " % (pyxml_path, URLS['PYXML']['patch']))
       
   203             p = patch.fromfile(URLS['PYXML']['patch'])
       
   204             p.apply()
       
   205             os.chdir(olddir)
       
   206             logger.notify("PyXML -> python version >= 2.6 : install")
       
   207             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],
       
   208                     cwd=os.path.abspath(tmp_dir),
       
   209                     filter_stdout=filter_python_develop,
       
   210                     show_stdout=True)
       
   211         else:
       
   212             call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS['PYXML'][res_source_key]],
       
   213                     cwd=os.path.abspath(tmp_dir),
       
   214                     filter_stdout=filter_python_develop,
       
   215                     show_stdout=True)
       
   216         
       
   217         logger.notify("Install Distribute from %s" % URLS['DISTRIBUTE'][res_source_key])
       
   218         call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS['DISTRIBUTE'][res_source_key]],
       
   219                         cwd=os.path.abspath(tmp_dir),
       
   220                         filter_stdout=filter_python_develop,
       
   221                         show_stdout=True)
       
   222                         
       
   223         logger.notify("Install Psycopg2 from %s" % URLS['PSYCOPG2'][res_source_key])
       
   224         call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS['PSYCOPG2'][res_source_key]],
       
   225                         cwd=os.path.abspath(tmp_dir),
       
   226                         filter_stdout=filter_python_develop,
       
   227                         show_stdout=True)
       
   228 
       
   229         logger.notify("Install PIL from %s" % URLS['PIL'][res_source_key])
       
   230         call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS['PIL'][res_source_key]],
       
   231                         cwd=os.path.abspath(tmp_dir),
       
   232                         filter_stdout=filter_python_develop,
       
   233                         show_stdout=True)
       
   234                         
       
   235         logger.notify("Install 4Suite-XML from %s" % URLS['FOURSUITE_XML'][res_source_key])
       
   236         call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), URLS['FOURSUITE_XML'][res_source_key]],
       
   237                         cwd=os.path.abspath(tmp_dir),
       
   238                         filter_stdout=filter_python_develop,
       
   239                         show_stdout=True)
       
   240                         
       
   241         logger.notify("Clear source dir")
       
   242         shutil.rmtree(src_dir)
       
   243 
       
   244     finally:
       
   245         logger.indent -= 2
       
   246     script_dir = join(base_dir, 'bin')
       
   247     logger.notify('Run "%s Package" to install new packages that provide builds'
       
   248                   % join(script_dir, 'easy_install'))
       
   249 
       
   250 def ensure_dir(dir):
       
   251     if not os.path.exists(dir):
       
   252         logger.notify('Creating directory %s' % dir)
       
   253         os.makedirs(dir)
       
   254 
       
   255 def filter_python_develop(line):
       
   256     if not line.strip():
       
   257         return Logger.DEBUG
       
   258     for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ',
       
   259                    'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ',
       
   260                    'creating ', 'Copying ']:
       
   261         if line.startswith(prefix):
       
   262             return Logger.DEBUG
       
   263     return Logger.NOTIFY
       
   264 """
       
   265 
       
   266 def main():
       
   267     python_version = ".".join(map(str,sys.version_info[0:2]))
       
   268     text = virtualenv.create_bootstrap_script(EXTRA_TEXT, python_version=python_version)
       
   269     if os.path.exists(script_name):
       
   270         f = open(script_name)
       
   271         cur_text = f.read()
       
   272         f.close()
       
   273     else:
       
   274         cur_text = ''
       
   275     print 'Updating %s' % script_name
       
   276     if cur_text == 'text':
       
   277         print 'No update'
       
   278     else:
       
   279         print 'Script changed; updating...'
       
   280         f = open(script_name, 'w')
       
   281         f.write(text)
       
   282         f.close()
       
   283 
       
   284 if __name__ == '__main__':
       
   285     main()
       
   286