File: change_source.py

package info (click to toggle)
abinit 9.10.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 518,712 kB
  • sloc: xml: 877,568; f90: 577,240; python: 80,760; perl: 7,019; ansic: 4,585; sh: 1,925; javascript: 601; fortran: 557; cpp: 454; objc: 323; makefile: 77; csh: 42; pascal: 31
file content (145 lines) | stat: -rwxr-xr-x 5,031 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# coding: utf-8
"""
This script replaces strings in the source files. See replace_string function.
"""
import sys
import os

TOPDIR = os.path.dirname(os.path.realpath(__file__))


def source_paths_from_abinit_src(top=None):
    """
    Return list with absolute paths of source files extracted from abinit.src
    """
    import imp
    source_paths = []
    black_list = ["m_build_info.F90", "m_optim_dumper.F90", os.path.basename(__file__)]
    if top is None: top = TOPDIR
    for root, dirs, files in os.walk(top):
        files = [f for f in files if f == "abinit.src"]
        if not files: continue
        assert len(files) == 1
        abinit_src = os.path.join(root, "abinit.src")
        mod = imp.load_source(abinit_src, abinit_src)
        if hasattr(mod, "sources"):
            source_paths.extend((os.path.join(root, s) for s in mod.sources if s not in black_list))

    return source_paths


def all_source_files(top=None, types=("fortran", "c", "h")):
    """
    Return list with the absolute paths of all the files in the project, exclude binary files
    or files that should not be modified.
    """
    if top is None: top = TOPDIR
    ext_list = []
    if "fortran" in types: ext_list += [".F90", ".f90", "finc"]
    if "c" in types: ext_list += [".c"]
    if "h" in types: ext_list += [ ".h"]
    def select_basename(f):
        if any(f.endswith(b) for b in ext_list): return True
        if f ==  os.path.basename(__file__): return False
        return False

    all_files = []
    for root, dirs, files in os.walk(top):
        all_files.extend(os.path.join(root, f) for f in files if select_basename(f))
    return all_files


def replace_string(s):
    """
    Main entry point for users.
    Change old2new dict to replace old_expression with new one
    using replace method of python string.
    """
    old2new = {
         # Malloc/free macros
         #"ABI_ALLOCATE(": "ABI_MALLOC(",
         #"ABI_DEALLOCATE(": "ABI_FREE(",
         #"ABI_DATATYPE_ALLOCATE(": "ABI_MALLOC(",
         #"ABI_DATATYPE_DEALLOCATE(": "ABI_FREE(",
         #"ABI_STAT_ALLOCATE(": "ABI_STAT_MALLOC(",
         #"ABI_DATATYPE_ALLOCATE_SCALAR(": "ABI_MALLOC_TYPE_SCALAR(",
         #"ABI_DATATYPE_DEALLOCATE_SCALAR(": "ABI_FREE(",
         # Logging macros
         #"MSG_COMMENT(": "ABI_COMMENT(",
         #"MSG_WARNING(": "ABI_WARNING(",
         #"MSG_COMMENT_UNIT(": "ABI_COMMENT_UNIT(",
         #"MSG_WARNING_UNIT(": "ABI_WARNING_UNIT(",
         #"MSG_ERROR(": "ABI_ERROR(",
         #"MSG_ERROR_CLASS(": "ABI_ERROR_CLASS(",
         #"MSG_BUG(": "ABI_BUG(",
         #"MSG_STOP(": "ABI_STOP(",
         #"MSG_ERROR_NODUMP(": "ABI_ERROR_NODUMP(",
         #"MSG_ERROR_NOSTOP(": "ABI_ERROR_NOSTOP(",
         #"MSG_WARNING_IF(": "ABI_WARNING_IF(",
         "use iso_c_binding": "use, intrinsic :: iso_c_binding",
         "USE ISO_C_BINDING": "use, intrinsic :: iso_c_binding",
         "USE iso_c_binding": "use, intrinsic :: iso_c_binding",
         "use ISO_C_BINDING": "use, intrinsic :: iso_c_binding",
    }

    #old2new = {
    #     "LIBPAW_COMMENT(": "ABI_COMMENT(",
    #     "LIBPAW_WARNING(": "ABI_WARNING(",
    #     "LIBPAW_COMMENT_UNIT(": "ABI_COMMENT_UNIT(",
    #     "LIBPAW_WARNING_UNIT(": "ABI_WARNING_UNIT(",
    #     "LIBPAW_ERROR(": "ABI_ERROR(",
    #     "LIBPAW_ERROR_CLASS(": "ABI_ERROR_CLASS(",
    #     "LIBPAW_BUG(": "ABI_BUG(",
    #     "LIBPAW_STOP(": "ABI_STOP(",
    #     "LIBPAW_ERROR_NODUMP(": "ABI_ERROR_NODUMP(",
    #     "LIBPAW_ERROR_NOSTOP(": "ABI_ERROR_NOSTOP(",
    #     "LIBPAW_WARNING_IF(": "ABI_WARNING_IF(",
    #}
    ## invert mapping
    #old2new = {v:k for k, v in old2new.items()}

    # This is just to print the table in github md format.
    #from tabulate import tabulate
    #print(str(tabulate(old2new.items(), headers=["OLD", "NEW"], tablefmt="github")))
    #raise RuntimeError("Just to print table.")

    for old, new in old2new.items():
        s = s.replace(old, new)

    return s


def main():

    if "-h" in sys.argv or "--help" in sys.argv:
        print(" Usage: change_source.py [DIR]")
        print(" to replace strings in all source files inside DIR")
        print(" If DIR not provided, uses current directory.")
        return 0

    #for path in all_files():
    #for path in source_paths_from_abinit_src():
    #for path in all_source_files(types=("fortran", "c", "h")):
    top_list = []
    if len(sys.argv) > 1:
        top_list.append(sys.argv[1])
        print("Replacing strings in files inside directory:", sys.argv[1])
    else:
        top_list = ["src", "shared"]

    for top in top_list:
        for path in all_source_files(top=top, types=("fortran", "h", "c")):
            print("Replacing strings in:", path)
            with open(path, "rt") as fh:
                old_s = fh.read()
                s = replace_string(old_s)
            if s != old_s:
                with open(path, "wt") as fh:
                    fh.write(s)

    return 0


if __name__ == "__main__":
    main()