From 2f966f4a3a8e19f2ffb2c660ffb12867a6848dd8 Mon Sep 17 00:00:00 2001 From: ActivePeter <1020401660@qq.com> Date: Sun, 13 Oct 2024 23:13:54 +0800 Subject: [PATCH] test --- .github/workflows/build.yml | 117 ++++++++++++++++++++++ .gitignore | 1 + build_eseential.yml | 2 + requirements.txt | 1 + setup.py | 189 ++++++++++++++++++++++++++++++++++++ 5 files changed, 310 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 build_eseential.yml create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..7859b37 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,117 @@ +name: Build for x86 and ARM + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + os: ["ubuntu-18.04"] + arch: + - x86_64 + # - arm64 + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up QEMU + if: matrix.arch == 'arm64' + uses: docker/setup-qemu-action@v1 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.8" + + - name: Configure environment for ARM + if: matrix.arch == 'arm64' + run: | + sudo dpkg --add-architecture arm64 + sudo apt-get update + sudo apt-get install -y python3.8:arm64 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Build project + run: | + # 这里是你的构建命令 + python setup.py + + - name: List release folders + id: list_folders + run: | + python3 -c ' + import yaml, os, json + folders = [f"releases/{d[:-3]}" for d in os.listdir("./") if d.endswith(".yml")] + with open("folders.json", "w") as fh: + json.dump(folders, fh) + ' + + - name: Upload folders artifact + uses: actions/upload-artifact@v3 + with: + name: release-folders + path: folders.json + + - name: Cache build artifacts + uses: actions/cache@v2 + with: + path: releases/ + key: ${{ runner.os }}-build-${{ matrix.arch }} + + create_releases: + needs: build + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Download folders artifact + uses: actions/download-artifact@v3 + with: + name: release-folders + + - name: Read folders + id: read_folders + run: | + folders=$(cat folders.json) + echo "folders=$folders" >> $GITHUB_OUTPUT + + - name: Restore build artifacts + uses: actions/cache@v2 + with: + path: releases/ + key: ${{ runner.os }}-build-${{ matrix.arch }} + + - name: Create releases using Python + run: | + python3 < ..." + exit 1 +fi + +# 更新软件包索引 +apt-get update + +# 调用 Python 脚本 +python3 /app/mount_scripts/download_debs.py "$@" +""" + with open(os.path.join(temp_dir, 'run.sh'), 'w') as f: + f.write(run_script_content) + os.chmod(os.path.join(temp_dir, 'run.sh'), 0o755) + +def create_container_script(temp_dir): + container_script_content = """#!/usr/bin/env python3 + +import subprocess +import os +import sys + +def get_dependencies(package_name): + result = subprocess.run( + ['apt-cache', 'depends', '--recurse', '--no-recommends', '--no-suggests', '--no-conflicts', '--no-breaks', '--no-replaces', '--no-enhances', package_name], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True + ) + if result.returncode != 0: + print(f"Error getting dependencies for {package_name}: {result.stderr}") + return [] + dependencies = result.stdout.splitlines() + print(f"Get dependencies for {package_name}, output: {dependencies}") + dependencies = [dep.strip() for dep in dependencies if dep.strip().startswith('Depends:')] + print(f"dependencies1: {dependencies}") + dependencies = [dep.split()[1] for dep in dependencies] + print(f"dependencies2: {dependencies}") + # remove repeated packages + dependencies = list(set(dependencies)) + dependencies = [dep for dep in dependencies if not dep.strip().startswith('<')] + return dependencies + +def download_packages(packages, output_dir): + os.system("ls") + os.chdir(output_dir) + for package in packages: + subprocess.run(['apt', 'download', package], check=True) + +def generate_install_script(packages, output_dir): + with open(os.path.join(output_dir, 'install.sh'), 'w') as f: + f.write('#!/bin/bash\\n') + for package in packages: + f.write(f'dpkg -i {package}.deb\\n') + os.chmod(os.path.join(output_dir, 'install.sh'), 0o755) + +def main(): + print("Enter Container Script") + if len(sys.argv) < 3: + print("Usage: python3 download_debs.py ...") + sys.exit(1) + + import os, pwd + + os.setuid(0) + os.setgid(0) + + output_dir = "/app/output" + os.chdir(output_dir) + + packages = sys.argv[2:] + print("Packages to download:", packages) + + # if not os.path.exists(output_dir): + try: + print(f"Output directory does not exist, creating... {output_dir}") + os.makedirs(output_dir) + except: + pass + + all_packages = set() + for package in packages: + dependencies = get_dependencies(package) + print(f"Getting dependencies for {package} with {dependencies}") + all_packages.update(dependencies) + all_packages.add(package) + + all_packages = sorted(all_packages) + download_packages(all_packages, output_dir) + generate_install_script(all_packages, output_dir) + +if __name__ == "__main__": + main() +""" + with open(os.path.join(temp_dir, 'download_debs.py'), 'w') as f: + f.write(container_script_content) + +def build_and_run_docker(output_dir, packages): + with tempfile.TemporaryDirectory() as temp_dir: + # 创建 Dockerfile + create_dockerfile(temp_dir) + + # 创建 run.sh + create_run_script(temp_dir) + + # 创建容器中的 Python 脚本 + create_container_script(temp_dir) + + # 构建 Docker 镜像 + image_tag = 'download-debs:18.04' + subprocess.run(['docker', 'build', '-t', image_tag, temp_dir], check=True) + + # 运行 Docker 容器 + print(f"bind output dir: {os.path.abspath(output_dir)} to /app/output") + command = ['docker', 'run', + '-v', f'{os.path.abspath(output_dir)}:/app/output', + '-v', f'{temp_dir}:/app/mount_scripts', + '--entrypoint=/app/mount_scripts/run.sh', + image_tag, '/app/output'] + packages + subprocess.run(command, check=True) + +def deb_one_pack(packname,sub_packs): + # if len(sys.argv) < 3: + # print("Usage: python3 download_debs.py ...") + # sys.exit(1) + + output_dir = "releases/"+packname + # packages = sys.argv[2:] + + if not os.path.exists(output_dir): + os.makedirs(output_dir) + os.system(f"chmod 777 {output_dir}") + + build_and_run_docker(output_dir, sub_packs) + +def main(): + os.system("docker login") + ymls=[f for f in os.listdir("./") if f.endswith(".yml")] + for yml in ymls: + with open(yml) as f: + conf=yaml.safe_load(f) + for key in conf: + if key=='apt': + deb_one_pack(yml.split(".yml")[0],conf['apt']) + else: + print("unsupported conf key: {key}") + + +if __name__ == "__main__": + main() \ No newline at end of file