configurationengine/build-scripts/install_cone.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #   Script for building and installing ConE into a specified directory.
       
    16 #
       
    17 
       
    18 import sys, os, shutil, subprocess, optparse
       
    19 import logging
       
    20 log = logging.getLogger()
       
    21 
       
    22 import utils
       
    23 utils.setup_logging('install_cone.log')
       
    24 
       
    25 ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
       
    26 
       
    27 SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source'))
       
    28 assert os.path.isdir(SOURCE_ROOT)
       
    29 SCRIPTS_SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source/scripts'))
       
    30 assert os.path.isdir(SCRIPTS_SOURCE_ROOT)
       
    31 PLUGIN_SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source/plugins'))
       
    32 assert os.path.isdir(PLUGIN_SOURCE_ROOT)
       
    33 
       
    34 sys.path.append(PLUGIN_SOURCE_ROOT)
       
    35 import plugin_utils
       
    36 
       
    37 # Temporary directory where ConE eggs are built into
       
    38 TEMP_CONE_EGG_DIR = os.path.join(ROOT_PATH, 'install-temp/cone-eggs')
       
    39 # Temporary directory where dependency lib eggs are copied
       
    40 TEMP_LIB_EGG_DIR = os.path.join(ROOT_PATH, 'install-temp/dep-eggs')
       
    41 
       
    42 class BuildFailedError(RuntimeError):
       
    43     pass
       
    44 
       
    45 def find_cone_egg_sources(plugin_package):
       
    46     """
       
    47     Return a list of paths to the source directories to install.
       
    48     """
       
    49     paths = [SOURCE_ROOT,
       
    50              SCRIPTS_SOURCE_ROOT]
       
    51     plugin_paths = plugin_utils.find_plugin_sources_by_package(plugin_package)
       
    52     paths.extend(plugin_paths)
       
    53     
       
    54     log.debug("ConE egg source paths:\n%s" % '\n'.join(paths))
       
    55     return paths
       
    56     
       
    57 
       
    58 def build_cone_eggs(source_paths):
       
    59     log.info("Cleaning temporary ConE egg dir...")
       
    60     utils.recreate_dir(TEMP_CONE_EGG_DIR)
       
    61     
       
    62     log.info("Building ConE eggs...")
       
    63     for source_path in source_paths:
       
    64         ok = utils.build_egg(source_path, TEMP_CONE_EGG_DIR)
       
    65         if not ok:
       
    66             raise BuildFailedError()
       
    67 
       
    68 def retrieve_dep_eggs(plugin_package):
       
    69     log.info("Cleaning temporary lib egg dir...")
       
    70     utils.recreate_dir(TEMP_LIB_EGG_DIR)
       
    71     
       
    72     log.info("Retrieving dependency eggs...")
       
    73     def copy_eggs(source_dir):
       
    74         log.debug("Copying eggs from '%s'..." % source_dir)
       
    75         for name in os.listdir(source_dir):
       
    76             if name.endswith('.egg'):
       
    77                 utils.copy_file(
       
    78                     source_path = os.path.join(source_dir, name),
       
    79                     target_path = TEMP_LIB_EGG_DIR)
       
    80    
       
    81     dep_dirs_by_package = [(None, os.path.join(ROOT_PATH, '../dep-eggs'))]
       
    82     dep_dirs_by_package.extend(plugin_utils.find_plugin_package_subpaths('dep-eggs', plugin_package))
       
    83     
       
    84     for package_name, dep_dir in dep_dirs_by_package:
       
    85         copy_eggs(dep_dir)
       
    86 
       
    87 def init_target_dir(target_dir, python_version):
       
    88     BASE_DIR = os.path.normpath(os.path.join(target_dir, 'cone', python_version))
       
    89     LIB_DIR     = os.path.join(BASE_DIR, 'lib')
       
    90     SCRIPT_DIR  = os.path.join(BASE_DIR, 'scripts')
       
    91     
       
    92     utils.recreate_dir(BASE_DIR)
       
    93     utils.recreate_dir(LIB_DIR)
       
    94     utils.recreate_dir(SCRIPT_DIR)
       
    95     return LIB_DIR, SCRIPT_DIR
       
    96 
       
    97 def install_cone_eggs(target_dir, python_version):
       
    98     """
       
    99     Install ConE eggs into the given target directory.
       
   100     """
       
   101     log.info("Installing ConE eggs...")
       
   102     LIB_DIR, SCRIPT_DIR = init_target_dir(target_dir, python_version)
       
   103     
       
   104     # Collect the eggs to install
       
   105     eggs = ['setuptools'] # Setuptools are needed also
       
   106     for name in os.listdir(TEMP_CONE_EGG_DIR):
       
   107         if name.endswith('.egg'):
       
   108             eggs.append(TEMP_CONE_EGG_DIR + '/' + name)
       
   109     
       
   110     # Run easy_install to install the eggs
       
   111     for egg in eggs:
       
   112         log.debug(egg)
       
   113         
       
   114         if sys.platform == "win32":
       
   115             platform_args = ["--always-copy"]
       
   116         else:
       
   117             platform_args = ["--no-deps"]
       
   118                     
       
   119         command = ['easy_install',
       
   120                    '--allow-hosts None',
       
   121                    '--find-links install-temp/dep-eggs',
       
   122                    '--install-dir "%s"' % LIB_DIR,
       
   123                    '--script-dir "%s"' % SCRIPT_DIR,
       
   124                    '--site-dirs "%s"' % LIB_DIR]
       
   125         command.extend(platform_args)
       
   126         command.append('"' + egg + '"')
       
   127         command = ' '.join(command)
       
   128         
       
   129         log.debug(command)
       
   130         ok = utils.run_command(command, env_overrides={'PYTHONPATH': LIB_DIR})
       
   131         if not ok:
       
   132             raise BuildFailedError()
       
   133 
       
   134 def develop_install_cone_sources(source_paths, target_dir, python_version):
       
   135     log.info("Installing ConE sources in develop mode...")
       
   136     LIB_DIR, SCRIPT_DIR = init_target_dir(target_dir, python_version)
       
   137     
       
   138     orig_workdir = os.getcwd()
       
   139     try:
       
   140         for source_path in source_paths:
       
   141             os.chdir(source_path)
       
   142             command = ['python setup.py develop',
       
   143                    '--allow-hosts None',
       
   144                    '--find-links "%s"' % os.path.normpath(os.path.join(ROOT_PATH, 'install-temp/dep-eggs')),
       
   145                    '--install-dir "%s"' % LIB_DIR,
       
   146                    '--script-dir "%s"' % SCRIPT_DIR,
       
   147                    '--site-dirs "%s"' % LIB_DIR,
       
   148                    '--always-copy']
       
   149             command = ' '.join(command)
       
   150             log.debug(command)
       
   151             ok = utils.run_command(command, env_overrides={'PYTHONPATH': LIB_DIR})
       
   152             if not ok:
       
   153                 raise BuildFailedError()
       
   154     finally:
       
   155         os.chdir(orig_workdir)
       
   156 
       
   157 def perform_build(target_dir, plugin_package, install_type, python_version):
       
   158     log.info("Target directory: %s" % target_dir)
       
   159     log.info("Plug-in package:  %r" % plugin_package)
       
   160     log.info("Python version:   %s" % python_version)
       
   161 
       
   162     # Retrieve dependencies to the correct location
       
   163     retrieve_dep_eggs(plugin_package)
       
   164     
       
   165     # Find paths to the sources to install
       
   166     source_paths = find_cone_egg_sources(plugin_package)
       
   167     
       
   168     log.info("Creating install directory...")
       
   169     if not os.path.exists(target_dir):
       
   170         os.makedirs(target_dir)
       
   171     
       
   172     if install_type == 'install':
       
   173         build_cone_eggs(source_paths)
       
   174         install_cone_eggs(target_dir, python_version)
       
   175     else:
       
   176         develop_install_cone_sources(source_paths, target_dir, python_version)
       
   177     
       
   178     # Copy RELEASE.txt
       
   179     utils.copy_file(
       
   180         source_path = os.path.join(SOURCE_ROOT, '..', 'RELEASE.TXT'),
       
   181         target_path = os.path.join(target_dir, 'cone', 'RELEASE.TXT'))
       
   182     
       
   183     # Copy cone.cmd or cone.sh, depending on the platform
       
   184     if sys.platform == "win32":
       
   185         filename = "cone.cmd"
       
   186     else:
       
   187         filename = "cone.sh"
       
   188     log.info("Copying %s" % filename)
       
   189     utils.copy_file(
       
   190         source_path = os.path.join(SOURCE_ROOT, filename),
       
   191         target_path = target_dir)
       
   192 
       
   193 def main():
       
   194     parser = optparse.OptionParser()
       
   195     parser.add_option("-t", "--target-dir",
       
   196                       help="The directory where ConE is to be installed.")
       
   197     parser.add_option("-p", "--plugin-package",\
       
   198                       help="The plug-in package to include in the installation.",\
       
   199                       default=None)
       
   200     parser.add_option("-i", "--install-type",\
       
   201                       help="The installation type, can be 'install' (the default) or 'develop'.",\
       
   202                       default='install')
       
   203     (options, args) = parser.parse_args()
       
   204     if options.target_dir is None:
       
   205         parser.error("Target directory must be given")
       
   206     if options.install_type not in ('install', 'develop'):
       
   207         parser.error("Invalid install type ('%s')" % options.install_type)
       
   208     
       
   209     if not utils.run_command("python --help"):
       
   210         log.critical("Could not run 'python'. Please make sure that you "\
       
   211                      "have Python installed and in your path.")
       
   212         return 1
       
   213     
       
   214     if not utils.run_command("easy_install --help"):
       
   215         log.critical("Could not run 'easy_install'. Please make sure that you "\
       
   216                      "have setuptools installed and the Python scripts directory in your path.")
       
   217         return 1
       
   218     
       
   219     python_version = utils.get_python_version()
       
   220     
       
   221     try:
       
   222         perform_build(options.target_dir, options.plugin_package, options.install_type, python_version)
       
   223     except BuildFailedError:
       
   224         return 1
       
   225     
       
   226     return 0
       
   227 
       
   228 if __name__ == "__main__":
       
   229     sys.exit(main())