From 09378f56c090786177e05f376e1119faa1596f15 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Tue, 17 Aug 2021 17:52:51 +0200 Subject: Use a patched argparse only in Python 2.X (#25376) Spack is internally using a patched version of `argparse` mainly to backport Python 3 functionality into Python 2. This PR makes it such that for the supported Python 3 versions we use `argparse` from the standard Python library. This PR has been extracted from #25371 where it was needed to be able to use recent versions of `pytest`. * Fixed formatting issues when using a pristine argparse.py * Fix error message for Python 3.X when missing positional arguments * Account for the change of API in Python 3.7 * Layout multi-valued args into columns in error messages * Seamless transition in develop if argparse.pyc is in external * Be more defensive in case we can't remove the file. --- bin/spack | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'bin') diff --git a/bin/spack b/bin/spack index a4c442227d..dffcbd2026 100755 --- a/bin/spack +++ b/bin/spack @@ -28,6 +28,7 @@ exit 1 from __future__ import print_function import os +import os.path import sys min_python3 = (3, 5) @@ -70,6 +71,28 @@ if "ruamel.yaml" in sys.modules: if "ruamel" in sys.modules: del sys.modules["ruamel"] +# The following code is here to avoid failures when updating +# the develop version, due to spurious argparse.pyc files remaining +# in the libs/spack/external directory, see: +# https://github.com/spack/spack/pull/25376 +# TODO: Remove in v0.18.0 or later +try: + import argparse +except ImportError: + argparse_pyc = os.path.join(spack_external_libs, 'argparse.pyc') + if not os.path.exists(argparse_pyc): + raise + try: + os.remove(argparse_pyc) + import argparse # noqa + except Exception: + msg = ('The file\n\n\t{0}\n\nis corrupted and cannot be deleted by Spack. ' + 'Either delete it manually or ask some administrator to ' + 'delete it for you.') + print(msg.format(argparse_pyc)) + sys.exit(1) + + import spack.main # noqa # Once we've set up the system path, run the spack main method -- cgit v1.2.3-60-g2f50