def test_bad_section(self): self.schema = ZConfig.loadSchema(CONFIG_BASE + "simplesections.xml") self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError, 'unexpected section end', self.loadtext, '</close>') self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError, 'unbalanced section end', self.loadtext, '<section>\n</close>') self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError, 'unclosed sections not allowed', self.loadtext, '<section>\n') self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError, 'malformed section header', self.loadtext, '<section()>\n</close>') self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError, 'malformed section end', self.loadtext, '<section>\n</section') self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError, 'malformed section start', self.loadtext, '<section') # ConfigLoader.endSection raises this and it is recaught and # changed to a SyntaxError self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError, "no values for", self.loadtext, "<hasmin foo>\n</hasmin>")
def runwsgi(): parser = argparse.ArgumentParser() parser.add_argument('-w', '--webdav', action='store_true') parser.add_argument('address', help='<ip>:<port>') parser.add_argument('zope_conf', help='path to zope.conf') args = parser.parse_args() startup = os.path.dirname(Zope2.Startup.__file__) schema = ZConfig.loadSchema(os.path.join(startup, 'zopeschema.xml')) conf, _ = ZConfig.loadConfig(schema, args.zope_conf) make_wsgi_app({}, zope_conf=args.zope_conf) from Signals.SignalHandler import SignalHandler SignalHandler.registerHandler(signal.SIGTERM, sys.exit) ip, port = splitport(args.address) port = int(port) createServer( app_wrapper( large_file_threshold=conf.large_file_threshold, webdav_ports=[port] if args.webdav else ()), listen=args.address, logger=logging.getLogger("access"), threads=conf.zserver_threads, ).run()
def load_schema(self): if self.schema is None: # Load schema if self.schemadir is None: self.schemadir = os.path.dirname(__file__) self.schemafile = os.path.join(self.schemadir, self.schemafile) self.schema = ZConfig.loadSchema(self.schemafile)
def setUp(self): # Load ZConfig schema self.schema = ZConfig.loadSchema(farb.CONFIG_SCHEMA) rewrite_config(RELEASE_CONFIG_FILE_IN, RELEASE_CONFIG_FILE, CONFIG_SUBS) self.config, handler = ZConfig.loadConfig(self.schema, RELEASE_CONFIG_FILE) self.instSection = self.config.Installations.Installation[0] self.releaseSection = self.config.Releases.Release[0] self.instSectionNoCommands = self.config.Installations.Installation[1] self.instSectionNoDisks = self.config.Installations.Installation[2]
def loadConfiguration(file, url=None): global _schema if _schema is None: here = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(here, "schema", "productconfig.xml") _schema = ZConfig.loadSchema(path) data, handlers = ZConfig.loadConfigFile(_schema, file, url=url) return dict((sect.getSectionName(), sect.mapping) for sect in data.product_config)
def setUp(self): # Load ZConfig schema self.schema = ZConfig.loadSchema(farb.CONFIG_SCHEMA) rewrite_config(RELEASE_CONFIG_FILE_IN, RELEASE_CONFIG_FILE, CONFIG_SUBS) rewrite_config(PACKAGES_CONFIG_FILE_IN, PACKAGES_CONFIG_FILE, CONFIG_SUBS) rewrite_config(PACKAGES_BAD_CONFIG_FILE_IN, PACKAGES_BAD_CONFIG_FILE, CONFIG_SUBS)
def options(args): """Password-specific options loaded from regular ZEO config file.""" try: opts, args = getopt.getopt(args, "dr:p:f:C:", ["configure=", "protocol=", "filename=", "realm"]) except getopt.error as msg: usage(msg) config = None delete = 0 auth_protocol = None auth_db = "" auth_realm = None for k, v in opts: if k == '-C' or k == '--configure': schemafile = os.path.join(os.path.dirname(ZEO.__file__), "schema.xml") schema = ZConfig.loadSchema(schemafile) config, nil = ZConfig.loadConfig(schema, v) if k == '-d' or k == '--delete': delete = 1 if k == '-p' or k == '--protocol': auth_protocol = v if k == '-f' or k == '--filename': auth_db = v if k == '-r' or k == '--realm': auth_realm = v if config is not None: if auth_protocol or auth_db: usage("Error: Conflicting options; use either -C *or* -p and -f") auth_protocol = config.zeo.authentication_protocol auth_db = config.zeo.authentication_database auth_realm = config.zeo.authentication_realm elif not (auth_protocol and auth_db): usage("Error: Must specifiy configuration file or protocol and database") password = None if delete: if not args: usage("Error: Must specify a username to delete") elif len(args) > 1: usage("Error: Too many arguments") username = args[0] else: if not args: usage("Error: Must specify a username") elif len(args) > 2: usage("Error: Too many arguments") elif len(args) == 1: username = args[0] else: username, password = args return auth_protocol, auth_db, auth_realm, delete, username, password
def options(args): """Password-specific options loaded from regular ZEO config file.""" try: opts, args = getopt.getopt( args, "dr:p:f:C:", ["configure=", "protocol=", "filename=", "realm"]) except getopt.error as msg: usage(msg) config = None delete = 0 auth_protocol = None auth_db = "" auth_realm = None for k, v in opts: if k == '-C' or k == '--configure': schemafile = os.path.join(os.path.dirname(ZEO.__file__), "schema.xml") schema = ZConfig.loadSchema(schemafile) config, nil = ZConfig.loadConfig(schema, v) if k == '-d' or k == '--delete': delete = 1 if k == '-p' or k == '--protocol': auth_protocol = v if k == '-f' or k == '--filename': auth_db = v if k == '-r' or k == '--realm': auth_realm = v if config is not None: if auth_protocol or auth_db: usage("Error: Conflicting options; use either -C *or* -p and -f") auth_protocol = config.zeo.authentication_protocol auth_db = config.zeo.authentication_database auth_realm = config.zeo.authentication_realm elif not (auth_protocol and auth_db): usage( "Error: Must specifiy configuration file or protocol and database") password = None if delete: if not args: usage("Error: Must specify a username to delete") elif len(args) > 1: usage("Error: Too many arguments") username = args[0] else: if not args: usage("Error: Must specify a username") elif len(args) > 2: usage("Error: Too many arguments") elif len(args) == 1: username = args[0] else: username, password = args return auth_protocol, auth_db, auth_realm, delete, username, password
def config(configfile, schemafile=None, features=()): # Load the configuration schema if schemafile is None: schemafile = os.path.join( os.path.dirname(appsetup.__file__), 'schema', 'schema.xml') # Let's support both, an opened file and path if isinstance(schemafile, basestring): schema = ZConfig.loadSchema(schemafile) else: schema = ZConfig.loadSchemaFile(schemafile) # Load the configuration file # Let's support both, an opened file and path try: if isinstance(configfile, basestring): options, handlers = ZConfig.loadConfig(schema, configfile) else: options, handlers = ZConfig.loadConfigFile(schema, configfile) except ZConfig.ConfigurationError as msg: sys.stderr.write("Error: %s\n" % str(msg)) sys.exit(2) # Insert all specified Python paths if options.path: sys.path[:0] = [os.path.abspath(p) for p in options.path] # Parse product configs zope.app.appsetup.product.setProductConfigurations( options.product_config) # Setup the event log options.eventlog() # Setup other defined loggers for logger in options.loggers: logger() # Insert the devmode feature, if turned on if options.devmode: features += ('devmode',) logging.warning("Developer mode is enabled: this is a security risk " "and should NOT be enabled on production servers. Developer mode " "can usually be turned off by setting the `devmode` option to " "`off` or by removing it from the instance configuration file " "completely.") # Execute the ZCML configuration. appsetup.config(options.site_definition, features=features) # Connect to and open the database, notify subscribers. db = appsetup.multi_database(options.databases)[0][0] notify(zope.processlifetime.DatabaseOpened(db)) return db
def config(configfile, schemafile=None, features=()): # Load the configuration schema if schemafile is None: schemafile = os.path.join(os.path.dirname(appsetup.__file__), 'schema', 'schema.xml') # Let's support both, an opened file and path if isinstance(schemafile, basestring): schema = ZConfig.loadSchema(schemafile) else: schema = ZConfig.loadSchemaFile(schemafile) # Load the configuration file # Let's support both, an opened file and path try: if isinstance(configfile, basestring): options, handlers = ZConfig.loadConfig(schema, configfile) else: options, handlers = ZConfig.loadConfigFile(schema, configfile) except ZConfig.ConfigurationError as msg: sys.stderr.write("Error: %s\n" % str(msg)) sys.exit(2) # Insert all specified Python paths if options.path: sys.path[:0] = [os.path.abspath(p) for p in options.path] # Parse product configs zope.app.appsetup.product.setProductConfigurations(options.product_config) # Setup the event log options.eventlog() # Setup other defined loggers for logger in options.loggers: logger() # Insert the devmode feature, if turned on if options.devmode: features += ('devmode', ) logging.warning( "Developer mode is enabled: this is a security risk " "and should NOT be enabled on production servers. Developer mode " "can usually be turned off by setting the `devmode` option to " "`off` or by removing it from the instance configuration file " "completely.") # Execute the ZCML configuration. appsetup.config(options.site_definition, features=features) # Connect to and open the database, notify subscribers. db = appsetup.multi_database(options.databases)[0][0] notify(zope.processlifetime.DatabaseOpened(db)) return db
def main(args=None): optparser = argparse.ArgumentParser( description="Script to check validity of a configuration file", epilog=""" Each file named on the command line is checked for syntactical errors and schema conformance. The schema must be specified. If no files are specified and standard input is not a TTY, standard in is treated as a configuration file. Specifying a schema and no configuration files causes the schema to be checked.""", ) optparser.add_argument("-s", "--schema", dest="schema", required=True, help="use the schema in FILE (can be a URL)", metavar="FILE") optparser.add_argument( "file", nargs='*', help="Optional configuration file to check", type=argparse.FileType('r'), ) options = optparser.parse_args(args=args) schema = ZConfig.loadSchema(options.schema) if not options.file: if sys.stdin.isatty(): # just checking the schema return 0 # stdin is a pipe options.file = [sys.stdin] errors = False for f in options.file: try: ZConfig.loadConfigFile(schema, f) except ZConfig.ConfigurationError as e: print(str(e), file=sys.stderr) errors = True return int(errors)
def _setZConfig(self): """Modify the config, adding automatically generated settings""" schemafile = pkg_resources.resource_filename( 'zope.app.server', 'schema.xml') schema = ZConfig.loadSchema(schemafile) root_options, handlers = ZConfig.loadConfig( schema, self.zope_config_file) # Devmode from the zope.app.server.main config, copied here for # ease of access. self.devmode = root_options.devmode # The defined servers. self.servers = root_options.servers # The number of configured threads. self.threads = root_options.threads
def _setZConfig(self): """Modify the config, adding automatically generated settings""" schemafile = pkg_resources.resource_filename('zope.app.server', 'schema.xml') schema = ZConfig.loadSchema(schemafile) root_options, handlers = ZConfig.loadConfig(schema, self.zope_config_file) # Devmode from the zope.app.server.main config, copied here for # ease of access. self.devmode = root_options.devmode # The defined servers. self.servers = root_options.servers # The number of configured threads. self.threads = root_options.threads
def test_simple_sections(self): self.schema = ZConfig.loadSchema(CONFIG_BASE + "simplesections.xml") conf = self.load("simplesections.conf") self.assertEqual(conf.var, "foo") # check each interleaved position between sections for c in "0123456": self.assertEqual(getattr(conf, "var_" + c), "foo-" + c) sect = list(sect for sect in conf.sections if sect.getSectionName() == "name")[0] self.assertEqual(sect.var, "bar") self.assertEqual(sect.var_one, "splat") self.assertTrue(sect.var_three is None) sect = list(sect for sect in conf.sections if sect.getSectionName() == "delegate")[0] self.assertEqual(sect.var, "spam") self.assertEqual(sect.var_two, "stuff") self.assertTrue(sect.var_three is None)
def test_simple_sections(self): self.schema = ZConfig.loadSchema(CONFIG_BASE + "simplesections.xml") conf = self.load("simplesections.conf") self.assertEqual(conf.var, "foo") # check each interleaved position between sections for c in "0123456": self.assertEqual(getattr(conf, "var_" +c), "foo-" + c) sect = [sect for sect in conf.sections if sect.getSectionName() == "name"][0] self.assertEqual(sect.var, "bar") self.assertEqual(sect.var_one, "splat") self.assert_(sect.var_three is None) sect = [sect for sect in conf.sections if sect.getSectionName() == "delegate"][0] self.assertEqual(sect.var, "spam") self.assertEqual(sect.var_two, "stuff") self.assert_(sect.var_three is None)
def runwsgi(): parser = argparse.ArgumentParser() parser.add_argument('-w', '--webdav', action='store_true') parser.add_argument('address', help='<ip>:<port>') parser.add_argument('zope_conf', help='path to zope.conf') parser.add_argument('--timerserver-interval', help='Interval for timerserver', type=float) args = parser.parse_args() startup = os.path.dirname(Zope2.Startup.__file__) schema = ZConfig.loadSchema(os.path.join(startup, 'zopeschema.xml')) conf, _ = ZConfig.loadConfig(schema, args.zope_conf) make_wsgi_app({}, zope_conf=args.zope_conf) if six.PY2: from Signals.SignalHandler import SignalHandler SignalHandler.registerHandler(signal.SIGTERM, sys.exit) else: import warnings warnings.warn("zope4py3: SignalHandling not implemented!") if args.timerserver_interval: import Products.TimerService Products.TimerService.timerserver.TimerServer.TimerServer( module='Zope2', interval=args.timerserver_interval, ) ip, port = splitport(args.address) port = int(port) createServer( app_wrapper(large_file_threshold=conf.large_file_threshold, webdav_ports=[port] if args.webdav else ()), listen=args.address, logger=logging.getLogger("access"), threads=conf.zserver_threads, asyncore_use_poll=True, # Prevent waitress from adding its own Via and Server response headers. ident=None, ).run()
def database_factory(global_conf, file_storage=None, db_definition=None): if file_storage is not None and db_definition is not None: raise TypeError("You may only provide a 'file_storage' or a " "'db_definition' setting, not both.") if file_storage is not None: filename = os.path.join(global_conf['here'], file_storage) db = zope.app.appsetup.database(filename) elif db_definition is not None: filename = os.path.join(global_conf['here'], db_definition) schema_xml = os.path.join(os.path.dirname(__file__), 'schema.xml') schema = ZConfig.loadSchema(schema_xml) cfgroot, cfghandlers = ZConfig.loadConfig(schema, filename) result, databases = multi_database(cfgroot.databases) db = result[0] zope.event.notify(zope.app.appsetup.DatabaseOpened(db)) else: db = None return db
def config(configfile, schemafile=None, features=()): # Load the configuration schema if schemafile is None: schemafile = os.path.join( os.path.dirname(appsetup.__file__), 'schema', 'schema.xml') # Let's support both, an opened file and path if isinstance(schemafile, basestring): schema = ZConfig.loadSchema(schemafile) else: schema = ZConfig.loadSchemaFile(schemafile) # Load the configuration file # Let's support both, an opened file and path try: if isinstance(configfile, basestring): options, handlers = ZConfig.loadConfig(schema, configfile) else: options, handlers = ZConfig.loadConfigFile(schema, configfile) except ZConfig.ConfigurationError, msg: sys.stderr.write("Error: %s\n" % str(msg)) sys.exit(2)
def application_factory(global_conf, conf='zope.conf'): # load 'zope.conf' configuration schema_xml = os.path.join( os.path.dirname(Zope2.Startup.__file__), 'zopeschema.xml') schema = ZConfig.loadSchema(schema_xml) options, handlers = ZConfig.loadConfig( schema, os.path.join(global_conf['here'], conf)) # read global settings from configuration file App.config.setConfiguration(options) starter = Zope2.Startup.get_starter() starter.setConfiguration(options) starter.setupInitialLogging() starter.setupLocale() starter.setupSecurityOptions() starter.setupInterpreter() Zope2.startup() # open ZODB, initialize Products, etc. starter.setupFinalLogging() db = Zope2.DB # XXX ick zope.event.notify(ProcessStarting()) return WSGIPublisherApplication(db)
def application_factory(global_conf, conf='zope.conf'): # load 'zope.conf' configuration schema_xml = os.path.join( os.path.dirname(zope.app.appsetup.__file__), 'schema', 'schema.xml') schema = ZConfig.loadSchema(schema_xml) options, handlers = ZConfig.loadConfig( schema, os.path.join(global_conf['here'], conf)) if options.path: sys.path[0:0] = [os.path.abspath(p) for p in options.path] options.eventlog() # load ZCML configuration features = () if options.devmode: features += ('devmode',) zope.app.appsetup.config(options.site_definition, features) # notify of ZODB database opening db = multi_database(options.databases)[0][0] zope.event.notify(DatabaseOpened(db)) zope.event.notify(ProcessStarting()) return WSGIPublisherApplication(db)
import copy import os import shutil import unittest import ZConfig import farb from farb import config, runner, utils # Useful Constants from farb.test import DATA_DIR, rewrite_config from farb.test.test_builder import BUILDROOT, INSTALLROOT, CDROM_INF, CDROM_INF_IN, ISO_MOUNTPOINT, builder from farb.test.test_config import RELEASE_CONFIG_FILE, RELEASE_CONFIG_FILE_IN, CONFIG_SUBS SCHEMA = ZConfig.loadSchema(farb.CONFIG_SCHEMA) RELEASE_NAMES = ['6.0', '6.2-release'] DISTFILES_CACHE = os.path.join(BUILDROOT, 'distfiles') PACKAGEDIR = os.path.join(DATA_DIR, 'fake_pkgs') class ReleaseBuildRunnerTestCase(unittest.TestCase): def setUp(self): rewrite_config(RELEASE_CONFIG_FILE_IN, RELEASE_CONFIG_FILE, CONFIG_SUBS) farbconfig, handler = ZConfig.loadConfig(SCHEMA, RELEASE_CONFIG_FILE) self.rbr = runner.ReleaseBuildRunner(farbconfig) self.rbr.run() def tearDown(self): os.unlink(RELEASE_CONFIG_FILE)
def getDbSchema(): global _db_schema if _db_schema is None: _db_schema = ZConfig.loadSchema(db_schema_path) return _db_schema
def getStorageSchema(): global _s_schema if _s_schema is None: _s_schema = ZConfig.loadSchema(s_schema_path) return _s_schema
def test_productconfig_xml(self): path = os.path.join(self.schema_dir, "productconfig.xml") url = self.path2url(path) ZConfig.loadSchema(url)
def test_schema_xml(self): path = os.path.join(self.schema_dir, "schema.xml") url = self.path2url(path) ZConfig.loadSchema(url)
def get_schema(self): if self.schema is None: ConfigurationTestCase.schema = ZConfig.loadSchema( CONFIG_BASE + "simple.xml") return self.schema
def getSchema(): startup = os.path.dirname(Zope2.Startup.__file__) schemafile = os.path.join(startup, 'zopeschema.xml') return ZConfig.loadSchema(schemafile)
def get_schema(self): if self.schema is None: ConfigurationTestCase.schema = ZConfig.loadSchema(CONFIG_BASE + "simple.xml") return self.schema
def getSchema(): startup = os.path.dirname(os.path.realpath(Zope2.Startup.__file__)) schemafile = os.path.join(startup, 'zopeschema.xml') return ZConfig.loadSchema(schemafile)
import os import unittest from lazr.config import ConfigSchema from lazr.config.interfaces import ConfigErrors import pkg_resources import ZConfig import lp.services.config # Configs that shouldn't be tested. EXCLUDED_CONFIGS = ['lpnet-template'] # Calculate some landmark paths. schema_file = pkg_resources.resource_filename('zope.app.server', 'schema.xml') schema = ZConfig.loadSchema(schema_file) here = os.path.dirname(lp.services.config.__file__) lazr_schema_file = os.path.join(here, 'schema-lazr.conf') def make_test(config_file, description): def test_function(): root, handlers = ZConfig.loadConfig(schema, config_file) # Hack the config file name into test_function's __name__ so that the test # -vv output is more informative. Unfortunately, FunctionTestCase's # description argument doesn't do what we want. test_function.__name__ = description return unittest.FunctionTestCase(test_function)
def load_schema(self, relurl): self.url = urljoin(CONFIG_BASE, relurl) self.schema = ZConfig.loadSchema(self.url) self.assert_(self.schema.issection()) return self.schema
import copy import os import shutil import unittest import ZConfig import farb from farb import config, runner, utils # Useful Constants from farb.test import DATA_DIR, rewrite_config from farb.test.test_builder import BUILDROOT, INSTALLROOT, CDROM_INF, CDROM_INF_IN, ISO_MOUNTPOINT, builder from farb.test.test_config import RELEASE_CONFIG_FILE, RELEASE_CONFIG_FILE_IN, CONFIG_SUBS SCHEMA = ZConfig.loadSchema(farb.CONFIG_SCHEMA) RELEASE_NAMES = ['6.0', '6.2-release'] DISTFILES_CACHE = os.path.join(BUILDROOT, 'distfiles') PACKAGEDIR = os.path.join(DATA_DIR, 'fake_pkgs') class ReleaseBuildRunnerTestCase(unittest.TestCase): def setUp(self): rewrite_config(RELEASE_CONFIG_FILE_IN, RELEASE_CONFIG_FILE, CONFIG_SUBS) farbconfig, handler = ZConfig.loadConfig(SCHEMA, RELEASE_CONFIG_FILE) self.rbr = runner.ReleaseBuildRunner(farbconfig) self.rbr.run() def tearDown(self): os.unlink(RELEASE_CONFIG_FILE) for release in RELEASE_NAMES: releaseroot = os.path.join(BUILDROOT, release)
def parseDependencies(): schema = ZConfig.loadSchema(conf_file('depends.xml')) config, handler = ZConfig.loadConfig(schema, conf_file('depends.conf')) return config, handler
opts, args = getopt.getopt(args, "dr:p:f:C:", ["configure=", "protocol=", "filename=", "realm"]) except getopt.error, msg: usage(msg) config = None delete = 0 auth_protocol = None auth_db = "" auth_realm = None for k, v in opts: if k == '-C' or k == '--configure': schemafile = os.path.join(os.path.dirname(ZEO.__file__), "schema.xml") schema = ZConfig.loadSchema(schemafile) config, nil = ZConfig.loadConfig(schema, v) if k == '-d' or k == '--delete': delete = 1 if k == '-p' or k == '--protocol': auth_protocol = v if k == '-f' or k == '--filename': auth_db = v if k == '-r' or k == '--realm': auth_realm = v if config is not None: if auth_protocol or auth_db: usage("Error: Conflicting options; use either -C *or* -p and -f") auth_protocol = config.zeo.authentication_protocol auth_db = config.zeo.authentication_database
def test_schema(self): dir = os.path.dirname(os.path.dirname(__file__)) filename = os.path.join(dir, "schema.xml") ZConfig.loadSchema(filename)