SOURCES: kernel-symsorder.py - use ordered dict - skip configs tha...
glen
glen at pld-linux.org
Mon May 5 20:54:31 CEST 2008
Author: glen Date: Mon May 5 18:54:31 2008 GMT
Module: SOURCES Tag: HEAD
---- Log message:
- use ordered dict
- skip configs that are really taken from env: option env="ARCH"
---- Files affected:
SOURCES:
kernel-symsorder.py (1.2 -> 1.3)
---- Diffs:
================================================================
Index: SOURCES/kernel-symsorder.py
diff -u SOURCES/kernel-symsorder.py:1.2 SOURCES/kernel-symsorder.py:1.3
--- SOURCES/kernel-symsorder.py:1.2 Mon May 5 18:58:04 2008
+++ SOURCES/kernel-symsorder.py Mon May 5 20:54:26 2008
@@ -14,9 +14,62 @@
#
# Date Created
# 2008-04-23
-#
+#
import os,sys
+from UserDict import UserDict
+
+# odict (Ordered Dict) from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
+class odict(UserDict):
+ def __init__(self, dict = None):
+ self._keys = []
+ UserDict.__init__(self, dict)
+
+ def __delitem__(self, key):
+ UserDict.__delitem__(self, key)
+ self._keys.remove(key)
+
+ def __setitem__(self, key, item):
+ UserDict.__setitem__(self, key, item)
+ if key not in self._keys: self._keys.append(key)
+
+ def clear(self):
+ UserDict.clear(self)
+ self._keys = []
+
+ def copy(self):
+ dict = UserDict.copy(self)
+ dict._keys = self._keys[:]
+ return dict
+
+ def items(self):
+ return zip(self._keys, self.values())
+
+ def keys(self):
+ return self._keys
+
+ def popitem(self):
+ try:
+ key = self._keys[-1]
+ except IndexError:
+ raise KeyError('dictionary is empty')
+
+ val = self[key]
+ del self[key]
+
+ return (key, val)
+
+ def setdefault(self, key, failobj = None):
+ UserDict.setdefault(self, key, failobj)
+ if key not in self._keys: self._keys.append(key)
+
+ def update(self, dict):
+ UserDict.update(self, dict)
+ for key in dict.keys():
+ if key not in self._keys: self._keys.append(key)
+
+ def values(self):
+ return map(self.get, self._keys)
class ParsingException(Exception):
pass
@@ -27,14 +80,15 @@
lineno = 0
helplen = 0
result = []
+ index = 0
# strip quotes from string
- def zapquotes(self, str):
+ def stripquotes(self, str):
if str[0] == '"': str = str[1:str.rfind('"')]
return str
# pre-proccess input lines. handles line continuations
- def preparse(self, data):
+ def getline(self, data):
x = data.pop(0)
self.lineno = self.lineno + 1
while True:
@@ -50,7 +104,7 @@
raise StopIteration
def parse(self, filename, basedir = None):
- self.result = []
+ self.result = odict()
if basedir:
self.basedir = basedir
try:
@@ -63,7 +117,7 @@
self.filename = filename
lines = open(filename).read().split("\n")
- for line in self.preparse(lines):
+ for line in self.getline(lines):
if self.helplen:
line = line.expandtabs()
if not len(line) or line[:self.helplen].isspace():
@@ -72,6 +126,13 @@
self.helplen = 0
self.parseline(line)
+ def addconfig(self, config):
+ self.result[config] = config
+
+ def addmenu(self, menu):
+ self.result[self.index] = menu
+ self.index = self.index + 1
+
def parseline(self, line):
words = line.strip().split(None, 1)
# skip empty lines
@@ -81,16 +142,21 @@
if words[0] in ('config', 'menuconfig'):
config = words[1]
- self.result.append(config)
+ self.addconfig(config)
elif words[0] in ('menu'):
- menu = self.zapquotes(words[1])
- self.result.append("")
- self.result.append("#")
- self.result.append("# " + menu)
- self.result.append("#")
+ menu = self.stripquotes(words[1])
+ self.addmenu(menu)
+
+ elif words[0] in ('option'):
+ option = self.stripquotes(words[1])
+ # delete config keys which have: option env="ARCH"
+ if option.find('=') > 0:
+ (key, val) = option.split('=')
+ val = self.stripquotes(val)
+ del self.result[val]
- elif words[0] in ('bool', 'boolean', 'mainmenu', 'option', 'def_tristate', \
+ elif words[0] in ('bool', 'boolean', 'mainmenu', 'def_tristate', \
'optional', 'comment', 'choice', 'endchoice', 'range', 'def_bool', \
'endmenu', 'tristate', 'string', 'hex', 'int', 'prompt', 'default', \
'depends', 'select', 'if', 'endif'):
@@ -100,8 +166,13 @@
self.helplen = len(line[:line.find(words[0])].expandtabs())
elif words[0] == 'source':
- file = self.zapquotes(words[1])
- self.result += MenuParser().parse(file, basedir = self.basedir)
+ file = self.stripquotes(words[1])
+ res = MenuParser().parse(file, basedir = self.basedir)
+ for a in res.keys():
+ if type(a) == int:
+ self.addmenu(res[a])
+ continue
+ self.addconfig(a)
else:
raise ParsingException, "%s:%d: Bad line: %s\n" % (self.filename, self.lineno, line.strip())
@@ -124,5 +195,14 @@
sys.stderr.write(e.message)
sys.exit(1)
- for symbol in res:
+ for symbol in res.keys():
+ if type(symbol) == int:
+ # comments
+ print ""
+ print "#"
+ print "# " + res[symbol]
+ print "#"
+ continue
+
+ # symbols
print symbol
================================================================
---- CVS-web:
http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/SOURCES/kernel-symsorder.py?r1=1.2&r2=1.3&f=u
More information about the pld-cvs-commit
mailing list