Suggestions on writing a sh <--> python Howto/Tutorial


R

Robert Laing

I've been tinkering around learning traditional Unix shell programming
and python at the same time. I set myself the following exercise which
I found quite educational. I first wrote a shell CGI script to read
the man pages on my web hosting service's computer via a browser like
so:

http://<mydomain>/rtfm.cgi?page=<n>&man=<cmd>

Where page=<n> is optional since one does not often need it. The shell
script that did the trick looks so:

#!/bin/sh
MAN=$(echo $QUERY_STRING | sed -n "s/^.*man=\([^&]*\).*$/\1/p")
PAGE=$(echo $QUERY_STRING | sed -n "s/^.*page=\([^&]*\).*$/\1/p")
echo "Content-type: text/html; charset=utf-8"
echo ""
gunzip < $(man --path $PAGE $MAN) | groff -Thtml -mandoc

That last line with the < redirect and pipe to groff is not purely
illustrative since while the Linux distro on my laptap has a version
of man that understands -Thtml, the one on my hosting company's server
does not (making the above quite a valuable script).

Now I managed to write a rtfm.py script that does the same thus:

#!/usr/local/bin/python3.2
import os, urllib.parse, gzip, subprocess
QUERY_STRING=os.environ['QUERY_STRING']
MAN=urllib.parse.parse_qs(QUERY_STRING)['man'][0]
try:
PAGE=urllib.parse.parse_qs(QUERY_STRING)['page'][0]
except (KeyError):
PAGE=''
manfile = subprocess.getoutput("man --path "+PAGE+" "+MAN)
p1 = subprocess.Popen(["gunzip","--to-stdout",manfile],
stdout=subprocess.PIPE)
p2 = subprocess.Popen(["groff", "-Thtml", "-mandoc"], stdin=p1.stdout,
stdout=subprocess.PIPE, universal_newlines=True)
p1.stdout.close()
print("Content-type: text/html; charset=utf-8\n")
print(p2.communicate()[0])

Figuring I needed to add 'universal_newlines=True' to get rid of all
the '\n's took me quite a while.

I'm now modifying this to use python's gzip library rather than
calling gunzip as a subprocess, but so far produce gibberish since
f=gzip.open(manfile) insists on flagging the text output as binary
data which confuses groff. That, however, is not the issue here.

The point is, I thought it would be worthwhile writing a Howto/
Tutorial on doing shell script type programming with python, and
thought I'd start a thread for suggestions on tricks from the gurus
out there.

PS: lets avoid any ideological fights about bash vs python. From my
side, I found the bash version of the above quicker and easier to
write, but would hate to expand it into a bigger program, whereas
python got my mind buzzing with ideas on features to add.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top