diff options
author | Rob Austein <sra@hactrn.net> | 2010-03-02 22:33:02 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2010-03-02 22:33:02 +0000 |
commit | 5d9ae5cf4f9087d5d146e04372d6369bc8692568 (patch) | |
tree | 26ea8601f49d5ebb2ddf8deb46378be0e52ca9f1 | |
parent | db13040f9ac9f91170198fc425566488a3d2762e (diff) |
Convert setup.py to use cmd module.
svn path=/myrpki.rototill/setup.py; revision=3013
-rw-r--r-- | myrpki.rototill/setup.py | 96 | ||||
-rw-r--r-- | myrpki.rototill/test-cmd.py | 90 |
2 files changed, 60 insertions, 126 deletions
diff --git a/myrpki.rototill/setup.py b/myrpki.rototill/setup.py index c2f98876..2c8647d3 100644 --- a/myrpki.rototill/setup.py +++ b/myrpki.rototill/setup.py @@ -16,24 +16,25 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ -import subprocess, csv, re, os, getopt, sys, base64, time, myrpki, rpki.config +import subprocess, csv, re, os, getopt, sys, base64, time, cmd, readline, glob +import myrpki, rpki.config from xml.etree.ElementTree import Element, SubElement, ElementTree -def usage(code = 1): - print __doc__ - sys.exit(code) +class main(cmd.Cmd): -class main(object): + prompt = "setup> " - def __init__(self): + identchars = cmd.IDENTCHARS + "/-." + def __init__(self): + cmd.Cmd.__init__(self) os.environ["TZ"] = "UTC" time.tzset() self.cfg_file = "myrpki.conf" - opts, self.argv = getopt.getopt(sys.argv[1:], "c:h?", ["config=", "help"]) + opts, argv = getopt.getopt(sys.argv[1:], "c:h?", ["config=", "help"]) for o, a in opts: if o in ("-c", "--config"): self.cfg_file = a @@ -44,10 +45,6 @@ class main(object): self.cfg = rpki.config.parser(self.cfg_file, "myrpki") myrpki.openssl = self.cfg.get("openssl", "openssl") - getattr(self, self.argv.pop(0) + "_main")() - - def common(self, initialize_bpki = False): - self.handle = self.cfg.get("handle") self.run_rpkid = self.cfg.getboolean("run_rpkid") self.run_pubd = self.cfg.getboolean("run_pubd") @@ -57,22 +54,53 @@ class main(object): raise RuntimeError, "Can't run rootd unless also running rpkid and pubd" self.bpki_myrpki = myrpki.CA(self.cfg_file, self.cfg.get("myrpki_bpki_directory")) - - if initialize_bpki: - self.bpki_myrpki.setup(self.cfg.get("bpki_myrpki_ta_dn", - "/CN=%s BPKI Resource Trust Anchor" % self.handle)) - if self.run_rpkid or self.run_pubd or self.run_rootd: - self.bpki_myirbe = myrpki.CA(self.cfg_file, self.cfg.get("myirbe_bpki_directory")) - if initialize_bpki: - self.bpki_myirbe.setup(self.cfg.get("bpki_myirbe_ta_dn", - "/CN=%s BPKI Server Trust Anchor" % self.handle)) - - def initialize_main(self): - - self.common(initialize_bpki = True) + if argv: + self.onecmd(" ".join(argv)) + else: + self.cmdloop_with_history() + + def completedefault(self, text, line, begidx, endidx): + return glob.glob(text + "*") + + def cmdloop_with_history(self): + old_completer_delims = readline.get_completer_delims() + histfile = self.cfg.get("history_file", ".setup_history") + try: + readline.read_history_file(histfile) + except IOError: + pass + try: + readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars))) + self.cmdloop() + finally: + if readline.get_current_history_length(): + readline.write_history_file(histfile) + readline.set_completer_delims(old_completer_delims) + + def do_EOF(self, arg): + print + return True + + def do_exit(self, arg): + """ + Exit program + """ + return True + + do_quit = do_exit + + def emptyline(self): + pass + + def do_initialize(self, arg): + self.bpki_myrpki.setup(self.cfg.get("bpki_myrpki_ta_dn", + "/CN=%s BPKI Resource Trust Anchor" % self.handle)) + if self.run_rpkid or self.run_pubd or self.run_rootd: + self.bpki_myirbe.setup(self.cfg.get("bpki_myirbe_ta_dn", + "/CN=%s BPKI Server Trust Anchor" % self.handle)) # Create directories for parents, children, and repositories. # Directory names should become configurable (later). @@ -134,24 +162,22 @@ class main(object): if not os.path.exists(rootd_child_fn): os.link(self.bpki_myirbe.xcert(self.bpki_myrpki.cer), rootd_child_fn) - def from_child_main(self): - - self.common() + def do_from_child(self, arg): child_handle = None - opts, self.argv = getopt.getopt(self.argv, "", ["child_handle="]) + opts, argv = getopt.getopt(arg.split(), "", ["child_handle="]) for o, a in opts: if o == "--child_handle": child_handle = a - if len(self.argv) != 1 or not os.path.exists(self.argv[0]): + if len(argv) != 1 or not os.path.exists(argv[0]): raise RuntimeError, "Need to specify filename for child.xml on command line" if not self.run_rpkid: raise RuntimeError, "Don't (yet) know how to set up child unless we run rpkid" - c = ElementTree(file = self.argv[0]).getroot() + c = ElementTree(file = argv[0]).getroot() if child_handle is None: child_handle = c["handle"] @@ -178,24 +204,22 @@ class main(object): myrpki.etree_write(e, "children/%s.xml" % child_handle) - def from_parent_main(self): - - self.common() + def do_from_parent(self, arg): parent_handle = None repository_handle = None - opts, self.argv = getopt.getopt(self.argv, "", ["parent_handle", "repository_handle"]) + opts, argv = getopt.getopt(arg.split(), "", ["parent_handle", "repository_handle"]) for o, a in opts: if o == "--parent_handle": parent_handle = a elif o == "--repository_handle": repository_handle = a - if len(self.argv) != 1 or not os.path.exists(self.argv[0]): + if len(argv) != 1 or not os.path.exists(argv[0]): raise RuntimeError, "Ned to specify filename for parent.xml on command line" - p = ElementTree(file = self.argv[0]).getroot() + p = ElementTree(file = argv[0]).getroot() if parent_handle is None: parent_handle = p["parent_handle"] diff --git a/myrpki.rototill/test-cmd.py b/myrpki.rototill/test-cmd.py deleted file mode 100644 index 56b9a0dd..00000000 --- a/myrpki.rototill/test-cmd.py +++ /dev/null @@ -1,90 +0,0 @@ -""" -Test of the cmd module. If given command line arguments, run them as -a single command, otherwise go into a command loop, with readline and -history and emacs keys and everything. Whee! - -$Id$ - -Copyright (C) 2010 Internet Systems Consortium ("ISC") - -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE -OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. -""" - -import cmd, readline, sys, glob - -class main(cmd.Cmd): - - prompt = "wibble> " - - identchars = cmd.IDENTCHARS + "/-." - - def do_tweedledee(self, arg): - """ - Tweedledee said Tweeldedum had spoiled his nice new rattle. - """ - print "Dee", arg - - def do_tweedledum(self, arg): - """ - Tweedledum and Tweedledee agreed to have a battle. - """ - print "Dum", arg - - def do_EOF(self, arg): - print - return True - - def do_exit(self, arg): - """ - Exit program - """ - return True - - do_quit = do_exit - - def emptyline(self): - pass - - def do_tarbarrel(self, arg): - """ - Just then flew down a monsterous crow as black as a tarbarrel. - """ - print "Quite forgot their quarrel" - - def completedefault(self, text, line, begidx, endidx): - return glob.glob(text + "*") - - def cmdloop_with_history(self, histfile): - old_completer_delims = readline.get_completer_delims() - try: - readline.read_history_file(histfile) - except IOError: - pass - try: - readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars))) - self.cmdloop() - finally: - if readline.get_current_history_length(): - readline.write_history_file(histfile) - readline.set_completer_delims(old_completer_delims) - - def __init__(self): - cmd.Cmd.__init__(self) - argv = sys.argv[1:] - if argv: - self.onecmd(" ".join(argv)) - else: - self.cmdloop_with_history(".wibble_history") - -if __name__ == "__main__": - main() |