Skip to content

Commit dadd43c

Browse files
authored
Merge pull request #29262 from Rostepher/migrate-migration-module
[NFC][Build System: build-script] Move the migration module.
2 parents 5fc2cfa + bb22690 commit dadd43c

File tree

8 files changed

+193
-207
lines changed

8 files changed

+193
-207
lines changed

utils/build-script

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ import time
2121

2222
from build_swift.build_swift import defaults
2323
from build_swift.build_swift import driver_arguments
24+
from build_swift.build_swift import migration
2425
from build_swift.build_swift import presets
25-
from build_swift.build_swift.migration import migrate_swift_sdks
2626

2727
from swift_build_support.swift_build_support import (
2828
arguments,
2929
debug,
3030
diagnostics,
31-
migration,
3231
products,
3332
shell,
3433
tar,
@@ -991,7 +990,7 @@ def main_preset():
991990
except presets.Error as e:
992991
diagnostics.fatal(e.message)
993992

994-
preset_args = migrate_swift_sdks(preset.format_args())
993+
preset_args = migration.migrate_swift_sdks(preset.format_args())
995994

996995
if args.distcc and (args.cmake_c_launcher or args.cmake_cxx_launcher):
997996
diagnostics.fatal(

utils/build_swift/build_swift/migration.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
Temporary module with functionaly used to migrate away from build-script-impl.
1212
"""
1313

14+
1415
from __future__ import absolute_import, unicode_literals
1516

1617
import itertools
18+
import subprocess
1719

1820
from swift_build_support.swift_build_support.targets import \
1921
StdlibDeploymentTarget
@@ -26,9 +28,19 @@
2628
imap = map
2729

2830

31+
try:
32+
# Python 2
33+
unicode
34+
except NameError:
35+
unicode = str
36+
37+
2938
__all__ = [
3039
'UnknownSDKError',
40+
41+
'check_impl_args',
3142
'migrate_swift_sdks',
43+
'parse_args',
3244
]
3345

3446

@@ -88,3 +100,63 @@ def _migrate_swift_sdks_arg(arg):
88100
return '--stdlib-deployment-targets={}'.format(' '.join(target_names))
89101

90102
return list(imap(_migrate_swift_sdks_arg, args))
103+
104+
105+
# -----------------------------------------------------------------------------
106+
107+
def _process_disambiguation_arguments(args, unknown_args):
108+
"""These arguments are only listed in the driver arguments to stop argparse
109+
from auto expanding arguments like --install-swift to the known argument
110+
--install-swiftevolve. Remove them from args and add them to unknown_args
111+
again.
112+
"""
113+
114+
if hasattr(args, 'impl_skip_test_swift'):
115+
if args.impl_skip_test_swift:
116+
unknown_args.append('--skip-test-swift')
117+
del args.impl_skip_test_swift
118+
119+
if hasattr(args, 'impl_install_swift'):
120+
if args.impl_install_swift:
121+
unknown_args.append('--install-swift')
122+
del args.impl_install_swift
123+
124+
return args, unknown_args
125+
126+
127+
def parse_args(parser, args, namespace=None):
128+
"""Parses a given argument list with the given argparse.ArgumentParser.
129+
130+
Return a processed arguments object. Any unknown arguments are stored in
131+
`build_script_impl_args` attribute as a list. Ignores '--' to be compatible
132+
with old style argument list.
133+
"""
134+
135+
args = [arg for arg in args if arg != '--']
136+
args, unknown_args = parser.parse_known_args(args, namespace)
137+
args, unknown_args = _process_disambiguation_arguments(args, unknown_args)
138+
139+
args.build_script_impl_args = unknown_args
140+
141+
return args
142+
143+
144+
# -----------------------------------------------------------------------------
145+
146+
def check_impl_args(build_script_impl, args):
147+
"""Check whether given argv are all known arguments for
148+
`build-script-impl`.
149+
150+
Raises a ValueError if any invalid argument is found. Return nothing
151+
otherwise.
152+
"""
153+
154+
pipe = subprocess.Popen(
155+
[build_script_impl, '--check-args-only=1'] + args,
156+
stdout=subprocess.PIPE,
157+
stderr=subprocess.PIPE)
158+
159+
_, err = pipe.communicate()
160+
161+
if pipe.returncode != 0:
162+
raise ValueError(unicode(err.splitlines()[0].decode()))

utils/build_swift/tests/test_driver_arguments.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,16 @@
1414
from contextlib import contextmanager
1515
from io import StringIO
1616

17-
from swift_build_support.swift_build_support import migration
1817
from swift_build_support.swift_build_support.SwiftBuildSupport import (
1918
get_all_preset_names, get_preset_options)
2019

2120
from . import expected_options as eo
21+
from .utils import BUILD_SCRIPT_IMPL_PATH, UTILS_PATH
2222
from ..build_swift import argparse
2323
from ..build_swift import driver_arguments
24+
from ..build_swift import migration
2425

2526

26-
FILE_PATH = os.path.abspath(__file__)
27-
TESTS_PATH = os.path.abspath(os.path.join(FILE_PATH, os.pardir))
28-
BUILD_SWIFT_PATH = os.path.abspath(os.path.join(TESTS_PATH, os.pardir))
29-
UTILS_PATH = os.path.abspath(os.path.join(BUILD_SWIFT_PATH, os.pardir))
30-
31-
BUILD_SCRIPT_IMPL = os.path.join(UTILS_PATH, 'build-script-impl')
32-
3327
PRESETS_FILES = [
3428
os.path.join(UTILS_PATH, 'build-presets.ini'),
3529
]
@@ -394,7 +388,7 @@ def _check_impl_args(self, namespace):
394388
assert hasattr(namespace, 'build_script_impl_args')
395389

396390
try:
397-
migration.check_impl_args(BUILD_SCRIPT_IMPL,
391+
migration.check_impl_args(BUILD_SCRIPT_IMPL_PATH,
398392
namespace.build_script_impl_args)
399393
except (SystemExit, ValueError) as e:
400394
raise ParserError('failed to parse impl arguments: ' +
@@ -406,12 +400,12 @@ def parse_args_and_unknown_args(self, args, namespace=None):
406400

407401
with self._quiet_output():
408402
try:
409-
namespace, unknown_args =\
410-
super(self.parser.__class__, self.parser)\
411-
.parse_known_args(args, namespace)
412-
namespace, unknown_args =\
413-
migration.process_disambiguation_arguments(namespace,
414-
unknown_args)
403+
namespace, unknown_args = (
404+
super(self.parser.__class__, self.parser).parse_known_args(
405+
args, namespace))
406+
namespace, unknown_args = (
407+
migration._process_disambiguation_arguments(
408+
namespace, unknown_args))
415409
except (SystemExit, argparse.ArgumentError) as e:
416410
raise ParserError('failed to parse arguments: ' + str(args), e)
417411

utils/build_swift/tests/test_migration.py

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99

1010
from __future__ import absolute_import, unicode_literals
1111

12-
from .utils import TestCase, add_metaclass
12+
import platform
13+
14+
from .utils import BUILD_SCRIPT_IMPL_PATH, TestCase, add_metaclass
15+
from ..build_swift import argparse
1316
from ..build_swift import migration
1417

1518

19+
# -----------------------------------------------------------------------------
20+
# Helpers
21+
1622
def _get_sdk_targets(sdk_names):
1723
targets = []
1824
for sdk_name in sdk_names:
@@ -26,6 +32,7 @@ def _get_sdk_target_names(sdk_names):
2632

2733

2834
# -----------------------------------------------------------------------------
35+
# Mirgrate Swift SDKs
2936

3037
class TestMigrateSwiftSDKsMeta(type):
3138
"""Metaclass used to dynamically generate test methods.
@@ -76,3 +83,88 @@ def test_multiple_swift_sdk_flags(self):
7683
'--stdlib-deployment-targets=',
7784
'--stdlib-deployment-targets={}'.format(' '.join(target_names))
7885
])
86+
87+
88+
# -----------------------------------------------------------------------------
89+
90+
class TestMigrateParseArgs(TestCase):
91+
92+
def test_report_unknown_args(self):
93+
parser = argparse.ArgumentParser()
94+
parser.add_argument('-R', '--release', action='store_true')
95+
parser.add_argument('-T', '--validation-test', action='store_true')
96+
parser.add_argument('--darwin-xcrun-toolchain')
97+
98+
args = migration.parse_args(parser, [
99+
'-RT',
100+
'--unknown', 'true',
101+
'--darwin-xcrun-toolchain=foo',
102+
'--',
103+
'--darwin-xcrun-toolchain=bar',
104+
'--other',
105+
])
106+
107+
expected = argparse.Namespace(
108+
release=True,
109+
validation_test=True,
110+
darwin_xcrun_toolchain='bar',
111+
build_script_impl_args=['--unknown', 'true', '--other'])
112+
113+
self.assertEqual(args, expected)
114+
115+
def test_no_unknown_args(self):
116+
parser = argparse.ArgumentParser()
117+
parser.add_argument('-R', '--release', action='store_true')
118+
parser.add_argument('-T', '--validation-test', action='store_true')
119+
parser.add_argument('--darwin-xcrun-toolchain')
120+
121+
args = migration.parse_args(
122+
parser, ['-RT', '--darwin-xcrun-toolchain=bar'])
123+
124+
expected = argparse.Namespace(
125+
release=True,
126+
validation_test=True,
127+
darwin_xcrun_toolchain='bar',
128+
build_script_impl_args=[])
129+
130+
self.assertEqual(args, expected)
131+
132+
def test_forward_impl_args(self):
133+
parser = argparse.ArgumentParser()
134+
parser.add_argument('--skip-test-swift',
135+
dest='impl_skip_test_swift',
136+
action='store_true')
137+
parser.add_argument('--install-swift',
138+
dest='impl_install_swift',
139+
action='store_true')
140+
141+
args = migration.parse_args(
142+
parser, ['--skip-test-swift', '--install-swift'])
143+
144+
expected = argparse.Namespace(
145+
build_script_impl_args=['--skip-test-swift', '--install-swift'])
146+
147+
self.assertEqual(args, expected)
148+
149+
150+
class TestMigrationCheckImplArgs(TestCase):
151+
152+
def test_check_impl_args(self):
153+
if platform.system() == 'Windows':
154+
self.skipTest("build-script-impl cannot run in Windows")
155+
return
156+
157+
self.assertIsNone(migration.check_impl_args(
158+
BUILD_SCRIPT_IMPL_PATH, ['--reconfigure']))
159+
160+
with self.assertRaises(ValueError) as cm:
161+
migration.check_impl_args(
162+
BUILD_SCRIPT_IMPL_PATH, ['foo'])
163+
164+
self.assertIn('foo', str(cm.exception))
165+
166+
with self.assertRaises(ValueError) as cm:
167+
migration.check_impl_args(
168+
BUILD_SCRIPT_IMPL_PATH, ['--reconfigure', '--foo=true'])
169+
170+
self.assertIn('foo', str(cm.exception))

