plumber

Plumber – a modern approach to plumbing
git clone git://r-36.net/plumber
Log | Files | Refs | LICENSE

fingeropener (1236B)


      1 #!/usr/bin/env python
      2 # coding=utf-8
      3 #
      4 # Copy me if you can.
      5 # by 20h
      6 #
      7 
      8 import os
      9 import sys
     10 import getopt
     11 import urllib.parse
     12 from subprocess import Popen
     13 
     14 def usage(app):
     15 	app = os.path.basename(app)
     16 	sys.stderr.write("usage: %s [-h] URI\n" % (app))
     17 	sys.exit(1)
     18 
     19 def runcmd(cmd, arg=None):
     20 	fd = open("/dev/null")
     21 	if os.fork() == 0:
     22 		if arg != None:
     23 			cmd = cmd % (arg)
     24 		p = Popen(cmd, shell=True, stdout=fd, stderr=fd)
     25 		p.wait()
     26 
     27 def main(args):
     28 	try:
     29 		opts, largs = getopt.getopt(args[1:], "h")
     30 	except getopt.GetoptError as err:
     31 		print(str(err))
     32 		usage(args[0])
     33 	
     34 	for o, a in opts:
     35 		if o == "-h":
     36 			usage(args[0])
     37 		else:
     38 			assert False, "unhandled option"
     39 	
     40 	if len(largs) < 1:
     41 		usage(args[0])
     42 	uri = " ".join(largs)
     43 
     44 	puri = urllib.parse.urlparse(uri)
     45 
     46 	host = puri.hostname
     47 	user = puri.username
     48 	if host != None and user != None:
     49 		req = "%s:%s" % (user, host)
     50 	elif host == None and user != None:
     51 		req = "%s" % (user)
     52 	elif host == None and user == None:
     53 		req = ""
     54 	else:
     55 		sys.stderr.write("Invalid URI.\n")
     56 		return 1
     57 
     58 	if req == "":
     59 		runcmd("st -e sh -c \"finger -l; read;\"")
     60 	else:
     61 		runcmd("st -e sh -c \"finger -l '%s'; read;\"", req)
     62 
     63 	return 0
     64 
     65 if __name__ == "__main__":
     66 	sys.exit(main(sys.argv))
     67