about summary refs log tree commit diff
path: root/src/ci
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-12-05 23:47:11 +0100
committerGitHub <noreply@github.com>2024-12-05 23:47:11 +0100
commit5a9c9ef54115a27a3eac4e39feb133e33870b145 (patch)
tree5856ef55af3f29e8f02fd6da38f4dadcf7736123 /src/ci
parente941e73368866d3ecc98f040d9f1376de7efcdf1 (diff)
parent536516f949ff37b0e10eaed835c2d3592d03e576 (diff)
downloadrust-5a9c9ef54115a27a3eac4e39feb133e33870b145.tar.gz
rust-5a9c9ef54115a27a3eac4e39feb133e33870b145.zip
Rollup merge of #133821 - Kobzol:replace-black-with-ruff, r=onur-ozkan
Replace black with ruff in `tidy`

`ruff` can both lint and format Python code (in fact, it should be a mostly drop-in replacement for `black` in terms of formatting), so it's not needed to use `black` anymore. This PR removes `black` and replaces it with `ruff`, to get rid of one Python dependency, and also to make Python formatting faster (although that's a small thing).

If we decide to merge this, we'll need to "reformat the world" - `ruff` is not perfectly compatible with `black`, and it also looks like `black` was actually ignoring some files before. I tried it locally (`./x test tidy --extra-checks=py:fmt --bless`) and it also reformatted some code in subtrees (e.g. `clippy` or `rustc_codegen_gcc`) - I'm not sure how to handle that.
Diffstat (limited to 'src/ci')
-rwxr-xr-xsrc/ci/cpu-usage-over-time.py36
-rwxr-xr-xsrc/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py149
-rwxr-xr-xsrc/ci/docker/scripts/android-sdk-manager.py60
-rwxr-xr-xsrc/ci/docker/scripts/fuchsia-test-runner.py8
-rwxr-xr-xsrc/ci/github-actions/calculate-job-matrix.py13
-rw-r--r--src/ci/scripts/upload-build-metrics.py21
6 files changed, 169 insertions, 118 deletions
diff --git a/src/ci/cpu-usage-over-time.py b/src/ci/cpu-usage-over-time.py
index adfd895ead0..3d9dc86734f 100755
--- a/src/ci/cpu-usage-over-time.py
+++ b/src/ci/cpu-usage-over-time.py
@@ -40,12 +40,13 @@ import time
 # Python 3.3 changed the value of `sys.platform` on Linux from "linux2" to just
 # "linux". We check here with `.startswith` to keep compatibility with older
 # Python versions (especially Python 2.7).
-if sys.platform.startswith('linux'):
+if sys.platform.startswith("linux"):
+
     class State:
         def __init__(self):
-            with open('/proc/stat', 'r') as file:
+            with open("/proc/stat", "r") as file:
                 data = file.readline().split()
-            if data[0] != 'cpu':
+            if data[0] != "cpu":
                 raise Exception('did not start with "cpu"')
             self.user = int(data[1])
             self.nice = int(data[2])
@@ -69,10 +70,21 @@ if sys.platform.startswith('linux'):
             steal = self.steal - prev.steal
             guest = self.guest - prev.guest
             guest_nice = self.guest_nice - prev.guest_nice
-            total = user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice
+            total = (
+                user
+                + nice
+                + system
+                + idle
+                + iowait
+                + irq
+                + softirq
+                + steal
+                + guest
+                + guest_nice
+            )
             return float(idle) / float(total) * 100
 
-elif sys.platform == 'win32':
+elif sys.platform == "win32":
     from ctypes.wintypes import DWORD
     from ctypes import Structure, windll, WinError, GetLastError, byref
 
@@ -104,9 +116,10 @@ elif sys.platform == 'win32':
             kernel = self.kernel - prev.kernel
             return float(idle) / float(user + kernel) * 100
 
-elif sys.platform == 'darwin':
+elif sys.platform == "darwin":
     from ctypes import *
