1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15  import os 
 16  import code 
 17  import atexit 
 18  from optparse import OptionParser 
 19  try: 
 20      import readline 
 21      import rlcompleter 
 22  except ImportError: 
 23      readline = rlcompleter = None 
 24   
 25   
 26   
 27  parser = OptionParser() 
 28  parser.add_option('--host', 
 29              dest="host",default=None, 
 30              help="hostname of zeo server") 
 31  parser.add_option('--port', 
 32              dest="port",type="int", default=None, 
 33              help="port of zeo server") 
 34  opts, args = parser.parse_args() 
 35   
 36   
 37  import Zope2 
 38  CONF_FILE = os.path.join(os.environ['ZENHOME'], 'etc', 'zope.conf') 
 39  Zope2.configure(CONF_FILE) 
 40   
 41   
 42  from Products.CMFCore.utils import getToolByName 
 43  from AccessControl.SecurityManagement import newSecurityManager 
 44  from AccessControl.SecurityManagement import noSecurityManager 
 45  from Products.ZenUtils.Utils import zenPath 
 46   
 47  _CUSTOMSTUFF = [] 
 48   
 49   
 51       
 52      from App.config import getConfiguration 
 53      serverconfig = getConfiguration().databases[1].config.storage.config 
 54      xhost, xport = serverconfig.server[0].address 
 55      if host: xhost = host 
 56      if port: xport = port 
 57      serverconfig.server[0].address = (xhost, xport) 
  58   
 60      from ZPublisher.HTTPRequest import HTTPRequest 
 61      from ZPublisher.HTTPResponse import HTTPResponse 
 62      from ZPublisher.BaseRequest import RequestContainer 
 63      resp = HTTPResponse(stdout=None) 
 64      env = { 
 65          'SERVER_NAME':'localhost', 
 66          'SERVER_PORT':'8080', 
 67          'REQUEST_METHOD':'GET' 
 68          } 
 69      req = HTTPRequest(None, env, resp) 
 70      return ob.__of__(RequestContainer(REQUEST = req)) 
  71   
 72   
 74      """ 
 75      Everything available in the console is defined here. 
 76      """ 
 77   
 78      import socket 
 79      from transaction import commit 
 80      from pprint import pprint 
 81   
 82       
 83      app = Zope2.app() 
 84      app = set_context(app) 
 85   
 86      def login(username='admin'): 
 87          utool = getToolByName(app, 'acl_users') 
 88          user = utool.getUserById(username) 
 89          if user is None: 
 90              user = app.zport.acl_users.getUserById(username) 
 91          user = user.__of__(utool) 
 92          newSecurityManager(None, user) 
  93   
 94      login('admin') 
 95   
 96       
 97      zport = app.zport 
 98      dmd   = zport.dmd 
 99      sync  = zport._p_jar.sync 
100      find  = dmd.Devices.findDevice 
101      devices = dmd.Devices 
102      me = find(socket.getfqdn()) 
103   
104      def reindex(): 
105          sync() 
106          dmd.Devices.reIndex() 
107          dmd.Events.reIndex() 
108          dmd.Manufacturers.reIndex() 
109          dmd.Networks.reIndex() 
110          commit() 
111   
112      def logout(): 
113          noSecurityManager() 
114   
115      def zhelp(): 
116          cmds = filter(lambda x: not x.startswith("_"), _CUSTOMSTUFF) 
117          cmds.sort() 
118          for cmd in cmds: print cmd 
119   
120      def grepdir(obj, regex=""): 
121          if regex: 
122              import re 
123              pattern = re.compile(regex) 
124              for key in dir(obj): 
125                  if pattern.search(key): 
126                      print key 
127   
128      def cleandir(obj): 
129          portaldir = set(dir(dmd)) 
130          objdir = set(dir(obj)) 
131          appdir = set(dir(app)) 
132          result = list(objdir - portaldir - appdir) 
133          result.sort() 
134          pprint(result) 
135   
136   
137      _CUSTOMSTUFF = locals() 
138      return _CUSTOMSTUFF 
139   
140   
141 -class HistoryConsole(code.InteractiveConsole): 
 142      """ 
143      Subclass the default InteractiveConsole to get readline history 
144      """ 
145 -    def __init__(self, locals=None, filename="<console>", 
146                   histfile=zenPath('.pyhistory')): 
 147          code.InteractiveConsole.__init__(self, locals, filename) 
148          self.init_history(histfile) 
 149   
150 -    def init_history(self, histfile): 
 151          if hasattr(readline, "read_history_file"): 
152              try: 
153                  readline.read_history_file(histfile) 
154              except IOError: 
155                  pass 
156              atexit.register(self.save_history, histfile) 
 157   
158 -    def save_history(self, histfile): 
 159          readline.write_history_file(histfile) 
  160   
161   
162  if __name__=="__main__": 
163       
164       
165      if opts.host or opts.port: 
166          set_db_config(opts.host, opts.port) 
167   
168      _banner=("Welcome to the Zenoss dmd command shell!\n" 
169               "'dmd' is bound to the DataRoot. 'zhelp()' to get a list of " 
170               "commands.") 
171   
172       
173      myconsole = HistoryConsole(locals=_customStuff()) 
174      myconsole.interact(_banner) 
175