From 94fa520ea36fb0ff2661636e7e4b535818383db6 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Tue, 22 Oct 2024 12:35:24 +0200 Subject: [PATCH] refact winmake.py --- make.bat | 2 +- scripts/internal/winmake.py | 89 +++++++++++++++++++++++++------------ setup.py | 15 +++---- 3 files changed, 68 insertions(+), 38 deletions(-) diff --git a/make.bat b/make.bat index 01883f0e..25398890 100644 --- a/make.bat +++ b/make.bat @@ -10,4 +10,4 @@ if "%PYTHON%" == "" ( set PYTHON=python ) -%PYTHON% scripts\internal\winmake.py %1 %2 %3 %4 %5 %6 +"%PYTHON%" scripts\internal\winmake.py %1 %2 %3 %4 %5 %6 diff --git a/scripts/internal/winmake.py b/scripts/internal/winmake.py index ee439312..0d6febad 100755 --- a/scripts/internal/winmake.py +++ b/scripts/internal/winmake.py @@ -4,18 +4,17 @@ # Use of this source code is governed by MIT license that can be # found in the LICENSE file. - """Shortcuts for various tasks, emulating UNIX "make" on Windows. This is supposed to be invoked by "make.bat" and not used directly. This was originally written as a bat file but they suck so much that they should be deemed illegal! """ - import argparse import errno import fnmatch import os +import shlex import shutil import site import subprocess @@ -23,7 +22,7 @@ PYTHON = os.getenv('PYTHON', sys.executable) -PYTEST_ARGS = "-v -s --tb=short" +PYTEST_ARGS = ["-v", "-s", "--tb=short"] HERE = os.path.abspath(os.path.dirname(__file__)) ROOT_DIR = os.path.realpath(os.path.join(HERE, "..", "..")) WINDOWS = os.name == "nt" @@ -62,11 +61,10 @@ def safe_print(text, file=sys.stdout): def sh(cmd, nolog=False): + assert isinstance(cmd, list), repr(cmd) if not nolog: - safe_print("cmd: " + cmd) - p = subprocess.Popen( # noqa S602 - cmd, shell=True, env=os.environ, cwd=os.getcwd() - ) + safe_print("cmd: " + " ".join(cmd)) + p = subprocess.Popen(cmd, env=os.environ, cwd=os.getcwd()) # noqa S602 p.communicate() if p.returncode != 0: sys.exit(p.returncode) @@ -161,12 +159,12 @@ def recursive_rm(*patterns): def install_pip(): """Install pip.""" - sh('%s %s' % (PYTHON, os.path.join(HERE, "install_pip.py"))) + sh([PYTHON, os.path.join(HERE, "install_pip.py")]) def install(): """Install in develop / edit mode.""" - sh(f"{PYTHON} setup.py develop") + sh([PYTHON, "setup.py", "develop"]) def uninstall(): @@ -182,7 +180,7 @@ def uninstall(): except ImportError: break else: - sh(f"{PYTHON} -m pip uninstall -y pyftpdlib") + sh([PYTHON, "-m", "pip", "uninstall", "-y", "pyftpdlib"]) finally: os.chdir(here) @@ -240,58 +238,91 @@ def install_pydeps_test(): """Install useful deps.""" install_pip() install_git_hooks() - sh("%s -m pip install --user -U %s" % (PYTHON, " ".join(TEST_DEPS))) + cmd = [PYTHON, "-m", "pip", "install", "--user", "-U", *TEST_DEPS] + sh(cmd) def install_pydeps_dev(): """Install useful deps.""" install_pip() install_git_hooks() - sh("%s -m pip install --user -U %s" % (PYTHON, " ".join(DEV_DEPS))) + cmd = [PYTHON, "-m", "pip", "install", "--user", "-U", *DEV_DEPS] + sh(cmd) -def test(args=""): +def test(args=None): """Run tests.""" - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} {args}") + if args is None: + args = [] + elif isinstance(args, str): + args = shlex.split(args) + sh([PYTHON, '-m', 'pytest', *PYTEST_ARGS, *args]) def test_authorizers(): - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} pyftpdlib/test/test_authorizers.py") + sh([ + PYTHON, + "-m", + "pytest", + *PYTEST_ARGS, + "pyftpdlib/test/test_authorizers.py", + ]) def test_filesystems(): - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} pyftpdlib/test/test_filesystems.py") + sh([ + PYTHON, + "-m", + "pytest", + *PYTEST_ARGS, + "pyftpdlib/test/test_filesystems.py", + ]) def test_functional(): - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} pyftpdlib/test/test_functional.py") + sh([ + PYTHON, + "-m", + "pytest", + *PYTEST_ARGS, + "pyftpdlib/test/test_functional.py", + ]) def test_functional_ssl(): - sh( - f"{PYTHON} -m pytest" - f" {PYTEST_ARGS} pyftpdlib/test/test_functional_ssl.py" - ) + sh([ + PYTHON, + "-m", + "pytest", + *PYTEST_ARGS, + "pyftpdlib/test/test_functional_ssl.py", + ]) def test_ioloop(): - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} pyftpdlib/test/test_ioloop.py") + sh([PYTHON, "-m", "pytest", *PYTEST_ARGS, "pyftpdlib/test/test_ioloop.py"]) def test_cli(): - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} pyftpdlib/test/test_cli.py") + sh([PYTHON, "-m", "pytest", *PYTEST_ARGS, "pyftpdlib/test/test_cli.py"]) def test_servers(): - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} pyftpdlib/test/test_servers.py") + sh([ + PYTHON, + "-m", + "pytest", + *PYTEST_ARGS, + "pyftpdlib/test/test_servers.py", + ]) def coverage(): """Run coverage tests.""" - sh(f"{PYTHON} -m coverage run -m pytest {PYTEST_ARGS}") - sh(f"{PYTHON} -m coverage report") - sh(f"{PYTHON} -m coverage html") - sh(f"{PYTHON} -m webbrowser -t htmlcov/index.html") + sh([PYTHON, "-m", "coverage", "run", "-m", "pytest", *PYTEST_ARGS]) + sh([PYTHON, "-m", "coverage", "report"]) + sh([PYTHON, "-m", "coverage", "html"]) + sh([PYTHON, "-m", "webbrowser", "-t", "htmlcov/index.html"]) def test_by_name(name): @@ -301,7 +332,7 @@ def test_by_name(name): def test_last_failed(): """Re-run tests which failed on last run.""" - sh(f"{PYTHON} -m pytest {PYTEST_ARGS} --last-failed") + sh([PYTHON, "-m", "pytest", *PYTEST_ARGS, "--last-failed"]) def install_git_hooks(): diff --git a/setup.py b/setup.py index a14f7612..67588592 100644 --- a/setup.py +++ b/setup.py @@ -13,14 +13,6 @@ import textwrap -try: - import setuptools - from setuptools import setup -except ImportError: - setuptools = None - from distutils.core import setup - - WINDOWS = os.name == "nt" # Test deps, installable via `pip install .[test]`. @@ -105,6 +97,13 @@ def hilite(s, ok=True, bold=False): def main(): + try: + import setuptools # noqa + from setuptools import setup # noqa + except ImportError: + setuptools = None + from distutils.core import setup # noqa + kwargs = dict( name='pyftpdlib', version=get_version(),