| 1 |
2 |
drasko |
#!/usr/bin/env python
|
| 2 |
|
|
#
|
| 3 |
|
|
# Merge a Configure.help file into a file of CML2 symbol declarations
|
| 4 |
|
|
#
|
| 5 |
|
|
# The Configure.help file must be argument 1; the partial symbol file
|
| 6 |
|
|
# must be argument 2.
|
| 7 |
|
|
#
|
| 8 |
|
|
# When given the -c option, suppress normal output and instead
|
| 9 |
|
|
# consistency-check the prompts.
|
| 10 |
|
|
|
| 11 |
|
|
import sys, string, re, os, os.path
|
| 12 |
|
|
|
| 13 |
|
|
if sys.version[0] < '2':
|
| 14 |
|
|
print "Python 2.0 or later is required for this program."
|
| 15 |
|
|
sys.exit(0)
|
| 16 |
|
|
|
| 17 |
|
|
directory = ""
|
| 18 |
|
|
|
| 19 |
|
|
def extract_dir(line, splitlocs):
|
| 20 |
|
|
global directory
|
| 21 |
|
|
# Handle directory directive
|
| 22 |
|
|
if line[:14] == "#% Directory: ":
|
| 23 |
|
|
fields = line[14:].strip().split()
|
| 24 |
|
|
if len(fields) == 1:
|
| 25 |
|
|
directory = fields[0]
|
| 26 |
|
|
else:
|
| 27 |
|
|
splitlocs[fields[0]] = fields[2]
|
| 28 |
|
|
return 1
|
| 29 |
|
|
else:
|
| 30 |
|
|
return 0
|
| 31 |
|
|
|
| 32 |
|
|
def help_scan(file, prefix):
|
| 33 |
|
|
# This assumes the format of Axel Boldt's Configure.help file
|
| 34 |
|
|
global directory
|
| 35 |
|
|
dict = {}
|
| 36 |
|
|
splitlocs = {}
|
| 37 |
|
|
stream = open(file)
|
| 38 |
|
|
name = None
|
| 39 |
|
|
ringbuffer = [0, 0, 0, 0, 0]
|
| 40 |
|
|
ringindex = choiceflag = 0
|
| 41 |
|
|
prompt = ""
|
| 42 |
|
|
lastline = ""
|
| 43 |
|
|
start = 0
|
| 44 |
|
|
while 1:
|
| 45 |
|
|
line = stream.readline()
|
| 46 |
|
|
if extract_dir(line, splitlocs):
|
| 47 |
|
|
continue
|
| 48 |
|
|
# Now everything else
|
| 49 |
|
|
if line and line[0] == '#':
|
| 50 |
|
|
if line.find("Choice:") > -1:
|
| 51 |
|
|
choiceflag = 1
|
| 52 |
|
|
continue
|
| 53 |
|
|
ringbuffer[ringindex] = here = stream.tell()
|
| 54 |
|
|
ringindex = (ringindex + 1) % 5
|
| 55 |
|
|
if line and line[0] in string.whitespace:
|
| 56 |
|
|
continue
|
| 57 |
|
|
if not line or line[0:7] == prefix:
|
| 58 |
|
|
if name:
|
| 59 |
|
|
if dict.has_key(name):
|
| 60 |
|
|
sys.stderr.write("Duplicate help text for %s\n" % name)
|
| 61 |
|
|
dict[name] = (file, start, ringbuffer[(ringindex - 4) % 5], prompt)
|
| 62 |
|
|
if directory != "UNKNOWN":
|
| 63 |
|
|
splitlocs[name] = directory
|
| 64 |
|
|
directory = "UNKNOWN"
|
| 65 |
|
|
if line:
|
| 66 |
|
|
name = string.strip(line[7:])
|
| 67 |
|
|
start = here
|
| 68 |
|
|
if choiceflag:
|
| 69 |
|
|
prompt = None # Disable prompt checking
|
| 70 |
|
|
else:
|
| 71 |
|
|
prompt = lastline.strip()
|
| 72 |
|
|
choiceflag = 0
|
| 73 |
|
|
else:
|
| 74 |
|
|
break
|
| 75 |
|
|
lastline = line
|
| 76 |
|
|
stream.close()
|
| 77 |
|
|
return (dict, splitlocs)
|
| 78 |
|
|
|
| 79 |
|
|
def fetch_help(symbol, helpdict):
|
| 80 |
|
|
"Fetch help text associated with given symbol, if any."
|
| 81 |
|
|
if helpdict.has_key(symbol):
|
| 82 |
|
|
(file, start, end, prompt) = helpdict[symbol]
|
| 83 |
|
|
stream = open(file)
|
| 84 |
|
|
stream.seek(start)
|
| 85 |
|
|
help = stream.read(end - start)
|
| 86 |
|
|
# Canonicalize trailing whitespace
|
| 87 |
|
|
help = help.rstrip() + "\n"
|
| 88 |
|
|
stream.close()
|
| 89 |
|
|
return help
|
| 90 |
|
|
else:
|
| 91 |
|
|
return None
|
| 92 |
|
|
|
| 93 |
|
|
def merge(helpfile, templatefile):
|
| 94 |
|
|
"Merge a Configure.help with a symbols file, write to stdout."
|
| 95 |
|
|
(helpdict, splitlocs) = help_scan(helpfile, "CONFIG_")
|
| 96 |
|
|
template = open(templatefile, "r")
|
| 97 |
|
|
promptre = re.compile("(?<=['\"])[^'\"]*(?=['\"])")
|
| 98 |
|
|
|
| 99 |
|
|
os.system('rm -f `find kernel-tree -name "*symbols.cml"`')
|
| 100 |
|
|
|
| 101 |
|
|
trim = re.compile("^ ", re.M)
|
| 102 |
|
|
trailing_comment = re.compile("\s*#[^#']*$")
|
| 103 |
|
|
outfp = None
|
| 104 |
|
|
lineno = 0
|
| 105 |
|
|
while 1:
|
| 106 |
|
|
lineno += 1
|
| 107 |
|
|
line = template.readline()
|
| 108 |
|
|
if not line:
|
| 109 |
|
|
break
|
| 110 |
|
|
elif line == "\n":
|
| 111 |
|
|
continue
|
| 112 |
|
|
elif line[0] == "#":
|
| 113 |
|
|
extract_dir(line, splitlocs)
|
| 114 |
|
|
continue
|
| 115 |
|
|
# Sanity check
|
| 116 |
|
|
prompt = promptre.search(line)
|
| 117 |
|
|
if not prompt:
|
| 118 |
|
|
sys.stderr.write("Malformed line %s: %s" % (lineno, line))
|
| 119 |
|
|
raise SystemExit, 1
|
| 120 |
|
|
# We've hit something that ought to be a symbol line
|
| 121 |
|
|
fields = line.split()
|
| 122 |
|
|
symbol = fields[0]
|
| 123 |
|
|
template_has_text = line.find("text\n") > -1
|
| 124 |
|
|
if symbol[:7] == "CONFIG_":
|
| 125 |
|
|
symbol = symbol[7:]
|
| 126 |
|
|
if checkonly:
|
| 127 |
|
|
# Consistency-check the prompts
|
| 128 |
|
|
prompt = prompt.group(0)
|
| 129 |
|
|
if helpdict.has_key(symbol):
|
| 130 |
|
|
oldprompt = helpdict[symbol][3]
|
| 131 |
|
|
if oldprompt == None:
|
| 132 |
|
|
continue
|
| 133 |
|
|
if oldprompt[-15:] == " (EXPERIMENTAL)":
|
| 134 |
|
|
oldprompt = oldprompt[:-15]
|
| 135 |
|
|
if oldprompt[-11:] == " (OBSOLETE)":
|
| 136 |
|
|
oldprompt = oldprompt[:-11]
|
| 137 |
|
|
if oldprompt[-12:] == " (DANGEROUS)":
|
| 138 |
|
|
oldprompt = oldprompt[:-12]
|
| 139 |
|
|
if oldprompt != prompt:
|
| 140 |
|
|
sys.stdout.write("%s:\n" % (symbol,))
|
| 141 |
|
|
sys.stdout.write("CML1: '" + oldprompt + "'\n")
|
| 142 |
|
|
sys.stdout.write("CML2: '" + prompt + "'\n")
|
| 143 |
|
|
while 1:
|
| 144 |
|
|
line = template.readline()
|
| 145 |
|
|
if line == ".\n":
|
| 146 |
|
|
break
|
| 147 |
|
|
else:
|
| 148 |
|
|
# Now splice in the actual help text
|
| 149 |
|
|
helptext = fetch_help(symbol, helpdict)
|
| 150 |
|
|
if helptext and template_has_text:
|
| 151 |
|
|
print line
|
| 152 |
|
|
sys.stderr.write("Template already contains help text for %s!\n" % symbol)
|
| 153 |
|
|
raise SystemExit, 1
|
| 154 |
|
|
if outfp:
|
| 155 |
|
|
outfp.close()
|
| 156 |
|
|
if splitlocs.has_key(symbol):
|
| 157 |
|
|
dest = splitlocs[symbol]
|
| 158 |
|
|
else:
|
| 159 |
|
|
dest = directory
|
| 160 |
|
|
if dest == "UNKNOWN":
|
| 161 |
|
|
sys.stderr.write("No directory for %s\n" % symbol)
|
| 162 |
|
|
sys.exit(1)
|
| 163 |
|
|
#print "%s -> %s" % (symbol, dest)
|
| 164 |
|
|
dest = os.path.join("kernel-tree", dest[1:], "symbols.cml")
|
| 165 |
|
|
exists = os.path.exists(dest)
|
| 166 |
|
|
if exists:
|
| 167 |
|
|
outfp = open(dest, "a")
|
| 168 |
|
|
else:
|
| 169 |
|
|
outfp = open(dest, "w")
|
| 170 |
|
|
outfp.write("symbols\n")
|
| 171 |
|
|
if helptext:
|
| 172 |
|
|
leader = line.rstrip()
|
| 173 |
|
|
comment_match = trailing_comment.search(leader)
|
| 174 |
|
|
if comment_match:
|
| 175 |
|
|
comment = comment_match.group(0)
|
| 176 |
|
|
leader = leader[:comment_match.start(0)]
|
| 177 |
|
|
else:
|
| 178 |
|
|
comment = ""
|
| 179 |
|
|
if len(leader) < 68:
|
| 180 |
|
|
outfp.write(leader + "\ttext")
|
| 181 |
|
|
if comment:
|
| 182 |
|
|
outfp.write("\t" + comment)
|
| 183 |
|
|
outfp.write("\n")
|
| 184 |
|
|
else:
|
| 185 |
|
|
outfp.write(leader + comment + "\ntext\n")
|
| 186 |
|
|
outfp.write(trim.sub("", helptext) + ".\n")
|
| 187 |
|
|
elif template_has_text:
|
| 188 |
|
|
outfp.write(line)
|
| 189 |
|
|
while 1:
|
| 190 |
|
|
line = template.readline()
|
| 191 |
|
|
outfp.write(line)
|
| 192 |
|
|
if line == ".\n":
|
| 193 |
|
|
break
|
| 194 |
|
|
else:
|
| 195 |
|
|
outfp.write(line)
|
| 196 |
|
|
|
| 197 |
|
|
def conditionalize(file, optset):
|
| 198 |
|
|
"Handle conditional inclusions and drop out choice lines."
|
| 199 |
|
|
import re
|
| 200 |
|
|
cond = re.compile(r"^#% (\S*) only$")
|
| 201 |
|
|
infp = open(file)
|
| 202 |
|
|
if optset:
|
| 203 |
|
|
sys.stdout.write("## This version generated for " + " with ".join(optset) + "\n")
|
| 204 |
|
|
while 1:
|
| 205 |
|
|
import re
|
| 206 |
|
|
line = infp.readline()
|
| 207 |
|
|
if not line:
|
| 208 |
|
|
break
|
| 209 |
|
|
if line[:9] == "# Choice:":
|
| 210 |
|
|
continue
|
| 211 |
|
|
match = cond.match(line)
|
| 212 |
|
|
if match and match.group(1) not in optset:
|
| 213 |
|
|
while 1:
|
| 214 |
|
|
line = infp.readline()
|
| 215 |
|
|
if not line:
|
| 216 |
|
|
break
|
| 217 |
|
|
if line == "\n":
|
| 218 |
|
|
line = infp.readline()
|
| 219 |
|
|
break
|
| 220 |
|
|
if line[:2] == "#%": # Drop out other directives
|
| 221 |
|
|
continue
|
| 222 |
|
|
sys.stdout.write(line)
|
| 223 |
|
|
|
| 224 |
|
|
def dump_symbol(symbol, helpdict):
|
| 225 |
|
|
"Dump a help entry."
|
| 226 |
|
|
sys.stdout.write("%s\n" % helpdict[symbol][3])
|
| 227 |
|
|
sys.stdout.write("CONFIG_%s\n" % symbol)
|
| 228 |
|
|
sys.stdout.write(fetch_help(symbol, helpdict))
|
| 229 |
|
|
sys.stdout.write("\n")
|
| 230 |
|
|
|
| 231 |
|
|
if __name__ == "__main__":
|
| 232 |
|
|
import getopt
|
| 233 |
|
|
|
| 234 |
|
|
checkonly = sort = 0
|
| 235 |
|
|
optset = []
|
| 236 |
|
|
(options, arguments) = getopt.getopt(sys.argv[1:], "D:Ecns")
|
| 237 |
|
|
for (switch, val) in options:
|
| 238 |
|
|
if switch == "-D":
|
| 239 |
|
|
optset.append(val)
|
| 240 |
|
|
elif switch == '-E': # Process conditionals
|
| 241 |
|
|
conditionalize(arguments[0], optset)
|
| 242 |
|
|
sys.exit(0)
|
| 243 |
|
|
elif switch == '-c': # Consistency check
|
| 244 |
|
|
checkonly = 1
|
| 245 |
|
|
elif switch == '-n': # List symbols with no match in second arg
|
| 246 |
|
|
import cmlsystem
|
| 247 |
|
|
configuration = cmlsystem.CMLSystem(arguments[1])
|
| 248 |
|
|
helpdict = help_scan(arguments[0], "CONFIG_")
|
| 249 |
|
|
keys = helpdict.keys()
|
| 250 |
|
|
keys.sort()
|
| 251 |
|
|
for symbol in keys:
|
| 252 |
|
|
if not configuration.dictionary.get(symbol):
|
| 253 |
|
|
dump_symbol(symbol, helpdict)
|
| 254 |
|
|
sys.exit(0)
|
| 255 |
|
|
elif switch == '-s': # Emit sorted version
|
| 256 |
|
|
helpdict = help_scan(arguments[0], "CONFIG_")
|
| 257 |
|
|
keys = helpdict.keys()
|
| 258 |
|
|
keys.sort()
|
| 259 |
|
|
for symbol in keys:
|
| 260 |
|
|
dump_symbol(symbol, helpdict)
|
| 261 |
|
|
sys.exit(0)
|
| 262 |
|
|
|
| 263 |
|
|
help = arguments[0]
|
| 264 |
|
|
symbols = arguments[1]
|
| 265 |
|
|
merge(help, symbols)
|
| 266 |
|
|
|
| 267 |
|
|
# That's all, folks!
|