def write_unit(self, fct_name, mode, part, s): def test_immutable(p): s = '\n'.join(read(p)) return s.find('//COMMENTED') != -1 nfp = Nt2_fct_props(self.tb_name, fct_name, mode) p = nfp.get_fct_unit_path(mode, part) if exist(os.path.split(p)[0]): if self.verbose: print('path = %s' % p) print("---%s" % exist(p)) if exist(p) and test_immutable(p): print("%s has been marked as immutable" % p) return if self.backup_on_write and exist(p): if self.verbose: print("backing up %s" % fct_name) i = 1 while True: pi = p + '.' + str(i) + '.bak' if not (exist(pi)): break i += 1 if self.verbose: print("to %s" % pi) shutil.copy(p, pi) elif self.verbose: print "writing to %s" % p write(p, s, self.check_on_write) p1 = os.path.join(os.path.split(p)[0], 'CMakeLists.txt') addline = Add_line(p1, fct_name) addline.update_file("SET\( *SOURCES") elif self.verbose: print("%s directory\n does not exist " % os.path.split(p)[0])
def __init__(self, fname, mode, name, d): self.debug = True self.name = name self.mode = mode self.base = d['base'] self.root = d['binarybase'] self.module = d['module'] self.fname = fname self.fnamedirect = d['fnamedirect'] self.modeloc = "simd/common" if self.mode == "simd" else self.mode if not exist(fname): pprint('Warning') pprint('File: ' + fname) pprint('does not exist') self.textorig = read(fname) if d['save']: pprint("saving to: " + fname + '_') write(fname + '_', self.textorig, False) if d['show_source']: pprint('Original file for ' + self.name) pprint('=orig===============================================') pprint(self.textorig) pprint('===============================================orig=') self.ids = {} self.includes = [] self.uncommented = self.suppress_comments_and_includes() self.anomaly = False self.anomaly_msg = '' self.modestr = "simd/" if (self.mode == "generic/" and d['add'] != "complex/") else self.mode self.modestr = "" if (d['add'] == "complex/") else self.modestr self.unspecif = self.unspecified_present() self.write = d["write"]
def modify_def(self, tryonly='tryonly') : """ text is always modified from old one""" style = self.get_tb_style() fct_name = self.get_fct_name() print("modifying def for %s with style %s"% (fct_name,style)) new = self.get_fct_def_path() old = new+'.old' if not exist(old) : self.duplicate_def() oldtxt = read(old) # PrettyPrinter().pprint(oldtxt) s = '\n'.join(oldtxt) s = re.sub('\t',' ',s) if style == 'sys' : s = re.sub('functors','tag',s) else : pattern = 'namespace %s' % self.get_tb_name() s = re.sub(pattern,pattern+' { namespace tag',s) pattern = '%s::'%self.get_tb_name() s = re.sub(pattern,pattern+'tag::',s) s = re.sub("(}\s*})","}\n } }",s) # print("===========================") newtxt = s.split('\n') if self.get_tb_style()=='usr' : newtxt = self.deplace("\s*NT2_FUNCTION_IMPLEMENTATION.*",newtxt) newtxt = self.deplace("\s*NT2_CRLIBM_FUNCTION_IMPLEMENTATION.*",newtxt) newtxt = self.replace('(?i)(:| )',fct_name,fct_name.lower(),newtxt) newtxt += ['// modified by %s the %s' % (self.get_author(), datetime.now().strftime("%d/%m/%Y"))] if tryonly != 'tryonly' : write(new,newtxt,False) else : print("---------------------------") PrettyPrinter().pprint(oldtxt) print("===========================") PrettyPrinter().pprint(newtxt) print("===========================")
def write_unit(self, fct_name, mode, part, s) : def test_immutable(p) : s= '\n'.join(read(p)) return s.find('//COMMENTED') != -1 nfp = Nt2_fct_props(self.tb_name,fct_name,mode) p = nfp.get_fct_unit_path(mode,part) if exist(os.path.split(p)[0]) : if self.verbose : print ('path = %s'%p) print("---%s"%exist(p)) if exist(p) and test_immutable(p) : print("%s has been marked as immutable"%p ) return if self.backup_on_write and exist(p) : if self.verbose : print("backing up %s" %fct_name) i = 1; while True : pi = p+'.'+str(i)+'.bak' if not(exist(pi)) : break i += 1 if self.verbose : print("to %s"% pi) shutil.copy(p,pi) elif self.verbose : print "writing to %s"%p write(p,s,self.check_on_write) p1 = os.path.join(os.path.split(p)[0],'CMakeLists.txt') addline=Add_line(p1,fct_name) addline.update_file("SET\( *SOURCES") elif self.verbose : print("%s directory\n does not exist " %os.path.split(p)[0])
def __init__(self, fname,mode,name,d) : self.debug= True self.name = name self.mode = mode self.base = d['base'] self.root = d['binarybase'] self.module = d['module'] self.fname = fname self.fnamedirect = d['fnamedirect'] self.modeloc = "simd/common" if self.mode =="simd" else self.mode if not exist(fname) : pprint('Warning') pprint('File: '+ fname) pprint('does not exist') self.textorig = read(fname) if d['save'] : pprint("saving to: "+fname+'_') write(fname+'_',self.textorig,False) if d['show_source'] : pprint('Original file for '+self.name) pprint('=orig===============================================') pprint(self.textorig) pprint('===============================================orig=') self.ids = {} self.includes = [] self.uncommented = self.suppress_comments_and_includes() self.anomaly= False self.anomaly_msg='' self.modestr = "simd/" if (self.mode == "generic/" and d['add'] != "complex/") else self.mode self.modestr = "" if (d['add'] == "complex/") else self.modestr self.unspecif = self.unspecified_present() self.write = d["write"];
def regress(self,path,txt) : """ file path does exist on entry is regressed on exit, old version is in path+'~' file """ print("regress %s"%path) shutil.move(path,path+'#') write(path,txt,False)
def write_file(self, p, txt, show=True, wr=False, check=True, backup=True): txt = txt.split('\n') just = "Just" if wr: just = "Also" if backup and exist(p): i = 1 while True: pi = p + '.' + str(i) + '.bak' if not (exist(pi)): break i += 1 print("backing to %s" % pi) shutil.copy(p, pi) print "writing to %s" % p write(p, txt, check) if p[-4:] == '.cpp': p1 = os.path.join(os.path.split(p)[0], 'CMakeLists.txt') addline = Add_line(p1, self.name) addline.update_file("SET\( *SOURCES") if show: print("%s showing text of %s for %s-test" % (just, os.path.split(p)[1], self.mode)) print("<" + "=" * 40) PrettyPrinter().pprint(txt) print("=" * 40 + ">")
def regress(self, path, txt): """ file path does exist on entry is regressed on exit, old version is in path+'~' file """ print("regress %s" % path) shutil.move(path, path + '#') write(path, txt, False)
def modify_unit(self,tryonly='tryonly') : """ text is always modified from old one""" style = self.get_tb_style() tb_name = self.get_tb_name() fct_name = self.get_fct_name() print("modifying fct for %s with style %s"% (self.get_fct_name(),style)) new = self.get_fct_unit_path('scalar') old = new+'.old' if not exist(new) : print("%s does not exist" % new) return if not exist(old) : self.duplicate_unit() oldtxt = read(old) newtxt = self.replacements(oldtxt,'\t',' ') pattern = ('nt2::functors' if style =='sys' else 'nt2::'+tb_name) rep = ('nt2::tag' if style =='sys' else pattern+'::tag') pattern = pattern+'::'+fct_name+'_' rep = rep + '::'+fct_name+'_' newtxt = self.replacements(newtxt,pattern,rep) ## newtxt = self.replacements(oldtxt,'\sNT2_CALL_RETURN_TYPE\(',' NT2_RETURN_TYPE(') if tryonly != 'tryonly' : write(new,newtxt,False) else : print("===============") PrettyPrinter().pprint(newtxt) print("===============")
def write_file(self,p,txt, show=True, wr=False, check=True, backup=True) : txt = txt.split('\n') just ="Just" if wr : just = "Also" if backup and exist(p) : i = 1; while True : pi = p+'.'+str(i)+'.bak' if not(exist(pi)) : break i += 1 print("backing to %s"% pi) shutil.copy(p,pi) print "writing to %s"%p write(p,txt,check) if p[-4:]=='.cpp' : p1 = os.path.join(os.path.split(p)[0],'CMakeLists.txt') addline=Add_line(p1,self.name) addline.update_file("SET\( *SOURCES") if show : print("%s showing text of %s for %s-test"% (just,os.path.split(p)[1],self.mode)) print("<"+"="*40) PrettyPrinter().pprint(txt) print("="*40+">")
def modify_fct(self,tryonly='tryonly') : """ text is always modified from old one""" style = self.get_tb_style() fct_name = self.get_fct_name() print("modifying fct for %s with style %s"% (fct_name,style)) new = self.get_fct_impl_path('scalar') old = new+'.old' if self.manual(new) : print("file \n %s\nwas manually modified: skipping" % new) return if not exist(old) : self.duplicate_fct() oldtxt = read(old) (beg,end)=Nt2_fct_internals.get_header_indices(oldtxt) newtxt=oldtxt[beg:end] newtxt += Nt2_fct_internals.get_extern_C(oldtxt) calls = Nt2_fct_internals.get_calls(oldtxt) for c in calls.keys() : newtxt += [l.expandtabs(6) for l in self.mk_nw_call(c,calls[c])] newtxt += ["","#endif"] newtxt += ['// modified by %s the %s' % (self.get_author(), datetime.now().strftime("%d/%m/%Y"))] newtxt = self.replacements(newtxt,'\sNT2_CALL_RETURN_TYPE\(',' NT2_RETURN_TYPE(') newtxt = self.replacements(newtxt,'long double_','long_double_') newtxt = self.replace('(?i)gsl_specfun::',fct_name,fct_name.lower(),newtxt) if tryonly != 'tryonly' : write(new,newtxt,False) else : print("---------------") PrettyPrinter().pprint(oldtxt) print("===============") PrettyPrinter().pprint(newtxt) print("===============")
def wr_txt(self,dico,key,p) : txt = d[key]["text"] for t in d[key]["token"] : txt = re.sub(dollarize(t),self.value(t,key),txt) txt = re.sub("'",'"',txt) print(p) print(txt) write(p,txt,True)
def wr_txt(self, dico, key, p): txt = d[key]["text"] for t in d[key]["token"]: txt = re.sub(dollarize(t), self.value(t, key), txt) txt = re.sub("'", '"', txt) print(p) print(txt) write(p, txt, True)
def save_new_fctor(self) : print "making %s" % self.fcti.get_fct_path() new = self.compose_new_fctor_txt() if type(new) is str : print "%s %s" % (self.fcti.get_fct_name(),new) else : shutil.move(self.fcti.get_fct_path(),re.sub('.hpp','.bak',self.fcti.get_fct_path())) write(self.fcti.get_fct_path(),new,True) print "done"
def write_pydoc(self,other_name=None,force=False,backup=True) : name = self.get_fct_name()+'.py' if other_name==None else other_name p = os.path.join(self.get_doc_path(),name) if backup and exist(p) : shutil.copy(p,p+'~') if exist(p) and not force : s = '\n'.join(read(p)) if re.search("manually",s) : print("%s \n was manually modified, skipping"%p) return write(p,str(self),check=False,verbose=True)
def write_new_doc(tb_name,fct_name) : if mv_old_doc(tb_name,fct_name) : nfp = Nt2_fct_props(tb_name,fct_name) od =nfp.get_fct_dict_list() p = nfp.get_fct_doc_path() nd = construct_new_dict_0_1(od) pp = PrettyPrinter() s=pp.pformat(nd) print s write(p,s,False) else : print "doc is already updatted"
def update_file(self,token,txt) : txt_orig = read(self.path_to) done, new_txt = self.insert_after(token,txt_orig,txt) if not done : print("Warning : line\n %s\nis already in %s file"% (txt,self.path_to)) else : if exist(self.path_to) : if self.verbose : print("file\n %s\nwill be updated"%self.path_to) write(self.path_to,new_txt,False) if self.verbose : print("file\n %s\nis now updated"%self.path_to) else : if self.verbose : print("file\n %s\n does not exist"%self.path_to)
def write_header2(self,path,check=True,flag=None): if (flag is None) or (flag == 'full') : l = self.get_banner()+self.get_guard_begin()+self.__inner+self.get_guard_end() elif flag == 'inner' : l = self.__inner elif flag == 'banner+inner' : l = self.get_banner()+self.__inner else : print "unexpected flag : %s " %flag raise SystemExit if len(self.__fill)==1 : l=sub_if_match_list(" @"+self.__comment, "@", self.__fill, l) write(path,l,check)
def update_file(self,token) : txt_orig = read(self.path_to) txt = "%s.cpp"%self.name done, new_txt = self.insert_after(token,txt_orig,txt) if not done : print("Warning : line\n %s\nis already in CMakelists.txt file"%txt ) else : if exist(self.path_to) : if self.verbose : print("file\n %s\nwill be updated"%self.path_to) write(self.path_to,new_txt,False) if self.verbose : print("file\n %s\nis now updated"%self.path_to) else : if self.verbose : print("file\n %s\n does not exist"%self.path_to)
def create_fct_pydoc_file(self, force=True,error='warn') : p = self.get_fct_doc_path() if not exist(p) or force: s = self.mk_doc_from_existing_fct() PrettyPrinter().pprint(s) print(p) write(p,s,check=not force) print("creating properties dictionnary file from template") print("please edit the template before using it") print("to create the functors files") if error == 'exit' : raise SystemExit else : print("file exist")
def write_bench(tb_name, fct_name, mode, s, check=False, backup=True): nfp = Nt2_fct_props(tb_name, fct_name, mode) print("mode %s" % mode) p = nfp.get_fct_bench_path(mode) ## print ('p = %s'%p) if backup and exist(p): ## print("p=%s" %p) i = 1 while True: pi = p + '.' + str(i) + '.bak' if not (exist(pi)): break i += 1 print("backing to %s" % pi) shutil.copy(p, pi) print "writing to %s" % p write(p, s, check)
def write_bench(tb_name,fct_name,mode,s,check=False,backup=True) : nfp = Nt2_fct_props(tb_name,fct_name,mode) print("mode %s"%mode) p = nfp.get_fct_bench_path(mode) print ('p = %s'%p) if backup and exist(p) : ## print("p=%s" %p) i = 1; while True : pi = p+'.'+str(i)+'.bak' if not(exist(pi)) : break i += 1 print("backing to %s"% pi) shutil.copy(p,pi) print "writing to %s"%p write(p,s,check)
def write(self,p,name,txt) : if self.show : show(txt) self.p =p self.name = name self.txt = txt if isinstance(p,str) and exist(self.p) : ## if self.verbose : print ('path = %s'%self.p) tp = os.path.join(self.p,self.name) if exist(tp) : if not self.write_files : return False if self.backup_on_write and exist(tp) : if self.verbose : print("backing up %s" %name) i = 1; while True : tpi = tp+'.'+str(i)+'.bak' if not(exist(tpi)) : break i += 1 if self.verbose : print("to %s"% tpi) shutil.copy(tp,tpi) elif not self.write_files : print "%s is not written"%tp return False if self.verbose : print "writing to %s"%tp if write(tp,self.txt,self.check_on_write) : print "%s is written"%tp return True; elif self.verbose and isinstance(p,str) : print("%s directory\n does not exist " %os.path.split(p)[0]) else : print("no path found") return False
def write_exhaustive(tb_name,fct_name,mode,s,check=False,backup=True) : nfp = Nt2_fct_props(tb_name,fct_name,mode) print("mode %s"%mode) p = nfp.get_fct_exhaustive_path(mode) print ('p = %s'%p) if backup and exist(p) : ## print("p=%s" %p) i = 1; while True : pi = p+'.'+str(i)+'.bak' if not(exist(pi)) : break i += 1 print("backing to %s"% pi) shutil.copy(p,pi) print "writing to %s"%p write(p,s,check) p1 = os.path.join(os.path.split(p)[0],'CMakeLists.txt') addline=Add_line(p1,fct_name) addline.update_file("SET\( *SOURCES")
def treat_files(self): if not self.file_list: pprint("no file match the choice") for fname in self.file_list: name = os.path.basename(fname)[:-4] fiif = Fix_includes_in_file(fname, self.mode, name, self.d) newtext, anomaly, anomaly_msg = fiif.treat_text() if self.show: pprint('Transformed file for ' + name) pprint('=trans===============================================') pprint(newtext) pprint('===============================================trans=') elif self.write: if anomaly: print(anomaly_msg) print(name + " was not modified") pprint(fiif.textorig) else: pprint(fname) write(fname, newtext, False, True)
def write_header(self,path=None,check=True,flag=None): # print "self.total %s"%self.get_total_path() # print "path %s"%path path2headerfile = os.path.join(self.get_nt2_path() if path is None else path,self.get_total_path()) # print "path2headerfile %s " % path2headerfile if (flag is None) or (flag == 'full') : l = self.get_banner()+self.get_guard_begin()+self.__inner+self.get_guard_end() elif flag == 'inner' : l = self.__inner elif flag == 'banner+inner' : l = self.get_banner()+self.__inner else : print "unexpected *** flag : %s " %flag raise SystemExit if len(self.__fill)==1 : l=sub_if_match_list(" @"+self.__comment, "@", self.__fill, l) print( "header written to:\n %s\n" % path2headerfile) write(path2headerfile,l,check)
def treat_files(self) : if not self.file_list : pprint("no file match the choice") for fname in self.file_list : name = os.path.basename(fname)[:-4] fiif = Fix_includes_in_file(fname,self.mode,name,self.d) newtext,anomaly,anomaly_msg =fiif.treat_text() if self.show : pprint('Transformed file for '+name) pprint('=trans===============================================') pprint(newtext) pprint('===============================================trans=') elif self.write: if anomaly : print(anomaly_msg) print(name + " was not modified") pprint(fiif.textorig) else : pprint(fname) write(fname,newtext,False,True)
def write_unit(self, fct_name, mode, part, s) : nfp = Nt2_fct_props(self.tb_name,fct_name,mode) p = nfp.get_fct_unit_path(mode,part) if self.verbose : print ('path = %s'%p) if self.backup_on_write and exist(p) : if self.verbose : print("backing up %s" %fct_name) i = 1; while True : pi = p+'.'+str(i)+'.bak' if not(exist(pi)) : break i += 1 if self.verbose : print("to %s"% pi) shutil.copy(p,pi) elif self.verbose : print "writing to %s"%p write(p,s,self.check_on_write) p1 = os.path.join(os.path.split(p)[0],'CMakeLists.txt') addline=Add_line(p1,fct_name) addline.update_file("SET\( SOURCES")
def suppresslinein(self,p,token) : txt = read(p); txt = [re.sub('\s*'+token,'',t) for t in txt ] write(p,txt,check=False)
def suppresslinein(self, p, token): txt = read(p) txt = [re.sub('\s*' + token, '', t) for t in txt] write(p, txt, check=False)
def get_types_list(self) : return "['real_']" def get_property_call(self,arity) : call_param = ','.join([ "a%d" % i for i in xrange(0, arity) ]) name = self.get_fct_name() suf = "" if ( name[-3:] in ['_rd','_rn','_ru','_rz']) else "<rn>" call = "nt2::crlibm::%s%s(%s)"% (self.get_fct_name(),suf,call_param) return "{'default' : ['%s'],}" % call def get_property_value(self,arity) : name = self.get_fct_name() n = name[:-3] if ( name[-3:] in ['_rd','_rn','_ru','_rz']) else name call_param = ','.join([ "a%d" % i for i in xrange(0, arity) ]) value = "nt2::%s(%s)"% (n,call_param) return "{'default' : ['%s'],}" % value if __name__ == "__main__" : ntp = Nt2_tb_props("crlibm") fcts = ntp.get_fcts_list() ## fcts = ["acos"] for fct in fcts : print(fct) mds = Mk_doc_skeleton_for_crlibm(fct) ##// print(mds) res =mds.construct_new_dict_txt() PrettyPrinter().pprint(res) p = os.path.join(mds.get_doc_path(),mds.get_fct_name()+'.py') write(p,res,check=False,verbose=True) ## mds.write_pydoc()
def write_unit(tb_name, fct_name, mode, s, check=False): nfp = Nt2_fct_props(tb_name, fct_name, mode) p = nfp.get_fct_unit_path(mode) print "write to %s" % p show(s) write(p, s, check)
def update(self, path, txt): """ file path does exist on entry is upated on exit, old version is in path+'~' file """ shutil.move(path, path + '~') write(path, txt, False)
def write_unit(tb_name,fct_name,mode,s,check=False) : nfp = Nt2_fct_props(tb_name,fct_name,mode) p = nfp.get_fct_unit_path(mode) print "write to %s"%p write(p,s,check=check,verbose=True)
def create(self,path,txt) : """ file path does not exist on entry is created on exit """ write(path,txt,False)
def create(self, path, txt): """ file path does not exist on entry is created on exit """ write(path, txt, False)
def write_unit(tb_name,fct_name,mode,s,check=False) : nfp = Nt2_fct_props(tb_name,fct_name,mode) p = nfp.get_fct_unit_path(mode) print "write to %s"%p show(s) write(p,s,check)
def update(self,path,txt) : """ file path does exist on entry is upated on exit, old version is in path+'~' file """ shutil.move(path,path+'~') write(path,txt,False)
def change_units(self) : p = os.path.join(tbi.get_unit_path(),"scalar",fct+'.cpp') # shutil.move(p,re.sub('.cpp','.bak',p)) s = read(p) # show(s[:-2]) write(p,s[:-2],False)
def __write_def_txt(self,s) : p = os.path.join(tbi.get_def_path(),fct+'.hpp') shutil.move(p,re.sub('.hpp','.bak',p)) write(p,s,False)
def write_unit_txt(self,types=None) : p = self.get_fct_unit_path("scalar") if exist(p) : bak = re.sub('.cpp','.cpp.bak',p) shutil.copy(p,bak) write(p,self.create_unit_txt(),False)