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

Fix scripts to run under uv correctly #174

Merged
merged 4 commits into from
Jan 10, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:

- name: Changelog
if: ${{ !contains(github.event.head_commit.author, 'renovate') }}
run: PYTHONPATH=build-support/bin uv run build changelog check
run: uv run scripts/changelog.py check

# - name: Upload coverage to CodeCov
# uses: codecov/codecov-action@v1
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ WORKDIR /home/dev/src
FROM uv as release

USER dev
ENV PYTHONPATH="build-support/bin/"

WORKDIR /home/dev
RUN mkdir -m 0750 .ssh
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ This set of libraries is managed using [uv](https://docs.astral.sh/uv/).
- Install `uv` as described in the manual: https://docs.astral.sh/uv/
- Bootstrap the `uv` environment: `uv python install 3.11 ; uv sync`

> [!WARNING]
> If you're running `uv` locally, any calls to the `build` command will need help.
> Prepend `PYTHONPATH=build-support/bin` to the command so uv will launch it properly.

#### Use the Docker Compose environment (recommended)

The Compose environment includes a container for general use called `shell` and one specifically for building releases called `release`. In either case, you'll get a shell with `uv` already set up, and with a PostgreSQL database available.
Expand All @@ -37,7 +33,7 @@ The `release` container is special and is set up to run `build` commands, includ
**Using:**
1. Build the images, so the source code in the image is up to date: `docker compose build`
2. Get a shell: `docker compose run --rm -ti release bash`
3. Run your command: `uv run build release create etc. etc. etc.`
3. Run your command: `uv run scripts/release.py create` etc. etc. etc.
4. If you've done things that involve Git, make sure you `git pull` when you leave the session.

### Navigating this repository
Expand Down Expand Up @@ -89,7 +85,7 @@ Run `uv run pytest`. This should run all the tests. If you want to run a specifi

We maintain changelogs in `changelog.d/` directories with each app. To create a new changelog for your changes, run:

- `uv run build changelog create --app APPNAME`
- `uv run scripts/changelog.py create --app APPNAME`
- `APPNAME`: the name of an application directory

Note warning above about `PYTHONPATH`. You will need to adjust permissions/ownership on the new file if you're using the Compose setup.
Expand All @@ -102,7 +98,7 @@ Changelogs are maintained according to [Keep a Changelog](https://keepachangelog
Versioning uses a date-based versioning scheme with incremental builds on the same day.
Version tags follow `{package-name}/v{version}`
To perform a release, run:
- `uv run build release create --app APPNAME --push`:
- `uv run scripts/release.py create --app APPNAME --push`:
- `APPNAME`: the name of an application directory

`release` expects to be run on the `main` branch and it expects you to not have changes pending.
Expand Down
21 changes: 0 additions & 21 deletions build-support/bin/mitol/build_support/cli.py

This file was deleted.

12 changes: 0 additions & 12 deletions build-support/bin/mitol/build_support/commands/version.py

This file was deleted.

3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ requires-python = ">= 3.8"
requires = ["hatchling"]
build-backend = "hatchling.build"

[project.scripts]
build = "mitol.build_support.cli:cli"

[tool.uv]
managed = true
dev-dependencies = [
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@

import toml

from mitol.build_support.contextlib import chdir

SOURCE_PATH = "src/"
from scripts.contextlibs import chdir


def get_source_dir() -> Path:
"""Get the source directory"""
source_dir = Path.cwd()
if SOURCE_PATH not in str(source_dir):
source_dir = source_dir / SOURCE_PATH
return source_dir
return Path(__file__).parent.parent / "src"


def get_app_dir(path: str) -> Path:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
from scriv.create import create
from scriv.scriv import Scriv

from mitol.build_support.apps import App, list_apps
from mitol.build_support.contextlib import chdir
from mitol.build_support.decorators import app_option, pass_app, pass_project
from mitol.build_support.project import Project
from scripts.apps import App, list_apps
from scripts.contextlibs import chdir
from scripts.decorators import app_option, pass_app, pass_project
from scripts.project import Project


@group("changelog")
@pass_context
def changelog(ctx): # noqa: ARG001
def changelog(ctx):
"""Manage application changelogs"""
ctx.ensure_object(Project)

makedirs("changelog.d", exist_ok=True) # noqa: PTH103

Expand Down Expand Up @@ -49,12 +50,22 @@ def _echo_change(change: Diff):
"""Echo the change to stdout"""
line = [change.change_type, change.a_path]

if change.renamed:
if change.renamed_file:
line.extend(["->", changelog.b_path])

echo(indent(" ".join(line), "\t"))


def _is_source_excluded(path) -> bool:
excluded_paths = ["*/changelog.d/*", "*/CHANGELOG.md"]
return any([fnmatch(path, exclude) for exclude in excluded_paths]) # noqa: C419


def _is_changelog_excluded(path) -> bool:
excluded_paths = ["*/scriv.ini"]
return any([fnmatch(path, exclude) for exclude in excluded_paths]) # noqa: C419


@changelog.command()
@option(
"-b",
Expand All @@ -71,7 +82,7 @@ def _echo_change(change: Diff):
@simple_verbosity_option()
@pass_project
@pass_context
def check(ctx: Context, project: Project, base: str, target: str): # noqa: C901
def check(ctx: Context, project: Project, base: str, target: str):
"""Check for missing changelogs"""
base_commit = project.repo.commit(base)
target_commit = project.repo.commit(target)
Expand All @@ -81,38 +92,24 @@ def check(ctx: Context, project: Project, base: str, target: str): # noqa: C901
for app_abs_path in list_apps():
app_rel_path = app_abs_path.relative_to(project.path)

excluded_paths = [app_rel_path / "changelog.d/*", app_rel_path / "CHANGELOG.md"]

def _is_excluded(path):
return any([fnmatch(path, exclude) for exclude in excluded_paths]) # noqa: B023, C419

source_changes = [
change
for change in base_commit.diff(target_commit, paths=[app_rel_path])
if not _is_excluded(change.a_path) and not _is_excluded(change.b_path)
if not _is_source_excluded(change.a_path)
and not _is_source_excluded(change.b_path)
]
has_source_changes = len(source_changes) > 0

changelogd_changes = base_commit.diff(
target_commit, paths=[app_rel_path / "changelog.d"]
)
changelogd_changes = [
change
for change in base_commit.diff(
target_commit, paths=[app_rel_path / "changelog.d"]
)
if not _is_changelog_excluded(change.a_path)
and not _is_changelog_excluded(change.b_path)
]
has_changelogd_changes = len(changelogd_changes) > 0

# If there's changes to the base requirements.txt, then we need to allow for
# changelogs to exist without the app code changing. There won't necessarily be
# changes in the individual apps if we're just updating project-wide dependencies. # noqa: E501
# So, if there's no source changes, check if there's been changes to the
# requirements, and treat that as source changes.

if not has_source_changes:
reqs_paths = [
"build-support/requirements/requirements-testing.txt",
"build-support/requirements/requirements.txt",
]

reqs_changes = base_commit.diff(target_commit, paths=reqs_paths)
has_source_changes = len(reqs_changes) > 0

if has_source_changes and not has_changelogd_changes:
echo(f"Changelog(s) are missing in {app_rel_path} for these changes:")
for change in source_changes:
Expand Down Expand Up @@ -146,3 +143,7 @@ def _is_excluded(path):

if is_error:
ctx.exit(1)


if __name__ == "__main__":
changelog()
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from click import Choice, Command, make_pass_decorator, option
from cloup import Context, pass_context

from mitol.build_support.apps import App, list_app_names
from mitol.build_support.project import Project
from scripts.apps import App, list_app_names
from scripts.project import Project

pass_project = make_pass_decorator(Project)
pass_app = make_pass_decorator(App)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from click import echo, pass_context
from cloup import Context, group, option

from mitol.build_support.apps import App
from mitol.build_support.commands import changelog, version
from mitol.build_support.decorators import (
from scripts import changelog, version
from scripts.apps import App
from scripts.decorators import (
app_option,
no_require_main,
pass_app,
pass_project,
)
from mitol.build_support.project import Project
from scripts.project import Project


@group()
def release():
pass
@pass_context
def release(ctx: Context):
ctx.ensure_object(Project)


@release.command()
Expand Down Expand Up @@ -88,3 +89,7 @@ def push_to_remote(project: Project, app: App):
[app.version_git_tag, "HEAD"],
follow_tags=True,
)


if __name__ == "__main__":
release()
File renamed without changes.
24 changes: 24 additions & 0 deletions scripts/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from bumpver import cli
from cloup import Context, group, pass_context

from scripts.decorators import app_option
from scripts.project import Project


@group()
@pass_context
def version(ctx: Context):
"""CLI for build tools"""
ctx.ensure_object(Project)
ctx.invoke(cli.cli, **ctx.params)


version.add_command(app_option(cli.grep))
version.add_command(app_option(cli.init))
version.add_command(app_option(cli.show))
version.add_command(cli.test) # this doesn't require config so doesn't require --app
version.add_command(app_option(cli.update))


if __name__ == "__main__":
version()
2 changes: 1 addition & 1 deletion src/authentication/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/common/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/digitalcredentials/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/geoip/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/google_sheets/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/google_sheets_deferrals/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/google_sheets_refunds/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/hubspot_api/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/mail/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/oauth_toolkit_extensions/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/olposthog/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/openedx/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
2 changes: 1 addition & 1 deletion src/payment_gateway/changelog.d/scriv.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[scriv]
format = md
md_header_level = 2
entry_title_template = file: ../../build-support/scriv/entry_title.${config:format}.j2
entry_title_template = file: ../../scripts/scriv/entry_title.${config:format}.j2
version = literal: __init__.py: __version__
Loading
Loading