Skip to content

Commit

Permalink
ci: pip pack
Browse files Browse the repository at this point in the history
  • Loading branch information
QiE2035 committed Apr 15, 2024
1 parent 8530540 commit 81971e3
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 1 deletion.
33 changes: 32 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,31 @@ jobs:
name: MAA-nupkgs
path: "tools/nupkgs/*.nupkg"

pip_pack:
needs: [meta, windows, ubuntu, macos]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
path: assets

- name: Pip Pack
working-directory: tools/pip_pack
run: |
tag=${{ needs.meta.outputs.tag }}
pip install --upgrade wheel build tomlkit
python pip_pack.py $tag
- uses: actions/upload-artifact@v3
if: always()
with:
name: MAA-pip-pkgs
path: "tools/pip_pack/wheel/*.whl"

release:
if: ${{ needs.meta.outputs.is_release == 'true' }}
needs: [meta, windows, ubuntu, macos, nuget_pack]
needs: [meta, windows, ubuntu, macos, nuget_pack, pip_pack]
runs-on: ubuntu-latest
env:
NUGET_PACKAGE_SOURCE: https://api.nuget.org/v3/index.json # https://apiint.nugettest.org/v3/index.json
Expand All @@ -325,6 +347,15 @@ jobs:
-SkipDuplicate
rm -r assets/MAA-nupkgs
- name: Publish Pip Packages
run: |
pip install --upgrade twine
python -m twine upload \
assets/MAA-pip-pkgs/*.whl \
--username __token__\
--password ${{ secrets.PYPI_TOKEN }}
rm -r assets/MAA-pip-pkgs
- run: |
cd assets
for f in *; do
Expand Down
199 changes: 199 additions & 0 deletions tools/pip_pack/pip_pack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import os
import shutil
import subprocess
import sys

import tomlkit

IGNORES = ["MaaHttp*", "MaaPiCli*"]


def set_toml_ver():
TOML_FILE = "pyproject.toml"

version = sys.argv[1]
if "ci" in version:
base_ver, ci_ver = version.split("ci")
version = base_ver + "dev" + ci_ver.split("-")[0]

print("Setting version to:", version)

with open(TOML_FILE, "r") as f:
py_project = tomlkit.load(f)
py_project["project"]["version"] = version

with open(TOML_FILE, "w") as f:
tomlkit.dump(py_project, f)


WHL_DIR = "wheel"
DIST_DIR = "dist"
SRC_DIR = "src"


def clean_cwd():
print("Cleaning working dir...")

for root, dirs, files in os.walk(os.getcwd()):
for dir_name in dirs:
if (
dir_name
in (
SRC_DIR,
DIST_DIR,
WHL_DIR,
"__pycache__",
".cache",
"build",
)
or ".egg-info" in dir_name
):

shutil.rmtree(os.path.join(root, dir_name))
print(" Removing", dir_name)
for file_name in files:
if file_name.endswith(
(".pyc", ".pyo", ".whl", ".md"),
):
os.remove(os.path.join(root, file_name))
print(" Removing", file_name)

print("done")


def gen_base_whl():
print("Generating base wheel...", end="")
subprocess.check_output(
[sys.executable, "-m", "build", "-w"],
)
print("done")


def unpack_base_whl() -> str:
"""Return: unpack path"""

print("Unpacking base wheel...", end="")

whl_name = os.listdir(DIST_DIR)[0]
subprocess.check_output(
[sys.executable, "-m", "wheel", "unpack", whl_name],
cwd=DIST_DIR,
)

for file in os.listdir(DIST_DIR):
unpack_path = os.path.join(DIST_DIR, file)
if os.path.isdir(unpack_path):
unpack_dir = file
break

for file in os.listdir(unpack_path):
if ".dist-info" in file:
info_dir = file
break

info_path = os.path.join(unpack_path, info_dir)
os.remove(os.path.join(info_path, "RECORD"))

print("done")
return unpack_dir, info_path


ASSETS_PATH = os.path.join("..", "..", "assets")


def pack(pack_dir: str, info_path: str):
WHL_TAGS = {
"win-x86_64": "win_amd64",
"win-aarch64": "win_arm64",
"macos-x86_64": "macosx_13_0_x86_64",
"macos-aarch64": "macosx_13_0_arm64",
"linux-x86_64": "manylinux2014_x86_64",
"linux-aarch64": "manylinux2014_aarch64",
}

os.mkdir(WHL_DIR)

wheel_path = os.path.join(info_path, "WHEEL")

def edit_whl_tag(whl_tag: str):
print(f" Editing wheel tag to: {whl_tag}...", end="")

lines = []
for line in open(wheel_path, "r"):
if line.startswith("Tag:"):
line = "Tag: " + "py3-none-" + whl_tag
lines.append(line.rstrip())
with open(wheel_path, "w") as f:
f.write("\n".join(lines))

print("done")

pack_bin_path = os.path.join(DIST_DIR, pack_dir, "maa", "bin")

def copy_bin(platform: str):
print(f" Copying bin for: {platform}...", end="")

bin_path = os.path.join(ASSETS_PATH, "MAA-" + platform, "bin")
shutil.copytree(
bin_path,
pack_bin_path,
ignore=shutil.ignore_patterns(*IGNORES),
)

print("done")

def clean_bin():
print(" Cleaning bin...", end="")
shutil.rmtree(pack_bin_path)
print("done")

def pack_whl(whl_tag: str):
print(f" Packing whl for: {whl_tag}...", end="")

pack_path = os.path.join("..", DIST_DIR, pack_dir)
subprocess.check_output(
[sys.executable, "-m", "wheel", "pack", pack_path],
cwd=WHL_DIR,
)

print("done")

for platform, whl_tag in WHL_TAGS.items():
print(f"Packing for: {platform}...")

edit_whl_tag(whl_tag)
copy_bin(platform)
pack_whl(whl_tag)
clean_bin()

print("done")


def copy_base():
print("Copying base...", end="")

asset_dir = os.listdir(ASSETS_PATH)[0]
asset_path = os.path.join(ASSETS_PATH, asset_dir)
src_path = os.path.join(asset_path, "binding", "Python")
shutil.copytree(src_path, SRC_DIR)

readme_path = os.path.join(asset_path, "README.md")
shutil.copy(readme_path, "README.md")

license_path = os.path.join(asset_path, "LICENSE.md")
shutil.copy(license_path, "LICENSE.md")

print("done")


def main():
clean_cwd()
set_toml_ver()
copy_base()
gen_base_whl()
paths = unpack_base_whl()
pack(*paths)


if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions tools/pip_pack/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "MaaFw"
version = "0"
authors = [{ name = "MaaXYZ" }]
description = "An automation black-box testing framework based on image recognition"
readme = "README.md"
urls = { Homepage = "https://github.com/MaaXYZ/MaaFramework" }
dependencies = ["numpy", "Pillow", "MaaAgentBinary"]

0 comments on commit 81971e3

Please sign in to comment.