Skip to content

Commit

Permalink
chore: use uv for package management; hatchling for builds
Browse files Browse the repository at this point in the history
  • Loading branch information
ahal committed Sep 6, 2024
1 parent c428679 commit 962d6a0
Show file tree
Hide file tree
Showing 21 changed files with 1,515 additions and 2,122 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
cache: 'pip'
- name: Build package distributions
run: |
pip install wheel
python setup.py sdist bdist_wheel
pip install build
python -m build
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
11 changes: 11 additions & 0 deletions .hatch_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pathlib import Path

from hatchling.metadata.plugin.interface import MetadataHookInterface


class MetaDataHook(MetadataHookInterface):
def update(self, metadata):
namespace = {}
src = Path(self.root) / "src" / "taskgraph" / "__init__.py"
exec(src.read_text(), namespace)
metadata["version"] = namespace["__version__"]
93 changes: 65 additions & 28 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,50 @@ Clone the Repo
--------------

To contribute to Taskgraph or use the debugging tools, you'll need to clone the
repository, activate a virtualenv and install dependencies:
repository:

.. code-block::
# first fork taskgraph
git clone https://github.com/<user>/taskgraph
cd taskgraph
git remote add upstream https://github.com/taskcluster/taskgraph
python -m venv taskgraph && source taskgraph/bin/activate
pip install -r requirements/dev.txt
python setup.py develop
Run Taskgraph
-------------

We use a tool called `uv`_ to manage Taskgraph and its dependencies. First,
follow the `installation instructions`_. Then run:

.. code-block::
uv run taskgraph --help
The ``uv run`` command does several things:

1. Creates a virtualenv for the project in a ``.venv`` directory (if necessary).
2. Syncs the project's dependencies as pinned in ``uv.lock`` (if necessary).
3. Installs ``taskgraph`` as an editable package (if necessary).
4. Invokes the specified command (in this case ``taskgraph --help``).

Anytime you wish to run a command within the project's virtualenv, prefix it
with ``uv run``. Alternatively you can activate the virtualenv as normal:

.. code-block::
source .venv/bin/activate
taskgraph --help
Just beware that with this method, the dependencies won't automatically be
synced prior to running your command. You can still sync dependencies manually
with:

.. code-block::
uv sync
.. _uv: https://docs.astral.sh/uv/
.. _installation instructions: https://docs.astral.sh/uv/getting-started/installation/

Running Tests
-------------
Expand All @@ -29,12 +62,9 @@ Tests are run with the `pytest`_ framework:

.. code-block::
pytest
We use `tox`_ to run tests across multiple versions of Python.
uv run pytest
.. _pytest: https://pre-commit.com/
.. _tox: https://tox.wiki/en/latest/

Running Checks
--------------
Expand All @@ -52,12 +82,11 @@ manually, you can use:
$ pre-commit run
Some of the checks we enforce include `black`_, `flake8`_ and `yamllint`_. See
Some of the checks we enforce include `ruff`_ and `yamllint`_. See
`pre-commit-config.yaml`_ for a full list.

.. _pre-commit: https://pre-commit.com/
.. _black: https://black.readthedocs.io
.. _flake8: https://flake8.pycqa.org/en/latest/
.. _ruff: https://docs.astral.sh/ruff/
.. _yamllint: https://yamllint.readthedocs.io/en/stable/
.. _pre-commit-config.yaml: https://github.com/taskcluster/taskgraph/blob/main/.pre-commit-config.yaml

Expand All @@ -71,15 +100,15 @@ docs, run:

.. code-block::
make livehtml
uv run make livehtml
This will start a live server that automatically re-generates when you edit a
documentation file. Alternatively you can generate static docs under the
``docs/_build`` directory:

.. code-block::
make html
uv run make html
Taskgraph also uses the ``autodoc`` extension to generate an API reference.
When new modules are created, be sure to add an ``autodoc`` directive for
Expand All @@ -90,41 +119,36 @@ them in the source reference.
Managing Dependencies
---------------------

