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 sys
00016 import os
00017 import shutil
00018 import json
00019 import time
00020 import copy
00021 
00022 # ============================================================================
00023 def init(i):
00024     return {'cm_return':0}
00025 
00026 # ============================================================================
00027 def change_work_dir(i):
00028 
00029     """
00030     Chaning working directory
00031 
00032     Input:  {
00033               work_dir                  - if !='', go to this directory
00034               (work_dir_repo_uoa)       - if !='', go to the directory for this cM entry
00035               (work_dir_module_uoa)     - if !='', go to the directory for this cM entry
00036               (source_code_uoa)         - if !='', go to the directory for this cM entry
00037             }
00038 
00039     Output: {
00040               cm_return                 - if =0, success
00041               work_dir                  - selected working directory 
00042             }
00043     """
00044 
00045     # Changing 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 'source_code_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['source_code_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('Changing path to '+work_dir+' ...')
00061        os.chdir(work_dir)
00062 
00063     return {'cm_return':0, 'work_dir':work_dir}
00064 
00065 # ============================================================================
00066 def build(i):
00067 
00068     """
00069     Build program
00070 
00071     Input:  {
00072               See code.run (all parameters will be passed to code.run)
00073 
00074               (work_dir_repo_uoa)             - change to the working directory in the repository (repo UOA)
00075               (work_dir_module_uoa)           - change to the working directory in the repository (module UOA)
00076               (work_dir_data_uoa)             - change to the working directory in the repository (data UOA)
00077 
00078               build_target_os_uoa             - target OS uoa for building
00079 
00080               If the above parameters are set, the following parameters will be added to run_set_env2 from the given cM entry:
00081                 CM_SOURCE_FILES="source_files"
00082                 CM_SOURCE_FILES_WITHOUT_EXT="source_files" (extension will be removed)
00083                 CM_TARGET_FILE="target_file"
00084 
00085               (target_file)                   - output file after building
00086               (add_target_extension)          - take extension from run_target_os_uoa
00087               (include_deps)                  - add 'include' directory from this list
00088               (include_prefix)                - add prefix before include directory (usually -I or /I) 
00089               (compiler_vars)                 - add compiler vars
00090               (compiler_var_prefix)           - add compiler var prefix (usually -D or /D)
00091               (lib_deps)                      - add libraries from this list (use CM_TARGET_FILE)
00092 
00093               (compilation_type)              - 'static' (by default) or 'dynamic'
00094 
00095               (install)                       - if 'yes', install to cM entry
00096               (install_data_uid)              - if !='', it will be used for installation data entry
00097               (install_data_alias)            - if !='', use it for installation data entry
00098               (install_data_display_as_alias) - use this display as alias for a generated entry
00099               (install_module_uoa)            - if =='', module 'code' is used
00100               (install_repo_uoa)              - repo for installation entry
00101 
00102               (source_files)                  - list of source files; if empty, use list of source files from the program entry
00103 
00104               (add_rem_to_script)             - add rem to install script
00105               (add_to_code_entry)             - add array to code entry
00106 
00107               (ignore_script_error)           - if 'yes', do not generate error when script returns !=0
00108                                                 (needed for experiment pipelines to continue running)
00109 
00110               (cm_dependencies)               - dependencies that set all other dependencies (code/include/lib).
00111                                                 Format [{"class UOA":"code UOA"},{},...]
00112 
00113               (skip_target_file_check)        - if 'yes', skip target file check
00114 
00115               (run_cmd_out1)                  - pass to 'code run' to redirect output1
00116               (run_cmd_out2)                  - pass to 'code run' to redirect output2
00117 
00118               (clean_program)                 - if 'yes', clean program before
00119               (clean_program_script_name)     - script name for cleaning ...
00120 
00121               (code_deps)                     - list with code UOA for dependencies [{"index":"uoa"} ...]
00122 
00123               (run_set_env2)                  - array with environment variables to be set before compiling
00124               (cm_verbose)                    - if 'yes', print all info
00125             }
00126 
00127     Output: {
00128               cm_return = 0, if successful
00129 
00130               Parameters from "code run" when executing script
00131 
00132               target_file      - output file after building
00133               target_file_size - size of output file
00134             }
00135     """
00136 
00137     # Check verbose - yes, by default
00138     vrb=i.get('cm_verbose','yes')
00139 
00140     if vrb=='yes':
00141        cm_kernel.print_for_con('***********************************************')
00142        cm_kernel.print_for_con('Building code ...')
00143 
00144     # Load target OS
00145     if vrb=='yes':
00146        cm_kernel.print_for_con('')
00147        cm_kernel.print_for_con('Loading target os '+i['build_target_os_uoa']+' ...')
00148     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00149         'cm_action':'load',
00150         'cm_data_uoa':i['build_target_os_uoa']}
00151     r=cm_kernel.access(ii)
00152     if r['cm_return']>0: return r
00153 
00154     target_os_cfg=r['cm_data_obj']['cfg']
00155     target_os_path=r['cm_path']
00156     target_os_uid=r['cm_uid']
00157     target_os_alias=r['cm_alias']
00158 
00159     # Dependencies
00160     code_deps=i.get('code_deps',[])
00161     include_deps=i.get('include_deps',[])
00162     lib_deps=i.get('lib_deps',[])
00163     compiler_vars=i.get('compiler_vars',[])
00164 
00165     # Check if code_deps set additional parameters
00166     qq=i.get('cm_dependencies',{})
00167     if len(qq)>0:
00168         if vrb=='yes':
00169            cm_kernel.print_for_con('')
00170            cm_kernel.print_for_con('Updating code/include/lib dependencies ...')
00171 
00172         code_deps1=[]
00173         include_deps1=[]
00174         lib_deps1=[]
00175 
00176         for q in i.get('cm_dependencies',{}):
00177             yy=q.keys()[0]
00178             x=q[yy]
00179 
00180             jj={'cm_run_module_uoa':ini['cfg']['cm_modules']['class'],
00181                 'cm_data_uoa':yy,
00182                 'cm_action':'load'}
00183             rj=cm_kernel.access(jj)
00184             if rj['cm_return']>0: return rj
00185 
00186             drj=rj['cm_data_obj']['cfg']
00187 
00188             vcd=drj.get('build_code_deps_var','')
00189             vid=drj.get('include_deps_var','')
00190             vld=drj.get('lib_deps_var','')
00191 
00192             if vcd!='': code_deps1.append({vcd:x})
00193             if vid!='': include_deps1.append({vid:x})
00194             if vld!='': lib_deps1.append({vld:x})
00195 
00196         for q in code_deps: code_deps1.append(q)
00197         for q in include_deps: include_deps1.append(q)
00198         for q in lib_deps: lib_deps1.append(q)
00199 
00200         code_deps=code_deps1
00201         include_deps=include_deps1
00202         lib_deps=lib_deps1
00203 
00204     i['code_deps']=code_deps
00205 
00206     if vrb=='yes':
00207        cm_kernel.print_for_con('')
00208        cm_kernel.print_for_con('Code dependencies:')
00209        cm_kernel.print_for_con('')
00210        cm_kernel.print_for_con(json.dumps(code_deps,indent=2))
00211        cm_kernel.print_for_con('')
00212        cm_kernel.print_for_con('Include dependencies:')
00213        cm_kernel.print_for_con('')
00214        cm_kernel.print_for_con(json.dumps(include_deps,indent=2))
00215        cm_kernel.print_for_con('')
00216        cm_kernel.print_for_con('Lib dependencies:')
00217        cm_kernel.print_for_con('')
00218        cm_kernel.print_for_con(json.dumps(lib_deps,indent=2))
00219 
00220     # Check if need to add some variables from code deps:
00221 
00222     if len(code_deps)>0:
00223        if vrb=='yes':
00224           cm_kernel.print_for_con('')
00225           cm_kernel.print_for_con('Updating variables from code dependencies ...')
00226 
00227        for q in code_deps:
00228            yy=q.keys()[0]
00229            x=q[yy]
00230 
00231            jj={'cm_run_module_uoa':ini['cfg']['cm_modules']['code'],
00232                'cm_data_uoa':x,
00233                'cm_action':'load'}
00234            rj=cm_kernel.access(jj)
00235            if rj['cm_return']>0: return rj
00236 
00237            drj=rj['cm_data_obj']['cfg']
00238 
00239            jj=drj.get('build_params',{})
00240            if len(jj)>0:
00241               cm_kernel.merge_arrays({'cm_array':i, 'cm_array1': jj})
00242 
00243     # Check a few special params
00244     include_prefix=i.get('include_prefix','')
00245     compiler_var_prefix=i.get('compiler_var_prefix','')
00246 
00247     # Check if install and prepare entry
00248     install=False
00249     if i.get('install', '')=='yes':
00250         install=True
00251 
00252         # Check if data_uoa is set and generate if not
00253         install_data_uid=''
00254         if 'install_data_uid' in i and i['install_data_uid']!='': install_data_uid=i['install_data_uid']
00255         install_data_alias=''
00256         if 'install_data_alias' in i: install_data_alias=i['install_data_alias']
00257 
00258         # Create install entry
00259         if vrb=='yes':
00260            cm_kernel.print_for_con('')
00261            cm_kernel.print_for_con('Creating installation entry ...')
00262         ii={'cm_action':'install',
00263             'cm_array':{'build_started':'yes',
00264                         'build_finished_successfully':'no'}}
00265         if 'add_to_code_entry' in i:
00266            ii['cm_array'].update(i['add_to_code_entry'])
00267         if len(code_deps)>0: ii['code_deps']=code_deps
00268         install_module_uoa=''
00269         if 'install_module_uoa' in i and i['install_module_uoa']!='':
00270            install_module_uoa=i['install_module_uoa']
00271            ii['install_module_uoa']=install_module_uoa
00272         else:
00273            install_module_uoa=ini['cfg']['cm_modules']['code']
00274         ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['code']
00275         if install_data_uid!='': ii.update({'install_data_uid':install_data_uid})
00276         if install_data_alias!='': ii.update({'install_data_uoa':install_data_alias})
00277         if 'install_data_display_as_alias' in i: ii['install_data_display_as_alias']=i['install_data_display_as_alias']
00278         install_repo_uoa=''
00279         if 'install_repo_uoa' in i and i['install_repo_uoa']!='': 
00280            install_repo_uoa=i['install_repo_uoa']
00281            ii['install_repo_uoa']=install_repo_uoa
00282 # It seems that here should be host os and not target!
00283 #        if 'build_target_os_uoa' in i and i['build_target_os_uoa']!='':
00284 #           ii['target_os_uoa']=i['build_target_os_uoa']
00285         if 'run_host_os_uoa' in i and i['run_host_os_uoa']!='':
00286            ii['target_os_uoa']=i['run_host_os_uoa']
00287         if 'add_rem_to_script' in i:
00288            ii['add_rem_to_script']=i['add_rem_to_script']
00289 
00290         if vrb=='yes':
00291            ii['cm_verbose']=vrb
00292 
00293         r=cm_kernel.access(ii)
00294         if r['cm_return']>0: return r
00295 
00296         install_path=r['cm_path']
00297         install_uid=r['cm_uid']
00298         install_alias=r['cm_alias']
00299         script_name=r['script_name']
00300         script_filename=r['script_filename']
00301 
00302         if vrb=='yes':
00303            cm_kernel.print_for_con('')
00304            cm_kernel.print_for_con('Prepared entry:')
00305            cm_kernel.print_for_con(' path:   '+install_path)
00306            cm_kernel.print_for_con(' alias:  '+install_alias)
00307            cm_kernel.print_for_con(' UID:    '+install_uid)
00308            cm_kernel.print_for_con(' Script: '+script_name)
00309 
00310     # Check vars
00311     run_set_env2={}
00312     if 'run_set_env2' in i: run_set_env2.update(i['run_set_env2'])
00313 
00314     target_file=''
00315     if 'target_file' in i: target_file=i['target_file']
00316 
00317     obj_dir=''
00318     local_src_dir=''
00319 
00320     # Check that build in a cM entry, then set additional parameters
00321     sys.stdout.flush()
00322 
00323     force_includes=[]
00324 
00325     work_dir=''
00326     work_cfg={}
00327     if 'work_dir' in i: work_dir=i['work_dir']
00328     elif ('work_dir_module_uoa' in i and 'work_dir_data_uoa' in i):
00329        if vrb=='yes':
00330           cm_kernel.print_for_con('')
00331           cm_kernel.print_for_con('Loading parameters from the cM entry ...')
00332 
00333        # Change path to another directory
00334        ii={'cm_run_module_uoa':i['work_dir_module_uoa'],
00335            'cm_action':'load',
00336            'cm_data_uoa':i['work_dir_data_uoa']}
00337        if 'work_dir_repo_uoa' in i and i['work_dir_repo_uoa']!='': ii['cm_repo_uoa']=i['work_dir_repo_uoa']
00338        r=cm_kernel.access(ii)
00339        if r['cm_return']>0: return r
00340 
00341        work_cfg=r['cm_data_obj']['cfg']
00342        work_dir=r['cm_path']
00343        local_src_dir=''
00344 
00345        source_files=i.get('source_files',[])
00346        if len(source_files)==0:
00347           source_files=work_cfg.get('source_files',[])
00348        if len(source_files)>0: 
00349           jj=''
00350           jj1=''
00351           for j in source_files:
00352               jj+=' '+j
00353               jj1+=' '+os.path.splitext(j)[0]
00354           run_set_env2['CM_SOURCE_FILES']=jj
00355           run_set_env2['CM_SOURCE_FILES_WITHOUT_EXT']=jj1
00356 
00357        if target_file=='' and 'CM_TARGET_FILE' not in run_set_env2 and 'target_file' in work_cfg: 
00358           target_file=work_cfg['target_file']
00359 
00360        if 'local_src_dir' in work_cfg and work_cfg['local_src_dir']!='': 
00361           local_src_dir=work_cfg['local_src_dir']
00362        else:
00363           local_src_dir=work_dir
00364 
00365        # Check if need to add extension
00366        add_target_extension=i.get('add_target_extension','')
00367        if add_target_extension=='':
00368           add_target_extension1=work_cfg.get('add_target_extension',{})
00369           ct=i.get('compilation_type','')
00370           if ct=='': ct='static'
00371           if ct in add_target_extension1: add_target_extension=add_target_extension1.get(ct)
00372 
00373        if add_target_extension!='':
00374           if 'file_extensions' in target_os_cfg and add_target_extension in target_os_cfg['file_extensions']:
00375              target_ext=target_os_cfg['file_extensions'][add_target_extension]
00376              if vrb=='yes':
00377                 cm_kernel.print_for_con('')
00378                 cm_kernel.print_for_con('Adding target extension '+target_ext)
00379 
00380              target_file+=target_ext
00381 
00382           if 'obj_dir' in target_os_cfg:
00383              obj_dir=target_os_cfg['obj_dir']
00384 
00385        if 'force_build_includes' in work_cfg:
00386           force_includes=work_cfg['force_build_includes']
00387 
00388     if work_dir!='':
00389        if vrb=='yes':
00390           cm_kernel.print_for_con('')
00391           cm_kernel.print_for_con('Changing path to '+work_dir+' ...')
00392        os.chdir(work_dir)
00393 
00394     # Target file
00395     if target_file!='': 
00396        run_set_env2['CM_TARGET_FILE']=target_file
00397 
00398     if install:
00399        run_set_env2['CM_INSTALL']='yes'
00400        if install_path!='':
00401           run_set_env2['CM_INSTALL_DIR']=install_path
00402        if local_src_dir!='':
00403           run_set_env2['CM_INSTALL_LOCAL_DIR']=local_src_dir
00404        run_set_env2['CM_SRC_DIR']=work_dir
00405        if 'install_obj_dir' in ini['cfg'] and ini['cfg']['install_obj_dir']!='':
00406           obj_dir=ini['cfg']['install_obj_dir']
00407        if obj_dir!='':
00408           run_set_env2['CM_INSTALL_OBJ_DIR']=os.path.join(install_path, ini['cfg']['install_obj_dir'])
00409        if local_src_dir!='':
00410           run_set_env2['CM_LOCAL_SRC_DIR']=os.path.join(work_dir, local_src_dir)
00411        run_set_env2['CM_CODE_ENV_FILE']=script_name
00412        run_set_env2['CM_CODE_UID']=install_uid
00413        run_set_env2['CM_OS_LIB_DIR']=target_os_cfg['lib_dir']
00414 
00415     # Check if include dependencies
00416     if len(include_deps)>0 and include_prefix!='':
00417        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['code'],
00418            'cm_action':'prepare_env_for_all_codes',
00419            'code_deps':include_deps,
00420            'os_uoa':target_os_uid}
00421        r=cm_kernel.access(ii)
00422        if r['cm_return']>0: return r
00423 
00424        cm_compiler_flags_misc=''
00425        if 'CM_COMPILER_FLAGS_MISC' in run_set_env2: cm_compiler_flags_misc=run_set_env2['CM_COMPILER_FLAGS_MISC']
00426 
00427        for x in r['include_paths']:
00428            cm_compiler_flags_misc+=' '+include_prefix+x
00429 
00430        run_set_env2['CM_COMPILER_FLAGS_MISC']=cm_compiler_flags_misc
00431 
00432     # Add forced includes
00433     if len(force_includes)>0 and include_prefix!='':
00434        x=run_set_env2.get('CM_COMPILER_FLAGS_MISC','')
00435        for y in force_includes:
00436            x+=' '+include_prefix+y
00437        run_set_env2['CM_COMPILER_FLAGS_MISC']=x
00438 
00439     # Check if compiler_vars
00440     if len(compiler_vars)>0 and compiler_var_prefix!='':
00441        x=run_set_env2.get('CM_COMPILER_FLAGS_MISC','')
00442 
00443        for y in compiler_vars:
00444            z=y
00445            if compiler_vars[y]!='': z+='='+compiler_vars[y]
00446            x+=' '+compiler_var_prefix+z
00447 
00448        run_set_env2['CM_COMPILER_FLAGS_MISC']=x
00449 
00450     # Check if lib dependencies
00451     if len(lib_deps)>0:
00452        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['code'],
00453            'cm_action':'prepare_env_for_all_codes',
00454            'code_deps':lib_deps,
00455            'os_uoa':target_os_uid}
00456        r=cm_kernel.access(ii)
00457        if r['cm_return']>0: return r
00458 
00459        cm_ld_flags_misc=''
00460        if 'CM_LD_FLAGS_MISC' in run_set_env2: cm_ld_flags_misc=run_set_env2['CM_LD_FLAGS_MISC']
00461 
00462        for x in r['lib_paths']:
00463            cm_ld_flags_misc+=' '+x
00464 
00465        run_set_env2['CM_LD_FLAGS_MISC']=cm_ld_flags_misc
00466 
00467     if vrb=='yes':
00468        cm_kernel.print_for_con('')
00469        cm_kernel.print_for_con('run_set_env2:')
00470        cm_kernel.print_for_con(json.dumps(run_set_env2, indent=2))
00471 
00472     # Call code.run to build "code.source" and install to "code" if needed
00473     ii={}
00474     ii.update(i)
00475     ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['code']
00476     ii['cm_action']='run'
00477     ii['run_set_env2']=run_set_env2
00478     if target_file!='': 
00479        if 'run_output_files' not in ii: ii['run_output_files']=[]
00480        if target_file not in ii['run_output_files']: ii['run_output_files'].append(target_file)
00481 
00482     # Check output
00483     if i.get('run_cmd_out1','')!='': ii['run_cmd_out1']=i['run_cmd_out1']
00484     if i.get('run_cmd_out2','')!='': ii['run_cmd_out2']=i['run_cmd_out2']
00485 
00486     sys.stdout.flush()
00487 
00488     # Check if cleaning program before
00489     if i.get('clean_program','')=='yes':
00490        ii_copy=copy.deepcopy(ii)
00491        ii_copy['run_script']=i.get('clean_program_script_name','')
00492        ii_copy['calming_delay']='0' # calming delay not needed here
00493        if vrb=='yes': ii['cm_verbose']=vrb
00494        r=cm_kernel.access(ii_copy)
00495        if r['cm_return']>0: return r
00496 
00497     sys.stdout.flush()
00498     calming_delay=ini['cfg'].get('calming_delay','')
00499     if calming_delay!='' and float(calming_delay)!=0:
00500        if vrb=='yes':
00501           cm_kernel.print_for_con('')
00502           cm_kernel.print_for_con('Calming delay: '+calming_delay+' second(s) ...')
00503        time.sleep(float(calming_delay))
00504 
00505     state_input=ii.copy()
00506 
00507     # Perform Building
00508     if vrb=='yes': ii['cm_verbose']=vrb
00509     r=cm_kernel.access(ii)
00510     state_output=r.copy()
00511     if r['cm_return']>0: return r
00512 
00513     rr={}
00514     rr.update(r)
00515     rr['target_file']=target_file
00516 
00517     if r.get('run_time_by_module','')!='':
00518        if vrb=='yes':
00519           cm_kernel.print_for_con("")
00520           cm_kernel.print_for_con('Build time: '+r['run_time_by_module'])
00521 
00522     if i.get('ignore_script_error','')!='yes' and ('exit_code' not in r or r['exit_code']!=0):
00523        return {'cm_return':16, 'cm_error':'build failed: script returned non-zero code ('+str(r.get('exit_code',''))+')'}
00524 
00525     # Check binary size
00526     size=0
00527     fail=False
00528     if i.get('skip_target_file_check','')!='yes' and target_file!='':
00529        if vrb=='yes':
00530           cm_kernel.print_for_con('')
00531        p=os.path.join(r['work_dir'], target_file)
00532        if not os.path.isfile(p):
00533           if vrb=='yes':
00534              cm_kernel.print_for_con('Target file ('+p+') was not found - likely problem with compilation')
00535           fail=True
00536        else:
00537           size=os.path.getsize(p);
00538           rr['target_file_size']=str(size)
00539           if vrb=='yes':
00540              cm_kernel.print_for_con('Target file size:  '+rr['target_file_size'])
00541 
00542     # Finish installation if set up
00543     if install and not fail:
00544        if vrb=='yes':
00545           cm_kernel.print_for_con('')
00546           cm_kernel.print_for_con('Copying script to the cM bin directory ...')
00547 
00548        p1=os.path.join(cm_kernel.ini[cm_kernel.env_cm_bin], script_filename)
00549        try:
00550           shutil.copyfile(script_name, p1)
00551        except Exception as e:
00552           return {'cm_return':1, 'cm_error':'error while copying script to cM bin directory ('+format(e)+')'}
00553 
00554        if 'set_executable' in target_os_cfg:
00555           r=os.system(target_os_cfg['set_executable']+' '+p1)
00556 
00557        if vrb=='yes':
00558           cm_kernel.print_for_con('')
00559           cm_kernel.print_for_con('Updating code entry (successful installation) ...')
00560           cm_kernel.print_for_con('')
00561 
00562        data={'build_finished_successfully':'yes'}
00563        if 'add_to_code' in work_cfg: data.update(work_cfg['add_to_code'])
00564 
00565        data['state_input']=state_input
00566        data['state_output']=state_output
00567 
00568        ii={'cm_run_module_uoa':install_module_uoa,
00569            'cm_action':'update',
00570            'cm_array':data,
00571            'cm_data_uoa':install_uid}
00572        if install_repo_uoa!='': ii['cm_repo_uoa']=install_repo_uoa
00573        r=cm_kernel.access(ii)
00574        if r['cm_return']>0: return r
00575 
00576     if not fail:
00577        if vrb=='yes':
00578           cm_kernel.print_for_con('')
00579           cm_kernel.print_for_con('Building finished successfully!')
00580 
00581     return rr
00582 
00583 # ============================================================================
00584 def web_build(i):
00585 
00586     """
00587     Build program through web
00588 
00589     Input:  See build(i)
00590     Output: See build(i)
00591 
00592     """
00593 
00594     # Get web style
00595     if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
00596     else:
00597        return {'cm_return':1, 'cm_error':'web style is not defined'}
00598 
00599     cm_kernel.print_for_con('<span class="cm-title">Build and run program</span><br>')
00600 
00601     # Detecting/restoring data from forms
00602     a1={}
00603     r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00604                         'cm_action':'detect_form_params',
00605                         'cm_array':i, 
00606                         'cm_prefix':'#form1'})
00607     if r['cm_return']>0: return r
00608     cm_form_array1=r['cm_array']
00609     cm_form_commands1=r['cm_commands']
00610 
00611     # Get data description for this action
00612     r=cm_kernel.get_data_description({'cm_module_uoa':ini['cm_module_uoa'], 
00613                                       'cm_which_action':i['cm_action']})
00614     if r['cm_return']>0: return r
00615     cm_data_desc1=r['cm_data_desc']
00616     cm_params_default1=r['cm_params_default']
00617 
00618     # Check default
00619     forms_exists='yes'
00620     if len(cm_form_array1)==0:
00621        a1=cm_params_default1
00622 
00623        forms_exists='no'
00624     else:
00625        r=cm_kernel.restore_flattened_array({'cm_array':cm_form_array1, 
00626                                             'cm_replace_in_keys':{'^35^':'#', '^64^':'@'}})
00627        if r['cm_return']>0: return r
00628        a1=r['cm_array']
00629 
00630     # Set common parameters
00631     last_command={}
00632 
00633     ii={}
00634     ii['work_dir_repo_uoa']=a1.get('work_dir_repo_uoa','')
00635     ii['ctuning_setup_uoa']=a1.get('ctuning_setup_uoa','')
00636     ii['work_dir_module_uoa']=ini['cm_module_uid']
00637     ii['work_dir_data_uoa']=a1.get('work_dir_data_uoa','')
00638     ii['run_host_os_uoa']=a1.get('run_host_os_uoa','')
00639     ii['run_target_processor_uoa']=a1.get('run_target_processor_uoa','')
00640     ii['cm_detach_console']='yes'
00641     if 'install_data_uid' in a1 and a1['install_data_uid']!='': ii['install_data_uid']=a1['install_data_uid']
00642     if 'install_data_display_as_alias' in i: ii['install_data_display_as_alias']=i['install_data_display_as_alias']
00643     if 'keep_all_files' in a1 and a1['keep_all_files']!='': ii['keep_all_files']=a1['keep_all_files']
00644 
00645     # Check if build
00646     if 'cm_submit_build' in i:
00647        ii['cm_run_module_uoa']=ini['cm_module_uoa']
00648        ii['cm_action']='build'
00649 
00650        ii['build_target_os_uoa']=a1.get('build_target_os_uoa','')
00651        ii['work_dir_module_uoa']=ini['cm_module_uid']
00652        ii['run_script_uoa']=a1.get('build_run_script_uoa')
00653        ii['run_script']=a1.get('build_run_script')
00654 
00655        ii['code_deps']=[]
00656        ii['run_set_env2']={}
00657 
00658        flags=''
00659        if a1.get('compiler_flags','')!='': flags=a1['compiler_flags']
00660        if a1.get('compiler_flags_node',{}).get('all_compiler_flags',{}).get('base_flag','')!='': 
00661           flags=a1['compiler_flags_node']['all_compiler_flags']['base_flag']+' '+flags
00662 
00663        if a1.get('compiler_code_uoa','')!='': ii['code_deps'].append({"CM_CODE_DEP_COMPILER":a1['compiler_code_uoa']})
00664        if len(a1.get('cm_dependencies',[]))>0: ii['cm_dependencies']=a1['cm_dependencies']
00665        if flags!='': ii['run_set_env2']['CM_COMPILER_FLAGS_CC_OPTS']=flags
00666        if a1.get('compilation_type','')!='': ii['compilation_type']=a1['compilation_type']
00667 
00668        if a1.get('install','')=='yes': ii['install']='yes'
00669        if a1.get('install_data_uid','')=='yes': ii['install_data_uid']=a1['install_data_uid']
00670        if a1.get('install_repo_uoa','')!='': ii['install_repo_uoa']=a1['install_repo_uoa']
00671 
00672        if a1.get('clean_program','')!='': ii['clean_program']=a1['clean_program']
00673        if a1.get('clean_program_script_name','')!='': ii['clean_program_script_name']=a1['clean_program_script_name']
00674 
00675        if 'add_to_code_entry' in i: ii['add_to_code_entry']=i['add_to_code_entry']
00676        if 'add_rem_to_script' in i: ii['add_rem_to_script']=i['add_rem_to_script']
00677 
00678        last_command=ii
00679 
00680        r=cm_kernel.access_fe_through_cmd(ii)
00681        if r['cm_return']>0: return r
00682 
00683        del(i['cm_submit_build'])
00684 
00685     # Check if run
00686     elif 'cm_submit_run' in i:
00687        ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['code']
00688        ii['cm_action']='run'
00689 
00690        ii['run_target_os_uoa']=a1.get('build_target_os_uoa','')
00691        ii['run_cmd_key']=a1.get('run_cmd_key')
00692        ii['dataset_uoa']=a1.get('dataset_uoa')
00693 
00694        ii['run_set_env2']={}
00695        if a1.get('repeat_kernel','')!='': ii['run_set_env2']['CT_REPEAT_MAIN']=a1['repeat_kernel']
00696 
00697        last_command=ii
00698 
00699        r=cm_kernel.access_fe_through_cmd(ii)
00700        if r['cm_return']>0: return r
00701 
00702        del(i['cm_submit_run'])
00703 
00704 
00705     # Always redrawweb form
00706     # Check if setup is selected and prune choices *********************************************************************
00707     cm_scen_c=''
00708     cm_scen_p=''
00709 
00710     cm_setup_uoa=a1.get('ctuning_setup_uoa','')
00711     if cm_setup_uoa!='':
00712        # Load setup
00713        ii={}
00714        ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['ctuning.setup']
00715        ii['cm_action']='load'
00716        ii['cm_data_uoa']=cm_setup_uoa
00717        r=cm_kernel.access(ii)
00718        if r['cm_return']>0: return r
00719 
00720        ds=r['cm_data_obj']['cfg']
00721 
00722        cm_host_os_uoa_list=ds.get('cm_host_os_uoa_list',[])
00723        if len(cm_host_os_uoa_list)>0: cm_data_desc['##package_host_os_uoa']['choice']=cm_host_os_uoa_list
00724 
00725        cm_target_os_uoa_list=ds.get('cm_target_os_uoa_list',[])
00726        if len(cm_target_os_uoa_list)>0: cm_data_desc['##build_target_os_uoa']['choice']=cm_target_os_uoa_list
00727 
00728        run_target_processor_uoa_list=ds.get('run_target_processor_uoa_list',[])
00729        if len(run_target_processor_uoa_list)>0: cm_data_desc['##run_target_processor_uoa']['choice']=run_target_processor_uoa_list
00730 
00731        # Check class
00732        ctuning_scenario_uoa=ds.get('ctuning_scenario_uoa','')
00733        if ctuning_scenario_uoa!='':
00734           ii={}
00735           ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['ctuning.scenario']
00736           ii['cm_action']='load'
00737           ii['cm_data_uoa']=ctuning_scenario_uoa
00738           r=cm_kernel.access(ii)
00739           if r['cm_return']>0: return r
00740           ds1=r['cm_data_obj']['cfg']
00741 
00742           cm_scen_c=ds1.get('cm_classes_uoa',[])
00743           cm_scen_p=ds1.get('cm_properties',{})
00744 
00745     # This table is used to show json in the right corner
00746     cm_kernel.print_for_con('<br><table border="0"><tr>')
00747 
00748     cm_kernel.print_for_con('<td align="center">')
00749     cm_kernel.print_for_con('<FORM ACTION="" name="add_edit" METHOD="POST" enctype="multipart/form-data" accept-charset="utf-8">' )
00750 
00751     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00752         'cm_action':'visualize_data',
00753         'cm_array':a1,
00754         'cm_data_desc':cm_data_desc1,
00755         'cm_form_commands':cm_form_commands1,
00756         'cm_separator':'#',
00757         'cm_separator_form':'#form1#',
00758         'cm_forms_exists':forms_exists,
00759         'cm_support_raw_edit':'no',
00760         'cm_mode':'add'}
00761     if 'cm_raw_edit' in i: ii['cm_raw_edit']=i['cm_raw_edit']
00762     if 'cm_back_json' in i: ii['cm_back_json']=i['cm_back_json']
00763     r=cm_kernel.access(ii)
00764     if r['cm_return']>0: return r
00765     cm_kernel.print_for_web(r['cm_string'])
00766 
00767     cm_kernel.print_for_con('<input type="submit" class="cm-button" name="cm_submit_build" value="Build">')
00768     cm_kernel.print_for_con('<input type="submit" class="cm-button" name="cm_submit_run" value="Run">')
00769 
00770     # Pruning vars
00771 
00772     cm_t_os_uoa=a1.get('build_target_os_uoa','')
00773     cm_h_os_uoa=a1.get('package_host_os_uoa','')
00774     cm_p_uoa=a1.get('run_target_processor_uoa','')
00775     cm_repo_uoa=a1.get('package_repo_uoa','')
00776 
00777     # Check target OS classes and properties
00778     cm_t_os_c=''
00779     cm_t_os_p=''
00780     if cm_t_os_uoa!='':
00781        ii={}
00782        ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['os']
00783        ii['cm_action']='load'
00784        ii['cm_data_uoa']=cm_t_os_uoa
00785        r=cm_kernel.access(ii)
00786        if r['cm_return']>0: return r
00787 
00788        cm_t_os_uid=r['cm_uid']
00789        cm_t_os_uoa=r['cm_uoa']
00790 
00791        d=r['cm_data_obj']['cfg']
00792 
00793        cm_t_os_c=d.get('cm_classes_uoa',[])
00794        cm_t_os_p=d.get('cm_properties',{})
00795 
00796     cm_h_os_c=''
00797     cm_h_os_p=''
00798     cm_h_os_cfg={}
00799     if cm_h_os_uoa!='':
00800        ii={}
00801        ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['os']
00802        ii['cm_action']='load'
00803        ii['cm_data_uoa']=cm_h_os_uoa
00804        r=cm_kernel.access(ii)
00805        if r['cm_return']>0: return r
00806 
00807        cm_h_os_uid=r['cm_uid']
00808        cm_h_os_uoa=r['cm_uoa']
00809        cm_h_os_cfg=r['cm_data_obj']['cfg']
00810 
00811        d=cm_h_os_cfg
00812        cm_h_os_c=d.get('cm_classes_uoa',[])
00813        cm_h_os_p=d.get('cm_properties',{})
00814 
00815     cm_p_c=''
00816     cm_p_p=''
00817     if cm_p_uoa!='':
00818        ii={}
00819        ii['cm_run_module_uoa']=ini['cfg']['cm_modules']['processor']
00820        ii['cm_action']='load'
00821        ii['cm_data_uoa']=cm_p_uoa
00822        r=cm_kernel.access(ii)
00823        if r['cm_return']>0: return r
00824 
00825        cm_p_uid=r['cm_uid']
00826        cm_p_uoa=r['cm_uoa']
00827 
00828        d=r['cm_data_obj']['cfg']
00829 
00830        cm_p_c=d.get('cm_classes_uoa',[])
00831        cm_p_p=d.get('cm_properties',{})
00832 
00833     cm_kernel.print_for_con('<br>')
00834     cm_kernel.print_for_con('<BR>')
00835     cm_kernel.print_for_con('<input type="submit" class="cm-button" value="Refresh">')
00836 
00837     cm_kernel.print_for_con('</FORM><br>')
00838     cm_kernel.print_for_con('</td>')
00839     cm_kernel.print_for_con('<td valign="TOP">')
00840  
00841     # Debug/developer mode
00842     if a1.get('cm_show_json','')=='yes':
00843        cm_kernel.print_for_web('<div class="cm-round-div" align="left">')
00844        cm_kernel.print_for_con('<i><B><small>JSON to reproduce last command</small></B></i>')
00845        cm_kernel.print_for_con('<HR class="cm-hr">')
00846 
00847        if 'cm_detach_console' in last_command: del(last_command['cm_detach_console'])
00848        cm_kernel.print_for_web(json.dumps(last_command, indent=2).replace('\n','\n<BR>\n'))
00849 
00850        if last_command.get('cm_run_module_uoa','')!='':
00851           cm_kernel.print_for_con('<HR class="cm-hr">')
00852           cm_kernel.print_for_con('<b>Envoke command from CMD:</b><BR>')
00853           cm_kernel.print_for_con('<i>cm '+last_command['cm_run_module_uoa']+' @input.json</i>')
00854 
00855        cm_kernel.print_for_web('</div>')
00856 
00857     cm_kernel.print_for_con('</td></tr></table>')
00858 
00859     return {'cm_return':0}

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