1#!/usr/bin/env python3 2""" Loops through all the source using doctext to generate the manual pages""" 3 4import os 5import re 6import subprocess 7import pathlib 8 9def findlmansec(file): 10 mansec = None 11 submansec = None 12 with open(file) as mklines: 13 #print(file) 14 submansecl = [line for line in mklines if (line.find('SUBMANSEC') > -1 and line.find('BFORT') == -1)] 15 if submansecl: 16 submansec = re.sub(r'[ ]*/\* [ ]*SUBMANSEC[ ]*=[ ]*','',submansecl[0]).strip('\n').strip('*/').strip() 17 if submansec == submansecl[0].strip('\n'): 18 submansec = re.sub('SUBMANSEC[ ]*=[ ]*','',submansecl[0]).strip('\n').strip() 19 #print(':SUBMANSEC:'+submansec) 20 return submansec 21 with open(file) as mklines: 22 mansecl = [line for line in mklines if line.startswith('MANSEC')] 23 if mansecl: 24 mansec = re.sub('MANSEC[ ]*=[ ]*','',mansecl[0]).strip('\n').strip() 25 #print(':MANSEC:'+mansec) 26 return mansec 27 return None 28 29def processdir(petsc_dir, build_dir, dir, doctext): 30 '''Runs doctext on each source file in the directory''' 31 #print('Processing '+dir) 32 doctext_path = os.path.join(build_dir,'manualpages','doctext') 33 lmansec = None 34 if os.path.isfile(os.path.join(dir,'makefile')): 35 lmansec = findlmansec(os.path.join(dir,'makefile')) 36 37 numberErrors = 0 38 for file in os.listdir(dir): 39 llmansec = lmansec 40 if os.path.isfile(os.path.join(dir,file)) and pathlib.Path(file).suffix in ['.c', '.cxx', '.h', '.cu', '.cpp', '.hpp']: 41 #print('Processing '+file) 42 if not llmansec: 43 llmansec = findlmansec(os.path.join(dir,file)) 44 if not llmansec: continue 45 if not os.path.isdir(os.path.join(build_dir,'manualpages',llmansec)): os.mkdir(os.path.join(build_dir,'manualpages',llmansec)) 46 47 command = [doctext, 48 '-myst', 49 '-mpath', os.path.join(build_dir,'manualpages',llmansec), 50 '-heading', 'PETSc', 51 '-defn', os.path.join(build_dir,'manualpages','doctext','myst.def'), 52 '-indexdir', '../'+llmansec, 53 '-index', os.path.join(build_dir,'manualpages','manualpages.cit'), 54 '-locdir', dir[len(petsc_dir)+1:]+'/', 55 '-Wargdesc', os.path.join(build_dir,'manualpages','doctext','doctextcommon.txt'), 56 file] 57 sp = subprocess.run(command, cwd=dir, capture_output=True, encoding='UTF-8', check=True) 58 if sp.stdout and sp.stdout.find('WARNING') > -1: 59 print(sp.stdout) 60 numberErrors = numberErrors + 1 61 if sp.stderr and sp.stderr.find('WARNING') > -1: 62 print(sp.stderr) 63 numberErrors = numberErrors + 1 64 return numberErrors 65 66 67def processkhash(T, t, KeyType, ValType, text): 68 '''Replaces T, t, KeyType, and ValType in text (from include/petsc/private/hashset.txt) with a set of supported values''' 69 import re 70 return re.sub('<ValType>',ValType,re.sub('<KeyType>',KeyType,re.sub('<t>',t,re.sub('<T>',T,text)))) 71 72def main(petsc_dir, build_dir, doctext): 73 # generate source code for manual pages for PETSc khash functions 74 text = '' 75 for f in ['hashset.txt', 'hashmap.txt']: 76 with open(os.path.join(petsc_dir,'include','petsc','private',f)) as mklines: 77 text = mklines.read() 78 with open(os.path.join(petsc_dir,'include','petsc','private',f+'.h'),mode='w') as khash: 79 khash.write(processkhash('I','i','PetscInt','',text)) 80 khash.write(processkhash('IJ','ij','struct {PetscInt i, j;}','',text)) 81 khash.write(processkhash('I','i','PetscInt','PetscInt',text)) 82 khash.write(processkhash('IJ','ij','struct {PetscInt i, j;}','PetscInt',text)) 83 khash.write(processkhash('IJ','ij','struct {PetscInt i, j;}','PetscScalar',text)) 84 khash.write(processkhash('IV','iv','PetscInt','PetscScalar',text)) 85 khash.write(processkhash('Obj','obj','PetscInt64','PetscObject',text)) 86 87 # generate the .md files for the manual pages from all the PETSc source code 88 try: 89 os.unlink(os.path.join(build_dir,'manualpages','manualpages.cit')) 90 except: 91 pass 92 numberErrors = 0 93 for dirpath, dirnames, filenames in os.walk(os.path.join(petsc_dir),topdown=True): 94 dirnames[:] = [d for d in dirnames if d not in ['tests', 'tutorials', 'doc', 'output', 'ftn-custom', 'ftn-auto', 'ftn-mod', 'binding', 'binding', 'config', 'lib', '.git', 'share', 'systems'] and not d.startswith('arch')] 95 numberErrors = numberErrors + processdir(petsc_dir,build_dir,dirpath,doctext) 96 if numberErrors: 97 raise RuntimeError('Stopping document build since errors were detected in generating manual pages') 98 99 # generate list of all manual pages 100 with open(os.path.join(build_dir,'manualpages','htmlmap'),mode='w') as map: 101 with open(os.path.join(build_dir,'manualpages','manualpages.cit')) as cit: 102 map.write(re.sub(r'man\+../','man+manualpages/',cit.read())) 103 with open(os.path.join(build_dir,'manualpages','mpi.www.index')) as mpi: 104 map.write(mpi.read()) 105