Skip to content

Commit c3397f7

Browse files
committed
Merge pull request #2858 from rintaro/build-script-capture
[build-script] Consistent use of shell.capture in swift_build_support
2 parents 47cd951 + 3384d2d commit c3397f7

File tree

10 files changed

+67
-54
lines changed

10 files changed

+67
-54
lines changed

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#
1515
# ----------------------------------------------------------------------------
1616

17-
import subprocess
17+
from __future__ import absolute_import
1818

1919
from numbers import Number
2020

21+
from . import shell
22+
2123

2224
class CMakeOptions(object):
2325
"""List like object used to define cmake options
@@ -125,8 +127,8 @@ def build_args(self):
125127
toolchain = self.toolchain
126128
jobs = args.build_jobs
127129
if args.distcc:
128-
jobs = str(subprocess.check_output(
129-
[toolchain.distcc, '-j']).decode()).rstrip()
130+
jobs = shell.capture([toolchain.distcc, '-j'],
131+
dry_run=False, echo=False).rstrip()
130132

131133
build_args = list(args.build_args)
132134

utils/swift_build_support/swift_build_support/debug.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@
1414
#
1515
# ----------------------------------------------------------------------------
1616

17+
from __future__ import absolute_import
1718
from __future__ import print_function
1819

19-
import subprocess
2020
import sys
2121

22-
23-
def _output(args):
24-
try:
25-
out = subprocess.check_output(args, stderr=subprocess.PIPE)
26-
return out.rstrip().decode()
27-
except subprocess.CalledProcessError:
28-
return None
22+
from . import shell
2923

3024

3125
def print_xcodebuild_versions(file=sys.stdout):
3226
"""
3327
Print the host machine's `xcodebuild` version, as well as version
3428
information for all available SDKs.
3529
"""
36-
print(u'{}\n'.format(_output(['xcodebuild', '-version'])), file=file)
37-
print(u'--- SDK versions ---', file=file)
38-
print(u'{}\n'.format(_output(['xcodebuild', '-version', '-sdk'])),
39-
file=file)
30+
version = shell.capture(
31+
['xcodebuild', '-version'], dry_run=False, echo=False).rstrip()
32+
sdks = shell.capture(
33+
['xcodebuild', '-version', '-sdk'], dry_run=False, echo=False).rstrip()
34+
fmt = """\
35+
{version}
36+
37+
--- SDK versions ---
38+
{sdks}
39+
40+
"""
41+
print(fmt.format(version=version, sdks=sdks), file=file)
4042
file.flush()
41-
# You can't test beyond this because each developer's machines may have
42-
# a different set of SDKs installed.

utils/swift_build_support/swift_build_support/shell.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
from . import diagnostics
2626

27+
28+
DEVNULL = getattr(subprocess, 'DEVNULL', subprocess.PIPE)
29+
2730
dry_run = False
2831

2932

@@ -88,7 +91,8 @@ def call(command, stderr=None, env=None, dry_run=None, echo=True):
8891
"': " + e.strerror)
8992

9093

91-
def capture(command, stderr=None, env=None, dry_run=None, echo=True):
94+
def capture(command, stderr=None, env=None, dry_run=None, echo=True,
95+
optional=False):
9296
"""
9397
capture(command, ...) -> str
9498
@@ -106,8 +110,12 @@ def capture(command, stderr=None, env=None, dry_run=None, echo=True):
106110
_env = dict(os.environ)
107111
_env.update(env)
108112
try:
109-
return subprocess.check_output(command, env=_env, stderr=stderr)
113+
out = subprocess.check_output(command, env=_env, stderr=stderr)
114+
# Coerce to `str` hack. not py3 `byte`, not py2 `unicode`.
115+
return str(out.decode())
110116
except subprocess.CalledProcessError as e:
117+
if optional:
118+
return None
111119
diagnostics.fatal(
112120
"command terminated with a non-zero exit status " +
113121
str(e.returncode) + ", aborting")

utils/swift_build_support/swift_build_support/tar.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from __future__ import absolute_import
1212

1313
import platform
14-
import subprocess
1514

1615
from . import shell
1716

@@ -29,7 +28,7 @@ def tar(source, destination):
2928
if platform.system() != 'Darwin':
3029
args += ['--owner=0', '--group=0']
3130

32-
# Capture stderr output such as 'tar: Failed to open ...'. We'll detect
33-
# these cases using the exit code, which should cause 'check_call' to
31+
# Discard stderr output such as 'tar: Failed to open ...'. We'll detect
32+
# these cases using the exit code, which should cause 'shell.call' to
3433
# raise.
35-
shell.call(args + [source], stderr=subprocess.PIPE)
34+
shell.call(args + [source], stderr=shell.DEVNULL)

utils/swift_build_support/swift_build_support/toolchain.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
from __future__ import absolute_import
1818

1919
import platform
20-
import subprocess
2120

