Patch to get Click working with Linux 2.6.27.10/Vserver
[linux-2.6.git] / kread
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,v 1.1 2006/12/01 16:21:13 mef Exp $
9 #
10
11 import sys, re, os, stat
12 import cStringIO, cPickle
13
14 currentconfig = ""
15 files = []
16 configs = {}
17 meta = None
18
19 def _config(parts,fb):
20     global currentconfig
21     currentconfig = parts[1]
22     return None
23
24 def _help(parts,fb):
25     global currentconfig
26     helptxt = ""
27     lineno = 0
28     while True:
29         line = fb.readline()
30         lineno = lineno + 1
31         if len(line)==0: break
32         if not line[0].isspace(): break
33         if len(line)>1: newline = line.lstrip()
34         else: newline = line
35         helptxt = helptxt+newline
36
37     configs[currentconfig]=helptxt
38     return line
39
40 def _source(parts,fb):
41     filename = "".join(parts[1:])
42     if filename[0]=='"' or filename[0]=='\'':
43         filename=filename[1:]
44     if filename[-1]=='"' or filename[-1]=='\'':
45         filename=filename[:-1]
46     process(filename)
47     return None
48
49 def _noop(parts,fb):
50     return None
51
52 keywords = {"config":_config,
53             "help":_help,
54             "---help---":_help,
55             "source":_source}
56
57 def process(filename):
58     files.append((filename,os.stat(filename)[stat.ST_MTIME])) 
59     fb = open(filename)
60     lineno = 0
61     line = fb.readline()
62     while True:
63         lineno = lineno + 1
64         if len(line)==0:break
65
66         line  = line.strip()
67         parts = line.split()
68         if len(parts)==0:
69             line = fb.readline()
70         else:
71             func  = keywords.get(parts[0],_noop)
72             line = func(parts,fb)
73             if line == None:
74                 line = fb.readline()
75
76     fb.close()
77
78 def init(force):
79     global configs
80     global files
81     ARCH=os.getenv("ARCH","i386")
82     reprocess = True
83     try:
84         f = open(".kread.db","r")
85         meta = cPickle.load(f)
86         f.close()
87         configs, files = meta
88         reprocess = False
89         try:
90             for file, mtime in files:
91                 cur_mtime = os.stat(file)[stat.ST_MTIME]
92                 if mtime <> cur_mtime:
93                     reprocess = True
94                     break
95         except:
96             pass
97     except IOError, e:
98         pass
99     if reprocess or force:
100         meta = (configs, files)
101         process("arch/%s/Kconfig" % ARCH)
102     try:
103         f = open(".kread.db.tmp","w")
104         cPickle.dump(meta,f,True)
105         f.close()
106         os.rename(".kread.db.tmp", ".kread.db")
107     except IOError, e:
108         pass
109             
110 def gethelp(option):
111     if option[:len("CONFIG_")] == "CONFIG_":
112         option=option[len("CONFIG_"):]
113     helptxt = configs.get(option,"")
114     return helptxt
115
116
117 if __name__ == '__main__':
118     if len(sys.argv) == 1:
119         print """USAGE\n%s configoptionname""" % os.path.basename(sys.argv[0])
120     else:
121         if sys.argv[1] == "-f" and len(sys.argv)>=2:
122             force = True
123             options=sys.argv[2:]
124         else:
125             options = sys.argv[1:]
126             force = False
127
128         init(force)
129         for option in options:
130             helptxt = gethelp(option)
131             print "CONFIG_%s:\n%s" % (option,helptxt)
132