utils/build_swift/tests/utils.py

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

99

10-
try:
11-
from StringIO import StringIO
12-
except ImportError:
13-
from io import StringIO
14-
1510
import os
1611
import sys
1712
import unittest
1813
from contextlib import contextmanager
1914

2015

16+
try:
17+
from StringIO import StringIO
18+
except ImportError:
19+
from io import StringIO
20+
21+
2122
__all__ = [
2223
'add_metaclass',
2324
'redirect_stderr',
2425
'redirect_stdout',
2526
'TestCase',
2627

28+
'BUILD_SCRIPT_IMPL_PATH',
29+
'BUILD_SWIFT_PATH',
30+
'TESTS_PATH',
2731
'UTILS_PATH',
2832
]
2933

3034

31-
UTILS_PATH = os.path.abspath(os.path.join(
32-
os.path.dirname(__file__),
33-
os.pardir,
34-
os.pardir,
35-
))
35+
# -----------------------------------------------------------------------------
36+
# Constants
37+
38+
TESTS_PATH = os.path.abspath(os.path.dirname(__file__))
39+
BUILD_SWIFT_PATH = os.path.abspath(os.path.join(TESTS_PATH, os.pardir))
40+
UTILS_PATH = os.path.abspath(os.path.join(BUILD_SWIFT_PATH, os.pardir))
41+
42+
BUILD_SCRIPT_IMPL_PATH = os.path.join(UTILS_PATH, 'build-script-impl')
3643

3744

3845
# -----------------------------------------------------------------------------

utils/swift_build_support/swift_build_support/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"debug",
2323
"diagnostics",
2424
"host_specific_configuration",
25-
"migration",
2625
"tar",
2726
"targets",
2827
"toolchain",

0 commit comments

Comments
 (0)