2221
from . import cache_util
22+
from . import shell
2323
from . import xcrun
2424
from .which import which
2525

@@ -151,13 +151,13 @@ def _release_date():
151151
"""Return the release date for FreeBSD operating system on this host.
152152
If the release date cannot be ascertained, return None.
153153
"""
154-
try:
155-
# For details on `sysctl`, see:
156-
# http://www.freebsd.org/cgi/man.cgi?sysctl(8)
157-
out = subprocess.check_output(['sysctl', '-n', 'kern.osreldate'])
158-
return int(out)
159-
except subprocess.CalledProcessError:
154+
# For details on `sysctl`, see:
155+
# http://www.freebsd.org/cgi/man.cgi?sysctl(8)
156+
out = shell.capture(['sysctl', '-n', 'kern.osreldate'],
157+
dry_run=False, echo=False, optional=True)
158+
if out is None:
160159
return None
160+
return int(out)
161161

162162

163163
class Cygwin(Linux):

utils/swift_build_support/swift_build_support/which.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
from __future__ import absolute_import
2020

21-
import subprocess
22-
2321
from . import cache_util
22+
from . import shell
2423

2524

2625
@cache_util.cached
@@ -35,8 +34,8 @@ def which(cmd):
3534
We provide our own implementation because shutil.which() has not
3635
been backported to Python 2.7, which we support.
3736
"""
38-
try:
39-
ret = subprocess.check_output(['which', cmd])
40-
return str(ret.rstrip().decode())
41-
except subprocess.CalledProcessError:
37+
out = shell.capture(['which', cmd],
38+
dry_run=False, echo=False, optional=True)
39+
if out is None:
4240
return None
41+
return out.rstrip()

utils/swift_build_support/swift_build_support/xcrun.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
from __future__ import absolute_import
1818

19-
import subprocess
20-
2119
from . import cache_util
20+
from . import shell
2221

2322

2423
@cache_util.cached
@@ -35,14 +34,14 @@ def find(tool, sdk=None, toolchain=None):
3534
if toolchain is not None:
3635
command += ['--toolchain', toolchain]
3736

38-
try:
39-
# `xcrun --find` prints to stderr when it fails to find the
40-
# given tool. We swallow that output with a pipe.
41-
out = subprocess.check_output(command,
42-
stderr=subprocess.PIPE)
43-
return str(out.rstrip().decode())
44-
except subprocess.CalledProcessError:
37+
# `xcrun --find` prints to stderr when it fails to find the
38+
# given tool. We swallow that output with a pipe.
39+
out = shell.capture(
40+
command,
41+
stderr=shell.DEVNULL, dry_run=False, echo=False, optional=True)
42+
if out is None:
4543
return None
44+
return out.rstrip()
4645

4746

4847
@cache_util.cached
@@ -53,8 +52,7 @@ def sdk_path(sdk):
5352
If `xcrun --show-sdk-path` cannot find the SDK, return None.
5453
"""
5554
command = ['xcrun', '--sdk', sdk, '--show-sdk-path']
56-
try:
57-
out = subprocess.check_output(command)
58-
return str(out.rstrip().decode())
59-
except subprocess.CalledProcessError:
55+
out = shell.capture(command, dry_run=False, echo=False, optional=True)
56+
if out is None:
6057
return None
58+
return out.rstrip()

utils/swift_build_support/tests/test_arguments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
from io import StringIO
2323

2424
from swift_build_support.arguments import (
25-
type as argtype,
2625
action as argaction,
26+
type as argtype,
2727
)
2828

2929

utils/swift_build_support/tests/test_debug.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
import platform
1212
import unittest
13-
14-
# StringIO import path differs across Python 2 and 3.
1513
try:
16-
from io import StringIO
14+
# py2
15+
from StringIO import StringIO
1716
except ImportError:
18-
from cStringIO import StringIO
17+
# py3
18+
from io import StringIO
1919

2020
from swift_build_support import debug
2121

@@ -33,6 +33,8 @@ def test_outputs_xcode_version(self):
3333
self.assertTrue(actual[0].startswith('Xcode '))
3434
self.assertTrue(actual[1].startswith('Build version '))
3535
self.assertEqual(actual[3], '--- SDK versions ---')
36+
# You can't test beyond this because each developer's machines may have
37+
# a different set of SDKs installed.
3638

3739

3840
if __name__ == '__main__':

utils/swift_build_support/tests/test_shell.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def test_capture(self):
6969
with self.assertRaises(SystemExit):
7070
shell.capture(["false"])
7171

72+
self.assertIsNone(shell.capture(["false"], optional=True))
73+
74+
with self.assertRaises(SystemExit):
75+
shell.capture(["**not-a-command**"], optional=True)
76+
7277
def test_rmtree(self):
7378
shell.dry_run = False
7479
path = os.path.join(self.tmpdir, 'foo', 'bar')

0 commit comments

Comments
 (0)