blob: 959516f00923dde3548256e7f4a308b134b22639 [file] [log] [blame]
Jack Jansen76167fe2000-09-15 22:44:08 +00001# First attempt at automatically generating CodeWarior projects
2import os
3
4Error="gencwproject.Error"
5#
6# These templates are executed in-order.
7#
8TEMPLATELIST= [
9 ("tmp_allsources", "file", "template-allsources.xml", "sources"),
10 ("tmp_linkorder", "file", "template-linkorder.xml", "sources"),
11 ("tmp_grouplist", "file", "template-grouplist.xml", "sources"),
Jack Jansencb54be82000-11-26 23:02:38 +000012 ("tmp_alllibraries", "file", "template-alllibraries.xml", "libraries"),
13 ("tmp_linkorderlib", "file", "template-linkorderlib.xml", "libraries"),
14 ("tmp_grouplistlib", "file", "template-grouplistlib.xml", "libraries"),
Jack Jansen07642c32000-09-22 23:26:55 +000015 ("tmp_extrasearchdirs", "file", "template-searchdirs.xml", "extrasearchdirs"),
Jack Jansen76167fe2000-09-15 22:44:08 +000016 ("tmp_projectxmldata", "file", "template.prj.xml", None)
17]
18
19class ProjectBuilder:
20 def __init__(self, dict, templatelist=TEMPLATELIST, templatedir=None):
21 if templatedir == None:
22 try:
23 packagedir = os.path.split(__file__)[0]
24 except NameError:
25 packagedir = os.curdir
26 templatedir = os.path.join(packagedir, 'template')
27 if not os.path.exists(templatedir):
28 raise Error, "Cannot file templatedir"
29 self.dict = dict
Jack Jansencb54be82000-11-26 23:02:38 +000030 if not dict.has_key('prefixname'):
31 dict['prefixname'] = 'mwerks_plugin_config.h'
Jack Jansen76167fe2000-09-15 22:44:08 +000032 self.templatelist = templatelist
33 self.templatedir = templatedir
34
35 def generate(self):
36 for tmpl in self.templatelist:
37 self._generate_one_template(tmpl)
38
39 def _generate_one_template(self, tmpl):
40 resultname, datasource, dataname, key = tmpl
41 result = ''
42 if key:
43 # This is a multi-element rule. Run for every item in dict[key]
44 if self.dict.has_key(key):
45 keyvalues = self.dict[key]
46 try:
47 if not type(keyvalues) in (type(()), type([])):
48 raise Error, "List or tuple expected for %s"%key
49 for curkeyvalue in keyvalues:
Jack Jansencb54be82000-11-26 23:02:38 +000050 if os.path.isabs(curkeyvalue):
51 self.dict['pathtype'] = 'Absolute'
52 else:
53 self.dict['pathtype'] = 'Project'
Jack Jansenaee9d6b2000-12-03 22:38:34 +000054 if curkeyvalue[-2:] == ':*':
55 curkeyvalue = curkeyvalue[:-2]
56 self.dict['recursive'] = 'true'
57 else:
58 self.dict['recursive'] = 'false'
59 self.dict[key] = curkeyvalue
Jack Jansen76167fe2000-09-15 22:44:08 +000060 curkeyvalueresult = self._generate_one_value(datasource, dataname)
61 result = result + curkeyvalueresult
62 finally:
63 # Restore the list
64 self.dict[key] = keyvalues
Jack Jansencb54be82000-11-26 23:02:38 +000065 self.dict['pathtype'] = None
66 del self.dict['pathtype']
Jack Jansenaee9d6b2000-12-03 22:38:34 +000067 self.dict['recursive'] = None
68 del self.dict['recursive']
Jack Jansen76167fe2000-09-15 22:44:08 +000069 else:
70 # Not a multi-element rule. Simply generate
71 result = self._generate_one_value(datasource, dataname)
72 # And store the result
73 self.dict[resultname] = result
74
75 def _generate_one_value(self, datasource, dataname):
76 if datasource == 'file':
77 filepath = os.path.join(self.templatedir, dataname)
78 fp = open(filepath, "r")
79 format = fp.read()
80 elif datasource == 'string':
81 format = dataname
82 else:
83 raise Error, 'Datasource should be file or string, not %s'%datasource
84 return format % self.dict
85
86def _test():
87 dict = {
Jack Jansen07642c32000-09-22 23:26:55 +000088 "mac_projectxmlname" : "controlstrip.prj.xml", # The XML filename (full path)
89 "mac_exportname" : "controlstrip.prj.exp", # Export file (relative to project)
90 "mac_outputdir" : ":", # The directory where the DLL is put (relative to project)
91 "mac_dllname" : "controlstrip.ppc.slb", # The DLL filename (within outputdir)
92 "mac_targetname" : "controlstrip.ppc", # The targetname within the project
93 "sysprefix" : sys.prefix, # Where the Python sources live
94 "mac_sysprefixtype" : "Absolute", # Type of previous pathname
95 "sources" : ["controlstripmodule.c"],
96 "extrasearchdirs": [], # -I and -L, in unix terms
Jack Jansen76167fe2000-09-15 22:44:08 +000097 }
98 pb = ProjectBuilder(dict)
99 pb.generate()
100 fp = open(dict["mac_projectxmlname"], "w")
101 fp.write(dict["tmp_projectxmldata"])
102
103if __name__ == '__main__':
104 _test()
105