Skip to content

Commit 1e270d6

Browse files
committed
Initial revision
1 parent 4be30e5 commit 1e270d6

16 files changed

Lines changed: 1007 additions & 0 deletions

File tree

Demo/scripts/README

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Contents of this directory:
2+
3+
byteyears.py Print product of a file's size and age
4+
eptags.py Create Emacs TAGS file for Python modules
5+
fact.py Factorize numbers
6+
findlinksto.py Find symbolic links to a given path (prefix)
7+
from.py Summarize mailbox
8+
lfact.py Factorize long numbers
9+
lpwatch.py Watch BSD line printer queues
10+
mkreal.py Turn a symbolic link into a real file or directory
11+
objgraph.py Print object graph from nm output on a library
12+
pdeps.py Print dependencies between Python modules
13+
pi.py Print digits of pi (uses arbitrary precision integers)
14+
primes.py Print prime numbers
15+
ptags.py Create vi tags file for Python modules
16+
suff.py Sort a list of files by suffix
17+
which.py Find a program in $PATH
18+
xxci.py Wrapper for rcsdiff and ci

Demo/scripts/fact.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /usr/local/python
2+
3+
# Factorize numbers, slowly.
4+
# This version uses plain integers and is thus limited to 2**31-1.
5+
6+
import sys
7+
from math import sqrt
8+
9+
error = 'fact.error' # exception
10+
11+
def fact(n):
12+
if n < 1: raise error # fact() argument should be >= 1
13+
if n = 1: return [] # special case
14+
res = []
15+
# Treat even factors special, so we can use i = i+2 later
16+
while n%2 = 0:
17+
res.append(2)
18+
n = n/2
19+
# Try odd numbers up to sqrt(n)
20+
limit = int(sqrt(float(n+1)))
21+
i = 3
22+
while i <= limit:
23+
if n%i = 0:
24+
res.append(i)
25+
n = n/i
26+
limit = int(sqrt(float(n+1)))
27+
else:
28+
i = i+2
29+
res.append(n)
30+
return res
31+
32+
def main():
33+
if len(sys.argv) > 1:
34+
for arg in sys.argv[1:]:
35+
n = int(eval(arg))
36+
print n, fact(n)
37+
else:
38+
try:
39+
while 1:
40+
n = int(input())
41+
print n, fact(n)
42+
except EOFError:
43+
pass
44+
45+
main()

Demo/scripts/from.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! /usr/local/python
2+
3+
# Print From and Subject of messages in $MAIL.
4+
# Extension to multiple mailboxes and other bells & whistles are left
5+
# as exercises for the reader.
6+
7+
import posix
8+
9+
# Open mailbox file. Exits with exception when this fails.
10+
11+
mail = open(posix.environ['MAIL'], 'r')
12+
13+
while 1:
14+
line = mail.readline()
15+
if not line: break # EOF
16+
if line[:5] = 'From ':
17+
# Start of message found
18+
print line[:-1],
19+
while 1:
20+
line = mail.readline()
21+
if not line: break # EOF
22+
if line = '\n': break # Blank line ends headers
23+
if line[:8] = 'Subject:':
24+
print `line[9:-1]`,
25+
print

