Scripts

From stacky wiki
Revision as of 06:31, 22 November 2011 by 147.46.215.206 (talk) (→‎unpnup)

Here are some scripts I've written for various purposes.

AirBearsLogin.py

While at Berkeley, I would use this following script to log in to AirBears (the wireless network).

#!/usr/bin/python
#
# Replace USERNAME with your username and PASSWORD with your password.
# You may need to install the python mechanize module. The command
# sudo apt-get install python-mechanize
# might do the trick

try:
    from mechanize import Browser
    import re
    br = Browser()
    br.open("https://auth.berkeley.edu/cas/login?service=https%3a%2f%2fwlan.berkeley.edu%2fcgi-bin%2flogin%2fcalnet.cgi%3fsubmit%3dCalNet")
    br.select_form(nr=0)
    br["username"]="USERNAME"
    br["password"]="PASSWORD"
    page = br.submit().read()
    errors = re.findall('<h2 id="status" class="error">[^<]*</h2>',page)
    if len(errors)==0:
        print "Logged into AirBears (probably)."
    else:
        for error in errors: print error[30:-5]
except IOError, e: print "Couldn't connect to wireless login page:\n", e
except KeyboardInterrupt: print "Ouch! Ok, quitting."

I'm quite pleased with the infromaiton in this one. TY!

djvu2pdf

A bash script I use for converting djvu to pdf so that I can read the document on a kindle or iLiad.

#!/bin/bash
# djvu2pdf script by Anton Geraschenko 2009; use at your own risk.
# requires djvulibre-bin and libtiff-tools
#
# "bug": Blank pages get removed. You can tell this happened
#        if you see the message "ddjvu: Cannot render image"

# exit codes
X_DDJVU_OR_TIFF2PDF=1
X_BAD_OPTION=2	
X_USER_INTERRUPT=13

USAGE () 
{
echo "Usage: `basename $0` [OPTIONS] FILE.djvu
Convert a djvu file into a pdf.

-b		Keep blank pages. Not yet implemented.
-o OUTFILE	Set the output file to OUTFILE.
-q		Quiet mode."
}

clean_up ()
{
echo
[ $QUIET ] || echo "cleaning up"
if [ -e $tmpdir ]; then
	rm $tmpdir/pg[0-9]*.*
	rmdir $tmpdir
fi
}

while getopts "o:qb" OPTION ; 
do
    case "$OPTION" in
	b) echo "Sorry, the -b option is not yet implemented. Blank pages will be stripped out." ;;
        o) OUTFILE="$OPTARG" ;;
        q) QUIET=1
	   set +v ;;
        *) USAGE 1>&2 
           exit $X_BAD_OPTION ;;
    esac
done
shift $(($OPTIND - 1))

if [ $# != 1 ] || [[ "$1" != *.djvu ]]; then
	USAGE 1>&2
	exit 1
else
	trap 'clean_up; exit $X_USER_INTERRUPT' SIGINT
	tmpdir=`mktemp -d`
	pages=`djvudump "$1" | head -n2 | tail -n1 | sed -e 's/^.*files \([0-9]*\) pages)/\1/'`
	[ $QUIET ] || echo -n "converting page"
	for i in `seq -w $pages`; do
		[ $QUIET ] || echo -n " $i"
		ddjvu -format=tiff -page=$i "$1" $tmpdir/pg$i.tiff &&
		tiff2pdf -j -o $tmpdir/pg$i.pdf $tmpdir/pg$i.tiff || [ $?=10 ] || 
		( echo encountered trouble;  exit $X_DDJVU_OR_TIFF2PDF; )
		# ddjvu exits with exit code 10 when the page is blank
		# if any other trouble comes up, exit
	done
	[ $QUIET ] || (echo ;echo -n "creating ${OUTFILE-${1%djvu}pdf}")
	pdftk $tmpdir/pg[0-9]*.pdf cat output "${OUTFILE-${1%djvu}pdf}"
	clean_up
fi

Scripts for managing notes

update.sh

#!/bin/bash
#
## tar the files into HalgSource.tgz
tar cfz HalgSource.tgz Halg.tex HalgPreamble.tex HalgBib.bib HalgLec??.tex HalgBibliography.tex

## move HalgSource.tgz to the math servers and execute the mathupdate script
scp HalgSource.tgz $math:/home/u2/grad/anton/public_html/written/Halg
ssh $math bash /home/u2/grad/anton/public_html/written/Halg/mathupdate.sh

mathupdate.sh

## untar the source
cd /home/u2/grad/anton/public_html/written/Halg/
tar xfz HalgSource.tgz -C ./

## make sure all files are readable (maybe not necessary)
chmod 666 HalgLec??.tex

## uncomment any \input commands that I had commented and uncomment table of contents
## (before I implemented this, people would often tell me that only the most recent lecture is posted)
cp Halg.tex halg.tex
sed -e 's/\% \\input/\\input/g' -e 's/\% \\tableofcontents/\\tableofcontents/g' halg.tex > Halg.tex

## compile and convert to pdf
latex Halg.tex
latex Halg.tex
latex Halg.tex
dvips Halg.dvi -o Halg.ps
ps2pdf Halg.ps Halg.pdf

## get rid of garbage
rm `ls | grep -ve .pdf -ve .sh -ve .tgz -ve .html -ve .sty -ve .txt`