scripts/release/util.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 13 Mar 2009 23:01:11 +0000
changeset 1839 92b28d1bf2f5
parent 1835 3f30b7b14c57
child 1846 ac30e04bcbba
permissions -rw-r--r--
Added example and help text to clarify the use of some fields on the proposal ranking. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     1
# Copyright 2009 the Melange authors.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     2
#
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     3
# Licensed under the Apache License, Version 2.0 (the "License");
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     4
# you may not use this file except in compliance with the License.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     5
# You may obtain a copy of the License at
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     6
#
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     7
#   http://www.apache.org/licenses/LICENSE-2.0
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     8
#
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
     9
# Unless required by applicable law or agreed to in writing, software
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    10
# distributed under the License is distributed on an "AS IS" BASIS,
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    12
# See the License for the specific language governing permissions and
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    13
# limitations under the License.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    14
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    15
"""Various utilities.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    16
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    17
Current contents:
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    18
 - Text colorization using ANSI color codes
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    19
 - A class to construct and manage paths under a root path.
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    20
 - A helper to manage running subprocesses, wrapping the subprocess module.
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    21
"""
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    22
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    23
__authors__ = [
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    24
    # alphabetical order by last name, please
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    25
    '"David Anderson" <dave@natulte.net>',
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    26
    ]
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    27
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    28
import os.path
1833
9df2e9a67081 Add a decolorize() helper to undo the effects of colorize().
David Anderson <david.jc.anderson@gmail.com>
parents: 1827
diff changeset
    29
import re
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    30
import subprocess
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    31
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    32
import error
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    33
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    34
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    35
class Error(error.Error):
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    36
  pass
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    37
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    38
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
    39
class SubprocessFailed(Error):
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    40
  """A subprocess returned a non-zero error code."""
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    41
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    42
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    43
# The magic escape sequence understood by modern terminal emulators to
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    44
# configure fore/background colors and other basic text display
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    45
# settings.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    46
_ANSI_ESCAPE = '\x1b[%dm'
1833
9df2e9a67081 Add a decolorize() helper to undo the effects of colorize().
David Anderson <david.jc.anderson@gmail.com>
parents: 1827
diff changeset
    47
_ANSI_ESCAPE_RE = re.compile(r'\x1b\[\d+m')
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    48
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    49
1826
12de6d73a908 Followup to r2496. Fix obvious errors.
David Anderson <david.jc.anderson@gmail.com>
parents: 1825
diff changeset
    50
# Some internal non-color settings that we use.
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    51
_RESET = 0  # Reset to terminal defaults.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    52
_BOLD = 1   # Brighter colors.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    53
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    54
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    55
# ANSI color codes.
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    56
RED = 31
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    57
GREEN = 32
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    58
WHITE = 37
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    59
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    60
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    61
def _ansi_escape(code):
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    62
  return _ANSI_ESCAPE % code
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    63
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    64
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    65
def colorize(text, color, bold=False):
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    66
  """Colorize some text using ANSI color codes.
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    67
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    68
  Note that while ANSI color codes look good in a terminal they look
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    69
  like noise in log files unless viewed in an ANSI color capable
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    70
  viewer (such as 'less -R').
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    71
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    72
  Args:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    73
    text: The text to colorize.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    74
    color: One of the color symbols from this module.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    75
    bold: If True, make the color brighter.
1824
c54c304e3c0e Refactor ANSI colorization into a new utility module.
David Anderson <david.jc.anderson@gmail.com>
parents:
diff changeset
    76
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    77
  Returns:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    78
    The input text string, appropriately sprinkled with color
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    79
    codes. Colors are reset to terminal defaults after the input
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    80
    text.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    81
  """
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    82
  bold = _ansi_escape(_BOLD) if bold else ''
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    83
  return '%s%s%s%s' % (bold, _ansi_escape(color),
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    84
                       text, _ansi_escape(_RESET))
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    85
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    86
1833
9df2e9a67081 Add a decolorize() helper to undo the effects of colorize().
David Anderson <david.jc.anderson@gmail.com>
parents: 1827
diff changeset
    87
