File: discover_programs.py

package info (click to toggle)
cp2k 2025.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 372,052 kB
  • sloc: fortran: 963,262; ansic: 64,495; f90: 21,676; python: 14,419; sh: 11,382; xml: 2,173; makefile: 953; pascal: 845; perl: 492; cpp: 345; lisp: 297; csh: 16
file content (64 lines) | stat: -rwxr-xr-x 1,636 bytes parent folder | download | duplicates (3)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3

import os
import re
import sys
from os import path

# pre-compiled regular expressions
re_program = re.compile(r"\n\s*end\s*program")
re_main = re.compile(r"\sint\s+main\s*\(")


# ============================================================================
def main():
    if len(sys.argv) != 2:
        print("Usage: discover_programs.py <src-dir>")
        sys.exit(1)

    srcdir = sys.argv[1]

    sys.stderr.write("Discovering programs ...\n")

    programs = []
    for root, _, files in os.walk(srcdir):
        if root.endswith("/preprettify"):
            continue
        if "/.git" in root:
            continue

        for fn in files:
            abs_fn = path.join(root, fn)
            if fn[-2:] == ".F":
                if is_fortran_program(abs_fn):
                    programs.append(abs_fn)

            elif fn[-2:] == ".c" or fn[-3:] == ".cu":
                if has_main_function(abs_fn):
                    programs.append(abs_fn)

    print(" ".join(programs))


# ============================================================================
def is_fortran_program(fn):
    f = open(fn, encoding="utf8")
    s = path.getsize(fn)
    f.seek(max(0, s - 100))
    tail = f.read()
    f.close()
    m = re_program.search(tail.lower())
    return m is not None


# ============================================================================
def has_main_function(fn):
    f = open(fn, encoding="utf8")
    content = f.read()
    f.close()
    m = re_main.search(content)
    return m is not None


# ===============================================================================
main()