Modernize all around (dont ask me how useful that script is nowadays...)

This commit is contained in:
Georg Brandl 2009-10-10 22:26:45 +00:00
parent ffc87d5c8c
commit 723fb3adc8
1 changed files with 67 additions and 71 deletions

View File

@ -32,22 +32,22 @@
# fraser@europarc.xerox.com qs101@cl.cam.ac.uk # fraser@europarc.xerox.com qs101@cl.cam.ac.uk
# # # #
####################################################################### #######################################################################
import sys,nntplib, string, marshal, time, os, posix, string import sys, nntplib, marshal, time, os
####################################################################### #######################################################################
# Check these variables before running! # # Check these variables before running! #
# Top directory. # Top directory.
# Filenames which don't start with / are taken as being relative to this. # Filenames which don't start with / are taken as being relative to this.
topdir='/anfs/qsbigdisc/web/html/newspage' topdir = os.path.expanduser('~/newspage')
# The name of your NNTP host # The name of your NNTP host
# eg. # eg.
# newshost = 'nntp-serv.cl.cam.ac.uk' # newshost = 'nntp-serv.cl.cam.ac.uk'
# or use following to get the name from the NNTPSERVER environment # or use following to get the name from the NNTPSERVER environment
# variable: # variable:
# newshost = posix.environ['NNTPSERVER'] # newshost = os.environ['NNTPSERVER']
newshost = 'nntp-serv.cl.cam.ac.uk' newshost = 'news.example.com'
# The filename for a local cache of the newsgroup list # The filename for a local cache of the newsgroup list
treefile = 'grouptree' treefile = 'grouptree'
@ -81,7 +81,7 @@ skipempty = 1
# pagelinkicon can contain html to put an icon after links to # pagelinkicon can contain html to put an icon after links to
# further pages. This helps to make important links stand out. # further pages. This helps to make important links stand out.
# Set to '' if not wanted, or '...' is quite a good one. # Set to '' if not wanted, or '...' is quite a good one.
pagelinkicon='... <img src="http://pelican.cl.cam.ac.uk/icons/page.xbm"> ' pagelinkicon = '... <img src="http://pelican.cl.cam.ac.uk/icons/page.xbm"> '
# --------------------------------------------------------------------- # ---------------------------------------------------------------------
# Less important personal preferences: # Less important personal preferences:
@ -106,7 +106,7 @@ from nntplib import NNTP
from stat import * from stat import *
rcsrev = '$Revision$' rcsrev = '$Revision$'
rcsrev = string.join(filter(lambda s: '$' not in s, string.split(rcsrev))) rcsrev = ' '.join(filter(lambda s: '$' not in s, rcsrev.split()))
desc = {} desc = {}
# Make (possibly) relative filenames into absolute ones # Make (possibly) relative filenames into absolute ones
@ -120,7 +120,7 @@ page = os.path.join(topdir,pagedir)
def addtotree(tree, groups): def addtotree(tree, groups):
print 'Updating tree...' print 'Updating tree...'
for i in groups: for i in groups:
parts = string.splitfields(i,'.') parts = i.split('.')
makeleaf(tree, parts) makeleaf(tree, parts)
# Makeleaf makes a leaf and the branch leading to it if necessary # Makeleaf makes a leaf and the branch leading to it if necessary
@ -141,34 +141,38 @@ def makeleaf(tree,path):
# to those groups beginning with <root>. # to those groups beginning with <root>.
def createpage(root, tree, p): def createpage(root, tree, p):
filename = os.path.join(pagedir,root+'.html') filename = os.path.join(pagedir, root+'.html')
if root == rootpage: if root == rootpage:
detail = '' detail = ''
else: else:
detail = ' under ' + root detail = ' under ' + root
f = open(filename,'w') with open(filename, 'w') as f:
# f.write('Content-Type: text/html\n') # f.write('Content-Type: text/html\n')
f.write('<TITLE>Newsgroups available' + detail + '</TITLE>\n') f.write('<html>\n<head>\n')
f.write('<H1>Newsgroups available' + detail +'</H1>\n') f.write('<title>Newsgroups available%s</title>\n' % detail)
f.write('<A HREF="'+httppref+rootpage+'.html">Back to top level</A><P>\n') f.write('</head>\n<body>\n')
printtree(f,tree,0,p) f.write('<h1>Newsgroups available%s</h1>\n' % detail)
f.write('<I>This page automatically created by \'newslist\' v. '+rcsrev+'.') f.write('<a href="%s%s.html">Back to top level</a><p>\n' %
f.write(time.ctime(time.time()) + '</I><P>') (httppref, rootpage))
f.close() 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 # Printtree prints the groups as a bulleted list. Groups with
# more than <sublistsize> subgroups will be put on a separate page. # more than <sublistsize> subgroups will be put on a separate page.
# Other sets of subgroups are just indented. # Other sets of subgroups are just indented.
def printtree(f, tree, indent, p): def printtree(f, tree, indent, p):
global desc
l = len(tree) l = len(tree)
if l > sublistsize and indent>0: if l > sublistsize and indent > 0:
# Create a new page and a link to it # Create a new page and a link to it
f.write('<LI><B><A HREF="'+httppref+p[1:]+'.html">') f.write('<li><b><a href="%s%s.html">' % (httppref, p[1:]))
f.write(p[1:]+'.*') f.write(p[1:] + '.*')
f.write('</A></B>'+pagelinkicon+'\n') f.write('</a></b>%s\n' % pagelinkicon)
createpage(p[1:], tree, p) createpage(p[1:], tree, p)
return return
@ -178,67 +182,64 @@ def printtree(f, tree, indent, p):
kl.sort() kl.sort()
if indent > 0: if indent > 0:
# Create a sub-list # Create a sub-list
f.write('<LI>'+p[1:]+'\n<UL>') f.write('<li>%s\n<ul>' % p[1:])
else: else:
# Create a main list # Create a main list
f.write('<UL>') f.write('<ul>')
indent = indent + 1 indent = indent + 1
for i in kl: for i in kl:
if i == '.': if i == '.':
# Output a newsgroup # Output a newsgroup
f.write('<LI><A HREF="news:' + p[1:] + '">'+ p[1:] + '</A> ') f.write('<li><a href="news:%s">%s</a> ' % (p[1:], p[1:]))
if desc.has_key(p[1:]): if p[1:] in desc:
f.write(' <I>'+desc[p[1:]]+'</I>\n') f.write(' <i>%s</i>\n' % desc[p[1:]])
else: else:
f.write('\n') f.write('\n')
else: else:
# Output a hierarchy # Output a hierarchy
printtree(f,tree[i], indent, p+'.'+i) printtree(f, tree[i], indent, p+'.'+i)
if l > 1: if l > 1:
f.write('\n</UL>') f.write('\n</ul>')
# Reading descriptions file --------------------------------------- # Reading descriptions file ---------------------------------------
# This returns an array mapping group name to its description # This returns a dict mapping group name to its description
def readdesc(descfile): def readdesc(descfile):
global desc global desc
desc = {} desc = {}
if descfile == '': if descfile == '':
return return
try: try:
d = open(descfile, 'r') with open(descfile, 'r') as d:
print 'Reading descriptions...' print 'Reading descriptions...'
except (IOError): 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 print 'Failed to open description file ' + descfile
return return
l = d.readline()
while l != '':
bits = string.split(l)
try:
grp = bits[0]
dsc = string.join(bits[1:])
if len(dsc)>1:
desc[grp] = dsc
except (IndexError):
pass
l = d.readline()
# Check that ouput directory exists, ------------------------------ # Check that ouput directory exists, ------------------------------
# and offer to create it if not # and offer to create it if not
def checkopdir(pagedir): def checkopdir(pagedir):
if not os.path.isdir(pagedir): if not os.path.isdir(pagedir):
print 'Directory '+pagedir+' does not exist.' print 'Directory %s does not exist.' % pagedir
print 'Shall I create it for you? (y/n)' print 'Shall I create it for you? (y/n)'
if sys.stdin.readline()[0] == 'y': if sys.stdin.readline()[0] == 'y':
try: try:
os.mkdir(pagedir,0777) os.mkdir(pagedir, 0777)
except: except:
print 'Sorry - failed!' print 'Sorry - failed!'
sys.exit(1) sys.exit(1)
@ -258,23 +259,21 @@ def readlocallist(treefile):
print 'If this is the first time you have run newslist, then' print 'If this is the first time you have run newslist, then'
print 'use the -a option to create it.' print 'use the -a option to create it.'
sys.exit(1) sys.exit(1)
treedate = '%02d%02d%02d' % (treetime[0] % 100 ,treetime[1], treetime[2]) treedate = '%02d%02d%02d' % (treetime[0] % 100, treetime[1], treetime[2])
try: try:
dump = open(treefile,'r') with open(treefile, 'rb') as dump:
tree = marshal.load(dump) tree = marshal.load(dump)
dump.close() except IOError:
except (IOError):
print 'Cannot open local group list ' + treefile print 'Cannot open local group list ' + treefile
return (tree, treedate) return (tree, treedate)
def writelocallist(treefile, tree): def writelocallist(treefile, tree):
try: try:
dump = open(treefile,'w') with open(treefile, 'wb') as dump:
groups = marshal.dump(tree,dump) groups = marshal.dump(tree, dump)
dump.close() print 'Saved list to %s\n' % treefile
print 'Saved list to '+treefile+'\n'
except: except:
print 'Sorry - failed to write to local group cache '+treefile print 'Sorry - failed to write to local group cache', treefile
print 'Does it (or its directory) have the correct permissions?' print 'Does it (or its directory) have the correct permissions?'
sys.exit(1) sys.exit(1)
@ -282,16 +281,16 @@ def writelocallist(treefile, tree):
def getallgroups(server): def getallgroups(server):
print 'Getting list of all groups...' print 'Getting list of all groups...'
treedate='010101' treedate = '010101'
info = server.list()[1] info = server.list()[1]
groups = [] groups = []
print 'Processing...' print 'Processing...'
if skipempty: if skipempty:
print '\nIgnoring following empty groups:' print '\nIgnoring following empty groups:'
for i in info: for i in info:
grpname = string.split(i[0])[0] grpname = i[0].split()[0]
if skipempty and string.atoi(i[1]) < string.atoi(i[2]): if skipempty and int(i[1]) < int(i[2]):
print grpname+' ', print grpname + ' ',
else: else:
groups.append(grpname) groups.append(grpname)
print '\n' print '\n'
@ -302,13 +301,13 @@ def getallgroups(server):
# Return list of new groups on server ----------------------------- # Return list of new groups on server -----------------------------
def getnewgroups(server, treedate): def getnewgroups(server, treedate):
print 'Getting list of new groups since start of '+treedate+'...', print 'Getting list of new groups since start of %s...' % treedate,
info = server.newgroups(treedate,'000001')[1] info = server.newgroups(treedate, '000001')[1]
print 'got %d.' % len(info) print 'got %d.' % len(info)
print 'Processing...', print 'Processing...',
groups = [] groups = []
for i in info: for i in info:
grpname = string.split(i)[0] grpname = i.split()[0]
groups.append(grpname) groups.append(grpname)
print 'Done' print 'Done'
return groups return groups
@ -316,28 +315,25 @@ def getnewgroups(server, treedate):
# Now the main program -------------------------------------------- # Now the main program --------------------------------------------
def main(): def main():
global desc tree = {}
tree={}
# Check that the output directory exists # Check that the output directory exists
checkopdir(pagedir) checkopdir(pagedir)
try: try:
print 'Connecting to '+newshost+'...' print 'Connecting to %s...' % newshost
if sys.version[0] == '0': if sys.version[0] == '0':
s = NNTP.init(newshost) s = NNTP.init(newshost)
else: else:
s = NNTP(newshost) s = NNTP(newshost)
connected = 1 connected = True
except (nntplib.error_temp, nntplib.error_perm), x: except (nntplib.error_temp, nntplib.error_perm), x:
print 'Error connecting to host:', x print 'Error connecting to host:', x
print 'I\'ll try to use just the local list.' print 'I\'ll try to use just the local list.'
connected = 0 connected = False
# If -a is specified, read the full list of groups from server # If -a is specified, read the full list of groups from server
if connected and len(sys.argv) > 1 and sys.argv[1] == '-a': if connected and len(sys.argv) > 1 and sys.argv[1] == '-a':
groups = getallgroups(s) groups = getallgroups(s)
# Otherwise just read the local file and then add # Otherwise just read the local file and then add