Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07c9645

Browse files
committed
Get rid of freeze (now its own directory).
Added some new demos. Fixed a few others.
1 parent 0118134 commit 07c9645

6 files changed

Lines changed: 345 additions & 25 deletions

File tree

Demo/scripts/README

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ findlinksto.py Recursively find symbolic links to a given path prefix
2121
fixps.py Fix Python scripts' first line (if #!)
2222
fixcid.py Massive identifier substitution on C source files
2323
fixheader.py Add some cpp magic to a C include file
24-
freeze.py Convert a Python script into a free-standing binary
2524
from.py Summarize mailbox
2625
ftpstats.py Summarize ftp daemon log file
2726
ifdef.py Remove #if(n)def groups from C sources
@@ -31,6 +30,7 @@ lpwatch.py Watch BSD line printer queues
3130
markov.py Markov chain simulation of words or characters
3231
mboxconvvert.py Convert MH or MMDF mailboxes to unix mailbox format
3332
methfix.py Fix old method syntax def f(self, (a1, ..., aN)):
33+
morse.py Produce morse code (audible or on AIFF file)
3434
mkreal.py Turn a symbolic link into a real file or directory
3535
mpzpi.py test mpz -- print digits of pi (compare pi.py)
3636
objgraph.py Print object graph from nm output on a library
@@ -41,7 +41,9 @@ pindent.py Indent Python code, giving block-closing comments
4141
pp.py Emulate some Perl command line options
4242
primes.py Print prime numbers
4343
ptags.py Create vi tags file for Python modules
44+
script.py Equivalent to BSD script(1) -- by Steen Lumholt
4445
suff.py Sort a list of files by suffix
46+
sum5.py Print md5 checksums of files
4547
unbirthday.py Print unbirthday count
4648
which.py Find a program in $PATH
4749
xxci.py Wrapper for rcsdiff and ci

Demo/scripts/morse.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# DAH should be three DOTs.
2+
# Space between DOTs and DAHs should be one DOT.
3+
# Space between two letters should be one DAH.
4+
# Space between two words should be DOT DAH DAH.
5+
6+
import sys, math, audiodev
7+
8+
DOT = 30
9+
DAH = 3 * DOT
10+
OCTAVE = 2 # 1 == 441 Hz, 2 == 882 Hz, ...
11+
12+
morsetab = {
13+
'A': '.-', 'a': '.-',
14+
'B': '-...', 'b': '-...',
15+
'C': '-.-.', 'c': '-.-.',
16+
'D': '-..', 'd': '-..',
17+
'E': '.', 'e': '.',
18+
'F': '..-.', 'f': '..-.',
19+
'G': '--.', 'g': '--.',
20+
'H': '....', 'h': '....',
21+
'I': '..', 'i': '..',
22+
'J': '.---', 'j': '.---',
23+
'K': '-.-', 'k': '-.-',
24+
'L': '.-..', 'l': '.-..',
25+
'M': '--', 'm': '--',
26+
'N': '-.', 'n': '-.',
27+
'O': '---', 'o': '---',
28+
'P': '.--.', 'p': '.--.',
29+
'Q': '--.-', 'q': '--.-',
30+
'R': '.-.', 'r': '.-.',
31+
'S': '...', 's': '...',
32+
'T': '-', 't': '-',
33+
'U': '..-', 'u': '..-',
34+
'V': '...-', 'v': '...-',
35+
'W': '.--', 'w': '.--',
36+
'X': '-..-', 'x': '-..-',
37+
'Y': '-.--', 'y': '-.--',
38+
'Z': '--..', 'z': '--..',
39+
'0': '-----',
40+
'1': '.----',
41+
'2': '..---',
42+
'3': '...--',
43+
'4': '....-',
44+
'5': '.....',
45+
'6': '-....',
46+
'7': '--...',
47+
'8': '---..',
48+
'9': '----.',
49+
',': '--..--',
50+
'.': '.-.-.-',
51+
'?': '..--..',
52+
';': '-.-.-.',
53+
':': '---...',
54+
"'": '.----.',
55+
'-': '-....-',
56+
'/': '-..-.',
57+
'(': '-.--.-',
58+
')': '-.--.-',
59+
'_': '..--.-',
60+
' ': ' '
61+
}
62+
63+
# If we play at 44.1 kHz (which we do), then if we produce one sine
64+
# wave in 100 samples, we get a tone of 441 Hz. If we produce two
65+
# sine waves in these 100 samples, we get a tone of 882 Hz. 882 Hz
66+
# appears to be a nice one for playing morse code.
67+
def mkwave(octave):
68+
global sinewave, nowave
69+
sinewave = ''
70+
for i in range(100):
71+
val = int(math.sin(math.pi * float(i) * octave / 50.0) * 30000)
72+
sinewave = sinewave + chr((val >> 8) & 255) + chr(val & 255)
73+
nowave = '\0' * 200
74+
75+
mkwave(OCTAVE)
76+
77+
def main():
78+
import getopt, string
79+
try:
80+
opts, args = getopt.getopt(sys.argv[1:], 'o:p:')
81+
except getopt.error:
82+
sys.stderr.write('Usage ' + sys.argv[0] +
83+
' [ -o outfile ] [ args ] ...\n')
84+
sys.exit(1)
85+
dev = None
86+
for o, a in opts:
87+
if o == '-o':
88+
import aifc
89+
dev = aifc.open(a, 'w')
90+
dev.setframerate(44100)
91+
dev.setsampwidth(2)
92+
dev.setnchannels(1)
93+
if o == '-p':
94+
mkwave(string.atoi(a))
95+
if not dev:
96+
import audiodev
97+
dev = audiodev.AudioDev()
98+
dev.setoutrate(44100)
99+
dev.setsampwidth(2)
100+
dev.setnchannels(1)
101+
dev.close = dev.stop
102+
dev.writeframesraw = dev.writeframes
103+
if args:
104+
line = string.join(args)
105+
else:
106+
line = sys.stdin.readline()
107+
while line:
108+
mline = morse(line)
109+
play(mline, dev)
110+
if hasattr(dev, 'wait'):
111+
dev.wait()
112+
if not args:
113+
line = sys.stdin.readline()
114+
else:
115+
line = ''
116+
dev.close()
117+
118+
# Convert a string to morse code with \001 between the characters in
119+
# the string.
120+
def morse(line):
121+
res = ''
122+
for c in line:
123+
try:
124+
res = res + morsetab[c] + '\001'
125+
except KeyError:
126+
pass
127+
return res
128+
129+
# Play a line of morse code.
130+
def play(line, dev):
131+
for c in line:
132+
if c == '.':
133+
sine(dev, DOT)
134+
elif c == '-':
135+
sine(dev, DAH)
136+
else: # space
137+
pause(dev, DAH + DOT)
138+
pause(dev, DOT)
139+
140+
def sine(dev, length):
141+
for i in range(length):
142+
dev.writeframesraw(sinewave)
143+
144+
def pause(dev, length):
145+
for i in range(length):
146+
dev.writeframesraw(nowave)
147+
148+
if __name__ == '__main__' or sys.argv[0] == __name__:
149+
main()

