Skip to content

Commit

Permalink
Merge pull request #1919 from ytausch/ruff
Browse files Browse the repository at this point in the history
Introduce Ruff Linter
  • Loading branch information
beckermr authored Jul 25, 2024
2 parents ee4cc78 + a3f1dd0 commit 0e2a3b6
Show file tree
Hide file tree
Showing 36 changed files with 682 additions and 783 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ repos:
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.5
hooks:
- id: ruff
args: [ --fix ]

ci:
autofix_commit_msg: |
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Overview

[![tests](https://github.com/conda-forge/conda-smithy/workflows/tests/badge.svg)](https://github.com/conda-forge/conda-smithy/actions?query=workflow%3Atests)
[![Coverage Status](https://coveralls.io/repos/github/conda-forge/conda-smithy/badge.svg?branch=main)](https://coveralls.io/github/conda-forge/conda-smithy?branch=main)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)

Installation
Expand Down
17 changes: 8 additions & 9 deletions bootstrap-obvious-ci-and-miniconda.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import os
import platform
import subprocess
import sys

try:
from urllib.request import urlretrieve
Expand Down Expand Up @@ -41,15 +40,15 @@ def miniconda_url(
"Windows": "Windows",
}
if target_system not in system_to_miniconda_os:
raise ValueError("Unexpected system {!r}.".format(target_system))
raise ValueError(f"Unexpected system {target_system!r}.")
template_values["OS"] = system_to_miniconda_os[target_system]

miniconda_os_ext = {"Linux": "sh", "MacOSX": "sh", "Windows": "exe"}
template_values["ext"] = miniconda_os_ext[template_values["OS"]]

if major_py_version not in ["3"]:
raise ValueError(
"Unexpected major Python version {!r}.".format(major_py_version)
f"Unexpected major Python version {major_py_version!r}."
)
template_values["major_py_version"] = major_py_version

Expand All @@ -64,10 +63,10 @@ def main(
install_obvci=True,
):
system = platform.system()
URL = miniconda_url(
url = miniconda_url(
system, target_arch, major_py_version, miniconda_version
)
basename = URL.rsplit("/", 1)[1]
basename = url.rsplit("/", 1)[1]
if system in ["Linux", "Darwin"]:
cmd = ["bash", basename, "-b", "-p", target_dir]
bin_dir = "bin"
Expand All @@ -86,14 +85,14 @@ def main(
raise ValueError("Unsupported operating system.")

if not os.path.exists(basename):
print("Downloading from {}".format(URL))
urlretrieve(URL, basename)
print(f"Downloading from {url}")
urlretrieve(url, basename)
else:
print("Using cached version of {}".format(URL))
print(f"Using cached version of {url}")

# Install with powershell.
if os.path.exists(target_dir):
raise IOError("Installation directory already exists")
raise OSError("Installation directory already exists")
subprocess.check_call(cmd)

if not os.path.isdir(target_dir):
Expand Down
2 changes: 1 addition & 1 deletion conda_smithy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
try:
from ._version import __version__
from conda_smithy._version import __version__
except ImportError:
__version__ = "0.0.0"
81 changes: 38 additions & 43 deletions conda_smithy/anaconda_token_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
import os
import sys
from contextlib import redirect_stderr, redirect_stdout
from github import Github

import requests
from github import Github

from .utils import update_conda_forge_config
from conda_smithy.utils import update_conda_forge_config


def _get_anaconda_token():
"""use this helper to enable easier patching for tests"""
try:
from .ci_register import anaconda_token
from conda_smithy.ci_register import anaconda_token

return anaconda_token
except ImportError:
Expand Down Expand Up @@ -58,7 +58,7 @@ def rotate_anaconda_token(
# note that these imports cover all providers
from .ci_register import travis_endpoint # noqa
from .azure_ci_utils import default_config # noqa
from .github import gh_token
from conda_smithy.github import gh_token

anaconda_token = _get_anaconda_token()

Expand Down Expand Up @@ -89,9 +89,9 @@ def rotate_anaconda_token(
raise e
else:
err_msg = (
"Failed to rotate token for %s/%s"
f"Failed to rotate token for {user}/{project}"
" on circle!"
) % (user, project)
)
failed = True
raise RuntimeError(err_msg)

Expand All @@ -110,9 +110,9 @@ def rotate_anaconda_token(
raise e
else:
err_msg = (
"Failed to rotate token for %s/%s"
" on drone endpoint %s!"
) % (user, project, drone_endpoint)
f"Failed to rotate token for {user}/{project}"
f" on drone endpoint {drone_endpoint}!"
)
failed = True
raise RuntimeError(err_msg)

Expand All @@ -130,9 +130,9 @@ def rotate_anaconda_token(
raise e
else:
err_msg = (
"Failed to rotate token for %s/%s"
f"Failed to rotate token for {user}/{project}"
" on travis!"
) % (user, project)
)
failed = True
raise RuntimeError(err_msg)

Expand All @@ -146,8 +146,9 @@ def rotate_anaconda_token(
raise e
else:
err_msg = (
"Failed to rotate token for %s/%s" " on azure!"
) % (user, project)
f"Failed to rotate token for {user}/{project}"
" on azure!"
)
failed = True
raise RuntimeError(err_msg)

Expand All @@ -161,9 +162,9 @@ def rotate_anaconda_token(
raise e
else:
err_msg = (
"Failed to rotate token for %s/%s"
f"Failed to rotate token for {user}/{project}"
" on appveyor!"
) % (user, project)
)
failed = True
raise RuntimeError(err_msg)

Expand All @@ -177,9 +178,9 @@ def rotate_anaconda_token(
raise e
else:
err_msg = (
"Failed to rotate token for %s/%s"
f"Failed to rotate token for {user}/{project}"
" on github actions!"
) % (user, project)
)
failed = True
raise RuntimeError(err_msg)

Expand All @@ -193,17 +194,14 @@ def rotate_anaconda_token(
raise RuntimeError(err_msg)
else:
raise RuntimeError(
(
"Rotating the feedstock token in providers for %s/%s failed!"
" Try the command locally with DEBUG_ANACONDA_TOKENS"
" defined in the environment to investigate!"
)
% (user, project)
f"Rotating the feedstock token in providers for {user}/{project} failed!"
" Try the command locally with DEBUG_ANACONDA_TOKENS"
" defined in the environment to investigate!"
)


def rotate_token_in_circle(user, project, binstar_token, token_name):
from .ci_register import circle_token
from conda_smithy.ci_register import circle_token

url_template = (
"https://circleci.com/api/v1.1/project/github/{user}/{project}/envvar{extra}?"
Expand All @@ -229,7 +227,7 @@ def rotate_token_in_circle(user, project, binstar_token, token_name):
token=circle_token,
user=user,
project=project,
extra="/%s" % token_name,
extra=f"/{token_name}",
)
)
if r.status_code != 200:
Expand All @@ -249,7 +247,7 @@ def rotate_token_in_circle(user, project, binstar_token, token_name):
def rotate_token_in_drone(
user, project, binstar_token, token_name, drone_endpoint
):
from .ci_register import drone_session
from conda_smithy.ci_register import drone_session

session = drone_session(drone_endpoint)

Expand Down Expand Up @@ -283,10 +281,10 @@ def rotate_token_in_travis(
user, project, feedstock_config_path, binstar_token, token_name
):
"""update the binstar token in travis."""
from .ci_register import (
from conda_smithy.ci_register import (
travis_endpoint,
travis_headers,
travis_get_repo_info,
travis_headers,
)

headers = travis_headers()
Expand All @@ -295,7 +293,7 @@ def rotate_token_in_travis(
repo_id = repo_info["id"]

r = requests.get(
"{}/repo/{repo_id}/env_vars".format(travis_endpoint, repo_id=repo_id),
f"{travis_endpoint}/repo/{repo_id}/env_vars",
headers=headers,
)
if r.status_code != 200:
Expand All @@ -316,20 +314,14 @@ def rotate_token_in_travis(

if have_binstar_token:
r = requests.patch(
"{}/repo/{repo_id}/env_var/{ev_id}".format(
travis_endpoint,
repo_id=repo_id,
ev_id=ev_id,
),
f"{travis_endpoint}/repo/{repo_id}/env_var/{ev_id}",
headers=headers,
json=data,
)
r.raise_for_status()
else:
r = requests.post(
"{}/repo/{repo_id}/env_vars".format(
travis_endpoint, repo_id=repo_id
),
f"{travis_endpoint}/repo/{repo_id}/env_vars",
headers=headers,
json=data,
)
Expand Down Expand Up @@ -361,10 +353,14 @@ def rotate_token_in_travis(


def rotate_token_in_azure(user, project, binstar_token, token_name):
from .azure_ci_utils import build_client, get_default_build_definition
from .azure_ci_utils import default_config as config
from vsts.build.v4_1.models import BuildDefinitionVariable

from conda_smithy.azure_ci_utils import (
build_client,
get_default_build_definition,
)
from conda_smithy.azure_ci_utils import default_config as config

bclient = build_client()

existing_definitions = bclient.get_definitions(
Expand All @@ -375,8 +371,7 @@ def rotate_token_in_azure(user, project, binstar_token, token_name):
ed = existing_definitions[0]
else:
raise RuntimeError(
"Cannot add %s to a repo that is not already registerd on azure CI!"
% token_name
f"Cannot add {token_name} to a repo that is not already registerd on azure CI!"
)

ed = bclient.get_definition(ed.id, project=config.project_name)
Expand Down Expand Up @@ -409,9 +404,9 @@ def rotate_token_in_azure(user, project, binstar_token, token_name):


def rotate_token_in_appveyor(feedstock_config_path, binstar_token, token_name):
from .ci_register import appveyor_token
from conda_smithy.ci_register import appveyor_token

headers = {"Authorization": "Bearer {}".format(appveyor_token)}
headers = {"Authorization": f"Bearer {appveyor_token}"}
url = "https://ci.appveyor.com/api/account/encrypt"
response = requests.post(
url, headers=headers, data={"plainValue": binstar_token}
Expand Down
15 changes: 9 additions & 6 deletions conda_smithy/azure_ci_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
from vsts.task_agent.v4_0.models import TaskAgentQueue
from vsts.task_agent.v4_0.task_agent_client import TaskAgentClient
from vsts.vss_connection import VssConnection
from .azure_defaults import AZURE_DEFAULT_ORG, AZURE_DEFAULT_PROJECT_NAME

from conda_smithy.azure_defaults import (
AZURE_DEFAULT_ORG,
AZURE_DEFAULT_PROJECT_NAME,
)


class AzureConfig:
Expand All @@ -40,13 +44,11 @@ def __init__(
)

try:
with open(
os.path.expanduser("~/.conda-smithy/azure.token"), "r"
) as fh:
with open(os.path.expanduser("~/.conda-smithy/azure.token")) as fh:
self.token = fh.read().strip()
if not self.token:
raise ValueError()
except (IOError, ValueError):
except (OSError, ValueError):
print(
"No azure token. Create a token and\n"
"put it in ~/.conda-smithy/azure.token"
Expand Down Expand Up @@ -133,12 +135,13 @@ def get_default_build_definition(
config: AzureConfig = default_config,
**kwargs,
):
import inspect

from vsts.build.v4_1.models import (
BuildDefinition,
BuildRepository,
)
from vsts.task_agent.v4_0.task_agent_client import TaskAgentClient
import inspect

aclient = TaskAgentClient(config.instance_base_url, config.credentials)

Expand Down
Loading

0 comments on commit 0e2a3b6

Please sign in to comment.