[Zope-Checkins] CVS: Zope/inst/in - make_instance.py.in:1.1.2.11

Chris McDonough chrism@zope.com
Sun, 5 Jan 2003 02:20:18 -0500


Update of /cvs-repository/Zope/inst/in
In directory cvs.zope.org:/tmp/cvs-serv565

Added Files:
      Tag: chrism-install-branch
	make_instance.py.in 
Log Message:
Committing with 755 mode.


=== Zope/inst/in/make_instance.py.in 1.1.2.10 => 1.1.2.11 ===
--- /dev/null	Sun Jan  5 02:20:17 2003
+++ Zope/inst/in/make_instance.py.in	Sun Jan  5 02:20:15 2003
@@ -0,0 +1,217 @@
+#!<<PYTHON>>
+##############################################################################
+#
+# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE
+#
+##############################################################################
+"""Make an INSTANCE_HOME."""
+
+
+
+import sys, os, string, stat, getopt
+import sha, binascii
+import install, file_from_infile
+
+def main(zopehome, insthome, skel_dir, in_dir, config_location, user, passwd):
+    make_insthome(zopehome, insthome, skel_dir, in_dir, config_location)
+    if not (user is None or passwd is None):
+        write_inituser(insthome, user, passwd)
+    name = os.path.join(insthome, 'bin', 'zctl.py')
+    print 'Done!  Use "%s start" to start Zope.' % name
+
+def usage():
+    print ("""
+%s [--insthome=dir] [--zopehome=dir] [--inituser=username:passwd]
+      [--config-location=path] [--help]
+""" % sys.argv[0])
+    print "  Options:"
+    print
+    print "    --insthome         specifies instance home target directory"
+    print "    --zopehome         specifies location of Zope software"
+    print "    --inituser         specifies username and password in the form"
+    print "                       'username:password' to use for the"
+    print "                       inituser file written to insthome.  If the"
+    print "                       value of --inituser is a null value, its"
+    print "                       value is not gathered interactively and an "
+    print "                       inituser file is not written to insthome."
+    print "    --config-location  specifies configuration file location"
+    print "                       (default: 'INSTANCE_HOME/etc/zope.conf')"
+    print "  If 'zopehome', 'insthome', or 'inituser' are not specified as"
+    print "  options, they are gathered interactively."
+
+def get_zopehome(default):
+    print
+    print "Please supply the directory which contains the base"
+    print "Zope software (the 'ZOPE_HOME').  If you've installed Zope"
+    print "via the ./configure, make, make install process, this should"
+    print "be the directory you specified via the '--prefix' option to"
+    print "'configure'.  If you are unsure, accept the default."
+    while 1:
+        print
+        home = raw_input('Zope home [default: %s]: ' % default)
+        if home == '':
+            home = default
+        else:
+            home = os.path.abspath(os.path.expanduser(home))
+        if not os.path.exists(home):
+            print '%s does not exist.' % `home`
+        elif not os.path.isdir(home):
+            print '%s is not a directory.' % `home`
+        else:
+            return home
+    
+def get_insthome(zopehome):
+    print
+    print 'An instance home is a directory from which you will run Zope.'
+    print 'Each instance home contains its own data and configuration but'
+    print 'shares "base" Zope software from %s.' % zopehome
+    while 1:
+        print
+        insthome = raw_input('Instance home [default: %s]: ' % zopehome)
+        if insthome == '':
+            insthome = zopehome
+        else:
+            insthome = os.path.abspath(os.path.expanduser(insthome))
+        if not os.path.exists(insthome):
+            print '%s does not exist.' % `insthome`
+            make_insthome = raw_input('Shall I create it [y]? ')
+            if make_insthome and make_insthome[0] not in 'yY': continue
+            try:
+                os.makedirs(insthome, 0775)
+            except:
+                print 'Unable to create %s' % `insthome`
+            else:
+                print 'Created.'
+                return insthome
+        elif not os.path.isdir(insthome):
+            print '%s is not a directory.' % `insthome`
+        else:
+            print "%s exists, so I left it alone." % `insthome`
+            return insthome
+
+def make_insthome(zopehome, insthome, skel_dir, in_dir, config_location):
+    try:
+        if not os.path.exists(insthome):
+            os.makedirs(insthome, 0775)
+        elif not os.path.isdir(insthome) or os.path.isfile(insthome):
+            print (
+                '%s directory exists and is a file. Giving up.' % insthome
+                )
+            sys.exit(127)
+    except:
+        print 'Could not create directory %s!  Giving up.' % insthome
+        sys.exit(127)
+    # Make skeleton
+    install.main(skel_dir, insthome, symlinks=0)
+    # create files from .in files
+    map = {'PYTHON':sys.executable,
+           'BASE_DIR':zopehome,
+           'CONFIG_LOCATION':config_location,
+           'ZOPE_HOME':zopehome,
+           'SOFTWARE_HOME':os.path.join(zopehome, 'lib', 'python'),
+           'INSTANCE_HOME':insthome,
+           }
+    file_from_infile.main(os.path.join(in_dir, 'zctl.py.in'),
+                          os.path.join(insthome, 'bin', 'zctl.py'), map, 0)
+    file_from_infile.main(os.path.join(in_dir, 'zope.conf.in'),
+                          os.path.join(insthome, 'etc', 'zope.conf'), map, 0)
+
+def write_inituser(insthome, username, passwd):
+    ac_path=os.path.join(insthome, 'inituser')
+    acfile=open(ac_path, 'w')
+    acfile.write('%s:%s'%(username,generate_sha_passwd(passwd)))
+    acfile.close()
+    os.chmod(ac_path, 0644)
+
+def get_inituser(home, insthome):
+    import getpass
+    print 'Please choose a username and password for the initial user.'
+    print 'These will be the credentials you use to initially manage'
+    print 'your %s Zope instance.' % insthome
+    print
+    user = raw_input("Username: ")
+    if user == '':
+        return
+
+    while 1:
+        passwd = getpass.getpass("Password: ")
+        verify = getpass.getpass("Verify password: ")
+        if verify == passwd:
+            break
+        else:
+            passwd = verify = ''
+            print "Password mismatch, please try again..."
+
+    return user, passwd
+
+def generate_sha_passwd(password,):
+    return '{SHA}' + binascii.b2a_base64(sha.new(password).digest())[:-1]
+
+if __name__=='__main__':
+    zopehome = None
+    insthome = None
+    user = None
+    passwd = None
+    do_inituser = 1
+    config_location = None
+    try:
+        longopts = ["zopehome=", "insthome=", "inituser=", "help",
+                    "config-location="]
+        opts, args = getopt.getopt(sys.argv[1:], "h", longopts)
+    except getopt.GetoptError, v:
+        print v
+        usage()
+        sys.exit(0)
+    for o, a in opts:
+        if o in ('-h', '--help'):
+            usage()
+            sys.exit()
+        if o == '--zopehome':
+            zopehome = a
+        if o == '--insthome':
+            insthome = os.path.abspath(os.path.expanduser(a))
+        if o == '--inituser':
+            if a == '':
+                do_inituser = 0
+                continue
+            tup=a.split(':', 1)
+            msg = ('--inituser must be specified in the format'
+                   ' "username:password"')
+            if len(tup) != 2:
+                print msg
+                usage()
+                sys.exit(127)
+            user, passwd = tup
+            if not user or not passwd:
+                print msg
+                usage()
+                sys.exit(127)
+        if o == '--config-location':
+            config_location = a
+    abspath = os.path.abspath
+    dirname = os.path.dirname
+    split = os.path.split
+    if zopehome is None:
+        zopehome=get_zopehome(abspath(split(split(sys.argv[0])[0])[0]))
+    if insthome is None:
+        insthome = get_insthome(zopehome)
+    if config_location is None:
+        config_location = os.path.join(insthome, 'etc', 'zope.conf')
+    if __name__ == '__main__':
+        here = os.path.abspath(os.path.dirname(sys.argv[0]))
+    else:
+        here = os.path.abspath(os.path.dirname(__file__))
+    if do_inituser:
+        if user is None or passwd is None:
+            user, passwd = get_inituser(zopehome, insthome)
+    # guess about skeleton and in directories.
+    skel_dir = os.path.join(here, 'skel')
+    in_dir = os.path.join(here, 'in')
+    main(zopehome, insthome, skel_dir, in_dir, config_location, user, passwd)