1
2
3
4
5
6
7
8
9
10
11 """ZenScriptBase
12
13 Scripts with classes who extend ZenScriptBase have a zope instance with a
14 dmd root and loaded ZenPacks, like zendmd.
15 """
16
17 from zope.component import getUtility
18
19 from AccessControl.SecurityManagement import newSecurityManager
20 from AccessControl.SecurityManagement import noSecurityManager
21 from transaction import commit
22 from Products.ZenUtils.Utils import getObjByPath, zenPath, set_context
23 from Products.ZenUtils.CmdBase import CmdBase
24 from Products.ZenUtils.ZodbFactory import IZodbFactoryLookup
25
26 from Products.ZenRelations.ZenPropertyManager import setDescriptors
27 from Products.ZenUtils.Exceptions import ZentinelException
28
29 defaultCacheDir = zenPath('var')
30
32
34
35 - def __init__(self, noopts=0, app=None, connect=False):
42
53
54
55 - def login(self, name='admin', userfolder=None):
56 """Logs in."""
57 if userfolder is None:
58 userfolder = self.app.acl_users
59 user = userfolder.getUserById(name)
60 if user is None: return
61 if not hasattr(user, 'aq_base'):
62 user = user.__of__(userfolder)
63 newSecurityManager(None, user)
64
65
67 """Logs out."""
68 noSecurityManager()
69
70
84
85
87 """Close all connections in both free an inuse pools.
88 """
89 self.db.close()
90
91
99
100
102 self.connection.sync()
103
104
106 self.connection.close()
107
108 self.app = None
109 self.dataroot = None
110 self.dmd = None
111
112
114 if not self.app: self.opendb()
115 if not self.dataroot:
116 self.dataroot = getObjByPath(self.app, self.options.dataroot)
117 self.dmd = self.dataroot
118
119
121 """return an object based on a path starting from the dmd"""
122 return getObjByPath(self.app, self.options.dataroot+path)
123
124
126 """return a device based on its FQDN"""
127 devices = self.dataroot.getDmdRoot("Devices")
128 return devices.findDevice(name)
129
130
137