Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add zconsole module
- Loading branch information
Showing
6 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import os | ||
import shutil | ||
import sys | ||
import tempfile | ||
import unittest | ||
from App.config import setConfiguration, getConfiguration | ||
from six import StringIO | ||
|
||
zope_conf_template = """ | ||
%define INSTANCE {} | ||
instancehome $INSTANCE | ||
<zodb_db main> | ||
<filestorage> | ||
path $INSTANCE/Data.fs | ||
</filestorage> | ||
mount-point / | ||
</zodb_db> | ||
""" | ||
|
||
test_script = """ | ||
import sys | ||
if __name__ == '__main__': | ||
app.foo = '42' | ||
print(app.foo) | ||
print(sys.argv[1:]) | ||
""" | ||
|
||
|
||
class ZConsoleTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.instancedir = tempfile.mkdtemp(prefix='foo42-') | ||
self.zopeconf = os.path.join(self.instancedir, 'zope.conf') | ||
self.stored_sys_argv = sys.argv | ||
self.stored_stdout = sys.stdout | ||
self.stored_app_config = getConfiguration() | ||
with open(self.zopeconf, 'w') as conffile: | ||
conffile.write(zope_conf_template.format(self.instancedir)) | ||
|
||
def tearDown(self): | ||
setConfiguration(self.stored_app_config) | ||
shutil.rmtree(self.instancedir) | ||
|
||
def test_debug(self): | ||
try: | ||
from Zope2.utilities.zconsole import debug | ||
sys.stdout = StringIO() | ||
got = debug(self.zopeconf) | ||
expected = '<Application at >' | ||
finally: | ||
sys.argv = self.stored_sys_argv | ||
sys.stdout = self.stored_stdout | ||
self.assertEqual(expected, str(got)) | ||
|
||
def test_runscript(self): | ||
script = os.path.join(self.instancedir, 'test_script.py') | ||
with open(script, 'w') as scriptfile: | ||
scriptfile.write(test_script) | ||
try: | ||
from Zope2.utilities.zconsole import runscript | ||
sys.argv = [ | ||
sys.executable, | ||
'run', | ||
self.zopeconf, | ||
script, | ||
'bar', 'baz'] | ||
sys.stdout = StringIO() | ||
runscript(self.zopeconf, script, 'bar', 'baz') | ||
sys.stdout.seek(0) | ||
got = sys.stdout.read() | ||
finally: | ||
sys.argv = self.stored_sys_argv | ||
sys.stdout = self.stored_stdout | ||
expected = "42\n['run', '{}', '{}', 'bar', 'baz']\n".format(self.zopeconf, script) # noqa: E501 | ||
self.assertEqual(expected, got) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
import sys | ||
import Zope2 | ||
from AccessControl.SecurityManagement import newSecurityManager | ||
from AccessControl.SpecialUsers import system as user | ||
from Testing.makerequest import makerequest | ||
from Zope2.Startup.run import make_wsgi_app | ||
from zope.globalrequest import setRequest | ||
|
||
|
||
def runscript(zopeconf, script_name, *extra_args): | ||
make_wsgi_app({}, zopeconf) | ||
app = Zope2.app() | ||
app = makerequest(app) | ||
app.REQUEST['PARENTS'] = [app] | ||
setRequest(app.REQUEST) | ||
newSecurityManager(None, user) | ||
scriptglobals = globals() | ||
scriptglobals.update(__name__='__main__') | ||
with open(script_name) as script: | ||
scriptcode = script.read() | ||
exec(compile(scriptcode, script_name, 'exec')) | ||
|
||
|
||
def debug(zopeconf): | ||
make_wsgi_app({}, zopeconf) | ||
return Zope2.app() | ||
|
||
|
||
def debug_console(zopeconf): | ||
cmd = '{} -i -c "import sys; sys.path={}; from Zope2.utilities.zconsole import debug; app = debug(\\\"{}\\\")"'.format(sys.executable, sys.path, zopeconf) # noqa: E501 | ||
os.system(cmd) | ||
|
||
|
||
def main(args=sys.argv): | ||
import argparse | ||
parser = argparse.ArgumentParser(description='Zope console') | ||
parser.add_argument( | ||
'mode', | ||
choices=['run', 'debug'], | ||
help='mode of operation, run: run script; debug: interactive console') # noqa: E501 | ||
parser.add_argument('zopeconf', help='path to zope.conf') | ||
parser.add_argument('scriptargs', nargs=argparse.REMAINDER) | ||
namespace, unused = parser.parse_known_args(args[1:]) | ||
if namespace.mode == 'debug': | ||
debug_console(namespace.zopeconf) | ||
elif namespace.mode == 'run': | ||
runscript(namespace.zopeconf, *namespace.scriptargs) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |