Skip to content

Commit

Permalink
feat: Python 3.7+
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed May 28, 2024
1 parent f3b4a78 commit a52436c
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 71 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
fail-fast: false
matrix:
include:
- os: ubuntu-20.04
- os: ubuntu-22.04
arch: "x86_64"
- os: ubuntu-22.04
arch: "i686"
Expand Down Expand Up @@ -78,16 +78,6 @@ jobs:
name: cibw-wheels-${{ matrix.os }}-${{ matrix.arch }}
path: ./wheelhouse/*.whl

- name: Install Ubuntu Python 2.7
if: matrix.os == 'ubuntu-20.04'
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends python2 python3-virtualenv
virtualenv -p python2 ${HOME}/cp27
${HOME}/cp27/bin/python -m pip install -U pip
${HOME}/cp27/bin/python -m pip install -U setuptools wheel
echo "${HOME}/cp27/bin" >> $GITHUB_PATH
- name: Test wheel on host Linux
if: runner.os == 'Linux' && matrix.arch == 'x86_64'
run: |
Expand Down
21 changes: 20 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@ repos:
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: trailing-whitespace
- id: fix-encoding-pragma

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.3.4"
hooks:
- id: ruff
args: [--fix, --show-fixes]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.10.0"
hooks:
- id: mypy
files: src
args: []

- repo: https://github.com/abravalheri/validate-pyproject
rev: "v0.16"
hooks:
- id: validate-pyproject
additional_dependencies: ["validate-pyproject-schema-store[all]"]

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: "0.28.2"
hooks:
- id: check-dependabot
- id: check-github-workflows
- id: check-readthedocs
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Before you submit a pull request, check that it meets these guidelines:
your new functionality into a function with a docstring, and add the
feature to the list in `README.rst`.

3. The pull request should work for Python 2.7, and 3.6+.
3. The pull request should work for Python 3.7+.
Check `GitHub Actions`_
and make sure that the tests pass for all supported Python versions.

Expand Down
12 changes: 8 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

import argparse
import sys
from pathlib import Path

import nox

nox.needs_version = ">=2024.4.15"
nox.options.default_venv_backend = "uv|virtualenv"
nox.options.sessions = ["lint", "build", "tests"]

if sys.platform.startswith("darwin"):
Expand All @@ -25,7 +28,7 @@ def build(session: nox.Session) -> str:
"""
Make an SDist and a wheel. Only runs once.
"""
global built
global built # noqa: PLW0603
if not built:
session.log(
"The files produced locally by this job are not intended to be redistributable"
Expand All @@ -48,7 +51,7 @@ def lint(session: nox.Session) -> str:
Run linters on the codebase.
"""
session.install("pre-commit")
session.run("pre-commit", "run", "-a")
session.run("pre-commit", "run", "-a", *session.posargs)


@nox.session
Expand Down Expand Up @@ -92,7 +95,8 @@ def bump(session: nox.Session) -> None:
else:
version = args.version

session.install("requests")
deps = nox.project.load_toml("scripts/update_ninja_version.py")["dependencies"]
session.install(*deps)

extra = ["--quiet"] if args.commit else []
session.run("python", "scripts/update_ninja_version.py", "--upstream-repository", args.upstream_repository, version, *extra)
Expand Down
44 changes: 31 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ classifiers = [
"Topic :: Software Development :: Build Tools",
"Typing :: Typed",
]
requires-python = ">=3.7"

[project.optional-dependencies]
test = [
"coverage>=4.2",
"importlib_metadata>=2.0",
"pytest-cov>=2.7.1",
"pytest>=4.5.0",
"pytest-cov>=3",
"pytest>=6.0",
]

[project.urls]
Expand All @@ -51,7 +52,7 @@ Homepage = "http://ninja-build.org/"
[tool.scikit-build]
minimum-version = "0.9"
build-dir = "build/{wheel_tag}"
wheel.py-api = "py2.py3"
wheel.py-api = "py3"
wheel.expand-macos-universal-tags = true
metadata.version.provider = "scikit_build_core.metadata.setuptools_scm"
metadata.readme.provider = "scikit_build_core.metadata.fancy_pypi_readme"
Expand Down Expand Up @@ -101,41 +102,58 @@ MACOSX_DEPLOYMENT_TARGET = "10.9"
select = "*-musllinux_*"
environment = { LDFLAGS = "-static-libstdc++ -static-libgcc" }


[tool.pytest.ini_options]
minversion = "6.0"
addopts = ["-ra", "--strict-markers", "--strict-config"]
xfail_strict = true
filterwarnings = ["error"]
log_cli_level = "info"
testpaths = [ "tests" ]


[tool.mypy]
warn_unused_configs = true
files = "src"
python_version = "3.8"
strict = true
enable_error_code = ["ignore-without-code", "truthy-bool", "redundant-expr"]
warn_unreachable = true


[tool.ruff]
src = ["src"]

[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"I", # isort
"ARG", # flake8-unused-arguments
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"EXE", # flake8-executable
"G", # flake8-logging-format
"I", # isort
"ICN", # flake8-import-conventions
"ISC", # flake8-implicit-str-concat
"G", # flake8-logging-format
"NPY", # NumPy specific rules
"PD", # pandas-vet
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PT", # flake8-pytest-style
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"EXE", # flake8-executable
"NPY", # NumPy specific rules
"PD", # pandas-vet
"UP", # pyupgrade
]
ignore = [
"PLR", # Design related pylint codes
"E501", # Line too long
"RUF005", # Python 3 needed
"B904", # Python 3 needed
# "SIM105", # Python 3 needed
]
isort.required-imports = ["from __future__ import annotations"]
flake8-unused-arguments.ignore-variadic-names = true

[tool.ruff.lint.per-file-ignores]
"*.pyi" = ["ARG001"]
"noxfile.py" = ["PLW0603"] # Could be fixed if Python 2 dropped

[tool.pytest.ini_config]
testpaths = ["tests"]
Expand Down
3 changes: 2 additions & 1 deletion scripts/repair_wheel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

import argparse
import re
import shutil
Expand Down
36 changes: 17 additions & 19 deletions scripts/update_ninja_version.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# -*- coding: utf-8 -*-
# /// script
# dependencies = ["requests"]
# ///

"""
Command line executable allowing to update NinjaUrls.cmake, documentation
and tests given a Ninja version.
"""
from __future__ import annotations

import argparse
import contextlib
Expand All @@ -12,13 +16,7 @@
import tempfile
import textwrap

try:
from requests import request
except ImportError:
raise SystemExit(
"requests not available: "
"consider installing it running 'pip install requests'"
)
from requests import request

ROOT_DIR = os.path.join(os.path.dirname(__file__), "..")

Expand All @@ -31,7 +29,7 @@ def _log(txt, verbose=True):
print(txt)
yield
if verbose:
print("%s - done" % txt)
print(f"{txt} - done")


def _download_file(download_url, filename):
Expand Down Expand Up @@ -67,7 +65,7 @@ def _hash_sum(filepath, algorithm="sha256", block_size=2 ** 20):

def _download_and_compute_sha256(url, filename):
filepath = os.path.join(tempfile.gettempdir(), filename)
with _log("Downloading %s" % url):
with _log(f"Downloading {url}"):
_download_file(url, filepath)
sha256 = _hash_sum(filepath, algorithm="sha256")
return url, sha256
Expand All @@ -77,7 +75,7 @@ def get_ninja_archive_urls_and_sha256s(upstream_repository, version, verbose=Fal
tag_name = f"v{version}"
files_base_url = f"https://github.com/{upstream_repository}/archive/{tag_name}"

with _log("Collecting URLs and SHA256s from '%s'" % files_base_url):
with _log(f"Collecting URLs and SHA256s from '{files_base_url}'"):

# Get SHA256s and URLs
urls = {
Expand All @@ -87,7 +85,7 @@ def get_ninja_archive_urls_and_sha256s(upstream_repository, version, verbose=Fal

if verbose:
for identifier, (url, sha256) in urls.items():
print("[{}]\n{}\n{}\n".format(identifier, url, sha256))
print(f"[{identifier}]\n{url}\n{sha256}\n")

return urls

Expand All @@ -97,8 +95,8 @@ def generate_cmake_variables(urls_and_sha256s):

# Get SHA256s and URLs
for var_prefix, urls_and_sha256s_values in urls_and_sha256s.items():
template_inputs["%s_url" % var_prefix] = urls_and_sha256s_values[0]
template_inputs["%s_sha256" % var_prefix] = urls_and_sha256s_values[1]
template_inputs[f"{var_prefix}_url"] = urls_and_sha256s_values[0]
template_inputs[f"{var_prefix}_sha256"] = urls_and_sha256s_values[1]

return textwrap.dedent(
"""
Expand All @@ -118,16 +116,16 @@ def update_cmake_urls_script(upstream_repository, version):
cmake_urls_filename = "NinjaUrls.cmake"
cmake_urls_filepath = os.path.join(ROOT_DIR, cmake_urls_filename)

msg = "Updating '{}' with Ninja version {}".format(cmake_urls_filename, version)
msg = f"Updating '{cmake_urls_filename}' with Ninja version {version}"
with _log(msg), open(cmake_urls_filepath, "w") as cmake_file:
cmake_file.write(content)


def _update_file(filepath, regex, replacement, verbose=True):
msg = "Updating %s" % os.path.relpath(filepath, ROOT_DIR)
msg = f"Updating {os.path.relpath(filepath, ROOT_DIR)}"
with _log(msg, verbose=verbose):
pattern = re.compile(regex)
with open(filepath, "r") as doc_file:
with open(filepath) as doc_file:
lines = doc_file.readlines()
updated_content = []
for line in lines:
Expand All @@ -138,7 +136,7 @@ def _update_file(filepath, regex, replacement, verbose=True):

def update_docs(upstream_repository, version):
pattern = re.compile(r"ninja \d+.\d+.\d+(\.[\w\-]+)*")
replacement = "ninja %s" % version
replacement = f"ninja {version}"
_update_file(
os.path.join(ROOT_DIR, "README.rst"),
pattern, replacement)
Expand Down Expand Up @@ -171,7 +169,7 @@ def update_tests(version):
version = ".".join(parts)

pattern = re.compile(r'expected_version = "\d+.\d+.\d+(\.[\w\-]+)*"')
replacement = 'expected_version = "%s"' % version
replacement = f'expected_version = "{version}"'
_update_file(os.path.join(
ROOT_DIR, "tests/test_ninja.py"), pattern, replacement)

Expand Down
18 changes: 11 additions & 7 deletions src/ninja/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

import os
import subprocess
import sys
import sysconfig
from collections.abc import Iterable
from typing import NoReturn

from ._version import version as __version__
from .ninja_syntax import Writer, escape, expand

__all__ = ["__version__", "DATA", "BIN_DIR", "ninja", "Writer", "escape", "expand"]


def __dir__():
def __dir__() -> list[str]:
return __all__


def _get_ninja_dir():
def _get_ninja_dir() -> str:
ninja_exe = "ninja" + sysconfig.get_config_var("EXE")

# Default path
Expand All @@ -27,7 +30,7 @@ def _get_ninja_dir():
user_scheme = sysconfig.get_preferred_scheme("user")
elif os.name == "nt":
user_scheme = "nt_user"
elif sys.platform.startswith("darwin") and sys._framework:
elif sys.platform.startswith("darwin") and getattr(sys, "_framework", None):
user_scheme = "osx_framework_user"
else:
user_scheme = "posix_user"
Expand All @@ -47,10 +50,11 @@ def _get_ninja_dir():

BIN_DIR = _get_ninja_dir()

def _program(name, args):

def _program(name: str, args: Iterable[str]) -> int:
cmd = os.path.join(BIN_DIR, name)
return subprocess.call([cmd] + args, close_fds=False)
return subprocess.call([cmd, *args], close_fds=False)


def ninja():
def ninja() -> NoReturn:
raise SystemExit(_program('ninja', sys.argv[1:]))
10 changes: 0 additions & 10 deletions src/ninja/__init__.pyi

This file was deleted.

3 changes: 2 additions & 1 deletion src/ninja/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

from ninja import ninja

if __name__ == '__main__':
Expand Down
Loading

0 comments on commit a52436c

Please sign in to comment.