Skip to content

Commit

Permalink
Don't use subprocess.check_output()
Browse files Browse the repository at this point in the history
It isn't in Python 2.6
  • Loading branch information
seanh committed Jul 2, 2014
1 parent 086fdeb commit 9016127
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
39 changes: 38 additions & 1 deletion ckan/new_tests/test_coding_standards.py
Expand Up @@ -9,6 +9,43 @@
import subprocess


# We implement our own check_output() function because
# subprocess.check_output() isn't in Python 2.6.
# This code is copy-pasted from Python 2.7:
# http://hg.python.org/cpython/file/d37f963394aa/Lib/subprocess.py#l544
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use stderr=STDOUT.
>>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd, output=output)
return output


def test_building_the_docs():
'''There should be no warnings or errors when building the Sphinx docs.
Expand All @@ -20,7 +57,7 @@ def test_building_the_docs():
'''
try:
output = subprocess.check_output(
output = check_output(
['python', 'setup.py', 'build_sphinx', '--all-files', '--fresh-env'],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
Expand Down
39 changes: 38 additions & 1 deletion doc/conf.py
Expand Up @@ -115,6 +115,43 @@
release = ckan.__version__


# We implement our own check_output() function because
# subprocess.check_output() isn't in Python 2.6.
# This code is copy-pasted from Python 2.7:
# http://hg.python.org/cpython/file/d37f963394aa/Lib/subprocess.py#l544
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use stderr=STDOUT.
>>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd, output=output)
return output


def latest_release_tag():
'''Return the name of the git tag for the latest stable release.
Expand All @@ -123,7 +160,7 @@ def latest_release_tag():
This requires git to be installed.
'''
git_tags = subprocess.check_output(['git', 'tag', '-l']).split()
git_tags = check_output(['git', 'tag', '-l']).split()

# FIXME: We could do more careful pattern matching against ckan-X.Y.Z here.
release_tags = [tag for tag in git_tags if tag.startswith('ckan-')]
Expand Down

0 comments on commit 9016127

Please sign in to comment.