Skip to content

Commit

Permalink
ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-cbarber committed Jan 7, 2024
1 parent eebae99 commit f9704f2
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 121 deletions.
4 changes: 1 addition & 3 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def pytest_configure(config):
config.addinivalue_line(
"markers", "external: mark test as depending on extenral pypi package to run"
)
config.addinivalue_line(
"markers", "slow: mark test as slow to run"
)
config.addinivalue_line("markers", "slow: mark test as slow to run")


def pytest_collection_modifyitems(config, items):
Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ disable = [
"useless-suppression",
"wrong-import-order"
]

[tool.ruff]
line-length = 88

[tool.ruff.format]
line-ending = "lf"
preview = true
quote-style = "preserve"
1 change: 1 addition & 0 deletions src/whl2conda/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
Main module allows invocation using `python -m whl2conda`
"""

from .cli.main import main

if __name__ == "__main__": # pragma: no cover
Expand Down
8 changes: 6 additions & 2 deletions src/whl2conda/api/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ def translate_version_spec(self, pip_version: str) -> str:
if m := pip_version_re.match(spec):
operator = m.group("operator")
v = m.group("version")
if v.startswith("v"): # e.g. convert v1.2 to 1.2
if v.startswith("v"): # e.g. convert v1.2 to 1.2
v = v[1:]
if operator == "~=":
# compatible operator, e.g. convert ~=1.2.3 to >=1.2.3,==1.2.*
Expand All @@ -817,7 +817,11 @@ def translate_version_spec(self, pip_version: str) -> str:
elif operator == "===":
operator = "=="
# TODO perhaps treat as an error in "strict" mode
self._warn("Converted arbitrary equality clause %s to ==%s - may not match!", spec, v)
self._warn(
"Converted arbitrary equality clause %s to ==%s - may not match!",
spec,
v,
)
version_specs[i] = f"{operator}{v}"
else:
self._warn("Cannot convert bad version spec: '%s'", spec)
Expand Down
20 changes: 9 additions & 11 deletions src/whl2conda/cli/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,15 @@ def _test_package(self, pkg: Path) -> None:

if import_names := test_section.get("imports", []):
for import_name in import_names:
subprocess.check_call(
[
"conda",
"run",
"-p",
str(test_prefix),
"python",
"-c",
f"import {import_name}",
]
)
subprocess.check_call([
"conda",
"run",
"-p",
str(test_prefix),
"python",
"-c",
f"import {import_name}",
])

if commands := test_section.get("commands", []):
for command in commands:
Expand Down
1 change: 1 addition & 0 deletions src/whl2conda/cli/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
CLI utility functions
"""

from __future__ import annotations

