server/virtualenv/res/lib/lib_create_env.py
changeset 1 e0dbcf98c13e
child 28 41087fe3db69
equal deleted inserted replaced
0:f7febf052997 1:e0dbcf98c13e
       
     1 import sys
       
     2 import os
       
     3 import os.path
       
     4 import shutil
       
     5 import tarfile
       
     6 import zipfile
       
     7 import urllib
       
     8 import platform
       
     9 import patch
       
    10 
       
    11 join = os.path.join
       
    12 system_str = platform.system()
       
    13 
       
    14 
       
    15 URLS = {
       
    16     #'': {'setup': '', 'url':'', 'local':''},
       
    17     'SQLALCHEMY' : {'setup': 'SQLAlchemy', 'url':'http://pypi.python.org/packages/source/S/SQLAlchemy/SQLAlchemy-0.7.3.tar.gz', 'local':'SQLAlchemy-0.7.3.tar.gz'},
       
    18     'FLASK' : {'setup': 'flask', 'url':'http://pypi.python.org/packages/source/F/Flask/Flask-0.8.tar.gz', 'local':'Flask-0.8.tar.gz'},
       
    19     'FLASK-SQLALCHEMY' : {'setup': 'flask', 'url':'https://github.com/mitsuhiko/flask-sqlalchemy/tarball/master', 'local':'Flask-SQLAlchemy-0.15.tar.gz'},
       
    20     'FLASK-CACHE' : { 'setup': 'Flask-Cache', 'url' : 'http://pypi.python.org/packages/source/F/Flask-Cache/Flask-Cache-0.3.4.tar.gz', 'local':'Flask-Cache-0.3.4.tar.gz'},
       
    21     'SIMPLEJSON' : {'setup': 'simplejson', 'url':'http://pypi.python.org/packages/source/s/simplejson/simplejson-2.2.1.tar.gz', 'local':'simplejson-2.2.1.tar.gz'},
       
    22     'ANYJSON' : {'setup': 'anyjson', 'url': 'http://pypi.python.org/packages/source/a/anyjson/anyjson-0.3.1.tar.gz', 'local':'anyjson-0.3.1.tar.gz'},
       
    23     'WERKZEUG' : { 'setup': 'werkzeug', 'url':'http://pypi.python.org/packages/source/W/Werkzeug/Werkzeug-0.8.1.tar.gz','local':'Werkzeug-0.8.1.tar.gz'},
       
    24     'JINJA2' : { 'setup': 'jinja2', 'url':'http://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.6.tar.gz','local':'Jinja2-2.6.tar.gz'},
       
    25     'PYTZ' : { 'setup':'pytz', 'url': 'http://pypi.python.org/packages/source/p/pytz/pytz-2011n.tar.bz2', 'local': 'pytz-2011n.tar.bz2'},
       
    26     'RFC3339' : { 'setup':'pyRFC3339', 'url': 'http://pypi.python.org/packages/source/p/pyRFC3339/pyRFC3339-0.1.tar.gz', 'local': 'pyRFC3339-0.1.tar.gz'},    
       
    27 }
       
    28 
       
    29 if system_str == 'Windows':
       
    30     URLS.update({
       
    31         'PSYCOPG2': {'setup': 'psycopg2','url': 'psycopg2-2.0.14.win32-py2.6-pg8.4.3-release.zip', 'local':"psycopg2-2.0.14.win32-py2.6-pg8.4.3-release.zip"},
       
    32     })
       
    33 else:
       
    34     URLS.update({
       
    35         'PSYCOPG2': {'setup': 'psycopg2','url': 'http://www.psycopg.org/psycopg/tarballs/PSYCOPG-2-4/psycopg2-2.4.2.tar.gz', 'local':"psycopg2-2.4.2.tar.gz"},
       
    36     })
       
    37 
       
    38 
       
    39 
       
    40 class ResourcesEnv(object):
       
    41 
       
    42     def __init__(self, src_base, urls, normal_installs):
       
    43         self.src_base = src_base
       
    44         self.URLS = {}
       
    45         self.__init_url(urls)
       
    46         self.NORMAL_INSTALL = normal_installs
       
    47 
       
    48     def get_src_base_path(self, fpath):
       
    49         return os.path.abspath(os.path.join(self.src_base, fpath)).replace("\\","/")
       
    50     
       
    51     def __add_package_def(self, key, setup, url, local):
       
    52         self.URLS[key] = {'setup':setup, 'url':url, 'local':self.get_src_base_path(local)}
       
    53 
       
    54     def __init_url(self, urls):
       
    55         for key, url_dict in urls.items():
       
    56             url = url_dict['url']
       
    57             if not url.startswith("http://"):
       
    58                 url = self.get_src_base_path(url)
       
    59             self.__add_package_def(key, url_dict["setup"], url, url_dict["local"])
       
    60 
       
    61 def ensure_dir(dir, logger):
       
    62     if not os.path.exists(dir):
       
    63         logger.notify('Creating directory %s' % dir)
       
    64         os.makedirs(dir)
       
    65 
       
    66 def extend_parser(parser):    
       
    67     parser.add_option(
       
    68         '--index-url',
       
    69         metavar='INDEX_URL',
       
    70         dest='index_url',
       
    71         default='http://pypi.python.org/simple/',
       
    72         help='base URL of Python Package Index')
       
    73     parser.add_option(
       
    74         '--type-install',
       
    75         metavar='type_install',
       
    76         dest='type_install',
       
    77         default='local',
       
    78         help='type install : local, url, setup')
       
    79     parser.add_option(
       
    80         '--ignore-packages',
       
    81         metavar='ignore_packages',
       
    82         dest='ignore_packages',
       
    83         default=None,
       
    84         help='list of comma separated keys for package to ignore')
       
    85 
       
    86 def adjust_options(options, args):
       
    87     pass
       
    88 
       
    89 
       
    90 def install_pylucene(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop):
       
    91     
       
    92     logger.notify("Get Pylucene from %s " % res_env.URLS['PYLUCENE'][res_source_key])
       
    93     pylucene_src = os.path.join(src_dir,"pylucene.tar.gz")
       
    94     if res_source_key == 'local':
       
    95         shutil.copy(res_env.URLS['PYLUCENE'][res_source_key], pylucene_src)
       
    96     else:
       
    97         urllib.urlretrieve(res_env.URLS['PYLUCENE'][res_source_key], pylucene_src)
       
    98     tf = tarfile.open(pylucene_src,'r:gz')
       
    99     pylucene_base_path = os.path.join(src_dir,"pylucene") 
       
   100     logger.notify("Extract Pylucene to %s " % pylucene_base_path)
       
   101     tf.extractall(pylucene_base_path)
       
   102     tf.close()
       
   103     
       
   104     pylucene_src_path = os.path.join(pylucene_base_path, os.listdir(pylucene_base_path)[0])
       
   105     jcc_src_path = os.path.abspath(os.path.join(pylucene_src_path,"jcc"))
       
   106     
       
   107     #install jcc
       
   108 
       
   109     #patch for linux
       
   110     if system_str == 'Linux' :
       
   111         olddir = os.getcwd()
       
   112         setuptools_path = os.path.join(lib_dir, 'site-packages', 'setuptools')
       
   113         if os.path.exists(setuptools_path) and os.path.isdir(setuptools_path):
       
   114             patch_dest_path = os.path.join(lib_dir, 'site-packages')
       
   115         else:
       
   116             patch_dest_path = os.path.join(lib_dir,'site-packages','setuptools-0.6c11-py%s.%s.egg' % (sys.version_info[0], sys.version_info[1]))
       
   117             if os.path.isfile(patch_dest_path):
       
   118                 # must unzip egg
       
   119                 # rename file and etract all
       
   120                 shutil.move(patch_dest_path, patch_dest_path + ".zip")
       
   121                 zf = zipfile.ZipFile(patch_dest_path + ".zip",'r')
       
   122                 zf.extractall(patch_dest_path)
       
   123                 os.remove(patch_dest_path + ".zip")
       
   124         logger.notify("Patch jcc : %s " % (patch_dest_path))
       
   125         os.chdir(patch_dest_path)
       
   126         p = patch.fromfile(os.path.join(jcc_src_path,"jcc","patches","patch.43.0.6c11"))
       
   127         p.apply()
       
   128         os.chdir(olddir)
       
   129 
       
   130     logger.notify("Install jcc")
       
   131     call_subprocess([os.path.abspath(os.path.join(home_dir, 'bin', 'python')), 'setup.py', 'install'],
       
   132                     cwd=jcc_src_path,
       
   133                     filter_stdout=filter_python_develop,
       
   134                     show_stdout=True)
       
   135     #install pylucene
       
   136     
       
   137     logger.notify("Install pylucene")
       
   138     #modify makefile
       
   139     makefile_path = os.path.join(pylucene_src_path,"Makefile")
       
   140     logger.notify("Modify makefile %s " % makefile_path)
       
   141     shutil.move( makefile_path, makefile_path+"~" )
       
   142 
       
   143     destination= open( makefile_path, "w" )
       
   144     source= open( makefile_path+"~", "r" )
       
   145     destination.write("PREFIX_PYTHON="+os.path.abspath(home_dir)+"\n")
       
   146     destination.write("ANT=ant\n")
       
   147     destination.write("PYTHON=$(PREFIX_PYTHON)/bin/python\n")
       
   148     
       
   149     if system_str == "Darwin":
       
   150         if sys.version_info >= (2,6):
       
   151             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   152         else:
       
   153             destination.write("JCC=$(PYTHON) -m jcc --shared --arch x86_64 --arch i386\n")
       
   154         destination.write("NUM_FILES=2\n")
       
   155     elif system_str == "Windows":
       
   156         destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared --arch x86_64 --arch i386\n")
       
   157         destination.write("NUM_FILES=2\n")
       
   158     else:
       
   159         if sys.version_info >= (2,6) and sys.version_info <= (2,7):
       
   160             destination.write("JCC=$(PYTHON) -m jcc.__main__ --shared\n")
       
   161         else:
       
   162             destination.write("JCC=$(PYTHON) -m jcc --shared\n")
       
   163         destination.write("NUM_FILES=2\n")
       
   164     for line in source:
       
   165         destination.write( line )
       
   166     source.close()
       
   167     destination.close()
       
   168     os.remove(makefile_path+"~" )
       
   169 
       
   170     logger.notify("pylucene make")
       
   171     call_subprocess(['make'],
       
   172                     cwd=os.path.abspath(pylucene_src_path),
       
   173                     filter_stdout=filter_python_develop,
       
   174                     show_stdout=True)
       
   175 
       
   176     logger.notify("pylucene make install")
       
   177     call_subprocess(['make', 'install'],
       
   178                     cwd=os.path.abspath(pylucene_src_path),
       
   179                     filter_stdout=filter_python_develop,
       
   180                     show_stdout=True)
       
   181     
       
   182 
       
   183 def install_psycopg2(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop):
       
   184     psycopg2_src = os.path.join(src_dir,"psycopg2.zip")
       
   185     shutil.copy(res_env.URLS['PSYCOPG2'][res_source_key], psycopg2_src)
       
   186     #extract psycopg2
       
   187     zf = zipfile.ZipFile(psycopg2_src)
       
   188     psycopg2_base_path = os.path.join(src_dir,"psycopg2")
       
   189     zf.extractall(psycopg2_base_path)
       
   190     zf.close()
       
   191     
       
   192     psycopg2_src_path = os.path.join(psycopg2_base_path, os.listdir(psycopg2_base_path)[0])
       
   193     shutil.copytree(os.path.join(psycopg2_src_path, 'psycopg2'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'psycopg2')))
       
   194     shutil.copy(os.path.join(psycopg2_src_path, 'psycopg2-2.0.14-py2.6.egg-info'), os.path.abspath(os.path.join(home_dir, 'Lib/site-packages', 'site-packages')))
       
   195 
       
   196 
       
   197 
       
   198 def lib_generate_install_methods(path_locations, src_base, Logger, call_subprocess, normal_installs, urls=None):
       
   199     
       
   200     all_urls = URLS.copy()
       
   201     if urls is not None:
       
   202         all_urls.update(urls)
       
   203         
       
   204     res_env = ResourcesEnv(src_base, all_urls, normal_installs)
       
   205 
       
   206     def filter_python_develop(line):
       
   207         if not line.strip():
       
   208             return Logger.DEBUG
       
   209         for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ',
       
   210                        'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ',
       
   211                        'creating ', 'Copying ']:
       
   212             if line.startswith(prefix):
       
   213                 return Logger.DEBUG
       
   214         return Logger.NOTIFY
       
   215     
       
   216     
       
   217     def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess):
       
   218         logger.notify("Install %s from %s with %s" % (key,res_env.URLS[key][res_source_key],method))
       
   219         if method == 'pip':
       
   220             if sys.platform == 'win32':
       
   221                 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', '-E', os.path.abspath(home_dir), res_env.URLS[key][res_source_key]]
       
   222             else:
       
   223                 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), res_env.URLS[key][res_source_key]]
       
   224             if option_str :
       
   225                 args.insert(4,option_str)
       
   226             call_subprocess(args,
       
   227                     cwd=os.path.abspath(tmp_dir),
       
   228                     filter_stdout=filter_python_develop,
       
   229                     show_stdout=True,
       
   230                     extra_env=extra_env)
       
   231         else:
       
   232             if sys.platform == 'win32':
       
   233                 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   234             else:
       
   235                 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), res_env.URLS[key][res_source_key]]
       
   236             if option_str :
       
   237                 args.insert(1,option_str)
       
   238             call_subprocess(args,
       
   239                     cwd=os.path.abspath(tmp_dir),
       
   240                     filter_stdout=filter_python_develop,
       
   241                     show_stdout=True,
       
   242                     extra_env=extra_env)            
       
   243  
       
   244     
       
   245     def after_install(options, home_dir):
       
   246         
       
   247         global logger
       
   248         
       
   249         verbosity = options.verbose - options.quiet
       
   250         logger = Logger([(Logger.level_for_integer(2-verbosity), sys.stdout)])
       
   251 
       
   252         
       
   253         home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir)
       
   254         base_dir = os.path.dirname(home_dir)
       
   255         src_dir = os.path.join(home_dir, 'src')
       
   256         tmp_dir = os.path.join(home_dir, 'tmp')
       
   257         ensure_dir(src_dir, logger)
       
   258         ensure_dir(tmp_dir, logger)
       
   259         system_str = platform.system()
       
   260         
       
   261         res_source_key = options.type_install
       
   262         
       
   263         ignore_packages = []
       
   264         
       
   265         if options.ignore_packages :
       
   266             ignore_packages = options.ignore_packages.split(",")
       
   267         
       
   268         logger.indent += 2
       
   269         try:    
       
   270             for key, method, option_str, extra_env in res_env.NORMAL_INSTALL:
       
   271                 if key not in ignore_packages:
       
   272                     if callable(method):
       
   273                         method(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop)
       
   274                     else:
       
   275                         normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess)
       
   276                             
       
   277             logger.notify("Clear source dir")
       
   278             shutil.rmtree(src_dir)
       
   279     
       
   280         finally:
       
   281             logger.indent -= 2
       
   282         script_dir = join(base_dir, bin_dir)
       
   283         logger.notify('Run "%s Package" to install new packages that provide builds'
       
   284                       % join(script_dir, 'easy_install'))
       
   285     
       
   286 
       
   287     return adjust_options, extend_parser, after_install