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