configurationengine/build-scripts/install_cone.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
child 5 d2c80f5cab53
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    22 import utils
    22 import utils
    23 utils.setup_logging('install_cone.log')
    23 utils.setup_logging('install_cone.log')
    24 
    24 
    25 ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
    25 ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
    26 
    26 
       
    27 # The sub-directory of the main install directory where all required libraries
       
    28 # etc. are installed
       
    29 INSTALL_SUBDIR = 'configurationengine'
       
    30 
       
    31 # The sub-directory for required libraries, platform-specific
       
    32 if sys.platform == "win32":
       
    33     PLATFORM_SUBDIR = 'win'
       
    34 else:
       
    35     PLATFORM_SUBDIR = 'linux'
       
    36 
       
    37 PYTHON_EXECUTABLE = 'python'
       
    38 
    27 SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source'))
    39 SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source'))
    28 assert os.path.isdir(SOURCE_ROOT)
    40 assert os.path.isdir(SOURCE_ROOT)
    29 SCRIPTS_SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source/scripts'))
    41 SCRIPTS_SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source/scripts'))
    30 assert os.path.isdir(SCRIPTS_SOURCE_ROOT)
    42 assert os.path.isdir(SCRIPTS_SOURCE_ROOT)
    31 PLUGIN_SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source/plugins'))
    43 PLUGIN_SOURCE_ROOT = os.path.abspath(os.path.join(ROOT_PATH, '../source/plugins'))
    32 assert os.path.isdir(PLUGIN_SOURCE_ROOT)
    44 assert os.path.isdir(PLUGIN_SOURCE_ROOT)
    33 
    45 TESTAUTOMATION_ROOT = os.path.abspath(os.path.join(SOURCE_ROOT, 'testautomation'))
       
    46 assert os.path.isdir(TESTAUTOMATION_ROOT)
       
    47 
       
    48 sys.path.insert(0, TESTAUTOMATION_ROOT)
    34 sys.path.append(PLUGIN_SOURCE_ROOT)
    49 sys.path.append(PLUGIN_SOURCE_ROOT)
    35 import plugin_utils
    50 from testautomation import plugin_utils
    36 
    51 
    37 # Temporary directory where ConE eggs are built into
    52 # Temporary directory where ConE eggs are built into
    38 TEMP_CONE_EGG_DIR = os.path.join(ROOT_PATH, 'install-temp/cone-eggs')
    53 TEMP_CONE_EGG_DIR = os.path.join(ROOT_PATH, 'install-temp/cone-eggs')
    39 # Temporary directory where dependency lib eggs are copied
    54 # Temporary directory where dependency lib eggs are copied
    40 TEMP_LIB_EGG_DIR = os.path.join(ROOT_PATH, 'install-temp/dep-eggs')
    55 TEMP_LIB_EGG_DIR = os.path.join(ROOT_PATH, 'install-temp/dep-eggs')
    46     """
    61     """
    47     Return a list of paths to the source directories to install.
    62     Return a list of paths to the source directories to install.
    48     """
    63     """
    49     paths = [SOURCE_ROOT,
    64     paths = [SOURCE_ROOT,
    50              SCRIPTS_SOURCE_ROOT]
    65              SCRIPTS_SOURCE_ROOT]
    51     plugin_paths = plugin_utils.find_plugin_sources_by_package(plugin_package)
    66     plugin_paths = plugin_utils.find_plugin_sources_by_package(PLUGIN_SOURCE_ROOT, plugin_package)
    52     paths.extend(plugin_paths)
    67     paths.extend(plugin_paths)
    53     
    68     
    54     log.debug("ConE egg source paths:\n%s" % '\n'.join(paths))
    69     log.debug("ConE egg source paths:\n%s" % '\n'.join(paths))
    55     return paths
    70     return paths
    56     
    71     
    57 
    72 
    58 def build_cone_eggs(source_paths):
    73 def build_cone_eggs(source_paths, python_executable):
    59     log.info("Cleaning temporary ConE egg dir...")
    74     log.info("Cleaning temporary ConE egg dir...")
    60     utils.recreate_dir(TEMP_CONE_EGG_DIR)
    75     utils.recreate_dir(TEMP_CONE_EGG_DIR)
    61     
    76     
    62     log.info("Building ConE eggs...")
    77     log.info("Building ConE eggs...")
    63     for source_path in source_paths:
    78     for source_path in source_paths:
    64         ok = utils.build_egg(source_path, TEMP_CONE_EGG_DIR)
    79         ok = utils.build_egg(source_path, TEMP_CONE_EGG_DIR, python_executable)
    65         if not ok:
    80         if not ok:
    66             raise BuildFailedError()
    81             raise BuildFailedError()
    67 
    82 
    68 def retrieve_dep_eggs(plugin_package):
    83 def retrieve_dep_eggs(plugin_package):
    69     log.info("Cleaning temporary lib egg dir...")
    84     log.info("Cleaning temporary lib egg dir...")
    72     log.info("Retrieving dependency eggs...")
    87     log.info("Retrieving dependency eggs...")
    73     def copy_eggs(source_dir):
    88     def copy_eggs(source_dir):
    74         log.debug("Copying eggs from '%s'..." % source_dir)
    89         log.debug("Copying eggs from '%s'..." % source_dir)
    75         for name in os.listdir(source_dir):
    90         for name in os.listdir(source_dir):
    76             if name.endswith('.egg'):
    91             if name.endswith('.egg'):
    77                 utils.copy_file(
    92                 if PLATFORM_SUBDIR == 'linux' and name.startswith('setuptools-0.6c11'):
    78                     source_path = os.path.join(source_dir, name),
    93                     continue
    79                     target_path = TEMP_LIB_EGG_DIR)
    94                 else:
       
    95                     utils.copy_file(
       
    96                         source_path = os.path.join(source_dir, name),
       
    97                         target_path = TEMP_LIB_EGG_DIR)
    80    
    98    
    81     dep_dirs_by_package = [(None, os.path.join(ROOT_PATH, '../dep-eggs'))]
    99     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))
   100     dep_dirs_by_package.extend(plugin_utils.find_plugin_package_subpaths(PLUGIN_SOURCE_ROOT, 'dep-eggs', plugin_package))
    83     
   101     
    84     for package_name, dep_dir in dep_dirs_by_package:
   102     for package_name, dep_dir in dep_dirs_by_package:
    85         copy_eggs(dep_dir)
   103         copy_eggs(dep_dir)
    86 
   104 
    87 def init_target_dir(target_dir, python_version):
   105 def init_target_dir(target_dir, python_version):
    88     BASE_DIR = os.path.normpath(os.path.join(target_dir, 'cone', python_version))
   106     BASE_DIR = os.path.normpath(os.path.join(target_dir, INSTALL_SUBDIR, PLATFORM_SUBDIR, python_version))
    89     LIB_DIR     = os.path.join(BASE_DIR, 'lib')
   107     LIB_DIR     = os.path.join(BASE_DIR, 'lib')
    90     SCRIPT_DIR  = os.path.join(BASE_DIR, 'scripts')
   108     SCRIPT_DIR  = os.path.join(BASE_DIR, 'scripts')
    91     
   109     
    92     utils.recreate_dir(BASE_DIR)
   110     utils.recreate_dir(BASE_DIR)
    93     utils.recreate_dir(LIB_DIR)
   111     utils.recreate_dir(LIB_DIR)
   100     """
   118     """
   101     log.info("Installing ConE eggs...")
   119     log.info("Installing ConE eggs...")
   102     LIB_DIR, SCRIPT_DIR = init_target_dir(target_dir, python_version)
   120     LIB_DIR, SCRIPT_DIR = init_target_dir(target_dir, python_version)
   103     
   121     
   104     # Collect the eggs to install
   122     # Collect the eggs to install
   105     eggs = ['setuptools'] # Setuptools are needed also
   123     eggs = ['setuptools'] # Setuptools are needed
       
   124         
   106     for name in os.listdir(TEMP_CONE_EGG_DIR):
   125     for name in os.listdir(TEMP_CONE_EGG_DIR):
   107         if name.endswith('.egg'):
   126         if name.endswith('.egg'):
   108             eggs.append(TEMP_CONE_EGG_DIR + '/' + name)
   127             eggs.append(TEMP_CONE_EGG_DIR + '/' + name)
   109     
   128     
   110     # Run easy_install to install the eggs
   129     # Run easy_install to install the eggs
   111     for egg in eggs:
   130     for egg in eggs:
   112         log.debug(egg)
   131         log.debug(egg)
   113         
   132         
   114         if sys.platform == "win32":
   133         if PLATFORM_SUBDIR == 'win': 
   115             platform_args = ["--always-copy"]
   134             command = ['easy_install-%s' % python_version,
       
   135                        '--allow-hosts None',
       
   136                        '--find-links install-temp/dep-eggs',
       
   137                        '--install-dir "%s"' % LIB_DIR,
       
   138                        '--script-dir "%s"' % SCRIPT_DIR,
       
   139                        '--site-dirs "%s"' % LIB_DIR,
       
   140                        '--always-copy',
       
   141                        '--always-unzip']
   116         else:
   142         else:
   117             platform_args = ["--no-deps"]
   143             command = ['easy_install-%s' % python_version,
   118                     
   144                        '--allow-hosts None',
   119         command = ['easy_install',
   145                        '--find-links install-temp/dep-eggs',
   120                    '--allow-hosts None',
   146                        '--install-dir "%s"' % LIB_DIR,
   121                    '--find-links install-temp/dep-eggs',
   147                        '--script-dir "%s"' % SCRIPT_DIR,
   122                    '--install-dir "%s"' % LIB_DIR,
   148                        '--site-dirs "%s"' % LIB_DIR,
   123                    '--script-dir "%s"' % SCRIPT_DIR,
   149                        '--always-unzip']
   124                    '--site-dirs "%s"' % LIB_DIR]
   150 
   125         command.extend(platform_args)
       
   126         command.append('"' + egg + '"')
   151         command.append('"' + egg + '"')
   127         command = ' '.join(command)
   152         command = ' '.join(command)
   128         
   153         
   129         log.debug(command)
   154         log.debug(command)
   130         ok = utils.run_command(command, env_overrides={'PYTHONPATH': LIB_DIR})
   155         ok = utils.run_command(command, env_overrides={'PYTHONPATH': LIB_DIR})
   131         if not ok:
   156         if not ok:
   132             raise BuildFailedError()
   157             raise BuildFailedError()
   133 
   158 
   134 def develop_install_cone_sources(source_paths, target_dir, python_version):
   159 def develop_install_cone_sources(source_paths, target_dir, python_version, python_executable):
   135     log.info("Installing ConE sources in develop mode...")
   160     log.info("Installing ConE sources in develop mode...")
   136     LIB_DIR, SCRIPT_DIR = init_target_dir(target_dir, python_version)
   161     LIB_DIR, SCRIPT_DIR = init_target_dir(target_dir, python_version)
   137     
   162     
   138     orig_workdir = os.getcwd()
   163     orig_workdir = os.getcwd()
   139     try:
   164     try:
   140         for source_path in source_paths:
   165         for source_path in source_paths:
   141             os.chdir(source_path)
   166             os.chdir(source_path)
   142             command = ['python setup.py develop',
   167             command = ['%s setup.py develop' % python_executable,
   143                    '--allow-hosts None',
   168                    '--allow-hosts None',
   144                    '--find-links "%s"' % os.path.normpath(os.path.join(ROOT_PATH, 'install-temp/dep-eggs')),
   169                    '--find-links "%s"' % os.path.normpath(os.path.join(ROOT_PATH, 'install-temp/dep-eggs')),
   145                    '--install-dir "%s"' % LIB_DIR,
   170                    '--install-dir "%s"' % LIB_DIR,
   146                    '--script-dir "%s"' % SCRIPT_DIR,
   171                    '--script-dir "%s"' % SCRIPT_DIR,
   147                    '--site-dirs "%s"' % LIB_DIR,
   172                    '--site-dirs "%s"' % LIB_DIR,
   152             if not ok:
   177             if not ok:
   153                 raise BuildFailedError()
   178                 raise BuildFailedError()
   154     finally:
   179     finally:
   155         os.chdir(orig_workdir)
   180         os.chdir(orig_workdir)
   156 
   181 
   157 def perform_build(target_dir, plugin_package, install_type, python_version):
   182 def perform_build(target_dir, plugin_package, install_type, python_version, python_executable):
   158     log.info("Target directory: %s" % target_dir)
   183     log.info("Target directory:  %s" % target_dir)
   159     log.info("Plug-in package:  %r" % plugin_package)
   184     log.info("Plug-in package:   %r" % plugin_package)
   160     log.info("Python version:   %s" % python_version)
   185     log.info("Python version:    %s" % python_version)
       
   186     log.info("Python executable: %s" % python_executable)
   161 
   187 
   162     # Retrieve dependencies to the correct location
   188     # Retrieve dependencies to the correct location
   163     retrieve_dep_eggs(plugin_package)
   189     retrieve_dep_eggs(plugin_package)
   164     
   190     
   165     # Find paths to the sources to install
   191     # Find paths to the sources to install
   167     
   193     
   168     log.info("Creating install directory...")
   194     log.info("Creating install directory...")
   169     if not os.path.exists(target_dir):
   195     if not os.path.exists(target_dir):
   170         os.makedirs(target_dir)
   196         os.makedirs(target_dir)
   171     
   197     
       
   198     if install_type == 'build':
       
   199         build_cone_eggs(source_paths, python_executable)
   172     if install_type == 'install':
   200     if install_type == 'install':
   173         build_cone_eggs(source_paths)
   201         build_cone_eggs(source_paths, python_executable)
   174         install_cone_eggs(target_dir, python_version)
   202         install_cone_eggs(target_dir, python_version)
   175     else:
   203     else:
   176         develop_install_cone_sources(source_paths, target_dir, python_version)
   204         develop_install_cone_sources(source_paths, target_dir, python_version, python_executable)
   177     
   205     
   178     # Copy RELEASE.txt
   206     # Copy RELEASE.txt
   179     utils.copy_file(
   207     utils.copy_file(
   180         source_path = os.path.join(SOURCE_ROOT, '..', 'RELEASE.TXT'),
   208         source_path = os.path.join(SOURCE_ROOT, '..', 'RELEASE.TXT'),
   181         target_path = os.path.join(target_dir, 'cone', 'RELEASE.TXT'))
   209         target_path = os.path.join(target_dir, INSTALL_SUBDIR, 'RELEASE.TXT'))
   182     
   210     
   183     # Copy cone.cmd or cone.sh, depending on the platform
   211     # Copy cone.cmd or cone.sh, depending on the platform
   184     if sys.platform == "win32":
   212     if sys.platform == "win32":
   185         filename = "cone.cmd"
   213         sourcefile = targetfile = "cone.cmd"
   186     else:
   214     else:
   187         filename = "cone.sh"
   215         sourcefile = "cone.sh"
   188     log.info("Copying %s" % filename)
   216         targetfile = "cone"
       
   217     log.info("Copying %s" % sourcefile)
   189     utils.copy_file(
   218     utils.copy_file(
   190         source_path = os.path.join(SOURCE_ROOT, filename),
   219         source_path = os.path.join(SOURCE_ROOT, sourcefile),
   191         target_path = target_dir)
   220         target_path = os.path.join(target_dir, targetfile))
   192 
   221 
   193 def main():
   222 def main():
   194     parser = optparse.OptionParser()
   223     parser = optparse.OptionParser()
   195     parser.add_option("-t", "--target-dir",
   224     parser.add_option("-t", "--target-dir",
   196                       help="The directory where ConE is to be installed.")
   225                       help="The directory where ConE is to be installed.")
   198                       help="The plug-in package to include in the installation.",\
   227                       help="The plug-in package to include in the installation.",\
   199                       default=None)
   228                       default=None)
   200     parser.add_option("-i", "--install-type",\
   229     parser.add_option("-i", "--install-type",\
   201                       help="The installation type, can be 'install' (the default) or 'develop'.",\
   230                       help="The installation type, can be 'install' (the default) or 'develop'.",\
   202                       default='install')
   231                       default='install')
       
   232     parser.add_option("--python-executable",\
       
   233                       help="The Python executable to run type, defaults to 'python'.",\
       
   234                       default='python')
   203     (options, args) = parser.parse_args()
   235     (options, args) = parser.parse_args()
   204     if options.target_dir is None:
   236     if options.target_dir is None:
   205         parser.error("Target directory must be given")
   237         parser.error("Target directory must be given")
   206     if options.install_type not in ('install', 'develop'):
   238     if options.install_type not in ('install', 'build', 'develop'):
   207         parser.error("Invalid install type ('%s')" % options.install_type)
   239         parser.error("Invalid install type ('%s')" % options.install_type)
   208     
   240         
   209     if not utils.run_command("python --help"):
   241     if not utils.run_command("%s --help" % options.python_executable):
   210         log.critical("Could not run 'python'. Please make sure that you "\
   242         log.critical("Could not run '%s'. Please make sure that you "\
   211                      "have Python installed and in your path.")
   243                      "have Python installed and in your path." % options.python_executable)
   212         return 1
   244         return 1
   213     
   245     
   214     if not utils.run_command("easy_install --help"):
   246     python_version = utils.get_python_version(options.python_executable)
   215         log.critical("Could not run 'easy_install'. Please make sure that you "\
   247     
   216                      "have setuptools installed and the Python scripts directory in your path.")
   248     easy_install_cmd = "easy_install-%s" % python_version
       
   249     if not utils.run_command("%s --help" % easy_install_cmd):
       
   250         log.critical("Could not run '%s'. Please make sure that you "\
       
   251                      "have setuptools installed and the Python scripts directory in your path."\
       
   252                      % easy_install_cmd)
   217         return 1
   253         return 1
   218     
   254     
   219     python_version = utils.get_python_version()
   255     
   220     
   256     
   221     try:
   257     try:
   222         perform_build(options.target_dir, options.plugin_package, options.install_type, python_version)
   258         perform_build(options.target_dir, options.plugin_package, options.install_type, python_version, options.python_executable)
   223     except BuildFailedError:
   259     except BuildFailedError:
   224         return 1
   260         return 1
   225     
   261     
   226     return 0
   262     return 0
   227 
   263