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"), |
Jack Jansen | cb54be8 | 2000-11-26 23:02:38 +0000 | [diff] [blame] | 12 | ("tmp_alllibraries", "file", "template-alllibraries.xml", "libraries"), |
| 13 | ("tmp_linkorderlib", "file", "template-linkorderlib.xml", "libraries"), |
| 14 | ("tmp_grouplistlib", "file", "template-grouplistlib.xml", "libraries"), |
Jack Jansen | 07642c3 | 2000-09-22 23:26:55 +0000 | [diff] [blame] | 15 | ("tmp_extrasearchdirs", "file", "template-searchdirs.xml", "extrasearchdirs"), |
Jack Jansen | 76167fe | 2000-09-15 22:44:08 +0000 | [diff] [blame] | 16 | ("tmp_projectxmldata", "file", "template.prj.xml", None) |
| 17 | ] |
| 18 | |
| 19 | class 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 Jansen | cb54be8 | 2000-11-26 23:02:38 +0000 | [diff] [blame] | 30 | if not dict.has_key('prefixname'): |
| 31 | dict['prefixname'] = 'mwerks_plugin_config.h' |
Jack Jansen | 76167fe | 2000-09-15 22:44:08 +0000 | [diff] [blame] | 32 | 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: |
| 50 | self.dict[key] = curkeyvalue |
Jack Jansen | cb54be8 | 2000-11-26 23:02:38 +0000 | [diff] [blame] | 51 | if os.path.isabs(curkeyvalue): |
| 52 | self.dict['pathtype'] = 'Absolute' |
| 53 | else: |
| 54 | self.dict['pathtype'] = 'Project' |
Jack Jansen | 76167fe | 2000-09-15 22:44:08 +0000 | [diff] [blame] | 55 | curkeyvalueresult = self._generate_one_value(datasource, dataname) |
| 56 | result = result + curkeyvalueresult |
| 57 | finally: |
| 58 | # Restore the list |
| 59 | self.dict[key] = keyvalues |
Jack Jansen | cb54be8 | 2000-11-26 23:02:38 +0000 | [diff] [blame] | 60 | self.dict['pathtype'] = None |
| 61 | del self.dict['pathtype'] |
Jack Jansen | 76167fe | 2000-09-15 22:44:08 +0000 | [diff] [blame] | 62 | else: |
| 63 | # Not a multi-element rule. Simply generate |
| 64 | result = self._generate_one_value(datasource, dataname) |
| 65 | # And store the result |
| 66 | self.dict[resultname] = result |
| 67 | |
| 68 | def _generate_one_value(self, datasource, dataname): |
| 69 | if datasource == 'file': |
| 70 | filepath = os.path.join(self.templatedir, dataname) |
| 71 | fp = open(filepath, "r") |
| 72 | format = fp.read() |
| 73 | elif datasource == 'string': |
| 74 | format = dataname |
| 75 | else: |
| 76 | raise Error, 'Datasource should be file or string, not %s'%datasource |
| 77 | return format % self.dict |
| 78 | |
| 79 | def _test(): |
| 80 | dict = { |
Jack Jansen | 07642c3 | 2000-09-22 23:26:55 +0000 | [diff] [blame] | 81 | "mac_projectxmlname" : "controlstrip.prj.xml", # The XML filename (full path) |
| 82 | "mac_exportname" : "controlstrip.prj.exp", # Export file (relative to project) |
| 83 | "mac_outputdir" : ":", # The directory where the DLL is put (relative to project) |
| 84 | "mac_dllname" : "controlstrip.ppc.slb", # The DLL filename (within outputdir) |
| 85 | "mac_targetname" : "controlstrip.ppc", # The targetname within the project |
| 86 | "sysprefix" : sys.prefix, # Where the Python sources live |
| 87 | "mac_sysprefixtype" : "Absolute", # Type of previous pathname |
| 88 | "sources" : ["controlstripmodule.c"], |
| 89 | "extrasearchdirs": [], # -I and -L, in unix terms |
Jack Jansen | 76167fe | 2000-09-15 22:44:08 +0000 | [diff] [blame] | 90 | } |
| 91 | pb = ProjectBuilder(dict) |
| 92 | pb.generate() |
| 93 | fp = open(dict["mac_projectxmlname"], "w") |
| 94 | fp.write(dict["tmp_projectxmldata"]) |
| 95 | |
| 96 | if __name__ == '__main__': |
| 97 | _test() |
| 98 | |