sandcastle-build-generic (4490B)
1 #!/usr/bin/env python3 2 # This file is in the public domain. 3 # Helper script to build the latest DEB packages in the container. 4 5 # Supported tag syntax variants: 6 # v$maj.$min$.$patch => release version 7 # v$maj.$min$.$patch-dev.$n => dev version 8 # deb-v$maj.$min$.$patch-$revision => release version with debian revision 9 # Debian revisions of dev versions are *not* supported 10 11 import os 12 import sys 13 import subprocess 14 import glob 15 import shutil 16 import shlex 17 from email.utils import formatdate 18 19 def run_cmd(cmd, shell=False, cwd=None, env=None): 20 """Helper to run commands and exit on failure (mimicking set -e).""" 21 # If specific env vars are passed, update the current env, otherwise use current 22 command_env = os.environ.copy() 23 if env: 24 command_env.update(env) 25 26 # Flush stdout so logs appear in order 27 sys.stdout.flush() 28 29 subprocess.check_call(cmd, shell=shell, cwd=cwd, env=command_env) 30 31 def get_tag_debver(tag): 32 """Get a debian version string from a git tag""" 33 if tag.startswith("v"): 34 devsuff = "-dev." 35 d = tag.find(devsuff) 36 if d < 0: 37 return tag[1:] 38 else: 39 return tag[1:d] + "~dev" + tag[d + len(devsuff)] 40 if tag.startswith("deb-v"): 41 tag = tag[5:] 42 if "-" in tag: 43 a, b = tag.split("-") 44 return a + ":" + b 45 return tag 46 return tag 47 #raise Error("unexpected tag format") 48 49 50 def main(): 51 if 'LD_LIBRARY_PATH' in os.environ: 52 del os.environ['LD_LIBRARY_PATH'] 53 54 # Arguments 55 if len(sys.argv) < 2: 56 print(f"Usage: {sys.argv[0]} <PACKAGE>", file=sys.stderr) 57 sys.exit(1) 58 59 package = sys.argv[1] 60 61 # Path of the debian/ folder in the repository 62 DEBIANPATH = "" 63 debpath_file = f"/buildconfig/{package}.debpath" 64 if os.path.exists(debpath_file): 65 with open(debpath_file, 'r') as f: 66 DEBIANPATH = f.read().strip() 67 68 print(f"Building {package} with generic build logic", file=sys.stderr) 69 70 with open(f"/buildconfig/{package}.tag", 'r') as f: 71 tag = f.read().strip() 72 73 outdir = f"/packages/{package}/" 74 75 os.makedirs(outdir, exist_ok=True) 76 77 os.chdir("/packages/") 78 79 # Using shell=True here to easily handle the pipe logic 80 run_cmd("dpkg-scanpackages . | xz - > /packages/Packages.xz", shell=True) 81 82 with open("/etc/apt/sources.list.d/taler-packaging-local.list", "w") as f: 83 f.write("deb [trusted=yes] file:/packages ./\n") 84 85 run_cmd(["apt-get", "update"]) 86 87 # Prepare Build Directory 88 if not os.path.exists("/build"): 89 os.makedirs("/build") 90 os.chdir("/build") 91 92 with open(f"/buildconfig/{package}.giturl", 'r') as f: 93 GITURL = f.read().strip() 94 95 run_cmd(["git", "config", "--global", "advice.detachedHead", "false"]) 96 run_cmd(["git", "clone", "--depth=1", f"--branch={tag}", GITURL, package]) 97 98 build_pkg_path = os.path.join("/build", package, DEBIANPATH) 99 os.chdir(build_pkg_path) 100 101 deb_version = get_tag_debver(tag) 102 103 # Bootstrap and Install Deps 104 os.chdir(os.path.join("/build", package)) 105 run_cmd(["./bootstrap"]) 106 107 os.chdir(build_pkg_path) 108 109 # Install build-time dependencies 110 tool_cmd = "apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends --yes" 111 run_cmd(["mk-build-deps", "--install", f"--tool={tool_cmd}", "debian/control"]) 112 113 # Sparse checkout hint 114 with open(".version", "w") as f: 115 f.write(f"{deb_version}\n") 116 117 # Configure Environment for Build 118 deb_dbg_repo = "debian/.debhelper/" 119 os.environ["DEB_DBG_SYMBOLS_REPO"] = deb_dbg_repo 120 os.makedirs(deb_dbg_repo, exist_ok=True) 121 122 open(os.path.join(deb_dbg_repo, "debian-symbols-pool"), 'a').close() 123 124 os.environ["DEB_BUILD_MAINT_OPTIONS"] = "debug" 125 126 debian_date = formatdate(localtime=True) 127 128 changelog = f"""\ 129 {package} ({deb_version}) unstable; urgency=low 130 131 * Release {deb_version} (for sandcastle-ng). 132 133 -- Taler Packaging Team <deb@taler.net> {debian_date} 134 """ 135 136 with open("debian/changelog", "w") as f: 137 f.write(changelog) 138 139 # Build Package 140 run_cmd(["dpkg-buildpackage", "-rfakeroot", "-b", "-uc", "-us"]) 141 142 # Copy artifacts 143 # Globs for ../*.deb and ../*.ddeb relative to current dir 144 deb_files = glob.glob("../*.deb") 145 ddeb_files = glob.glob("../*.ddeb") 146 147 for f in deb_files: 148 shutil.copy(f, outdir) 149 150 for f in ddeb_files: 151 shutil.copy(f, outdir) 152 153 if __name__ == "__main__": 154 main()