Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt ruff and reduce pre-commit usage #97

Merged
merged 6 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,18 @@ jobs:
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- run: hatch run docs:build

pre_commit:
name: pre-commit
test_lint:
name: Test Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- uses: jupyterlab/maintainer-tools/.github/actions/pre-commit@v1
- name: Run Linters
run: |
hatch run typing:test
hatch run lint:style
pipx run 'validate-pyproject[all]' pyproject.toml
pipx run doc8 --max-line-length=200 --ignore-path=docs/source/other/full-config.rst

test_minimum_versions:
name: Test Minimum Versions
Expand Down Expand Up @@ -131,7 +136,8 @@ jobs:
if: always()
needs:
- build
- pre_commit
- migration
- test_lint
- test_docs
- test_minimum_versions
- test_prereleases
Expand Down
59 changes: 13 additions & 46 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ci:
autoupdate_schedule: monthly

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand All @@ -15,59 +18,23 @@ repos:
- id: check-builtin-literals
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
files: \.py$

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
hooks:
- id: mypy
exclude: tests/data
additional_dependencies: [types-requests]

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.10.1
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.19.2
hooks:
- id: validate-pyproject
stages: [manual]
- id: check-github-workflows

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.16
hooks:
- id: mdformat

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.0
hooks:
- id: pyupgrade
args: [--py38-plus]

- repo: https://github.com/PyCQA/doc8
rev: v1.0.0
hooks:
- id: doc8
args: [--max-line-length=200]
stages: [manual]

- repo: https://github.com/john-hen/Flake8-pyproject
rev: 1.2.2
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: Flake8-pyproject
alias: flake8
exclude: tests/data
additional_dependencies:
["flake8-bugbear==22.6.22", "flake8-implicit-str-concat==0.2.0"]
stages: [manual]
- id: black

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.19.2
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.165
hooks:
- id: check-github-workflows
- id: ruff
args: ["--fix"]
16 changes: 10 additions & 6 deletions hatch_jupyter_builder/compare_migrated/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Compare the dist file created by a migrated package to one created by the original."""
import argparse
import glob
import logging
import os
import shutil
import subprocess
Expand Down Expand Up @@ -45,6 +46,9 @@ def filter_file(path):
def main(source_dir, target_dir, dist_name):
subprocess.check_call([sys.executable, "-m", "pip", "install", "build"])

logger = logging.getLogger(__name__)
logging.basicConfig()

build_file(source_dir, dist_name)
build_file(target_dir, dist_name)

Expand All @@ -58,18 +62,18 @@ def main(source_dir, target_dir, dist_name):
removed = source_names - target_names
removed = [r for r in removed if not filter_file(r)]
if removed:
print("\nRemoved_files:")
[print(f) for f in removed]
logger.info("\nRemoved_files:")
[logger.info(f) for f in removed] # type:ignore

added = target_names - source_names
added = [a for a in added if not filter_file(a)]
if added:
print("\nAdded files:")
[print(f) for f in added]
logger.info("\nAdded files:")
[logger.info(f) for f in added] # type:ignore

print()
logger.info("")

return dict(added=added, removed=removed)
return {"added": added, "removed": removed}


def make_parser(
Expand Down
68 changes: 38 additions & 30 deletions hatch_jupyter_builder/migrate/_migrate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import os
import subprocess
import sys
Expand All @@ -8,17 +9,21 @@
import tomli_w
from packaging import version

logger = logging.getLogger(__name__)
logging.basicConfig()

# Handle the version.
# If it is a dev version, use the previous minor version.
builder_version = version.parse(sys.argv[1])
if builder_version.is_devrelease:
assert isinstance(builder_version, version.Version)
builder_version = f"{builder_version.major}.{builder_version.minor - 1}.0"
builder_version = f">={builder_version}"
builder_version_str = f">={builder_version.major}.{builder_version.minor - 1}.0"
else:
builder_version_str = f">={builder_version}"
if "BUILDER_VERSION_SPEC" in os.environ:
builder_version = os.environ["BUILDER_VERSION_SPEC"]
builder_version_str = os.environ["BUILDER_VERSION_SPEC"]

print("\n\nStarting pyproject.toml migration")
logger.info("\n\nStarting pyproject.toml migration")

warnings = []

Expand Down Expand Up @@ -50,12 +55,12 @@
current_version = "!!UNKONWN!!"

# Run the hatch migration script.
print("Running hatch migration")
logger.info("Running hatch migration")
subprocess.run([sys.executable, "-m", "hatch", "new", "--init"])

# Run the jupyter-packaging migration script - must be done after
# hatch migration to avoid conflicts.
print("Running jupyter-packaging migration")
logger.info("Running jupyter-packaging migration")
here = os.path.abspath(os.path.dirname(__file__))
prev_pythonpath = os.environ.get("PYTHONPATH", "")
if prev_pythonpath:
Expand Down Expand Up @@ -93,15 +98,15 @@

# Migrate and remove unused config.
# Read in the project.toml after auto migration.
print("Migrating static data")
logger.info("Migrating static data")
data = tomli.loads(pyproject.read_text("utf-8"))
tool_table = data.setdefault("tool", {})

# Handle license file.
for lic_name in ["LICENSE", "COPYING.md", "LICENSE.txt"]:
for fname in os.listdir("."):
if fname.lower() == lic_name.lower():
data["project"]["license"] = dict(file=fname)
data["project"]["license"] = {"file": fname}

# Add the other build requirements.
data["build-system"]["requires"].extend(requires)
Expand All @@ -124,11 +129,11 @@
del targets_table["sdist"]

# Exclude the .github folder by default.
targets_table["sdist"] = dict(exclude=[".github"])
targets_table["sdist"] = {"exclude": [".github"]}

hooks_table = build_table.setdefault("hooks", {})
builder_table = hooks_table.setdefault("jupyter-builder", {})
builder_table["dependencies"] = [f"hatch-jupyter-builder{builder_version}"]
builder_table["dependencies"] = [f"hatch-jupyter-builder{builder_version_str}"]
builder_table["build-function"] = "hatch_jupyter_builder.npm_builder"

# Migrate the jupyter-packaging static data.
Expand Down Expand Up @@ -175,25 +180,28 @@
data["project"].pop("dynamic", None)

tbump_table = tool_table.setdefault("tbump", {})
tbump_table["version"] = dict(
current=current_version,
regex=r"""
tbump_table["version"] = {
"current": current_version,
"regex": r"""
(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)((?P<channel>a|b|rc|.dev)(?P<release>\d+))?
""".strip(),
)
tbump_table["git"] = dict(message_template=r"Bump to {new_version}", tag_template=r"v{new_version}")
tbump_table["field"] = [dict(name="channel", default=""), dict(name="release", default="")]
}
tbump_table["git"] = {
"message_template": r"Bump to {new_version}",
"tag_template": r"v{new_version}",
}
tbump_table["field"] = [{"name": "channel", "default": ""}, {"name": "release", "default": ""}]
tbump_table["file"] = [
dict(
src="pyproject.toml",
version_template='version = "{major}.{minor}.{patch}{channel}{release}"',
)
{
"src": "pyproject.toml",
"version_template": 'version = "{major}.{minor}.{patch}{channel}{release}"',
}
]