def decolorize(text):
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    88
  """Remove ANSI color codes from text."""
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    89
  return _ANSI_ESCAPE_RE.sub('', text)
1833
9df2e9a67081 Add a decolorize() helper to undo the effects of colorize().
David Anderson <david.jc.anderson@gmail.com>
parents: 1827
diff changeset
    90
9df2e9a67081 Add a decolorize() helper to undo the effects of colorize().
David Anderson <david.jc.anderson@gmail.com>
parents: 1827
diff changeset
    91
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    92
class Paths(object):
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    93
  """A helper to construct and check paths under a given root."""
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    94
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    95
  def __init__(self, root):
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    96
    """Initializer.
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
    97
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    98
    Args:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
    99
      root: The root of all paths this instance will consider.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   100
    """
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   101
    self._root = os.path.abspath(
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   102
      os.path.expandvars(os.path.expanduser(root)))
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
   103
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   104
  def path(self, path=''):
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   105
    """Construct and return a path under the path root.
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
   106
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   107
    Args:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   108
      path: The desired path string relative to the root.
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
   109
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   110
    Returns:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   111
      The absolute path corresponding to the relative input path.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   112
    """
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   113
    assert not os.path.isabs(path)
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   114
    return os.path.abspath(os.path.join(self._root, path))
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
   115
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   116
  def exists(self, path=''):
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   117
    """Check for the existence of a path under the path root.
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
   118
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   119
    Does not discriminate on the path type (ie. it could be a
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   120
    directory, a file, a symbolic link...), just checks for the
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   121
    existence of the path.
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
   122
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   123
    Args:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   124
      path: The path string relative to the root.
1825
a610a2df83d2 Move the Paths class to the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1824
diff changeset
   125
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   126
    Returns:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   127
      True if the path exists, False otherwise.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   128
    """
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   129
    return os.path.exists(self.path(path))
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   130
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   131
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   132
def run(argv, cwd=None, capture=False, split_capture=True, stdin=''):
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   133
  """Run the given command and optionally return its output.
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   134
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   135
  Note that if you set capture=True, the command's output is
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   136
  buffered in memory. Output capture should only be used with
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   137
  commands that output small amounts of data. O(kB) is fine, O(MB)
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   138
  is starting to push it a little.
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   139
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   140
  Args:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   141
    argv: A list containing the name of the program to run, followed
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   142
      by its argument vector.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   143
    cwd: Run the program from this directory.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   144
    capture: If True, capture the program's stdout stream. If False,
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   145
         stdout will output to sys.stdout.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   146
    split_capture: If True, return the captured output as a list of
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   147
           lines. Else, return as a single unaltered string.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   148
    stdin: The string to feed to the program's stdin stream.
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   149
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   150
  Returns:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   151
    If capture is True, a string containing the combined
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   152
    stdout/stderr output of the program. If capture is False,
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   153
    nothing is returned.
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   154
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   155
  Raises:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   156
    SubprocessFailed: The subprocess exited with a non-zero exit
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   157
            code.
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   158
  """
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   159
  print colorize('# ' + ' '.join(argv), WHITE, bold=True)
1827
c03995a6a88e Move run() into the util module.
David Anderson <david.jc.anderson@gmail.com>
parents: 1826
diff changeset
   160
1835
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   161
  process = subprocess.Popen(argv,
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   162
                             shell=False,
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   163
                             cwd=cwd,
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   164
                             stdin=subprocess.PIPE,
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   165
                             stdout=(subprocess.PIPE if capture else None),
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   166
                             stderr=None)
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   167
  output, _ = process.communicate(input=stdin)
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   168
  if process.returncode != 0:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   169
    raise SubprocessFailed('Process %s failed with output: %s' %
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   170
                           (argv[0], output))
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   171
  if output is not None and split_capture:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   172
    return output.strip().split('\n')
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   173
  else:
3f30b7b14c57 Fix indentation to match Melange/Google style.
David Anderson <david.jc.anderson@gmail.com>
parents: 1833
diff changeset
   174
    return output