Skip to content
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 .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ repos:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.206
rev: v0.0.237
hooks:
- id: ruff
args: ["--fix"]
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# -- Project information -----------------------------------------------------

project = "Jupyter Releaser"
copyright = "2021, Project Jupyter"
copyright = "2021, Project Jupyter" # noqa
author = "Project Jupyter"

# The full version, including alpha/beta/rc tags.
Expand Down
3 changes: 2 additions & 1 deletion jupyter_releaser/actions/populate_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
dry_run = os.environ.get("RH_DRY_RUN", "").lower() == "true"

if not os.environ.get("RH_RELEASE_URL"):
raise RuntimeError("Cannot complete Draft Release, no draft GitHub release url found!")
msg = "Cannot complete Draft Release, no draft GitHub release url found!"
raise RuntimeError(msg)

run_action("jupyter-releaser prep-git")
run_action("jupyter-releaser ensure-sha")
Expand Down
40 changes: 21 additions & 19 deletions jupyter_releaser/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ def get_version_entry(

util.log(f"Getting changes to {repo} since {since} on branch {branch}...")

if until:
until = until.replace("%", "")
else:
until = None
until = until.replace("%", "") if until else None

md = generate_activity_md(
repo,
Expand All @@ -115,10 +112,7 @@ def get_version_entry(

entry = md.replace("[full changelog]", "[Full Changelog]")

if until:
entry = entry.replace("...None", f"...{until}")
else:
entry = entry.replace("...None", "")
entry = entry.replace("...None", f"...{until}") if until else entry.replace("...None", "")

entry = entry.splitlines()[2:]

Expand Down Expand Up @@ -186,10 +180,12 @@ def update_changelog(changelog_path, entry):
changelog = Path(changelog_path).read_text(encoding="utf-8")

if START_MARKER not in changelog or END_MARKER not in changelog:
raise ValueError("Missing insert marker for changelog")
msg = "Missing insert marker for changelog"
raise ValueError(msg)

if changelog.find(START_MARKER) != changelog.rfind(START_MARKER):
raise ValueError("Insert marker appears more than once in changelog")
msg = "Insert marker appears more than once in changelog"
raise ValueError(msg)

changelog = insert_entry(changelog, entry, version=version)
Path(changelog_path).write_text(changelog, encoding="utf-8")
Expand Down Expand Up @@ -222,13 +218,13 @@ def insert_entry(changelog, entry, version=None):
return format(changelog)


def format(changelog):
def format(changelog): # noqa
"""Clean up changelog formatting"""
changelog = re.sub(r"\n\n+", r"\n\n", changelog)
return re.sub(r"\n\n+$", r"\n", changelog)


def check_entry(
def check_entry( # noqa
ref,
branch,
repo,
Expand All @@ -252,10 +248,12 @@ def check_entry(
end = changelog.find(END_MARKER)

if start == -1 or end == -1: # pragma: no cover
raise ValueError("Missing new changelog entry delimiter(s)")
msg = "Missing new changelog entry delimiter(s)"
raise ValueError(msg)

if start != changelog.rfind(START_MARKER): # pragma: no cover
raise ValueError("Insert marker appears more than once in changelog")
msg = "Insert marker appears more than once in changelog"
raise ValueError(msg)

final_entry = changelog[start + len(START_MARKER) : end]

Expand All @@ -274,7 +272,8 @@ def check_entry(

if f"# {version}" not in final_entry: # pragma: no cover
util.log(final_entry)
raise ValueError(f"Did not find entry for {version}")
msg = f"Did not find entry for {version}"
raise ValueError(msg)

final_prs = re.findall(r"\[#(\d+)\]", final_entry)
raw_prs = re.findall(r"\[#(\d+)\]", raw_entry)
Expand All @@ -289,10 +288,12 @@ def check_entry(
if skip:
continue
if f"[#{pr}]" not in final_entry: # pragma: no cover
raise ValueError(f"Missing PR #{pr} in changelog")
msg = f"Missing PR #{pr} in changelog"
raise ValueError(msg)
for pr in final_prs:
if f"[#{pr}]" not in raw_entry: # pragma: no cover
raise ValueError(f"PR #{pr} does not belong in changelog for {version}")
msg = f"PR #{pr} does not belong in changelog for {version}"
raise ValueError(msg)

if output:
Path(output).write_text(final_entry, encoding="utf-8")
Expand Down Expand Up @@ -327,7 +328,7 @@ def splice_github_entry(orig_entry, github_entry):
if preamble.startswith("## "):
preamble = preamble.replace("## ", "### ")

lines = preamble.splitlines() + [""] + lines
lines = [*preamble.splitlines(), "", *lines]

return "\n".join(lines)

Expand All @@ -350,5 +351,6 @@ def extract_current_version(changelog_path):
body = extract_current(changelog_path)
match = re.match(r"#+ (\d\S+)", body.strip())
if not match:
raise ValueError("Could not find previous version")
msg = "Could not find previous version"
raise ValueError(msg)
return match.groups()[0]
10 changes: 5 additions & 5 deletions jupyter_releaser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ReleaseHelperGroup(click.Group):

_needs_checkout_dir: t.Dict[str, bool] = {}

def invoke(self, ctx):
def invoke(self, ctx): # noqa
"""Handle jupyter-releaser config while invoking a command"""
# Get the command name and make sure it is valid
cmd_name = ctx.protected_args[0]
Expand All @@ -28,9 +28,8 @@ def invoke(self, ctx):
envvars: t.Dict[str, str] = {}
for cmd_name in self.commands:
for param in self.commands[cmd_name].params:
if isinstance(param, click.Option):
if param.envvar:
envvars[str(param.name)] = str(param.envvar)
if isinstance(param, click.Option) and param.envvar:
envvars[str(param.name)] = str(param.envvar)

for key in sorted(envvars):
util.log(f"{key.replace('_', '-')}: {envvars[key]}")
Expand All @@ -41,7 +40,8 @@ def invoke(self, ctx):

if cmd_name.replace("-", "_") in self._needs_checkout_dir:
if not osp.exists(util.CHECKOUT_NAME):
raise ValueError("Please run prep-git first")
msg = "Please run prep-git first"
raise ValueError(msg)
os.chdir(util.CHECKOUT_NAME)

# Read in the config
Expand Down
42 changes: 22 additions & 20 deletions jupyter_releaser/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import shutil
import tempfile
import uuid
from datetime import datetime
from datetime import datetime, timezone
from glob import glob
from pathlib import Path
from subprocess import CalledProcessError
Expand All @@ -31,7 +31,8 @@ def bump_version(version_spec, version_cmd, changelog_path):
parsed = parse_version(version)

if util.SETUP_PY.exists() and not hasattr(parsed, "major"):
raise ValueError(f"Invalid version {version}")
msg = f"Invalid version {version}"
raise ValueError(msg)

# Bail if tag already exists
tag_name = f"v{version}"
Expand Down Expand Up @@ -72,7 +73,8 @@ def draft_changelog(

tags = util.run("git --no-pager tag", quiet=True)
if f"v{version}" in tags.splitlines():
raise ValueError(f"Tag v{version} already exists")
msg = f"Tag v{version} already exists"
raise ValueError(msg)

current = changelog.extract_current(changelog_path)
util.log(f"\n\nCurrent Changelog Entry:\n{current}")
Expand Down Expand Up @@ -116,8 +118,10 @@ def draft_changelog(
if str(rel.draft).lower() == "false":
continue
created = rel.created_at
d_created = datetime.strptime(created, r"%Y-%m-%dT%H:%M:%SZ")
delta = datetime.utcnow() - d_created
d_created = datetime.strptime(created, r"%Y-%m-%dT%H:%M:%SZ").astimezone(
tz=timezone.utc
)
delta = datetime.now(tz=timezone.utc) - d_created
if delta.days > 0:
gh.repos.delete_release(rel.id)

Expand Down Expand Up @@ -254,7 +258,8 @@ def delete_release(auth, release_url, dry_run=False):
match = re.match(pattern, release_url)
match = match or re.match(util.RELEASE_API_PATTERN, release_url)
if not match:
raise ValueError(f"Release url is not valid: {release_url}")
msg = f"Release url is not valid: {release_url}"
raise ValueError(msg)

gh = util.get_gh_object(dry_run=dry_run, owner=match["owner"], repo=match["repo"], token=auth)
release = util.release_for_url(gh, release_url)
Expand Down Expand Up @@ -300,14 +305,16 @@ def extract_release(auth, dist_dir, dry_run, release_url):
if asset.name.endswith(".json"):
continue
if asset.name not in asset_shas:
raise ValueError(f"{asset.name} was not found in asset_shas file")
msg = f"{asset.name} was not found in asset_shas file"
raise ValueError(msg)
if util.compute_sha256(dist / asset.name) != asset_shas[asset.name]:
raise ValueError(f"sha for {asset.name} does not match asset_shas file")
msg = f"sha for {asset.name} does not match asset_shas file"
raise ValueError(msg)

os.chdir(orig_dir)


def publish_assets(
def publish_assets( # noqa
dist_dir,
npm_token,
npm_cmd,
Expand All @@ -321,7 +328,7 @@ def publish_assets(
"""Publish release asset(s)"""
os.environ["NPM_REGISTRY"] = npm_registry
os.environ["TWINE_REPOSITORY_URL"] = twine_repository_url
twine_token = ""
twine_token = "" # noqa

if len(glob(f"{dist_dir}/*.tgz")):
npm.handle_npm_config(npm_token)
Expand All @@ -330,10 +337,7 @@ def publish_assets(

res = python_package.split(":")
python_package_path = res[0]
if len(res) == 2:
python_package_name = res[1].replace("-", "_")
else:
python_package_name = ""
python_package_name = res[1].replace("-", "_") if len(res) == 2 else "" # noqa

if release_url and len(glob(f"{dist_dir}/*.whl")):
twine_token = python.get_pypi_token(release_url, python_package_path)
Expand All @@ -357,10 +361,7 @@ def publish_assets(
suffix = Path(path).suffix
if suffix in [".gz", ".whl"]:
dist: Union[Type[SDist], Type[Wheel]]
if suffix == ".gz":
dist = SDist
else:
dist = Wheel
dist = SDist if suffix == ".gz" else Wheel
pkg = dist(path)
if not python_package_name or python_package_name == pkg.name:
env = os.environ.copy()
Expand Down Expand Up @@ -409,7 +410,7 @@ def publish_release(auth, dry_run, release_url):
util.actions_output("release_url", release.html_url)


def prep_git(ref, branch, repo, auth, username, url):
def prep_git(ref, branch, repo, auth, username, url): # noqa
"""Set up git"""
repo = repo or util.get_repo()

Expand Down Expand Up @@ -538,7 +539,8 @@ def forwardport_changelog(auth, ref, branch, repo, username, changelog_path, dry
break

if not prev_header:
raise ValueError("No anchor for previous entry")
msg = "No anchor for previous entry"
raise ValueError(msg)

# Check out the branch again
util.run(f"git checkout -B {branch} origin/{branch}")
Expand Down
10 changes: 5 additions & 5 deletions jupyter_releaser/mock_github.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""A mock GitHub API implementation."""
import atexit
import datetime
import json
import os
import tempfile
import uuid
from datetime import datetime, timezone
from typing import Dict, List

from fastapi import FastAPI, Request
Expand Down Expand Up @@ -57,7 +57,7 @@ def write_to_file(name, data):
class Asset(BaseModel):
"""An asset model."""

id: int
id: int # noqa
name: str
content_type: str
size: int
Expand All @@ -83,7 +83,7 @@ class Release(BaseModel):
published_at: str = ""
draft: bool
body: str = ""
id: int
id: int # noqa
node_id: str = ""
author: str = ""
html_url: str
Expand Down Expand Up @@ -121,7 +121,7 @@ class Tag(BaseModel):
"""A tag model."""

ref: str
object: TagObject
object: TagObject # noqa


releases: Dict[str, "Release"] = load_from_file("releases", Release)
Expand Down Expand Up @@ -152,7 +152,7 @@ async def create_a_release(owner: str, repo: str, request: Request) -> Release:
html_url = f"{base_url}/{owner}/{repo}/releases/tag/{data['tag_name']}"
upload_url = f"{base_url}/repos/{owner}/{repo}/releases/{release_id}/assets"
fmt_str = r"%Y-%m-%dT%H:%M:%SZ"
created_at = datetime.datetime.utcnow().strftime(fmt_str)
created_at = datetime.now(tz=timezone.utc).strftime(fmt_str)
model = Release(
id=release_id,
url=url,
Expand Down
3 changes: 2 additions & 1 deletion jupyter_releaser/npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def extract_dist(dist_dir, target):
if "main" in data:
main = osp.join(target, "package", data["main"])
if not osp.exists(main):
raise ValueError(f"{name} is missing 'main' file {data['main']}")
msg = f"{name} is missing 'main' file {data['main']}"
raise ValueError(msg)

shutil.move(str(target / "package"), str(pkg_dir))

Expand Down
2 changes: 1 addition & 1 deletion jupyter_releaser/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def check_dist(
dist_file = util.normalize_path(dist_file)
dist_dir = os.path.dirname(dist_file) # used for check cmds.

for cmd in [check_cmd] + list(extra_check_cmds or []):
for cmd in [check_cmd, *list(extra_check_cmds or [])]:
util.run(cmd.format(**locals()))

test_commands = []
Expand Down
6 changes: 3 additions & 3 deletions jupyter_releaser/tee.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def _read_stream(stream: StreamReader, callback: Callable[..., Any]) -> No
break


async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess:
async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess: # noqa
platform_settings: Dict[str, Any] = {}
if platform.system() == "Windows":
platform_settings["env"] = os.environ
Expand All @@ -61,10 +61,10 @@ async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess:
tee = kwargs.get("tee", True)
stdout = kwargs.get("stdout", sys.stdout)
if stdout == subprocess.DEVNULL or not tee:
stdout = open(os.devnull, "w")
stdout = open(os.devnull, "w") # noqa
stderr = kwargs.get("stderr", sys.stderr)
if stderr == subprocess.DEVNULL or not tee:
stderr = open(os.devnull, "w")
stderr = open(os.devnull, "w") # noqa

# We need to tell subprocess which shell to use when running shell-like
# commands.
Expand Down
5 changes: 2 additions & 3 deletions jupyter_releaser/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,8 @@ def test_publish_assets_py(py_package, runner, mocker, git_prep, mock_github):

def wrapped(cmd, **kwargs):
nonlocal called
if "twine upload" in cmd:
if kwargs["env"]["TWINE_PASSWORD"] == "foo-token":
called += 1
if "twine upload" in cmd and kwargs["env"]["TWINE_PASSWORD"] == "foo-token":
called += 1
return orig_run(cmd, **kwargs)

mock_run = mocker.patch("jupyter_releaser.util.run", wraps=wrapped)
Expand Down
Loading