#! /usr/bin/env python """ epaste.py takes input from STDIN (-) or multiple files , concatenates them, and 'pastes' the resulting text to http://rafb.net/paste to assist with data retrival for troubleshooting, especially when there is no X server available, and output is longer than can be viewed within a console concurrently. TODO: add exception handling TODO: add signal handling TODO: gracefully handle opts TODO: gracefully handle no data given TODO: add help TODO: gracefully handle bad file params """ import os import sys import pwd import urllib import urllib2 import fileinput def get_nick(): """ uses username and host (username@host) as nick # TODO: allow customization """ return pwd.getpwuid(os.getuid())[0] + "@" + os.uname()[1] def get_title(): """ allows user to select a title for the paste, uses default title if none provided # TODO: add some sane length checks """ # clear input sys.stdin = open("/dev/tty") # promt and read title = raw_input("Paste title: ") # ensure that it has a value if not title: title = "untitled paste" return title def get_text(): """ handles concatenation of data # TODO: add spaces between files """ data = "" for line in fileinput.input(): data += line return data def get_text2(): """ TODO: remove code """ # check for arguements args = sys.argv[1:] # if there are arguements, get files # else read from stdin if args: return get_text_files(args) else: return get_text_stdin() return def get_text_stdin(): """ TODO: remove code """ data = sys.stdin.read() return data def get_text_files(args): """ TODO: remove code """ print "get files" return "" def do_paste(): """ paste text to pastebin site """ do_paste_rafb() return def do_paste_rafb(): """ paste text to rafb.net's pastebin service """ # build http client handler_redirect = urllib2.HTTPRedirectHandler() opener = urllib2.build_opener(handler_redirect) urllib2.install_opener(opener) # setup data url = "http://rafb.net/paste/paste.php" values = { 'lang' : "Plain Text", 'cvt_tabs' : "No", 'nick' : get_nick(), 'desc' : get_title(), 'text' : get_text() } data = urllib.urlencode(values) # open connection and get response request = urllib2.Request(url, data) response = urllib2.urlopen(request) page = response.read() # print resulting url print response.geturl() return def init(): """ TODO: do a few sanity checks here """ return def main(): """ made code block to run necessary funtions """ init() do_paste() return if __name__ == "__main__": main()