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