-    libc = cdll.LoadLibrary('/usr/lib/libc.dylib')
+
+    libc = cdll.LoadLibrary("/usr/lib/libc.dylib")
 
     class host_cpu_load_info_data_t(Structure):
         _fields_ = [("cpu_ticks", c_uint * 4)]
@@ -116,7 +129,7 @@ elif sys.platform == 'darwin':
         c_uint,
         c_int,
         POINTER(host_cpu_load_info_data_t),
-        POINTER(c_int)
+        POINTER(c_int),
     ]
     host_statistics.restype = c_int
 
@@ -124,13 +137,14 @@ elif sys.platform == 'darwin':
     CPU_STATE_SYSTEM = 1
     CPU_STATE_IDLE = 2
     CPU_STATE_NICE = 3
+
     class State:
         def __init__(self):
             stats = host_cpu_load_info_data_t()
-            count = c_int(4) # HOST_CPU_LOAD_INFO_COUNT
+            count = c_int(4)  # HOST_CPU_LOAD_INFO_COUNT
             err = libc.host_statistics(
                 libc.mach_host_self(),
-                c_int(3), # HOST_CPU_LOAD_INFO
+                c_int(3),  # HOST_CPU_LOAD_INFO
                 byref(stats),
                 byref(count),
             )
@@ -148,7 +162,7 @@ elif sys.platform == 'darwin':
             return float(idle) / float(user + system + idle + nice) * 100.0
 
 else:
-    print('unknown platform', sys.platform)
+    print("unknown platform", sys.platform)
     sys.exit(1)
 
 cur_state = State()
diff --git a/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py b/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py
index 3577643ca55..4f877389fbc 100755
--- a/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py
+++ b/src/ci/docker/host-x86_64/test-various/uefi_qemu_test/run.py
@@ -8,78 +8,79 @@ import tempfile
 
 from pathlib import Path
 
-TARGET_AARCH64 = 'aarch64-unknown-uefi'
-TARGET_I686 = 'i686-unknown-uefi'
-TARGET_X86_64 = 'x86_64-unknown-uefi'
+TARGET_AARCH64 = "aarch64-unknown-uefi"
+TARGET_I686 = "i686-unknown-uefi"
+TARGET_X86_64 = "x86_64-unknown-uefi"
+
 
 def run(*cmd, capture=False, check=True, env=None, timeout=None):
     """Print and run a command, optionally capturing the output."""
     cmd = [str(p) for p in cmd]
-    print(' '.join(cmd))
-    return subprocess.run(cmd,
-                          capture_output=capture,
-                          check=check,
-                          env=env,
-                          text=True,
-                          timeout=timeout)
+    print(" ".join(cmd))
+    return subprocess.run(
+        cmd, capture_output=capture, check=check, env=env, text=True, timeout=timeout
+    )
+
 
 def build_and_run(tmp_dir, target):
     if target == TARGET_AARCH64:
-        boot_file_name = 'bootaa64.efi'
-        ovmf_dir = Path('/usr/share/AAVMF')
-        ovmf_code = 'AAVMF_CODE.fd'
-        ovmf_vars = 'AAVMF_VARS.fd'
-        qemu = 'qemu-system-aarch64'
-        machine = 'virt'
-        cpu = 'cortex-a72'
+        boot_file_name = "bootaa64.efi"
+        ovmf_dir = Path("/usr/share/AAVMF")
+        ovmf_code = "AAVMF_CODE.fd"
+        ovmf_vars = "AAVMF_VARS.fd"
+        qemu = "qemu-system-aarch64"
+        machine = "virt"
+        cpu = "cortex-a72"
     elif target == TARGET_I686:
-        boot_file_name = 'bootia32.efi'
-        ovmf_dir = Path('/usr/share/OVMF')
-        ovmf_code = 'OVMF32_CODE_4M.secboot.fd'
-        ovmf_vars = 'OVMF32_VARS_4M.fd'
+        boot_file_name = "bootia32.efi"
+        ovmf_dir = Path("/usr/share/OVMF")
+        ovmf_code = "OVMF32_CODE_4M.secboot.fd"
+        ovmf_vars = "OVMF32_VARS_4M.fd"
         # The i686 target intentionally uses 64-bit qemu; the important
         # difference is that the OVMF code provides a 32-bit environment.
