sbin/virtualenv/create_python_env.py
changeset 2 252f2e87cdc3
parent 0 ecdfc63274bf
child 10 84e31387a741
equal deleted inserted replaced
0:ecdfc63274bf 2:252f2e87cdc3
     6 
     6 
     7 - virtualenv
     7 - virtualenv
     8 - distribute
     8 - distribute
     9 - psycopg2 requires the PostgreSQL libpq libraries and the pg_config utility
     9 - psycopg2 requires the PostgreSQL libpq libraries and the pg_config utility
    10 
    10 
    11 - virtualenv --distribute --no-site-packages venv
    11 - python project-boot.py --distribute --no-site-packages --index-url=http://pypi.websushi.org/ --clear --type-install=local <path_to_venv>
    12 - python project-boot.py --distribute --no-site-packages --index-url=http://pypi.websushi.org/ --clear bvenv
    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>
    13 
    14 
    14 """
    15 """
    15 
    16 
    16 import os
    17 import os
    17 import subprocess
    18 import subprocess
    77         '--type-install',
    78         '--type-install',
    78         metavar='type_install',
    79         metavar='type_install',
    79         dest='type_install',
    80         dest='type_install',
    80         default='local',
    81         default='local',
    81         help='type install : local, url, setup')
    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 
    82 
    90 
    83 
    91 
    84 def adjust_options(options, args):
    92 def adjust_options(options, args):
    85     pass
    93     pass
    86 
    94 
    94     ensure_dir(tmp_dir)
   102     ensure_dir(tmp_dir)
    95     system_str = platform.system()
   103     system_str = platform.system()
    96     
   104     
    97     res_source_key = options.type_install
   105     res_source_key = options.type_install
    98     
   106     
       
   107     ignore_packages = []
       
   108     
       
   109     if options.ignore_packages :
       
   110         ignore_packages = options.ignore_packages.split(",")
       
   111     
    99     logger.indent += 2
   112     logger.indent += 2
   100     try:
   113     try:
   101         
   114         
   102         #get pylucene
   115         if 'PYLUCENE' not in ignore_packages:
   103         logger.notify("Get Pylucene from %s " % URLS['PYLUCENE'][res_source_key])
   116             #get pylucene
   104         pylucene_src = os.path.join(src_dir,"pylucene.tar.gz")
   117             logger.notify("Get Pylucene from %s " % URLS['PYLUCENE'][res_source_key])
   105         urllib.urlretrieve(URLS['PYLUCENE'][res_source_key], pylucene_src)
   118             pylucene_src = os.path.join(src_dir,"pylucene.tar.gz")
   106         tf = tarfile.open(pylucene_src,'r:gz')
   119             urllib.urlretrieve(URLS['PYLUCENE'][res_source_key], pylucene_src)
   107         pylucene_base_path = os.path.join(src_dir,"pylucene") 
   120             tf = tarfile.open(pylucene_src,'r:gz')
   108         logger.notify("Extract Pylucene to %s " % pylucene_base_path)
   121             pylucene_base_path = os.path.join(src_dir,"pylucene") 
   109         tf.extractall(pylucene_base_path)
   122             logger.notify("Extract Pylucene to %s " % pylucene_base_path)
   110         tf.close()
   123             tf.extractall(pylucene_base_path)
   111         
   124             tf.close()
   112         pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0])
   125             
   113         jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc"))
   126             pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0])
   114         
   127             jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc"))
   115         #install jcc
   128             
   116 
   129             #install jcc
   117         #patch for linux
   130     
   118         if system_str == 'Linux' :
   131             #patch for linux
   119             olddir = os.getcwd()
   132             if system_str == 'Linux' :
   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')
   133                 olddir = os.getcwd()
   121             logger.notify("Patch jcc : %s " % (patch_dest_path))
   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')
   122             os.chdir(patch_dest_path)
   135                 logger.notify("Patch jcc : %s " % (patch_dest_path))
   123             p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
   136                 os.chdir(patch_dest_path)
   124             p.apply()
   137                 p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
   125             os.chdir(olddir)
   138                 p.apply()
   126 
   139                 os.chdir(olddir)
   127         logger.notify("Install jcc")
   140     
   128         call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
   141             logger.notify("Install jcc")
   129                         cwd=jcc_src_path,
   142             call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
       
   143                             cwd=jcc_src_path,
       
   144                             filter_stdout=filter_python_develop,
       
   145                             show_stdout=True)
       
   146             #install pylucene
       
   147             
       
   148             logger.notify("Install pylucene")
       
   149             #modify makefile
       
   150             makefile_path = os.path.join(pylucene_src_path,"Makefile")
       
   151             logger.notify("Modify makefile %s " % makefile_path)
       
   152             shutil.move( makefile_path, makefile_path+"~" )
       
   153     
       
   154             destination= open( makefile_path, "w" )
       
   155             source= open( makefile_path+"~", "r" )
       
   156             destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\\n")
       
   157             destination.write("ANT=ant\\n")
       
   158             destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\\n")
       
   159             
       
   160             if system_str == "Darwin":
       
   161                 if sys.version_info >= (2,6):
       
   162                     destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n")
       
   163                 else:
       
   164                     destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\\n")
       
   165                 destination.write("NUM_FILES=2\\n")
       
   166             elif system_str == "Windows":
       
   167                 destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\\n")
       
   168                 destination.write("NUM_FILES=2\\n")
       
   169             else:
       
   170                 destination.write("JCC=$(PYTHON) -m jcc --shared\\n")
       
   171                 destination.write("NUM_FILES=2\\n")
       
   172             for line in source:
       
   173                 destination.write( line )
       
   174             source.close()
       
   175             destination.close()
       
   176             os.remove(makefile_path+"~" )
       
   177     
       
   178             logger.notify("pylucene make")
       
   179             call_subprocess(['make'],
       
   180                             cwd=os.path.abspath(pylucene_src_path),
       
   181                             filter_stdout=filter_python_develop,
       
   182                             show_stdout=True)
       
   183     
       
   184             logger.notify("pylucene make install")
       
   185             call_subprocess(['make', 'install'],
       
   186                             cwd=os.path.abspath(pylucene_src_path),
       
   187                             filter_stdout=filter_python_develop,
       
   188                             show_stdout=True)
       
   189 
       
   190         if system_str == 'Linux'  and 'DISTRIBUTE' not in ignore_packages:
       
   191             normal_install('DISTRIBUTE', 'pip', None, res_source_key, home_dir, tmp_dir)
       
   192 
       
   193         if 'PYXML' not in ignore_packages:
       
   194             logger.notify("PyXML install : %s " % URLS['PYXML'][res_source_key])
       
   195             if sys.version_info >= (2,6):
       
   196                 logger.notify("PyXML -> python version >= 2.6 : patching")
       
   197                 pyxml_src = os.path.join(src_dir,"pyxml.tar.gz")
       
   198                 urllib.urlretrieve(URLS['PYXML'][res_source_key], pyxml_src)
       
   199                 logger.notify("PyXML -> python version >= 2.6 : extract archive")
       
   200                 tf = tarfile.open(pyxml_src,'r:gz')
       
   201                 pyxml_base_path = os.path.join(src_dir,"pyxml") 
       
   202                 tf.extractall(pyxml_base_path)
       
   203                 tf.close()
       
   204     
       
   205                 #patch
       
   206                 pyxml_version = os.listdir(pyxml_base_path)[0]            
       
   207                 pyxml_path = os.path.join(pyxml_base_path, pyxml_version)
       
   208                 olddir = os.getcwd()
       
   209                 os.chdir(pyxml_path)
       
   210                 logger.notify("PyXML -> python version >= 2.6 : do patch %s : %s " % (pyxml_path, URLS['PYXML']['patch']))
       
   211                 p = patch.fromfile(URLS['PYXML']['patch'])
       
   212                 p.apply()
       
   213                 os.chdir(olddir)
       
   214                 logger.notify("PyXML -> python version >= 2.6 : install")
       
   215                 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],
       
   216                         cwd=os.path.abspath(tmp_dir),
   130                         filter_stdout=filter_python_develop,
   217                         filter_stdout=filter_python_develop,
   131                         show_stdout=True)
   218                         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:
   219             else:
   150                 destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\\n")
   220                 call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS['PYXML'][res_source_key]],
   151             destination.write("NUM_FILES=2\\n")
   221                         cwd=os.path.abspath(tmp_dir),
   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,
   222                         filter_stdout=filter_python_develop,
   168                         show_stdout=True)
   223                         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         
   224         
   207         
   225         
   208         NORMAL_INSTALL = [ #(key,method)
   226         NORMAL_INSTALL = [ #(key,method)
   209             ('DISTRIBUTE', 'pip', None),
       
   210             ('PSYCOPG2', 'pip', None),
   227             ('PSYCOPG2', 'pip', None),
   211             ('MYSQL', 'pip', None),
   228             ('MYSQL', 'pip', None),
   212             ('PIL', 'pip', None), 
   229             ('PIL', 'pip', None), 
   213             ('FOURSUITE_XML','easy_install', None), 
   230             ('FOURSUITE_XML','easy_install', None), 
   214             ('DJANGO','pip', None),
   231             ('DJANGO','pip', None),
   215             ('DJANGO-EXTENSIONS', 'pip', None),
   232             ('DJANGO-EXTENSIONS', 'pip', None),
   216             ('DJANGO-REGISTRATION', 'easy_install', '-Z')
   233             ('DJANGO-REGISTRATION', 'easy_install', '-Z')
   217             ]
   234             ]
   218          
   235         
   219          if sys.version_info < (2,6):
   236         if sys.version_info < (2,6):
   220              NORMAL_INSTALL.append(('JSON','pip'))
   237             NORMAL_INSTALL.append(('JSON','pip', None))
   221         
   238         
   222             
   239             
   223         for key, method, options in NORMAL_INSTALL:
   240         for key, method, option_str in NORMAL_INSTALL:
   224             logger.notify("Install %s from %s with %s" % (key,URLS[key][res_source_key],method))
   241             if key not in ignore_packages:
   225             if method == 'pip':
   242                 normal_install(key, method, option_str, res_source_key, home_dir, tmp_dir)
   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                         
   243         logger.notify("Clear source dir")
   244         logger.notify("Clear source dir")
   244         shutil.rmtree(src_dir)
   245         shutil.rmtree(src_dir)
   245 
   246 
   246     finally:
   247     finally:
   247         logger.indent -= 2
   248         logger.indent -= 2
   248     script_dir = join(base_dir, 'bin')
   249     script_dir = join(base_dir, 'bin')
   249     logger.notify('Run "%s Package" to install new packages that provide builds'
   250     logger.notify('Run "%s Package" to install new packages that provide builds'
   250                   % join(script_dir, 'easy_install'))
   251                   % join(script_dir, 'easy_install'))
       
   252 
       
   253 
       
   254 def normal_install(key, method, option_str, res_source_key, home_dir, tmp_dir):
       
   255     logger.notify("Install %s from %s with %s" % (key,URLS[key][res_source_key],method))
       
   256     if method == 'pip':
       
   257         args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS[key][res_source_key]]
       
   258         if option_str :
       
   259             args.insert(4,option_str)
       
   260         call_subprocess(args,
       
   261                 cwd=os.path.abspath(tmp_dir),
       
   262                 filter_stdout=filter_python_develop,
       
   263                 show_stdout=True)
       
   264     else:
       
   265         args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), URLS[key][res_source_key]]
       
   266         if option_str :
       
   267             args.insert(1,option_str)
       
   268         call_subprocess(args,
       
   269                 cwd=os.path.abspath(tmp_dir),
       
   270                 filter_stdout=filter_python_develop,
       
   271                 show_stdout=True)
       
   272     
   251 
   273 
   252 def ensure_dir(dir):
   274 def ensure_dir(dir):
   253     if not os.path.exists(dir):
   275     if not os.path.exists(dir):
   254         logger.notify('Creating directory %s' % dir)
   276         logger.notify('Creating directory %s' % dir)
   255         os.makedirs(dir)
   277         os.makedirs(dir)