2 # -*- coding: iso-8859-1 -*-
3 # Copyright © 2003-2008, The AROS Development Team. All rights reserved.
5 import sys
, re
, os
, errno
7 if not len(sys
.argv
) in [2, 4] :
8 print "Usage:",sys
.argv
[0],"tmplfile [inputfile outputfile]"
9 print "Usage:",sys
.argv
[0],"tmplfile --listfile filename"
11 # A regular expression for the start of a template instantiation (ex. %build_module)
12 re_tmplinst
= re
.compile('%([a-zA-Z0-9][a-zA-Z0-9_]*)(?=(?:\s|$))')
13 # A regular expression for the argument specification during template instantiation
14 # (ex. cflags=$(CFLAGS) or uselibs="amiga arosc")
15 re_arg
= re
.compile('([a-zA-Z0-9][a-zA-Z0-9_]*)=([^\s"]+|".*?")?')
17 ##################################
18 # Class and function definitions #
19 ##################################
21 # Exception used throughout this program
23 def __init__(self
, s
):
29 # Scan the given lines for template instantiations
31 # - lines: an array of strings
32 # - templates: an assosiative array of objects of class template
35 # - templrefs: an array of tuples with two elems. First element is the lineno
36 # in the lines array, second is the results of the search for that line with
37 # re_tmplinst. Only templates which name are present in the templates argument
38 # will be added to this array.
39 def generate_templrefs(lines
, templates
):
42 for lineno
in range(len(lines
)):
44 if len(line
) == 0 or line
[0] == "#":
47 m
= re_tmplinst
.search(line
)
48 if m
and templates
.has_key(m
.group(1)) and not (m
.start() > 0 and line
[m
.start()-1] == "#"):
49 templrefs
.append((lineno
, m
))
54 # Write out the lines to a file with instantiating the templates present in the file
56 # - lines: The lines to write out
57 # - templrefs: the instantiated templates present in these lines
58 # - templates: the template definitions
59 # - outfile: the file to write to
61 # This function does not return anything but raises a GenmfException when there are
63 def writelines(lines
, templrefs
, templates
, outfile
):
65 for lineno
, m
in templrefs
:
67 outfile
.writelines(lines
[start
:lineno
])
71 while line
[len(line
)-2] == "\\" and start
< len(lines
):
72 line
= line
[0:len(line
)-2] + lines
[start
]
75 if m
.group(1) == "common":
76 template
.hascommon
= 1
79 templates
[m
.group(1)].write(outfile
, m
.group(1), line
[m
.end():].lstrip(), templates
)
80 except GenmfException
, ge
:
81 raise GenmfException(("In instantiation of %s, line %d\n" % (m
.group(1), lineno
+1))+ge
.s
)
83 if start
< len(lines
):
84 outfile
.writelines(lines
[start
:len(lines
)])
87 # arg is q class that stores the specification of an argument in the template header
88 # The of the arg is not stored in this class but this class is supposed to be
89 # stored in an assosiative array with the names of the arg as indices. E.g
90 # arg['cflags'] gives back an object of this class.
91 # It has the following members:
92 # - ismain: boolean to indicate if it has the /M definition
93 # - isneeded: boolean to indicate if it has the /A definition
94 # - used: boolean to indicate if a value is used in the body of a template
95 # - default: default value when the argument is not given during a template
97 # - value: The value this argument has during a template instantiation is stored
100 # Specification can end with /A or /M
101 re_mode
= re
.compile('/(A|M)')
103 # You create this object with giving it the default value
104 def __init__(self
, default
=None):
109 while default
and len(default
)>1:
110 m
= arg
.re_mode
.match(default
[len(default
)-2:])
113 if m
.group(1) == "M":
115 elif m
.group(1) == "A":
118 sys
.exit('Internal error: Unknown match')
120 default
= default
[:len(default
)-2]
122 if default
and default
[0] == '"':
123 default
= default
[1:len(default
)-1]
125 self
.default
= default
126 # The value field will get the value passed to the argument when the tmeplate is used
130 # template is a class to store the whole definition of a genmf template
132 # - name: name of the template
133 # - args: an assosiative of the arguments of this template
134 # - body: an array of strings with the body of the template
135 # - mainarg: contains the arg with /M if it is present
136 # - used: Is this template already used; used to check for recursive calling
138 # - linerefs: an array to indicate the genmf variables used in the body of the
139 # template. This is generated with the generate_linerefs method of this class
140 # - templrefs: an array to indicate the templates used in the body of the template.
141 # This is generated with the generate_templrefs function of this class.
143 re_arginst
= re
.compile('%\(([a-zA-Z0-9][a-zA-Z0-9_]*)\)')
146 # Generate a template
148 # - name: name of the template
149 # - args: an assosiative array of the arguments defined for this template
150 # - body: an array of the template with the bodylines of the template
151 def __init__(self
, name
, args
, body
):
158 self
.templrefs
= None
160 for argname
, argbody
in args
.items():
163 sys
.exit('A template can have only one main (/M) argument')
164 self
.mainarg
= argbody
166 # Generate the references for the genmf variable used in this template
167 # This function will return an assositive array of tuples with linerefs[lineno]
168 # an array of tuples named argrefs with the tuple of the form (argbody, start, stop)
169 # with argbody an object of class arg, start and stop the start and end of this variable
170 # in the string of this line.
171 def generate_linerefs(self
):
174 while lineno
< len(self
.body
):
176 for m
in template
.re_arginst
.finditer(self
.body
[lineno
]):
177 if self
.args
.has_key(m
.group(1)):
178 argbody
= self
.args
[m
.group(1)]
179 argrefs
.append((argbody
, m
.start(), m
.end()))
183 linerefs
[lineno
] = argrefs
186 self
.linerefs
= linerefs
188 for argname
, argbody
in self
.args
.items():
190 sys
.stderr
.write("Warning: template '%s': unused argument '%s'\n" % (self
.name
, argname
))
193 # Write out the body of the template
194 def write(self
, outfile
, name
, line
, templates
):
196 raise GenmfException("Template '%s' called recursively" % name
)
199 # Reading arguments of the template
202 m
= re_arg
.match(line
)
203 if m
and self
.args
.has_key(m
.group(1)):
206 #sys.stderr.write("Arg:"+m.group(1)+" Value: None Line:"+line+"\n")
207 self
.args
[m
.group(1)].value
= ''
209 #sys.stderr.write("Arg:"+m.group(1)+" Value:"+m.group(2)+" Line:"+line+"\n")
210 if len(value
)>0 and value
[0] == '"':
211 value
= value
[1:len(value
)-1]
212 self
.args
[m
.group(1)].value
= value
213 line
= line
[m
.end():].lstrip()
215 self
.mainarg
.value
= line
[:len(line
)-1]
218 raise GenmfException('Syntax error in arguments: '+line
)
220 if self
.linerefs
== None:
221 self
.generate_linerefs()
222 self
.templrefs
= generate_templrefs(self
.body
, templates
)
224 for argname
, argbody
in self
.args
.items():
225 if argbody
.isneeded
and argbody
.value
== None:
226 raise GenmfException('Arg "%s" not specified but should have been' % argname
)
230 for lineno
, argrefs
in self
.linerefs
.items():
235 for argref
in argrefs
:
237 lineout
= lineout
+ line
[pos
:argref
[1]]
239 if not argref
[0].value
== None:
240 lineout
= lineout
+ argref
[0].value
241 elif argref
[0].default
:
242 lineout
= lineout
+ argref
[0].default
247 lineout
= lineout
+ line
[pos
:]
249 text
[lineno
] = lineout
251 writelines(text
, self
.templrefs
, templates
, outfile
)
254 for argname
, argbody
in self
.args
.items():
260 # Read in the definition of the genmf templates from the given filename
261 # Return an assosiative array of the templates present in this file.
262 def read_templates(filename
):
264 infile
= open(filename
)
266 print "Error reading template file: "+filename
268 re_name
= re
.compile('[a-zA-Z0-9][a-zA-Z0-9_]*(?=(?:\s|$))')
269 re_openstring
= re
.compile('[^\s"]*"[^"]*$')
270 re_define
= re
.compile('%define(?=\s)')
272 lines
= infile
.readlines()
275 while lineno
< len(lines
):
277 if re_define
.match(line
):
278 while line
[len(line
)-2] == "\\" and lineno
< len(lines
):
280 line
= line
[0:len(line
)-2] + lines
[lineno
]
282 line
= line
[7:].strip()
284 m
= re_name
.match(line
)
286 sys
.exit("%s:%d:Error in syntax of template name" % (filename
, lineno
+1))
287 tmplname
= m
.group(0)
288 line
= line
[m
.end():].lstrip()
292 m
= re_arg
.match(line
)
294 sys
.exit("%s:%d:Error in syntax of argument %d Line: %s" % (filename
, lineno
+1, len(args
)+1, line
))
295 args
[m
.group(1)] = arg(m
.group(2))
297 line
= line
[m
.end():].lstrip()
299 #print "Line: %d Template: %s" % (lineno+1, tmplname)
304 while lineno
< len(lines
) and line
[0:4] <> "%end":
308 if lineno
== len(lines
):
309 sys
.exit('%s:End of file reached in a template definition' % filename
)
311 templates
[tmplname
] = template(tmplname
, args
, lines
[bodystart
:lineno
])
325 while i
< len(sys
.argv
):
326 if sys
.argv
[i
] == "--listfile":
327 listfile
= sys
.argv
[i
+1]
330 argv
.append(sys
.argv
[i
])
333 #sys.stderr.write("Reading templates\n")
334 templates
= read_templates(argv
[1])
335 #sys.stderr.write("Read %d templates\n" % len(templates))
338 # Read one input file and write out one outputfile
339 if len(sys
.argv
) == 2:
340 lines
= sys
.stdin
.readlines()
342 infile
= open(sys
.argv
[2], "r")
343 lines
= infile
.readlines()
346 if len(sys
.argv
) == 2:
350 outfile
= open(sys
.argv
[3], "w")
354 writelines(lines
, generate_templrefs(lines
, templates
), templates
, outfile
)
355 except GenmfException
, ge
:
357 if len(sys
.argv
) == 4:
358 s
= sys
.argv
[3]+":"+s
361 # If %common was not present in the file write it out at the end of the file
362 if not template
.hascommon
:
364 if templates
.has_key("common"):
365 templates
["common"].write(outfile
, "common", "", templates
)
370 # When a listfile is specified each line in this listfile is of the form
371 # inputfile outputfile
372 # and apply the instantiation of the templates to all these files listed there
373 infile
= open(listfile
, "r")
374 filelist
= infile
.readlines()
377 for fileno
in range(len(filelist
)):
378 files
= filelist
[fileno
].split()
380 sys
.exit('%s:%d: Syntax error: %s' % (listfile
, fileno
+1, filelist
[fileno
]))
382 sys
.stderr
.write('Regenerating file %4d of %4d\r' % (fileno
+1, len(filelist
)))
385 infile
= open(files
[0], "r")
386 lines
= infile
.readlines()
390 # os.makedirs will also create all the parent directories
391 os
.makedirs(os
.path
.dirname(files
[1]))
396 outfile
= open(files
[1], "w")
397 template
.hascommon
= 0
400 writelines(lines
, generate_templrefs(lines
, templates
), templates
, outfile
)
401 except GenmfException
, ge
:
403 if len(sys
.argv
) == 4:
407 if not template
.hascommon
:
409 if templates
.has_key("common"):
410 templates
["common"].write(outfile
, "common", "", templates
)
414 sys
.stderr
.write('\n')