-        qemu = 'qemu-system-x86_64'
-        machine = 'q35'
-        cpu = 'qemu64'
+        qemu = "qemu-system-x86_64"
+        machine = "q35"
+        cpu = "qemu64"
     elif target == TARGET_X86_64:
-        boot_file_name = 'bootx64.efi'
-        ovmf_dir = Path('/usr/share/OVMF')
-        ovmf_code = 'OVMF_CODE.fd'
-        ovmf_vars = 'OVMF_VARS.fd'
-        qemu = 'qemu-system-x86_64'
-        machine = 'q35'
-        cpu = 'qemu64'
+        boot_file_name = "bootx64.efi"
+        ovmf_dir = Path("/usr/share/OVMF")
+        ovmf_code = "OVMF_CODE.fd"
+        ovmf_vars = "OVMF_VARS.fd"
+        qemu = "qemu-system-x86_64"
+        machine = "q35"
+        cpu = "qemu64"
     else:
-        raise KeyError('invalid target')
+        raise KeyError("invalid target")
 
-    host_artifacts = Path('/checkout/obj/build/x86_64-unknown-linux-gnu')
-    stage0 = host_artifacts / 'stage0/bin'
-    stage2 = host_artifacts / 'stage2/bin'
+    host_artifacts = Path("/checkout/obj/build/x86_64-unknown-linux-gnu")
+    stage0 = host_artifacts / "stage0/bin"
+    stage2 = host_artifacts / "stage2/bin"
 
     env = dict(os.environ)
-    env['PATH'] = '{}:{}:{}'.format(stage2, stage0, env['PATH'])
+    env["PATH"] = "{}:{}:{}".format(stage2, stage0, env["PATH"])
 
     # Copy the test create into `tmp_dir`.
-    test_crate = Path(tmp_dir) / 'uefi_qemu_test'
-    shutil.copytree('/uefi_qemu_test', test_crate)
+    test_crate = Path(tmp_dir) / "uefi_qemu_test"
+    shutil.copytree("/uefi_qemu_test", test_crate)
 
     # Build the UEFI executable.
-    run('cargo',
-        'build',
-        '--manifest-path',
-        test_crate / 'Cargo.toml',
-        '--target',
+    run(
+        "cargo",
+        "build",
+        "--manifest-path",
+        test_crate / "Cargo.toml",
+        "--target",
         target,
-        env=env)
+        env=env,
+    )
 
     # Create a mock EFI System Partition in a subdirectory.
-    esp = test_crate / 'esp'
-    boot = esp / 'efi/boot'
+    esp = test_crate / "esp"
+    boot = esp / "efi/boot"
     os.makedirs(boot, exist_ok=True)
 
     # Copy the executable into the ESP.
-    src_exe_path = test_crate / 'target' / target / 'debug/uefi_qemu_test.efi'
+    src_exe_path = test_crate / "target" / target / "debug/uefi_qemu_test.efi"
     shutil.copy(src_exe_path, boot / boot_file_name)
     print(src_exe_path, boot / boot_file_name)
 
@@ -89,37 +90,39 @@ def build_and_run(tmp_dir, target):
 
     # Make a writable copy of the vars file. aarch64 doesn't boot
     # correctly with read-only vars.
-    ovmf_rw_vars = Path(tmp_dir) / 'vars.fd'
+    ovmf_rw_vars = Path(tmp_dir) / "vars.fd"
     shutil.copy(ovmf_vars, ovmf_rw_vars)
 
     # Run the executable in QEMU and capture the output.
