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 csv
00016 import os
00017 import io
00018 import copy
00019 import base64
00020 import codecs
00021 
00022 # ============================================================================
00023 def init(i):
00024     return {'cm_return':0}
00025 
00026 # ============================================================================
00027 def detect(i):
00028 
00029     """
00030     Detect system properties
00031 
00032     Input:  {
00033               (host_os_uoa)                     - host OS on which cM is running to detect which scripts to use for detection. 
00034                                                   If not set, the default one is taken from cM configuration.
00035 
00036               (target_os_uoa)                   - target OS to detect. If =='', host OS is used
00037     
00038               (get_uoa_from_repo)               - if 'yes', check if already in repo and return UOA
00039 
00040               (detect_repo_uoa)                 - search entries in this repository (if not set, do not search),
00041                                                     if not found, add entries
00042 
00043               (record_if_doesnt_exist)          - if 'yes', record in repo if doesn't exist
00044 
00045               (ask_for_strings_if_not_detected) - if 'yes' and strings are not detected, ask for them
00046 
00047               (add_all)                         - if 'yes' add all information including generated files about system
00048                                                   (used in private setups to keep all information for debugging
00049                                                    or machine learning (to improve models and find appropriate features))
00050             }
00051 
00052     Output: {
00053               cm_return           - return code = 0, if successful
00054 
00055               cm_array            - {
00056                                       processor_string
00057                                       system_string
00058                                       available_physical_memory
00059                                     }
00060 
00061               processor_string       - processor
00062               os_string              - OS
00063               system_string          - System
00064               memory_string          - All memory
00065               all_frequencies_string - All possible frequencies
00066 
00067 
00068               processor_uoa       - detected processor UOA or taken from the input
00069               os_uoa              - detected OS UOA or taken from the input
00070               system_uoa          - prepared system UOA
00071             }
00072     """
00073 
00074     # Check some vars
00075     processor_uoa=i.get('processor_uoa','')
00076     processor_string=''
00077     os_uoa=i.get('os_uoa','')
00078     os_string=''
00079     system_uoa=i.get('system_uoa','')
00080     ignore_system=i.get('ignore_system','')
00081     system_type_uoa=i.get('system_type_uoa','')
00082     system_type_string=''
00083     system_provider_uoa=i.get('system_provider_uoa','')
00084     system_provider_string=''
00085     system_name=i.get('system_name','')
00086     system_string=''
00087     system_manufacturer=''
00088     available_physical_memory=''
00089     cpu_freqs_all=''
00090     cpu_freq_cur=''
00091 
00092     cm_array={}
00093 
00094     rr={'cm_return':0}
00095 
00096     # Check host OS
00097     host_os_uoa=''
00098     if i.get('host_os_uoa','')!='':
00099        host_os_uoa=i['host_os_uoa']
00100     elif 'cm_default_os_uoa' in cm_kernel.ini['dcfg'] and cm_kernel.ini['dcfg']['cm_default_os_uoa']!='':
00101        host_os_uoa=cm_kernel.ini['dcfg']['cm_default_os_uoa']
00102 
00103     if host_os_uoa=='' not in i:
00104        return {'cm_return':1, 'cm_error':'"host_os_uoa" is not defined and not in kernel'}
00105 
00106     cm_kernel.print_for_con('')
00107     cm_kernel.print_for_con('Loading host os ('+host_os_uoa+') ...')
00108     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00109         'cm_action':'load',
00110         'cm_data_uoa':host_os_uoa}
00111     r=cm_kernel.access(ii)
00112     if r['cm_return']>0: return r
00113 
00114     host_os_cfg=r['cm_data_obj']['cfg']
00115     host_os_path=r['cm_path']
00116     host_os_uid=r['cm_uid']
00117     host_os_alias=r['cm_alias']
00118 
00119     # Check target OS
00120     target_os_uoa=''
00121     if i.get('target_os_uoa','')!='': target_os_uoa=i['target_os_uoa']
00122     else: target_os_uoa=host_os_uid
00123 
00124     cm_kernel.print_for_con('')
00125     cm_kernel.print_for_con('Loading target os ('+target_os_uoa+') ...')
00126     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00127         'cm_action':'load',
00128         'cm_data_uoa':target_os_uoa}
00129     r=cm_kernel.access(ii)
00130     if r['cm_return']>0: return r
00131 
00132     target_os_cfg=r['cm_data_obj']['cfg']
00133     target_os_path=r['cm_path']
00134     target_os_uid=r['cm_uid']
00135     target_os_alias=r['cm_alias']
00136 
00137     # Generate tmp file
00138     r=cm_kernel.gen_cm_tmp_file({})
00139     if r['cm_return']>0: return r
00140     tf=r['cm_path']
00141     tf1=r['cm_path1']
00142     tff={}
00143 
00144     if target_os_cfg.get('remote','')=='yes': tf1=os.path.basename(tf1)
00145 
00146     # Prepare files depending on OS
00147     if ini['cfg']['cm_data']['os_classes']['windows'] in target_os_cfg.get('cm_classes_uoa',[]) or \
00148        ini['cfg']['cm_data']['os_classes']['windows_mingw'] in target_os_cfg.get('cm_classes_uoa',[]):
00149       tff['sysinfo']=tf1+'.systeminfo.csv'
00150       tff['proc']=tf1+'.processor.reg'
00151     else:
00152       tff['cpu']=tf1+'.cpuinfo.txt'
00153       tff['cpu.freq.all']=tf1+'.cpu.freq.all.txt'
00154       tff['cpu.freq.cur']=tf1+'.cpu.freq.cur.txt'
00155       tff['os']=tf1+'.version.txt'
00156       tff['mem']=tf1+'.meminfo.txt'
00157       tff['sys']=tf1+'.system_build_prop.txt'
00158       tff['dmesg']=tf1+'.dmesg.txt'
00159 
00160     # Prepare files depending on OS
00161     for q in tff:
00162         if os.path.isfile(q): os.remove(q)
00163 
00164     # Run system detection script
00165     cm_kernel.print_for_con('')
00166     cm_kernel.print_for_con('Execute script')
00167     cm_kernel.print_for_con('')
00168 
00169     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['code'],
00170         'cm_action':'run',
00171         'run_host_os_uoa':host_os_uid,
00172         'run_target_os_uoa':target_os_uid,
00173         'run_script':ini['cfg']['cm_data']['run_script_name'],
00174         'run_script_uoa':ini['cfg']['cm_data']['run_script_uoa'],
00175         'run_set_env2':{'SYS_OUT_FILE':tf1},
00176         'calming_delay':''}
00177     work_dir=cm_kernel.ini[cm_kernel.env_cm_tmp]
00178     if work_dir!='': ii['work_dir']=work_dir
00179     if target_os_cfg.get('remote','')=='yes': 
00180        ii['run_output_files']=[]
00181        for q in tff: 
00182            if tff[q] not in ii['run_output_files']: ii['run_output_files'].append(tff[q])
00183     r=cm_kernel.access(ii)
00184     if r['cm_return']>0: return r
00185 
00186     system={}
00187 
00188     s_sys=''
00189     s_cpu=''
00190     s_os=''
00191     cpu_freq_all=[]
00192 
00193     # Check file(s) depending on OS
00194     if ini['cfg']['cm_data']['os_classes']['windows'] in target_os_cfg.get('cm_classes_uoa',[]) or \
00195        ini['cfg']['cm_data']['os_classes']['windows_mingw'] in target_os_cfg.get('cm_classes_uoa',[]):
00196        if os.path.isfile(tff['sysinfo']):
00197           cm_kernel.print_for_con('')
00198           cm_kernel.print_for_con('Processing output (Windows)')
00199 
00200           # Read system info
00201           f=open(tff['sysinfo'], 'rt')
00202           while True:
00203               line = f.readline()
00204               if not line: break
00205               s_sys+=line.replace(chr(255),',')
00206           f.close()
00207 
00208           f=open(tff['sysinfo'], 'rb')
00209           c=csv.reader(f, delimiter=',')
00210 
00211           l=[]
00212           for q in c:
00213               l.append(q)
00214 
00215           os_name=''
00216           os_version=''
00217           if len(l)>0:
00218              for q in range(0, len(l[0])):
00219                  if l[0][q].lower()=='os name': os_name=l[1][q].strip()
00220                  elif l[0][q].lower()=='os version': os_version=l[1][q].strip()
00221                  elif l[0][q].lower()=='system manufacturer': system_manufacturer=l[1][q].strip()
00222                  elif l[0][q].lower()=='system model': system_model=l[1][q].strip()
00223                  elif l[0][q].lower()=='available physical memory': available_physical_memory=l[1][q].strip().replace(chr(255),'')
00224 
00225           f.close()
00226 
00227           if system_manufacturer!='' and system_model!='': system_string=system_manufacturer+' '+system_model
00228 
00229           if os_name!='' and os_version!='': os_string=os_name+' '+os_version
00230 
00231           # Scan processor
00232           f=io.open(tff['proc'], 'r', encoding='utf-16')
00233           while True:
00234               line = f.readline()
00235               if not line: break
00236               line=line.strip('\r\n')
00237               x='"ProcessorNameString"='
00238               if line.startswith(x):
00239                  processor_string=line[len(x):].strip('"')
00240           f.close()
00241 
00242           os.remove(tff['sysinfo'])
00243           os.remove(tff['proc'])
00244        else:
00245           cm_kernel.print_for_con('')
00246           cm_kernel.print_for_con('Output files not found!')
00247   
00248     else:
00249        if os.path.isfile(tff['cpu']) or os.path.isfile(tff['os']) or os.path.isfile(tff['mem']):
00250           cm_kernel.print_for_con('')
00251           cm_kernel.print_for_con('Processing output (Unix)')
00252 
00253           # Process cpu file
00254           if os.path.isfile(tff['cpu']):
00255              f=open(tff['cpu'], 'rt')
00256              while True:
00257                  line = f.readline()
00258                  if not line: break
00259                  s_cpu+=line
00260                  line=line.strip('\r\n ')
00261                  q=line.find(':')
00262                  if q!=0:
00263                     k=line[0:q].strip('\r\n\t ')
00264                     v=line[q+1:].strip(' ')
00265                     if k=='Processor' and len(v)>2: processor=v
00266                     if k=='model name': processor_string=v
00267              f.close()
00268 
00269              if processor_string=='' and processor!='': processor_string=processor
00270 
00271              os.remove(tff['cpu'])
00272 
00273           # Process all freq
00274           if os.path.isfile(tff['cpu.freq.all']):
00275              f=open(tff['cpu.freq.all'], 'rt')
00276              line = f.readline().strip('\r\n\t ')
00277              f.close()
00278 
00279              cpu_freqs_all=line
00280              cpu_freq_all=line.split()
00281 
00282              os.remove(tff['cpu.freq.all'])
00283 
00284           # Process cur freq
00285           if os.path.isfile(tff['cpu.freq.cur']):
00286              f=open(tff['cpu.freq.cur'], 'rt')
00287              line = f.readline().strip('\r\n\t ')
00288              f.close()
00289 
00290              cpu_freq_cur=line
00291 
00292              os.remove(tff['cpu.freq.cur'])
00293 
00294           # Process OS file
00295           if os.path.isfile(tff['os']):
00296 
00297              f=open(tff['os'], 'rt')
00298              line = f.readline()
00299              s_os=line.strip('\r\n ')
00300              f.close()
00301 
00302              if s_os!='': os_string=s_os
00303 
00304              os.remove(tff['os'])
00305 
00306           # Process memory file
00307           if os.path.isfile(tff['mem']):
00308 
00309              mem_total=''
00310              f=open(tff['mem'], 'rt')
00311              while True:
00312                  line = f.readline()
00313                  if not line: break
00314                  line=line.strip('\r\n ')
00315                  q=line.find(':')
00316                  if q!=0:
00317                     k=line[0:q].strip('\r\n\t ').lower()
00318                     v=line[q+1:].strip(' ')
00319                     if k=='memtotal' and len(v)>0: mem_total=v
00320              f.close()
00321 
00322              os.remove(tff['mem'])
00323 
00324              if mem_total!='':
00325                 mem=mem_total.split(' ', 1);
00326                 if len(mem)>0:
00327                    mem1=mem[0]
00328                    mem2='1'
00329                    if len(mem1)>3:
00330                       mem2=mem1[:-3]
00331 
00332                    available_physical_memory=mem2+' MB'
00333 
00334           # Process system file
00335           if os.path.isfile(tff['sys']):
00336 
00337              manufacturer=''
00338              product_name=''
00339 
00340              f=open(tff['sys'], 'rt')
00341              while True:
00342                  line = f.readline()
00343                  if not line: break
00344                  line=line.strip('\r\n ')
00345                  q=line.find('=')
00346                  if q!=0:
00347                     k=line[0:q].strip('\r\n\t ').lower()
00348                     v=line[q+1:].strip(' ')
00349                     if k=='ro.product.manufacturer' and len(v)>0: manufacturer=v
00350                     elif k=='ro.product.name' and len(v)>0: product_name=v
00351                     elif k=='ro.build.product' and len(v)>0: 
00352                          if product_name=='': 
00353                             product_name=v
00354              f.close()
00355 
00356              os.remove(tff['sys'])
00357 
00358              if manufacturer!='' and product_name!='':
00359                 system_string=manufacturer[0].upper()+manufacturer[1:]+' '+product_name
00360 
00361           # Process system file
00362           if system_string=='' and os.path.isfile(tff['dmesg']):
00363 
00364              dmi=''
00365 
00366              f=open(tff['dmesg'], 'rt')
00367              while True:
00368                  line = f.readline()
00369                  if not line: break
00370                  line=line.strip('\r\n ')
00371                  q=line.lower().find(' dmi: ')
00372                  if q>0:
00373                     dmi=line[q+5:].strip(' ')
00374                     break
00375              f.close()
00376 
00377              os.remove(tff['dmesg'])
00378 
00379              if dmi!='':
00380                 q1=dmi.find('  ')
00381                 if q1>0: dmi=dmi[:q1].strip()
00382 
00383              if dmi!='':
00384                 q1=dmi.lower().find(', bios')
00385                 if q1>0: dmi=dmi[:q1].strip()
00386 
00387              if dmi!='':
00388                 q1=dmi.lower().find('/')
00389                 if q1>0: dmi=dmi[:q1].strip()
00390 
00391              if dmi!='':
00392                 system_string=dmi
00393 
00394     # Ask if system was not automatically detected
00395     if system_string=='' and i.get('ask_for_strings_if_not_detected','')=='yes':
00396        cm_kernel.print_for_con('')
00397        cm_kernel.print_for_con('System string was not automatically detected!')
00398 
00399        cm_kernel.print_for_con('')
00400        system_string=raw_input('Enter system description in a format "<Manufacturer> <System name>": ')
00401 
00402        system_string=system_string.strip()
00403 
00404     if os_string=='' and i.get('ask_for_strings_if_not_detected','')=='yes':
00405        cm_kernel.print_for_con('')
00406        cm_kernel.print_for_con('OS string was not automatically detected!')
00407 
00408        cm_kernel.print_for_con('')
00409        os_string=raw_input('Enter OS description: ')
00410 
00411        os_string=os_string.strip()
00412 
00413     if available_physical_memory=='' and i.get('ask_for_strings_if_not_detected','')=='yes':
00414        cm_kernel.print_for_con('')
00415        cm_kernel.print_for_con('Memory string was not automatically detected!')
00416 
00417        cm_kernel.print_for_con('')
00418        available_physical_memory=raw_input('Enter memory description: ')
00419 
00420        available_physical_memory=available_physical_memory.strip()
00421 
00422     # Finalizing system detection
00423 
00424     processor_string=processor_string.replace(unichr(255),',')
00425     processor_string=" ".join(processor_string.split())
00426 
00427     cm_array['processor_string']=processor_string
00428     cm_array['system_string']=system_string
00429     cm_array['available_physical_memory']=available_physical_memory
00430 
00431     rr['cm_array']=cm_array
00432     rr['processor_string']=processor_string
00433     rr['os_string']=os_string
00434     rr['system_string']=system_string
00435     rr['memory_string']=available_physical_memory
00436     rr['all_frequencies_string']=cpu_freqs_all
00437 
00438     cm_kernel.print_for_con('')
00439     cm_kernel.print_for_con('Processor string: '+processor_string)
00440     cm_kernel.print_for_con('   Memory string: '+available_physical_memory)
00441     cm_kernel.print_for_con('       OS string: '+os_string)
00442     cm_kernel.print_for_con('   System string: '+system_string)
00443     cm_kernel.print_for_con('')
00444     cm_kernel.print_for_con(' All frequencies: '+cpu_freqs_all)
00445     cm_kernel.print_for_con('  Cur. frequency: '+cpu_freq_cur)
00446 
00447     if i.get('get_uoa_from_repo','')!='yes': 
00448        return rr
00449 
00450     # Check in the repository
00451     repo=i.get('detect_repo_uoa','')
00452     if repo=='':
00453        return {'cm_return':1, 'cm_error':'repository is not defined'}
00454 
00455     record=i.get('record_if_doesnt_exist','')
00456     notes=i.get('notes','')
00457 
00458     # Check/add processor *****************************************************************
00459     if processor_string!='':
00460 
00461        cm_kernel.print_for_con('')
00462        cm_kernel.print_for_con('Searching processor "'+processor_string+'" in the repository ...')
00463 
00464        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-core'],
00465            'cm_action':'search',
00466            'key_0':'##cm_properties#processor_string',
00467            'value_0':processor_string,
00468            'use_flat_array':'yes',
00469            'module_selection':[ini['cfg']['cm_modules']['ctuning.processor']]
00470           }
00471        if repo!='': ii['repo_selection']=[repo]
00472        r=cm_kernel.access(ii)
00473        if r['cm_return']>0: return r
00474 
00475        all=r['all']
00476 
00477        if len(all)==0:
00478           cm_kernel.print_for_con('')
00479           cm_kernel.print_for_con('Processor not found in the repository!')
00480 
00481           # Prepare to add
00482           jj={'cm_run_module_uoa':ini['cfg']['cm_modules']['ctuning.processor'],
00483               'cm_action':'add',
00484               'cm_array':{'cm_properties':{'processor_string':processor_string, 'cpu_frequency_all':cpu_freq_all}},
00485               'cm_display_as_alias':processor_string,
00486               'cm_repo_uoa':repo,
00487               'cm_note':notes
00488              }
00489 
00490           if i.get('add_all','')=='yes':
00491              if s_cpu!='':
00492                 jj['cm_file_upload']=s_cpu
00493                 jj['cm_file_upload_name']='cpuinfo.txt'
00494              elif s_sys!='':
00495                 jj['cm_file_upload_base64']=base64.urlsafe_b64encode(s_sys)
00496                 jj['cm_file_upload_name']='systeminfo.csv'
00497 
00498           r=cm_kernel.access(jj)
00499           if r['cm_return']>0: return r
00500 
00501           processor_uoa=r['cm_uid']
00502 
00503           cm_kernel.print_for_con('')
00504           cm_kernel.print_for_con('Processor entry added: '+processor_uoa)
00505 
00506        else:
00507           processor_uoa=all[0]['cm_data_uoa']
00508 
00509           cm_kernel.print_for_con('')
00510           cm_kernel.print_for_con('Processor entry found: '+processor_uoa)
00511 
00512     # Check/add OS *****************************************************************
00513     if os_string!='':
00514 
00515        cm_kernel.print_for_con('')
00516        cm_kernel.print_for_con('Searching OS "'+os_string+'" in the repository ...')
00517 
00518        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-core'],
00519            'cm_action':'search',
00520            'key_0':'##cm_properties#os_string',
00521            'value_0':os_string,
00522            'use_flat_array':'yes',
00523            'module_selection':[ini['cfg']['cm_modules']['ctuning.os']],
00524            'cm_repo_uoa':repo
00525           }
00526        r=cm_kernel.access(ii)
00527        if r['cm_return']>0: return r
00528 
00529        all=r['all']
00530 
00531        if len(all)==0:
00532           cm_kernel.print_for_con('')
00533           cm_kernel.print_for_con('OS not found in the repository!')
00534 
00535           # Prepare to add
00536           jj={'cm_run_module_uoa':ini['cfg']['cm_modules']['ctuning.os'],
00537               'cm_action':'add',
00538               'cm_array':{'cm_properties':{'os_string':os_string}},
00539               'cm_display_as_alias':os_string,
00540               'cm_repo_uoa':repo,
00541               'cm_note':notes
00542              }
00543 
00544           r=cm_kernel.access(jj)
00545           if r['cm_return']>0: return r
00546 
00547           os_uoa=r['cm_uid']
00548 
00549           cm_kernel.print_for_con('')
00550           cm_kernel.print_for_con('OS entry added: '+os_uoa)
00551        else:
00552           os_uoa=all[0]['cm_data_uoa']
00553 
00554           cm_kernel.print_for_con('')
00555           cm_kernel.print_for_con('OS entry found: '+os_uoa)
00556 
00557     # Check/add system *****************************************************************
00558     if system_string!='':
00559 
00560        cm_kernel.print_for_con('')
00561        cm_kernel.print_for_con('Searching System "'+system_string+'" in the repository ...')
00562 
00563        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-core'],
00564            'cm_action':'search',
00565            'key_0':'##cm_properties#system_string',
00566            'value_0':system_string,
00567            'use_flat_array':'yes',
00568            'module_selection':[ini['cfg']['cm_modules']['ctuning.system']],
00569            'cm_repo_uoa':repo
00570           }
00571        r=cm_kernel.access(ii)
00572        if r['cm_return']>0: return r
00573 
00574        all=r['all']
00575 
00576        if len(all)==0:
00577           cm_kernel.print_for_con('')
00578           cm_kernel.print_for_con('System not found in the repository!')
00579 
00580           # Prepare to add
00581           jj={'cm_run_module_uoa':ini['cfg']['cm_modules']['ctuning.system'],
00582               'cm_action':'add',
00583               'cm_array':{'cm_properties':{'system_string':system_string}, 
00584                           'cm_misc_info':cm_array},
00585               'cm_display_as_alias':system_string,
00586               'cm_repo_uoa':repo,
00587               'cm_note':notes
00588              }
00589 
00590           if system_type_uoa!='':
00591              jj['cm_array']['system_type_uoa']=system_type_uoa
00592 
00593           r=cm_kernel.access(jj)
00594           if r['cm_return']>0: return r
00595 
00596           system_uoa=r['cm_uid']
00597 
00598           cm_kernel.print_for_con('')
00599           cm_kernel.print_for_con('System entry added: '+system_uoa)
00600        else:
00601           system_uoa=all[0]['cm_data_uoa']
00602 
00603           cm_kernel.print_for_con('')
00604           cm_kernel.print_for_con('System entry found: '+system_uoa)
00605 
00606 
00607     cm_kernel.print_for_con('')
00608     cm_kernel.print_for_con('Detection finished!')
00609 
00610     rr['cm_return']=0
00611 
00612     rr['processor_uoa']=processor_uoa
00613     rr['os_uoa']=os_uoa
00614     rr['system_uoa']=system_uoa
00615 
00616     return rr
00617 
00618 # ============================================================================
00619 def web_detect(i):
00620 
00621     """
00622     Detect system properties through Web
00623 
00624     Input:  See detect(i)
00625     Output: See detect(i)
00626 
00627     """
00628 
00629     # Get web style
00630     if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
00631     else:
00632        return {'cm_return':1, 'cm_error':'web style is not defined'}
00633 
00634     cm_kernel.print_for_con('<span class="cm-title">Prepare/detect system for collective tuning</span><br>')
00635 
00636     # Detecting/restoring data from forms
00637     a1={}
00638     r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00639                         'cm_action':'detect_form_params',
00640                         'cm_array':i, 
00641                         'cm_prefix':'#form1'})
00642     if r['cm_return']>0: return r
00643     cm_form_array1=r['cm_array']
00644     cm_form_commands1=r['cm_commands']
00645 
00646     # Get data description for this action
00647     r=cm_kernel.get_data_description({'cm_module_uoa':ini['cm_module_uoa'], 
00648                                       'cm_which_action':i['cm_action']})
00649     if r['cm_return']>0: return r
00650     cm_data_desc1=r['cm_data_desc']
00651     cm_params_default1=r['cm_params_default']
00652 
00653     # Check default
00654     forms_exists='yes'
00655     if len(cm_form_array1)==0:
00656        a1=cm_params_default1
00657 
00658        forms_exists='no'
00659     else:
00660        r=cm_kernel.restore_flattened_array({'cm_array':cm_form_array1, 
00661                                             'cm_replace_in_keys':{'^35^':'#', '^64^':'@'}})
00662        if r['cm_return']>0: return r
00663        a1=r['cm_array']
00664 
00665     # Check if start detection
00666     if 'cm_submit_detect' in i:
00667        ii={}
00668        ii['cm_run_module_uoa']=ini['cm_module_uid']
00669        ii['cm_action']='detect'
00670        ii['cm_detach_console']='yes'
00671        ii.update(a1)
00672        r=cm_kernel.access_fe_through_cmd(ii)
00673        if r['cm_return']>0: return r
00674 
00675        del(i['cm_submit_detect'])
00676 
00677     # This table is used to show json in the right corner
00678     cm_kernel.print_for_con('<br><table border="0"><tr>')
00679 
00680     cm_kernel.print_for_con('<td align="center">')
00681     cm_kernel.print_for_con('<FORM ACTION="" name="add_edit" METHOD="POST" enctype="multipart/form-data" accept-charset="utf-8">' )
00682 
00683     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00684         'cm_action':'visualize_data',
00685         'cm_array':a1,
00686         'cm_data_desc':cm_data_desc1,
00687         'cm_form_commands':cm_form_commands1,
00688         'cm_separator':'#',
00689         'cm_separator_form':'#form1#',
00690         'cm_forms_exists':forms_exists,
00691         'cm_support_raw_edit':'no',
00692         'cm_mode':'add'}
00693     if 'cm_raw_edit' in i: ii['cm_raw_edit']=i['cm_raw_edit']
00694     if 'cm_back_json' in i: ii['cm_back_json']=i['cm_back_json']
00695     r=cm_kernel.access(ii)
00696     if r['cm_return']>0: return r
00697     cm_kernel.print_for_web(r['cm_string'])
00698 
00699     cm_kernel.print_for_con('<input type="submit" class="cm-button" name="cm_submit_detect" value="Detect / add">')
00700 
00701     cm_kernel.print_for_con('<input type="submit" class="cm-button" value="Refresh">')
00702 
00703     cm_kernel.print_for_con('</FORM><br>')
00704     cm_kernel.print_for_con('</td>')
00705     cm_kernel.print_for_con('<td valign="TOP">')
00706  
00707     # Debug/developer mode
00708     if a1.get('cm_show_json','')=='yes':
00709        cm_kernel.print_for_web('<div class="cm-round-div">')
00710        cm_kernel.print_for_con('<i><B><small>JSON to reproduce last command</small></B></i>')
00711        cm_kernel.print_for_con('<HR class="cm-hr">')
00712 
00713        cm_kernel.print_for_web(json.dumps(last_command, indent=2).replace('\n','\n<BR>\n'))
00714 
00715        cm_kernel.print_for_con('<HR class="cm-hr">')
00716        cm_kernel.print_for_con('<b>Envoke command from CMD:</b><BR>')
00717        cm_kernel.print_for_con('<i>cm '+last_command.get('cm_run_module_uoa')+' @input.json</i>')
00718 
00719        cm_kernel.print_for_web('</div>')
00720 
00721     cm_kernel.print_for_con('</td></tr></table>')
00722 
00723     return {'cm_return':0}
00724 
00725 
00726 # ============================================================================
00727 def get_state(i):
00728 
00729     """
00730     Get current state
00731 
00732     Input:  {
00733               (host_os_uoa)        - host OS on which cM is running to detect which scripts to use for detection. 
00734                                      If not set, the default one is taken from cM configuration.
00735 
00736               (target_os_uoa)      - target OS to detect. If =='', host OS is used
00737             }
00738 
00739     Output: {
00740               cm_return - return code = 0, if successful
00741 
00742               system_state         - [windows]
00743                                      [unix]
00744             }
00745     """
00746 
00747     # Check host OS
00748     host_os_uoa=''
00749     if i.get('host_os_uoa','')!='':
00750        host_os_uoa=i['host_os_uoa']
00751     elif 'cm_default_os_uoa' in cm_kernel.ini['dcfg'] and cm_kernel.ini['dcfg']['cm_default_os_uoa']!='':
00752        host_os_uoa=cm_kernel.ini['dcfg']['cm_default_os_uoa']
00753 
00754     if host_os_uoa=='' not in i:
00755        return {'cm_return':1, 'cm_error':'"host_os_uoa" is not defined and not in kernel'}
00756 
00757     cm_kernel.print_for_con('')
00758     cm_kernel.print_for_con('Loading host os ('+host_os_uoa+') ...')
00759     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00760         'cm_action':'load',
00761         'cm_data_uoa':host_os_uoa}
00762     r=cm_kernel.access(ii)
00763     if r['cm_return']>0: return r
00764 
00765     host_os_cfg=r['cm_data_obj']['cfg']
00766     host_os_path=r['cm_path']
00767     host_os_uid=r['cm_uid']
00768     host_os_alias=r['cm_alias']
00769 
00770     # Check target OS
00771     target_os_uoa=''
00772     if i.get('target_os_uoa','')!='': target_os_uoa=i['target_os_uoa']
00773     else: target_os_uoa=host_os_uid
00774 
00775     cm_kernel.print_for_con('')
00776     cm_kernel.print_for_con('Loading target os ('+target_os_uoa+') ...')
00777     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00778         'cm_action':'load',
00779         'cm_data_uoa':target_os_uoa}
00780     r=cm_kernel.access(ii)
00781     if r['cm_return']>0: return r
00782 
00783     target_os_cfg=r['cm_data_obj']['cfg']
00784     target_os_path=r['cm_path']
00785     target_os_uid=r['cm_uid']
00786     target_os_alias=r['cm_alias']
00787 
00788     # Generate tmp file
00789     r=cm_kernel.gen_cm_tmp_file({})
00790     if r['cm_return']>0: return r
00791     tf=r['cm_path']
00792     tf1=r['cm_path1']
00793     tff={}
00794 
00795     if target_os_cfg.get('remote','')=='yes': tf1=os.path.basename(tf1)
00796 
00797     # Prepare files depending on OS
00798     if ini['cfg']['cm_data']['os_classes']['windows'] in target_os_cfg.get('cm_classes_uoa',[]):
00799       tff['cpu']=tf1+'.cpu.txt'
00800     else:
00801       tff['cpu.info']=tf1+'.cpu.info.txt'
00802       tff['cpu.freq.all']=tf1+'.cpu.freq.all.txt'
00803       tff['cpu.freq.cur']=tf1+'.cpu.freq.cur.txt'
00804 
00805     # Run system state detection script
00806     cm_kernel.print_for_con('')
00807     cm_kernel.print_for_con('Execute script')
00808     cm_kernel.print_for_con('')
00809 
00810     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['code'],
00811         'cm_action':'run',
00812         'run_host_os_uoa':host_os_uid,
00813         'run_target_os_uoa':target_os_uid,
00814         'run_script':ini['cfg']['cm_data']['run_script_name_get_state'],
00815         'run_script_uoa':ini['cfg']['cm_data']['run_script_uoa'],
00816         'run_set_env2':{'SYS_OUT_FILE':tf1},
00817         'calming_delay':''}
00818     work_dir=cm_kernel.ini[cm_kernel.env_cm_tmp]
00819     if work_dir!='': ii['work_dir']=work_dir
00820     if target_os_cfg.get('remote','')=='yes': 
00821        ii['run_output_files']=[]
00822        for q in tff: 
00823            if tff[q] not in ii['run_output_files']: ii['run_output_files'].append(tff[q])
00824     r=cm_kernel.access(ii)
00825     if r['cm_return']>0: return r
00826 
00827     system_state={}
00828     system_state['properties']={}
00829     ssp=system_state['properties']
00830 
00831     # Check file(s) depending on OS
00832     if ini['cfg']['cm_data']['os_classes']['windows'] in target_os_cfg.get('cm_classes_uoa',[]):
00833        ssp['windows']={}
00834        ssp['windows']['cpu_state']={}
00835        ss=ssp['windows']['cpu_state']
00836        if os.path.isfile(tff['cpu']):
00837           cm_kernel.print_for_con('')
00838           cm_kernel.print_for_con('Processing output (Windows)')
00839 
00840           f=codecs.open(tff['cpu'], encoding='utf-16', mode='r')
00841           line='x'
00842           while line!='':
00843               line=f.readline()
00844               line1=line.strip()
00845               q=line1.find('=')
00846               if q>0:
00847                  k=line1[0:q].strip(' ')
00848                  v=line1[q+1:].strip(' ')
00849                  ss[k]=v
00850           f.close()
00851 
00852           if 'AddressWidth' in ss: ssp['cpu_address_width']=ss['AddressWidth']
00853           if 'DataWidth' in ss: ssp['cpu_data_width']=ss['DataWidth']
00854           if 'CurrentClockSpeed' in ss: ssp['cpu_frequency']=str(float(ss['CurrentClockSpeed'])*1000)
00855           if 'MaxClockSpeed' in ss: ssp['cpu_max_frequency']=str(float(ss['MaxClockSpeed'])*1000)
00856           if 'L2CacheSize' in ss: ssp['cache_l2_size']=ss['L2CacheSize']
00857           if 'L3CacheSize' in ss: ssp['cache_l3_size']=ss['L3CacheSize']
00858           if 'NumberOfCores' in ss: ssp['number_of_cores']=ss['NumberOfCores']
00859           if 'NumberOfLogicalProcessors' in ss: ssp['number_of_logical_processors']=ss['NumberOfLogicalProcessors']
00860 
00861           os.remove(tff['cpu'])
00862 
00863     else:
00864        ssp['unix']={}
00865        ssp['unix']['cpu_state']={}
00866        ss=ssp['unix']['cpu_state']
00867 
00868        cm_kernel.print_for_con('')
00869        cm_kernel.print_for_con('Processing output (Unix)')
00870 
00871        # Process cpu file
00872        if os.path.isfile(tff['cpu.info']):
00873           f=open(tff['cpu.info'], 'rt')
00874           while True:
00875               line = f.readline()
00876               if not line: break
00877               line=line.strip('\r\n\t ')
00878               q=line.find(':')
00879               if q!=0:
00880                  k=line[0:q].strip('\r\n\t ')
00881                  v=line[q+1:].strip('\r\n\t ')
00882                  ss[k]=v
00883           f.close()
00884 
00885           os.remove(tff['cpu.info'])
00886 
00887        # Process all freq
00888        if os.path.isfile(tff['cpu.freq.all']):
00889           f=open(tff['cpu.freq.all'], 'rt')
00890           line = f.readline().strip('\r\n\t ')
00891           f.close()
00892 
00893           ssp['cpu_frequency_all']=line.split()
00894 
00895           os.remove(tff['cpu.freq.all'])
00896 
00897        # Process cur freq
00898        if os.path.isfile(tff['cpu.freq.cur']):
00899           f=open(tff['cpu.freq.cur'], 'rt')
00900           line = f.readline().strip('\r\n\t ')
00901           f.close()
00902 
00903           ssp['cpu_frequency']=line
00904 
00905           os.remove(tff['cpu.freq.cur'])
00906 
00907     return {'cm_return':0, 'system_state':system_state}
00908 
00909 # ============================================================================
00910 def set_cpu_frequency(i):
00911 
00912     """
00913     Set CPU frequency
00914 
00915     Input:  {
00916               (host_os_uoa)        - host OS on which cM is running to detect which scripts to use for detection. 
00917                                      If not set, the default one is taken from cM configuration.
00918 
00919               (target_os_uoa)      - target OS to detect. If =='', host OS is used
00920               frequency            - frequency
00921               (use_sudo)           - if 'yes', use SUDO
00922             }
00923 
00924     Output: {
00925               cm_return - return code = 0, if successful
00926 
00927               system_state         - [windows]
00928                                      [unix]
00929             }
00930     """
00931 
00932     # Check host OS
00933     host_os_uoa=''
00934     if i.get('host_os_uoa','')!='':
00935        host_os_uoa=i['host_os_uoa']
00936     elif 'cm_default_os_uoa' in cm_kernel.ini['dcfg'] and cm_kernel.ini['dcfg']['cm_default_os_uoa']!='':
00937        host_os_uoa=cm_kernel.ini['dcfg']['cm_default_os_uoa']
00938 
00939     if host_os_uoa=='' not in i:
00940        return {'cm_return':1, 'cm_error':'"host_os_uoa" is not defined and not in kernel'}
00941 
00942     cm_kernel.print_for_con('')
00943     cm_kernel.print_for_con('Loading host os ('+host_os_uoa+') ...')
00944     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00945         'cm_action':'load',
00946         'cm_data_uoa':host_os_uoa}
00947     r=cm_kernel.access(ii)
00948     if r['cm_return']>0: return r
00949 
00950     host_os_cfg=r['cm_data_obj']['cfg']
00951     host_os_path=r['cm_path']
00952     host_os_uid=r['cm_uid']
00953     host_os_alias=r['cm_alias']
00954 
00955     # Check target OS
00956     target_os_uoa=''
00957     if i.get('target_os_uoa','')!='': target_os_uoa=i['target_os_uoa']
00958     else: target_os_uoa=host_os_uid
00959 
00960     cm_kernel.print_for_con('')
00961     cm_kernel.print_for_con('Loading target os ('+target_os_uoa+') ...')
00962     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['os'],
00963         'cm_action':'load',
00964         'cm_data_uoa':target_os_uoa}
00965     r=cm_kernel.access(ii)
00966     if r['cm_return']>0: return r
00967 
00968     target_os_cfg=r['cm_data_obj']['cfg']
00969     target_os_path=r['cm_path']
00970     target_os_uid=r['cm_uid']
00971     target_os_alias=r['cm_alias']
00972 
00973     # Run script
00974     cm_kernel.print_for_con('')
00975     cm_kernel.print_for_con('Execute script')
00976     cm_kernel.print_for_con('')
00977 
00978     sudo=''
00979     if i.get('use_sudo','')=='yes': sudo='sudo '
00980 
00981     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['code'],
00982         'cm_action':'run',
00983         'run_host_os_uoa':host_os_uid,
00984         'run_target_os_uoa':target_os_uid,
00985         'run_script':ini['cfg']['cm_data']['run_script_name_set_cpu_freq'],
00986         'run_script_uoa':ini['cfg']['cm_data']['run_script_uoa'],
00987         'run_set_env2':{'CM_CPU_FREQUENCY':i.get('frequency',''), 
00988                         'CM_SUDO':sudo},
00989         'calming_delay':''}
00990     if target_os_cfg.get('cm_tool_prefix','')!='':
00991        ii['run_set_env2']['CM_TOOL_PREFIX']=target_os_cfg['cm_tool_prefix']
00992     r=cm_kernel.access(ii)
00993     if r['cm_return']>0: return r
00994 
00995     return {'cm_return':0}

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