Jack Jansen | 76167fe | 2000-09-15 22:44:08 +0000 | [diff] [blame^] | 1 | # First attempt at automatically generating CodeWarior projects |
| 2 | import os |
| 3 | |
| 4 | Error="gencwproject.Error" |
| 5 | # |
| 6 | # These templates are executed in-order. |
| 7 | # |
| 8 | TEMPLATELIST= [ |
| 9 | ("tmp_allsources", "file", "template-allsources.xml", "sources"), |
| 10 | ("tmp_linkorder", "file", "template-linkorder.xml", "sources"), |
| 11 | ("tmp_grouplist", "file", "template-grouplist.xml", "sources"), |
| 12 | ("tmp_projectxmldata", "file", "template.prj.xml", None) |
| 13 | ] |
| 14 | |
| 15 | class ProjectBuilder: |
| 16 | def __init__(self, dict, templatelist=TEMPLATELIST, templatedir=None): |
| 17 | if templatedir == None: |
| 18 | try: |
| 19 | packagedir = os.path.split(__file__)[0] |
| 20 | except NameError: |
| 21 | packagedir = os.curdir |
| 22 | templatedir = os.path.join(packagedir, 'template') |
| 23 | if not os.path.exists(templatedir): |
| 24 | raise Error, "Cannot file templatedir" |
| 25 | self.dict = dict |
| 26 | self.templatelist = templatelist |
| 27 | self.templatedir = templatedir |
| 28 | |
| 29 | def generate(self): |
| 30 | for tmpl in self.templatelist: |
| 31 | self._generate_one_template(tmpl) |
| 32 | |
| 33 | def _generate_one_template(self, tmpl): |
| 34 | resultname, datasource, dataname, key = tmpl |
| 35 | result = '' |
| 36 | if key: |
| 37 | # This is a multi-element rule. Run for every item in dict[key] |
| 38 | if self.dict.has_key(key): |
| 39 | keyvalues = self.dict[key] |
| 40 | try: |
| 41 | if not type(keyvalues) in (type(()), type([])): |
| 42 | raise Error, "List or tuple expected for %s"%key |
| 43 | for curkeyvalue in keyvalues: |
| 44 | self.dict[key] = curkeyvalue |
| 45 | curkeyvalueresult = self._generate_one_value(datasource, dataname) |
| 46 | result = result + curkeyvalueresult |
| 47 | finally: |
| 48 | # Restore the list |
| 49 | self.dict[key] = keyvalues |
| 50 | else: |
| 51 | # Not a multi-element rule. Simply generate |
| 52 | result = self._generate_one_value(datasource, dataname) |
| 53 | # And store the result |
| 54 | self.dict[resultname] = result |
| 55 | |
| 56 | def _generate_one_value(self, datasource, dataname): |
| 57 | if datasource == 'file': |
| 58 | filepath = os.path.join(self.templatedir, dataname) |
| 59 | fp = open(filepath, "r") |
| 60 | format = fp.read() |
| 61 | elif datasource == 'string': |
| 62 | format = dataname |
| 63 | else: |
| 64 | raise Error, 'Datasource should be file or string, not %s'%datasource |
| 65 | return format % self.dict |
| 66 | |
| 67 | def _test(): |
| 68 | dict = { |
| 69 | "mac_projectxmlname" : "xxnew.prj.xml", |
| 70 | "mac_targetname" : "xxnew.ppc", |
| 71 | "mac_dllname" : "xxnew.ppc.slb", |
| 72 | "sources" : ["xxnewmodule.c"], |
| 73 | "mac_exportname" : "xxnew.prj.exp", |
| 74 | } |
| 75 | pb = ProjectBuilder(dict) |
| 76 | pb.generate() |
| 77 | fp = open(dict["mac_projectxmlname"], "w") |
| 78 | fp.write(dict["tmp_projectxmldata"]) |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | _test() |
| 82 | |