Created
December 31, 2013 22:26
-
-
Save obriencj/8202872 to your computer and use it in GitHub Desktop.
Stub that I find useful for writing CLI utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
""" | |
Given two tags, do absolutely nothing. | |
This script is just a stub illustrating the bits I re-use for every | |
CLI utility I write. | |
You should always post what you want it | |
to do up here, so you can remind yourself later what it was you were | |
working on. | |
""" | |
import sys | |
from optparse import OptionParser | |
from socket import gaierror | |
from xmlrpclib import Fault, ServerProxy | |
KOJI_SERVICE="http://redacted/" | |
def _debug_on(msg): | |
print >> sys.stderr, msg | |
def _debug_off(msg): | |
pass | |
def _verbose_on(msg): | |
print msg | |
def _verbose_off(msg): | |
pass | |
class ForgotMyWork(Exception): | |
pass | |
def do_work(): | |
raise ForgotMyWork("I forgot to make this script do anything") | |
# Do the important stuff here. | |
def cli(options, release_tag, candidate_tag): | |
kojihub = ServerProxy(options.koji) | |
debug = _debug_on if options.debug else _debug_off | |
verbose = _verbose_on if options.verbose else _verbose_off | |
debug("starting to work on secret stuff") | |
verbose("Did Zero Work!") | |
do_work() | |
def create_optparser(): | |
parse = OptionParser("%prog [OPTIONS] TAG_A TAG_B", | |
description="Do something with two tags") | |
parse.add_option("--reverse", action="store_true", default=False, | |
help="reverse behavior") | |
parse.add_option("--inherit", action="store_true", default=False, | |
help="look through the entire inheritance") | |
parse.add_option("--koji", action="store", default=KOJI_SERVICE, | |
help="URI for the koji XMLRPC service") | |
parse.add_option("--debug", action="store_true", | |
help="print debugging output") | |
parse.add_option("--verbose", action="store_true", | |
help="print verbose messages") | |
return parse | |
def main(args): | |
parser = create_optparser() | |
options, args = parser.parse_args(args) | |
if len(args) > 3: | |
parser.error("Too many arguments!") | |
elif len(args) < 3: | |
parser.error("Not enough arguments!") | |
# this is a good place to check any argument and options for | |
# sanity, so you can barf out the parser help message. | |
# unpack the real arguments that you want here | |
name, release, candidate = args | |
try: | |
# if there are "expected" exceptions, such as NoSuchFile, | |
# NoSuchTag, make up your own exception classes and raise them | |
# from the cli function, and catch them here, then return the | |
# appropriate code if any. | |
cli(options, release, candidate) | |
except KeyboardInterrupt, ki: | |
return 130 | |
except Fault, xmlf: | |
print >> sys.stderr, xmlf.faultString | |
return -1 | |
except gaierror, dns: | |
print >> sys.stderr, dns.message | |
print >> sys.stderr, "Try using --koji with an IP address" | |
return -2 | |
except ForgotMyWork, fml: | |
print >> sys.stderr, fml | |
else: | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) | |
# | |
# The end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some things worth considering; if there is a lot of output, it could be worth setting the debug and verbose functions to be at the top level.