General Line Ordering Reversal

#!/usr/bin/python
import sys
l = []
while (1):
	s = sys.stdin.readline()
	if (not s):
		break
	s = s.strip()
	l.append(s)
l.reverse()
for i in l:
	print(i)

General Line Ordering Reversal

Python Function Grep In Python

I am replacing this post (which was just normal grep in Python) with a script that will look for a search term and print out the last py method (def) it was seen in.

#!/usr/bin/python

import re
import sys

linenumb = 1
srchterm = ""
srchfunc = ""

lastfnum = 0
lastfunc = ""

if (len(sys.argv) < 2):
	print("Usage: %s <regex search term> [<function name>]" % (sys.argv[0]))
	sys.exit(0)

if (len(sys.argv) >= 2):
	srchterm = sys.argv[1]

if (len(sys.argv) >= 3):
	srchfunc = sys.argv[2]

while (1):
	lineread = sys.stdin.readline()
	
	if (not lineread):
		break
	
	lineread = lineread.rstrip()
	
	if (re.match("^def .*$", lineread)):
		lastfnum = linenumb
		lastfunc = lineread
	
	if (re.match("^[\t ]+.*" + srchterm + ".*$", lineread)):
		if ((lastfunc == "") or (re.match("^def " + srchfunc + ".*$", lastfunc))):
			if (lastfunc != ""):
				print("[*] [%d] %s" % (lastfnum, lastfunc))
				
				lastfnum = 0
				lastfunc = ""
			
			print("[-] [%d] %s" % (linenumb, lineread))
	
	linenumb += 1

Python Function Grep In Python

Remote IRC Viewer In Py

Here is a script (which probably contains bugs still) that allows one to host an “IRC like proxy” server in which multiple IRC clients can connect to one IRC server. Basically, the IRC session is simply relayed to all clients while the server maintains just one IRC connection. Anyway, the code is below in case you would like to play around with it.

import os
import random
import re
import select
import stat
import socket
import sys
import time