Demo/scripts/lpwatch.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#! /ufs/guido/bin/sgi/python
2+
#! /usr/local/python
3+
4+
# Watch line printer queue(s).
5+
# Intended for BSD 4.3 lpq.
6+
7+
import posix
8+
import sys
9+
import time
10+
import string
11+
12+
DEF_PRINTER = 'psc'
13+
DEF_DELAY = 10
14+
15+
def main():
16+
delay = DEF_DELAY # XXX Use getopt() later
17+
try:
18+
thisuser = posix.environ['LOGNAME']
19+
except:
20+
thisuser = posix.environ['USER']
21+
printers = sys.argv[1:]
22+
if not printers:
23+
if posix.environ.has_key('PRINTER'):
24+
printers = [posix.environ['PRINTER']]
25+
else:
26+
printers = [DEF_PRINTER]
27+
#
28+
clearhome = posix.popen('clear', 'r').read()
29+
#
30+
while 1:
31+
# Pipe output through cat for extra buffering,
32+
# so the output (which overwrites the previous)
33+
# appears instantaneous.
34+
sys.stdout = posix.popen('exec cat', 'w')
35+
sys.stdout.write(clearhome)
36+
for name in printers:
37+
pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
38+
showstatus(name, pipe, thisuser)
39+
sts = pipe.close()
40+
if sts:
41+
print name + ': *** lpq exit status', sts
42+
sts = sys.stdout.close()
43+
time.sleep(delay)
44+
45+
def showstatus(name, pipe, thisuser):
46+
lines = 0
47+
users = {}
48+
aheadbytes = 0
49+
aheadjobs = 0
50+
userseen = 0
51+
totalbytes = 0
52+
totaljobs = 0
53+
while 1:
54+
line = pipe.readline()
55+
if not line: break
56+
fields = string.split(line)
57+
n = len(fields)
58+
if len(fields) >= 6 and fields[n-1] = 'bytes':
59+
rank = fields[0]
60+
user = fields[1]
61+
job = fields[2]
62+
files = fields[3:-2]
63+
bytes = eval(fields[n-2])
64+
if user = thisuser:
65+
userseen = 1
66+
elif not userseen:
67+
aheadbytes = aheadbytes + bytes
68+
aheadjobs = aheadjobs + 1
69+
totalbytes = totalbytes + bytes
70+
totaljobs = totaljobs + 1
71+
if users.has_key(user):
72+
ujobs, ubytes = users[user]
73+
else:
74+
ujobs, ubytes = 0, 0
75+
ujobs = ujobs + 1
76+
ubytes = ubytes + bytes
77+
users[user] = ujobs, ubytes
78+
else:
79+
if fields and fields[0] <> 'Rank':
80+
if line[-1:] = '\n':
81+
line = line[:-1]
82+
if not lines:
83+
print name + ':',
84+
else:
85+
print
86+
print line,
87+
lines = lines + 1
88+
if totaljobs:
89+
if lines > 1:
90+
print
91+
lines = lines+1
92+
print (totalbytes+1023)/1024, 'K',
93+
if totaljobs <> len(users):
94+
print '(' + `totaljobs` + ' jobs)',
95+
if len(users) = 1:
96+
print 'for', users.keys()[0],
97+
else:
98+
print 'for', len(users), 'users',
99+
if userseen:
100+
if aheadjobs = 0:
101+
print '(' + thisuser + ' first)',
102+
else:
103+
print '(' + `(aheadbytes+1023)/1024`,
104+
print 'K before', thisuser + ')'
105+
if lines:
106+
print
107+
108+
try:
109+
main()
110+
except KeyboardInterrupt:
111+
pass

Demo/scripts/pi.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#! /usr/local/python
2+
3+
# Print digits of pi forever.
4+
#
5+
# The algorithm, using Python's 'long' integers ("bignums"), works
6+
# with continued fractions, and was conceived by Lambert Meertens.
7+
#
8+
# See also the ABC Programmer's Handbook, by Geurts, Meertens & Pemberton,
9+
# published by Prentice-Hall (UK) Ltd., 1990.
10+
11+
import sys
12+
13+
def main():
14+
k, a, b, a1, b1 = 2l, 4l, 1l, 12l, 4l
15+
while 1:
16+
# Next approximation
17+
p, q, k = k*k, 2l*k+1l, k+1l
18+
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
19+
# Print common digits
20+
d, d1 = a/b, a1/b1
21+
#print a, b, a1, b1
22+
while d = d1:
23+
# Use write() to avoid spaces between the digits
24+
sys.stdout.write(`int(d)`)
25+
# Flush so the output is seen immediately
26+
sys.stdout.flush()
27+
a, a1 = 10l*(a%b), 10l*(a1%b1)
28+
d, d1 = a/b, a1/b1
29+
30+
main()

