File: info_hints.py

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (51 lines) | stat: -rw-r--r-- 2,086 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
##
## Copyright (C) by Argonne National Laboratory
##     See COPYRIGHT in top-level directory
##

from local_python import RE
import subprocess

def collect_info_hint_blocks(root_dir):
    """Collect INFO_HINT_BLOCKS from source files and return a function-name-keyed dictionary"""
    infos_by_funcname = {}

    # xargs may process files in batches and return a non-zero exit code if grep comes up empty
    # use "|| true" to avoid terminating the script in those instances
    # https://stackoverflow.com/questions/26540813/got-exit-code-123-in-find-xargs-grep
    files = subprocess.check_output(
        "find %s -name '*.[ch]' |xargs grep -l BEGIN_INFO_HINT_BLOCK || true" % root_dir,
        shell=True).splitlines()
    for f in files:
        infos = parse_info_block(f)
        for info in infos:
            for funcname in info['functions'].replace(' ', '').split(','):
                if funcname not in infos_by_funcname:
                    infos_by_funcname[funcname] = []
                infos_by_funcname[funcname].append(info)
    return infos_by_funcname

def parse_info_block(f):
    """Parse a source file with INFO_HINT_BLOCKs, and return a list of info hints"""
    hints = []
    info = None # loop variable
    stage = 0
    with open(f) as In:
        for line in In:
            if line.startswith("=== BEGIN_INFO_HINT_BLOCK ==="):
                stage = 1
            elif line.startswith("=== END_INFO_HINT_BLOCK ==="):
                stage = 0
            elif stage:
                if RE.match(r'\s*-\s*name\s*:\s*(\w+)', line):
                    info = {"name":RE.m.group(1)}
                    hints.append(info)
                elif RE.match(r'\s*(functions|type|default)\s*:\s*(.*)', line):
                    info[RE.m.group(1)] = RE.m.group(2)
                elif RE.match(r'\s*description\s*:\s*>-', line):
                    info['description'] = ""
                elif RE.match(r'\s+(\S.+)', line):
                    info['description'] += RE.m.group(1) + ' '
                else:
                    info = None
    return hints