-    output = run(qemu,
-                 '-machine',
-                 machine,
-                 '-cpu',
-                 cpu,
-                 '-display',
-                 'none',
-                 '-serial',
-                 'stdio',
-                 '-drive',
-                 f'if=pflash,format=raw,readonly=on,file={ovmf_code}',
-                 '-drive',
-                 f'if=pflash,format=raw,readonly=off,file={ovmf_rw_vars}',
-                 '-drive',
-                 f'format=raw,file=fat:rw:{esp}',
-                 capture=True,
-                 check=True,
-                 # Set a timeout to kill the VM in case something goes wrong.
-                 timeout=60).stdout
-
-    if 'Hello World!' in output:
-        print('VM produced expected output')
+    output = run(
+        qemu,
+        "-machine",
+        machine,
+        "-cpu",
+        cpu,
+        "-display",
+        "none",
+        "-serial",
+        "stdio",
+        "-drive",
+        f"if=pflash,format=raw,readonly=on,file={ovmf_code}",
+        "-drive",
+        f"if=pflash,format=raw,readonly=off,file={ovmf_rw_vars}",
+        "-drive",
+        f"format=raw,file=fat:rw:{esp}",
+        capture=True,
+        check=True,
+        # Set a timeout to kill the VM in case something goes wrong.
+        timeout=60,
+    ).stdout
+
+    if "Hello World!" in output:
+        print("VM produced expected output")
     else:
-        print('unexpected VM output:')
-        print('---start---')
+        print("unexpected VM output:")
+        print("---start---")
         print(output)
-        print('---end---')
+        print("---end---")
         sys.exit(1)
 
 
diff --git a/src/ci/docker/scripts/android-sdk-manager.py b/src/ci/docker/scripts/android-sdk-manager.py
index 66cba58427b..6356f15a886 100755
--- a/src/ci/docker/scripts/android-sdk-manager.py
+++ b/src/ci/docker/scripts/android-sdk-manager.py
@@ -35,6 +35,7 @@ MIRROR_BUCKET = "rust-lang-ci-mirrors"
 MIRROR_BUCKET_REGION = "us-west-1"
 MIRROR_BASE_DIR = "rustc/android/"
 
+
 class Package:
     def __init__(self, path, url, sha1, deps=None):
         if deps is None:
@@ -53,18 +54,25 @@ class Package:
             sha1 = hashlib.sha1(f.read()).hexdigest()
             if sha1 != self.sha1:
                 raise RuntimeError(
-                    "hash mismatch for package " + self.path + ": " +
-                    sha1 + " vs " + self.sha1 + " (known good)"
+                    "hash mismatch for package "
+                    + self.path
+                    + ": "
+                    + sha1
+                    + " vs "
+                    + self.sha1
+                    + " (known good)"
                 )
         return file
 
     def __repr__(self):
-        return "<Package "+self.path+" at "+self.url+" (sha1="+self.sha1+")"
+        return "<Package " + self.path + " at " + self.url + " (sha1=" + self.sha1 + ")"
+
 
 def fetch_url(url):
     page = urllib.request.urlopen(url)
     return page.read()
 
+
 def fetch_repository(base, repo_url):
     packages = {}
     root = ET.fromstring(fetch_url(base + repo_url))
@@ -92,12 +100,14 @@ def fetch_repository(base, repo_url):
 
     return packages
 
+
 def fetch_repositories():
     packages = {}
     for repo in REPOSITORIES:
         packages.update(fetch_repository(BASE_REPOSITORY, repo))
     return packages
 
+
 class Lockfile:
     def __init__(self, path):
         self.path = path
@@ -123,6 +133,7 @@ class Lockfile:
             for package in packages:
                 f.write(package.path + " " + package.url + " " + package.sha1 + "\n")
 
+
 def cli_add_to_lockfile(args):
     lockfile = Lockfile(args.lockfile)
     packages = fetch_repositories()
@@ -130,28 +141,49 @@ def cli_add_to_lockfile(args):
         lockfile.add(packages, package)
     lockfile.save()
 
+
 def cli_update_mirror(args):
     lockfile = Lockfile(args.lockfile)
     for package in lockfile.packages.values():
         path = package.download(BASE_REPOSITORY)
