1#!/usr/bin/env python 2""" Adds links in the manual pages to implementations of the function""" 3 4import os 5import re 6 7def processfile(petsc_dir,dir,file,implsClassAll,implsFuncAll): 8 #print('Processing '+os.path.join(dir,file)) 9 itemName = file[0:-3] 10 func = list(filter(lambda x: x.find(itemName+'_') > -1, implsFuncAll)) 11 iclass = list(filter(lambda x: x.find('_p_'+itemName) > -1, implsClassAll)) 12 if func or iclass: 13 with open(os.path.join(dir,file),'a') as f: 14 f.write('\n## Implementations\n') 15 if func: 16 for str in func: 17 f.write(re.sub('(.*\.[ch]x*u*).*('+itemName+'.*)(\(.*\))','<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER/\\1.html#\\2\">\\2() in \\1</A><BR>',str,count=1)+'\n') 18 if iclass: 19 for str in iclass: 20 f.write(re.sub('(.*\.[ch]x*u*):.*struct.*(_p_'+itemName+').*{','<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER/\\1.html#\\2\">\\2 in \\1</A><BR>',str,count=1)+'\n') 21 22def loadstructfunctions(petsc_dir): 23 '''Creates the list of structs and class functions''' 24 import subprocess 25 implsClassAll = subprocess.check_output(['git', 'grep', 'struct\s\+_[pn]_[^\s]\+.*{', '--', '*.c', '*.cpp', '*.cu', '*.c', '*.h', '*.cxx'], cwd = petsc_dir).strip().decode('utf-8') 26 implsClassAll = list(filter(lambda x: not (x.find('/tests/') > -1 or x.find('/tutorials') > -1 or x.find(';') > -1), implsClassAll.split('\n'))) 27 28 implsFuncAll = subprocess.check_output(['git', 'grep', '-n', '^\(static \)\?\(PETSC_EXTERN \)\?\(PETSC_INTERN \)\?\(extern \)\?PetscErrorCode \+[^_ ]\+_[^_ ]\+(', '--', '*/impls/*.c', '*/impls/*.cpp', '*/impls/*.cu', '*/impls/*.c', '*/impls/*.h', '*/impls/*.cxx'], cwd = petsc_dir).strip().decode('utf-8') 29 implsFuncAll = list(filter(lambda x: not (x.find('_Private') > -1 or x.find('_private') > -1 or x.find(';') > -1), implsFuncAll.split('\n'))) 30 return (implsClassAll,implsFuncAll) 31 32def main(petsc_dir): 33 (implsClassAll,implsFuncAll) = loadstructfunctions(petsc_dir) 34 for dirpath, dirnames, filenames in os.walk(os.path.join(petsc_dir,'doc','manualpages'),topdown=True): 35 #print('Processing directory '+dirpath) 36 for file in filenames: 37 if file.endswith('.md'): processfile(petsc_dir,dirpath,file,implsClassAll,implsFuncAll) 38 39if __name__ == "__main__": 40 main(os.path.abspath(os.environ['PETSC_DIR'])) 41