[Zope-Checkins] CVS: Zope/ZopeControl - __init__.py:1.1.2.3 zope.in:1.1.2.3

Matt Behrens matt@zigg.com
Mon, 27 May 2002 18:58:33 -0400


Update of /cvs-repository/Zope/ZopeControl
In directory cvs.zope.org:/tmp/cvs-serv18365/ZopeControl

Modified Files:
      Tag: zigg_unix-install-control-config-branch
	__init__.py zope.in 
Log Message:
More progress:

- Rewrite the control script to not have any special knowledge of
  'create', to help maintain the divorce from what the software home's
  control package does.

- Add ZOPE_HOME, now that it's needed (to look at the default imports).

- A few more portability fixes (now working on Linux).

- Start to option-enable z2.py, though there is no code yet.



=== Zope/ZopeControl/__init__.py 1.1.2.2 => 1.1.2.3 ===
 """
 
-import ConfigParser, os
+from ConfigParser import ConfigParser
+import os
 
-def help(instance_name, instance_conf, args):
+def help(options, extra_args, **kwargs):
     """Provide help"""
     print "If we were providing help, you'd see it here."
 
-def create(instance_name, instance_home, software_name, config_dir):
+def create(options, extra_args, software_name=None, software_home=None,
+	   instance_name=None, instance_home=None, instance_conf_dir=None,
+	   **kwargs):
     """Create a new instance"""
-    try: os.makedirs(instance_home)
-    except: pass
 
+    # Create and populate instance home
+    os.makedirs(instance_home)
     for dir in ('Extensions', 'Packages', 'Products', 'import', 'var'):
         os.mkdir(os.path.join(instance_home, dir))
 
-    instance_conf = ConfigParser.ConfigParser()
-    instance_conf.add_section('Paths')
-    instance_conf.set('Paths', 'InstanceHome', instance_home)
+    # Create instance configuration file
+    instance_conf = ConfigParser()
+    instance_conf.add_section('Instance')
+    instance_conf.set('Instance', 'home', instance_home)
     instance_conf.add_section('Software')
-    instance_conf.set('Software', 'SoftwareName', software_name)
+    if software_name is not None:
+	instance_conf.set('Software', 'name', software_name)
+    else:
+	instance_conf.set('Software', 'home', software_home)
 
-    try: os.makedirs(os.path.join(config_dir, instance_name))
-    except: pass
-    f = open(os.path.join(config_dir, instance_name, 'instance.conf'), 'w')
+    os.makedirs(instance_conf_dir)
+    f = open(os.path.join(instance_conf_dir, 'instance.conf'), 'w')
     instance_conf.write(f)
     f.close()
+
+def start(options, extra_args, software_home=None, instance_home=None,
+	  python_binary=None, **kwargs):
+    """Start an instance"""
+
+    os.environ['INSTANCE_HOME']=instance_home
+    os.environ['SOFTWARE_HOME']=software_home
+    os.environ['ZOPE_HOME']=software_home
+    command_line = '%s %s/z2.py -D' % (python_binary, software_home)
+    os.system(command_line)
 


=== Zope/ZopeControl/zope.in 1.1.2.2 => 1.1.2.3 ===
 
 # These commands are reserved for this script.
-SYSTEM_COMMANDS = ('config', 'create')
+SYSTEM_COMMANDS = ['config']
 
 # Configuration information, written by the source distribution's configure
 # script.
@@ -32,104 +32,147 @@
     'CONFIG_DIR':       '@CONFIG_DIR@',
     'SOFTWARE_DIR':     '@SOFTWARE_DIR@',
     'INSTANCE_DIR':     '@INSTANCE_DIR@',
+    # Other interesting things
+    'PYTHON':		'@PYTHON@',
 }
 
-import ConfigParser, getopt, os, string, sys
+from ConfigParser import ConfigParser
+import sys, os 
 
-def do(command, instance, args):
-    # Determine software and instance homes for the given instance.
-    instance_conf = getInstanceConf(instance)
-    instance_home = instance_conf.get('paths', 'instance_home')
-    software_name = instance_conf.get('software', 'software_name')
-    software_home = getSystemConf().get('software_homes', software_name)
-
-    # Import Control from the instance home or fallback to software home.
-    sys.path.insert(0, software_home)
-    sys.path.insert(0, instance_home)
-    import ZopeControl
-
-    # Run the given command.
-    sys.exit(ZopeControl.__dict__[command](instance, args))
-
-def doSystemCommand(command, args):
-    if command == 'config':
-        for key in CONFIG.keys():
-            print "%s=%s" % (key, CONFIG[key])
-        sys.exit(0)
-    elif command == 'create':
-        argc = len(args)
-        if (argc == 0) or (args[0][0] == '-'):
-            instance_name = 'default'
-            arg_start = 0
-        else:
-            instance_name = args[0]
-            arg_start = 1
-        instance_home = os.path.join(CONFIG['INSTANCE_DIR'], instance_name)
-        software_name = getDefaultSoftware()
-        opts, args = getopt.getopt(args[arg_start:], '',
-                                   ['software-name=', 'instance-home='])
-        for opt, value in opts:
-            if opt == '--software-name':
-                software_name = value
-            if opt == '--instance-home':
-                instance_home = value
-
-        software_home = getSystemConf().get('software_homes', software_name)
-        sys.path.insert(0, software_home)
-        import ZopeControl
-
-        sys.exit(ZopeControl.__dict__['create'](instance_name, instance_home,
-                                                software_name,
-                                                CONFIG['CONFIG_DIR']))
-
-def getDefaultInstance():
-    try: return getSystemConf().get('defaults', 'instance')
-    except: return None
-
-def getDefaultSoftware():
-    try: return getSystemConf().get('defaults', 'software')
-    except: return None
-
-def getInstanceConf(instance):
-    instance_conf = ConfigParser.ConfigParser()
-    instance_conf.read(os.path.join(CONFIG['CONFIG_DIR'],
-                                    instance,
-                                    'instance.conf'))
-    return instance_conf
-
-def getSystemConf():
-    system_conf = ConfigParser.ConfigParser()
-    system_conf.read(os.path.join(CONFIG['CONFIG_DIR'], 'system.conf'))
-    return system_conf
+class NoDefaultInstance(Exception):
+    pass
+
+def getDefaultInstance(system_conf):
+    if system_conf.has_option('Defaults', 'instance'):
+	return system_conf.get('Defaults', 'instance')
+    else:
+	raise NoDefaultInstance
+
+def printError(s):
+    sys.stderr.write('%s: %s\n' % (sys.argv[0], s))
 
 def printUsage():
-    print "Usage: %s <command> [<instance>] [<arg> [<arg> ...]]" % sys.argv[0]
+    printError('%s <command> [<instance>] [<option>=<value> [<option>=<value> ...]]' % sys.argv[0])
 
 if __name__ == '__main__':
     argc = len(sys.argv)
     if argc > 1:
-        command = string.lower(sys.argv[1])
-        if command in SYSTEM_COMMANDS:
-            if argc > 2:
-                doSystemCommand(command, sys.argv[2:])
-            else:
-                doSystemCommand(command, [])
-        if argc > 2:
-            if sys.argv[2][0] == '-':
-                instance = getDefaultInstance()
-                args = sys.argv[2:]
-            else:
-                instance = sys.argv[2]
-                args = sys.argv[3:]
-        else:
-            instance = getDefaultInstance()
-            args = []
-        if instance is None:
-            print "No default instance has been configured."
-            printUsage()
-            sys.exit(1)
-        do(command, instance, args)
+	command = sys.argv[1].lower()
+
+	# System commands are handled specially.
+	if command == 'config':
+	    for key in CONFIG.keys():
+		print "%s=%s" % (key, CONFIG[key])
+	    sys.exit(0)
+
+	# Read the system configuration file.
+	system_conf = ConfigParser()
+	system_conf.read(os.path.join(CONFIG['CONFIG_DIR'], 'system.conf'))
+
+	# Determine the instance name.
+	try:
+	    if argc > 2:
+		if (sys.argv[2].find('=') != -1) and (sys.argv[2][0] != '-'):
+		    instance_name = getDefaultInstance(system_conf)
+		    arg_start = 2
+		else:
+		    instance_name = sys.argv[2]
+		    arg_start = 3
+	    else:
+		instance_name = getDefaultInstance(system_conf)
+		arg_start = 2
+	except NoDefaultInstance:
+	    printError('system.conf specifies no default instance, '
+		       'and no instance was specified on the command line')
+	    sys.exit(1)
+
+	# Parse option/value pairs.
+	options = {}
+	extra_args = []
+	for arg in sys.argv[arg_start:]:
+	    try:
+		(option, value) = arg.split('=')
+		options[option.lower()] = value
+	    except ValueError: # unpack list of wrong size
+		extra_args.append(arg)
+
+	# Determine the software path and prepend it to sys.path.
+	software_name = None
+	software_home = None
+
+	if system_conf.has_option('Defaults', 'software'):
+	    software_name = system_conf.get('Defaults', 'software')
+
+	instance_conf_dir = os.path.join(CONFIG['CONFIG_DIR'], instance_name)
+	instance_conf = ConfigParser()
+	instance_conf.read(os.path.join(instance_conf_dir, 'instance.conf'))
+	if instance_conf.has_option('Software', 'name'):
+	    software_name = instance_conf.get('Software', 'name')
+	if instance_conf.has_option('Software', 'home'):
+	    software_home = instance_conf.get('Software', 'home')
+
+	try:
+	    software_name = options['software']
+	    del options['software']
+	    software_home = None
+	except KeyError:
+	    pass
+
+	try:
+	    software_home = options['software_home']
+	    del options['software_home']
+	except KeyError:
+	    pass
+
+	if software_home is None:
+	    if software_name is None:
+		printError('cannot determine software for this instance')
+		sys.exit(1)
+	    else:
+		if system_conf.has_option('Software', software_name):
+		    software_home = system_conf.get('Software', software_name)
+		else:
+		    software_home = os.path.join(CONFIG['SOFTWARE_DIR'],
+						 software_name)
+
+	sys.path.insert(0, software_home)
+
+	# Determine the instance home, and insert its Packages directory.
+	instance_home = os.path.join(CONFIG['INSTANCE_DIR'], instance_name)
+	if instance_conf.has_option('Instance', 'home'):
+	    instance_home = instance_conf.get('Instance', 'home')
+	try:
+	    instance_home = options['instance_home']
+	    del options['instance_home']
+	except KeyError:
+	    pass
+
+	sys.path.insert(0, os.path.join(instance_home, 'Packages'))
+
+	# After all those gyrations, we can now actually invoke the command.
+	try:
+	    import ZopeControl
+	except ImportError:
+	    printError('cannot import ZopeControl')
+	    sys.exit(1)
+
+	try:
+	    os.chdir(instance_home)
+	except OSError: # this will fail for 'create'
+	    pass
+
+	if ZopeControl.__dict__.has_key(command):
+	    ZopeControl.__dict__[command](options, extra_args,
+					  software_name=software_name,
+					  software_home=software_home,
+					  instance_name=instance_name,
+					  instance_home=instance_home,
+					  instance_conf_dir=instance_conf_dir,
+					  python_binary=CONFIG['PYTHON'])
+	else:
+	    printError('unrecognized command "%s"' % command)
+
     else:
-        printUsage()
-        sys.exit(1)
+	printUsage()
+	sys.exit(1)