Add a proper deprecation warning for update-index -d (#34520)
This commit is contained in:
		| @@ -712,9 +712,23 @@ def update_index(mirror_url, update_keys=False): | |||||||
|         bindist.generate_key_index(keys_url) |         bindist.generate_key_index(keys_url) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def _mirror_url_from_args_deprecated_format(args): | ||||||
|  |     # In Spack 0.19 the -d flag was equivalent to --mirror-url. | ||||||
|  |     # Spack 0.20 deprecates this, so in 0.21 -d means --directory. | ||||||
|  |     if args.directory and url_util.validate_scheme(urllib.parse.urlparse(args.directory).scheme): | ||||||
|  |         tty.warn( | ||||||
|  |             "Passing a URL to `update-index -d <url>` is deprecated " | ||||||
|  |             "and will be removed in Spack 0.21. " | ||||||
|  |             "Use `update-index --mirror-url <url>` instead." | ||||||
|  |         ) | ||||||
|  |         return spack.mirror.push_url_from_mirror_url(args.directory) | ||||||
|  |     else: | ||||||
|  |         return _mirror_url_from_args(args) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def update_index_fn(args): | def update_index_fn(args): | ||||||
|     """Update a buildcache index.""" |     """Update a buildcache index.""" | ||||||
|     push_url = _mirror_url_from_args(args) |     push_url = _mirror_url_from_args_deprecated_format(args) | ||||||
|     update_index(push_url, update_keys=args.keys) |     update_index(push_url, update_keys=args.keys) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|   | |||||||
| @@ -667,8 +667,7 @@ def push_url_from_directory(output_directory): | |||||||
|     """Given a directory in the local filesystem, return the URL on |     """Given a directory in the local filesystem, return the URL on | ||||||
|     which to push binary packages. |     which to push binary packages. | ||||||
|     """ |     """ | ||||||
|     scheme = urllib.parse.urlparse(output_directory, scheme="<missing>").scheme |     if url_util.validate_scheme(urllib.parse.urlparse(output_directory).scheme): | ||||||
|     if scheme != "<missing>": |  | ||||||
|         raise ValueError("expected a local path, but got a URL instead") |         raise ValueError("expected a local path, but got a URL instead") | ||||||
|     mirror_url = url_util.path_to_file_url(output_directory) |     mirror_url = url_util.path_to_file_url(output_directory) | ||||||
|     mirror = spack.mirror.MirrorCollection().lookup(mirror_url) |     mirror = spack.mirror.MirrorCollection().lookup(mirror_url) | ||||||
| @@ -685,8 +684,7 @@ def push_url_from_mirror_name(mirror_name): | |||||||
| 
 | 
 | ||||||
| def push_url_from_mirror_url(mirror_url): | def push_url_from_mirror_url(mirror_url): | ||||||
|     """Given a mirror URL, return the URL on which to push binary packages.""" |     """Given a mirror URL, return the URL on which to push binary packages.""" | ||||||
|     scheme = urllib.parse.urlparse(mirror_url, scheme="<missing>").scheme |     if not url_util.validate_scheme(urllib.parse.urlparse(mirror_url).scheme): | ||||||
|     if scheme == "<missing>": |  | ||||||
|         raise ValueError('"{0}" is not a valid URL'.format(mirror_url)) |         raise ValueError('"{0}" is not a valid URL'.format(mirror_url)) | ||||||
|     mirror = spack.mirror.MirrorCollection().lookup(mirror_url) |     mirror = spack.mirror.MirrorCollection().lookup(mirror_url) | ||||||
|     return url_util.format(mirror.push_url) |     return url_util.format(mirror.push_url) | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ | |||||||
| # | # | ||||||
| # SPDX-License-Identifier: (Apache-2.0 OR MIT) | # SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||||||
| 
 | 
 | ||||||
|  | import argparse | ||||||
| import errno | import errno | ||||||
| import os | import os | ||||||
| import platform | import platform | ||||||
| @@ -12,9 +13,11 @@ | |||||||
| import pytest | import pytest | ||||||
| 
 | 
 | ||||||
| import spack.binary_distribution | import spack.binary_distribution | ||||||
|  | import spack.cmd.buildcache | ||||||
| import spack.environment as ev | import spack.environment as ev | ||||||
| import spack.main | import spack.main | ||||||
| import spack.spec | import spack.spec | ||||||
|  | import spack.util.url | ||||||
| from spack.spec import Spec | from spack.spec import Spec | ||||||
| 
 | 
 | ||||||
| buildcache = spack.main.SpackCommand("buildcache") | buildcache = spack.main.SpackCommand("buildcache") | ||||||
| @@ -265,3 +268,13 @@ def test_buildcache_create_install( | |||||||
|     tarball = spack.binary_distribution.tarball_name(spec, ".spec.json") |     tarball = spack.binary_distribution.tarball_name(spec, ".spec.json") | ||||||
|     assert os.path.exists(os.path.join(str(tmpdir), "build_cache", tarball_path)) |     assert os.path.exists(os.path.join(str(tmpdir), "build_cache", tarball_path)) | ||||||
|     assert os.path.exists(os.path.join(str(tmpdir), "build_cache", tarball)) |     assert os.path.exists(os.path.join(str(tmpdir), "build_cache", tarball)) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_deprecation_mirror_url_dir_flag(capfd): | ||||||
|  |     # Test that passing `update-index -d <url>` gives a deprecation warning. | ||||||
|  |     parser = argparse.ArgumentParser() | ||||||
|  |     spack.cmd.buildcache.setup_parser(parser) | ||||||
|  |     url = spack.util.url.path_to_file_url(os.getcwd()) | ||||||
|  |     args = parser.parse_args(["update-index", "-d", url]) | ||||||
|  |     spack.cmd.buildcache._mirror_url_from_args_deprecated_format(args) | ||||||
|  |     assert "Passing a URL to `update-index -d <url>` is deprecated" in capfd.readouterr()[1] | ||||||
|   | |||||||
| @@ -18,6 +18,14 @@ | |||||||
| from spack.util.path import convert_to_posix_path | from spack.util.path import convert_to_posix_path | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def validate_scheme(scheme): | ||||||
|  |     """Returns true if the URL scheme is generally known to Spack. This function | ||||||
|  |     helps mostly in validation of paths vs urls, as Windows paths such as | ||||||
|  |     C:/x/y/z (with backward not forward slash) may parse as a URL with scheme | ||||||
|  |     C and path /x/y/z.""" | ||||||
|  |     return scheme in ("file", "http", "https", "ftp", "s3", "gs", "ssh", "git") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def _split_all(path): | def _split_all(path): | ||||||
|     """Split path into its atomic components. |     """Split path into its atomic components. | ||||||
| 
 | 
 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Harmen Stoppels
					Harmen Stoppels