Mercurial > hg > toybox
annotate scripts/config2help.py @ 542:06763d1a2f9d
Tighten up the code a bit, and use actual process group id instead of what /etc/passwd says.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 12 Mar 2012 20:56:56 -0500 |
parents | fb0745eec453 |
children |
rev | line source |
---|---|
139
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 #!/usr/bin/python |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 import os,sys |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 def zapquotes(str): |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 if str[0]=='"': str = str[1:str.rfind('"')] |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 return str |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 def escapequotes(str): |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 return str.strip().replace("\\","\\\\").replace('"','\\"') |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 helplen = morelines = 0 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 out = sys.stdout |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 def readfile(filename): |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 global helplen, morelines |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 #sys.stderr.write("Reading %s\n" % filename) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 try: |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 lines = open(filename).read().split("\n") |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 except IOError: |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 sys.stderr.write("File %s missing\n" % filename) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 return |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 config = None |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 description = None |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 for i in lines: |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 if helplen: |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 i = i.expandtabs() |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 if not len(i) or i[:helplen].isspace(): |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 if morelines: out.write('\\n') |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 morelines = 1 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 out.write(escapequotes(i)) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 continue |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 else: |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 helplen = morelines = 0 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 out.write('"\n') |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 words = i.strip().split(None,1) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 if not len(words): continue |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 if words[0] in ("config", "menuconfig"): |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 config = words[1] |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 description = "" |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 elif words[0] in ("bool", "boolean", "tristate", "string", "hex", "int"): |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 if len(words)>1: description = zapquotes(words[1]) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 elif words[0]=="prompt": |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 description = htmlescape(zapquotes(words[1])) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 elif words[0] in ("help", "---help---"): |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 out.write('#define help_%s "' % config.lower()) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 helplen = len(i[:i.find(words[0])].expandtabs()) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 elif words[0] == "source": readfile(zapquotes(words[1])) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 elif words[0] in ("default","depends", "select", "if", "endif", "#", "comment", "menu", "endmenu"): pass |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 readfile(sys.argv[1]) |
fb0745eec453
Add "help" command. (Building help/help.h requires python, but I'll ship
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 if helplen: out.write('"\n') |