call init() function from gethelp()
[linux-2.6.git] / configs / kread.py
1 #!/bin/env python
2 #
3 # compare: a tool to compare kernel config files
4 #
5 # Marc E. Fiuczynski <mef@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
7 #
8 # $Id: kread.py,v 1.2 2006/12/01 02:40:40 mef Exp $
9 #
10
11 import sys, re, os
12
13 currentconfig = ""
14 configs = {}
15
16 def _config(parts,fb):
17     global currentconfig
18     currentconfig = parts[1]
19
20 def _help(parts,fb):
21     global currentconfig
22     helptxt = ""
23     lineno = 0
24     while True:
25         line = fb.readline()
26         lineno = lineno + 1
27         if len(line)==0 or not line[0].isspace():break
28         if len(line)>1: line = line.lstrip()
29         helptxt = helptxt+line
30
31     configs[currentconfig]=helptxt
32
33 def _source(parts,fb):
34     filename = "".join(parts[1:])
35     if filename[0]=='"' or filename[0]=='\'':
36         filename=filename[1:]
37     if filename[-1]=='"' or filename[-1]=='\'':
38         filename=filename[:-1]
39     process(filename)
40
41 def _noop(parts,fb):
42     pass
43
44 keywords = {"config":_config,
45             "help":_help,
46             "---help---":_help,
47             "source":_source,
48             "#":_noop}
49
50 def process(filename):
51     fb = open(filename)
52     lineno = 0
53     while True:
54         line = fb.readline()
55         lineno = lineno + 1
56         if len(line)==0:break
57
58         line  = line.strip()
59         parts = line.split()
60         if len(parts)==0:continue
61
62         func  = keywords.get(parts[0],_noop)
63         func(parts,fb)
64
65     fb.close()
66
67 initialized = False
68 def init():
69     if not initialized: 
70         initialized = True
71         ARCH=os.getenv("ARCH","i386")
72         process("arch/%s/Kconfig" % ARCH)
73
74 def gethelp(option):
75     if option[:len("CONFIG_")] == "CONFIG_":
76         option=option[len("CONFIG_"):]
77     init()
78     helptxt = configs.get(option,"")
79     return helptxt
80
81
82 if __name__ == '__main__':
83     if len(sys.argv) == 1:
84         print """USAGE\n%s configoptionname""" % os.path.basename(sys.argv[0])
85     else:
86         option = sys.argv[1]
87         init()
88         helptxt = gethelp(option)
89         print "CONFIG_%s:\n%s" % (option,helptxt)
90