Demo/scripts/script.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! /usr/local/bin/python
2+
# script.py -- Make typescript of terminal session.
3+
# Usage:
4+
# -a Append to typescript.
5+
# -p Use Python as shell.
6+
# Author: Steen Lumholt.
7+
8+
9+
import os, time, sys
10+
import pty
11+
12+
def read(fd):
13+
data = os.read(fd, 1024)
14+
file.write(data)
15+
return data
16+
17+
shell = 'sh'
18+
filename = 'typescript'
19+
mode = 'w'
20+
if os.environ.has_key('SHELL'):
21+
shell = os.environ['SHELL']
22+
if '-a' in sys.argv:
23+
mode = 'a'
24+
if '-p' in sys.argv:
25+
shell = 'python'
26+
27+
file = open(filename, mode)
28+
29+
sys.stdout.write('Script started, file is %s\n' % filename)
30+
file.write('Script started on %s\n' % time.ctime(time.time()))
31+
pty.spawn(shell, read)
32+
file.write('Script done on %s\n' % time.ctime(time.time()))
33+
sys.stdout.write('Script done, file is %s\n' % filename)

Tools/scripts/fixcid.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def fix(filename):
194194

195195
# Tokenizing ANSI C (partly)
196196

197-
Identifier = '[a-zA-Z_][a-zA-Z0-9_]+'
197+
Identifier = '\(struct \)?[a-zA-Z_][a-zA-Z0-9_]+'
198198
String = '"\([^\n\\"]\|\\\\.\)*"'
199199
Char = '\'\([^\n\\\']\|\\\\.\)*\''
200200
CommentStart = '/\*'
@@ -246,6 +246,7 @@ def fixline(line):
246246
if Program is InsideCommentProgram:
247247
if not Docomments:
248248
print 'Found in comment:', found
249+
i = i + n
249250
continue
250251
if NotInComment.has_key(found):
251252
## print 'Ignored in comment:',
@@ -290,7 +291,9 @@ def addsubst(substfile):
290291
i = -1 # Happens to delete trailing \n
291292
words = string.split(line[:i])
292293
if not words: continue
293-
if len(words) <> 2:
294+
if len(words) == 3 and words[0] == 'struct':
295+
words[:2] == [words[0] + ' ' + words[1]]
296+
elif len(words) <> 2:
294297
err(substfile + ':' + `lineno` +
295298
': warning: bad line: ' + line)
296299
continue