-        subprocess.run([
-            "aws", "s3", "mv", path,
-            "s3://" + MIRROR_BUCKET + "/" + MIRROR_BASE_DIR + package.url,
-            "--profile=" + args.awscli_profile,
-        ], check=True)
+        subprocess.run(
+            [
+                "aws",
+                "s3",
+                "mv",
+                path,
+                "s3://" + MIRROR_BUCKET + "/" + MIRROR_BASE_DIR + package.url,
+                "--profile=" + args.awscli_profile,
+            ],
+            check=True,
+        )
+
 
 def cli_install(args):
     lockfile = Lockfile(args.lockfile)
     for package in lockfile.packages.values():
         # Download the file from the mirror into a temp file
-        url = "https://" + MIRROR_BUCKET + ".s3-" + MIRROR_BUCKET_REGION + \
-              ".amazonaws.com/" + MIRROR_BASE_DIR
+        url = (
+            "https://"
+            + MIRROR_BUCKET
+            + ".s3-"
+            + MIRROR_BUCKET_REGION
+            + ".amazonaws.com/"
+            + MIRROR_BASE_DIR
+        )
         downloaded = package.download(url)
         # Extract the file in a temporary directory
         extract_dir = tempfile.mkdtemp()
-        subprocess.run([
-            "unzip", "-q", downloaded, "-d", extract_dir,
-        ], check=True)
+        subprocess.run(
+            [
+                "unzip",
+                "-q",
+                downloaded,
+                "-d",
+                extract_dir,
+            ],
+            check=True,
+        )
         # Figure out the prefix used in the zip
         subdirs = [d for d in os.listdir(extract_dir) if not d.startswith(".")]
         if len(subdirs) != 1:
@@ -162,6 +194,7 @@ def cli_install(args):
         os.rename(os.path.join(extract_dir, subdirs[0]), dest)
         os.unlink(downloaded)
 
+
 def cli():
     parser = argparse.ArgumentParser()
     subparsers = parser.add_subparsers()
@@ -187,5 +220,6 @@ def cli():
         exit(1)
     args.func(args)
 
+
 if __name__ == "__main__":
     cli()
diff --git a/src/ci/docker/scripts/fuchsia-test-runner.py b/src/ci/docker/scripts/fuchsia-test-runner.py
index 77e2741f9ea..4a30de5a263 100755
--- a/src/ci/docker/scripts/fuchsia-test-runner.py
+++ b/src/ci/docker/scripts/fuchsia-test-runner.py
@@ -588,7 +588,7 @@ class TestEnvironment:
                 "--repo-path",
                 self.repo_dir(),
                 "--repository",
-                self.TEST_REPO_NAME
+                self.TEST_REPO_NAME,
             ],
             env=ffx_env,
             stdout_handler=self.subprocess_logger.debug,
@@ -619,9 +619,7 @@ class TestEnvironment:
     # `facet` statement required for TCP testing via
     # protocol `fuchsia.posix.socket.Provider`. See
     # https://fuchsia.dev/fuchsia-src/development/testing/components/test_runner_framework?hl=en#legacy_non-hermetic_tests
