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