Tools/scripts/h2py.py

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
#! /usr/local/bin/python
22

3-
# Read #define's from stdin and translate to Python code on stdout.
4-
# Very primitive: non-#define's are ignored, as is anything that isn't
5-
# valid Python as it stands.
3+
# Read #define's and translate to Python code.
4+
# Handle #include statements.
5+
# Handle #define macros with one argument.
6+
# Anything that isn't recognized or doesn't translate into valid
7+
# Python is ignored.
8+
9+
# Without filename arguments, acts as a filter.
610
# If one or more filenames are given, output is written to corresponding
711
# filenames in the local directory, translated to all uppercase, with
812
# the extension replaced by ".py".
13+
914
# By passing one or more options of the form "-i regular_expression"
1015
# you can specify additional strings to be ignored. This is useful
1116
# e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".
1217

1318
# XXX To do:
1419
# - turn trailing C comments into Python comments
15-
# - turn C string quotes into Python comments
1620
# - turn C Boolean operators "&& || !" into Python "and or not"
1721
# - what to do about #if(def)?
18-
# - what to do about #include?
19-
# - what to do about macros with parameters?
20-
# - reject definitions with semicolons in them
22+
# - what to do about macros with multiple parameters?
2123

22-
import sys, regex, string, getopt, os
24+
import sys, regex, regsub, string, getopt, os
2325

2426
p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
2527

28+
p_macro = regex.compile(
29+
'^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
30+
31+
p_include = regex.compile('^#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
32+
2633
p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
2734

2835
ignores = [p_comment]
2936

37+
p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
38+
39+
filedict = {}
40+
3041
def main():
3142
opts, args = getopt.getopt(sys.argv[1:], 'i:')
3243
for o, a in opts:
@@ -47,40 +58,65 @@ def main():
4758
outfile = outfile + '.py'
4859
outfp = open(outfile, 'w')
4960
outfp.write('# Generated by h2py from %s\n' % filename)
61+
filedict = {}
62+
if filename[:13] == '/usr/include/':
63+
filedict[filename[13:]] = None
5064
process(fp, outfp)
5165
outfp.close()
5266
fp.close()
5367

54-
def process(fp, outfp):
55-
env = {}
68+
def process(fp, outfp, env = {}):
5669
lineno = 0
5770
while 1:
5871
line = fp.readline()
5972
if not line: break
6073
lineno = lineno + 1
61-
# gobble up continuation lines
62-
while line[-2:] == '\\\n':
63-
nextline = fp.readline()
64-
if not nextline: break
65-
lineno = lineno + 1
66-
line = line + nextline
6774
n = p_define.match(line)
6875
if n >= 0:
76+
# gobble up continuation lines
77+
while line[-2:] == '\\\n':
78+
nextline = fp.readline()
79+
if not nextline: break
80+
lineno = lineno + 1
81+
line = line + nextline
6982
name = p_define.group(1)
7083
body = line[n:]
7184
# replace ignored patterns by spaces
7285
for p in ignores:
73-
while p.search(body) >= 0:
74-
a, b = p.regs[0]
75-
body = body[:a] + ' ' + body[b:]
86+
body = regsub.gsub(p, ' ', body)
87+
# replace char literals by ord(...)
88+
body = regsub.gsub(p_char, 'ord(\\0)', body)
7689
stmt = '%s = %s\n' % (name, string.strip(body))
7790
ok = 0
7891
try:
7992
exec stmt in env
80-
ok = 1
8193
except:
8294
sys.stderr.write('Skipping: %s' % stmt)
83-
if ok:
95+
else:
8496
outfp.write(stmt)
85-
97+
n =p_macro.match(line)
98+
if n >= 0:
99+
macro, arg = p_macro.group(1, 2)
100+
body = line[n:]
101+
for p in ignores:
102+
body = regsub.gsub(p, ' ', body)
103+
body = regsub.gsub(p_char, 'ord(\\0)', body)
104+
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
105+
try:
106+
exec stmt in env
107+
except:
108+
sys.stderr.write('Skipping: %s' % stmt)
109+
else:
110+
outfp.write(stmt)
111+
if p_include.match(line) >= 0:
112+
regs = p_include.regs
113+
a, b = regs[1]
114+
filename = line[a:b]
115+
if not filedict.has_key(filename):
116+
filedict[filename] = None
117+
outfp.write(
118+
'\n# Included from %s\n' % filename)
119+
inclfp = open('/usr/include/' + filename, 'r')
120+
process(inclfp, outfp, env)
86121
main()
122+

0 commit comments

Comments
 (0)