Mercurial > p > roundup > code
comparison roundup-admin @ 371:f348aa576d51
roundup-admin now accepts abbreviated commands (eg. l = li = lis = list)
[thanks Engelbert Gruber for the inspiration]
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 08 Nov 2001 04:29:59 +0000 |
| parents | f90abe9e811d |
| children | 3b9410cb8b61 |
comparison
equal
deleted
inserted
replaced
| 370:745f9cacfba0 | 371:f348aa576d51 |
|---|---|
| 14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 15 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" | 15 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" |
| 16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, | 16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 18 # | 18 # |
| 19 # $Id: roundup-admin,v 1.38 2001-11-05 23:45:40 richard Exp $ | 19 # $Id: roundup-admin,v 1.39 2001-11-08 04:29:59 richard Exp $ |
| 20 | 20 |
| 21 import sys | 21 import sys |
| 22 if int(sys.version[0]) < 2: | 22 if int(sys.version[0]) < 2: |
| 23 print 'Roundup requires python 2.0 or later.' | 23 print 'Roundup requires python 2.0 or later.' |
| 24 sys.exit(1) | 24 sys.exit(1) |
| 25 | 25 |
| 26 import string, os, getpass, getopt, re | 26 import string, os, getpass, getopt, re, UserDict |
| 27 try: | 27 try: |
| 28 import csv | 28 import csv |
| 29 except ImportError: | 29 except ImportError: |
| 30 csv = None | 30 csv = None |
| 31 from roundup import date, hyperdb, roundupdb, init, password | 31 from roundup import date, hyperdb, roundupdb, init, password |
| 32 import roundup.instance | 32 import roundup.instance |
| 33 | 33 |
| 34 class CommandDict(UserDict.UserDict): | |
| 35 '''Simple dictionary that lets us do lookups using partial keys. | |
| 36 | |
| 37 Original code submitted by Engelbert Gruber. | |
| 38 ''' | |
| 39 _marker = [] | |
| 40 def get(self, key, default=_marker): | |
| 41 d = self.data.get(key, default) | |
| 42 if d is not default: return [(key, d)] | |
| 43 keylist = self.data.keys() | |
| 44 keylist.sort() | |
| 45 l = [] | |
| 46 for ki in keylist: | |
| 47 if ki.startswith(key): | |
| 48 l.append((ki, self.data[ki])) | |
| 49 if not l and default is self._marker: | |
| 50 raise KeyError, key | |
| 51 return l | |
| 52 | |
| 34 class AdminTool: | 53 class AdminTool: |
| 35 | 54 |
| 36 def __init__(self): | 55 def __init__(self): |
| 37 self.commands = {} | 56 self.commands = CommandDict() |
| 38 for k in AdminTool.__dict__.keys(): | 57 for k in AdminTool.__dict__.keys(): |
| 39 if k[:3] == 'do_': | 58 if k[:3] == 'do_': |
| 40 self.commands[k[3:]] = getattr(self, k) | 59 self.commands[k[3:]] = getattr(self, k) |
| 41 self.help = {} | 60 self.help = {} |
| 42 for k in AdminTool.__dict__.keys(): | 61 for k in AdminTool.__dict__.keys(): |
| 129 commands -- list commands | 148 commands -- list commands |
| 130 <command> -- help specific to a command | 149 <command> -- help specific to a command |
| 131 initopts -- init command options | 150 initopts -- init command options |
| 132 all -- all available help | 151 all -- all available help |
| 133 ''' | 152 ''' |
| 134 help = self.help.get(args[0], None) | 153 topic = args[0] |
| 135 if help: | 154 |
| 136 help() | 155 # try help_ methods |
| 156 if self.help.has_key(topic): | |
| 157 self.help[topic]() | |
| 137 return | 158 return |
| 138 help = self.commands.get(args[0], None) | 159 |
| 139 if help: | 160 # try command docstrings |
| 140 # display the help, removing the docsring indent | 161 try: |
| 162 l = self.commands.get(topic) | |
| 163 except KeyError: | |
| 164 print 'Sorry, no help for "%s"'%topic | |
| 165 return | |
| 166 | |
| 167 # display the help for each match, removing the docsring indent | |
| 168 for name, help in l: | |
| 141 lines = nl_re.split(help.__doc__) | 169 lines = nl_re.split(help.__doc__) |
| 142 print lines[0] | 170 print lines[0] |
| 143 indent = indent_re.match(lines[1]) | 171 indent = indent_re.match(lines[1]) |
| 144 if indent: indent = len(indent.group(1)) | 172 if indent: indent = len(indent.group(1)) |
| 145 for line in lines[1:]: | 173 for line in lines[1:]: |
| 146 if indent: | 174 if indent: |
| 147 print line[indent:] | 175 print line[indent:] |
| 148 else: | 176 else: |
| 149 print line | 177 print line |
| 150 else: | |
| 151 print 'Sorry, no help for "%s"'%args[0] | |
| 152 | 178 |
| 153 def help_initopts(self): | 179 def help_initopts(self): |
| 154 import roundup.templates | 180 import roundup.templates |
| 155 templates = roundup.templates.listTemplates() | 181 templates = roundup.templates.listTemplates() |
| 156 print 'Templates:', ', '.join(templates) | 182 print 'Templates:', ', '.join(templates) |
| 599 self.do_help(['help']) | 625 self.do_help(['help']) |
| 600 self.help_commands() | 626 self.help_commands() |
| 601 self.help_all() | 627 self.help_all() |
| 602 return 0 | 628 return 0 |
| 603 | 629 |
| 630 # figure what the command is | |
| 631 try: | |
| 632 functions = self.commands.get(command) | |
| 633 except KeyError: | |
| 634 # not a valid command | |
| 635 print 'Unknown command "%s" ("help commands" for a list)'%command | |
| 636 return 1 | |
| 637 | |
| 638 # check for multiple matches | |
| 639 if len(functions) > 1: | |
| 640 print 'Multiple commands match "%s": %s'%(command, | |
| 641 ', '.join([i[0] for i in functions])) | |
| 642 return 1 | |
| 643 command, function = functions[0] | |
| 644 | |
| 604 # make sure we have an instance_home | 645 # make sure we have an instance_home |
| 605 while not self.instance_home: | 646 while not self.instance_home: |
| 606 self.instance_home = raw_input('Enter instance home: ').strip() | 647 self.instance_home = raw_input('Enter instance home: ').strip() |
| 607 | 648 |
| 608 # before we open the db, we may be doing an init | 649 # before we open the db, we may be doing an init |
| 609 if command == 'init': | 650 if command == 'init': |
| 610 return self.do_init(self.instance_home, args) | 651 return self.do_init(self.instance_home, args) |
| 611 | |
| 612 function = self.commands.get(command, None) | |
| 613 | |
| 614 # not a valid command | |
| 615 if function is None: | |
| 616 print 'Unknown command "%s" ("help commands" for a list)'%command | |
| 617 return 1 | |
| 618 | 652 |
| 619 # get the instance | 653 # get the instance |
| 620 instance = roundup.instance.open(self.instance_home) | 654 instance = roundup.instance.open(self.instance_home) |
| 621 self.db = instance.open('admin') | 655 self.db = instance.open('admin') |
| 622 | 656 |
| 685 tool = AdminTool() | 719 tool = AdminTool() |
| 686 sys.exit(tool.main()) | 720 sys.exit(tool.main()) |
| 687 | 721 |
| 688 # | 722 # |
| 689 # $Log: not supported by cvs2svn $ | 723 # $Log: not supported by cvs2svn $ |
| 724 # Revision 1.38 2001/11/05 23:45:40 richard | |
| 725 # Fixed newuser_action so it sets the cookie with the unencrypted password. | |
| 726 # Also made it present nicer error messages (not tracebacks). | |
| 727 # | |
| 690 # Revision 1.37 2001/10/23 01:00:18 richard | 728 # Revision 1.37 2001/10/23 01:00:18 richard |
| 691 # Re-enabled login and registration access after lopping them off via | 729 # Re-enabled login and registration access after lopping them off via |
| 692 # disabling access for anonymous users. | 730 # disabling access for anonymous users. |
| 693 # Major re-org of the htmltemplate code, cleaning it up significantly. Fixed | 731 # Major re-org of the htmltemplate code, cleaning it up significantly. Fixed |
| 694 # a couple of bugs while I was there. Probably introduced a couple, but | 732 # a couple of bugs while I was there. Probably introduced a couple, but |
