File: libadd_mca_comp_update.py

package info (click to toggle)
prrte 3.0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 9,128 kB
  • sloc: ansic: 80,431; sh: 4,289; perl: 3,195; makefile: 1,829; lex: 352; python: 239
file content (228 lines) | stat: -rw-r--r-- 7,668 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python

# Copyright (c) 2017      IBM Corporation. All rights reserved.
# Copyright (c) 2017-2020 Intel, Inc.  All rights reserved.
# Copyright (c) 2020      Cisco Systems, Inc.  All rights reserved
# $COPYRIGHT$
#

import glob, os, re, shutil

projects= { 'prte' : ["$(top_builddir)/src/libprrte.la"],
          }

no_anchor_file = []
missing_files = []
skipped_files = []
partly_files = []
updated_files = []

#
# Check of all of the libadd fields are accounted for in the LIBADD
# Return a list indicating which are missing (positional)
#
def check_libadd(content, libadd_field, project):
    global projects

    libadd_list = projects[project]
    libadd_missing = [True] * len(libadd_list)

    on_libadd = False
    for line in content:
        # First libadd line
        if re.search( r"^\s*"+libadd_field, line):
            # If line continuation, then keep searching after this point
            if line[-2] == '\\':
                on_libadd = True

            for idx, lib in enumerate(libadd_list):
                if True == libadd_missing[idx]:
                    if 0 <= line.find(lib):
                        libadd_missing[idx] = False

        # Line continuation
        elif True == on_libadd:
            for idx, lib in enumerate(libadd_list):
                if True == libadd_missing[idx]:
                    if 0 <= line.find(lib):
                        libadd_missing[idx] = False

            # No more line continuations, so stop processing
            if line[-2] != '\\':
                on_libadd = False
                break

    return libadd_missing

#
# Update all of the Makefile.am's with the proper LIBADD additions
#
def update_makefile_ams():
    global projects
    global no_anchor_file
    global missing_files
    global skipped_files
    global partly_files
    global updated_files

    for project, libadd_list in projects.items():
        libadd_str = " \\\n\t".join(libadd_list)

        print("="*40)
        print("Project: "+project)
        print("LIBADD:\n"+libadd_str)
        print("="*40)

        #
        # Walk the directory structure
        #
        for root, dirs, files in os.walk(project+"/mca"):
            parts = root.split("/")
            if len(parts) != 4:
                continue
            if parts[-1] == ".libs" or parts[-1] == ".deps" or parts[-1] == "base":
                continue
            if parts[2] == "common":
                continue

            print("Processing: "+root)

            #
            # Find Makefile.am
            #
            make_filename = os.path.join(root, "Makefile.am")
            if False == os.path.isfile( make_filename ):
                missing_files.append("Missing: "+make_filename)
                print("  ---> Error: "+make_filename+" is not present in this directory")
                continue

            #
            # Stearching for: mca_FRAMEWORK_COMPONENT_la_{LIBADD|LDFLAGS}
            # First scan file to see if it has an LIBADD / LDFLAGS
            #
            libadd_field  = "mca_"+parts[2]+"_"+parts[3]+"_la_LIBADD"
            ldflags_field = "mca_"+parts[2]+"_"+parts[3]+"_la_LDFLAGS"
            has_ldflags = False
            has_libadd  = False

            r_fd = open(make_filename, 'r')
            orig_content = r_fd.readlines()
            r_fd.close()
            libadd_missing = []

            for line in orig_content:
                if re.search( r"^\s*"+ldflags_field, line):
                    has_ldflags = True
                elif re.search( r"^\s*"+libadd_field, line):
                    has_libadd = True

            if True == has_libadd:
                libadd_missing = check_libadd(orig_content, libadd_field, project)

            #
            # Sanity Check: Was there an anchor field.
            # If not skip, we might need to manually update or it might be a
            # static component.
            #
            if False == has_ldflags and False == has_libadd:
                no_anchor_file.append("No anchor ("+ldflags_field+"): "+make_filename)
                print("  ---> Error: Makefile.am does not contain necessary anchor")
                continue

            #
            # Sanity Check: This file does not need to be updated.
            #
            if True == has_libadd and all(False == v for v in libadd_missing):
                skipped_files.append("Skip: "+make_filename)
                print("       Skip: Already updated Makefile.am")
                continue

            #
            # Now go though and create a new version of the Makefile.am
            #
            r_fd = open(make_filename, 'r')
            w_fd = open(make_filename+".mod", 'w')

            num_libadds=0
            for line in r_fd:
                # LDFLAGS anchor
                if re.search( r"^\s*"+ldflags_field, line):
                    w_fd.write(line)
                    # If there is no LIBADD, then put it after the LDFLAGS
                    if False == has_libadd:
                        w_fd.write(libadd_field+" = "+libadd_str+"\n")
                # Existing LIBADD field to extend
                elif 0 == num_libadds and re.search( r"^\s*"+libadd_field, line):
                    parts = line.partition("=")
                    num_libadds += 1

                    if parts[0][-1] == '+':
                        w_fd.write(libadd_field+" += ")
                    else:
                        w_fd.write(libadd_field+" = ")

                    # If all libs are missing, then add the full string
                    # Otherwise only add the missing items
                    if all(True == v for v in libadd_missing):
                        w_fd.write(libadd_str)
                        # Only add a continuation if there is something to continue
                        if 0 != len(parts[2].strip()):
                            w_fd.write(" \\")
                        w_fd.write("\n")
                    else:
                        partly_files.append("Partly updated: "+make_filename)
                        for idx, lib in enumerate(libadd_list):
                            if True == libadd_missing[idx]:
                                w_fd.write(lib+" \\\n")

                    # Original content (unless it's just a line continuation)
                    if 0 != len(parts[2].strip()) and parts[2].strip() != "\\":
                        w_fd.write("\t"+parts[2].lstrip())

                # Non matching line, just echo
                else:
                    w_fd.write(line)

            r_fd.close()
            w_fd.close()

            #
            # Replace the original with the updated version
            #
            shutil.move(make_filename+".mod", make_filename)
            updated_files.append(make_filename)


if __name__ == "__main__":

    update_makefile_ams()

    print("")

    print("="*40);
    print("{:>3} : Files skipped".format(len(skipped_files)))
    print("="*40);

    print("="*40);
    print("{:>3} : Files updated, but had some libs already in place.".format(len(partly_files)))
    print("="*40);
    for fn in partly_files:
        print(fn)

    print("="*40);
    print("{:>3} : Files fully updated".format(len(updated_files)))
    print("="*40);
    for fn in updated_files:
        print(fn)

    print("="*40);
    print("{:>3} : Missing Makefile.am".format(len(missing_files)))
    print("="*40);
    for err in missing_files:
        print(err)

    print("="*40);
    print("{:>3} : Missing Anchor for parsing (might be static-only components)".format(len(no_anchor_file)))
    print("="*40);
    for err in no_anchor_file:
        print(err)