diff options
Diffstat (limited to 'Demo/scripts')
-rwxr-xr-x | Demo/scripts/eqfix.py | 198 | ||||
-rwxr-xr-x | Demo/scripts/find-uname.py | 40 | ||||
-rwxr-xr-x | Demo/scripts/from.py | 35 | ||||
-rwxr-xr-x | Demo/scripts/lpwatch.py | 102 | ||||
-rwxr-xr-x | Demo/scripts/makedir.py | 21 | ||||
-rwxr-xr-x | Demo/scripts/mboxconvert.py | 124 | ||||
-rwxr-xr-x | Demo/scripts/morse.py | 128 | ||||
-rwxr-xr-x | Demo/scripts/newslist.doc | 59 | ||||
-rwxr-xr-x | Demo/scripts/newslist.py | 361 | ||||
-rwxr-xr-x | Demo/scripts/pi.py | 33 | ||||
-rwxr-xr-x | Demo/scripts/pp.py | 125 | ||||
-rwxr-xr-x | Demo/scripts/primes.py | 31 | ||||
-rwxr-xr-x | Demo/scripts/script.py | 42 | ||||
-rwxr-xr-x | Demo/scripts/update.py | 92 |
14 files changed, 0 insertions, 1391 deletions
diff --git a/Demo/scripts/eqfix.py b/Demo/scripts/eqfix.py deleted file mode 100755 index 8d0c8df..0000000 --- a/Demo/scripts/eqfix.py +++ /dev/null @@ -1,198 +0,0 @@ -#! /usr/bin/env python3 - -# Fix Python source files to use the new equality test operator, i.e., -# if x = y: ... -# is changed to -# if x == y: ... -# The script correctly tokenizes the Python program to reliably -# distinguish between assignments and equality tests. -# -# Command line arguments are files or directories to be processed. -# Directories are searched recursively for files whose name looks -# like a python module. -# Symbolic links are always ignored (except as explicit directory -# arguments). Of course, the original file is kept as a back-up -# (with a "~" attached to its name). -# It complains about binaries (files containing null bytes) -# and about files that are ostensibly not Python files: if the first -# line starts with '#!' and does not contain the string 'python'. -# -# Changes made are reported to stdout in a diff-like format. -# -# Undoubtedly you can do this using find and sed or perl, but this is -# a nice example of Python code that recurses down a directory tree -# and uses regular expressions. Also note several subtleties like -# preserving the file's mode and avoiding to even write a temp file -# when no changes are needed for a file. -# -# NB: by changing only the function fixline() you can turn this -# into a program for a different change to Python programs... - -import sys -import re -import os -from stat import * -import string - -err = sys.stderr.write -dbg = err -rep = sys.stdout.write - -def main(): - bad = 0 - if not sys.argv[1:]: # No arguments - err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') - sys.exit(2) - for arg in sys.argv[1:]: - if os.path.isdir(arg): - if recursedown(arg): bad = 1 - elif os.path.islink(arg): - err(arg + ': will not process symbolic links\n') - bad = 1 - else: - if fix(arg): bad = 1 - sys.exit(bad) - -ispythonprog = re.compile('^[a-zA-Z0-9_]+\.py$') -def ispython(name): - return ispythonprog.match(name) >= 0 - -def recursedown(dirname): - dbg('recursedown(%r)\n' % (dirname,)) - bad = 0 - try: - names = os.listdir(dirname) - except os.error as msg: - err('%s: cannot list directory: %r\n' % (dirname, msg)) - return 1 - names.sort() - subdirs = [] - for name in names: - if name in (os.curdir, os.pardir): continue - fullname = os.path.join(dirname, name) - if os.path.islink(fullname): pass - elif os.path.isdir(fullname): - subdirs.append(fullname) - elif ispython(name): - if fix(fullname): bad = 1 - for fullname in subdirs: - if recursedown(fullname): bad = 1 - return bad - -def fix(filename): -## dbg('fix(%r)\n' % (dirname,)) - try: - f = open(filename, 'r') - except IOError as msg: - err('%s: cannot open: %r\n' % (filename, msg)) - return 1 - head, tail = os.path.split(filename) - tempname = os.path.join(head, '@' + tail) - g = None - # If we find a match, we rewind the file and start over but - # now copy everything to a temp file. - lineno = 0 - while 1: - line = f.readline() - if not line: break - lineno = lineno + 1 - if g is None and '\0' in line: - # Check for binary files - err(filename + ': contains null bytes; not fixed\n') - f.close() - return 1 - if lineno == 1 and g is None and line[:2] == '#!': - # Check for non-Python scripts - words = string.split(line[2:]) - if words and re.search('[pP]ython', words[0]) < 0: - msg = filename + ': ' + words[0] - msg = msg + ' script; not fixed\n' - err(msg) - f.close() - return 1 - while line[-2:] == '\\\n': - nextline = f.readline() - if not nextline: break - line = line + nextline - lineno = lineno + 1 - newline = fixline(line) - if newline != line: - if g is None: - try: - g = open(tempname, 'w') - except IOError as msg: - f.close() - err('%s: cannot create: %r\n' % (tempname, msg)) - return 1 - f.seek(0) - lineno = 0 - rep(filename + ':\n') - continue # restart from the beginning - rep(repr(lineno) + '\n') - rep('< ' + line) - rep('> ' + newline) - if g is not None: - g.write(newline) - - # End of file - f.close() - if not g: return 0 # No changes - - # Finishing touch -- move files - - # First copy the file's mode to the temp file - try: - statbuf = os.stat(filename) - os.chmod(tempname, statbuf[ST_MODE] & 0o7777) - except os.error as msg: - err('%s: warning: chmod failed (%r)\n' % (tempname, msg)) - # Then make a backup of the original file as filename~ - try: - os.rename(filename, filename + '~') - except os.error as msg: - err('%s: warning: backup failed (%r)\n' % (filename, msg)) - # Now move the temp file to the original file - try: - os.rename(tempname, filename) - except os.error as msg: - err('%s: rename failed (%r)\n' % (filename, msg)) - return 1 - # Return succes - return 0 - - -from tokenize import tokenprog - -match = {'if':':', 'elif':':', 'while':':', 'return':'\n', \ - '(':')', '[':']', '{':'}', '`':'`'} - -def fixline(line): - # Quick check for easy case - if '=' not in line: return line - - i, n = 0, len(line) - stack = [] - while i < n: - j = tokenprog.match(line, i) - if j < 0: - # A bad token; forget about the rest of this line - print('(Syntax error:)') - print(line, end=' ') - return line - a, b = tokenprog.regs[3] # Location of the token proper - token = line[a:b] - i = i+j - if stack and token == stack[-1]: - del stack[-1] - elif token in match: - stack.append(match[token]) - elif token == '=' and stack: - line = line[:a] + '==' + line[b:] - i, n = a + len('=='), len(line) - elif token == '==' and not stack: - print('(Warning: \'==\' at top level:)') - print(line, end=' ') - return line - -if __name__ == "__main__": - main() diff --git a/Demo/scripts/find-uname.py b/Demo/scripts/find-uname.py deleted file mode 100755 index b6ec1b6..0000000 --- a/Demo/scripts/find-uname.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python3 - -""" -For each argument on the command line, look for it in the set of all Unicode -names. Arguments are treated as case-insensitive regular expressions, e.g.: - - % find-uname 'small letter a$' 'horizontal line' - *** small letter a$ matches *** - LATIN SMALL LETTER A (97) - COMBINING LATIN SMALL LETTER A (867) - CYRILLIC SMALL LETTER A (1072) - PARENTHESIZED LATIN SMALL LETTER A (9372) - CIRCLED LATIN SMALL LETTER A (9424) - FULLWIDTH LATIN SMALL LETTER A (65345) - *** horizontal line matches *** - HORIZONTAL LINE EXTENSION (9135) -""" - -import unicodedata -import sys -import re - -def main(args): - unicode_names = [] - for ix in range(sys.maxunicode+1): - try: - unicode_names.append((ix, unicodedata.name(chr(ix)))) - except ValueError: # no name for the character - pass - for arg in args: - pat = re.compile(arg, re.I) - matches = [(y,x) for (x,y) in unicode_names - if pat.search(y) is not None] - if matches: - print("***", arg, "matches", "***") - for match in matches: - print("%s (%d)" % match) - -if __name__ == "__main__": - main(sys.argv[1:]) diff --git a/Demo/scripts/from.py b/Demo/scripts/from.py deleted file mode 100755 index c8a9346..0000000 --- a/Demo/scripts/from.py +++ /dev/null @@ -1,35 +0,0 @@ -#! /usr/bin/env python3 - -# Print From and Subject of messages in $MAIL. -# Extension to multiple mailboxes and other bells & whistles are left -# as exercises for the reader. - -import sys, os - -# Open mailbox file. Exits with exception when this fails. - -try: - mailbox = os.environ['MAIL'] -except (AttributeError, KeyError): - sys.stderr.write('No environment variable $MAIL\n') - sys.exit(2) - -try: - mail = open(mailbox) -except IOError: - sys.exit('Cannot open mailbox file: ' + mailbox) - -while 1: - line = mail.readline() - if not line: - break # EOF - if line.startswith('From '): - # Start of message found - print(line[:-1], end=' ') - while 1: - line = mail.readline() - if not line or line == '\n': - break - if line.startswith('Subject: '): - print(repr(line[9:-1]), end=' ') - print() diff --git a/Demo/scripts/lpwatch.py b/Demo/scripts/lpwatch.py deleted file mode 100755 index 01d9fea..0000000 --- a/Demo/scripts/lpwatch.py +++ /dev/null @@ -1,102 +0,0 @@ -#! /usr/bin/env python3 - -# Watch line printer queue(s). -# Intended for BSD 4.3 lpq. - -import os -import sys -import time - -DEF_PRINTER = 'psc' -DEF_DELAY = 10 - -def main(): - delay = DEF_DELAY # XXX Use getopt() later - try: - thisuser = os.environ['LOGNAME'] - except: - thisuser = os.environ['USER'] - printers = sys.argv[1:] - if printers: - # Strip '-P' from printer names just in case - # the user specified it... - for i, name in enumerate(printers): - if name[:2] == '-P': - printers[i] = name[2:] - else: - if 'PRINTER' in os.environ: - printers = [os.environ['PRINTER']] - else: - printers = [DEF_PRINTER] - - clearhome = os.popen('clear', 'r').read() - - while True: - text = clearhome - for name in printers: - text += makestatus(name, thisuser) + '\n' - print(text) - time.sleep(delay) - -def makestatus(name, thisuser): - pipe = os.popen('lpq -P' + name + ' 2>&1', 'r') - lines = [] - users = {} - aheadbytes = 0 - aheadjobs = 0 - userseen = False - totalbytes = 0 - totaljobs = 0 - for line in pipe: - fields = line.split() - n = len(fields) - if len(fields) >= 6 and fields[n-1] == 'bytes': - rank, user, job = fields[0:3] - files = fields[3:-2] - bytes = int(fields[n-2]) - if user == thisuser: - userseen = True - elif not userseen: - aheadbytes += bytes - aheadjobs += 1 - totalbytes += bytes - totaljobs += 1 - ujobs, ubytes = users.get(user, (0, 0)) - ujobs += 1 - ubytes += bytes - users[user] = ujobs, ubytes - else: - if fields and fields[0] != 'Rank': - line = line.strip() - if line == 'no entries': - line = name + ': idle' - elif line[-22:] == ' is ready and printing': - line = name - lines.append(line) - - if totaljobs: - line = '%d K' % ((totalbytes+1023) // 1024) - if totaljobs != len(users): - line += ' (%d jobs)' % totaljobs - if len(users) == 1: - line += ' for %s' % next(iter(users)) - else: - line += ' for %d users' % len(users) - if userseen: - if aheadjobs == 0: - line += ' (%s first)' % thisuser - else: - line += ' (%d K before %s)' % ( - (aheadbytes+1023) // 1024, thisuser) - lines.append(line) - - sts = pipe.close() - if sts: - lines.append('lpq exit status %r' % (sts,)) - return ': '.join(lines) - -if __name__ == "__main__": - try: - main() - except KeyboardInterrupt: - pass diff --git a/Demo/scripts/makedir.py b/Demo/scripts/makedir.py deleted file mode 100755 index a14e848..0000000 --- a/Demo/scripts/makedir.py +++ /dev/null @@ -1,21 +0,0 @@ -#! /usr/bin/env python3 - -# Like mkdir, but also make intermediate directories if necessary. -# It is not an error if the given directory already exists (as long -# as it is a directory). -# Errors are not treated specially -- you just get a Python exception. - -import sys, os - -def main(): - for p in sys.argv[1:]: - makedirs(p) - -def makedirs(p): - if p and not os.path.isdir(p): - head, tail = os.path.split(p) - makedirs(head) - os.mkdir(p, 0o777) - -if __name__ == "__main__": - main() diff --git a/Demo/scripts/mboxconvert.py b/Demo/scripts/mboxconvert.py deleted file mode 100755 index cb3ed89..0000000 --- a/Demo/scripts/mboxconvert.py +++ /dev/null @@ -1,124 +0,0 @@ -#! /usr/bin/env python3 - -# Convert MH directories (1 message per file) or MMDF mailboxes (4x^A -# delimited) to unix mailbox (From ... delimited) on stdout. -# If -f is given, files contain one message per file (e.g. MH messages) - -import rfc822 -import sys -import time -import os -import stat -import getopt -import re - -def main(): - dofile = mmdf - try: - opts, args = getopt.getopt(sys.argv[1:], 'f') - except getopt.error as msg: - sys.stderr.write('%s\n' % msg) - sys.exit(2) - for o, a in opts: - if o == '-f': - dofile = message - if not args: - args = ['-'] - sts = 0 - for arg in args: - if arg == '-' or arg == '': - sts = dofile(sys.stdin) or sts - elif os.path.isdir(arg): - sts = mh(arg) or sts - elif os.path.isfile(arg): - try: - f = open(arg) - except IOError as msg: - sys.stderr.write('%s: %s\n' % (arg, msg)) - sts = 1 - continue - sts = dofile(f) or sts - f.close() - else: - sys.stderr.write('%s: not found\n' % arg) - sts = 1 - if sts: - sys.exit(sts) - -numeric = re.compile('[1-9][0-9]*') - -def mh(dir): - sts = 0 - msgs = os.listdir(dir) - for msg in msgs: - if numeric.match(msg) != len(msg): - continue - fn = os.path.join(dir, msg) - try: - f = open(fn) - except IOError as msg: - sys.stderr.write('%s: %s\n' % (fn, msg)) - sts = 1 - continue - sts = message(f) or sts - return sts - -def mmdf(f): - sts = 0 - while 1: - line = f.readline() - if not line: - break - if line == '\1\1\1\1\n': - sts = message(f, line) or sts - else: - sys.stderr.write( - 'Bad line in MMFD mailbox: %r\n' % (line,)) - return sts - -counter = 0 # for generating unique Message-ID headers - -def message(f, delimiter = ''): - sts = 0 - # Parse RFC822 header - m = rfc822.Message(f) - # Write unix header line - fullname, email = m.getaddr('From') - tt = m.getdate('Date') - if tt: - t = time.mktime(tt) - else: - sys.stderr.write( - 'Unparseable date: %r\n' % (m.get('Date'),)) - t = os.fstat(f.fileno())[stat.ST_MTIME] - print('From', email, time.ctime(t)) - # Copy RFC822 header - for line in m.headers: - print(line, end=' ') - # Invent Message-ID header if none is present - if 'message-id' not in m: - global counter - counter = counter + 1 - msgid = "<%s.%d>" % (hex(t), counter) - sys.stderr.write("Adding Message-ID %s (From %s)\n" % - (msgid, email)) - print("Message-ID:", msgid) - print() - # Copy body - while 1: - line = f.readline() - if line == delimiter: - break - if not line: - sys.stderr.write('Unexpected EOF in message\n') - sts = 1 - break - if line[:5] == 'From ': - line = '>' + line - print(line, end=' ') - # Print trailing newline - print() - return sts - -if __name__ == "__main__": - main() diff --git a/Demo/scripts/morse.py b/Demo/scripts/morse.py deleted file mode 100755 index c2f408f..0000000 --- a/Demo/scripts/morse.py +++ /dev/null @@ -1,128 +0,0 @@ -#! /usr/bin/env python3 - -# DAH should be three DOTs. -# Space between DOTs and DAHs should be one DOT. -# Space between two letters should be one DAH. -# Space between two words should be DOT DAH DAH. - -import sys, math, aifc -from contextlib import closing - -DOT = 30 -DAH = 3 * DOT -OCTAVE = 2 # 1 == 441 Hz, 2 == 882 Hz, ... - -morsetab = { - 'A': '.-', 'a': '.-', - 'B': '-...', 'b': '-...', - 'C': '-.-.', 'c': '-.-.', - 'D': '-..', 'd': '-..', - 'E': '.', 'e': '.', - 'F': '..-.', 'f': '..-.', - 'G': '--.', 'g': '--.', - 'H': '....', 'h': '....', - 'I': '..', 'i': '..', - 'J': '.---', 'j': '.---', - 'K': '-.-', 'k': '-.-', - 'L': '.-..', 'l': '.-..', - 'M': '--', 'm': '--', - 'N': '-.', 'n': '-.', - 'O': '---', 'o': '---', - 'P': '.--.', 'p': '.--.', - 'Q': '--.-', 'q': '--.-', - 'R': '.-.', 'r': '.-.', - 'S': '...', 's': '...', - 'T': '-', 't': '-', - 'U': '..-', 'u': '..-', - 'V': '...-', 'v': '...-', - 'W': '.--', 'w': '.--', - 'X': '-..-', 'x': '-..-', - 'Y': '-.--', 'y': '-.--', - 'Z': '--..', 'z': '--..', - '0': '-----', ',': '--..--', - '1': '.----', '.': '.-.-.-', - '2': '..---', '?': '..--..', - '3': '...--', ';': '-.-.-.', - '4': '....-', ':': '---...', - '5': '.....', "'": '.----.', - '6': '-....', '-': '-....-', - '7': '--...', '/': '-..-.', - '8': '---..', '(': '-.--.-', - '9': '----.', ')': '-.--.-', - ' ': ' ', '_': '..--.-', -} - -nowave = b'\0' * 200 - -# If we play at 44.1 kHz (which we do), then if we produce one sine -# wave in 100 samples, we get a tone of 441 Hz. If we produce two -# sine waves in these 100 samples, we get a tone of 882 Hz. 882 Hz -# appears to be a nice one for playing morse code. -def mkwave(octave): - sinewave = bytearray() - for i in range(100): - val = int(math.sin(math.pi * i * octave / 50.0) * 30000) - sinewave.extend([(val >> 8) & 255, val & 255]) - return bytes(sinewave) - -defaultwave = mkwave(OCTAVE) - -def main(): - import getopt - try: - opts, args = getopt.getopt(sys.argv[1:], 'o:p:') - except getopt.error: - sys.stderr.write('Usage ' + sys.argv[0] + - ' [ -o outfile ] [ -p octave ] [ words ] ...\n') - sys.exit(1) - wave = defaultwave - outfile = 'morse.aifc' - for o, a in opts: - if o == '-o': - outfile = a - if o == '-p': - wave = mkwave(int(a)) - with closing(aifc.open(outfile, 'w')) as fp: - fp.setframerate(44100) - fp.setsampwidth(2) - fp.setnchannels(1) - if args: - source = [' '.join(args)] - else: - source = iter(sys.stdin.readline, '') - for line in source: - mline = morse(line) - play(mline, fp, wave) - -# Convert a string to morse code with \001 between the characters in -# the string. -def morse(line): - res = '' - for c in line: - try: - res += morsetab[c] + '\001' - except KeyError: - pass - return res - -# Play a line of morse code. -def play(line, fp, wave): - for c in line: - if c == '.': - sine(fp, DOT, wave) - elif c == '-': - sine(fp, DAH, wave) - else: # space - pause(fp, DAH + DOT) - pause(fp, DOT) - -def sine(fp, length, wave): - for i in range(length): - fp.writeframesraw(wave) - -def pause(fp, length): - for i in range(length): - fp.writeframesraw(nowave) - -if __name__ == '__main__': - main() diff --git a/Demo/scripts/newslist.doc b/Demo/scripts/newslist.doc deleted file mode 100755 index 87fd9ba..0000000 --- a/Demo/scripts/newslist.doc +++ /dev/null @@ -1,59 +0,0 @@ - NEWSLIST - ======== - A program to assist HTTP browsing of newsgroups - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -WWW browsers such as NCSA Mosaic allow the user to read newsgroup -articles by specifying the group name in a URL eg 'news:comp.answers'. - -To browse through many groups, though, (and there are several thousand -of them) you really need a page or pages containing links to all the -groups. There are some good ones out there, for example, - - http://info.cern.ch/hypertext/DataSources/News/Groups/Overview.html - -is the standard one at CERN, but it only shows the groups available there, -which may be rather different from those available on your machine. - -Newslist is a program which creates a hierarchy of pages for you based -on the groups available from YOUR server. It is written in python - a -splendid interpreted object-oriented language which I suggest you get -right now from the directory /pub/python at ftp.cwi.nl, if you haven't -already got it. - -You should be able to see some sample output by looking at: - http://pelican.cl.cam.ac.uk/newspage/root.html - -Descriptions of newsgroups can be added from a file with one group -per line. eg: - - alt.foo Articles about foo - comp.bar Programming in 'bar' and related languages - -A suitable list detailing most groups can be found at ftp.uu.net in -/uunet-info/newsgroups.gz. - -Make sure you read the information at the beginning of the program source and -configure the variables before running. - -In addition to python, you need: - - An NNTP-based news feed. - A directory in which to put the pages. - -The programming is not very beautiful, but it works! It comes with no -warranty, express or implied, but with the hope that some others may -find it useful. - -Comments, improvements & suggestions welcomed. -Quentin Stafford-Fraser - - ---------------------------------------------------------------------- - Quentin Stafford-Fraser - http://pelican.cl.cam.ac.uk/people/qs101/me.html - - Cambridge University Computer Lab Rank Xerox Cambridge EuroPARC - qs101@cl.cam.ac.uk fraser@europarc.xerox.com - Tel: +44 223 334411 Tel: +44 223 341521 - Fax: +44 223 334679 Fax: +44 223 341510 - ---------------------------------------------------------------------- diff --git a/Demo/scripts/newslist.py b/Demo/scripts/newslist.py deleted file mode 100755 index b345f2ef..0000000 --- a/Demo/scripts/newslist.py +++ /dev/null @@ -1,361 +0,0 @@ -#! /usr/bin/env python3 -####################################################################### -# Newslist $Revision$ -# -# Syntax: -# newslist [ -a ] -# -# This is a program to create a directory full of HTML pages -# which between them contain links to all the newsgroups available -# on your server. -# -# The -a option causes a complete list of all groups to be read from -# the server rather than just the ones which have appeared since last -# execution. This recreates the local list from scratch. Use this on -# the first invocation of the program, and from time to time thereafter. -# When new groups are first created they may appear on your server as -# empty groups. By default, empty groups are ignored by the -a option. -# However, these new groups will not be created again, and so will not -# appear in the server's list of 'new groups' at a later date. Hence it -# won't appear until you do a '-a' after some articles have appeared. -# -# I should really keep a list of ignored empty groups and re-check them -# for articles on every run, but I haven't got around to it yet. -# -# This assumes an NNTP news feed. -# -# Feel free to copy, distribute and modify this code for -# non-commercial use. If you make any useful modifications, let me -# know! -# -# (c) Quentin Stafford-Fraser 1994 -# fraser@europarc.xerox.com qs101@cl.cam.ac.uk -# # -####################################################################### -import sys, nntplib, marshal, time, os - -####################################################################### -# Check these variables before running! # - -# Top directory. -# Filenames which don't start with / are taken as being relative to this. -topdir = os.path.expanduser('~/newspage') - -# The name of your NNTP host -# eg. -# newshost = 'nntp-serv.cl.cam.ac.uk' -# or use following to get the name from the NNTPSERVER environment -# variable: -# newshost = os.environ['NNTPSERVER'] -newshost = 'news.example.com' - -# The filename for a local cache of the newsgroup list -treefile = 'grouptree' - -# The filename for descriptions of newsgroups -# I found a suitable one at ftp.uu.net in /uunet-info/newgroups.gz -# You can set this to '' if you don't wish to use one. -descfile = 'newsgroups' - -# The directory in which HTML pages should be created -# eg. -# pagedir = '/usr/local/lib/html/newspage' -# pagedir = 'pages' -pagedir = topdir - -# The html prefix which will refer to this directory -# eg. -# httppref = '/newspage/', -# or leave blank for relative links between pages: (Recommended) -# httppref = '' -httppref = '' - -# The name of the 'root' news page in this directory. -# A .html suffix will be added. -rootpage = 'root' - -# Set skipempty to 0 if you wish to see links to empty groups as well. -# Only affects the -a option. -skipempty = 1 - -# pagelinkicon can contain html to put an icon after links to -# further pages. This helps to make important links stand out. -# Set to '' if not wanted, or '...' is quite a good one. -pagelinkicon = '... <img src="http://pelican.cl.cam.ac.uk/icons/page.xbm"> ' - -# --------------------------------------------------------------------- -# Less important personal preferences: - -# Sublistsize controls the maximum number of items the will appear as -# an indented sub-list before the whole thing is moved onto a different -# page. The smaller this is, the more pages you will have, but the -# shorter each will be. -sublistsize = 4 - -# That should be all. # -####################################################################### - -for dir in os.curdir, os.environ['HOME']: - rcfile = os.path.join(dir, '.newslistrc.py') - if os.path.exists(rcfile): - print(rcfile) - exec(open(rcfile).read()) - break - -from nntplib import NNTP -from stat import * - -rcsrev = '$Revision$' -rcsrev = ' '.join([s for s in rcsrev.split() if '$' not in s]) -desc = {} - -# Make (possibly) relative filenames into absolute ones -treefile = os.path.join(topdir,treefile) -descfile = os.path.join(topdir,descfile) -page = os.path.join(topdir,pagedir) - -# First the bits for creating trees --------------------------- - -# Addtotree creates/augments a tree from a list of group names -def addtotree(tree, groups): - print('Updating tree...') - for i in groups: - parts = i.split('.') - makeleaf(tree, parts) - -# Makeleaf makes a leaf and the branch leading to it if necessary -def makeleaf(tree,path): - j = path[0] - l = len(path) - - if j not in tree: - tree[j] = {} - if l == 1: - tree[j]['.'] = '.' - if l > 1: - makeleaf(tree[j],path[1:]) - -# Then the bits for outputting trees as pages ---------------- - -# Createpage creates an HTML file named <root>.html containing links -# to those groups beginning with <root>. - -def createpage(root, tree, p): - filename = os.path.join(pagedir, root+'.html') - if root == rootpage: - detail = '' - else: - detail = ' under ' + root - with open(filename, 'w') as f: - # f.write('Content-Type: text/html\n') - f.write('<html>\n<head>\n') - f.write('<title>Newsgroups available%s</title>\n' % detail) - f.write('</head>\n<body>\n') - f.write('<h1>Newsgroups available%s</h1>\n' % detail) - f.write('<a href="%s%s.html">Back to top level</a><p>\n' % - (httppref, rootpage)) - printtree(f, tree, 0, p) - f.write('\n<p>') - f.write("<i>This page automatically created by 'newslist' v. %s." % - rcsrev) - f.write(time.ctime(time.time()) + '</i>\n') - f.write('</body>\n</html>\n') - -# Printtree prints the groups as a bulleted list. Groups with -# more than <sublistsize> subgroups will be put on a separate page. -# Other sets of subgroups are just indented. - -def printtree(f, tree, indent, p): - l = len(tree) - - if l > sublistsize and indent > 0: - # Create a new page and a link to it - f.write('<li><b><a href="%s%s.html">' % (httppref, p[1:])) - f.write(p[1:] + '.*') - f.write('</a></b>%s\n' % pagelinkicon) - createpage(p[1:], tree, p) - return - - kl = sorted(tree.keys()) - - if l > 1: - if indent > 0: - # Create a sub-list - f.write('<li>%s\n<ul>' % p[1:]) - else: - # Create a main list - f.write('<ul>') - indent = indent + 1 - - for i in kl: - if i == '.': - # Output a newsgroup - f.write('<li><a href="news:%s">%s</a> ' % (p[1:], p[1:])) - if p[1:] in desc: - f.write(' <i>%s</i>\n' % desc[p[1:]]) - else: - f.write('\n') - else: - # Output a hierarchy - printtree(f, tree[i], indent, p+'.'+i) - - if l > 1: - f.write('\n</ul>') - -# Reading descriptions file --------------------------------------- - -# This returns a dict mapping group name to its description - -def readdesc(descfile): - global desc - desc = {} - - if descfile == '': - return - - try: - with open(descfile, 'r') as d: - print('Reading descriptions...') - for l in d: - bits = l.split() - try: - grp = bits[0] - dsc = ' '.join(bits[1:]) - if len(dsc) > 1: - desc[grp] = dsc - except IndexError: - pass - except IOError: - print('Failed to open description file ' + descfile) - return - -# Check that ouput directory exists, ------------------------------ -# and offer to create it if not - -def checkopdir(pagedir): - if not os.path.isdir(pagedir): - print('Directory %s does not exist.' % pagedir) - print('Shall I create it for you? (y/n)') - if sys.stdin.readline()[0] == 'y': - try: - os.mkdir(pagedir, 0o777) - except: - print('Sorry - failed!') - sys.exit(1) - else: - print('OK. Exiting.') - sys.exit(1) - -# Read and write current local tree ---------------------------------- - -def readlocallist(treefile): - print('Reading current local group list...') - tree = {} - try: - treetime = time.localtime(os.stat(treefile)[ST_MTIME]) - except: - print('\n*** Failed to open local group cache '+treefile) - print('If this is the first time you have run newslist, then') - print('use the -a option to create it.') - sys.exit(1) - treedate = '%02d%02d%02d' % (treetime[0] % 100, treetime[1], treetime[2]) - try: - with open(treefile, 'rb') as dump: - tree = marshal.load(dump) - except IOError: - print('Cannot open local group list ' + treefile) - return (tree, treedate) - -def writelocallist(treefile, tree): - try: - with open(treefile, 'wb') as dump: - groups = marshal.dump(tree, dump) - print('Saved list to %s\n' % treefile) - except: - print('Sorry - failed to write to local group cache', treefile) - print('Does it (or its directory) have the correct permissions?') - sys.exit(1) - -# Return list of all groups on server ----------------------------- - -def getallgroups(server): - print('Getting list of all groups...') - treedate = '010101' - info = server.list()[1] - groups = [] - print('Processing...') - if skipempty: - print('\nIgnoring following empty groups:') - for i in info: - grpname = i[0].split()[0] - if skipempty and int(i[1]) < int(i[2]): - print(grpname.decode() + ' ', end=' ') - else: - groups.append(grpname.decode()) - print('\n') - if skipempty: - print('(End of empty groups)') - return groups - -# Return list of new groups on server ----------------------------- - -def getnewgroups(server, treedate): - print('Getting list of new groups since start of %s...' % treedate, end=' ') - info = server.newgroups(treedate, '000001')[1] - print('got %d.' % len(info)) - print('Processing...', end=' ') - groups = [] - for i in info: - grpname = i.split()[0] - groups.append(grpname.decode()) - print('Done') - return groups - -# Now the main program -------------------------------------------- - -def main(): - tree = {} - - # Check that the output directory exists - checkopdir(pagedir) - - try: - print('Connecting to %s...' % newshost) - if sys.version[0] == '0': - s = NNTP.init(newshost) - else: - s = NNTP(newshost) - connected = True - except (nntplib.error_temp, nntplib.error_perm) as x: - print('Error connecting to host:', x) - print('I\'ll try to use just the local list.') - connected = False - - # If -a is specified, read the full list of groups from server - if connected and len(sys.argv) > 1 and sys.argv[1] == '-a': - groups = getallgroups(s) - - # Otherwise just read the local file and then add - # groups created since local file last modified. - else: - - (tree, treedate) = readlocallist(treefile) - if connected: - groups = getnewgroups(s, treedate) - - if connected: - addtotree(tree, groups) - writelocallist(treefile,tree) - - # Read group descriptions - readdesc(descfile) - - print('Creating pages...') - createpage(rootpage, tree, '') - print('Done') - -if __name__ == "__main__": - main() - -# That's all folks -###################################################################### diff --git a/Demo/scripts/pi.py b/Demo/scripts/pi.py deleted file mode 100755 index 248f81f..0000000 --- a/Demo/scripts/pi.py +++ /dev/null @@ -1,33 +0,0 @@ -#! /usr/bin/env python3 - -# Print digits of pi forever. -# -# The algorithm, using Python's 'long' integers ("bignums"), works -# with continued fractions, and was conceived by Lambert Meertens. -# -# See also the ABC Programmer's Handbook, by Geurts, Meertens & Pemberton, -# published by Prentice-Hall (UK) Ltd., 1990. - -import sys - -def main(): - k, a, b, a1, b1 = 2, 4, 1, 12, 4 - while True: - # Next approximation - p, q, k = k*k, 2*k+1, k+1 - a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1 - # Print common digits - d, d1 = a//b, a1//b1 - while d == d1: - output(d) - a, a1 = 10*(a%b), 10*(a1%b1) - d, d1 = a//b, a1//b1 - -def output(d): - # Use write() to avoid spaces between the digits - sys.stdout.write(str(d)) - # Flush so the output is seen immediately - sys.stdout.flush() - -if __name__ == "__main__": - main() diff --git a/Demo/scripts/pp.py b/Demo/scripts/pp.py deleted file mode 100755 index 87b9d8f..0000000 --- a/Demo/scripts/pp.py +++ /dev/null @@ -1,125 +0,0 @@ -#! /usr/bin/env python3 - -# Emulate some Perl command line options. -# Usage: pp [-a] [-c] [-d] [-e scriptline] [-F fieldsep] [-n] [-p] [file] ... -# Where the options mean the following: -# -a : together with -n or -p, splits each line into list F -# -c : check syntax only, do not execute any code -# -d : run the script under the debugger, pdb -# -e scriptline : gives one line of the Python script; may be repeated -# -F fieldsep : sets the field separator for the -a option [not in Perl] -# -n : runs the script for each line of input -# -p : prints the line after the script has run -# When no script lines have been passed, the first file argument -# contains the script. With -n or -p, the remaining arguments are -# read as input to the script, line by line. If a file is '-' -# or missing, standard input is read. - -# XXX To do: -# - add -i extension option (change files in place) -# - make a single loop over the files and lines (changes effect of 'break')? -# - add an option to specify the record separator -# - except for -n/-p, run directly from the file if at all possible - -import sys -import getopt - -FS = '' -SCRIPT = [] -AFLAG = 0 -CFLAG = 0 -DFLAG = 0 -NFLAG = 0 -PFLAG = 0 - -try: - optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np') -except getopt.error as msg: - sys.stderr.write('%s: %s\n' % (sys.argv[0], msg)) - sys.exit(2) - -for option, optarg in optlist: - if option == '-a': - AFLAG = 1 - elif option == '-c': - CFLAG = 1 - elif option == '-d': - DFLAG = 1 - elif option == '-e': - for line in optarg.split('\n'): - SCRIPT.append(line) - elif option == '-F': - FS = optarg - elif option == '-n': - NFLAG = 1 - PFLAG = 0 - elif option == '-p': - NFLAG = 1 - PFLAG = 1 - else: - print(option, 'not recognized???') - -if not ARGS: ARGS.append('-') - -if not SCRIPT: - if ARGS[0] == '-': - fp = sys.stdin - else: - fp = open(ARGS[0], 'r') - while 1: - line = fp.readline() - if not line: break - SCRIPT.append(line[:-1]) - del fp - del ARGS[0] - if not ARGS: ARGS.append('-') - -if CFLAG: - prologue = ['if 0:'] - epilogue = [] -elif NFLAG: - # Note that it is on purpose that AFLAG and PFLAG are - # tested dynamically each time through the loop - prologue = [ - 'LINECOUNT = 0', - 'for FILE in ARGS:', - ' \tif FILE == \'-\':', - ' \t \tFP = sys.stdin', - ' \telse:', - ' \t \tFP = open(FILE, \'r\')', - ' \tLINENO = 0', - ' \twhile 1:', - ' \t \tLINE = FP.readline()', - ' \t \tif not LINE: break', - ' \t \tLINENO = LINENO + 1', - ' \t \tLINECOUNT = LINECOUNT + 1', - ' \t \tL = LINE[:-1]', - ' \t \taflag = AFLAG', - ' \t \tif aflag:', - ' \t \t \tif FS: F = L.split(FS)', - ' \t \t \telse: F = L.split()' - ] - epilogue = [ - ' \t \tif not PFLAG: continue', - ' \t \tif aflag:', - ' \t \t \tif FS: print(FS.join(F))', - ' \t \t \telse: print(\' \'.join(F))', - ' \t \telse: print(L)', - ] -else: - prologue = ['if 1:'] - epilogue = [] - -# Note that we indent using tabs only, so that any indentation style -# used in 'command' will come out right after re-indentation. - -program = '\n'.join(prologue) + '\n' -for line in SCRIPT: - program += ' \t \t' + line + '\n' -program += '\n'.join(epilogue) + '\n' - -if DFLAG: - import pdb - pdb.run(program) -else: - exec(program) diff --git a/Demo/scripts/primes.py b/Demo/scripts/primes.py deleted file mode 100755 index c994623..0000000 --- a/Demo/scripts/primes.py +++ /dev/null @@ -1,31 +0,0 @@ -#! /usr/bin/env python3 - -# Print prime numbers in a given range - -def primes(min, max): - if max >= 2 >= min: - print(2) - primes = [2] - i = 3 - while i <= max: - for p in primes: - if i % p == 0 or p*p > i: - break - if i % p != 0: - primes.append(i) - if i >= min: - print(i) - i += 2 - -def main(): - import sys - min, max = 2, 0x7fffffff - if sys.argv[1:]: - min = int(sys.argv[1]) - if sys.argv[2:]: - max = int(sys.argv[2]) - primes(min, max) - - -if __name__ == "__main__": - main() diff --git a/Demo/scripts/script.py b/Demo/scripts/script.py deleted file mode 100755 index a815a35..0000000 --- a/Demo/scripts/script.py +++ /dev/null @@ -1,42 +0,0 @@ -#! /usr/bin/env python3 - -# script.py -- Make typescript of terminal session. -# Usage: -# -a Append to typescript. -# -p Use Python as shell. -# Author: Steen Lumholt. - - -import os, time, sys, getopt -import pty - -def read(fd): - data = os.read(fd, 1024) - script.write(data) - return data - -shell = 'sh' -filename = 'typescript' -mode = 'wb' -if 'SHELL' in os.environ: - shell = os.environ['SHELL'] - -try: - opts, args = getopt.getopt(sys.argv[1:], 'ap') -except getopt.error as msg: - print('%s: %s' % (sys.argv[0], msg)) - sys.exit(2) - -for o, a in opts: - if o == '-a': - mode = 'ab' - elif o == '-p': - shell = 'python' - -script = open(filename, mode) - -sys.stdout.write('Script started, file is %s\n' % filename) -script.write(('Script started on %s\n' % time.ctime(time.time())).encode()) -pty.spawn(shell, read) -script.write(('Script done on %s\n' % time.ctime(time.time())).encode()) -sys.stdout.write('Script done, file is %s\n' % filename) diff --git a/Demo/scripts/update.py b/Demo/scripts/update.py deleted file mode 100755 index 49c09d8..0000000 --- a/Demo/scripts/update.py +++ /dev/null @@ -1,92 +0,0 @@ -#! /usr/bin/env python3 - -# Update a bunch of files according to a script. -# The input file contains lines of the form <filename>:<lineno>:<text>, -# meaning that the given line of the given file is to be replaced -# by the given text. This is useful for performing global substitutions -# on grep output: - -import os -import sys -import re - -pat = '^([^: \t\n]+):([1-9][0-9]*):' -prog = re.compile(pat) - -class FileObj: - def __init__(self, filename): - self.filename = filename - self.changed = 0 - try: - self.lines = open(filename, 'r').readlines() - except IOError as msg: - print('*** Can\'t open "%s":' % filename, msg) - self.lines = None - return - print('diffing', self.filename) - - def finish(self): - if not self.changed: - print('no changes to', self.filename) - return - try: - os.rename(self.filename, self.filename + '~') - fp = open(self.filename, 'w') - except (os.error, IOError) as msg: - print('*** Can\'t rewrite "%s":' % self.filename, msg) - return - print('writing', self.filename) - for line in self.lines: - fp.write(line) - fp.close() - self.changed = 0 - - def process(self, lineno, rest): - if self.lines is None: - print('(not processed): %s:%s:%s' % ( - self.filename, lineno, rest), end=' ') - return - i = eval(lineno) - 1 - if not 0 <= i < len(self.lines): - print('*** Line number out of range: %s:%s:%s' % ( - self.filename, lineno, rest), end=' ') - return - if self.lines[i] == rest: - print('(no change): %s:%s:%s' % ( - self.filename, lineno, rest), end=' ') - return - if not self.changed: - self.changed = 1 - print('%sc%s' % (lineno, lineno)) - print('<', self.lines[i], end=' ') - print('---') - self.lines[i] = rest - print('>', self.lines[i], end=' ') - -def main(): - if sys.argv[1:]: - try: - fp = open(sys.argv[1], 'r') - except IOError as msg: - print('Can\'t open "%s":' % sys.argv[1], msg) - sys.exit(1) - else: - fp = sys.stdin - curfile = None - while 1: - line = fp.readline() - if not line: - if curfile: curfile.finish() - break - n = prog.match(line) - if n < 0: - print('Funny line:', line, end=' ') - continue - filename, lineno = prog.group(1, 2) - if not curfile or filename != curfile.filename: - if curfile: curfile.finish() - curfile = FileObj(filename) - curfile.process(lineno, line[n:]) - -if __name__ == "__main__": - main() |