|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""This script packages the dfetch build directory into OS-specific installers using fpm.""" |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from dfetch import __version__ |
| 9 | + |
| 10 | +# Configuration |
| 11 | +BUILD_DIR = Path("build", "dfetch.dist") |
| 12 | +OUTPUT_DIR = Path("build", "dfetch-package") |
| 13 | +PACKAGE_NAME = "dfetch" |
| 14 | +INSTALL_PREFIX = "/opt/dfetch" # Where the files will be installed on Linux/macOS |
| 15 | +MAINTAINER = "DFetch Team <dfetch@spoor.cc>" |
| 16 | +DESCRIPTION = ( |
| 17 | + "DFetch: A vendoring tool for fetching and managing external dependencies." |
| 18 | +) |
| 19 | +URL = "https://github.com/dfetch-org/dfetch" |
| 20 | +LICENSE = "MIT" |
| 21 | + |
| 22 | + |
| 23 | +def run_command(command): |
| 24 | + """Run a system command and handle errors.""" |
| 25 | + command[0] = shutil.which(command[0]) # On windows .bat files need full path |
| 26 | + |
| 27 | + if not command[0]: |
| 28 | + raise FileNotFoundError(f"Command not found: {command[0]}") |
| 29 | + |
| 30 | + print("Running:", " ".join(command)) |
| 31 | + subprocess.check_call(command) |
| 32 | + |
| 33 | + |
| 34 | +def package_linux() -> None: |
| 35 | + """Package the build directory into .deb and .rpm installers.""" |
| 36 | + for target in ("deb", "rpm"): |
| 37 | + output = f"{OUTPUT_DIR}/{PACKAGE_NAME}_{__version__}.{target}" |
| 38 | + cmd = [ |
| 39 | + "fpm", |
| 40 | + "-s", |
| 41 | + "dir", |
| 42 | + "-t", |
| 43 | + target, |
| 44 | + "-n", |
| 45 | + PACKAGE_NAME, |
| 46 | + "-v", |
| 47 | + __version__, |
| 48 | + "-C", |
| 49 | + str(BUILD_DIR), |
| 50 | + "--prefix", |
| 51 | + INSTALL_PREFIX, |
| 52 | + "--description", |
| 53 | + DESCRIPTION, |
| 54 | + "--maintainer", |
| 55 | + MAINTAINER, |
| 56 | + "--url", |
| 57 | + URL, |
| 58 | + "--license", |
| 59 | + LICENSE, |
| 60 | + "-p", |
| 61 | + output, |
| 62 | + ".", |
| 63 | + ] |
| 64 | + run_command(cmd) |
| 65 | + |
| 66 | + |
| 67 | +def package_macos() -> None: |
| 68 | + """Package the build directory into a .pkg installer for macOS.""" |
| 69 | + cmd = [ |
| 70 | + "fpm", |
| 71 | + "-s", |
| 72 | + "dir", |
| 73 | + "-t", |
| 74 | + "osxpkg", |
| 75 | + "-n", |
| 76 | + PACKAGE_NAME, |
| 77 | + "-v", |
| 78 | + __version__, |
| 79 | + "-C", |
| 80 | + str(BUILD_DIR), |
| 81 | + "--prefix", |
| 82 | + INSTALL_PREFIX, |
| 83 | + "--description", |
| 84 | + DESCRIPTION, |
| 85 | + "--maintainer", |
| 86 | + MAINTAINER, |
| 87 | + "--url", |
| 88 | + URL, |
| 89 | + "--license", |
| 90 | + LICENSE, |
| 91 | + "-p", |
| 92 | + f"{OUTPUT_DIR}/{PACKAGE_NAME}_{__version__}.pkg", |
| 93 | + ".", |
| 94 | + ] |
| 95 | + run_command(cmd) |
| 96 | + |
| 97 | + |
| 98 | +def package_windows() -> None: |
| 99 | + """Package the build directory into a .msi installer for Windows.""" |
| 100 | + cmd = [ |
| 101 | + "fpm", |
| 102 | + "-s", |
| 103 | + "dir", |
| 104 | + "-t", |
| 105 | + "msi", |
| 106 | + "-n", |
| 107 | + PACKAGE_NAME, |
| 108 | + "-v", |
| 109 | + __version__, |
| 110 | + "-C", |
| 111 | + str(BUILD_DIR), |
| 112 | + "--description", |
| 113 | + DESCRIPTION, |
| 114 | + "--maintainer", |
| 115 | + MAINTAINER, |
| 116 | + "--url", |
| 117 | + URL, |
| 118 | + "--license", |
| 119 | + LICENSE, |
| 120 | + "-p", |
| 121 | + f"{OUTPUT_DIR}/{PACKAGE_NAME}_{__version__}.msi", |
| 122 | + ".", |
| 123 | + ] |
| 124 | + run_command(cmd) |
| 125 | + |
| 126 | + |
| 127 | +def main() -> None: |
| 128 | + """Main packaging function.""" |
| 129 | + if not BUILD_DIR.exists(): |
| 130 | + print(f"Error: Build directory {BUILD_DIR} does not exist. Run build.py first.") |
| 131 | + sys.exit(1) |
| 132 | + OUTPUT_DIR.mkdir(parents=True, exist_ok=True) |
| 133 | + |
| 134 | + if sys.platform.startswith("linux"): |
| 135 | + package_linux() |
| 136 | + elif sys.platform.startswith("darwin"): |
| 137 | + package_macos() |
| 138 | + elif sys.platform.startswith("win"): |
| 139 | + package_windows() |
| 140 | + else: |
| 141 | + print(f"Unsupported platform: {sys.platform}") |
| 142 | + sys.exit(1) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments