2 # -*- coding: iso-8859-1 -*-
3 # Copyright © 2003-2004, The AROS Development Team. All rights reserved.
8 if not len(sys
.argv
) in [2, 4] :
9 print "Usage:",sys
.argv
[0],"tmplfile [inputfile outputfile]"
10 print "Usage:",sys
.argv
[0],"tmplfile --listfile filename"
12 # A regular expression for the start of a template instantiation (ex. %build_module)
13 re_tmplinst
= re
.compile('%([a-zA-Z0-9][a-zA-Z0-9_]*)(?=(?:\s|$))')
14 # A regular expression for the argument specification during template instantiation
15 # (ex. cflags=$(CFLAGS) or uselibs="amiga arosc")
16 re_arg
= re
.compile('([a-zA-Z0-9][a-zA-Z0-9_]*)=([^\s"]+|".*?")?')
18 ##################################
19 # Class and function definitions #
20 ##################################
22 # Exception used throughout this program
24 def __init__(self
, s
):
30 # Scan the given lines for template instantiations
32 # - lines: an array of strings
33 # - templates: an assosiative array of objects of class template
36 # - templrefs: an array of tuples with two elems. First element is the lineno
37 # in the lines array, second is the results of the search for that line with
38 # re_tmplinst. Only templates which name are present in the templates argument
39 # will be added to this array.
40 def generate_templrefs(lines
, templates
):
43 for lineno
in range(len(lines
)):
45 if len(line
) == 0 or line
[0] == "#":
48 m
= re_tmplinst
.search(line
)
49 if m
and templates
.has_key(m
.group(1)) and not (m
.start() > 0 and line
[m
.start()-1] == "#"):
50 templrefs
.append((lineno
, m
))
55 # Write out the lines to a file with instantiating the templates present in the file
57 # - lines: The lines to write out
58 # - templrefs: the instantiated templates present in these lines
59 # - templates: the template definitions
60 # - outfile: the file to write to
62 # This function does not return anything but raises a GenmfException when there are
64 def writelines(lines
, templrefs
, templates
, outfile
):
66 for lineno
, m
in templrefs
:
68 outfile
.writelines(lines
[start
:lineno
])
72 while line
[len(line
)-2] == "\\" and start
< len(lines
):
73 line
= line
[0:len(line
)-2] + lines
[start
]
76 if m
.group(1) == "common":
77 template
.hascommon
= 1
80 templates
[m
.group(1)].write(outfile
, m
.group(1), line
[m
.end():].lstrip(), templates
)
81 except GenmfException
, ge
:
82 raise GenmfException(("In instantiation of %s, line %d\n" % (m
.group(1), lineno
+1))+ge
.s
)
84 if start
< len(lines
):
85 outfile
.writelines(lines
[start
:len(lines
)])
88 # arg is q class that stores the specification of an argument in the template header
89 # The of the arg is not stored in this class but this class is supposed to be
90 # stored in an assosiative array with the names of the arg as indices. E.g
91 # arg['cflags'] gives back an object of this class.
92 # It has the following members:
93 # - ismain: boolean to indicate if it has the /M definition
94 # - isneeded: boolean to indicate if it has the /A definition
95 # - used: boolean to indicate if a value is used in the body of a template
96 # - default: default value when the argument is not given during a template
98 # - value: The value this argument has during a template instantiation is stored
101 # Specification can end with /A or /M
102 re_mode
= re
.compile('/(A|M)')
104 # You create this object with giving it the default value
105 def __init__(self
, default
=None):
110 while default
and len(default
)>1:
111 m
= arg
.re_mode
.match(default
[len(default
)-2:])
114 if m
.group(1) == "M":
116 elif m
.group(1) == "A":
119 sys
.exit('Internal error: Unknown match')
121 default
= default
[:len(default
)-2]
123 if default
and default
[0] == '"':
124 default
= default
[1:len(default
)-1]
126 self
.default
= default
127 # The value field will get the value passed to the argument when the tmeplate is used
131 # template is a class to store the whole definition of a genmf template
133 # - name: name of the template
134 # - args: an assosiative of the arguments of this template
135 # - body: an array of strings with the body of the template
136 # - mainarg: contains the arg with /M if it is present
137 # - used: Is this template already used; used to check for recursive calling
139 # - linerefs: an array to indicate the genmf variables used in the body of the
140 # template. This is generated with the generate_linerefs method of this class
141 # - templrefs: an array to indicate the templates used in the body of the template.
142 # This is generated with the generate_templrefs function of this class.
144 re_arginst
= re
.compile('%\(([a-zA-Z0-9][a-zA-Z0-9_]*)\)')
147 # Generate a template
149 # - name: name of the template
150 # - args: an assosiative array of the arguments defined for this template
151 # - body: an array of the template with the bodylines of the template
152 def __init__(self
, name
, args
, body
):
159 self
.templrefs
= None
161 for argname
, argbody
in args
.items():
164 sys
.exit('A template can have only one main (/M) argument')
165 self
.mainarg
= argbody
167 # Generate the references for the genmf variable used in this template
168 # This function will return an assositive array of tuples with linerefs[lineno]
169 # an array of tuples named argrefs with the tuple of the form (argbody, start, stop)
170 # with argbody an object of class arg, start and stop the start and end of this variable
171 # in the string of this line.
172 def generate_linerefs(self
):
175 while lineno
< len(self
.body
):
177 for m
in template
.re_arginst
.finditer(self
.body
[lineno
]):
178 if self
.args
.has_key(m
.group(1)):
179 argbody
= self
.args
[m
.group(1)]
180 argrefs
.append((argbody
, m
.start(), m
.end()))
184 linerefs
[lineno
] = argrefs
187 self
.linerefs
= linerefs
189 for argname
, argbody
in self
.args
.items():
191 sys
.stderr
.write("Warning: template '%s': unused argument '%s'\n" % (self
.name
, argname
))
194 # Write out the body of the template
195 def write(self
, outfile
, name
, line
, templates
):
197 raise GenmfException("Template '%s' called recursively" % name
)
200 # Reading arguments of the template
203 m
= re_arg
.match(line
)
204 if m
and self
.args
.has_key(m
.group(1)):
207 #sys.stderr.write("Arg:"+m.group(1)+" Value: None Line:"+line+"\n")
208 self
.args
[m
.group(1)].value
= ''
210 #sys.stderr.write("Arg:"+m.group(1)+" Value:"+m.group(2)+" Line:"+line+"\n")
211 if len(value
)>0 and value
[0] == '"':
212 value
= value
[1:len(value
)-1]
213 self
.args
[m
.group(1)].value
= value
214 line
= line
[m
.end():].lstrip()
216 self
.mainarg
.value
= line
[:len(line
)-1]
219 raise GenmfException('Syntax error in arguments: '+line
)
221 if self
.linerefs
== None:
222 self
.generate_linerefs()
223 self
.templrefs
= generate_templrefs(self
.body
, templates
)
225 for argname
, argbody
in self
.args
.items():
226 if argbody
.isneeded
and argbody
.value
== None:
227 raise GenmfException('Arg "%s" not specified but should have been' % argname
)
231 for lineno
, argrefs
in self
.linerefs
.items():
236 for argref
in argrefs
:
238 lineout
= lineout
+ line
[pos
:argref
[1]]
240 if not argref
[0].value
== None:
241 lineout
= lineout
+ argref
[0].value
242 elif argref
[0].default
:
243 lineout
= lineout
+ argref
[0].default
248 lineout
= lineout
+ line
[pos
:]
250 text
[lineno
] = lineout
252 writelines(text
, self
.templrefs
, templates
, outfile
)
255 for argname
, argbody
in self
.args
.items():
261 # Read in the definition of the genmf templates from the given filename
262 # Return an assosiative array of the templates present in this file.
263 def read_templates(filename
):
265 infile
= open(filename
)
267 print "Error reading template file: "+filename
269 re_name
= re
.compile('[a-zA-Z0-9][a-zA-Z0-9_]*(?=(?:\s|$))')
270 re_openstring
= re
.compile('[^\s"]*"[^"]*$')
271 re_define
= re
.compile('%define(?=\s)')
273 lines
= infile
.readlines()
276 while lineno
< len(lines
):
278 if re_define
.match(line
):
279 while line
[len(line
)-2] == "\\" and lineno
< len(lines
):
281 line
= line
[0:len(line
)-2] + lines
[lineno
]
283 line
= line
[7:].strip()
285 m
= re_name
.match(line
)
287 sys
.exit("%s:%d:Error in syntax of template name" % (filename
, lineno
+1))
288 tmplname
= m
.group(0)
289 line
= line
[m
.end():].lstrip()
293 m
= re_arg
.match(line
)
295 sys
.exit("%s:%d:Error in syntax of argument %d Line: %s" % (filename
, lineno
+1, len(args
)+1, line
))
296 args
[m
.group(1)] = arg(m
.group(2))
298 line
= line
[m
.end():].lstrip()
300 #print "Line: %d Template: %s" % (lineno+1, tmplname)
305 while lineno
< len(lines
) and line
[0:4] <> "%end":
309 if lineno
== len(lines
):
310 sys
.exit('%s:End of file reached in a template definition' % filename
)
312 templates
[tmplname
] = template(tmplname
, args
, lines
[bodystart
:lineno
])
326 while i
< len(sys
.argv
):
327 if sys
.argv
[i
] == "--listfile":
328 listfile
= sys
.argv
[i
+1]
331 argv
.append(sys
.argv
[i
])
334 #sys.stderr.write("Reading templates\n")
335 templates
= read_templates(argv
[1])
336 #sys.stderr.write("Read %d templates\n" % len(templates))
339 # Read on input file and write out one outputfile
340 if len(sys
.argv
) == 2:
341 lines
= sys
.stdin
.readlines()
343 infile
= open(sys
.argv
[2], "r")
344 lines
= infile
.readlines()
347 if len(sys
.argv
) == 2:
351 outfile
= open(sys
.argv
[3], "w")
355 writelines(lines
, generate_templrefs(lines
, templates
), templates
, outfile
)
356 except GenmfException
, ge
:
358 if len(sys
.argv
) == 4:
359 s
= sys
.argv
[3]+":"+s
362 # If %common was not present in the file write it out at the end of the file
363 if not template
.hascommon
:
365 if templates
.has_key("common"):
366 templates
["common"].write(outfile
, "common", "", templates
)
371 # When a listfile is specified each line in this listfile is of the form
372 # inputfile outputfile
373 # and apply the instantiation of the templates to all these files listed there
374 infile
= open(listfile
, "r")
375 filelist
= infile
.readlines()
378 for fileno
in range(len(filelist
)):
379 files
= filelist
[fileno
].split()
381 sys
.exit('%s:%d: Syntax error: %s' % (listfile
, fileno
+1, filelist
[fileno
]))
383 sys
.stderr
.write('Regenerating file %4d of %4d\r' % (fileno
+1, len(filelist
)))
386 infile
= open(files
[0], "r")
387 lines
= infile
.readlines()
390 outfile
= open(files
[1], "w")
391 template
.hascommon
= 0
394 writelines(lines
, generate_templrefs(lines
, templates
), templates
, outfile
)
395 except GenmfException
, ge
:
397 if len(sys
.argv
) == 4:
401 if not template
.hascommon
:
403 if templates
.has_key("common"):
404 templates
["common"].write(outfile
, "common", "", templates
)
408 sys
.stderr
.write('\n')