1# -*- coding: utf-8 -*- 2 3import sys 4import os 5import re 6from pprint import pformat, pprint 7from distutils import log 8from distutils import sysconfig 9from configuration.util import execute, get_function, re_search, check_and_store, read_rcfile, prefix2etc 10 11 12def get_version_number(prefix): 13 # try for version >= 1.8 14 version = get_version_number_last(prefix) 15 if version: 16 log.info("previous installed version is %s (>= 1.8)", version) 17 return version 18 # try for version >= 1.7.5 19 version = get_version_number_1_7_5(prefix) 20 if version: 21 log.info("previous installed version is %s (>= 1.7.5)", version) 22 return version 23 # try for version <= 1.7.4 24 version = get_version_number_older(prefix) 25 if version: 26 log.info("previous installed version is %s", version) 27 return version 28 if not version: 29 log.warn("warning: can not get currently installed version!") 30 return version 31 32 33def get_version_number_last(prefix): 34 version = None 35 try: 36 path = sysconfig.get_python_lib(prefix=prefix) 37 cmd = [sys.executable, '-c', 38 "import sys ; sys.path.insert(0, '%s') ; import asrun ; "\ 39 "print 'as_run # %%s # %%s' %% (asrun.__version__, asrun.__file__)" % path] 40 output = execute(cmd) 41 log.debug("get_version_number >= 1.8, output :\n%s", output) 42 out = output.split('#') 43 if len(out) != 3: 44 return version 45 version, asrfile = [s.strip() for s in out[1:3]] 46 asrfile = re.sub("/asrun/__init__.py.*$", "", asrfile) 47 if asrfile.replace(path, '') != '': 48 raise Exception("unexpected asrun found :%s" % asrfile) 49 except Exception as msg: 50 log.debug("get_version_number >= 1.8, message : %s", str(msg)) 51 return version 52 53 54def get_version_number_1_7_5(prefix): 55 version = None 56 try: 57 path = os.path.join(prefix, 'ASTK', 'ASTK_SERV', 'lib') 58 assert os.path.exists(os.path.join(path, '__pkginfo__.py')), 'module not found: %s' % path 59 cmd = [sys.executable, '-c', 60 "import sys ; sys.path.insert(0, '%s') ; import __pkginfo__ as p ; print 'as_run', p.version" % path] 61 output = execute(cmd) 62 log.debug("get_version_number >= 1.7.5, output :\n%s", output) 63 mat = re.search('as_run +([0-9\-\.A-Za-z_]+)', output) 64 version = mat.group(1) 65 except Exception as msg: 66 log.debug("get_version_number >= 1.7.5, message : %s", str(msg)) 67 return version 68 69 70def get_version_number_older(prefix): 71 version = None 72 try: 73 context = { 'path' : os.path.join(prefix, 'ASTK', 'ASTK_SERV', 'bin') } 74 cmd = ['%(path)s/as_run' % context, '--version'] 75 output = execute(cmd) 76 log.debug("get_version_number <= 1.7.4, output :\n%s", output) 77 mat = re.search('as_run +([0-9\-\.A-Za-z_]+)', output) 78 version = mat.group(1) 79 except Exception as msg: 80 log.debug("get_version_number <= 1.7.4, message : %s", str(msg)) 81 return version 82 83 84def get_parameters(version, where, prefix=None): 85 """Return the dict of installation parameters. 86 """ 87 if where is None: 88 where = '' 89 if prefix is None: 90 prefix = where 91 if not version: 92 version = 'unknown' 93 log.info("searching for installation parameters of version %s...", version) 94 log.debug(" from %s", where) 95 log.debug(" destination %s", prefix) 96 # default values 97 parameters = { 98 'ASTER_ROOT' : prefix, 99 'ASTER_VERSION' : 'testing', 100 'SHELL_EXECUTION' : '/bin/bash', 101 'WISH_EXE' : 'wish', 102 'PYTHON_EXE' : sys.executable, 103 'HOME_PYTHON' : sys.prefix, 104 'server_confdir' : '', 105 'server_conf_values' : {}, 106 'client_confdir' : '', 107 } 108 try: 109 from external_configuration import parameters as ext_param 110 assert type(ext_param) is dict, 'invalid type for external_configuration dict : %s' % type(ext_param) 111 parameters.update(ext_param) 112 except (ImportError, AssertionError) as error: 113 log.info(str(error)) 114 pass 115 parameters.update({ 116 'TOOLS_DIR' : os.path.join('$ASTER_ROOT', 'outils'), 117 'ASRUN_SITE_PKG' : sysconfig.get_python_lib(prefix='$ASTER_ROOT'), 118 }) 119 if where: 120 # SHELL_EXECUTION 121 func_shell(parameters, where, version) 122 # WISH_EXE 123 func_wish(parameters, where, version) 124 # add config files location 125 func_config_files(parameters, where, version) 126 # read config file 127 func_config(parameters, where, version) 128 # for auto_update.cron 129 func_config_auto_update(parameters, where) 130 # print dict 131 log.info("parameters values :") 132 pprint(parameters) 133## log.info(pformat(parameters)) # python-Bugs-1541642 134 return parameters 135 136 137def func_shell(param, where, version): 138 files = ( ('0.0.0', os.path.join(where, 'ASTK', 'ASTK_SERV', 'bin', 'as_run')), 139 ('1.8.0', os.path.join(where, 'bin', 'as_run')) ) 140 mat = re_search(get_function(files, version), '^#!(.*)') 141 check_and_store(param, mat, 'SHELL_EXECUTION') 142 143 144def func_wish(param, where, version): 145 files = ( ('0.0.0', os.path.join(where, 'ASTK', 'ASTK_CLIENT', 'bin', 'astk')), 146 ('1.8.0', os.path.join(where, 'etc', 'codeaster', 'profile.sh')) ) 147 expressions = ( ('0.0.0', '(.*) +.*/ASTK/ASTK_CLIENT/lib/ASTK/astk.tcl'), 148 ('1.8.0', 'WISHEXECUTABLE=(.*)') ) 149 mat = re_search(get_function(files, version), get_function(expressions, version)) 150 check_and_store(param, mat, 'WISH_EXE') 151 152 153def func_config(param, where, version): 154 """Store values to configure each file ('new' in the loop)""" 155 confdir = param['server_confdir'] 156 if not confdir: 157 return 158 per_file = param['server_conf_values'] 159 for new, config_file in list(param['server_config_files'].items()): 160 if not os.path.isfile(config_file): 161 log.warn("warning: file not found: %s", config_file) 162 continue 163 log.info("reading configuration file: %s", config_file) 164 per_file[new] = {} 165 read_rcfile(config_file, per_file[new]) 166 # if the philosophy of a field changes, it should be written here. 167 168def func_config_auto_update(param, where): 169 """Store the parameters set in auto_update.cron""" 170 dico = param['server_conf_values']['auto_update.cron'] = {} 171 fname = os.path.join(where, 'bin', 'auto_update.cron') 172 for var in ('VERSIONS_TO_UPDATE', 'VERSIONS_TO_BUILD', 'MAIL_DEST'): 173 mat = re_search(fname, '%s=(.*)' % var) 174 check_and_store(dico, mat, var) 175 176def func_config_files(param, where, version): 177 server = ( ('0.0.0', os.path.join(where, 'ASTK', 'ASTK_SERV', 'conf')), 178 ('1.8.0', os.path.join(prefix2etc(where), 'codeaster')) ) 179 client = ( ('0.0.0', os.path.join(where, 'ASTK', 'ASTK_CLIENT', 'lib', 'ASTK', 'astkrc')), 180 ('1.8.0', os.path.join(prefix2etc(where), 'codeaster', 'astkrc')) ) 181 param['server_confdir'] = get_function(server, version) 182 param['client_confdir'] = get_function(client, version) 183 # server 184 files = ('asrun', 'aster', '.mysql_connect_REX') 185 exceptions = ( ('0.0.0', { 'asrun' : 'config', 186 'aster' : 'config', }), 187 ('1.8.0', {}) ) 188 dchg = get_function(exceptions, version) 189 dpair = {} 190 for name in files: 191 dpair[name] = os.path.join(param['server_confdir'], dchg.get(name, name)) 192 param['server_config_files'] = dpair 193 194 # client 195 files = ('config_serveurs', 'outils', 'prefs') 196 exceptions = ( ('0.0.0', {}), 197 ('1.8.0', {}) ) 198 dchg = get_function(exceptions, version) 199 dpair = {} 200 for name in files: 201 dpair[os.path.join('astkrc', name)] = \ 202 os.path.join(param['client_confdir'], dchg.get(name, name)) 203 param['client_config_files'] = dpair 204 205 206