Welcome    Usage    Browse    Find CID    Search     Log in

cM API Documentation

module.py

Go to the documentation of this file.
00001 #
00002 # Collective Mind
00003 #
00004 # See cM LICENSE.txt for licensing details.
00005 # See cM Copyright.txt for copyright details.
00006 #
00007 # Developer(s): (C) Grigori Fursin, started on 2011.09
00008 #
00009 
00010 # Should always be here
00011 ini={}
00012 cm_kernel=None
00013 
00014 # Local settings
00015 import os
00016 import sys
00017 import time
00018 import json
00019 import copy
00020 
00021 # ============================================================================
00022 def init(i):
00023     return {'cm_return':0}
00024 
00025 # ============================================================================
00026 def change_work_dir(i):
00027 
00028     """
00029     Chaning working directory
00030 
00031     Input:  {
00032               (work_dir)                 - change to this working directory before running code
00033                   or
00034               (work_dir_repo_uoa)        - change to the working directory in the repository (repo UOA)
00035               (work_dir_module_uoa)      - change to the working directory in the repository (module UOA)
00036               (work_dir_data_uoa)        - change to the working directory in the repository (data UOA)
00037             }
00038 
00039     Output: {
00040               cm_return                 - if =0, success
00041               work_dir                  - selected working directory 
00042             }
00043     """
00044 
00045     # Changing to working directory
00046     sys.stdout.flush()
00047     work_dir=''
00048     if 'work_dir' in i: work_dir=i['work_dir']
00049     elif ('work_dir_module_uoa' in i and 'work_dir_data_uoa' in i):
00050        # Change path to another directory
00051        ii={'cm_run_module_uoa':i['work_dir_module_uoa'],
00052            'cm_action':'load',
00053            'cm_data_uoa':i['work_dir_data_uoa']}
00054        if 'work_dir_repo_uoa' in i: ii['cm_repo_uoa']=i['work_dir_repo_uoa']
00055        r=cm_kernel.access(ii)
00056        if r['cm_return']>0: return r
00057        work_dir=r['cm_path']
00058 
00059     if work_dir!='':
00060        cm_kernel.print_for_con('')
00061        cm_kernel.print_for_con('Changing path to '+work_dir+' ...')
00062        os.chdir(work_dir)
00063 
00064     return {'cm_return':0, 'work_dir':work_dir}
00065 
00066 # ============================================================================
00067 def get_env(i):
00068 
00069     """
00070     cM web index
00071 
00072     Input:  {
00073               cm_data_uoa - code UOA to prepare environment script name
00074               os_uoa      - OS configuration to prepare extension
00075             }
00076 
00077     Output: {
00078               cm_return  - return code >0 if error
00079             }
00080     """
00081 
00082     if 'os_uoa' not in i or i['os_uoa']=='':
00083        return {'cm_return':1, 'cm_error': '"os_uoa" is not defined for "get_env" in "code" module'}
00084 
00085     # Load OS
00086     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00087         'cm_action':'load',
00088         'cm_data_uoa':i['os_uoa']}
00089     r=cm_kernel.access(ii)
00090     if r['cm_return']>0: return r
00091 
00092     os_cfg=r['cm_data_obj']['cfg']
00093 
00094     # Prepare script
00095     s=ini['cfg']['cm_env_prefix']+i['cm_data_uoa']+os_cfg['script_ext']
00096 
00097     return {'cm_return':0, 'cm_string':s}
00098 
00099 # ============================================================================
00100 def run(i):
00101 
00102     """
00103     Run code (binary or script)
00104 
00105     Input:  {
00106               (run_host_os_uoa)          - host OS UOA (if not set, use default OS from kernel)
00107               (run_target_os_uoa)        - target OS UOA (if not set, use run_host_uoa_os)
00108 
00109               (run_target_processor_uoa) - target processor UOA (not strictly needed - can add some helper parameters before executing code)
00110 
00111               --------------------------------------------------------------------------------------------
00112               run_cmd_key                - if !='', set all further run parameters (including target name) 
00113                                            from the work_dir_data_uoa (must be set)
00114               (dataset_uoa)              - use dataset to update cmd params from above
00115                  or
00116               run_cmd                    - command line to run
00117                  or
00118               run_script                 - use the following script (add extension from run_target_os)
00119               run_script_uoa             - take script from this entry in os.script (add extension from run_target_os)
00120                  or
00121               (run_cmd1)                 - following format: run_cmd1 run_cmd_name|binary_name run_cmd2 run_cmd_main run_cmd3 > run_cmd_out1 2> run_cmd_out2
00122               (run_cmd_name)
00123               (run_cmd2)
00124               (run_cmd_main) or (binary_name)
00125               (run_cmd3)
00126               (run_cmd_out1)
00127               (run_cmd_out2)
00128               --------------------------------------------------------------------------------------------
00129 
00130               (work_dir)                 - change to this working directory before running code
00131                   or
00132               (work_dir_repo_uoa)        - change to the working directory in the repository (repo UOA)
00133               (work_dir_module_uoa)      - change to the working directory in the repository (module UOA)
00134               (work_dir_data_uoa)        - change to the working directory in the repository (data UOA)
00135 
00136               (run_input_files)          - list of files needed to run code (can be copied to the remote host system)
00137               (run_output_files)         - list of files that will be created (for validation, etc)
00138 
00139               (code_deps)                - list with code UOA for dependencies [{"index":"uoa"} ...]
00140               (skip_code_deps)           - if 'yes', skip code deps
00141 
00142               (run_set_env1)             - array with environment variables to be set before code deps
00143               (run_set_env2)             - array with environment variables to be set before executable
00144 
00145               (run_commands_before)      - run commands before executable
00146               (run_commands_after)       - run commands after executable
00147 
00148               (cm_dependencies)          - dependencies that set all other dependencies (code/include/lib).
00149                                            Format [{"class UOA":"code UOA"},{},...]
00150 
00151               (run_timeout)              - time out for run (overheads may occur)
00152 
00153               (keep_all_files)           - if 'yes', do not remove tmp files and do not clean up remote device
00154               (cm_verbose)               - if 'yes', print all info
00155             }
00156 
00157     Output: {
00158               cm_return          - return code = 0, if successful
00159 
00160               timed_out          - 'yes' if timed out
00161               failed_run         - 'yes' if return code !=0 or timed out or binary doesn't exist
00162               run_time_by_module - run time measured by module
00163               exit_code          - exit code of the system call
00164               run_cmd            - final run cmd
00165               work_dir           - work directory
00166               run_output_files   - final output files
00167             }
00168     """
00169 
00170     # Check verbose - yes, by default
00171     vrb=i.get('cm_verbose','yes')
00172 
00173     if vrb=='yes':
00174 #       cm_kernel.print_for_con('***********************************************')
00175        cm_kernel.print_for_con('Running code ...')
00176 
00177     # Load OS configuration
00178     host_os_uoa=''
00179     if 'run_host_os_uoa' in i and i['run_host_os_uoa']!='': host_os_uoa=i['run_host_os_uoa']
00180     elif 'cm_default_os_uoa' in cm_kernel.ini['dcfg'] and cm_kernel.ini['dcfg']['cm_default_os_uoa']!='':
00181        host_os_uoa=cm_kernel.ini['dcfg']['cm_default_os_uoa']
00182 
00183     if host_os_uoa=='':
00184        return {'cm_return':1, 'cm_error':'"host_os_uoa" is not set and not in kernel'}
00185 
00186     target_os_uoa=''
00187     if 'run_target_os_uoa' in i and i['run_target_os_uoa']!='': target_os_uoa=i['run_target_os_uoa']
00188     elif host_os_uoa!='': target_os_uoa=host_os_uoa
00189 
00190     if vrb=='yes':
00191        sys.stdout.flush()
00192        cm_kernel.print_for_con('')
00193        cm_kernel.print_for_con('Loading host os '+host_os_uoa+' ...')
00194     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00195         'cm_action':'load',
00196         'cm_data_uoa':host_os_uoa}
00197     r=cm_kernel.access(ii)
00198     if r['cm_return']>0: return r
00199 
00200     host_os_cfg=r['cm_data_obj']['cfg']
00201     host_os_path=r['cm_path']
00202     host_os_uid=r['cm_uid']
00203     host_os_alias=r['cm_alias']
00204 
00205     if vrb=='yes':
00206        cm_kernel.print_for_con('')
00207        cm_kernel.print_for_con('Loading target os '+target_os_uoa+' ...')
00208     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00209         'cm_action':'load',
00210         'cm_data_uoa':target_os_uoa}
00211     r=cm_kernel.access(ii)
00212     if r['cm_return']>0: return r
00213 
00214     target_os_cfg=r['cm_data_obj']['cfg']
00215     target_os_path=r['cm_path']
00216     target_os_uid=r['cm_uid']
00217     target_os_alias=r['cm_alias']
00218 
00219     # Dependencies
00220     code_deps=i.get('code_deps',[])
00221 
00222     # Check if code_deps set additional parameters
00223     qq=i.get('cm_dependencies',{})
00224     if len(qq)>0:
00225        if vrb=='yes':
00226           cm_kernel.print_for_con('')
00227           cm_kernel.print_for_con('Updating code dependencies from cm_dependencies ...')
00228 
00229        code_deps1=[]
00230 
00231        for q in i.get('cm_dependencies',{}):
00232            yy=q.keys()[0]
00233            x=q[yy]
00234 
00235            jj={'cm_run_module_uoa':ini['cfg']['cm_modules']['class'],
00236                'cm_data_uoa':yy,
00237                'cm_action':'load'}
00238            rj=cm_kernel.access(jj)
00239            if rj['cm_return']>0: return rj
00240 
00241            drj=rj['cm_data_obj']['cfg']
00242 
00243            vcd=drj.get('build_code_deps_var','')
00244            if vcd!='': code_deps1.append({vcd:x})
00245 
00246        for q in code_deps: code_deps1.append(q)
00247 
00248        code_deps=code_deps1
00249 
00250     i['code_deps']=code_deps
00251 
00252     processor_params={}
00253     if 'run_target_processor_uoa' in i and i['run_target_processor_uoa']!='':
00254        if vrb=='yes':
00255           cm_kernel.print_for_con('')
00256           cm_kernel.print_for_con('Loading target processor '+i['run_target_processor_uoa']+' ...')
00257        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['processor'],
00258            'cm_action':'load',
00259            'cm_data_uoa':i['run_target_processor_uoa']}
00260        r=cm_kernel.access(ii)
00261        if r['cm_return']>0: return r
00262 
00263        target_processor_cfg=r['cm_data_obj']['cfg']
00264        target_processor_path=r['cm_path']
00265        target_processor_uid=r['cm_uid']
00266        target_processor_alias=r['cm_alias']
00267 
00268        if 'family' in target_processor_cfg:
00269           processor_params['CM_PROCESSOR_FAMILY']=target_processor_cfg['family']
00270        if 'architecture' in target_processor_cfg:
00271           processor_params['CM_PROCESSOR_ARCH']=target_processor_cfg['architecture']
00272        if 'bits' in target_processor_cfg:
00273           processor_params['CM_PROCESSOR_BITS']=target_processor_cfg['bits']
00274 
00275     # Changing to working directory
00276     sys.stdout.flush()
00277     work_dir=''
00278     if 'work_dir' in i: work_dir=i['work_dir']
00279     elif ('work_dir_module_uoa' in i and 'work_dir_data_uoa' in i):
00280        # Change path to another directory
00281        ii={'cm_run_module_uoa':i['work_dir_module_uoa'],
00282            'cm_action':'load',
00283            'cm_data_uoa':i['work_dir_data_uoa']}
00284        if 'work_dir_repo_uoa' in i: ii['cm_repo_uoa']=i['work_dir_repo_uoa']
00285        r=cm_kernel.access(ii)
00286        if r['cm_return']>0: return r
00287        work_dir=r['cm_path']
00288 
00289     if work_dir!='':
00290        if vrb=='yes':
00291           cm_kernel.print_for_con('')
00292           cm_kernel.print_for_con('Changing path to '+work_dir+' ...')
00293        os.chdir(work_dir)
00294 
00295     e1a={}
00296 
00297     # Check if remote
00298     remote=False
00299     if target_os_cfg.get('remote','')=='yes': 
00300        remote=True
00301        files=[]
00302 
00303     # Check vars
00304     run_input_files=i.get('run_input_files',[])
00305 
00306     run_name=''
00307     run_cmd=''
00308 
00309     if i.get('run_script','')!='':
00310        if vrb=='yes':
00311           cm_kernel.print_for_con('')
00312           cm_kernel.print_for_con('Using script '+i['run_script'])
00313 
00314        r=prepare_sub_script({'run_script':i['run_script'],
00315                              'run_script_uoa':i.get('run_script_uoa',''),
00316                              'target_os_cfg':target_os_cfg,
00317                              'run_cmd':i.get('run_cmd',''),
00318                              'run_cmd_out1':i.get('run_cmd_out1',''),
00319                              'run_cmd_out2':i.get('run_cmd_out2','')})
00320        if r['cm_return']>0: return r
00321        run_cmd=r['run_cmd']
00322     elif i.get('run_cmd','')!='':
00323        run_cmd=i['run_cmd']
00324 
00325        if i.get('run_cmd_out1','')!='': run_cmd+=' 1>'+i['run_cmd_out1']
00326        if i.get('run_cmd_out2','')!='': run_cmd+=' 2>'+i['run_cmd_out2']
00327     elif i.get('binary_name','')!='' or i.get('run_cmd_name','')!='' or i.get('run_cmd_key','')!='': 
00328        run_time={}
00329 
00330        rcmd=''
00331        if i.get('run_cmd_key','')!='':
00332           r=prepare_cmd({'code_module_uoa':i.get('work_dir_module_uoa',''),
00333                          'code_data_uoa':i.get('work_dir_data_uoa',''),
00334                          'run_cmd_key':i.get('run_cmd_key',''),
00335                          'dataset_uoa':i.get('dataset_uoa',''),
00336                          'os_uoa':target_os_uoa})
00337           if r['cm_return']>0: return r
00338           run_time=r['run_time']
00339           for j in run_time.get('run_input_files',[]):
00340               if j not in run_input_files: run_input_files.append(j)
00341 
00342           run_set_env2=i.get('run_set_env2',{})
00343           if len(run_time.get('run_set_env2',{}))>0: run_set_env2.update(run_time['run_set_env2'])
00344           i['run_set_env2']=run_set_env2
00345 
00346           if run_time.get('run_script','')!='':
00347              if vrb=='yes':
00348                 cm_kernel.print_for_con('')
00349                 cm_kernel.print_for_con('Using script '+run_time['run_script'])
00350 
00351              ii={'run_script':run_time['run_script'],
00352                  'run_script_uoa':run_time.get('run_script_uoa',''),
00353                  'target_os_cfg':target_os_cfg,
00354                  'run_cmd':i.get('run_cmd',''),
00355                  'run_cmd_out1':i.get('run_cmd_out1',''),
00356                  'run_cmd_out2':i.get('run_cmd_out2','')}
00357              if ii['run_cmd_out1']=='': ii['run_cmd_out1']=run_time.get('run_cmd_out1','')
00358              if ii['run_cmd_out2']=='': ii['run_cmd_out2']=run_time.get('run_cmd_out2','')
00359              r=prepare_sub_script(ii)
00360              if r['cm_return']>0: return r
00361              rcmd=r['run_cmd']
00362 
00363        if i.get('run_cmd1','')!='': run_time['run_cmd1']=i['run_cmd1']
00364        if i.get('binary_name','')!='': run_time['binary_name']=i['binary_name']
00365        elif i.get('run_cmd_name','')!='': run_time['run_cmd_name']=i['run_cmd_name']
00366        if i.get('run_cmd2','')!='': run_time['run_cmd2']=i['run_cmd2']
00367        if i.get('run_cmd_main','')!='': run_time['run_cmd_main']=i['run_cmd_main']
00368        if i.get('run_cmd_out1','')!='': run_time['run_cmd_out1']=i['run_cmd_out1']
00369        if i.get('run_cmd_out2','')!='': run_time['run_cmd_out2']=i['run_cmd_out2']
00370 
00371        if len(run_time.get('run_output_files',[]))>0 and ('run_output_files' not in i or len(i['run_output_files'])==0):
00372           i['run_output_files']=run_time['run_output_files']
00373 
00374        run_cmd=''
00375        run_name=''
00376        if 'run_cmd1' in run_time:         run_cmd+=' '+run_time['run_cmd1']
00377        if 'binary_name' in run_time:      run_cmd+=' '+run_time['binary_name'];    run_name=run_time['binary_name']
00378        elif 'run_cmd_name' in run_time:   run_cmd+=' '+run_time['run_cmd_name'];   run_name=run_time['run_cmd_name']
00379        if 'run_cmd2' in run_time:         run_cmd+=' '+run_time['run_cmd2']
00380        if 'run_cmd_main' in run_time:     run_cmd+=' '+run_time['run_cmd_main']
00381        if 'run_cmd_out1' in run_time:     run_cmd+=' 1>'+run_time['run_cmd_out1']; i['run_cmd_out1']=run_time['run_cmd_out1']
00382        if 'run_cmd_out2' in run_time:     run_cmd+=' 2>'+run_time['run_cmd_out2']; i['run_cmd_out2']=run_time['run_cmd_out2']
00383 
00384        if rcmd!='':
00385           e1a['CM_RUN_CMD1']=run_time.get('run_cmd1','')
00386           e1a['CM_RUN_CMD2']=run_time.get('run_cmd2','')
00387           e1a['CM_RUN_NAME']=run_name
00388           e1a['CM_RUN_CMD_MAIN']=run_time.get('run_cmd_main','')
00389           run_cmd=rcmd
00390 
00391     else:
00392         return {'cm_return':1, 'cm_error':'not enough parameters to run code in "code run"'}
00393 
00394     if vrb=='yes':
00395        cm_kernel.print_for_con('')
00396        cm_kernel.print_for_con('Prepared command line:')
00397        cm_kernel.print_for_con('  '+run_cmd)
00398 
00399     # Check commands after
00400     run_commands_after=[]
00401     if 'run_commands_after' in i: run_commands_after=i['run_commands_after']
00402 
00403     # Check that executable exists
00404     if run_name!='' and not os.path.isfile(run_name):
00405        if vrb=='yes':
00406           cm_kernel.print_for_con('Can\'t find executable '+run_name)
00407        return {'cm_return':0, 'failed_run':'yes'}
00408 
00409     # Prepare environment before code deps
00410     # First, set fixed ones
00411     if 'family' in target_os_cfg: e1a['CM_OS_FAMILY']=target_os_cfg['family']
00412     if 'bits' in target_os_cfg: e1a['CM_OS_BITS']=target_os_cfg['bits']
00413     if 'version' in target_os_cfg: e1a['CM_OS_VERSION']=target_os_cfg['version']
00414     if 'lib_dir' in target_os_cfg: e1a['CM_OS_LIB_DIR']=target_os_cfg['lib_dir']
00415 
00416     if 'run_set_env1' in i: e1a.update(i['run_set_env1'])
00417     e1a.update(processor_params)
00418 
00419     # Prepare script name
00420     script=''
00421     if 'exec_prefix' in target_os_cfg and target_os_cfg['exec_prefix']!='': script+=target_os_cfg['exec_prefix']
00422     script+=ini['cfg']['cm_batch_script_ext']
00423 
00424     r=cm_kernel.gen_uid({})
00425     if r['cm_return']>0: return r
00426     script+=r['cm_uid']
00427 
00428     # Create script  *****************************************************************************************
00429     ii={'script_name':script,
00430         'target_os_uoa':target_os_uoa,
00431         'set_env1':e1a}
00432     if 'code_deps' in i and i.get('skip_code_deps','')!='yes':
00433        ii['code_deps']=i['code_deps']
00434     if 'run_set_env2' in i and i['run_set_env2']!='':  ii['set_env2']=i['run_set_env2']
00435     if 'run_commands_before' in i:                     ii['run_commands_before']=i['run_commands_before']
00436     if run_cmd!='':                                    ii['run_cmd']=run_cmd
00437     if len(run_commands_after)>0:                      ii['run_commands_after']=run_commands_after
00438     r=prepare_script(ii)
00439     if r['cm_return']>0: return r
00440     script=r['cm_path']
00441 
00442     # Check if remote
00443     if remote:
00444        fail=False
00445 
00446     if remote and 'remote_init' in target_os_cfg:
00447        sys.stdout.flush()
00448        if vrb=='yes':
00449           cm_kernel.print_for_con('')
00450           cm_kernel.print_for_con('Initializing remote device ...')
00451        p=target_os_cfg['remote_init']; cm_kernel.print_for_con(p); x=os.system(p)
00452        if x!=0: fail=True
00453 
00454        if not fail:
00455           if vrb=='yes':
00456              cm_kernel.print_for_con('')
00457              cm_kernel.print_for_con('Copying files to device ...')
00458           if target_os_cfg.get('no_script_execution','')!='yes': files.append(script)
00459           if run_name!='': files.append(run_name)
00460           if len(run_input_files)>0: 
00461              for x in run_input_files: 
00462                  if x not in files: files.append(x)
00463 
00464           for z in files: 
00465               filename=os.path.basename(z)
00466               p=target_os_cfg['remote_push']+' '+host_os_cfg['env_quotes']+z+host_os_cfg['env_quotes']+\
00467                 ' '+target_os_cfg['remote_dir']+target_os_cfg['dir_sep']+filename
00468               if vrb=='yes':
00469                  cm_kernel.print_for_con('  '+p)
00470               x=os.system(p)
00471               if x!=0: 
00472                  fail=True
00473                  if vrb=='yes':
00474                     cm_kernel.print_for_con('')
00475                     cm_kernel.print_for_con('Error: Couldn\'t copy all necessary files for a run!')
00476 
00477        if not fail and run_name!='' and 'set_executable' in target_os_cfg:
00478           if vrb=='yes':
00479              cm_kernel.print_for_con('')
00480              cm_kernel.print_for_con('Setting permissions for binary ...')
00481 
00482           filename=run_name
00483           if remote: filename=os.path.basename(run_name)
00484 
00485           p=target_os_cfg['remote_shell']+' '+target_os_cfg['set_executable']+' '+target_os_cfg['remote_dir']+\
00486             target_os_cfg['dir_sep']+filename
00487           if vrb=='yes':
00488              cm_kernel.print_for_con('  '+p)
00489           x=os.system(p)
00490           if x!=0: fail=True
00491 
00492           if target_os_cfg.get('no_script_execution', '')!='yes':
00493              p=target_os_cfg['remote_shell']+' '+target_os_cfg['set_executable']+' '+target_os_cfg['remote_dir']+\
00494                target_os_cfg['dir_sep']+script
00495              if vrb=='yes':
00496                 cm_kernel.print_for_con('  '+p)
00497              x=os.system(p)
00498              if x!=0: fail=True
00499 
00500     # Clean output files 
00501     if remote:
00502        # if remote and run_cmd_out1/run_cmd_out2 !='', add to run_output_files 
00503        # to get them from remote device
00504        if i.get('run_cmd_out1','')!='' or i.get('run_cmd_out2','')!='':
00505           if len(i.get('run_output_files',[]))==0: i['run_output_files']=[]
00506           if i.get('run_cmd_out1','')!='' and i['run_cmd_out1'] not in i['run_output_files']: i['run_output_files'].append(i['run_cmd_out1'])
00507           if i.get('run_cmd_out2','')!='' and i['run_cmd_out2'] not in i['run_output_files']: i['run_output_files'].append(i['run_cmd_out2'])
00508     else:
00509        if i.get('run_cmd_out1','')!='':
00510           try:
00511              os.remove(i['run_cmd_out1'])
00512           except:
00513              pass
00514        if i.get('run_cmd_out2','')!='':
00515           try:
00516              os.remove(i['run_cmd_out2'])
00517           except:
00518              pass
00519 
00520     if 'run_output_files' in i and len(i['run_output_files'])>0: 
00521        if vrb=='yes':
00522           sys.stdout.flush()
00523           cm_kernel.print_for_con('')
00524           cm_kernel.print_for_con('Deleting output files ...')
00525        for z in i['run_output_files']:
00526            if vrb=='yes':
00527               cm_kernel.print_for_con('  '+z)
00528 
00529            if remote:
00530               p=target_os_cfg['remote_shell']+' '+target_os_cfg['delete_file']+' '+host_os_cfg['env_quotes']+target_os_cfg['remote_dir']+\
00531                 target_os_cfg['dir_sep']+z+host_os_cfg['env_quotes']
00532               if vrb=='yes':
00533                  cm_kernel.print_for_con('     '+p)
00534               x=os.system(p)
00535               # Ignore errors
00536               # if x!=0: fail=True
00537 
00538            else:
00539               try:
00540                  os.remove(z)
00541               except:
00542                  pass
00543 
00544     # Run cmd
00545     cmd=''
00546     if target_os_cfg.get('no_script_execution','')=='yes':
00547        r=cm_kernel.load_array_from_file({'cm_filename':script})
00548        if r['cm_return']>0: return r
00549        a=r['cm_array']
00550        for x in a:
00551            xx=x.strip()
00552            if xx!='' and not xx.startswith(target_os_cfg['rem']):
00553               if cmd!='': cmd+=target_os_cfg['env_separator']+' '
00554               cmd+=xx
00555     else:
00556        # FGG: this was originally, (we used . ./script) but Abdul reported many problems 
00557        # on UBUNTU and Debian when running scripts with variable substitution (MILEPOST GCC, for example)
00558        # cmd=target_os_cfg['env_call']+' '+script
00559        if target_os_cfg.get('set_executable','')!='':
00560           cmd=target_os_cfg['set_executable']+' '+script+' '+target_os_cfg['env_separator']+' '+script
00561        else:
00562           # for Windows like
00563           cmd=target_os_cfg['env_call']+' '+script
00564 
00565     if remote:
00566        cmd=target_os_cfg['change_dir']+' '+target_os_cfg['remote_dir']+\
00567             target_os_cfg['env_separator']+' '+cmd
00568        if host_os_cfg.get('env_quotes_if_remote','')!='':
00569           cmdx1=cmd.replace(host_os_cfg['env_quotes_if_remote'], '\\'+host_os_cfg['env_quotes_if_remote'])
00570           cmdx=host_os_cfg['env_quotes_if_remote']+cmdx1+host_os_cfg['env_quotes_if_remote']
00571        else:
00572           cmdx=cmd
00573 
00574        if host_os_cfg.get('env_dollar_if_remote','')=='yes':
00575           cmdx=cmdx.replace('$','\\$')
00576 
00577        cmd=target_os_cfg['remote_shell']+' '+cmdx
00578 
00579     ii={'cmd':cmd}
00580     if 'run_timeout' in i and i['run_timeout']!='': ii['timeout']=i['run_timeout']
00581     r=-1
00582     fail=False
00583 
00584     sys.stdout.flush()
00585     if 'calming_delay' in i: 
00586        calming_delay=i['calming_delay']
00587     else:
00588        calming_delay=ini['cfg'].get('calming_delay','')
00589 
00590     if calming_delay!='' and float(calming_delay)!=0:
00591        if vrb=='yes':
00592           cm_kernel.print_for_con('')
00593           cm_kernel.print_for_con('Calming delay: '+calming_delay+' second(s) ...')
00594        time.sleep(float(calming_delay))
00595 
00596     if vrb=='yes':
00597        cm_kernel.print_for_con('')
00598        cm_kernel.print_for_con('Executing script:')
00599        cm_kernel.print_for_con('  '+cmd)
00600 
00601     start_time=time.time()
00602     rx=cm_kernel.system(ii)
00603     t=time.time()-start_time
00604 
00605     sys.stdout.flush()
00606 
00607     ii={'cm_return':0}
00608 
00609     if rx['cm_return']==1: # timeout
00610        ii.update({'timed_out':'yes', 'failed_run':'yes'})
00611     elif rx['cm_return']>0: return rx # Module error
00612     else: 
00613        ts="%.3f" % t
00614        rc=rx['cm_return_code']
00615        ii.update({'run_time_by_module':ts, 'exit_code':rc})
00616        if rc!=0: ii.update({'failed_run':'yes'})
00617 
00618     ii['run_cmd']=run_cmd
00619 
00620     # Moving back output files if needed
00621     if remote and 'run_output_files' in i and len(i['run_output_files'])>0: 
00622        if vrb=='yes':
00623           sys.stdout.flush()
00624           cm_kernel.print_for_con('')
00625           cm_kernel.print_for_con('Copying output files from remote device ...')
00626        for z in i['run_output_files']:
00627            if vrb=='yes':
00628               cm_kernel.print_for_con('  '+z)
00629 
00630            filename=os.path.basename(z)
00631            p=target_os_cfg['remote_pull']+' '+target_os_cfg['remote_dir']+target_os_cfg['dir_sep']+filename+\
00632              ' '+host_os_cfg['env_quotes']+z+host_os_cfg['env_quotes']
00633            if vrb=='yes':
00634               cm_kernel.print_for_con('  '+p)
00635            x=os.system(p)
00636            if x!=0: fail=True
00637 
00638     # Cleaning up tmp and other files
00639     if i.get('keep_all_files','')!='yes':
00640        if vrb=='yes':
00641           cm_kernel.print_for_con('')
00642           cm_kernel.print_for_con('Cleaning up local files ...')
00643        # clean up script if not from cM
00644 
00645        try:
00646           os.remove(script)
00647        except:
00648           pass
00649 
00650        if remote:
00651           if vrb=='yes':
00652              cm_kernel.print_for_con('')
00653              cm_kernel.print_for_con('Cleaning up files on remote device ...')
00654           # files already exist - add output files if needed
00655           if 'run_output_files' in i and len(i['run_output_files'])>0: 
00656              for x in i['run_output_files']: 
00657                  if x not in files: files.append(x)
00658 
00659           for z in files:
00660               filename=os.path.basename(z)
00661               p=target_os_cfg['remote_shell']+' '+target_os_cfg['delete_file']+' '+host_os_cfg['env_quotes']+target_os_cfg['remote_dir']+\
00662                 target_os_cfg['dir_sep']+filename+host_os_cfg['env_quotes']
00663               if vrb=='yes':
00664                  cm_kernel.print_for_con('     '+p); x=os.system(p)
00665 
00666     if vrb=='yes':
00667        cm_kernel.print_for_con('')
00668        cm_kernel.print_for_con('Run time (by module): '+ts)
00669 
00670     ii['work_dir']=work_dir
00671     ii['run_output_files']=i.get('run_output_files',[])
00672 
00673     if vrb=='yes':
00674        cm_kernel.print_for_con('')
00675        cm_kernel.print_for_con('Execution finished!')
00676 
00677     return ii
00678 
00679 # ============================================================================
00680 def prepare_env_for_all_codes(i):
00681 
00682     """
00683     Prepare environment for all codes
00684 
00685     Input:  {
00686               code_deps       - list with code UOA for dependencies [{"index":"uoa"} ...]
00687               os_uoa          - OS UOA
00688               no_strict_check - if 'yes', do not check if code dependency was installed properly
00689             }
00690 
00691     Output: {
00692               cm_return      - return code = 0, if successful
00693               cm_string      - environment string
00694               cm_array       - array with environment setting (for scripts)
00695               include_paths1 - list with paths with include directories
00696               lib_paths      - list with libraries 
00697               env_separator  - environment separator (just in case to avoid double loading of OS)
00698             }
00699     """
00700 
00701     # Check vars
00702     if 'code_deps' not in i: return {'cm_return':1, 'cm_error':'"code_deps" is not defined in "code prepare_env_for_all_codes"'}
00703 
00704     include_paths=[]
00705     lib_paths=[]
00706 
00707     # Load OS
00708     os_uoa=''
00709     if 'os_uoa' in i and i['os_uoa']!='': os_uoa=i['os_uoa']
00710     elif 'cm_default_os_uoa' in cm_kernel.ini['dcfg'] and cm_kernel.ini['dcfg']['cm_default_os_uoa']!='':
00711        os_uoa=cm_kernel.ini['dcfg']['cm_default_os_uoa']
00712 
00713     if os_uoa=='' not in i:
00714        return {'cm_return':1, 'cm_error':'"os_uoa" is not defined and not in kernel'}
00715 
00716     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00717         'cm_action':'load',
00718         'cm_data_uoa':os_uoa}
00719     r=cm_kernel.access(ii)
00720     if r['cm_return']>0: return r
00721 
00722     os_cfg=r['cm_data_obj']['cfg']
00723     os_path=r['cm_path']
00724     os_uid=r['cm_uid']
00725     os_alias=r['cm_alias']
00726 
00727     s_code_deps=''
00728     a_code_deps=[]
00729     if 'code_deps' in i:
00730        for xx in i['code_deps']:
00731            yy=xx.keys()[0]
00732            x=xx[yy]
00733 
00734            if x=='':
00735               return {'cm_return':1, 'cm_error':'dependency "'+yy+'" is empty, please check your input'}
00736 
00737            # Check if code was installed
00738            if i.get('no_strict_check','')!='yes':
00739               ii={'cm_run_module_uoa':ini['cm_module_uid'],
00740                   'cm_action':'load',
00741                   'cm_data_uoa':x}
00742               r=cm_kernel.access(ii)
00743               if r['cm_return']==16:
00744                  return {'cm_return':1, 'cm_error':'dependency is not resolved - code '+x+' ('+yy+') is not installed'}
00745               elif r['cm_return']>0: return r
00746               code_cfg=r['cm_data_obj']['cfg']
00747               if code_cfg.get('build_finished_successfully','')!='yes':
00748                  return {'cm_return':1, 'cm_error':'dependency is not resolved - code '+x+' ('+yy+') is not installed'}
00749 
00750               code_path=r['cm_path']
00751               include_paths.append(os.path.join(code_path, 'include'))
00752 
00753               if 'state_input' in code_cfg and \
00754                  'run_set_env2' in code_cfg['state_input'] and \
00755                  'CM_TARGET_FILE' in code_cfg['state_input']['run_set_env2']:
00756                  lib_paths.append(os.path.join(code_path, os_cfg['lib_dir'], 
00757                        code_cfg['state_input']['run_set_env2']['CM_TARGET_FILE']))
00758 
00759            # Environment script
00760            r=get_env({'cm_data_uoa':x, 'os_uoa':os_uoa})
00761            if r['cm_return']>0: return r
00762 
00763 #           z=os_cfg['env_call']+' '+os.path.join(cm_kernel.ini[cm_kernel.env_cm_bin],r['cm_string'])
00764            z1=os_cfg['env_set']+' '+yy+'='+os_cfg['env_quotes']+x+os_cfg['env_quotes']
00765            z=os_cfg['env_call']+' '+r['cm_string']
00766 
00767            if s_code_deps!='': s_code_deps+=' '+os_cfg['env_separator']+' '
00768            s_code_deps+=z1
00769            if s_code_deps!='': s_code_deps+=' '+os_cfg['env_separator']+' '
00770            s_code_deps+=z
00771            # FGG added again setting environment variable since calling other scripts can change it
00772            # for example, we set CM_CODE_DEP_COMPILER and then call GMP that was compiled with another
00773            # compiler, then it will change this variable to a wrong value and further tools will 
00774            # not be working correctly ...
00775            if s_code_deps!='': s_code_deps+=' '+os_cfg['env_separator']+' '
00776            s_code_deps+=z1
00777 
00778            a_code_deps.append(z1)
00779            a_code_deps.append(z)
00780            a_code_deps.append(z1)
00781 
00782     return {'cm_return':0, 'cm_string':s_code_deps, 'cm_array':a_code_deps, 'env_separator': os_cfg['env_separator'],
00783                            'include_paths':include_paths, 'lib_paths':lib_paths}
00784 
00785 # ============================================================================
00786 def prepare_env_vars(i):
00787     """
00788     Prepare environment variables
00789 
00790     Input:  {
00791               array      - array of environment variables to set
00792               prefix     - add prefix to set environment variable
00793               separator  - add separator for the command line
00794               (quotes)   - use quotes?
00795             }
00796 
00797     Output: {
00798               cm_return   - if =0, success
00799               cm_string   - cmd with prepared environment (export A=B; export C=D; ...)
00800               cm_string1  - simplified string (CM_ALL_ENV="A=B C=D E=F")
00801               cm_array    - array with environment setting (for scripts)
00802             }
00803     """
00804 
00805     # Check vars
00806     if 'array' not in i: return {'cm_return':1, 'cm_error': '"array" is not set in "os prepare_env_vars"'}
00807     if 'prefix' not in i: return {'cm_return':1, 'cm_error': '"prefix" is not set in "os prepare_env_vars"'}
00808     if 'separator' not in i: return {'cm_return':1, 'cm_error': '"separator" is not set in "os prepare_env_vars"'}
00809 
00810     q=''
00811     if 'quotes' in i: q=i['quotes']
00812 
00813     a=[]
00814     s=''
00815     s1=i['prefix']+' CM_ALL_ENV='+q
00816 
00817     for k,v in i['array'].iteritems():
00818         if s!='': 
00819            s+=' '+i['separator']+' '
00820            s1+=' '
00821 
00822         z=i['prefix']+' '+k+'='
00823 
00824         # Check quotes (a bit tricky depending on the OS)
00825         x=''
00826         if q!='':
00827            # likely Linux/Android
00828            x=q+v.replace('"', '\"')+q
00829         else:
00830            # likely Windows
00831            x=v
00832 
00833         s+=z+x
00834         a.append(z+x)
00835 
00836         s1+=k+'='+q+v+q
00837     s1+=q
00838 
00839     return {'cm_return':0, 'cm_string':s, 'cm_string1':s1, 'cm_array':a}
00840 
00841 # ============================================================================
00842 def set_env(i):
00843 
00844     """
00845     Set environment for a given code
00846 
00847     Input:  {
00848               input_key  - key with code_uoa
00849             }
00850 
00851     Output: {
00852               cm_return  - return code = 0, if successful
00853               os_env     - OS environment from the code
00854             }
00855     """
00856 
00857     # Check vars
00858     if 'input_key' not in i: return {'cm_return':1, 'cm_error':'"input_key" is not defined in "code set_env"'}
00859     if i['input_key'] not in i: return {'cm_return':1, 'cm_error':'i["input_key"] is not defined in "code set_env"'}
00860 
00861     code_uoa=i[i['input_key']]
00862 
00863     # Load code
00864     ii={'cm_run_module_uoa':ini['cm_module_uid'],
00865         'cm_action':'load',
00866         'cm_data_uoa':code_uoa}
00867     r=cm_kernel.access(ii)
00868     if r['cm_return']>0: return r
00869     code_cfg=r['cm_data_obj']['cfg']
00870 
00871     r={'cm_return':0}
00872 
00873     if 'os_env' in code_cfg: r.update({'os_env':code_cfg['os_env']})
00874 
00875     return r
00876 
00877 # ============================================================================
00878 def install(i):
00879 
00880     """
00881     Install code and set environment
00882 
00883     Input:  {
00884               (install_data_uid)              - create an entry with this UID
00885               (install_data_alias)            - create an entry with this alias
00886               (install_data_display_as_alias) - use this display as alias for a generated entry
00887               (install_module_uoa)            - use this module to create an entry (if not set, use 'code')
00888               (install_repo_uoa)              - use this repo to create an entry
00889               (cm_array)                      - add this data to the new entry
00890               target_os_uoa                   - target os to install script
00891               (add_rem_to_script)             - add rem to install script
00892             }
00893 
00894     Output: {
00895               cm_return  - return code = 0, if successful
00896 
00897               Parameters from entry creation
00898 
00899               script_filename - script filename
00900               script_name     - full script name
00901             }
00902     """
00903 
00904     cm_kernel.print_for_con('***********************************************')
00905     cm_kernel.print_for_con('Installing code ...')
00906 
00907     # Check vars
00908     if 'target_os_uoa' not in i: return {'cm_return':1, 'cm_error':'"target_os_uoa" is not defined in "code install"'}
00909 
00910     # Create entry
00911     ii={'cm_run_module_uoa':ini['cm_module_uid'],
00912         'cm_action':'update'}
00913     if 'install_data_uid' in i and i['install_data_uid']!='': 
00914        ii['cm_data_uid']=i['install_data_uid']
00915     if 'install_data_alias' in i and i['install_data_alias']!='': 
00916        ii['cm_data_uoa']=i['install_data_alias']
00917     if 'install_data_display_as_alias' in i: 
00918        ii['cm_display_as_alias']=i['install_data_display_as_alias']
00919     if 'install_module_uoa' in i and i['install_module_uoa']!='':
00920        ii['cm_run_module_uoa']=i['install_module_uoa']
00921     if 'cm_array' in i and len(i['cm_array'])>0: ii['cm_array']=i['cm_array']
00922     if 'install_repo_uoa' in i and i['install_repo_uoa']!='': 
00923        ii['cm_repo_uoa']=i['install_repo_uoa']
00924     r=cm_kernel.access(ii)
00925     if r['cm_return']>0: return r
00926 
00927     target_path=r['cm_path']
00928     target_uid=r['cm_uid']
00929     target_alias=r['cm_alias']
00930 
00931     # Prepare script
00932     rx=get_env({'cm_data_uoa':target_uid,
00933                 'os_uoa':i['target_os_uoa']})
00934     if rx['cm_return']>0: return rx
00935 
00936     script=rx['cm_string']
00937 
00938     ii={'script_name':script,
00939         'skip_extension':'yes',
00940         'target_os_uoa':i['target_os_uoa'],
00941         'cm_path':target_path}
00942     if 'code_deps' in i and i.get('skip_code_deps','')!='yes':
00943        ii['code_deps']=i['code_deps']
00944 
00945     # Add remark about how code was built
00946     if 'add_rem_to_script' in i:
00947        run_commands_before=[]
00948        run_commands_before.append('')
00949        for x in i['add_rem_to_script']:
00950            run_commands_before.append(x)
00951        ii['run_commands_before']=run_commands_before
00952 
00953     rx=prepare_script(ii)
00954     if rx['cm_return']>0: return rx
00955 
00956     r['script_name']=rx['cm_path']
00957     r['script_filename']=script
00958 
00959     return r
00960 
00961 # ============================================================================
00962 def prepare_script(i):
00963 
00964     """
00965     Prepare script with code deps
00966 
00967     Input:  {
00968               script_name           - script name (usually without extensions)
00969               target_os_uoa         - target os to get script extension and other parameters
00970               (skip_extension)      - if 'yes', script already has extension
00971               (cm_path)             - path to create script
00972               (set_env1)            - add environment vars (1) to script
00973               (code_deps)           - add code deps to script
00974               (set_env2)            - add environment vars (2) to script
00975               (run_commands_before) - add some commands before main command
00976               (run_cmd)             - add main command
00977               (run_commands_after)  - add some commands after main command
00978             }
00979 
00980     Output: {
00981               cm_return  - return code = 0, if successful
00982               cm_path    - full name of the created script with path
00983             }
00984     """
00985 
00986     # Check vars
00987     if 'script_name' not in i: return {'cm_return':1, 'cm_error':'"script_name" is not defined in "code prepare_script"'}
00988     if 'target_os_uoa' not in i: return {'cm_return':1, 'cm_error':'"target_os_uoa" is not defined in "code prepare_script"'}
00989 
00990     # Prepare path
00991     p=''
00992     if 'cm_path' in i and i['cm_path']!='':
00993        p=os.path.join(i['cm_path'], i['script_name'])
00994     else:
00995        p=i['script_name']
00996 
00997     # Load OS
00998     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00999         'cm_action':'load',
01000         'cm_data_uoa':i['target_os_uoa']}
01001     r=cm_kernel.access(ii)
01002     if r['cm_return']>0: return r
01003 
01004     target_os_cfg=r['cm_data_obj']['cfg']
01005     target_os_path=r['cm_path']
01006     target_os_uid=r['cm_uid']
01007     target_os_alias=r['cm_alias']
01008 
01009     if i.get('skip_extension','')!='yes':
01010        p+=target_os_cfg['script_ext']
01011 
01012     try:
01013        f=file(p, 'w')
01014        if 'batch_prefix' in target_os_cfg and target_os_cfg['batch_prefix']!='': f.write(target_os_cfg['batch_prefix'])
01015 
01016        if 'rem' in target_os_cfg: f.write('\n'+target_os_cfg['rem']+' target_os_uoa: '+i['target_os_uoa']+'\n')
01017 
01018        if 'set_env1' in i and len(i['set_env1'])>0:
01019           f.write('\n')
01020           if 'rem' in target_os_cfg: f.write(target_os_cfg['rem']+' Set global parameters\n')
01021           r=prepare_env_vars({'array':i['set_env1'],
01022                               'prefix':target_os_cfg['env_set'],
01023                               'separator':target_os_cfg['env_separator'],
01024                               'quotes':target_os_cfg['env_quotes']})
01025           if r['cm_return']>0: return r
01026           for x in r['cm_array']: f.write(x+'\n')
01027 
01028        if 'code_deps' in i and len(i['code_deps'])>0:
01029           r=prepare_env_for_all_codes({'code_deps':i['code_deps'],
01030                                        'os_uoa':i['target_os_uoa']})
01031           if r['cm_return']>0: return r
01032           f.write('\n')
01033           if 'rem' in target_os_cfg: f.write(target_os_cfg['rem']+' Prepare code dependencies\n')
01034           for x in r['cm_array']: f.write(x+'\n')
01035 
01036        if 'set_env2' in i and len(i['set_env2'])>0:
01037           f.write('\n')
01038           if 'rem' in target_os_cfg: f.write(target_os_cfg['rem']+' Set execution parameters\n')
01039           r=prepare_env_vars({'array':i['set_env2'],
01040                               'prefix':target_os_cfg['env_set'],
01041                               'separator':target_os_cfg['env_separator'],
01042                               'quotes':target_os_cfg['env_quotes']})
01043           if r['cm_return']>0: return r
01044           for x in r['cm_array']: f.write(x+'\n')
01045 
01046        if 'run_commands_before' in i:
01047           for x in i['run_commands_before']: f.write(x+'\n')
01048 
01049        if 'run_cmd' in i:
01050           f.write('\n')
01051           if 'rem' in target_os_cfg: f.write(target_os_cfg['rem']+' Executable\n')
01052           f.write(i['run_cmd'].strip()+'\n')
01053 
01054        if 'run_commands_after' in i:
01055           for x in i['run_commands_after']: f.write(x+'\n')
01056 
01057        f.close()
01058     except Exception as e:
01059        return {'cm_return':1, 'cm_error':'error while preparing script in "code prepare_script" ('+format(e)+')'}
01060 
01061     return {'cm_return':0, 'cm_path':p}
01062 
01063 # ============================================================================
01064 def prepare_sub_script(i):
01065 
01066     """
01067     Prepare sub_script with extension
01068 
01069     Input:  {
01070               run_script           - script name (usually without extensions)
01071               (run_script_uoa)     - script UOA if any
01072               target_os_cfg        -  target os cfg to get script extension and other parameters
01073 
01074             }
01075 
01076     Output: {
01077               cm_return  - return code = 0, if successful
01078               cm_path    - full name of the created script with path
01079             }
01080     """
01081 
01082     run_cmd=''
01083 
01084     target_os_cfg=i['target_os_cfg']
01085 
01086     remote=False
01087     if target_os_cfg.get('remote','')=='yes': 
01088        remote=True
01089 
01090     script_name=i['run_script']
01091 
01092     script_path=''
01093     if 'run_script_uoa' in i and i['run_script_uoa']!='':
01094 #       cm_kernel.print_for_con('')
01095 #       cm_kernel.print_for_con('Preparing path for OS script '+i['run_script_uoa']+' ...')
01096 
01097        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os.script'],
01098            'cm_action':'load',
01099            'cm_data_uoa':i['run_script_uoa']}
01100        r=cm_kernel.access(ii)
01101        if r['cm_return']>0: return r
01102 
01103        script_cfg=r['cm_data_obj']['cfg']
01104        script_path=r['cm_path']
01105 
01106        if 'scripts' not in script_cfg or i['run_script'] not in script_cfg['scripts']:
01107           return {'cm_return':1, 'cm_error':'can\'t find script in os.script configuration'}
01108 
01109        script_name=script_cfg['scripts'][script_name]
01110 
01111     script_name+=target_os_cfg['script_ext']
01112 
01113     run_name=script_name
01114     if script_path!='':
01115        run_name=os.path.join(script_path, run_name)
01116     elif 'exec_prefix' in target_os_cfg and target_os_cfg['exec_prefix']!='': 
01117        run_name=target_os_cfg['exec_prefix']+run_name
01118 
01119     if target_os_cfg.get('set_executable','')!='':
01120        p=target_os_cfg['set_executable']+' '+run_name
01121        x=os.system(p)
01122 
01123     run_cmd=''
01124     if remote and target_os_cfg.get('no_script_execution','')=='yes':
01125        r=cm_kernel.load_array_from_file({'cm_filename':run_name})
01126        if r['cm_return']>0: return r
01127        a=r['cm_array']
01128        for x in a:
01129            xx=x.strip()
01130            if xx!='' and not xx.startswith(target_os_cfg['rem']):
01131               if run_cmd!='': run_cmd+=target_os_cfg['env_separator']+' '
01132               run_cmd+=xx
01133        run_name=''
01134     else:
01135        run_cmd=run_name
01136 
01137     if i.get('run_cmd','')!='': run_cmd+=' '+i['run_cmd']
01138 
01139     if i.get('run_cmd_out1','')!='': run_cmd+=' 1>'+i['run_cmd_out1']
01140     if i.get('run_cmd_out2','')!='': run_cmd+=' 2>'+i['run_cmd_out2']
01141 
01142 
01143     return {'cm_return':0, 'run_cmd':run_cmd}
01144 
01145 # ============================================================================
01146 def prepare_cmd(i):
01147     """
01148     Prepare command line for the code
01149 
01150     Input:  {
01151               (code_module_uoa)  - module of the working data
01152               (code_data_uoa)    - working data uoa
01153               (run_cmd_key)      - name if multiple CMDs available for this program
01154               (dataset_uoa)      - UOA of dataset used to prepare this cmd
01155               (os_uoa)           - OS UOA
01156               (compilation_type) - static or dynamic
01157             }
01158 
01159     Output: {
01160               cm_return               - if =0, success
01161               (cm_error)              - if cm_return>0, error text
01162 
01163               run_time={
01164                 run_env               - set environment before command line
01165                                       - the following params will be aggregated to 1 line
01166                 run_cmd1
01167                 run_cmd_binary_name
01168                 run_cmd2
01169                 run_cmd_main
01170                 run_cmd3
01171                 run_cmd_out1
01172                 run_cmd_out2
01173                 run_output_files
01174                 run_input_files
01175                 dataset_files
01176               }
01177 
01178               build_compiler_vars     - possible compiler variables (often set hardwired dataset)
01179             }
01180     """
01181 
01182     # Prepare environment
01183     rr={'cm_return':0}
01184 
01185     # Load OS configuration
01186     os_uoa=''
01187     if 'os_uoa' in i and i['os_uoa']!='': os_uoa=i['os_uoa']
01188     elif 'cm_default_os_uoa' in cm_kernel.ini['dcfg'] and cm_kernel.ini['dcfg']['cm_default_os_uoa']!='':
01189        os_uoa=cm_kernel.ini['dcfg']['cm_default_os_uoa']
01190 
01191     if os_uoa=='' not in i:
01192        return {'cm_return':1, 'cm_error':'"os_uoa" is not defined and not in kernel'}
01193     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
01194         'cm_action':'load',
01195         'cm_data_uoa':os_uoa}
01196     r=cm_kernel.access(ii)
01197     if r['cm_return']>0: return r
01198 
01199     os_cfg=r['cm_data_obj']['cfg']
01200     os_path=r['cm_path']
01201     os_uid=r['cm_uid']
01202     os_alias=r['cm_alias']
01203 
01204     run_time={}
01205     run_input_files=[]
01206     target_file=''
01207 
01208     # Check vars
01209     if 'code_data_uoa' in i and 'run_cmd_key' in i:
01210        # Load source code
01211        m=ini['cm_module_uid']
01212        if 'code_module_uoa' in i and i['code_module_uoa']!='': m=i['code_module_uoa']
01213        ii={'cm_run_module_uoa':m,
01214            'cm_action':'load',
01215            'cm_data_uoa':i['code_data_uoa']}
01216        r=cm_kernel.access(ii)
01217        if r['cm_return']>0: return r
01218 
01219        sc_cfg=r['cm_data_obj']['cfg']
01220        sc_path=r['cm_path']
01221        sc_uid=r['cm_uid']
01222        sc_alias=r['cm_alias']
01223 
01224        # Prepare binary file
01225        target_file=''
01226        if 'target_file' in sc_cfg: target_file=os_cfg.get('exec_prefix','')+sc_cfg['target_file']
01227 
01228        add_target_extension1=sc_cfg.get('add_target_extension',{})
01229        ct=i.get('compilation_type','')
01230        if ct=='': ct='static'
01231        if ct in add_target_extension1: add_target_extension=add_target_extension1.get(ct)
01232 
01233        if add_target_extension!='':
01234           if 'file_extensions' in os_cfg and add_target_extension in os_cfg['file_extensions']:
01235              target_ext=os_cfg['file_extensions'][add_target_extension]
01236              target_file+=target_ext
01237 
01238        cm_kernel.print_for_con('Prepared target binary name: '+target_file)
01239 
01240        # Set vars
01241        if 'run_cmds' in sc_cfg and i['run_cmd_key'] in sc_cfg['run_cmds']:
01242           rc=sc_cfg['run_cmds'][i['run_cmd_key']]
01243 
01244           run_time=copy.deepcopy(rc.get('run_time',{})) # otherwise change somewhere 
01245                                                         # and it mixes up caching
01246 
01247        # Put original input files (useful for mobiles not to mix with dataset files)
01248        orif=[]
01249        for q in run_time.get('run_input_files',[]):
01250            orif.append(q)
01251 
01252        # Load dataset
01253        if i.get('dataset_uoa','')!='':
01254           cm_kernel.print_for_con('')
01255           cm_kernel.print_for_con('Loading dataset ...')
01256           ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['dataset'],
01257               'cm_action':'load',
01258               'cm_data_uoa':i['dataset_uoa']}
01259           r=cm_kernel.access(ii)
01260           if r['cm_return']>0: return r
01261 
01262           ds_cfg=r['cm_data_obj']['cfg']
01263           ds_path=r['cm_path']
01264           ds_uid=r['cm_uid']
01265           ds_alias=r['cm_alias']
01266 
01267           if 'build_compiler_vars' in ds_cfg:
01268              rr['build_compiler_vars']=ds_cfg['build_compiler_vars']
01269 
01270           if 'run_cmd_main' in run_time: 
01271              ds_path1=ds_path
01272              os_sep=os_cfg.get('dir_sep','')
01273              if os_cfg.get('remote','')=='yes': 
01274                 ds_path1=''
01275                 os_sep=''
01276              run_time['run_cmd_main']=run_time['run_cmd_main'].replace(cm_kernel.convert_str_to_special('dataset_path'), ds_path1)
01277              run_time['run_cmd_main']=run_time['run_cmd_main'].replace(cm_kernel.convert_str_to_special('os_dir_separator'), os_sep)
01278 
01279              # Check which path to use for data set
01280              if 'dataset_files' in ds_cfg and len(ds_cfg['dataset_files'])>0:
01281                 run_time['dataset_files']=ds_cfg['dataset_files']
01282                 xx=0
01283                 for x in ds_cfg['dataset_files']:
01284                     y='dataset_filename'
01285                     if xx!=0: y+='_'+str(xx)
01286                     run_time['run_cmd_main']=run_time['run_cmd_main'].replace(cm_kernel.convert_str_to_special(y), x)
01287                     xx+=1
01288 
01289           # Prepare input files (for remote)
01290           if 'dataset_files' in ds_cfg and len(ds_cfg['dataset_files'])>0:
01291               x1=ds_path
01292               for x2 in ds_cfg['dataset_files']:
01293                   x3=os.path.join(x1,x2)
01294                   if x3 not in run_input_files: run_input_files.append(x3)
01295 
01296           # Check cm_properties
01297           rcm=ds_cfg.get('cm_properties',{}).get('run_time',{}).get('run_cmd_main',{})
01298           for x in rcm:
01299               run_time['run_cmd_main']=run_time['run_cmd_main'].replace(cm_kernel.convert_str_to_special(x), rcm[x])
01300 
01301     run_time['binary_name']=target_file
01302     rr['run_time']=run_time
01303     rr['run_time']['original_run_input_files']=orif
01304     if 'run_input_files' not in rr['run_time']: rr['run_time']['run_input_files']=[]
01305     for q in run_input_files:
01306         if q not in rr['run_time']['run_input_files']: rr['run_time']['run_input_files'].append(q)
01307 
01308     return rr
01309 
01310 # ============================================================================
01311 def native_run(i):
01312     """
01313     Native run with timing
01314 
01315     Input:  {
01316               cm_unparsed - run
01317               skip_output - if 'yes', do not print 'Execution time ...'
01318             }
01319 
01320     Output: {
01321               cm_return      - return of the system
01322               execution_time - execution time of a command
01323             }
01324     """
01325 
01326     ucmd=i['cm_unparsed']
01327 
01328     cmd=''
01329     for q in ucmd:
01330         cmd+=' '+q
01331     cmd=cmd.strip()
01332 
01333     tstart=time.time()
01334 
01335     rc=os.system(cmd)
01336 
01337     t=time.time()-tstart
01338 
01339     if i.get('skip_output','')!='yes':
01340        cm_kernel.print_for_con('Execution time: '+"%.3f" % t+' seconds')
01341 
01342     return {'cm_return':0, 'execution_time':str(t)}

Generated on Wed May 28 02:49:01 2014 for Collective Mind Framework by DoxyGen 1.6.1
Concept, design and coordination: Grigori Fursin (C) 1993-2013