[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/twisted/ Merged from
3.2 branch:
Jim Fulton
jim at zope.com
Sat Dec 24 11:37:17 EST 2005
Log message for revision 41018:
Merged from 3.2 branch:
------------------------------------------------------------------------
r40835 | jim | 2005-12-16 17:03:49 -0500 (Fri, 16 Dec 2005) | 2 lines
Removed surplus mkzopeinstance
Changed:
D Zope3/trunk/src/zope/app/twisted/mkzopeinstance.py
D Zope3/trunk/src/zope/app/twisted/tests/test_mkzopeinstance.py
-=-
Deleted: Zope3/trunk/src/zope/app/twisted/mkzopeinstance.py
===================================================================
--- Zope3/trunk/src/zope/app/twisted/mkzopeinstance.py 2005-12-24 16:36:23 UTC (rev 41017)
+++ Zope3/trunk/src/zope/app/twisted/mkzopeinstance.py 2005-12-24 16:37:17 UTC (rev 41018)
@@ -1,261 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2004 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (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.
-#
-##############################################################################
-"""Implementation of the mkzopeinstance script.
-
-This creates a new instances of the Zope server instance home. An
-'instance home' contains two things:
-
-- application server configuration and data
-
-- server process control scripts and data
-
-$Id$
-"""
-import optparse
-import os
-import shutil
-import sys
-
-import zope
-
-from zope.app.applicationcontrol import zopeversion
-
-
-def main(argv=None, from_checkout=False):
- """Top-level script function to create a new Zope instance."""
- if argv is None:
- argv = sys.argv
- try:
- options = parse_args(argv, from_checkout)
- except SystemExit, e:
- if e.code:
- return 2
- else:
- return 0
- app = Application(options)
- try:
- return app.process()
- except KeyboardInterrupt:
- return 1
- except SystemExit, e:
- return e.code
-
-
-class Application(object):
-
- def __init__(self, options):
- self.options = options
- self.need_blank_line = False
-
- def read_input_line(self, prompt):
- # The tests replace this to make sure the right things happen.
- return raw_input(prompt)
-
- def read_password(self, prompt):
- # The tests replace this to make sure the right things happen.
- import getpass
- try:
- return getpass.getpass(prompt)
- except KeyboardInterrupt:
- # The cursor was left on the same line as the prompt,
- # which we don't like. Print a blank line.
- print
- raise
-
- def process(self):
- options = self.options
-
- # make sure we can find the skeleton
- if not os.path.isdir(options.skeleton):
- print >>sys.stderr, "skeleton directory", options.skeleton
- print >>sys.stderr, "does not exist or is not a directory"
- return 1
-
- # create the destination
- if not options.destination:
- options.destination = self.get_skeltarget()
- options.destination = os.path.abspath(options.destination)
- if not os.path.exists(options.destination):
- try:
- os.mkdir(options.destination)
- except OSError, e:
- print >>sys.stderr, "could not create instance home:", e
- return 1
- elif not os.path.isdir(options.destination):
- print >>sys.stderr, options.destination, "is not a directory"
- print >>sys.stderr, ("(instance homes cannot be created in"
- " non-directories)")
- return 1
-
- if not options.username:
- options.username = self.get_username()
- if not options.password:
- options.password = self.get_password()
-
- # now create the instance!
- self.copy_skeleton()
- if options.add_package_includes:
- # need to copy ZCML differently since it's not in the skeleton:
- import __main__
- swhome = os.path.dirname(
- os.path.dirname(os.path.realpath(__main__.__file__)))
- shutil.copy2(os.path.join(swhome, "securitypolicy.zcml"),
- os.path.join(options.destination, "etc"))
- piname = "package-includes"
- pisrc = os.path.join(swhome, piname)
- pidst = os.path.join(options.destination, "etc", piname)
- for fn in os.listdir(pisrc):
- if fn.endswith(".zcml"):
- shutil.copy2(os.path.join(pisrc, fn), pidst)
- return 0
-
- def get_skeltarget(self):
- self.print_message(SKELTARGET_MESSAGE)
- self.need_blank_line = True
- while 1:
- skeltarget = self.read_input_line("Directory: ").strip()
- if skeltarget == '':
- print >>sys.stderr, 'You must specify a directory'
- continue
- return os.path.expanduser(skeltarget)
-
- def get_username(self):
- self.print_message(USERNAME_MESSAGE)
- self.need_blank_line = True
- while 1:
- username = self.read_input_line("Username: ").strip()
- if not username:
- print >>sys.stderr, "You must specify an administrative user"
- continue
- return username
-
- def get_password(self):
- self.print_message(PASSWORD_MESSAGE)
- while 1:
- password = self.read_password("Password: ")
- if not password:
- print >>sys.stderr, "Password may not be empty"
- continue
- if password != password.strip() or password.split() != [password]:
- print >>sys.stderr, "Password may not contain spaces"
- continue
- break
- again = self.read_password("Verify password: ")
- if again != password:
- print >>sys.stderr, "Password not verified!"
- sys.exit(1)
- return password
-
- def print_message(self, message):
- if self.need_blank_line:
- print
- self.need_blank_line = False
- print message
-
- def copy_skeleton(self):
- options = self.options
- # TODO we should be able to compute the script
- script = os.path.abspath(sys.argv[0])
- zope_home = os.path.dirname(os.path.dirname(script))
- zope_init = os.path.abspath(zope.__file__)
- software_home = os.path.dirname(os.path.dirname(zope_init))
- self.replacements = [
- ("<<USERNAME>>", options.username),
- ("<<PASSWORD>>", options.password),
- ("<<PYTHON>>", sys.executable),
- ("<<INSTANCE_HOME>>", options.destination),
- ("<<ZOPE_HOME>>", zope_home),
- ("<<SOFTWARE_HOME>>", software_home),
- ]
- self.copytree(self.options.skeleton, self.options.destination)
-
- def copytree(self, src, dst):
- # Similar to shutil.copytree(), but doesn't care about
- # symlinks, doesn't collect errors, and uses self.copyfile()
- # instead of shutil.copy2().
- assert os.path.isdir(dst), dst
- names = os.listdir(src)
- if ".svn" in names:
- names.remove(".svn")
- for name in names:
- srcname = os.path.join(src, name)
- dstname = os.path.join(dst, name)
- if os.path.isdir(srcname):
- os.mkdir(dstname)
- self.copytree(srcname, dstname)
- else:
- self.copyfile(srcname, dstname)
- # There shouldn't be any need to deal with devices, sockets etc.
-
- def copyfile(self, src, dst):
- if dst.endswith(".in"):
- dst = dst[:-3]
- text = open(src, "rU").read()
- # perform replacements
- for var, string in self.replacements:
- text = text.replace(var, string)
- f = open(dst, "w")
- f.write(text)
- f.close()
- shutil.copymode(src, dst)
- shutil.copystat(src, dst)
- else:
- shutil.copy2(src, dst)
-
-
-SKELTARGET_MESSAGE = """\
-Please choose a directory in which you'd like to install Zope
-'instance home' files such as database files, configuration files,
-etc.
-"""
-
-USERNAME_MESSAGE = """\
-Please choose a username for the initial administrator account.
-This is required to allow Zope's management interface to be used.
-"""
-
-PASSWORD_MESSAGE = """\
-Please provide a password for the initial administrator account.
-"""
-
-
-def parse_args(argv, from_checkout=False):
- """Parse the command line, returning an object representing the input."""
- path, prog = os.path.split(os.path.realpath(argv[0]))
- version = "%prog for " + zopeversion.ZopeVersionUtility.getZopeVersion()
- p = optparse.OptionParser(prog=prog,
- usage="%prog [options]",
- version=version)
- p.add_option("-d", "--dir", dest="destination", metavar="DIR",
- help="the dir in which the instance home should be created")
- p.add_option("-s", "--skelsrc", dest="skeleton", metavar="DIR",
- help="template skeleton directory")
- p.add_option("-u", "--user", dest="username", metavar="USER:PASSWORD",
- help="set the user name and password of the initial user")
- options, args = p.parse_args(argv[1:])
- if options.skeleton is None:
- options.add_package_includes = from_checkout
- basedir = os.path.dirname(path)
- # no assurance that this exists!
- options.skeleton = os.path.join(basedir, "zopeskel")
- else:
- options.add_package_includes = False
- options.program = prog
- options.version = version
- if args:
- p.error("too many arguments")
- options.password = None
- if options.username and ":" in options.username:
- options.username, options.password = options.username.split(":", 1)
- return options
Deleted: Zope3/trunk/src/zope/app/twisted/tests/test_mkzopeinstance.py
===================================================================
--- Zope3/trunk/src/zope/app/twisted/tests/test_mkzopeinstance.py 2005-12-24 16:36:23 UTC (rev 41017)
+++ Zope3/trunk/src/zope/app/twisted/tests/test_mkzopeinstance.py 2005-12-24 16:37:17 UTC (rev 41018)
@@ -1,294 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2004 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (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.
-#
-##############################################################################
-"""Tests for the implementation of the mkzopeinstance script.
-
-$Id$
-"""
-import os
-import shutil
-import sys
-import tempfile
-import unittest
-
-from StringIO import StringIO
-
-from zope.app.twisted import mkzopeinstance
-
-
-class TestBase(unittest.TestCase):
-
- def setUp(self):
- self.stdout = StringIO()
- self.stderr = StringIO()
- self.old_stdout = sys.stdout
- self.old_stderr = sys.stderr
- sys.stdout = self.stdout
- sys.stderr = self.stderr
-
- def tearDown(self):
- sys.stdout = self.old_stdout
- sys.stderr = self.old_stderr
-
-
-class ArgumentParsingTestCase(TestBase):
- """Ensure the command line is properly converted to an options
- object.
- """
-
- def parse_args(self, args):
- argv = ["foo/bar.py"] + args
- options = mkzopeinstance.parse_args(argv)
- self.assertEqual(options.program, "bar.py")
- self.assert_(options.version)
- return options
-
- def test_no_arguments(self):
- options = self.parse_args([])
-
- def test_version_long(self):
- self.check_stdout_content(["--version"])
-
- def test_help_long(self):
- self.check_stdout_content(["--help"])
-
- def test_help_short(self):
- self.check_stdout_content(["-h"])
-
- def check_stdout_content(self, args):
- try:
- options = self.parse_args(args)
- except SystemExit, e:
- self.assertEqual(e.code, 0)
- self.assert_(self.stdout.getvalue())
- self.failIf(self.stderr.getvalue())
- else:
- self.fail("expected SystemExit")
-
- def test_without_destination(self):
- options = self.parse_args([])
- self.assertEqual(options.destination, None)
-
- def test_destination_long(self):
- options = self.parse_args(["--dir", "some/dir"])
- self.assertEqual(options.destination, "some/dir")
-
- def test_destination_short(self):
- options = self.parse_args(["-d", "some/dir"])
- self.assertEqual(options.destination, "some/dir")
-
- def test_without_skeleton(self):
- # make sure we get *some* skeleton directory by default
- # there's no claim that it exists
- options = self.parse_args([])
- self.assertNotEqual(options.skeleton, None)
-
- def test_with_skeleton_long(self):
- options = self.parse_args(["--skelsrc", "some/dir"])
- self.assertEqual(options.skeleton, "some/dir")
- self.failIf(options.add_package_includes)
-
- def test_with_skeleton_short(self):
- options = self.parse_args(["-s", "some/dir"])
- self.assertEqual(options.skeleton, "some/dir")
- self.failIf(options.add_package_includes)
-
- def test_without_username(self):
- options = self.parse_args([])
- self.assertEqual(options.username, None)
- self.assertEqual(options.password, None)
-
- def test_username_without_password_long(self):
- options = self.parse_args(["--user", "User"])
- self.assertEqual(options.username, "User")
- self.assertEqual(options.password, None)
-
- def test_username_without_password_short(self):
- options = self.parse_args(["-u", "User"])
- self.assertEqual(options.username, "User")
- self.assertEqual(options.password, None)
-
- def test_username_with_password_long(self):
- options = self.parse_args(["--user", "User:Pass"])
- self.assertEqual(options.username, "User")
- self.assertEqual(options.password, "Pass")
-
- def test_username_with_password_short(self):
- options = self.parse_args(["-u", "User:Pass"])
- self.assertEqual(options.username, "User")
- self.assertEqual(options.password, "Pass")
-
- def test_junk_positional_arg(self):
- try:
- self.parse_args(["junk"])
- except SystemExit, e:
- self.assert_(e.code)
- else:
- self.fail("expected SystemExit")
-
-
-class InputCollectionTestCase(TestBase):
-
- def setUp(self):
- super(InputCollectionTestCase, self).setUp()
- self.tmpdir = tempfile.mkdtemp(prefix="test-mkzopeinstance-")
- self.skeleton = os.path.join(self.tmpdir, "skel")
- self.instance = os.path.join(self.tmpdir, "inst")
- os.mkdir(self.skeleton)
-
- def tearDown(self):
- shutil.rmtree(self.tmpdir)
- super(InputCollectionTestCase, self).tearDown()
-
- def createOptions(self):
- options = Options()
- options.skeleton = self.skeleton
- return options
-
- def test_get_skeltarget(self):
- options = self.createOptions()
- input = [" ", " foo "]
- app = ControlledInputApplication(options, input)
- skel = app.get_skeltarget()
- self.assertEqual(skel, "foo")
- self.assertEqual(input, [])
- self.assert_(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
- def test_process_creates_destination(self):
- options = self.createOptions()
- input = [self.instance]
- app = ControlledInputApplication(options, input)
- self.assertEqual(app.process(), 0)
- self.assert_(os.path.isdir(self.instance))
- self.assertEqual(input, [])
- self.failUnless(app.all_input_consumed())
-
- def test_process_aborts_on_file_destination(self):
- options = self.createOptions()
- options.destination = self.instance
- open(self.instance, "w").close()
- app = ControlledInputApplication(options, [])
- self.assertEqual(app.process(), 1)
- self.assert_(self.stderr.getvalue())
-
- def test_process_aborts_on_failed_destination_creation(self):
- options = self.createOptions()
- options.destination = os.path.join(self.instance, "foo")
- app = ControlledInputApplication(options, [])
- self.assertEqual(app.process(), 1)
- self.assert_(self.stderr.getvalue())
-
- def test_get_username(self):
- options = self.createOptions()
- app = ControlledInputApplication(options, ["myuser"])
- usr = app.get_username()
- self.assertEqual(usr, "myuser")
- self.failIf(self.stderr.getvalue())
- self.failUnless(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
- def test_get_username_strips_whitespace(self):
- options = self.createOptions()
- app = ControlledInputApplication(options, [" myuser\t"])
- usr = app.get_username()
- self.assertEqual(usr, "myuser")
- self.failIf(self.stderr.getvalue())
- self.failUnless(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
- def test_get_username_ignores_empty_names(self):
- options = self.createOptions()
- app = ControlledInputApplication(options, ["", " ", "\t", "myuser"])
- usr = app.get_username()
- self.assertEqual(usr, "myuser")
- self.failUnless(self.stderr.getvalue())
- self.failUnless(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
- def test_get_password(self):
- options = self.createOptions()
- app = ControlledInputApplication(options, ["foo", "foo"])
- pw = app.get_password()
- self.assertEqual(pw, "foo")
- self.failIf(self.stderr.getvalue())
- self.failUnless(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
- def test_get_password_not_verified(self):
- options = self.createOptions()
- app = ControlledInputApplication(options, ["foo", "bar"])
- try:
- app.get_password()
- except SystemExit, e:
- self.assertEqual(e.code, 1)
- else:
- self.fail("expected SystemExit")
- self.failUnless(self.stderr.getvalue())
- self.failUnless(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
- def test_get_password_empty(self):
- # Make sure the empty password is ignored.
- options = self.createOptions()
- app = ControlledInputApplication(options, ["", "foo", "foo"])
- pw = app.get_password()
- self.assertEqual(pw, "foo")
- self.failUnless(self.stderr.getvalue())
- self.failUnless(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
- def test_get_password_disallows_whitespace(self):
- # Any password that contains spaces is disallowed.
- options = self.createOptions()
- app = ControlledInputApplication(options, [" ", "\t", "a b",
- " a", "b ", "foo", "foo"])
- pw = app.get_password()
- self.assertEqual(pw, "foo")
- self.failUnless(self.stderr.getvalue())
- self.failUnless(self.stdout.getvalue())
- self.failUnless(app.all_input_consumed())
-
-
-class ControlledInputApplication(mkzopeinstance.Application):
-
- def __init__(self, options, input_lines):
- mkzopeinstance.Application.__init__(self, options)
- self.__input = input_lines
-
- def read_input_line(self, prompt):
- return self.__input.pop(0)
-
- read_password = read_input_line
-
- def all_input_consumed(self):
- return not self.__input
-
-
-class Options(object):
-
- username = "[test-username]"
- password = "[test-password]"
- destination = None
- version = "[test-version]"
- program = "[test-program]"
- add_package_includes = False
-
-
-def test_suite():
- suite = unittest.makeSuite(ArgumentParsingTestCase)
- suite.addTest(unittest.makeSuite(InputCollectionTestCase))
- return suite
-
-if __name__ == "__main__":
- unittest.main(defaultTest="test_suite")
More information about the Zope3-Checkins
mailing list