Skip to content

Commit 516ad28

Browse files
authored
Merge pull request swiftlang#29244 from Rostepher/adopt-six-in-build-swift
[Build System: build-script] Adopt the six compatibility library in the build_swift module.
2 parents 3fd9098 + 0fdef59 commit 516ad28

File tree

18 files changed

+88
-70
lines changed

18 files changed

+88
-70
lines changed

utils/build_swift/build_swift/argparse/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
super-set of the argparse API and is meant to be used as a drop-in replacement.
1515
"""
1616

17+
from __future__ import absolute_import, unicode_literals
1718

1819
from argparse import (ArgumentDefaultsHelpFormatter, ArgumentError,
1920
ArgumentTypeError, FileType, HelpFormatter,

utils/build_swift/build_swift/argparse/actions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
"""
1414

1515

16+
from __future__ import absolute_import, unicode_literals
17+
1618
import argparse
1719
import copy
1820

21+
import six
22+
1923
from .types import BoolType, PathType
2024

2125

@@ -77,7 +81,7 @@ def __init__(self,
7781
if dests == argparse.SUPPRESS:
7882
dests = []
7983
metavar = metavar or ''
80-
elif isinstance(dests, str):
84+
elif isinstance(dests, six.string_types):
8185
dests = [dests]
8286
metavar = metavar or dests[0].upper()
8387

@@ -134,7 +138,7 @@ def __init__(self, option_strings, join=None, **kwargs):
134138
**kwargs)
135139

136140
def __call__(self, parser, namespace, values, option_string=None):
137-
if isinstance(values, str):
141+
if isinstance(values, six.string_types):
138142
values = [values]
139143

140144
for dest in self.dests:
@@ -339,5 +343,5 @@ def __call__(self, parser, namespace, values, option_string=None):
339343
if self.message is not None:
340344
parser.error(self.message)
341345

342-
arg = option_string or str(values)
346+
arg = option_string or six.text_type(values)
343347
parser.error('unsupported argument: {}'.format(arg))

utils/build_swift/build_swift/argparse/parser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
"""
1515

1616

17+
from __future__ import absolute_import, unicode_literals
18+
1719
import argparse
1820
from contextlib import contextmanager
1921

22+
import six
23+
2024
from . import Namespace, SUPPRESS, actions
2125
from .actions import Action
2226

@@ -129,7 +133,7 @@ def thunk(**kwargs):
129133
*names, action=action, **kwargs)
130134

131135
def add_positional(self, dests, action=None, **kwargs):
132-
if isinstance(dests, str):
136+
if isinstance(dests, six.string_types):
133137
dests = [dests]
134138

135139
if any(dest.startswith('-') for dest in dests):
@@ -141,7 +145,7 @@ def add_positional(self, dests, action=None, **kwargs):
141145
return self._add_argument(dests, action, **kwargs)
142146

143147
def add_option(self, option_strings, *actions, **kwargs):
144-
if isinstance(option_strings, str):
148+
if isinstance(option_strings, six.string_types):
145149
option_strings = [option_strings]
146150

147151
if not all(opt.startswith('-') for opt in option_strings):

utils/build_swift/build_swift/argparse/types.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
"""
1414

1515

16+
from __future__ import absolute_import, unicode_literals
17+
1618
import os.path
1719
import re
1820
import shlex
1921

22+
import six
23+
2024
from . import ArgumentTypeError
2125

2226

@@ -40,7 +44,7 @@ class CompilerVersion(object):
4044

4145
def __init__(self, *components):
4246
if len(components) == 1:
43-
if isinstance(components[0], str):
47+
if isinstance(components[0], six.string_types):
4448
components = components[0].split('.')
4549
elif isinstance(components[0], (list, tuple)):
4650
components = components[0]
@@ -54,7 +58,7 @@ def __eq__(self, other):
5458
return self.components == other.components
5559

5660
def __str__(self):
57-
return '.'.join([str(part) for part in self.components])
61+
return '.'.join([six.text_type(part) for part in self.components])
5862

5963

6064
# -----------------------------------------------------------------------------

