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