Demo/scripts/primes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#! /usr/local/python
2+
3+
# Print prime numbers in a given range
4+
5+
def main():
6+
import sys
7+
min, max = 2, 0x7fffffff
8+
if sys.argv[1:]:
9+
min = int(eval(sys.argv[1]))
10+
if sys.argv[2:]:
11+
max = int(eval(sys.argv[2]))
12+
primes(min, max)
13+
14+
def primes(min, max):
15+
if 2 >= min: print 2
16+
primes = [2]
17+
i = 3
18+
while i <= max:
19+
for p in primes:
20+
if i%p = 0 or p*p > i: break
21+
if i%p <> 0:
22+
primes.append(i)
23+
if i >= min: print i
24+
i = i+2
25+
26+
main()

Tools/scripts/byteyears.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /usr/local/python
2+
3+
# byteyears file ...
4+
#
5+
# Print a number representing the product of age and size of each file,
6+
# in suitable units.
7+
8+
import sys, posix, time
9+
from stat import *
10+
11+
secs_per_year = 365.0 * 24.0 * 3600.0
12+
now = time.time()
13+
status = 0
14+
15+
for file in sys.argv[1:]:
16+
try:
17+
st = posix.stat(file)
18+
except posix.error, msg:
19+
sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n')
20+
status = 1
21+
st = ()
22+
if st:
23+
mtime = st[ST_MTIME]
24+
size = st[ST_SIZE]
25+
age = now - mtime
26+
byteyears = float(size) * float(age) / secs_per_year
27+
print file + '\t\t' + `int(byteyears)`
28+
29+
sys.exit(status)

Tools/scripts/eptags.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#! /usr/local/python
2+
3+
# eptags
4+
#
5+
# Create a TAGS file for Python programs, usable with GNU Emacs (version 18).
6+
# Tagged are:
7+
# - functions (even inside other defs or classes)
8+
# - classes
9+
# Warns about files it cannot open.
10+
# No warnings about duplicate tags.
11+
12+
import sys
13+
import regexp
14+
15+
def main():
16+
outfp = open('TAGS', 'w')
17+
args = sys.argv[1:]
18+
for file in args:
19+
treat_file(file, outfp)
20+
21+
matcher = regexp.compile('^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*\(')
22+
23+
def treat_file(file, outfp):
24+
try:
25+
fp = open(file, 'r')
26+
except:
27+
print 'Cannot open', file
28+
return
29+
charno = 0
30+
lineno = 0
31+
tags = []
32+
size = 0
33+
while 1:
34+
line = fp.readline()
35+
if not line: break
36+
lineno = lineno + 1
37+
res = matcher.exec(line)
38+
if res:
39+
(a, b), (a1, b1), (a2, b2) = res
40+
name = line[a2:b2]
41+
pat = line[a:b]
42+
tag = pat + '\177' + `lineno` + ',' + `charno` + '\n'
43+
tags.append(name, tag)
44+
size = size + len(tag)
45+
charno = charno + len(line)
46+
outfp.write('\f\n' + file + ',' + `size` + '\n')
47+
for name, tag in tags:
48+
outfp.write(tag)
49+
50+
main()

Tools/scripts/findlinksto.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /usr/local/python
2+
3+
# findlinksto
4+
#
5+
# find symbolic links to a given path
6+
7+
import posix, path, sys
8+
9+
def visit(pattern, dirname, names):
10+
if path.islink(dirname):
11+
names[:] = []
12+
return
13+
if path.ismount(dirname):
14+
print 'descend into', dirname
15+
n = len(pattern)
16+
for name in names:
17+
name = path.cat(dirname, name)
18+
try:
19+
linkto = posix.readlink(name)
20+
if linkto[:n] = pattern:
21+
print name, '->', linkto
22+
except posix.error:
23+
pass
24+
25+
def main(pattern, args):
26+
for dirname in args:
27+
path.walk(dirname, visit, pattern)
28+
29+
main(sys.argv[1], sys.argv[2:])

0 commit comments

Comments
 (0)