-    CML_TEMPLATE: ClassVar[
-        str
-    ] = """
+    CML_TEMPLATE: ClassVar[str] = """
     {{
         program: {{
             runner: "elf_test_runner",
@@ -994,7 +992,7 @@ class TestEnvironment:
                 "repository",
                 "server",
                 "stop",
-                self.TEST_REPO_NAME
+                self.TEST_REPO_NAME,
             ],
             env=self.ffx_cmd_env(),
             stdout_handler=self.subprocess_logger.debug,
diff --git a/src/ci/github-actions/calculate-job-matrix.py b/src/ci/github-actions/calculate-job-matrix.py
index 7de6d5fcd5f..1f994f0ffd2 100755
--- a/src/ci/github-actions/calculate-job-matrix.py
+++ b/src/ci/github-actions/calculate-job-matrix.py
@@ -7,6 +7,7 @@ be executed on CI.
 It reads job definitions from `src/ci/github-actions/jobs.yml`
 and filters them based on the event that happened on CI.
 """
+
 import dataclasses
 import json
 import logging
@@ -94,7 +95,7 @@ def find_run_type(ctx: GitHubCtx) -> Optional[WorkflowRunType]:
         try_build = ctx.ref in (
             "refs/heads/try",
             "refs/heads/try-perf",
-            "refs/heads/automation/bors/try"
+            "refs/heads/automation/bors/try",
         )
 
         # Unrolled branch from a rollup for testing perf
@@ -135,11 +136,15 @@ def calculate_jobs(run_type: WorkflowRunType, job_data: Dict[str, Any]) -> List[
                     continue
                 jobs.append(job[0])
             if unknown_jobs:
-                raise Exception(f"Custom job(s) `{unknown_jobs}` not found in auto jobs")
+                raise Exception(
+                    f"Custom job(s) `{unknown_jobs}` not found in auto jobs"
+                )
 
         return add_base_env(name_jobs(jobs, "try"), job_data["envs"]["try"])
     elif isinstance(run_type, AutoRunType):
-        return add_base_env(name_jobs(job_data["auto"], "auto"), job_data["envs"]["auto"])
+        return add_base_env(
+            name_jobs(job_data["auto"], "auto"), job_data["envs"]["auto"]
+        )
 
     return []
 
@@ -161,7 +166,7 @@ def get_github_ctx() -> GitHubCtx:
         event_name=event_name,
         ref=os.environ["GITHUB_REF"],
         repository=os.environ["GITHUB_REPOSITORY"],
-        commit_message=commit_message
+        commit_message=commit_message,
     )
 
 
diff --git a/src/ci/scripts/upload-build-metrics.py b/src/ci/scripts/upload-build-metrics.py
index a95e0949d70..23061884a39 100644
--- a/src/ci/scripts/upload-build-metrics.py
+++ b/src/ci/scripts/upload-build-metrics.py
@@ -19,6 +19,7 @@ $ python3 upload-build-metrics.py <path-to-CPU-usage-CSV>
 
 `path-to-CPU-usage-CSV` is a path to a CSV generated by the `src/ci/cpu-usage-over-time.py` script.
 """
+
 import argparse
 import csv
 import os
@@ -31,7 +32,7 @@ from typing import List
 def load_cpu_usage(path: Path) -> List[float]:
     usage = []
     with open(path) as f:
-        reader = csv.reader(f, delimiter=',')
+        reader = csv.reader(f, delimiter=",")
         for row in reader:
             # The log might contain incomplete rows or some Python exception
             if len(row) == 2:
@@ -50,25 +51,21 @@ def upload_datadog_measure(name: str, value: float):
     print(f"Metric {name}: {value:.4f}")
 
     datadog_cmd = "datadog-ci"
-    if os.getenv("GITHUB_ACTIONS") is not None and sys.platform.lower().startswith("win"):
+    if os.getenv("GITHUB_ACTIONS") is not None and sys.platform.lower().startswith(
+        "win"
+    ):
         # Due to weird interaction of MSYS2 and Python, we need to use an absolute path,
         # and also specify the ".cmd" at the end. See https://github.com/rust-lang/rust/pull/125771.
         datadog_cmd = "C:\\npm\\prefix\\datadog-ci.cmd"
 
-    subprocess.run([
-        datadog_cmd,
-        "measure",
-        "--level", "job",
-        "--measures", f"{name}:{value}"
-    ],
-        check=False
+    subprocess.run(
+        [datadog_cmd, "measure", "--level", "job", "--measures", f"{name}:{value}"],
+        check=False,
     )
 
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser(
-        prog="DataDog metric uploader"
-    )
+    parser = argparse.ArgumentParser(prog="DataDog metric uploader")
     parser.add_argument("cpu-usage-history-csv")
     args = parser.parse_args()