# Add entry for _version.py if it exists.
version_py = Path(project_name) / "_version.py"
if version_py.exists():
tbump_table["file"].append(dict(src=str(version_py)))
tbump_table["file"].append({"src": str(version_py)})
text = version_py.read_text(encoding="utf-8")
if current_version not in text:
warnings.append(
Expand All @@ -207,10 +215,10 @@
npm_version = json.loads(text)["version"]
if npm_version == current_version:
tbump_table["file"].append(
dict(
src="package.json",
version_template='"version": "{major}.{minor}.{patch}{channel}{release}"',
)
{
"src": "package.json",
"version_template": '"version": "{major}.{minor}.{patch}{channel}{release}"',
}
)

# Add a setup.py shim.
Expand All @@ -225,13 +233,13 @@
os.remove(fname)

# Write out the new config.
print("\n\nWriting pyproject.toml")
logger.info("\n\nWriting pyproject.toml")
pyproject.write_text(tomli_w.dumps(data), "utf-8")

if warnings:
print("\n\nWarning!! Not everything could be migrated automatically.")
print("Please address the following concerns:")
logger.info("\n\nWarning!! Not everything could be migrated automatically.")
logger.info("Please address the following concerns:")
for warning in warnings:
print(f" - {warning}")
logger.info(f" - {warning}")

print("\n\nMigration complete!")
logger.info("\n\nMigration complete!")
5 changes: 4 additions & 1 deletion hatch_jupyter_builder/migrate/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Migrate a Jupyter project from setuptools/jupyter-packaging to hatch and
hatch_jupyter_builder."""
import argparse
import logging
import os
import subprocess
import sys
Expand All @@ -13,13 +14,15 @@


def main(td, target_dir):
logger = logging.getLogger(__name__)
logging.basicConfig()
venv.create(td, with_pip=True)
if os.name == "nt":
python = Path(td) / "Scripts/python.exe"
else:
python = Path(td) / "bin/python"

print("Installing in temporary virtual environment...")
logger.info("Installing in temporary virtual environment...")

# Create a virtual environment and use it to run the migration.
runner = subprocess.check_call
Expand Down
4 changes: 1 addition & 3 deletions hatch_jupyter_builder/migrate/jupyter_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__this_shim = sys.modules.pop("jupyter_packaging")
__current_directory = sys.path.pop(0)

import jupyter_packaging as __real_jupyter_packaging
import jupyter_packaging as __real_jupyter_packaging # type:ignore

sys.path.insert(0, __current_directory)
sys.modules["jupyter_packaging"] = __this_shim
Expand Down Expand Up @@ -113,8 +113,6 @@ def create_cmdclass(
else:
shared_data[f"{dname}/{pattern}"] = f"{path}/{pattern}"

print(shared_data)

_write_config("tool.hatch.build.targets.wheel.shared-data", shared_data)

return __real_jupyter_packaging.create_cmdclass(
Expand Down
5 changes: 4 additions & 1 deletion hatch_jupyter_builder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,18 @@ def install_pre_commit_hook():
ARGS+=(--hook-dir "$HERE" -- "$@")
exec "$INSTALL_PYTHON" -m pre_commit "${{ARGS[@]}}"
"""
log = _get_log()
if not os.path.exists(".git"):
log = _get_log()
log.warning("Refusing to install pre-commit hook since this is not a git repository")
return

path = Path(".git/hooks/pre-commit")
if not path.exists():
log.info("Writing pre-commit hook")
with open(path, "w") as fid:
fid.write(data)
else:
log.warning("Refusing to overwrite pre-commit hook")

mode = os.stat(path).st_mode
mode |= (mode & 0o444) >> 2 # copy R bits to X
Expand Down
Loading