To help lock dependencies, Taskgraph uses `uv`_. Make sure it is installed and
on your ``$PATH``.

Adding a New Dependency
~~~~~~~~~~~~~~~~~~~~~~~

To add or update a dependency first edit the relevant ``.in`` file under the
``requirements`` directory. If the dependency is needed by the actual Taskgraph
library, edit ``requirements/base.in``. If it's required by the CI system, edit
``requirements/test.in``. And if it's only needed for developing Taskgraph,
edit ``requirements/dev.in``.
To add a new dependency to Taskgraph, run:

.. code-block::
uv add <dependency>
Next run the following script:
To add a new dependency that is only used in the development process, run:

.. code-block::
./requirements/pin.sh
uv add --dev <dependency>
Updating Existing Dependencies
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you'd like to update all dependencies within their constraints defined in
the ``.in`` files, run:
the ``pyproject.toml`` file, run:

.. code-block::
./requirements/pin.sh -U
uv sync -U
Or if you'd like to update a specific dependency:

.. code-block::
./requirements/pin.sh -P <package>
.. _uv: https://github.com/astral-sh/uv
uv sync -P <package>
Releasing
---------
Expand All @@ -141,3 +165,16 @@ In order to release a new version of Taskgraph, you will need to:

.. _pypi: https://pypi.org/project/taskcluster-taskgraph
.. _DockerHub: https://hub.docker.com/r/mozillareleases/taskgraph/tags

Building the Package
--------------------

Typically building the package manually is not required, as this is handled in
automation prior to release. However, if you'd like to test the package builds
manually, you can do so with:

.. code-block::
uvx --from build pyproject-build --installer uv
Source and wheel distributions will be available under the ``dist/`` directory.
77 changes: 76 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,91 @@
### Project
[project]
name = "taskcluster-taskgraph"
description = "Build taskcluster taskgraphs"
readme = "README.rst"
authors = [
{ name = "Mozilla Release Engineering", email = "release+taskgraph@mozilla.com" },
]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Software Development",
]
requires-python = ">=3.8"
dependencies = [
"appdirs>=1.4",
"cookiecutter~=2.1",
"json-e>=2.7",
"mozilla-repo-urls",
"PyYAML>=5.3.1",
"redo>=2.0",
"requests>=2.25",
"slugid>=2.0",
"taskcluster-urls>=11.0",
"voluptuous>=0.12.1",
]
dynamic = ["version"] # set in src/taskgraph/__init__.py

[project.optional-dependencies]
load-image = [
"zstandard"
]

[project.scripts]
taskgraph = "taskgraph.main:main"

[project.urls]
Repository = "https://github.com/taskcluster/taskgraph"
Issues = "https://github.com/taskcluster/taskgraph/issues"

[tool.uv]
dev-dependencies = [
"coverage",
"mock",
"pre-commit",
"pyright",
"pytest",
"pytest-mock",
"responses",
"sphinx",
"sphinx-autobuild",
"sphinx-argparse",
"sphinx-book-theme >=1",
"sphinxcontrib-mermaid",
"zstandard",
]

### Build
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["src/taskgraph"]

[tool.hatch.metadata.hooks.custom]
path = ".hatch_build.py" # handles reading version from src/taskgraph/__init__.py

### Test
[tool.pytest.ini_options]
xfail_strict = true

[tool.coverage.run]
branch = true
source = ["src/taskgraph/", "src/taskgraph/run-task/", "test"]

### Lint and Format
[tool.ruff]
extend-exclude = [
"taskcluster/scripts/external_tools"
]
line-length = 88
target-version = "py38"

[tool.ruff.lint]
select = [
Expand Down
10 changes: 0 additions & 10 deletions requirements/base.in

This file was deleted.

Loading

0 comments on commit 962d6a0

Please sign in to comment.