|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | +# Copyright © 2026 The TokTok team |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tempfile |
| 10 | + |
| 11 | +import common |
| 12 | +from lib import git, github |
| 13 | + |
| 14 | +# Add tools directory to path to allow importing lib |
| 15 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 16 | + |
| 17 | + |
| 18 | +def parse_args(): |
| 19 | + parser = argparse.ArgumentParser(description="Verify AppImage reproducibility.") |
| 20 | + parser.add_argument("--tag", help="Tag to verify", default=git.current_tag()) |
| 21 | + parser.add_argument( |
| 22 | + "--repo", help="Repository name", default=common.get_default_repo() |
| 23 | + ) |
| 24 | + parser.add_argument( |
| 25 | + "--project-name", help="Project name (defaults to repo name)", default=None |
| 26 | + ) |
| 27 | + return parser.parse_args() |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + args = parse_args() |
| 32 | + repo_name = args.repo |
| 33 | + tag = args.tag |
| 34 | + |
| 35 | + gh = github.GitHub() |
| 36 | + assets = gh.release_assets(tag) |
| 37 | + # Search for AppImage assets (can be CiTools-... or citools-...) |
| 38 | + appimage_asset = next( |
| 39 | + ( |
| 40 | + a |
| 41 | + for a in assets |
| 42 | + if a.name.lower().endswith(".appimage") and "x86_64" in a.name.lower() |
| 43 | + ), |
| 44 | + None, |
| 45 | + ) |
| 46 | + |
| 47 | + if not appimage_asset: |
| 48 | + print(f"No x86_64 AppImage found for tag {tag} in {repo_name}", file=sys.stderr) |
| 49 | + return 1 |
| 50 | + |
| 51 | + with common.Workspace(repo_name, tag) as workspace: |
| 52 | + # Detect project name from the workspace (the cloned repo) |
| 53 | + project_name = ( |
| 54 | + args.project_name or common.detect_project_name(workspace.root) or repo_name |
| 55 | + ) |
| 56 | + print(f"Detected project name: {project_name}", file=sys.stderr) |
| 57 | + |
| 58 | + released_path = os.path.join(workspace.root, "released.AppImage") |
| 59 | + print(f"Downloading {appimage_asset.name}...", file=sys.stderr) |
| 60 | + with open(released_path, "wb") as f: |
| 61 | + f.write(gh.download_asset(appimage_asset.id)) |
| 62 | + |
| 63 | + # 2. Build local |
| 64 | + # Get the commit timestamp for SOURCE_DATE_EPOCH |
| 65 | + commit_date = subprocess.check_output( |
| 66 | + ["git", "-C", workspace.root, "log", "-1", "--format=%ct"], text=True |
| 67 | + ).strip() |
| 68 | + |
| 69 | + print( |
| 70 | + f"Building local AppImage via Docker (SOURCE_DATE_EPOCH={commit_date})...", |
| 71 | + file=sys.stderr, |
| 72 | + ) |
| 73 | + workspace.run_docker( |
| 74 | + "alpine-appimage", |
| 75 | + [ |
| 76 | + "third_party/ci-tools/platform/appimage/build.sh", |
| 77 | + "--arch", |
| 78 | + "x86_64", |
| 79 | + "--src-dir", |
| 80 | + "/qtox", |
| 81 | + "--project-name", |
| 82 | + project_name, |
| 83 | + "--", |
| 84 | + ], |
| 85 | + env={ |
| 86 | + "GITHUB_REPOSITORY": f"TokTok/{repo_name}", |
| 87 | + "GITHUB_REF": f"refs/tags/{tag}", |
| 88 | + "SOURCE_DATE_EPOCH": commit_date, |
| 89 | + }, |
| 90 | + ) |
| 91 | + print(f"Build completed successfully.", file=sys.stderr) |
| 92 | + |
| 93 | + # Find the produced AppImage in the workspace root |
| 94 | + # Filename format: $PROJECT_NAME-$(git rev-parse --short HEAD | head -c7)-$ARCH.AppImage |
| 95 | + sha = subprocess.check_output( |
| 96 | + ["git", "-C", workspace.root, "rev-parse", "--short", "HEAD"], text=True |
| 97 | + ).strip()[:7] |
| 98 | + local_name = f"{project_name}-{sha}-x86_64.AppImage" |
| 99 | + local_path = os.path.join(workspace.root, local_name) |
| 100 | + |
| 101 | + if not os.path.exists(local_path): |
| 102 | + print(f"Local build failed to produce {local_path}", file=sys.stderr) |
| 103 | + return 1 |
| 104 | + |
| 105 | + # 3. Compare |
| 106 | + print(f"Comparing released and local AppImages...", file=sys.stderr) |
| 107 | + |
| 108 | + def get_sha256(path): |
| 109 | + return subprocess.check_output(["sha256sum", path], text=True).split()[0] |
| 110 | + |
| 111 | + released_sha = get_sha256(released_path) |
| 112 | + local_sha = get_sha256(local_path) |
| 113 | + |
| 114 | + print(f"Released SHA256: {released_sha}") |
| 115 | + print(f"Local SHA256: {local_sha}") |
| 116 | + |
| 117 | + if released_sha != local_sha: |
| 118 | + print("AppImages differ!", file=sys.stderr) |
| 119 | + |
| 120 | + # Deep investigation |
| 121 | + print("\n--- Deep Investigation ---", file=sys.stderr) |
| 122 | + released_dir = os.path.join(workspace.root, "released_ext") |
| 123 | + local_dir = os.path.join(workspace.root, "local_ext") |
| 124 | + |
| 125 | + def extract_appimage(path, out): |
| 126 | + """Extract SquashFS content from AppImage.""" |
| 127 | + # AppImages extract themselves when called with --appimage-extract |
| 128 | + # We make sure it's executable first. |
| 129 | + os.chmod(path, 0o755) |
| 130 | + try: |
| 131 | + # In some environments (like Docker/restricted shells), |
| 132 | + # we might need --appimage-extract-and-run or specific env vars. |
| 133 | + # But usually this is the most direct way. |
| 134 | + subprocess.run( |
| 135 | + [path, "--appimage-extract"], |
| 136 | + cwd=workspace.root, |
| 137 | + check=True, |
| 138 | + capture_output=True, |
| 139 | + ) |
| 140 | + target_squash = os.path.join(workspace.root, "squashfs-root") |
| 141 | + if os.path.exists(target_squash): |
| 142 | + shutil.move(target_squash, out) |
| 143 | + else: |
| 144 | + print( |
| 145 | + f"Extraction of {path} failed to produce squashfs-root", |
| 146 | + file=sys.stderr, |
| 147 | + ) |
| 148 | + except Exception as e: |
| 149 | + print(f"Extraction of {path} failed: {e}", file=sys.stderr) |
| 150 | + # Fallback to unsquashfs if the binary can't run |
| 151 | + subprocess.run( |
| 152 | + ["unsquashfs", "-d", out, "-f", path], |
| 153 | + check=True, |
| 154 | + capture_output=True, |
| 155 | + ) |
| 156 | + |
| 157 | + try: |
| 158 | + print("Extracting released AppImage...", file=sys.stderr) |
| 159 | + extract_appimage(released_path, released_dir) |
| 160 | + print("Extracting local AppImage...", file=sys.stderr) |
| 161 | + extract_appimage(local_path, local_dir) |
| 162 | + |
| 163 | + print("Comparing extracted filesystem contents...", file=sys.stderr) |
| 164 | + # Compare file structures and content hashes |
| 165 | + diff_proc = subprocess.run( |
| 166 | + ["diff", "-rq", released_dir, local_dir], |
| 167 | + capture_output=True, |
| 168 | + text=True, |
| 169 | + ) |
| 170 | + |
| 171 | + if diff_proc.returncode == 0: |
| 172 | + print( |
| 173 | + "SUCCESS: Filesystem contents are bit-for-bit identical!", |
| 174 | + file=sys.stderr, |
| 175 | + ) |
| 176 | + print( |
| 177 | + "The difference lies solely in the AppImage packaging (SquashFS metadata or Runtime).", |
| 178 | + file=sys.stderr, |
| 179 | + ) |
| 180 | + else: |
| 181 | + print("DIFFERENCES FOUND in filesystem contents:", file=sys.stderr) |
| 182 | + print(diff_proc.stdout) |
| 183 | + |
| 184 | + # Run diffoscope on the directories for a detailed breakdown |
| 185 | + if shutil.which("diffoscope"): |
| 186 | + print( |
| 187 | + "\nRunning diffoscope on extracted directories...", |
| 188 | + file=sys.stderr, |
| 189 | + ) |
| 190 | + # Limit output to avoid huge logs |
| 191 | + subprocess.run( |
| 192 | + ["diffoscope", "--text", "-", released_dir, local_dir] |
| 193 | + ) |
| 194 | + except Exception as e: |
| 195 | + print(f"Deep investigation failed: {e}", file=sys.stderr) |
| 196 | + |
| 197 | + # Always run diffoscope on the full AppImage as a final reference |
| 198 | + if shutil.which("diffoscope"): |
| 199 | + print("\n--- Full AppImage Diff (Reference) ---", file=sys.stderr) |
| 200 | + subprocess.run(["diffoscope", released_path, local_path]) |
| 201 | + |
| 202 | + return 1 |
| 203 | + else: |
| 204 | + print("AppImages are identical!", file=sys.stderr) |
| 205 | + return 0 |
| 206 | + |
| 207 | + |
| 208 | +if __name__ == "__main__": |
| 209 | + sys.exit(main()) |
0 commit comments