class loccom:
    def rand(self, numb):
        outp = ""
        
        for x in range(0, numb):
            outp += random.choice("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
        
        return outp
    
    def clrf(self):
        try:
            fobj = open(self.file, "w")
            fobj.write("")
            fobj.close()
        
        except:
            pass
    
    def tmpc(self):
        dlst = os.listdir(self.fold)
        
        for item in dlst:
            if (not re.match("^%s.*$" % (self.pref), item)):
                continue
            
            sobj = os.stat("%s/%s" % (self.fold, item))
            
            psec = time.time()
            asec = sobj.st_atime
            msec = sobj.st_mtime
            stle = (60 * 60 * 1)
            
            if (((psec - asec) >= stle) or ((psec - msec) >= stle)):
                os.unlink("%s/%s" % (self.fold, item))
    
    def __init__(self):
        toke = self.rand(16)
        
        self.fold = "/tmp"
        self.pref = "loccom"
        self.file = ("%s/%s.%s.txt" % (self.fold, self.pref, toke))
        
        self.lock = 0
        self.wait = 0.1
        
        self.clrf()
        
        self.tmpc()
    
    def sloc(self):
        if (not os.path.isfile(self.file)):
            raise IOError("File does not exist.")
        
        while (self.lock != 0):
            time.sleep(self.wait)
        
        self.lock = 1
    
    def rloc(self):
        self.lock = 0
    
    def send(self, stri):
        self.sloc()
        
        try:
            fobj = open(self.file, "a")
            fobj.write(stri)
            fobj.close()
        
        except:
            pass
        
        self.rloc()
    
    def recv(self):
        self.sloc()
        
        try:
            fobj = open(self.file, "r")
            outp = fobj.read()
            fobj.close()
        
        except:
            outp = ""
        
        self.clrf()
        
        self.rloc()
        
        return outp


def resi(srch, repl, inpt):
    while (1):
        regx = re.match("^.*(%s).*$" % srch, inpt, re.I | re.S)
        
        if (not regx):
            break
        
        inpt = re.sub(regx.group(1), repl, inpt)
    
    return inpt


def ircpmake():
    ircphost = "irc.freenode.net"
    ircpport = 6667
    
    ircpobjc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    ircpobjc.connect((ircphost, ircpport))
    
    return ircpobjc


statdata = ""

def readline(inptstri=""):
    global statdata
    
    outpstri = ""
    
    if (inptstri != ""):
        statdata += inptstri;print("setting["+inptstri+"]")
    
    elif (statdata != ""):
        statlist = statdata.split("\n")
        
        if (len(statlist) > 1):
            outpstri = (statlist[0] + "\n")
            tempdata = ""
            
            for x in range(1, len(statlist)):
                if (statlist[x] == ""):
                    continue
                
                if (tempdata != ""):
                    tempdata += "\n"
                
                tempdata += statlist[x]
            
            if (tempdata != ""):
                if (statdata[-1] == '\n'):
                    tempdata += "\n"
            
            statdata = tempdata
    
    return outpstri


# irc client object

ircpobjc = ircpmake()

ipaddr = ircpobjc.getsockname()
ipaddr = ipaddr[0]

imptlist = []
chanlist = []
sendlist = []
recvlist = []

# parent server object

hostname = ""
portnumb = 6060

sockobjc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sockobjc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sockobjc.bind((hostname, portnumb))
sockobjc.listen(1)

while (1):
    sendremo = []
    recvremo = []
    (readlist, writelst, errolist) = select.select([sockobjc, ircpobjc], [], [], 0.1)
    
    if (sockobjc in readlist):
        (clieobjc, clieaddr) = sockobjc.accept()
        print("parent accepted [%s]" % (clieaddr[0]))
        
        servclie = loccom()
        clieserv = loccom()
        forkpidn = os.fork()
        
        if (forkpidn == 0):
            try:
                ircpobjc.close()
                sockobjc.close()
                sendlist = []
                recvlist = []
            
            except:
                pass
            
            break
        
        clieobjc.close()
        sendlist.append(servclie)
        recvlist.append(clieserv)
        
        for ircpitem in imptlist:
            servclie.send("FORK INIT\r\n%s\r\n" % (ircpitem))
        
        for chanitem in chanlist:
            servclie.send("FORK INIT\r\n%s JOIN %s\r\n" % (chanitem[0], chanitem[1]))
    
    if (ircpobjc in readlist):
        try:
            ircpdata = ircpobjc.recv(1024)
            
            if (ircpdata):
                readline(inptstri=ircpdata)
            
            else:
                ircpobjc = ircpmake()
        
        except:
            ircpobjc = ircpmake()
    
    while (1):
        ircpdata = readline()
        
        if (not ircpdata):
            break
        
        ircpline = ircpdata.strip()
        
        regxobjc = re.match("^:[^ ]+ 001 .+$", ircpline, re.I)
        
        if (regxobjc):
            imptlist.append(ircpline)
        
        regxobjc = re.match("^(:[^ ]*%s[^ ]*) join ([^ ]+).*$" % (ipaddr), ircpline, re.I)
        
        if (regxobjc):
            chanlist.append([regxobjc.group(1), regxobjc.group(2)])
        
        regxobjc = re.match("^(:[^ ]*%s[^ ]*) part ([^ ]+).*$" % (ipaddr), ircpline, re.I)
        
        if (regxobjc):
            remochan = []
            
            for chanitem in chanlist:
                if (chanitem[1] == regxobjc.group(2)):
                    remochan.append(chanitem)
            
            for remoitem in remochan:
                chanlist.remove(remoitem)
        
        print("ircp -> serv -> clie [%s]" % (ircpline))
        
        for senditem in sendlist:
            try:
                senditem.send(ircpdata)
            
            except:
                sendremo.append(senditem)
    
    x = 0
    
    for recvitem in recvlist:
        try:
            recvdata = recvitem.recv()
            
            if (recvdata):
                if (x > 0):
                    recvdata = resi("\nNICK ", "\nTICK ", "\n" + recvdata)
                
                print("clie -> serv -> ircp [%s]" % (recvdata.strip()))
                
                try:
                    ircpobjc.send(recvdata)
                
                except:
                    ircpobjc = ircpmake()
        
        except:
            recvremo.append(recvitem)
        
        x += 1
    
    for remoitem in sendremo:
        sendlist.remove(remoitem)
    
    for remoitem in recvremo:
        recvlist.remove(remoitem)

if (forkpidn != 0):
    sys.exit(0)

# child server object

print("child accepted [%s]" % (clieaddr[0]))

while (1):
    (readlist, writelst, errolist) = select.select([clieobjc], [], [], 0.1)
    
    if (clieobjc in readlist):
        cliedata = clieobjc.recv(1024)
        
        if (cliedata):
            cliedata = re.sub("^WHO ", "NAMES ", cliedata)
            print("user -> clie -> serv [%s]" % (cliedata.strip()))
            clieserv.send(cliedata)
    
    servdata = servclie.recv()
    
    if (servdata != ""):
        print("serv -> clie -> user [%s]" % (servdata.strip()))
        clieobjc.send(servdata)


Remote IRC Viewer In Py

Recursive GIT Tree Traverser In Python

Here’s a little script I wrote which will traverse through an online git repo and hopefully display the file tree. There’s probably a command to do this but I thought I’d try to write it anyway to test my recursion skills.

#!/usr/bin/python

import re
import urllib

def mt(tn):
    o = ""
    
    for x in range(0, tn):
        o += "    "
    
    return o

def rr(pd, tn):
    webobj = urllib.urlopen("http://git.fedorahosted.org/git/?p=arm.git;a=tree;f=" + pd)
    webstr = webobj.read()
    
    webstr = webstr.replace("\t", "")
    webstr = webstr.replace("\r", "")
    webstr = webstr.replace("\n", "")
    
    webstr = re.sub("<tr", "\n<tr", webstr)
    
    weblist = webstr.split("\n")
    
    dirlist = pd.split("/")
    
    while (len(dirlist) > 1):
        dirlist.pop(0)
    
    print(mt(tn) + dirlist[0] + "/")
    
    for webitem in weblist:
        if (re.match("^<tr", webitem)):
            webitem = re.sub("<[^>]*>", " ", webitem)
            webitem = webitem.strip()
            webitem = re.sub("  [ ]*", " ", webitem)
            
            itemlist = webitem.split(" ")
            
            if (len(itemlist) < 3):
                continue
            
            if (itemlist[2] == ".."):
                continue
            
            if (not re.match("^d.*", itemlist[0])):
                print(mt(tn + 1) + itemlist[2])
            
            else:
                rr(pd + "/" + itemlist[2], tn + 1)

rr("styrene", 0)

also to get the last check-in name/time:

#!/bin/bash

webpoutp=`curl -s 'http://git.fedorahosted.org/git/?p=arm.git;a=shortlog'`
webpoutp=`echo "$webpoutp" | tr -d '\t\r\n'`
webpoutp=`echo "$webpoutp" | awk '{ gsub(/<td/, "\n<td"); gsub(/<\/td/, "\n</td"); print; }'`
webpoutp=`echo "$webpoutp" | grep -i '^<td'`
webpoutp=`echo "$webpoutp" | head -n 2`
webpoutp=`echo "$webpoutp" | sed -e 's/<[^>]*>//g'`

fnaloutp=""

echo "$webpoutp" | while read lineread
do
	if [ "$fnaloutp" != "" ]
	then
		fnaloutp="${fnaloutp} - "
	fi
	
	fnaloutp="${fnaloutp}${lineread}"
	echo "Last git push: [$fnaloutp]" > /tmp/git.log
done

cat /tmp/git.log

Recursive GIT Tree Traverser In Python

[weekend] Real-Time BASH To HTTP Viewer

So for some reason, I decided to make python call up a bash shell while outputting everything from the terminal to a web page. I don’t think this is supposed to even work but with FF4 it seems to. Basically the python script starts by setting stdin to raw (canon) i/o mode. Then it forks a child process (bash) in which it can write to it’s stdin and read from it’s stdout/stderror. It then fires up a simple TCP socket HTTP server which sends any viewers the output of the shell’s stdout. It uses basic alarm interrupts to prevent blocking calls and basically acts as a service-in-the-middle. In English, this thing lets web viewers monitor your command line input/output. I don’t know why you would even want or need this but I guess it’s a cool hack/proof of concept.

#!/usr/bin/python

import os
import re
import signal
import socket
import sys
import time

import tty
import termios

def cleanstr(inptstri):
	backspce = (chr(8) + chr(27) + "[K")
	outpstri = inptstri
	
	outpstri = outpstri.replace(backspce, "^H")
	outpstri = outpstri.replace("&", "&amp;")
	outpstri = outpstri.replace("<", "&lt;")
	outpstri = outpstri.replace(">", "&gt;")
	outpstri = outpstri.replace(" ", "&nbsp;")
	outpstri = outpstri.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;")
	outpstri = outpstri.replace("\r", "")
	outpstri = outpstri.replace("\n", "<br /> \n")
	
	fnalstri = ""
	
	for c in outpstri:
		d = ord(c)
		if ((d != 10) and (d < 32)):
			fnalstri += ("[%d]" % (d))
		else:
			fnalstri += c
	
	return fnalstri

# define a dummy alarm handler

def alarmhan(signanum, framenum):
	pass

signal.signal(signal.SIGALRM, alarmhan)

# spawn a new child shell

(shelcpid, shelcdes) = os.forkpty()

if (shelcpid == 0):
	os.execve("/bin/bash", [], os.environ)
	sys.exit(0)

# init some socket variables for our server

hostname = ""
portnumb = 8080
sockobjc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sockobjc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sockobjc.bind((hostname, portnumb))
sockobjc.listen(5)
sockobjc.setblocking(0)

# set stdin i/o to raw (canon) mode

stdidesc = sys.stdin.fileno()
stdiolds = termios.tcgetattr(stdidesc)
tty.setraw(stdidesc)

# become a man-in-the-middle : user <-> us <-> shell -> us -> viewer

timeouta = 0.05
socklist = []

while (1):
	signal.setitimer(signal.ITIMER_REAL, timeouta)
	try:
		c = sys.stdin.read(1)
		if (ord(c) == 4):
			break
		if (ord(c) == 13):
			c = chr(10)
		os.write(shelcdes, c)
	except:
		pass
	signal.alarm(0)
	
	try:
		(clieconn, clieaddr) = sockobjc.accept()
		clieconn.send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
		clieconn.send("<script>function stob() { window.scrollTo(0, document.body.scrollHeight); } setInterval('stob();', 250);</script> \n")
		for x in range(0, 64):
			clieconn.send(" &nbsp; <br /> \n ")
		socklist.append(clieconn)
	except:
		pass
	
	signal.setitimer(signal.ITIMER_REAL, timeouta)
	try:
		r = os.read(shelcdes, 1024)
		sys.stdout.write(r)
		sys.stdout.flush()
		l = []
		s = cleanstr(r)
		for sockitem in socklist:
			try:
				sockitem.send(s)
			except:
				l.append(sockitem)
		for i in l:
			socklist.remove(i)
	except:
		pass
	signal.alarm(0)

termios.tcsetattr(stdidesc, termios.TCSADRAIN, stdiolds)
print("")

[weekend] Real-Time BASH To HTTP Viewer

[off-topic] IRC Notifications In Shell Script

So I haven’t had anything that I thought was important enough to blog about lately and I decided to take a break from in-depth python and make something random/weird. This is a shell script which runs in a loop to search for packets with certain text. Once that text is found, it executes a given command.

Example:

./irce.sh eth0 6667 'privmsg.*nickname' 'xeyes'

This example will monitor any IRC traffic for any message which contains the nickname given above and if so, it will execute the xeyes program to pop-up on your display.

#!/bin/bash

if [ "$4" == "" ]
then
	echo "Usage: $0 <interface> <port> <search regex> <command>"
	exit 0
fi

intf="$1"
port="$2"
srch="$3"
comd="$4"

tcpdump -Alnni "$intf" port "$port" 2> /dev/null | while read lineread
do
	testoutp=`echo "$lineread" | grep -i "$srch"`
	if [ "$testoutp" != "" ]
	then
		/bin/bash -c $comd &
	fi
done

[off-topic] IRC Notifications In Shell Script

Fedora-Arm Mailing List Monitor Bot

So I couldn’t find a reasonable way to manage the amount of mailing list email being sent to my personal and school address so I had to do this. I know you’re probably tired of seeing my various IRC bot scripts but I couldn’t take the amount of email that the mailing list generates. This is a bot which monitors the fedora-arm mailing list and posts any new changes to a given channel. Right now its being tested in the #seneca-cdot-arm channel on the freenode network.

Edit: Last updated on June 30, 2011

mbot.py

import os
import re
import sys
import time

import lbot


def fechfile(filename, urlnstri=""):
	resllist = []
	
	try:
		fileobjc = file("/tmp/%s" % (filename))
		readdata = fileobjc.read()
		fileobjc.close()
	
	except:
		return resllist
	
	readdata = readdata.strip().lower()
	readdata = readdata.replace("\t", "").replace("\r", "").replace("\n", "")
	readdata = readdata.replace("<li>", "\n<li>")
	readdata = readdata.replace("</i>", "</i>\n")
	
	readlist = readdata.split("\n")
	
	for readitem in readlist:
		readitem = readitem.strip()
		
		regxobjc = re.match('^<li><a href="([^"]+)">(.+)</a><a name="[^"]+">[^<]+</a><i>([^<]+)</i>$', readitem)
		
		if (regxobjc):
			reslstri = ("[%s] [%s] [ %s ]" % (regxobjc.group(3), regxobjc.group(2), urlnstri + regxobjc.group(1)))
			resllist.append(reslstri)
	
	return resllist

def ircbmain():
	if (len(sys.argv) < 4):
		print("Usage: %s <nick> - <chan 0> ... <chan N>" % (sys.argv[0]))
		sys.exit(0)
	
	nickname = ""
	chanlist = []
	
	x = 1
	l = len(sys.argv)
	f = 0
	
	while (x < l):
		if (sys.argv[x] == "-"):
			f += 1
		
		elif (f == 0):
			nickname = sys.argv[x]
		
		elif (f == 1):
			chanlist.append(sys.argv[x])
		
		x += 1
	
	websstri = ("http://lists.fedoraproject.org/pipermail/arm/%s-%s/" % (time.strftime("%Y"), time.strftime("%B")))
	
	p = os.fork()
	
	if (p == 0):
		websstri = ("%sdate.html" % (websstri))
		
		lbot.readurls(urlnlist=[websstri], filename=nickname)
		sys.exit(0)
	
	pastdata = []
	presdata = []
	
	lbot.ircbinit(nickname, chanlist)
	
	while (1):
		tempdata = fechfile(nickname, urlnstri=websstri)
		
		if (tempdata):
			presdata = tempdata
			
			if (not pastdata):
				pastdata = presdata
		
		ircdstri = lbot.ircbmain(pastdata, presdata)
		
		if (type(ircdstri) == type(-1)):
			break
		
		if (presdata):
			pastdata = presdata

ircbmain()

Fedora-Arm Mailing List Monitor Bot

Message Highlighting Bot

This py bot script will join all of the channels given to it and sit there monitoring for messages containing any keywords provided to it. It will then forward those messages along to a given nick.

Edit: Last updated on June 30, 2011

pbot.py

import os
import re
import sys
import time

import lbot

def ircbmain():
	print(sys.argv)
	if (len(sys.argv) < 8):
		print("Usage: %s <nick> - <chan 0> ... <chan N> - <dest> - <srch 0> ... <srch N>" % (sys.argv[0]))
		sys.exit(0)
	
	nickname = ""
	chanlist = []
	destname = ""
	srchlist = []
	
	x = 1
	l = len(sys.argv)
	f = 0
	
	while (x < l):
		if (sys.argv[x] == "-"):
			f += 1
		
		elif (f == 0):
			nickname = sys.argv[x]
		
		elif (f == 1):
			chanlist.append(sys.argv[x])
		
		elif (f == 2):
			destname = sys.argv[x]
		
		elif (f == 3):
			srchlist.append(sys.argv[x])
		
		x += 1
	
	lbot.ircbinit(nickname, chanlist)
	
	while (1):
		ircdstri = lbot.ircbmain([], [])
		
		for srchstri in srchlist:
			regxobjc = re.match("^:?([^ ]+)![^ ]+ PRIVMSG ([^ ]+) :(.*%s.*)" % (srchstri), ircdstri, re.I)
			
			if (regxobjc):
				mesgstri = ("PRIVMSG %s :mesg detect: [%s] in [%s] mesg [ %s ]" % (destname, regxobjc.group(1), regxobjc.group(2), regxobjc.group(3)))
				lbot.waitsend(mesgstri)
				break
			
			regxobjc = re.match("^:?([^ ]*%s[^ ]*)![^ ]+ PRIVMSG ([^ ]+) :(.+)" % (srchstri), ircdstri, re.I)
			
			if (regxobjc):
				mesgstri = ("PRIVMSG %s :nick detect: [%s] in [%s] mesg [ %s ]" % (destname, regxobjc.group(1), regxobjc.group(2), regxobjc.group(3)))
				lbot.waitsend(mesgstri)
				break

ircbmain()

Message Highlighting Bot

reddit/slashdot RSS IRC Bot

Just wanted to separate the below post a bit and I pulled this bot script out to do so. It simply grabs the RSS feed from the given list of URLs and posts any changes to a given channel.

rbot.py

import os
import re
import select
import urllib
import sys
import time

import lbot

def fechwebs():
	readdata = ""
	urlnlist = ["www.reddit.com/.rss", "rss.slashdot.org/Slashdot/slashdot"]
	resllist = []
	
	xmlshead = ""
	xmlslink = ""
	
	for urlnitem in urlnlist:
		uobjdata = urllib.urlopen("http://" + urlnitem)
		readdata += uobjdata.read()
	
	readdata = readdata.replace("\r", "")
	readdata = readdata.replace("\n", "")
	readdata = readdata.replace("<", "\n<")
	
	datalist = readdata.split("\n")
	
	for dataitem in datalist:
		regxobjc = re.match("<title>(.*)", dataitem)
		
		if (regxobjc):
			xmlshead = ("[" + regxobjc.group(1) + "]")
		
		regxobjc = re.match("<link>(.*)", dataitem)
		
		if (regxobjc):
			xmlslink = regxobjc.group(1)
		
		if ((xmlshead != "") and (xmlslink != "")):
			resllist.append(xmlshead + " " + xmlslink)
			
			xmlshead = ""
			xmlslink = ""
	
	if (len(resllist) == 0):
		return -1
	
	return resllist

def ircbmain():
	if (len(sys.argv) < 4):
		print("Usage: %s <nick> - <chan 0> ... <chan n>" % (sys.argv[0]))
		sys.exit(0)
	
	nickname = ""
	chanlist = []
	
	x = 1
	l = len(sys.argv)
	f = 0
	
	while (x < l):
		if (sys.argv[x] == "-"):
			f += 1
		elif (f == 0):
			nickname = sys.argv[x]
		elif (f == 1):
			chanlist.append(sys.argv[x])
		x += 1
	
	webstime = 0
	webswait = (5 * 60)
	pastdata = -1
	presdata = -1
	
	lbot.ircbinit(nickname, chanlist)
	
	while (1):
		prestime = time.time()
		difftime = (prestime - webstime)
		
		if (difftime > webswait):
			print(lbot.formtime() + " WEB [INFO] Checking site(s)...")
			presdata = fechwebs()
			
			if (type(pastdata) == type(-1)):
				pastdata = presdata
			
			webstime = prestime
		
		ircdstri = lbot.ircbmain(pastdata, presdata)
		
		if (type(ircdstri) == type(-1)):
			break
		
		if (type(presdata) != type(-1)):
			pastdata = presdata

ircbmain()

reddit/slashdot RSS IRC Bot

Checking JavaScript Variables With Python

So I have OCD when it comes to JavaScript because it’s such a lenient language and it is hard to tell when you’re misusing variables. The editors that I use on a daily basis don’t tell me if (1) a variable has been declared but never used and (2) if a variable is being used without having been declared. I wrote a python script which attempts to check for these cases. There may be a bunch of bugs as I did not write a JavaScript language parser but instead a series of checks based on regex. Here is the code:

Edit: One of the commenter’s pointed out that one could use jslint and jshint to receive way better information about your JS code. Thank you for the recommendation!

#!/usr/bin/python

import re
import sys

def rmescape(inptstri):
	backflag = 0
	outpstri = ""
	
	for letritem in inptstri:
		if (letritem == '\\'):
			backflag = 1
		
		elif (backflag == 1):
			backflag = 0
		
		else:
			outpstri += letritem
	
	return outpstri

scptflag = 0
readlist = []

while (1):
	tempread = sys.stdin.readline()
	
	if (not tempread):
		break
	
	# clean the front and end of the string
	tempread = tempread.strip()
	
	if (re.match("^</script.*$", tempread)):
		scptflag = 0
	
	if (scptflag == 1):
		templist = tempread.split(";")
		
		for tempitem in templist:
			if (tempitem not in readlist):
				readlist.append(tempitem)
	
	if (re.match("^<script.*$", tempread)):
		scptflag = 1

varslist = []

for lineread in readlist:
	# remove any escapes
	lineread = rmescape(lineread)
	
	# remove any chars, strings, regex
	lineread = re.sub("\'[^\']*\'", "", lineread)
	lineread = re.sub("\"[^\"]*\"", "", lineread)
	lineread = re.sub("\/[^\/]+\/", "", lineread)
	
	# remove any comments
	lineread = re.sub("//.*", "", lineread)
	
	# remove any lists, arrays, dictionaries
	lineread = re.sub("\<[^\>]*\>", "", lineread)
	lineread = re.sub("\[[^\]]*\]", "", lineread)
	lineread = re.sub("\{[^\}]*\}", "", lineread)
	#lineread = re.sub("\([^\)]*\)", "", lineread)
	
	tempstri = ""
	
	regxobjc = re.match("^var[ ]+(.*)$", lineread)
	
	if (regxobjc):
		tempstri = regxobjc.group(1)
	
	regxobjc = re.match("^function[ ]+(.*)$", lineread)
	
	if (regxobjc):
		tempstri = regxobjc.group(1)
		tempstri = tempstri.replace("(", ",")
		tempstri = tempstri.replace(")", "")
	
	if (tempstri != ""):
		# remove any spaces, tabs
		tempstri = tempstri.replace(" ", "")
		tempstri = tempstri.replace("\t", "")
		
		# remove any variable assignment values
		tempstri = re.sub("=[^,]*", "", tempstri)
		
		# split the line to get all of the decalred variables
		templist = tempstri.split(",")
		
		for tempitem in templist:
			if (tempitem not in varslist):
				varslist.append(tempitem)

#print(varslist)

for varsread in varslist:
	usedflag = 0
	
	for lineread in readlist:
		# remove any spaces, tabs
		lineread = lineread.replace(" ", "")
		lineread = lineread.replace("\t", "")
		
		# skip variable assignment lines
		regxobjc = re.match("^.*" + varsread + "=[^=].*$", lineread)
		
		if (regxobjc):
			continue
		
		# get variable usage lines
		regxobjc = re.match("^.*" + varsread + ".*$", lineread)
		
		if (regxobjc):
			usedflag = 1
			break
	
	if (usedflag == 0):
		print("note: variable possibly unused: [%s]" % (varsread))

nameregx = "[A-Za-z][0-9A-Za-z\_]*"
templist = ["null", "true", "false", "document", "window", "Math"]

for tempitem in templist:
	varslist.append(tempitem)

for lineread in readlist:
	origline = lineread
	
	# replace any variable declaration prefix's
	lineread = re.sub("^var ", "", lineread)
	
	# replace any function declaration lines
	lineread = re.sub("^function .*", "", lineread)
	
	# replace any return statement lines
	lineread = re.sub("^return .*", "", lineread)
	
	# remove any spaces, tabs
	lineread = lineread.replace(" ", "")
	lineread = lineread.replace("\t", "")
	
	# remove any chars, strings, regex
	lineread = re.sub("\'[^\']*\'", "", lineread)
	lineread = re.sub("\"[^\"]*\"", "", lineread)
	lineread = re.sub("\/[^\/]+\/", "", lineread)
	
	# remove any comments
	lineread = re.sub("//.*", "", lineread)
	
	# replace any method calls (special replace char to prevent function replace)
	lineread = re.sub("\." + nameregx + "[=\(]*", "~", lineread)
	
	# replace any assignments, function calls
	lineread = re.sub(nameregx + "[=\(]", "", lineread)
	
	while (1):
		regxobjc = re.search("(" + nameregx + ")", lineread)
		
		if (not regxobjc):
			break
		
		namestri = regxobjc.group(1)
		
		if (namestri not in varslist):
			print("warn: variable possibly undeclared: [%s] on line [%s]" % (namestri, origline))
		
		lineread = lineread.replace(namestri, "")

Checking JavaScript Variables With Python

Convert Text To HTML Char Codes

I got tired of hand-formatting or shell-scripting a text converter for this blog to post code properly so I wrote this py script to do it for me. It converts a text file containing possibly used HTML characters into HTML character codes.

#!/usr/bin/python
import sys
s = ""
l = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
while (1):
	c = sys.stdin.read(1)
	if (not c):
		break
	o = ord(c)
	if (o == 13):
		continue
	elif ((o < 127) and (c not in l)):
		s += ("&#" + str(o) + ";")
	else:
		s += (c)
print(s)

Convert Text To HTML Char Codes

Non-Exiting Screen Startup

I was trying to run screen in the background with a python script inside of it, however, when the python was exiting due to coding errors, the screen was quitting too. I could have turned on logging but I didn’t want to deal with screen log files all the time. I wanted the screen session to remain open, even if the python quit so I could reattach to it and see what was going on (like a normal command prompt). Here was the solution I came up with:

/usr/bin/screen -dm /bin/bash -c 'echo "your program here" ; /bin/bash'

Run this to reattach it later:

/usr/bin/screen -dr
Non-Exiting Screen Startup

JavaScript Popup For The CDoT Dashboard

Below is a simple piece of JavaScript which will open a new pop-up window pointing to the CDoT dashboard that contains various interesting stats related to our work. You are able to change the height and width of the window by modifying the appropriate options below. In addition, you are able to disable features like the toolbar, menubar and statusbar in order to give the window a fullscreen-kiosk-like look and feel.

<script type="text/javascript">
	window.open("http://scotland.proximity.on.ca/jbuck/status/", "awesome", "width=1700, height=1900, \
toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no");
</script>
JavaScript Popup For The CDoT Dashboard

Simple RSS Application For OS X

Here’s an application I created for OS X which grabs new stories/articles that are posted to slashdot and reddit front pages (you can’t enter any site yet but it works with RSS). The app is meant to be run off focus (behind other windows) and it will keep a list of stories read in order to tell which stories are new. As you click on the new stories you want to read, they will disappear and once the window is put in the background, it will open the chosen articles in the default browser and remove the new stories from the list.

I created this app as it acts more like an RSS-diff program and simply tracks and monitors changes in RSS stories. The busier the feed the better as this program will basically keep a log of stories until you check them out. Let me know if you find this program useful and/or have any suggestions on how to make it better!

Feed App Download [via mediafire]

Simple RSS Application For OS X

Smarter Fedora Build Process (Styrene)

This project will hopefully allow for a smarter build process to take place when building Fedora which includes choosing a specific order for the packages destined to be queued. It should allow one to intelligently determine which packages need to be built first (if it has 0 dependencies or all of its dependencies already built). The main loop basically just performs passes through the list of unbuilt packages and seeing which ones meet these requirements and queuing them.



[replaced by a new script: koji-follow.py]

Smarter Fedora Build Process (Styrene)