| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | # Parse Makefiles and Python Setup(.in) files. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-18 11:27:58 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Extract variable definitions from a Makefile. | 
					
						
							|  |  |  | # Return a dictionary mapping names to values. | 
					
						
							|  |  |  | # May raise IOError. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-18 11:27:58 +00:00
										 |  |  | makevardef = re.compile('^([a-zA-Z0-9_]+)[ \t]*=(.*)') | 
					
						
							| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def getmakevars(filename): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     variables = {} | 
					
						
							|  |  |  |     fp = open(filename) | 
					
						
							|  |  |  |     pendingline = "" | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         while 1: | 
					
						
							|  |  |  |             line = fp.readline() | 
					
						
							|  |  |  |             if pendingline: | 
					
						
							|  |  |  |                 line = pendingline + line | 
					
						
							|  |  |  |                 pendingline = "" | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             if line.endswith('\\\n'): | 
					
						
							|  |  |  |                 pendingline = line[:-2] | 
					
						
							|  |  |  |             matchobj = makevardef.match(line) | 
					
						
							|  |  |  |             if not matchobj: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             (name, value) = matchobj.group(1, 2) | 
					
						
							|  |  |  |             # Strip trailing comment | 
					
						
							|  |  |  |             i = value.find('#') | 
					
						
							|  |  |  |             if i >= 0: | 
					
						
							|  |  |  |                 value = value[:i] | 
					
						
							|  |  |  |             value = value.strip() | 
					
						
							|  |  |  |             variables[name] = value | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         fp.close() | 
					
						
							|  |  |  |     return variables | 
					
						
							| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Parse a Python Setup(.in) file. | 
					
						
							|  |  |  | # Return two dictionaries, the first mapping modules to their | 
					
						
							|  |  |  | # definitions, the second mapping variable names to their values. | 
					
						
							|  |  |  | # May raise IOError. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-18 11:27:58 +00:00
										 |  |  | setupvardef = re.compile('^([a-zA-Z0-9_]+)=(.*)') | 
					
						
							| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def getsetupinfo(filename): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     modules = {} | 
					
						
							|  |  |  |     variables = {} | 
					
						
							|  |  |  |     fp = open(filename) | 
					
						
							|  |  |  |     pendingline = "" | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         while 1: | 
					
						
							|  |  |  |             line = fp.readline() | 
					
						
							|  |  |  |             if pendingline: | 
					
						
							|  |  |  |                 line = pendingline + line | 
					
						
							|  |  |  |                 pendingline = "" | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             # Strip comments | 
					
						
							|  |  |  |             i = line.find('#') | 
					
						
							|  |  |  |             if i >= 0: | 
					
						
							|  |  |  |                 line = line[:i] | 
					
						
							|  |  |  |             if line.endswith('\\\n'): | 
					
						
							|  |  |  |                 pendingline = line[:-2] | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             matchobj = setupvardef.match(line) | 
					
						
							|  |  |  |             if matchobj: | 
					
						
							|  |  |  |                 (name, value) = matchobj.group(1, 2) | 
					
						
							|  |  |  |                 variables[name] = value.strip() | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 words = line.split() | 
					
						
							|  |  |  |                 if words: | 
					
						
							|  |  |  |                     modules[words[0]] = words[1:] | 
					
						
							|  |  |  |     finally: | 
					
						
							|  |  |  |         fp.close() | 
					
						
							|  |  |  |     return modules, variables | 
					
						
							| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Test the above functions. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test(): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     import sys | 
					
						
							|  |  |  |     import os | 
					
						
							|  |  |  |     if not sys.argv[1:]: | 
					
						
							| 
									
										
										
										
											2007-02-09 23:27:01 +00:00
										 |  |  |         print('usage: python parsesetup.py Makefile*|Setup* ...') | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |         sys.exit(2) | 
					
						
							|  |  |  |     for arg in sys.argv[1:]: | 
					
						
							|  |  |  |         base = os.path.basename(arg) | 
					
						
							|  |  |  |         if base[:8] == 'Makefile': | 
					
						
							| 
									
										
										
										
											2007-02-09 23:27:01 +00:00
										 |  |  |             print('Make style parsing:', arg) | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |             v = getmakevars(arg) | 
					
						
							|  |  |  |             prdict(v) | 
					
						
							|  |  |  |         elif base[:5] == 'Setup': | 
					
						
							| 
									
										
										
										
											2007-02-09 23:27:01 +00:00
										 |  |  |             print('Setup style parsing:', arg) | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |             m, v = getsetupinfo(arg) | 
					
						
							|  |  |  |             prdict(m) | 
					
						
							|  |  |  |             prdict(v) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2007-02-09 23:27:01 +00:00
										 |  |  |             print(arg, 'is neither a Makefile nor a Setup file') | 
					
						
							|  |  |  |             print('(name must begin with "Makefile" or "Setup")') | 
					
						
							| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def prdict(d): | 
					
						
							| 
									
										
										
										
											2007-06-12 00:28:30 +00:00
										 |  |  |     keys = sorted(d.keys()) | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     for key in keys: | 
					
						
							|  |  |  |         value = d[key] | 
					
						
							| 
									
										
										
										
											2007-02-09 23:27:01 +00:00
										 |  |  |         print("%-15s" % key, str(value)) | 
					
						
							| 
									
										
										
										
											1994-10-03 16:33:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     test() |