conduit r1841 - in trunk: . conduit/utils test/python-tests



Author: jstowers
Date: Wed Jan 28 12:12:31 2009
New Revision: 1841
URL: http://svn.gnome.org/viewvc/conduit?rev=1841&view=rev

Log:
2009-01-29  John Stowers  <john stowers gmail com>

	* conduit/utils/__init__.py:
	* test/python-tests/TestCoreUtil.py:
	Add utility funtions, and tests, for executing OS commands



Modified:
   trunk/ChangeLog
   trunk/conduit/utils/__init__.py
   trunk/test/python-tests/TestCoreUtil.py

Modified: trunk/conduit/utils/__init__.py
==============================================================================
--- trunk/conduit/utils/__init__.py	(original)
+++ trunk/conduit/utils/__init__.py	Wed Jan 28 12:12:31 2009
@@ -15,6 +15,7 @@
 import time
 import re
 import logging
+import subprocess
 log = logging.getLogger("Utils")
 
 def _get_http_resource(url):
@@ -392,3 +393,17 @@
         
     return "%s%s%s" % (module.__name__, version, path)
 
+def exec_command_and_return_result(cmd, arg):
+    """
+    Executes a commmand and returns the result, or None on error
+    """
+    try:
+        p = subprocess.Popen([cmd, arg], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        out,err = p.communicate()
+        if p.returncode == 0:
+            return out
+        else:
+            return None
+    except OSError:
+        return None
+

Modified: trunk/test/python-tests/TestCoreUtil.py
==============================================================================
--- trunk/test/python-tests/TestCoreUtil.py	(original)
+++ trunk/test/python-tests/TestCoreUtil.py	Wed Jan 28 12:12:31 2009
@@ -62,6 +62,11 @@
 s = Utils.get_user_string()
 ok("user string: %s" % s, len(s) > 0 and type(s) == str)
 
+#test command line processing
+ok("Cmd executed", len(Utils.exec_command_and_return_result("ls",".")) > 0)
+ok("Cmd with wrong args", Utils.exec_command_and_return_result("ls","does-not-exist") == None) 
+ok("Cmd that doesnt exist", Utils.exec_command_and_return_result("cmd-does-not-exist",".") == None)  
+
 ts = 0
 dt = datetime.datetime(1970, 1, 1, 1, 0)
 ok("Datetime to unix timestamp", Utils.datetime_get_timestamp(dt) == ts)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]