[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Adding an easy method for getting a controller instance
commit f999b627dc3ab00cdd7c245d4811ad7b0a2cc20c
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sat Aug 18 11:21:08 2012 -0700
Adding an easy method for getting a controller instance
Adding a 'prompt' script that kicks off a python interpretor with a readily
available stem.control.Controller instance. This starts a tor test instance if
it isn't already running and offers to shut it down when done.
The purpose of this script is to make it easy to get a controller instance we
can exercise. The sh shebang makes this *nix specific, though I suppose windows
users can still use it by running 'bash prompt'.
Example usage:
atagar@morrigan:~/Desktop/stem$ ./prompt
Welcome to stem's testing prompt. You currently have a controller available
via the 'control' variable.
>>> control.get_info('version')
'0.2.1.30'
>>> quit()
Would you like to stop the tor instance we made? (y/n, default: n): y
---
prompt | 19 +++++++++++
test/__init__.py | 1 +
test/prompt.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 109 insertions(+), 0 deletions(-)
diff --git a/prompt b/prompt
new file mode 100755
index 0000000..e833fd2
--- /dev/null
+++ b/prompt
@@ -0,0 +1,19 @@
+#!/bin/sh
+#
+# Provides a quick method of getting a controller that we can use to test stem
+# with. This starts tor if it isn't already running and provides us with a
+# controller instance. When done it gives us the option to stop the tor.
+#
+# atagar@morrigan:~/Desktop/stem$ ./prompt
+# Welcome to stem's testing prompt. You currently have a controller available
+# via the 'control' variable.
+#
+# >>> control.get_info("version")
+# '0.2.1.30'
+# >>> quit()
+#
+# Would you like to stop the tor instance we made? (y/n, default: n): y
+
+python -i -c "import test.prompt; test.prompt.print_usage(); control = test.prompt.controller()"
+python -c "import test.prompt; test.prompt.stop(True)"
+
diff --git a/test/__init__.py b/test/__init__.py
index c0297aa..78d6543 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -6,6 +6,7 @@ __all__ = [
"check_whitespace",
"mocking",
"output",
+ "prompt",
"runner",
]
diff --git a/test/prompt.py b/test/prompt.py
new file mode 100644
index 0000000..eccdbec
--- /dev/null
+++ b/test/prompt.py
@@ -0,0 +1,89 @@
+"""
+Simple helper methods to make troubleshooting with the python interpretor
+easier.
+
+::
+
+ >>> from test.prompt import *
+ >>> control = controller()
+ >>> control.get_info("version")
+ '0.2.1.30'
+
+ >>> is_running()
+ True
+
+ >>> stop()
+"""
+
+import os
+import signal
+
+import stem.control
+import stem.process
+import stem.util.system
+
+CONTROL_PORT = 2779
+
+STOP_CONFIRMATION = "Would you like to stop the tor instance we made? (y/n, default: n): "
+
+def print_usage():
+ """
+ Provides a welcoming message.
+ """
+
+ print "Welcome to stem's testing prompt. You currently have a controller available"
+ print "via the 'control' variable."
+ print
+
+def start():
+ """
+ Starts up a tor instance that we can attach a contorller to.
+ """
+
+ tor_config = {
+ 'SocksPort': '0',
+ 'ControlPort': str(CONTROL_PORT),
+ 'ExitPolicy': 'reject *:*',
+ }
+
+ stem.process.launch_tor_with_config(config = tor_config, completion_percent = 5)
+
+def stop(prompt = False):
+ """
+ Stops the tor instance spawned by this module.
+
+ :param bool prompt: asks user for confirmation that they would like to stop tor if True
+ """
+
+ tor_pid = stem.util.system.get_pid_by_port(CONTROL_PORT)
+
+ if tor_pid:
+ if prompt:
+ print
+ response = raw_input(STOP_CONFIRMATION)
+ if not response.lower() in ("y", "yes"): return
+
+ os.kill(tor_pid, signal.SIGTERM)
+
+def is_running():
+ """
+ Checks if we're likely running a tor instance spawned by this module. This is
+ simply a check if our custom control port is in use, so it can be confused by
+ other applications (not likely, but possable).
+
+ :returns: True if the control port is used, False otherwise
+ """
+
+ return bool(stem.util.system.get_pid_by_port(CONTROL_PORT))
+
+def controller():
+ """
+ Provides a Controller for our tor instance. This starts tor if it isn't
+ already running.
+ """
+
+ if not is_running(): start()
+ control = stem.control.Controller.from_port(control_port = CONTROL_PORT)
+ control.authenticate()
+ return control
+
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits