You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by ad...@apache.org on 2014/04/26 18:53:22 UTC
svn commit: r1590250 - in /labs/panopticon: src/asf/utils/execute.py
tests/test_execute.py
Author: adc
Date: Sat Apr 26 16:53:22 2014
New Revision: 1590250
URL: http://svn.apache.org/r1590250
Log:
Handy execute function
Added:
labs/panopticon/src/asf/utils/execute.py
labs/panopticon/tests/test_execute.py
Added: labs/panopticon/src/asf/utils/execute.py
URL: http://svn.apache.org/viewvc/labs/panopticon/src/asf/utils/execute.py?rev=1590250&view=auto
==============================================================================
--- labs/panopticon/src/asf/utils/execute.py (added)
+++ labs/panopticon/src/asf/utils/execute.py Sat Apr 26 16:53:22 2014
@@ -0,0 +1,50 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+""" Various utility functions to support command execution
+"""
+import subprocess
+
+
+class ExecutionError(ValueError):
+ def __init__(self, code, stdout, stderr):
+ self.code = code
+ self.stdout = stdout
+ self.stderr = stderr
+
+
+def execute(command):
+ """ Executes a command and returns both stdout and stderr as lists
+ :param str command: the command to be executed
+ :returns stdout, stderr as lists
+ :raises ExecutionError if the command does not execute correctly.
+ """
+ p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+ stdout = []
+ for line in p.stdout.readlines():
+ stdout.append(line.strip())
+ stderr = []
+ for line in p.stdout.readlines():
+ stderr.append(line.strip())
+
+ code = p.wait()
+ if code:
+ raise ExecutionError(code, stdout, stderr)
+
+ return stdout, stderr
Added: labs/panopticon/tests/test_execute.py
URL: http://svn.apache.org/viewvc/labs/panopticon/tests/test_execute.py?rev=1590250&view=auto
==============================================================================
--- labs/panopticon/tests/test_execute.py (added)
+++ labs/panopticon/tests/test_execute.py Sat Apr 26 16:53:22 2014
@@ -0,0 +1,39 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+import os
+
+from nose.tools import assert_equals, assert_raises
+
+from asf.utils.execute import execute, ExecutionError
+
+
+def test_execute():
+ data_dir = os.path.join(os.path.dirname(__file__), 'data')
+ stdout, stderr = execute('ls %s' % data_dir)
+ assert_equals(['podlings.xml'], stdout)
+ assert_equals([], stderr)
+
+def test_execute_fail():
+ try:
+ execute('ls ZZZ')
+ assert False, 'Execution should have thrown an error'
+ except ExecutionError as ee:
+ assert_equals(1, ee.code)
+ assert_equals([], ee.stdout)
+ assert_equals([], ee.stderr)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org