import argparse
Expand Down
39 changes: 25 additions & 14 deletions src/whl2conda/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def install_main(
deps_options.add_argument(
"--no-deps",
action="store_true",
help="Only packages themselves without any dependencies."
help="Only packages themselves without any dependencies.",
)

env_options.add_argument(
Expand Down Expand Up @@ -188,7 +188,9 @@ def install_main(

subdir = "noarch"
dependencies: list[str] = []
file_specs: list[tuple[str,str]] = [] # name/version pairs of package files being installed
file_specs: list[
tuple[str, str]
] = [] # name/version pairs of package files being installed

for conda_file in conda_files:
conda_fname = str(conda_file.name)
Expand All @@ -201,7 +203,9 @@ def install_main(
# provides an extra validity check on the file.
tmp_path = Path(tmpdir)
extract_conda_pkg(str(conda_file), dest_dir=tmp_path)
index = json.loads(tmp_path.joinpath("info", "index.json").read_text("utf"))
index = json.loads(
tmp_path.joinpath("info", "index.json").read_text("utf")
)
subdir = index["subdir"]
package_name = index["name"]
package_version = index.get("version", "")
Expand All @@ -227,13 +231,15 @@ def conda_bld_install(parsed: InstallArgs, subdir: str):
print(f"Installing {package_file} into {subdir_path}")
if not parsed.dry_run:
subdir_path.mkdir(parents=True, exist_ok=True)
shutil.copyfile(
package_file, subdir_path.joinpath(package_file.name)
)
shutil.copyfile(package_file, subdir_path.joinpath(package_file.name))
if not parsed.dry_run:
subprocess.check_call(
["conda", "index", "--subdir", subdir, str(conda_bld_path)]
)
subprocess.check_call([
"conda",
"index",
"--subdir",
subdir,
str(conda_bld_path),
])


def conda_env_install(parsed: InstallArgs, dependencies: list[str]):
Expand Down Expand Up @@ -280,13 +286,20 @@ def conda_env_install(parsed: InstallArgs, dependencies: list[str]):

# Workaround for https://github.com/conda/conda/issues/13479
# If a package is installed directly from file, then set solver to classic
set_solver_cmd = ["conda", "run"] + env_opts + ["conda", "config", "--env", "--set", "solver", "classic"]
set_solver_cmd = (
["conda", "run"]
+ env_opts
+ ["conda", "config", "--env", "--set", "solver", "classic"]
)
if parsed.dry_run:
print("Running ", set_solver_cmd)
else:
subprocess.check_call(set_solver_cmd)

def _prune_dependencies(dependencies: list[str], file_specs: list[tuple[str,str]]) -> list[str]:

def _prune_dependencies(
dependencies: list[str], file_specs: list[tuple[str, str]]
) -> list[str]:
"""
Prunes dependencies list according to arguments
Expand All @@ -302,7 +315,7 @@ def _prune_dependencies(dependencies: list[str], file_specs: list[tuple[str,str]
List of pruned dependencies.
"""

exclude_packages: dict[str,str] = dict(file_specs)
exclude_packages: dict[str, str] = dict(file_specs)
deps: set[str] = set()

for dep in dependencies:
Expand Down Expand Up @@ -330,5 +343,3 @@ def _prune_dependencies(dependencies: list[str], file_specs: list[tuple[str,str]
deps.add(dep)

return sorted(deps)


1 change: 1 addition & 0 deletions src/whl2conda/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
Main whl2conda CLI
"""

from __future__ import annotations

import argparse
Expand Down
1 change: 1 addition & 0 deletions src/whl2conda/impl/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
Interactive prompt utilities.
"""

from __future__ import annotations

import io
Expand Down
26 changes: 12 additions & 14 deletions test-projects/setup/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,27 @@
from setuptools import setup

setup(
name = "mypkg",
version = "1.3.4",
description = "Test package using setup.py",
author = "John Doe",
name="mypkg",
version="1.3.4",
description="Test package using setup.py",
author="John Doe",
author_email="jdoe@nowhere.com",
classifiers = [
classifiers=[
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
keywords=["python","test"],
maintainer = "Zuzu",
maintainer_email= "zuzu@nowhere.com",
keywords=["python", "test"],
maintainer="Zuzu",
maintainer_email="zuzu@nowhere.com",
license_files=[
"LICENSE.md",
os.path.abspath("LICENSE2.rst"),
],
install_requires = [
install_requires=[
"tables",
"wheel",
],
extras_require = {
'bdev': [ 'black' ]
},
packages=["mypkg"]
)
extras_require={'bdev': ['black']},
packages=["mypkg"],
)
12 changes: 9 additions & 3 deletions test/api/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"""
Test fixtures for the converter module
"""

from __future__ import annotations

import shutil
Expand Down Expand Up @@ -144,9 +145,14 @@ def _get_wheel(self) -> Path:
with tempfile.TemporaryDirectory(dir=self.pip_downloads) as tmpdir:
download_dir = Path(tmpdir)
try:
subprocess.check_call(
["pip", "download", spec, "--no-deps", "-d", str(download_dir)]
)
subprocess.check_call([
"pip",
"download",
spec,
"--no-deps",
"-d",
str(download_dir),
])
except subprocess.CalledProcessError as ex:
pytest.skip(f"Cannot download {spec} from pypi: {ex}")
downloaded_wheel = next(download_dir.glob("*.whl"))
Expand Down
51 changes: 26 additions & 25 deletions test/api/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
CondaPackageFormat,
DependencyRename,
RequiresDistEntry,
Wheel2CondaError, Wheel2CondaConverter,
Wheel2CondaError,
Wheel2CondaConverter,
)
from whl2conda.cli.convert import do_build_wheel
from .converter import ConverterTestCaseFactory
Expand Down Expand Up @@ -189,6 +190,7 @@ def test_dependency_rename() -> None:
# ignore redefinition of test_case
# ruff: noqa: F811


def test_this(test_case: ConverterTestCaseFactory) -> None:
"""Test using this own project's wheel"""
wheel_dir = test_case.tmp_path_factory.mktemp("test_this_wjheel_dir")
Expand Down Expand Up @@ -264,22 +266,24 @@ def test_simple_wheel(

# Repack wheel with build number
dest_dir = test_case.tmp_path / "number"
subprocess.check_call(
["wheel", "unpack", str(simple_wheel), "--dest", str(dest_dir)]
)
subprocess.check_call([
"wheel",
"unpack",
str(simple_wheel),
"--dest",
str(dest_dir),
])
unpack_dir = next(dest_dir.glob("*"))
assert unpack_dir.is_dir()
subprocess.check_call(
[
"wheel",
"pack",
str(unpack_dir),
"--build-number",
"42",
"--dest",
str(dest_dir),
]
)
subprocess.check_call([
"wheel",
"pack",
str(unpack_dir),
"--build-number",
"42",
"--dest",
str(dest_dir),
])
build42whl = next(dest_dir.glob("*.whl"))

test_case(
Expand Down Expand Up @@ -509,23 +513,20 @@ def fake_input(prompt: str) -> str:
case.build()


def test_version_translation(
tmp_path: Path,
caplog: pytest.LogCaptureFixture
) -> None:
def test_version_translation(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:
"""Test for Wheel2CondaConverter.translate_version_spec"""
converter = Wheel2CondaConverter(tmp_path, tmp_path)
for spec, expected in {
"~= 1.2.3" : ">=1.2.3,==1.2.*",
"~=1" : ">=1",
">=3.2 , ~=1.2.4.dev4" : ">=3.2,>=1.2.4.dev4,==1.2.*",
" >=1.2.3 , <4.0" : ">=1.2.3,<4.0",
" >v1.2+foo" : ">1.2+foo"
"~= 1.2.3": ">=1.2.3,==1.2.*",
"~=1": ">=1",
">=3.2 , ~=1.2.4.dev4": ">=3.2,>=1.2.4.dev4,==1.2.*",
" >=1.2.3 , <4.0": ">=1.2.3,<4.0",
" >v1.2+foo": ">1.2+foo",
}.items():
assert converter.translate_version_spec(spec) == expected

caplog.clear()
assert converter.translate_version_spec("bad-version") =="bad-version"
assert converter.translate_version_spec("bad-version") == "bad-version"
assert len(caplog.records) == 1
logrec = caplog.records[0]
assert logrec.levelname == "WARNING"
Expand Down
25 changes: 12 additions & 13 deletions test/api/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# ignore redefinition of test_case
# ruff: noqa: F811


@pytest.mark.external
def test_pypi_tomlkit(test_case: ConverterTestCaseFactory):
"""
Expand Down Expand Up @@ -85,16 +86,14 @@ def test_pypi_orix(test_case: ConverterTestCaseFactory) -> None:

subprocess.check_call(["conda", "install", "-p", str(test_env), "pytest", "--yes"])

subprocess.check_call(
[
"conda",
"run",
"-p",
str(test_env),
"pytest",
"--pyargs",
"orix.tests",
"-k",
"not test_restrict_to_fundamental_sector",
]
)
subprocess.check_call([
"conda",
"run",
"-p",
str(test_env),
"pytest",
"--pyargs",
"orix.tests",
"-k",
"not test_restrict_to_fundamental_sector",
])
1 change: 1 addition & 0 deletions test/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
Unit tests for `whl2conda config` CLI
"""

from __future__ import annotations

from pathlib import Path
Expand Down
Loading

0 comments on commit f9704f2

Please sign in to comment.