1*e1124065SBarry Smith#!/usr/bin/env python 2*e1124065SBarry Smith""" Adds links in the manual pages to tutorials that utilize the functions""" 3*e1124065SBarry Smith 4*e1124065SBarry Smithimport os 5*e1124065SBarry Smithimport errno 6*e1124065SBarry Smithimport subprocess 7*e1124065SBarry Smithimport shutil 8*e1124065SBarry Smithimport argparse 9*e1124065SBarry Smith 10*e1124065SBarry Smith 11*e1124065SBarry Smith'''manexamples: 12*e1124065SBarry Smith -@base=$$(basename $$(pwd)); \ 13*e1124065SBarry Smith if [ "$${base}" = "tutorials" ] ; then \ 14*e1124065SBarry Smith petsc_dir=$$(realpath ${PETSC_DIR}); LOCDIR=$$(pwd | sed s"?$${petsc_dir}/??"g)/; \ 15*e1124065SBarry Smith for i in ${EXAMPLESALL} foo ; do \ 16*e1124065SBarry Smith if [ "$$i" != "foo" ] ; then \ 17*e1124065SBarry Smith a=`cat $$i | ${MAPNAMES} -map ${LOC}/manualpages/manualpages.cit \ 18*e1124065SBarry Smith -printmatch-link -o /dev/null| cut -f 2 | cut -d '#' -f 1 |sed -e s~^../~~ | grep \\.md$$ | sort | uniq` ; \ 19*e1124065SBarry Smith for j in $$a ; do \ 20*e1124065SBarry Smith b=`ls ${LOC}/manualpages/$${j} | grep -v /all/ | cut -f9` ; \ 21*e1124065SBarry Smith l=`grep "^<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER.*/tutorials/" $${b} | wc -l`; \ 22*e1124065SBarry Smith if [ $$l -le 10 ] ; then \ 23*e1124065SBarry Smith if [ $$l -eq 0 ] ; then \ 24*e1124065SBarry Smith printf "\n## Examples\n" >> $$b; \ 25*e1124065SBarry Smith fi; \ 26*e1124065SBarry Smith echo "<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER/XX\">BB</A><BR>" | sed s?XX?$${LOCDIR}$$i.html?g | sed s?BB?$${LOCDIR}$$i?g >> $$b; \ 27*e1124065SBarry Smith fi; \ 28*e1124065SBarry Smith done; \ 29*e1124065SBarry Smith fi; \ 30*e1124065SBarry Smith done; \ 31*e1124065SBarry Smith fi 32*e1124065SBarry Smith''' 33*e1124065SBarry Smith 34*e1124065SBarry Smith''' 35*e1124065SBarry Smithr = re.compile(".*cat") 36*e1124065SBarry Smithnewlist = list(filter(r.match, mylist)) # Read Note below 37*e1124065SBarry Smith''' 38*e1124065SBarry Smith 39*e1124065SBarry Smith 40*e1124065SBarry Smithdef processfile(petsc_dir,dir,file,keyre,mdict,counts): 41*e1124065SBarry Smith '''Find all functions used in the tutorial and add links to the manual page for the function''' 42*e1124065SBarry Smith #print('Processing '+os.path.join(dir,file)) 43*e1124065SBarry Smith with open(os.path.join(dir,file),'r') as fd: 44*e1124065SBarry Smith text = fd.read() 45*e1124065SBarry Smith found = list(set(keyre.findall(text))) 46*e1124065SBarry Smith for i in found: counts[i] = counts[i]+1 47*e1124065SBarry Smith 48*e1124065SBarry Smithdef processdir(petsc_dir,dir,keyre,mdict,counts): 49*e1124065SBarry Smith '''Loop over tutorials, call processfile() on each''' 50*e1124065SBarry Smith print('Processing '+dir) 51*e1124065SBarry Smith for file in os.listdir(dir): 52*e1124065SBarry Smith if os.path.isfile(os.path.join(dir,file)) and (file.endswith('.c') or file.endswith('.cxx')): processfile(petsc_dir,dir,file,keyre,mdict,counts) 53*e1124065SBarry Smith 54*e1124065SBarry Smithdef loadmanualpagescit(loc): 55*e1124065SBarry Smith '''Loads and parses the manualpages.cit file generated by Sowing doctext''' 56*e1124065SBarry Smith import re 57*e1124065SBarry Smith mdict = {} 58*e1124065SBarry Smith PATTERN = re.compile(r'man:\+(.*)\+\+(.*)\+\+\+\+man\+(.*)#.*') 59*e1124065SBarry Smith EXCLUDE_PATTERN = re.compile('PetscCall|Petsc[A-Z]*Int|PetscReal|PetscScalar|PetscBool|PetscComplex|PetscErrorCode|SETERR|PetscLog|PETSC_FALSE|PETSC_TRUE') 60*e1124065SBarry Smith with open(os.path.join(loc,'manualpages','manualpages.cit'),'r') as fd: 61*e1124065SBarry Smith text = fd.read() 62*e1124065SBarry Smith for line in text.split(): 63*e1124065SBarry Smith m = re.match(PATTERN, line) 64*e1124065SBarry Smith # print('Manual page '+m.group(1)+' location '+m.group(3)) 65*e1124065SBarry Smith if re.match(EXCLUDE_PATTERN,m.group(1)): continue 66*e1124065SBarry Smith mdict[m.group(1)] = m.group(3) 67*e1124065SBarry Smith # sort to find enclosing names first 68*e1124065SBarry Smith mdict = dict(sorted(mdict.items(), key=lambda item: len(item[0]), reverse = True)) 69*e1124065SBarry Smith keyre = re.compile('|'.join(list(mdict.keys()))) 70*e1124065SBarry Smith return keyre,mdict 71*e1124065SBarry Smith 72*e1124065SBarry Smithdef main(petsc_dir,loc): 73*e1124065SBarry Smith keyre,mdict = loadmanualpagescit(loc) 74*e1124065SBarry Smith counts = dict(zip(mdict.keys(), [0]*len(mdict.keys()))) 75*e1124065SBarry Smith for dirpath, dirnames, filenames in os.walk(os.path.join(petsc_dir,'src'),topdown=True): 76*e1124065SBarry Smith dirnames[:] = [d for d in dirnames if d not in ['output', 'ftn-custom', 'f90-custom', 'ftn-auto', 'f90-mod', 'tests', 'binding']] 77*e1124065SBarry Smith if dirpath.endswith('tutorials'): 78*e1124065SBarry Smith processdir(petsc_dir,dirpath,keyre,mdict,counts) 79*e1124065SBarry Smith print(counts['VecCreate']) 80*e1124065SBarry Smith 81*e1124065SBarry Smithif __name__ == "__main__": 82*e1124065SBarry Smith main(os.path.abspath(os.environ['PETSC_DIR']),os.path.abspath(os.environ['LOC'])) 83