Mercurial > public > mercurial-scm > hg-stable
diff tests/run-tests.py @ 52689:f5091286b10c
packaging: modernize (compat PEP 517) with less distutils and setup.py calls
- setup.py: less distutils imports and setuptools required
distutils is deprecated and one should import commands from setuptools to support
modern workflows depending on PEP 517 and 518.
Moreover, for Python >=3.12, distutils comes from setuptools. It corresponds to old and
unmaintain code that do not support PEP 517.
The PEP 517 frontends (pip, build, pipx, PDM, UV, etc.) are responsible for creating a
venv just for the build. The build dependencies (currently only setuptools) are specified
in the pyproject.toml file. Therefore, there is no reason to support building without
setuptools.
Calling directly setup.py is deprecated and we have to use a PEP 517 frontend.
For this commit we use pip with venv.
- run-tests.py: install with pip instead of direct call of setup.py
Mercurial is then built in an isolated environment.
- Makefile: use venv+pip instead of setup.py
author | paugier <pierre.augier@univ-grenoble-alpes.fr> |
---|---|
date | Wed, 08 Jan 2025 05:07:00 +0100 |
parents | 70a75d379daf |
children | ba8aa20ff31f |
line wrap: on
line diff
--- a/tests/run-tests.py Wed Dec 04 23:09:32 2024 +0100 +++ b/tests/run-tests.py Wed Jan 08 05:07:00 2025 +0100 @@ -87,6 +87,7 @@ MACOS = sys.platform == 'darwin' WINDOWS = os.name == r'nt' shellquote = shlex.quote +BINDIRNAME = b"Scripts" if WINDOWS else b"bin" # The number of HGPORTx ports allocated to each test. HGPORT_COUNT = 4 @@ -752,7 +753,17 @@ parser.error('--pyoxidized does not work with --local (yet)') testdir = os.path.dirname(_sys2bytes(canonpath(sys.argv[0]))) reporootdir = os.path.dirname(testdir) - pathandattrs = [(b'hg', 'with_hg')] + venv_local = b'.venv_%s%d.%d' % ( + sys.implementation.name.encode(), + sys.version_info.major, + sys.version_info.minor, + ) + path_local_hg = os.path.join(reporootdir, venv_local, BINDIRNAME, b"hg") + if not os.path.exists(path_local_hg): + # no local environment but we can still use ./hg to please test-run-tests.t + path_local_hg = os.path.join(reporootdir, b"hg") + + pathandattrs = [(path_local_hg, 'with_hg')] if options.chg: pathandattrs.append((b'contrib/chg/chg', 'with_chg')) if options.rhg: @@ -2805,6 +2816,18 @@ pass +def get_site_packages_dir(python_exe): + return subprocess.run( + [ + python_exe, + "-c", + "import sys; print([p for p in sys.path if p.startswith(sys.prefix) and p.endswith('site-packages')][0])", + ], + check=True, + capture_output=True, + ).stdout.strip() + + class TextTestRunner(unittest.TextTestRunner): """Custom unittest test runner that uses appropriate settings.""" @@ -3260,10 +3283,18 @@ # the Mercurial modules are relative to its path and tell the tests # to load Python modules from its directory. with open(whg, 'rb') as fh: - initial = fh.read(1024) - - if re.match(b'#!.*python', initial): - self._pythondir = self._bindir + first_line = fh.readline() + + if re.match(b'#!.*python', first_line): + python_exe = first_line.split(b"#!")[1].strip() + try: + self._pythondir = get_site_packages_dir(python_exe) + except (FileNotFoundError, subprocess.CalledProcessError): + self._pythondir = self._bindir + elif self.options.local: + assert WINDOWS + python_exe = os.path.join(self._bindir, b"python.exe") + self._pythondir = get_site_packages_dir(python_exe) # If it looks like our in-repo Rust binary, use the source root. # This is a bit hacky. But rhg is still not supported outside the # source directory. So until it is, do the simple thing. @@ -3291,16 +3322,7 @@ bindir = b"Scripts" if WINDOWS else b"bin" self._bindir = os.path.join(self._installdir, bindir) self._python = _bytes2sys(os.path.join(self._bindir, b"python")) - - self._pythondir = subprocess.run( - [ - self._python, - "-c", - "import sys; print([p for p in sys.path if p.startswith(sys.prefix) and p.endswith('site-packages')][0])", - ], - check=True, - capture_output=True, - ).stdout.strip() + self._pythondir = get_site_packages_dir(self._python) # Force the use of hg.exe instead of relying on MSYS to recognize hg is # a python script and feed it to python.exe. Legacy stdio is force @@ -3854,27 +3876,11 @@ hgroot = os.path.dirname(os.path.dirname(script)) self._hgroot = hgroot os.chdir(hgroot) - cmd = [ - self._pythonb, - b"setup.py", - ] + cmd = [self._pythonb, b"-m", b"pip", b"install", b"."] if setup_opts: - cmd.append(setup_opts) - cmd.extend( - [ - b"clean", - b"--all", - b"build", - ] - ) - cmd.extend( - [ - b"--build-base=%s" % os.path.join(self._hgtmp, b"build"), - b"install", - b"--force", - ] - ) - + cmd.extend( + [b"--config-settings", b"--global-option=%s" % setup_opts] + ) return cmd def _installhg(self):