summaryrefslogtreecommitdiffstats
path: root/languages/python/app_templates/pyqt/pyqt-am-preproc.py
blob: b58f3328916656b660850ce79463318f6370a624 (plain)
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
#!/usr/bin/env python

# Preprocess Makefile.am for PyQT project
# Julian Rockey 2002

# TODO:
#   Test real_lines properly
#   Process Makefile.am's in sub-directories


import sys
import re

# function to convert list of lists with continuation chars
# (bashslashes) to list of actual lines
def real_lines(m):
    while len(m)>0:
        result = ""
        if m[0][-1]=='\\':
            while (len(m)>0 and m[0][-1]=='\\'):
                result += m[0][:-1]
                m=m[1:]
        if len(m)>0:
            result += m[0]
            m=m[1:]
        yield result

# check for arguments
if len(sys.argv)<2:
    makefile_am = "Makefile.am"
else:
    makefile_am = sys.argv[1]


# read in Makefile.am
f = open(makefile_am,"r")
mf = f.readlines()
f.close()

# parse for variables
re_variable = re.compile("^(#tdevelop:[ \t]*)?([A-Za-z][A-Za-z0-9_]*)[ \t]*:?=[ \t]*(.*)$")
variables = {}


for l in real_lines(mf):
    m = re_variable.match(l)
    if m!=None:
        (nowt, lhs, rhs) = m.groups()
        variables[lhs]=rhs

# look for app sources
for (var,data) in list(variables.items()):
    spl = var.split("_",1)
    spl.append("")
    (prefix,primary) = (spl[0], spl[1])
    if primary=="PROGRAMS":
        target=data
        if target + "_SOURCES" in variables:
            sources = variables[target+"_SOURCES"].split()

# change .ui into .py
py_sources = []
for s in sources:
    py_sources.append(re.sub("\.ui$",".py",s))
    
# replace dependencies of main target with list of .py sources
# FIXME escape target
re_maintarget = re.compile("(" + target + ".*?:).*")
out = []
for l in mf:
    m = re_maintarget.match(l)
    if m!=None:
        (tar,)=m.groups()
        l=tar + " " + " ".join(py_sources) + "\n"
    out.append(l)

# backup existing Makefile.am
f = open(makefile_am + "~", "w")
f.writelines(mf)
f.close()

# write file
f = open(makefile_am, "w")
f.writelines(out)
f.close()