utils/build_swift/build_swift/defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"""
1313

1414

15+
from __future__ import absolute_import, unicode_literals
16+
1517
from .argparse import CompilerVersion
1618

1719

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88

99

10+
from __future__ import absolute_import, unicode_literals
11+
1012
import multiprocessing
1113
import os
1214

utils/build_swift/build_swift/migration.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,12 @@
1717
import itertools
1818
import subprocess
1919

20+
from six.moves import map
21+
2022
from swift_build_support.swift_build_support.targets import \
2123
StdlibDeploymentTarget
2224

2325

24-
try:
25-
# Python 2
26-
from itertools import imap
27-
except ImportError:
28-
imap = map
29-
30-
31-
try:
32-
# Python 2
33-
unicode
34-
except NameError:
35-
unicode = str
36-
37-
3826
__all__ = [
3927
'UnknownSDKError',
4028

@@ -94,12 +82,12 @@ def _migrate_swift_sdks_arg(arg):
9482
sdks = arg.split('=')[1]
9583
sdk_list = [] if sdks == '' else sdks.split(';')
9684

97-
targets = _flatten(imap(_swift_sdk_to_stdlib_targets, sdk_list))
85+
targets = _flatten(map(_swift_sdk_to_stdlib_targets, sdk_list))
9886
target_names = [target.name for target in targets]
9987

10088
return '--stdlib-deployment-targets={}'.format(' '.join(target_names))
10189

102-
return list(imap(_migrate_swift_sdks_arg, args))
90+
return list(map(_migrate_swift_sdks_arg, args))
10391

10492

10593
# -----------------------------------------------------------------------------

utils/build_swift/build_swift/presets.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717
from collections import namedtuple
1818
from contextlib import contextmanager
1919

20-
try:
21-
# Python 2
22-
import ConfigParser as configparser
23-
from StringIO import StringIO
24-
except ImportError:
25-
import configparser
26-
from io import StringIO
20+
from six import StringIO
21+
from six.moves import configparser
2722

2823

2924
__all__ = [

utils/build_swift/tests/argparse/test_actions.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88

99

10+
from __future__ import absolute_import, unicode_literals
11+
12+
import six
13+
1014
from ..utils import TestCase, redirect_stderr
1115
from ...build_swift.argparse import (
1216
ArgumentParser, BoolType, Nargs, PathType, SUPPRESS, actions)
@@ -179,7 +183,7 @@ def test_valid_int(self):
179183
parser.add_argument('--foo', action=actions.StoreIntAction)
180184

181185
for i in [0, 1, 42, -64]:
182-
args = parser.parse_args(['--foo', str(i)])
186+
args = parser.parse_args(['--foo', six.text_type(i)])
183187
self.assertEqual(args.foo, i)
184188

185189
def test_invalid_int(self):
@@ -188,7 +192,7 @@ def test_invalid_int(self):
188192

189193
for i in [0.0, True, 'bar']:
190194
with self.quietOutput(), self.assertRaises(SystemExit):
191-
parser.parse_args(['--foo', str(i)])
195+
parser.parse_args(['--foo', six.text_type(i)])
192196

193197

194198
class TestStoreTrueAction(TestCase):
@@ -294,7 +298,7 @@ def test_with_optional_true_arg(self):
294298
parser.add_argument('--foo', action=actions.ToggleTrueAction)
295299

296300
for value in BoolType.TRUE_VALUES:
297-
args = parser.parse_args(['--foo', str(value)])
301+
args = parser.parse_args(['--foo', six.text_type(value)])
298302
self.assertTrue(args.foo)
299303

300304
args = parser.parse_args(['--foo={}'.format(value)])
@@ -305,7 +309,7 @@ def test_with_optional_false_arg(self):
305309
parser.add_argument('--foo', action=actions.ToggleTrueAction)
306310

307311
for value in BoolType.FALSE_VALUES:
308-
args = parser.parse_args(['--foo', str(value)])
312+
args = parser.parse_args(['--foo', six.text_type(value)])
309313
self.assertFalse(args.foo)
310314

311315
args = parser.parse_args(['--foo={}'.format(value)])
@@ -356,7 +360,7 @@ def test_with_optional_true_arg(self):
356360
parser.add_argument('--foo', action=actions.ToggleFalseAction)
357361

358362
for value in BoolType.TRUE_VALUES:
359-
args = parser.parse_args(['--foo', str(value)])
363+
args = parser.parse_args(['--foo', six.text_type(value)])
360364
self.assertFalse(args.foo)
361365

362366
args = parser.parse_args(['--foo={}'.format(value)])
@@ -367,7 +371,7 @@ def test_with_optional_false_arg(self):
367371
parser.add_argument('--foo', action=actions.ToggleFalseAction)
368372

369373
for value in BoolType.FALSE_VALUES:
370-
args = parser.parse_args(['--foo', str(value)])
374+
args = parser.parse_args(['--foo', six.text_type(value)])
371375
self.assertTrue(args.foo)
372376

373377
args = parser.parse_args(['--foo={}'.format(value)])

utils/build_swift/tests/argparse/test_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88

99

10+
from __future__ import absolute_import, unicode_literals
11+
1012
from argparse import _ArgumentGroup, _MutuallyExclusiveGroup
1113

1214
from ..utils import TestCase

utils/build_swift/tests/argparse/test_types.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88

99

10+
from __future__ import absolute_import, unicode_literals
11+
1012
import os.path
1113
import platform
1214

15+
import six
16+
1317
from ..utils import TestCase
1418
from ...build_swift.argparse import ArgumentTypeError, types
1519

@@ -67,10 +71,10 @@ def test_eq(self):
6771

6872
def test_str(self):
6973
version = types.CompilerVersion('1.0.0')
70-
self.assertEqual(str(version), '1.0.0')
74+
self.assertEqual(six.text_type(version), '1.0.0')
7175

7276
version = types.CompilerVersion('1.0.0.1')
73-
self.assertEqual(str(version), '1.0.0.1')
77+
self.assertEqual(six.text_type(version), '1.0.0.1')
7478

7579

7680
class TestBoolType(TestCase):

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88

99

10+
from __future__ import absolute_import, unicode_literals
11+
1012
import multiprocessing
1113

1214
from swift_build_support.swift_build_support import host

0 commit comments

Comments
 (0)