def tearDownApp(self): """Undo Zope 2 startup by unsetting the global state it creates. """ import Zope2 Zope2.app()._p_jar.close() Zope2._began_startup = 0 Zope2.DB = None Zope2.bobo_application = None Zope2.zpublisher_transactions_manager = None Zope2.zpublisher_validated_hook = None Zope2.zpublisher_exception_hook = None Zope2.__bobo_before__ = None import App.config try: self._testingHome except AttributeError: pass else: config = App.config.getConfiguration() config.testinghome = self._testingHome App.config.setConfiguration(config) del self._testingHome import ZPublisher.WSGIPublisher ZPublisher.WSGIPublisher._MODULES.clear() for k, v in self._publisher_globals.items(): setattr(ZPublisher.WSGIPublisher, k, v)
def main(app=None, args=None): full_args = args if args is not None: full_args = args + sys.argv[1:] args = parser.parse_args(full_args) if args.upgrade_profiles: args.upgrade_all_profiles = False elif not (args.upgrade_portal or args.upgrade_all_profiles): parser.error('The supplied options would not upgrade any profiles') if app is None: import Zope2 from App import config if config._config is None: if not args.zope_conf: parser.error( 'Must give the "--zope-conf" option when not used as a ' 'zopectl "run" script.') Zope2.configure(args.zope_conf) app = Zope2.app() elif args.zope_conf: parser.error( 'Do not give the "--zope-conf" option when used as a ' 'zopectl "run" script.') root = logging.getLogger() root.setLevel(logging.INFO) stderr_handler, = [h for h in root.handlers if getattr(h, 'stream', None) is sys.__stderr__] stderr_handler.setLevel(logging.INFO) stderr_handler.addFilter(zodbupdate.main.duplicate_filter) log_file = logging.FileHandler(args.log_file) log_file.addFilter(zodbupdate.main.duplicate_filter) log_file.setFormatter(utils.formatter) root.addHandler(log_file) kw = dict( paths=args.portal_paths, upgrade_portal=args.upgrade_portal, upgrade_all_profiles=args.upgrade_all_profiles, upgrade_profiles=args.upgrade_profiles) if args.disable_link_integrity: kw['enable_link_integrity_checks'] = False from AccessControl import SpecialUsers from AccessControl.SecurityManagement import newSecurityManager newSecurityManager(None, SpecialUsers.system) from Testing.makerequest import makerequest app = makerequest(app) runner = app.restrictedTraverse('@@collective.upgrade.form') try: runner.upgrade(**kw) except: transaction.abort() runner.logger.exception('Exception running the upgrades.') pdb.post_mortem(sys.exc_info()[2]) raise
def process(name, swhome, ihome): admin_username = name os.environ['INSTANCE_HOME'] = ihome if sys.platform == 'win32': sys.path.insert(0, os.path.join(swhome, 'Zope', 'lib', 'python')) sys.path.insert(1, os.path.join(swhome, 'Python', 'lib')) sys.path.insert(2, swhome) else: os.environ['SOFTWARE_HOME'] = swhome sys.path.insert(0, swhome) # have to set up env first try: # Zope 2.8 on, Zope is now 'Zope2' and 'zope' is the Zope 3 # libs. import Zope2 as Zope except ImportError: import Zope configfile = os.path.join(ihome, 'etc', 'zope.conf') # nuke remaining command line arguments sys.argv = sys.argv[:1] # for 2.7 run configure Zope.configure(configfile) app = Zope.app() from Testing import makerequest app = makerequest.makerequest(app) from Products.CMFPlone.PloneInitialize import create out = create(app, admin_username) return out
def tearDownApp(self): """Undo Zope 2 startup by unsetting the global state it creates. """ import Zope2 Zope2.app()._p_jar.close() Zope2._began_startup = 0 Zope2.DB = None Zope2.bobo_application = None Zope2.zpublisher_transactions_manager = None Zope2.zpublisher_validated_hook = None Zope2.zpublisher_exception_hook = None Zope2.__bobo_before__ = None import App.config if hasattr(self, '_testingHome'): config = App.config.getConfiguration() config.testinghome = self._testingHome App.config.setConfiguration(config) del self._testingHome # Clear out the app reference cached in get_module_info's # 'modules' parameter default dict. (waaaaa) import ZPublisher.Publish defaults = ZPublisher.Publish.get_module_info.func_defaults if defaults: d = list(defaults) d[0] = {} ZPublisher.Publish.get_module_info.func_defaults = tuple(d)
def main(args=None): lib_python = find_lib_python() sys.path.insert(0, lib_python) if args is None: args = sys.argv[1:] if not args: print __doc__ sys.exit(0) print args random.seed(hash(tuple(args))) # always use the same for the given args options = {"mbox": None, "threads": None} jobdefs = [] collect_options(args, jobdefs, options) mboxes = {} if options["mbox"]: mboxes[options["mbox"]] = MBox(options["mbox"]) # Perform a ZConfig-based Zope initialization: zetup(os.path.join(lib_python, '..', '..', 'etc', 'zope.conf')) if options.has_key('setup'): setup(lib_python) else: import Zope2 Zope2.startup() jobs = JobProducer() for job, kw, frequency, sleep, repeatp in jobdefs: Job = globals()[job.capitalize()+'Job'] if getattr(Job, 'needs_mbox', 0): if not kw.has_key("mbox"): if not options["mbox"]: raise ValueError( "no mailbox (mbox option) file specified") kw['mbox'] = mboxes[options["mbox"]] else: if not mboxes.has_key[kw["mbox"]]: mboxes[kw['mbox']] = MBox[kw['mbox']] kw["mbox"] = mboxes[kw['mbox']] jobs.add(Job(**kw), frequency, sleep, repeatp) if not jobs: print "No jobs to execute" return threads = int(options['threads'] or '0') if threads > 1: threads = [threading.Thread(target=run, args=(jobs, i), name=str(i)) for i in range(threads)] for thread in threads: thread.start() for thread in threads: thread.join() else: run(jobs)
def _start_zope(): # Startup Zope 2.7 or 2.8+ import Testing try: import Zope2 as Zope except ImportError: import Zope Zope.startup()
def getApp(*args, **kwargs): if Zope2.bobo_application is None: orig_argv = sys.argv sys.argv = [''] res = Zope2.app(*args, **kwargs) sys.argv = orig_argv return res # should set bobo_application # man, freaking zope2 is weird return Zope2.bobo_application(*args, **kwargs)
def get_stuff(): # Zope magic ensues! import Zope2 CONF_FILE = os.path.join(os.environ['ZENHOME'], 'etc', 'zope.conf') # hide any positional arguments during Zope2 configure _argv = sys.argv sys.argv = [sys.argv[0], ] + [x for x in sys.argv[1:] if x.startswith("-")] Zope2.configure(CONF_FILE) sys.argv = _argv # restore normality from Products.ZenModel.zendmd import _customStuff return _customStuff()
def getZope2App(*args, **kwargs): """Gets the Zope2 app. Copied almost verbatim from collective.celery """ if Zope2.bobo_application is None: orig_argv = sys.argv sys.argv = [''] res = Zope2.app(*args, **kwargs) sys.argv = orig_argv return res # should set bobo_application # man, freaking zope2 is weird return Zope2.bobo_application(*args, **kwargs)
def getApp(*args, **kwargs): try: return _local.app except AttributeError: pass if Zope2.bobo_application is None: orig_argv = sys.argv sys.argv = [''] res = Zope2.app(*args, **kwargs) sys.argv = orig_argv return res # should set bobo_application # man, freaking zope2 is weird return Zope2.bobo_application(*args, **kwargs)
def getNodeText(self, jid, node): app = Zope2.app() text = '' try: portal = app.unrestrictedTraverse(self.portal_id, None) if portal is None: raise DSCException( 'Portal with id %s not found' % self.portal_id) setSite(portal) acl_users = getToolByName(portal, 'acl_users') user_id = unescapeNode(JID(jid).user) user = acl_users.getUserById(user_id) if user is None: raise DSCException( 'Invalid user %s' % user_id) newSecurityManager(None, user) ct = getToolByName(portal, 'portal_catalog') uid, html_id = node.split('#') item = ct.unrestrictedSearchResults(UID=uid) if not item: raise DSCException( 'Content with UID %s not found' % uid) item = ICollaborativelyEditable(item[0].getObject()) text = item.getNodeTextFromHtmlID(html_id) finally: noSecurityManager() setSite(None) return text
def setUp(self): self._trap_warning_output() zcml.load_config('meta.zcml', Products.Five) zcml.load_config('configure.zcml', Products.Five) zcml.load_config('configure.zcml', Products.GenericSetup) zcml.load_config('configure.zcml', Products.CMFCore) zcml.load_config('configure.zcml', Products.CMFDefault) zcml.load_config('configure.zcml', Products.DCWorkflow) self._oldSkindata = Skinnable.SKINDATA.copy() transaction.begin() app = self.app = makerequest(Zope2.app()) # Log in as a god :-) newSecurityManager(None, UnrestrictedUser('god', 'god', ['Manager'], '') ) factory = app.manage_addProduct['CMFDefault'].addConfiguredSite factory('CalendarTest', 'CMFDefault:default', snapshot=False, extension_ids=('CMFCalendar:default',)) self.Site = app.CalendarTest self.Tool = app.CalendarTest.portal_calendar # sessioning setup if getattr(app, 'temp_folder', None) is None: temp_folder = MountedTemporaryFolder('temp_folder') app._setObject('temp_folder', temp_folder) if getattr(app.temp_folder, 'session_data', None) is None: session_data = TransientObjectContainer('session_data') app.temp_folder._setObject('session_data', session_data) app.REQUEST.set_lazy( 'SESSION', app.session_data_manager.getSessionData )
def _registerPackage(module_, init_func=None): """Registers the given python package as a Zope 2 style product """ if not hasattr(module_, '__path__'): raise ValueError("Must be a package and the " \ "package must be filesystem based") app = Zope2.app() try: product = initializeProduct(module_, module_.__name__, module_.__path__[0], app) product.package_name = module_.__name__ if init_func is not None: newContext = ProductContext(product, app, module_) init_func(newContext) finally: try: import transaction transaction.commit() finally: app._p_jar.close()
def __call__(self, config_file, run_as, server_url=None, context_path=None, portal_id=None, **environ): Zope2.Startup.run.configure(config_file) environ['SERVER_URL'] = server_url self.app = makerequest(Zope2.app(), environ=environ) setHooks() if portal_id is not None: self.portal = self.app[portal_id] else: portals = self.app.objectValues('Plone Site') if len(portals) > 1: log.warn('More than one portal - using first one') self.portal = portals[0] setSite(self.portal) self.app.REQUEST.other['PARENTS'] = [self.portal, self.app] self.app.REQUEST.other['VirtualRootPhysicalPath'] = ( '', self.portal.id) log.setLevel(logging.INFO) handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler.setFormatter(formatter) log.addHandler(handler) with api.env.adopt_user(username=run_as): if context_path is not None: self.context = self.portal.restrictedTraverse(context_path) else: self.context = self.portal self.run() transaction.commit()
def hist(n): import Zope2 app=Zope2.app() import cPickle pickler=cPickle.Pickler(open("h%s.hist" % n, 'w')) h=app.cat._catalog.indexes['PrincipiaSearchSource'].histogram() pickler.dump(list(h.items()))
def testSetUp(self): """Do not mess up here with another stacked demo storage!!!""" import Zope2 environ = { 'SERVER_NAME': 'localhost', 'SERVER_PORT': str(self['port']), } app = z2.addRequestContainer( Zope2.app(), environ=environ) request = app.REQUEST request['PARENTS'] = [app] # Make sure we have a zope.globalrequest request try: from zope.globalrequest import setRequest setRequest(request) except ImportError: pass self['app'] = app self['request'] = request self['portal'] = portal = self['app'][PLONE_SITE_ID] transaction.commit() self.setUpEnvironment(portal) transaction.commit() LayerMixin.testSetUp(self) transaction.commit()
def setUp(self): transaction.begin() self.app = makerequest(Zope2.app()) f = self.app.manage_addProduct['PageTemplates'].manage_addPageTemplate self._addPT = f self.title = 'title of page template' self.text = 'text of page template'
def setUp(self): self._trap_warning_output() transaction.begin() app = self.app = makerequest(Zope2.app()) # Log in as a god :-) newSecurityManager( None, UnrestrictedUser('god', 'god', ['Manager'], '') ) app.manage_addProduct['CMFDefault'].manage_addCMFSite('CalendarTest') self.Site = app.CalendarTest manage_addExternalMethod(app.CalendarTest, id='install_events', title="Install Events", module="CMFCalendar.Install", function="install") ExMethod = app.restrictedTraverse('/CalendarTest/install_events') ExMethod() self.Tool = app.CalendarTest.portal_calendar self.Site.clearCurrentSkin() self.Site.setupCurrentSkin(app.REQUEST) # sessioning setup if getattr(app, 'temp_folder', None) is None: temp_folder = MountedTemporaryFolder('temp_folder') app._setObject('temp_folder', temp_folder) if getattr(app.temp_folder, 'session_data', None) is None: session_data = TransientObjectContainer('session_data') app.temp_folder._setObject('session_data', session_data) app.REQUEST.set_lazy( 'SESSION', app.session_data_manager.getSessionData )
def main(): import Zope2 app = Zope2.app() sys.argv = [__file__] + sys.argv[3:] options, args = parser.parse_args() options.app = app import_site(options)
def setNodeText(self, jid, node, text): transaction.begin() app = Zope2.app() try: try: portal = app.unrestrictedTraverse(self.portal_id, None) if portal is None: raise DSCException( 'Portal with id %s not found' % self.portal_id) setSite(portal) acl_users = getToolByName(portal, 'acl_users') user_id = JID(jid).user user = acl_users.getUserById(user_id) if user is None: raise DSCException( 'Invalid user %s' % user_id) newSecurityManager(None, user) ct = getToolByName(portal, 'portal_catalog') uid, html_id = node.split('#') item = ct.unrestrictedSearchResults(UID=uid) if not item: raise DSCException( 'Content with UID %s not found' % uid) item = ICollaborativelyEditable(item[0].getObject()) item.setNodeTextFromHtmlID(html_id, text) transaction.commit() except: transaction.abort() raise finally: noSecurityManager() setSite(None) app._p_jar.close() return text
def product_packages(): """Returns all product packages including the regularly defined zope2 packages and those without the Products namespace package. """ old_product_packages = {} for x in dir(Products): m = getattr(Products, x) if isinstance(m, types.ModuleType): old_product_packages[x] = m packages = {} app = Zope2.app() try: products = app.Control_Panel.Products for product_id in products.objectIds(): product = products[product_id] if hasattr(product, 'package_name'): pos = product.package_name.rfind('.') if pos > -1: packages[product_id] = __import__(product.package_name, globals(), {}, product.package_name[pos+1:]) else: packages[product_id] = __import__(product.package_name) elif old_product_packages.has_key(product_id): packages[product_id] = old_product_packages[product_id] finally: try: app._p_jar.close() except: # don't fret if the app connection is attached to an ongoing session pass return packages
def __init__(self): self.instancehome = os.environ.get("INSTANCE_HOME") configfile = os.environ.get("CONFIG_FILE") if configfile is None and self.instancehome is not None: configfile = os.path.join(self.instancehome, "etc", "zope.conf") if configfile is None: raise RuntimeError("CONFIG_FILE env not set") print "CONFIG_FILE=", configfile print "INSTANCE_HOME=", self.instancehome self.configfile = configfile try: from Zope2 import configure except ImportError: from Zope import configure configure(configfile) try: import Zope2 app = Zope2.app() except ImportError: import Zope app = Zope.app() from Testing.makerequest import makerequest self.app = makerequest(app) try: self._make_permissive() print "Permissive security installed" except: print "Permissive security NOT installed" self._pwd = self.portal or self.app try: from zope.component import getSiteManager from zope.component import getGlobalSiteManager try: from zope.app.component.hooks import setSite except ImportError: from zope.component.hooks import setSite if self.portal is not None: setSite(self.portal) gsm = getGlobalSiteManager() sm = getSiteManager() if sm is gsm: print "ERROR SETTING SITE!" except: # XXX: What exceptions is this supposed to catch? pass
def setUp(self): import transaction self.guarded_getattr = guarded_getattr_py transaction.manager.begin() self.app = makerequest(Zope2.app()) try: # Set up a manager user self.uf = self.app.acl_users self.uf._doAddUser('manager', 'secret', ['Manager'], []) self.login('manager') # Set up objects in the root that we want to aquire self.app.manage_addFolder('plain_folder') self.app._setObject('error_log2', ProtectedSiteErrorLog()) # We also want to be able to acquire simple attributes self.app.manage_addProperty(id='simple_type', type='string', value='a string') # Set up a subfolder and the objects we want to acquire from self.app.manage_addFolder('subfolder') self.folder = self.app.subfolder self.folder._setObject('allowed', AllowedItem()) self.folder._setObject('denied', DeniedItem()) self.folder._setObject('protected', ProtectedItem()) except: self.tearDown() raise
def testSetUp(self): import Zope2 import transaction # Open a new app and save it as the resource ``app``. environ = { 'SERVER_NAME': self['host'], 'SERVER_PORT': str(self['port']), } app = addRequestContainer(Zope2.app(), environ=environ) request = app.REQUEST request['PARENTS'] = [app] # Make sure we have a zope.globalrequest request try: from zope.globalrequest import setRequest setRequest(request) except ImportError: pass # Start a transaction transaction.begin() # Save resources for tests to access self['app'] = app self['request'] = request
def testSetUp(self): import Zope2 import transaction # Override zodbDB from the layer setup. Since it was set up by # this layer, we can't just assign a new shadow. We therefore keep # track of the original so that we can restore it on tear-down. self['zodbDB'] = zodb.stackDemoStorage(self.get('zodbDB'), name='FunctionalTest') # Save the app environ = { 'SERVER_NAME': self['host'], 'SERVER_PORT': str(self['port']), } app = addRequestContainer(Zope2.app(), environ=environ) request = app.REQUEST request['PARENTS'] = [app] # Make sure we have a zope.globalrequest request try: from zope.globalrequest import setRequest setRequest(request) except ImportError: pass # Start a transaction transaction.begin() # Save resources for the test self['app'] = app self['request'] = request
def get_plone_site(): import Zope2 app = Zope2.app() from Testing.ZopeTestCase import utils utils._Z2HOST = HOST path = PLONE.split('/') app = utils.makerequest(app) app.REQUEST['PARENTS'] = [app] app.REQUEST.other['VirtualRootPhysicalPath'] = path from zope.globalrequest import setRequest setRequest(app.REQUEST) from AccessControl.SpecialUsers import system as user from AccessControl.SecurityManagement import newSecurityManager newSecurityManager(None, user) _site = app[path[-1]] site = _site.__of__(app) from zope.site.hooks import setSite setSite(site) return site
def setup(lib_python): try: os.remove(os.path.join(lib_python, '..', '..', 'var', 'Data.fs')) except: pass import Zope2 import Products import AccessControl.SecurityManagement app=Zope2.app() Products.ZCatalog.ZCatalog.manage_addZCatalog(app, 'cat', '') from Products.ZCTextIndex.ZCTextIndex import PLexicon from Products.ZCTextIndex.Lexicon import Splitter, CaseNormalizer app.cat._setObject('lex', PLexicon('lex', '', Splitter(), CaseNormalizer()) ) class extra: doc_attr = 'PrincipiaSearchSource' lexicon_id = 'lex' index_type = 'Okapi BM25 Rank' app.cat.addIndex('PrincipiaSearchSource', 'ZCTextIndex', extra) transaction.commit() system = AccessControl.SpecialUsers.system AccessControl.SecurityManagement.newSecurityManager(None, system) app._p_jar.close()
def setUp(self): super(ZPTMacros, self).setUp() zope.component.provideAdapter(DefaultTraversable, (None,)) transaction.begin() self.app = makerequest(Zope2.app()) f = self.app.manage_addProduct['PageTemplates'].manage_addPageTemplate self._addPT = f self.title = 'title of page template' self.text = """ <metal:block use-macro="template/macros/themacro"> <p metal:fill-slot="theslot"> This is in the slot </p> </metal:block> <tal:block condition="nothing"> <div metal:define-macro="themacro"> <h1>This is the header</h1> <p metal:define-slot="theslot"> This will be replaced </p> </div> </tal:block> """ self.result = """<div>
def initialize(context): """Initializer called when used as a Zope 2 product.""" root = Zope2.app() sites = root.objectValues("Plone Site") version = os.environ.get("EEA_KGS_VERSION", "") if not version: return changed = False for site in sites: anno = queryAdapter(site, IAnnotations) if not anno: continue if not anno.get("EEA_KGS_VERSION", None): anno["EEA_KGS_VERSION"] = OOBTree() changed = True if not anno["EEA_KGS_VERSION"].get(version, None): anno["EEA_KGS_VERSION"][version] = datetime.now() changed = True if changed: transaction.get().note('eea.design: updating EEA_KGS_VERSION') try: transaction.commit() except Exception as err: logger.warn("EEA_KGS_VERSION already updated elsewhere: %s", err) transaction.abort() else: logger.info("EEA_KGS_VERSION updated to: %s", version)
def _executeAsUser(context_path, portal_path, uf_path, user_id, func, *args, **kwargs): """Reconstruct environment and execute func.""" transaction = Zope2.zpublisher_transactions_manager # Supports isDoomed transaction.begin() app = Zope2.app() result = None try: try: portal = app.unrestrictedTraverse(portal_path, None) if portal is None: raise BadRequest( 'Portal path %s not found' % '/'.join(portal_path)) setSite(portal) if uf_path: acl_users = app.unrestrictedTraverse(uf_path, None) if acl_users is None: raise BadRequest( 'Userfolder path %s not found' % '/'.join(uf_path)) user = acl_users.getUserById(user_id) if user is None: raise BadRequest('User %s not found' % user_id) newSecurityManager(None, user) context = portal.unrestrictedTraverse(context_path, None) if context is None: raise BadRequest( 'Context path %s not found' % '/'.join(context_path)) # Create a request to work with import sys from ZPublisher.HTTPResponse import HTTPResponse from ZPublisher.HTTPRequest import HTTPRequest response = HTTPResponse(stdout=sys.stdout) env = {'SERVER_NAME':'fake_server', 'SERVER_PORT':'80', 'REQUEST_METHOD':'GET'} request = HTTPRequest(sys.stdin, env, response) # Set values from original request original_request = kwargs.get('original_request') if original_request: for k,v in original_request.items(): request.set(k, v) context.REQUEST = request result = func(context, *args, **kwargs) del context.REQUEST #Avoid "can't pickle file objects" transaction.commit() except: transaction.abort() raise finally: noSecurityManager() setSite(None) app._p_jar.close() return result
def load_config_file(option, opt, config_file, *ignored): config_file = os.path.abspath(config_file) print "Parsing %s" % config_file import Zope2 Zope2.configure(config_file)
global _patched _patched = True # Do not patch a running Zope if not Zope2._began_startup: _apply_patches() # Allow test authors to install Zope products into the test environment. Note # that installProduct() must be called at module level -- never from tests. from OFS.Application import get_folder_permissions, get_products from OFS.Application import install_product, install_package from OFS.Folder import Folder import Products _theApp = Zope2.app() _installedProducts = {} _installedPackages = {} def hasProduct(name): '''Checks if a product can be found along Products.__path__''' return name in [n[1] for n in get_products()] def installProduct(name, quiet=0): '''Installs a Zope product.''' start = time.time() meta_types = [] if _patched and not _installedProducts.has_key(name): for priority, product_name, index, product_dir in get_products():
def main(args=None): lib_python = find_lib_python() sys.path.insert(0, lib_python) if args is None: args = sys.argv[1:] if not args: print __doc__ sys.exit(0) print args random.seed(hash(tuple(args))) # always use the same for the given args options = {"mbox": None, "threads": None} jobdefs = [] collect_options(args, jobdefs, options) mboxes = {} if options["mbox"]: mboxes[options["mbox"]] = MBox(options["mbox"]) # Perform a ZConfig-based Zope initialization: zetup(os.path.join(lib_python, '..', '..', 'etc', 'zope.conf')) if options.has_key('setup'): setup(lib_python) else: import Zope2 Zope2.startup() #from ThreadedAsync.LoopCallback import loop #threading.Thread(target=loop, args=(), name='asyncore').start() jobs = JobProducer() for job, kw, frequency, sleep, repeatp in jobdefs: Job = globals()[job.capitalize()+'Job'] if getattr(Job, 'needs_mbox', 0): if not kw.has_key("mbox"): if not options["mbox"]: raise ValueError( "no mailbox (mbox option) file specified") kw['mbox'] = mboxes[options["mbox"]] else: if not mboxes.has_key[kw["mbox"]]: mboxes[kw['mbox']] = MBox[kw['mbox']] kw["mbox"] = mboxes[kw['mbox']] jobs.add(Job(**kw), frequency, sleep, repeatp) if not jobs: print "No jobs to execute" return threads = int(options['threads'] or '0') if threads > 1: threads = [threading.Thread(target=run, args=(jobs, i), name=str(i)) for i in range(threads)] for thread in threads: thread.start() for thread in threads: thread.join() else: run(jobs)
def load_config_file(option, opt, config_file, *ignored): print "Parsing %s" % config_file import Zope2 Zope2.configure(config_file)
def obj(self, context): obj = Zope2.app().unrestrictedTraverse(self.search_path) wrapped = obj.__of__(context) return wrapped
def startZope(self): # Import Zope import Zope2 Zope2.startup()
from ZPublisher.BeforeTraverse import rewriteBeforeTraverse from Products.SiteAccess.AccessRule import AccessRule stack = [app] while stack: o = stack.pop() ov = getattr(o, 'objectValues', None) if ov is not None: stack.extend(list(ov())) btr = getattr(o, '__before_traverse__', None) if btr and type(btr) == type({}): touched = 0 for k, v in btr.items(): if type(v) is type(''): touched = 1 if k[1] == 'AccessRule': btr[k] = AccessRule(v) else: btr[k] = NameCaller(v) if touched: rewriteBeforeTraverse(o, btr) if __name__ == '__main__': import Zope2 import transaction print "Converting SiteAccess objects from 1.x to 2.x ..." app = Zope2.app() _cvt_btr(app) transaction.commit() print "Done."
def _customStuff(): """ Everything available in the console is defined here. """ import socket from pprint import pprint from Products.ZenUtils.Utils import setLogLevel from Products.Zuul import getFacade, listFacades # Connect to the database, set everything up app = Zope2.app() app = set_context(app) def login(username='******'): utool = getToolByName(app, 'acl_users') user = utool.getUserById(username) if user is None: user = app.zport.acl_users.getUserById(username) user = user.__of__(utool) newSecurityManager(None, user) from AccessControl.Implementation import setImplementation #Chip's pitched battle against segfault. #import pdb;pdb.set_trace() #setImplementation('PYTHON') login('admin') # Useful references zport = app.zport dmd = zport.dmd sync = zport._p_jar.sync find = dmd.Devices.findDevice devices = dmd.Devices me = find(socket.getfqdn()) auditComment = audit.auditComment shell_stdout = [] shell_stderr = [] # evaluate any command the user has defined to startup for idx, line in enumerate(dmd.getZenDMDStartupCommands()): try: exec line in globals(), locals() except Exception as e: print "Error evaluating zendmd startup command #%s %s, %s" %(idx, line, str(e)) def reindex(): sync() dmd.Devices.reIndex() dmd.Events.reIndex() dmd.Manufacturers.reIndex() dmd.Networks.reIndex() commit() def logout(): noSecurityManager() def zhelp(): cmds = sorted(filter(lambda x: not x.startswith("_"), _CUSTOMSTUFF)) for cmd in cmds: print cmd def commit(): audit.audit('Shell.Script.Commit') from transaction import commit commit() def grepdir(obj, regex=""): if regex: import re pattern = re.compile(regex) for key in dir(obj): if pattern.search(key): print key def indexObject(obj): """ Updates every index available for the object. """ if hasattr(obj, 'index_object'): obj.index_object() notify(IndexingEvent(obj)) def lookupGuid(guid): """ Given a guid this returns the object that it identifies """ from Products.ZenUtils.guid.interfaces import IGUIDManager manager = IGUIDManager(dmd) return manager.getObject(guid) def version(): for info in zport.About.getAllVersions(): print "%10s: %s" % (info['header'], info['data']) print "%10s: %s" % ("DMD", dmd.version) def printNets(net=dmd.Networks, format="text", out=sys.stdout): """ Print out the IpNetwork and IpAddress hierarchy under net. To print out everything call printNets(dmd.Networks). format can be text, python, or xml. """ factory = IpNetworkPrinterFactory() printer = factory.createIpNetworkPrinter(format, out) printer.printIpNetwork(net) def cleandir(obj): portaldir = set(dir(dmd)) objdir = set(dir(obj)) appdir = set(dir(app)) result = sorted(objdir - portaldir - appdir) pprint(result) def search(obj, p=None, s=None, a=None): """Search obj for matching attribute and method names. p: pattern to match s: super depth (how many inheritance levels to search) a: acquisition depth (ignores any attribute with '__' in its name) """ pattern = None if p is None else re.compile(p, re.IGNORECASE) aq_end = None if a is None else a + 1 seen = set([]) all_print_args = {} chain = [x for x in aq_chain(obj)[:aq_end] if safe_hasattr(x, 'id') \ and not inspect.ismethod(x.id)] for obj_ in chain: new_seen, print_args = _search_super(aq_base(obj_), pattern, s, seen) seen |= new_seen all_print_args[obj_.id] = print_args for obj_ in reversed(chain): mro_slice, attrs, methods = all_print_args[obj_.id] for cls in mro_slice: if attrs[cls] or methods[cls]: print '\n', '=' * 79, '\n', path(obj_) _search_print(mro_slice, attrs, methods) break def path(obj): path_ = '/'.join(x.id for x in reversed(aq_chain(obj)) \ if safe_hasattr(x, 'id') \ and not inspect.ismethod(x.id)) if path_ == '': return obj if path_ == 'zport': return path_ if path_ == 'zport/dmd': return 'dmd' return path_[len('zport/dmd/'):] def history(start=None, end=None, lines=30, number=False): """ Display the history starting from entry 'start' to entry 'end'. Only available on platforms where the readline module can be imported. History starts from 0 and goes to a large number. The history file is $ZENHOME/.pyhistory by default. @parameter start: Line number to start printing @type start: integer @parameter end: Line number to finish printing @type end: integer @parameter lines: number of lines to show if no end @type lines: integer @parameter number: show the line numbers? @type number: boolean """ if readline is not None: maxHistLength = readline.get_current_history_length() if start is None: start = maxHistLength if end is None: end = maxHistLength - lines if start < end: end, start = start, end for i in range(end, start): if number: print i, readline.get_history_item(i) else: print readline.get_history_item(i) def sh(cmd, interactive=True): """ Execute a shell command. If interactive is False, then direct the contents of stdout into shell_stdout and the output of stderr into shell_stderr. @parameter cmd: shell command to execute @type cmd: string @parameter interactive: show outut to the screen or not @type interactive: boolean """ if interactive: proc = Popen(cmd, shell=True) else: proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) output, errors = proc.communicate() proc.wait() if not interactive: output = output.split('\n')[:-1] errors = errors.split('\n')[:-1] _CUSTOMSTUFF['shell_stdout'] = output _CUSTOMSTUFF['shell_stderr'] = errors return output, errors def edit(file=None, start=None, end=None, lines=30): """ Use the value of the EDITOR environment variable to edit a file. Defaults to the original Unix IDE -- vi. @parameter file: name of file to edit -- defaults to a temp file @type file: string @parameter start: Line number to start printing @type start: integer @parameter end: Line number to finish printing @type end: integer @parameter lines: number of lines to show if no end @type lines: integer """ editor = os.environ.get('EDITOR', 'vi') isNewFile = True isTmpName = False if file == None: isTmpName = True file = os.tempnam() fp = open(file, 'w') elif os.path.exists(file): isNewFile = False else: fp = open(file, 'w') if isNewFile and readline is not None: maxHistLength = readline.get_current_history_length() if start is None: start = maxHistLength if end is None: end = maxHistLength - lines if start < end: end, start = start, end for i in range(end, start): fp.write(readline.get_history_item(i) + '\n') fp.close() sh('%s %s' % (editor, file)) execfile(file, globals(), _CUSTOMSTUFF) if isTmpName: os.unlink(file) _CUSTOMSTUFF = locals() return _CUSTOMSTUFF
def zopeApp(db=None, connection=None, environ=None): """Context manager for working with the Zope2 app:: with zopeApp() as app: ... The ``app`` object has a request container and a simple ``REQUEST``. To set the request environment, pass a dict ``environ``. See ``addRequestContainer()`` for details. Pass a ZODB handle as ``db`` to use a specificdatabase. Alternatively, pass an open connection as ``connection`` (the connection will not be closed). """ import Zope2 import transaction closeConn = True if connection is not None: closeConn = False if connection is None and db is not None: connection = db.open() app = addRequestContainer(Zope2.app(connection), environ=environ) if connection is None: connection = app._p_jar # exceptions in finally clauses can mask exceptions # in the preceeding code block. So we catch # every exception and throw it instead of the exception # in the finally clause inner_exception = None try: yield app except Exception as e: inner_exception = e try: transaction.abort() except Exception as e: inner_exception = e raise raise else: try: transaction.commit() except Exception as e: inner_exception = e finally: try: app.REQUEST.close() if closeConn: connection.close() except: if inner_exception: raise inner_exception else: raise
# Copyright (C) Zenoss, Inc. 2015, all rights reserved. # # This content is made available according to terms specified in # License.zenoss under the directory where your Zenoss product is installed. # ############################################################################## import os import sys import getpass import Zope2 import transaction CONF_FILE = os.path.join(os.environ['ZENHOME'], 'etc', 'zope.conf') Zope2.configure(CONF_FILE) app = Zope2.app() locker = getattr(app.acl_users, 'account_locker_plugin') if not locker: print("Account locker plugin is not installed") sys.exit(0) def menu(): while 1: try: print( """ Account manager: 1. Reset bad attempts for particular account.
def bootstrap_zope(config_file): config_file = os.path.abspath(config_file) import Zope2 Zope2.configure(config_file)
def setUp(self): transaction.begin() self.root = makerequest(Zope2.app())
def startZope(self): # Import Zope import Zope2 Zope2.startup_wsgi()
def _startup(): global _theApp _theApp = Zope2.app()
from OFS.Application import Application from OFS.Cache import ZCM_MANAGERS from OFS.Image import Pdata from OFS.SimpleItem import SimpleItem from Testing.makerequest import makerequest from zExceptions import Redirect from zope.component import adapter from zope.lifecycleevent.interfaces import IObjectCreatedEvent from zope.lifecycleevent.interfaces import IObjectModifiedEvent from ZPublisher.HTTPRequest import HTTPRequest from ZPublisher.HTTPResponse import HTTPResponse here = os.path.dirname(os.path.abspath(__file__)) filedata = os.path.join(here, 'test.gif') Zope2.startup_wsgi() def makeConnection(): import ZODB from ZODB.DemoStorage import DemoStorage s = DemoStorage() return ZODB.DB(s).open() def aputrequest(file, content_type): resp = HTTPResponse(stdout=sys.stdout) environ = {} environ['SERVER_NAME'] = 'foo' environ['SERVER_PORT'] = '80'
def _customStuff(): """ Everything available in the console is defined here. """ import socket from transaction import commit from pprint import pprint from Products.ZenUtils.Utils import setLogLevel from Products.Zuul import getFacade, listFacades # Connect to the database, set everything up app = Zope2.app() app = set_context(app) def login(username='******'): utool = getToolByName(app, 'acl_users') user = utool.getUserById(username) if user is None: user = app.zport.acl_users.getUserById(username) user = user.__of__(utool) newSecurityManager(None, user) from AccessControl.Implementation import setImplementation #Chip's pitched battle against segfault. #import pdb;pdb.set_trace() #setImplementation('PYTHON') login('admin') # Useful references zport = app.zport dmd = zport.dmd sync = zport._p_jar.sync find = dmd.Devices.findDevice devices = dmd.Devices me = find(socket.getfqdn()) auditComment = audit.auditComment shell_stdout = [] shell_stderr = [] def reindex(): sync() dmd.Devices.reIndex() dmd.Events.reIndex() dmd.Manufacturers.reIndex() dmd.Networks.reIndex() commit() def logout(): noSecurityManager() def zhelp(): cmds = sorted(filter(lambda x: not x.startswith("_"), _CUSTOMSTUFF)) for cmd in cmds: print cmd def grepdir(obj, regex=""): if regex: import re pattern = re.compile(regex) for key in dir(obj): if pattern.search(key): print key def lookupGuid(guid): """ Given a guid this returns the object that it identifies """ from Products.ZenUtils.guid.interfaces import IGUIDManager manager = IGUIDManager(dmd) return manager.getObject(guid) def version(): for info in zport.About.getAllVersions(): print "%10s: %s" % (info['header'], info['data']) print "%10s: %s" % ("DMD", dmd.version) def printNets(net=dmd.Networks, format="text", out=sys.stdout): """ Print out the IpNetwork and IpAddress hierarchy under net. To print out everything call printNets(dmd.Networks). format can be text, python, or xml. """ factory = IpNetworkPrinterFactory() printer = factory.createIpNetworkPrinter(format, out) printer.printIpNetwork(net) def cleandir(obj): portaldir = set(dir(dmd)) objdir = set(dir(obj)) appdir = set(dir(app)) result = sorted(objdir - portaldir - appdir) pprint(result) def history(start=None, end=None, lines=30, number=False): """ Display the history starting from entry 'start' to entry 'end'. Only available on platforms where the readline module can be imported. History starts from 0 and goes to a large number. The history file is $ZENHOME/.pyhistory by default. @parameter start: Line number to start printing @type start: integer @parameter end: Line number to finish printing @type end: integer @parameter lines: number of lines to show if no end @type lines: integer @parameter number: show the line numbers? @type number: boolean """ if readline is not None: maxHistLength = readline.get_current_history_length() if start is None: start = maxHistLength if end is None: end = maxHistLength - lines if start < end: end, start = start, end for i in range(end, start): if number: print i, readline.get_history_item(i) else: print readline.get_history_item(i) def sh(cmd, interactive=True): """ Execute a shell command. If interactive is False, then direct the contents of stdout into shell_stdout and the output of stderr into shell_stderr. @parameter cmd: shell command to execute @type cmd: string @parameter interactive: show outut to the screen or not @type interactive: boolean """ if interactive: proc = Popen(cmd, shell=True) else: proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) output, errors = proc.communicate() proc.wait() if not interactive: output = output.split('\n')[:-1] errors = errors.split('\n')[:-1] _CUSTOMSTUFF['shell_stdout'] = output _CUSTOMSTUFF['shell_stderr'] = errors return output, errors def edit(file=None, start=None, end=None, lines=30): """ Use the value of the EDITOR environment variable to edit a file. Defaults to the original Unix IDE -- vi. @parameter file: name of file to edit -- defaults to a temp file @type file: string @parameter start: Line number to start printing @type start: integer @parameter end: Line number to finish printing @type end: integer @parameter lines: number of lines to show if no end @type lines: integer """ editor = os.environ.get('EDITOR', 'vi') isNewFile = True isTmpName = False if file == None: isTmpName = True file = os.tempnam() fp = open(file, 'w') elif os.path.exists(file): isNewFile = False else: fp = open(file, 'w') if isNewFile and readline is not None: maxHistLength = readline.get_current_history_length() if start is None: start = maxHistLength if end is None: end = maxHistLength - lines if start < end: end, start = start, end for i in range(end, start): fp.write(readline.get_history_item(i) + '\n') fp.close() sh('%s %s' % (editor, file)) execfile(file, globals(), _CUSTOMSTUFF) if isTmpName: os.unlink(file) _CUSTOMSTUFF = locals() return _CUSTOMSTUFF
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # ############################################################################## """ Unit tests for ContentTypeRegistry module. $Id$ """ from unittest import TestCase, TestSuite, makeSuite, main import Testing try: import Zope2 except ImportError: # BBB: for Zope 2.7 import Zope as Zope2 Zope2.startup() class MajorMinorPredicateTests(TestCase): def _makeOne(self, *args, **kw): from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate return MajorMinorPredicate(*args, **kw) def test_z2interfaces(self): from Interface.Verify import verifyClass from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate from Products.CMFCore.interfaces.ContentTypeRegistry \ import ContentTypeRegistryPredicate \ as IContentTypeRegistryPredicate
############################################################################## # # Copyright (C) Zenoss, Inc. 2009, all rights reserved. # # This content is made available according to terms specified in # License.zenoss under the directory where your Zenoss product is installed. # ############################################################################## import os import Zope2 CONF_FILE = os.path.join(os.environ['ZENHOME'], 'etc', 'zope.conf') Zope2.configure(CONF_FILE) import unittest import Globals from uuid import uuid4 import logging log = logging.getLogger('signalProcessorTest') from zope.interface import implements from zenoss.protocols.protobufs.zep_pb2 import Signal from Products.ZenEvents.zenactiond import ProcessSignalTask from Products.ZenEvents.NotificationDao import NotificationDao from Products.ZenModel.actions import TargetableAction from Products.ZenModel.Trigger import Trigger from Products.ZenModel.NotificationSubscription import NotificationSubscription from Products.ZenTestCase.BaseTestCase import BaseTestCase from Products.ZenModel.interfaces import IAction
def debug(zopeconf): make_wsgi_app({}, zopeconf) print('Starting debugger (the name "app" is bound to the top-level ' 'Zope object)') return Zope2.app()
def connect(self): zopeconf = zenPath("etc","zope.conf") import Zope2 Zope2.configure(zopeconf) self.app = Zope2.app()