Skip to content

Commit 5f6170b

Browse files
authored
♻️ refactor: hard written target_arch、target_os_info (#14)
1 parent 2317f62 commit 5f6170b

File tree

4 files changed

+56
-54
lines changed

4 files changed

+56
-54
lines changed

.github/workflows/lint-and-fmt.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
architecture: ${{ matrix.architecture }}
3131

3232
- name: Install dependencies
33+
env:
34+
CIBW_ARCHS: "x86_64"
35+
CIBW_PLATFORM: "manylinux_2_17"
3336
run: |
3437
uv sync --all-extras --dev
3538

.github/workflows/release.yaml

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
tags:
66
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10
7+
pull_request:
78
workflow_dispatch:
89

910
jobs:
@@ -12,12 +13,14 @@ jobs:
1213
strategy:
1314
matrix:
1415
platform:
15-
- {"runner": "ubuntu-latest", "os": "linux", "target": "x86_64"}
16-
- {"runner": "ubuntu-latest", "os": "linux", "target": "aarch64"}
17-
- {"runner": "windows-latest", "os": "win", "target": "x86_64"}
18-
- {"runner": "windows-latest", "os": "win", "target": "arm64"}
19-
- {"runner": "macos-latest", "os": "macos", "target": "x86_64"}
20-
- {"runner": "macos-latest", "os": "macos", "target": "arm64"}
16+
- {"runner": "ubuntu-latest", "platform_tag": "musllinux_1_2", "arch": "x86_64"}
17+
- {"runner": "ubuntu-latest", "platform_tag": "musllinux_1_2", "arch": "aarch64"}
18+
- {"runner": "ubuntu-latest", "platform_tag": "manylinux_2_17", "arch": "x86_64"}
19+
- {"runner": "ubuntu-latest", "platform_tag": "manylinux_2_17", "arch": "aarch64"}
20+
- {"runner": "windows-latest", "platform_tag": "win", "arch": "amd64"}
21+
- {"runner": "windows-latest", "platform_tag": "win", "arch": "arm64"}
22+
- {"runner": "macos-latest", "platform_tag": "macosx_10_9", "arch": "x86_64"}
23+
- {"runner": "macos-latest", "platform_tag": "macosx_11_0", "arch": "arm64"}
2124
steps:
2225
- name: Checkout
2326
uses: actions/checkout@v4
@@ -32,15 +35,15 @@ jobs:
3235

3336
- name: build release distributions
3437
env:
35-
CIBW_ARCHS: ${{ matrix.platform.target }}
36-
CIBW_PLATFORM: ${{ matrix.platform.os }}
38+
CIBW_ARCHS: ${{ matrix.platform.arch }}
39+
CIBW_PLATFORM: ${{ matrix.platform.platform_tag }}
3740
run: |
3841
uv build
3942
4043
- name: upload dists
4144
uses: actions/upload-artifact@v4
4245
with:
43-
name: ${{ matrix.platform.os }}-${{ matrix.platform.target }}
46+
name: ${{ matrix.platform.platform_tag }}-${{ matrix.platform.arch }}
4447
path: dist/
4548

4649
publish-pypi:
@@ -54,17 +57,19 @@ jobs:
5457
strategy:
5558
matrix:
5659
platform:
57-
- {"runner": "ubuntu-latest", "os": "linux", "target": "x86_64"}
58-
- {"runner": "ubuntu-latest", "os": "linux", "target": "aarch64"}
59-
- {"runner": "windows-latest", "os": "win", "target": "x86_64"}
60-
- {"runner": "windows-latest", "os": "win", "target": "arm64"}
61-
- {"runner": "macos-latest", "os": "macos", "target": "x86_64"}
62-
- {"runner": "macos-latest", "os": "macos", "target": "arm64"}
60+
- {"runner": "ubuntu-latest", "platform_tag": "musllinux_1_2", "arch": "x86_64"}
61+
- {"runner": "ubuntu-latest", "platform_tag": "musllinux_1_2", "arch": "aarch64"}
62+
- {"runner": "ubuntu-latest", "platform_tag": "manylinux_2_17", "arch": "x86_64"}
63+
- {"runner": "ubuntu-latest", "platform_tag": "manylinux_2_17", "arch": "aarch64"}
64+
- {"runner": "windows-latest", "platform_tag": "win", "arch": "amd64"}
65+
- {"runner": "windows-latest", "platform_tag": "win", "arch": "arm64"}
66+
- {"runner": "macos-latest", "platform_tag": "macosx_10_9", "arch": "x86_64"}
67+
- {"runner": "macos-latest", "platform_tag": "macosx_11_0", "arch": "arm64"}
6368
steps:
6469
- name: Retrieve release distributions
6570
uses: actions/download-artifact@v4
6671
with:
67-
name: ${{ matrix.platform.os }}-${{ matrix.platform.target }}
72+
name: ${{ matrix.platform.platform_tag }}-${{ matrix.platform.arch }}
6873
path: dist/
6974

7075
- name: Install uv
@@ -84,16 +89,18 @@ jobs:
8489
strategy:
8590
matrix:
8691
platform:
87-
- {"runner": "ubuntu-latest", "os": "linux", "target": "x86_64"}
88-
- {"runner": "ubuntu-latest", "os": "linux", "target": "aarch64"}
89-
- {"runner": "windows-latest", "os": "win", "target": "x86_64"}
90-
- {"runner": "windows-latest", "os": "win", "target": "arm64"}
91-
- {"runner": "macos-latest", "os": "macos", "target": "x86_64"}
92-
- {"runner": "macos-latest", "os": "macos", "target": "arm64"}
92+
- {"runner": "ubuntu-latest", "platform_tag": "musllinux_1_2", "arch": "x86_64"}
93+
- {"runner": "ubuntu-latest", "platform_tag": "musllinux_1_2", "arch": "aarch64"}
94+
- {"runner": "ubuntu-latest", "platform_tag": "manylinux_2_17", "arch": "x86_64"}
95+
- {"runner": "ubuntu-latest", "platform_tag": "manylinux_2_17", "arch": "aarch64"}
96+
- {"runner": "windows-latest", "platform_tag": "win", "arch": "amd64"}
97+
- {"runner": "windows-latest", "platform_tag": "win", "arch": "arm64"}
98+
- {"runner": "macos-latest", "platform_tag": "macosx_10_9", "arch": "x86_64"}
99+
- {"runner": "macos-latest", "platform_tag": "macosx_11_0", "arch": "arm64"}
93100
steps:
94101
- uses: actions/download-artifact@v4
95102
with:
96-
name: ${{ matrix.platform.os }}-${{ matrix.platform.target }}
103+
name: ${{ matrix.platform.platform_tag }}-${{ matrix.platform.arch }}
97104
path: dist/
98105
- name: Get tag name
99106
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

hatch_build.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from __future__ import annotations
22

33
import os
4-
import platform
54
import re
65
import shutil
7-
import sys
86
import tarfile
97
import tempfile
108
from pathlib import Path
@@ -13,6 +11,18 @@
1311

1412
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
1513

14+
# key 为 pypi 分发的系统和架构组合
15+
BUILD_TARGET = {
16+
("musllinux_1_2", "x86_64"): {"download_file": ("linux", "x86_64")},
17+
("musllinux_1_2", "aarch64"): {"download_file": ("linux", "arm64")},
18+
("manylinux_2_17", "x86_64"): {"download_file": ("linux", "x86_64")},
19+
("manylinux_2_17", "aarch64"): {"download_file": ("linux", "arm64")},
20+
("macosx_10_9", "x86_64"): {"download_file": ("darwin", "x86_64")},
21+
("macosx_11_0", "arm64"): {"download_file": ("darwin", "arm64")},
22+
("win", "amd64"): {"download_file": ("windows", "x86_64")},
23+
("win", "arm64"): {"download_file": ("windows", "arm64")},
24+
}
25+
1626

1727
class SpecialBuildHook(BuildHookInterface):
1828
BIN_NAME = "yamlfmt"
@@ -27,24 +37,14 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
2737
if self.target_name != "wheel":
2838
return
2939

30-
target_arch = os.environ.get("CIBW_ARCHS", platform.machine()).lower()
31-
target_os_info = os.environ.get("CIBW_PLATFORM", sys.platform).lower()
32-
33-
if target_arch not in ["x86_64", "arm64", "aarch64"]:
34-
raise NotImplementedError(f"no support arch: {target_arch}")
40+
target_arch = os.environ.get("CIBW_ARCHS", None)
41+
target_os_info = os.environ.get("CIBW_PLATFORM", None)
3542

36-
if not any(os_name in target_os_info for os_name in ["linux", "darwin", "macos", "win"]):
37-
raise NotImplementedError(f"no support os: {target_os_info}")
43+
assert target_arch is not None, f"CIBW_ARCHS not set see: {BUILD_TARGET}"
44+
assert target_os_info is not None, f"CIBW_PLATFORM not set see: {BUILD_TARGET}"
3845

39-
# 检查系统和架构的组合
40-
if target_os_info in ["win"] and target_arch == "x86_64":
41-
target_arch = "amd64"
42-
elif target_os_info in ["linux"] and target_arch == "arm64":
43-
target_arch = "aarch64"
44-
if target_os_info in ["darwin", "macos"]:
45-
target_os_info = f"macosx_{'10_9' if target_arch == 'x86_64' else '11_0'}"
46-
if target_arch == "aarch64":
47-
target_arch = "arm64"
46+
if (target_os_info, target_arch) not in BUILD_TARGET:
47+
raise ValueError(f"Unsupported target: {target_os_info}, {target_arch}")
4848

4949
# 构建完整的 Wheel 标签
5050
full_wheel_tag = f"py3-none-{target_os_info}_{target_arch}"
@@ -72,21 +72,13 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
7272

7373
def download_yamlfmt(self, target_os_info: str, target_arch: str) -> None:
7474
"""Download the yamlfmt binary for the specified OS and architecture."""
75-
target_os_info_to_go_os = {
76-
"macosx_10_9": "Darwin",
77-
"macosx_11_0": "Darwin",
78-
"win": "Windows",
79-
}
80-
target_arch_to_go_arch = {
81-
"amd64": "x86_64",
82-
"aarch64": "arm64",
83-
}
84-
file_path = self.temp_dir / f"{self.BIN_NAME}_{target_os_info}_{target_arch}.tar.gz"
75+
download_target = BUILD_TARGET[(target_os_info, target_arch)]["download_file"]
76+
file_path = self.temp_dir / f"{self.BIN_NAME}_{download_target[0]}_{download_target[1]}.tar.gz"
8577
request.urlretrieve(
8678
self.YAMLFMT_REPO.format(
8779
version=re.sub(r"[ab]\d+$", "", self.metadata.version), # 去掉版本号中的后缀, alpha/beta
88-
target_os_info=target_os_info_to_go_os.get(target_os_info, target_os_info),
89-
target_arch=target_arch_to_go_arch.get(target_arch, target_arch),
80+
target_os_info=download_target[0],
81+
target_arch=download_target[1],
9082
),
9183
file_path,
9284
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "google-yamlfmt"
3-
version = "0.16.0-alpha.0"
3+
version = "0.16.0-alpha.1"
44
description = "A tool for formatting YAML files, yamlfmt from Google: https://github.com/google/yamlfmt"
55
readme = "README.md"
66
license = { text = "MIT" }

0 commit comments

Comments
 (0)