|
| 1 | +#!/usr/bin/python3 |
| 2 | +import argparse |
| 3 | +import logging |
| 4 | +import pathlib |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import tarfile |
| 8 | +import tempfile |
| 9 | +import textwrap |
| 10 | +import urllib.request |
| 11 | + |
| 12 | + |
| 13 | +logging.basicConfig( |
| 14 | + level=logging.INFO, |
| 15 | + datefmt="%Y-%m-%d %H:%M:%S%z", |
| 16 | + format="%(asctime)s | %(levelname)s | %(message)s") |
| 17 | +log = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def download_archive(version: str, archive_dir: pathlib.Path) -> pathlib.Path: |
| 21 | + path = archive_dir / "golang_{}.tar.gz".format(version) |
| 22 | + if path.exists(): |
| 23 | + log.info("Golang archive already exists locally. Skip download.") |
| 24 | + return path |
| 25 | + url = "https://storage.googleapis.com/golang/go{}.linux-amd64.tar.gz" \ |
| 26 | + .format(version) |
| 27 | + log.info("Download: url=%s, dest=%s", url, path) |
| 28 | + urllib.request.urlretrieve(url, str(path)) |
| 29 | + return path |
| 30 | + |
| 31 | + |
| 32 | +def extract_archive(archive_path: pathlib.Path, |
| 33 | + extract_into: pathlib.Path) -> None: |
| 34 | + log.info("Extract: archive=%s, dest=%s", archive_path, extract_into) |
| 35 | + with tarfile.open(str(archive_path)) as tar: |
| 36 | + tar.extractall(str(extract_into)) |
| 37 | + |
| 38 | + |
| 39 | +def generate_control_file(version: str, |
| 40 | + revision: str, |
| 41 | + dest: pathlib.Path) -> None: |
| 42 | + dest.parent.mkdir(parents=True, exist_ok=True) |
| 43 | + with dest.open('w') as f: |
| 44 | + f.write(textwrap.dedent("""\ |
| 45 | + Package: nojima-golang |
| 46 | + Version: {}-{} |
| 47 | + Architecture: amd64 |
| 48 | + Priority: optional |
| 49 | + Section: devel |
| 50 | + Maintainer: Yusuke Nojima <[email protected]> |
| 51 | + Description: Go programming language compiler |
| 52 | + """).format(version, revision)) |
| 53 | + |
| 54 | + |
| 55 | +def build_deb(root_dir: pathlib.Path, dest_dir: pathlib.Path) -> None: |
| 56 | + cmd = ["fakeroot", "dpkg-deb", "--build", "-Z", "gzip", |
| 57 | + str(root_dir), str(dest_dir)] |
| 58 | + subprocess.check_call(cmd) |
| 59 | + |
| 60 | + |
| 61 | +def build(version: str, revision: str, dest_dir: pathlib.Path): |
| 62 | + golang_archive_path = download_archive(version, pathlib.Path(".")) |
| 63 | + with tempfile.TemporaryDirectory() as temp_dir_str: |
| 64 | + temp_dir = pathlib.Path(temp_dir_str) |
| 65 | + |
| 66 | + golang_dir = temp_dir / 'usr/local' |
| 67 | + extract_archive(golang_archive_path, golang_dir) |
| 68 | + |
| 69 | + control_path = temp_dir / 'DEBIAN/control' |
| 70 | + generate_control_file(version, revision, control_path) |
| 71 | + |
| 72 | + build_deb(temp_dir, dest_dir) |
| 73 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description="Build the deb package of golang") |
| 78 | + parser.add_argument('version') |
| 79 | + parser.add_argument('revision') |
| 80 | + parser.add_argument('dest_dir') |
| 81 | + args = parser.parse_args() |
| 82 | + |
| 83 | + version = args.version |
| 84 | + revision = args.revision |
| 85 | + dest_dir = pathlib.Path(args.dest_dir) |
| 86 | + if not dest_dir.is_dir(): |
| 87 | + log.error("Not a directory: path=%s", dest_dir) |
| 88 | + sys.exit(2) |
| 89 | + |
| 90 | + build(version, revision, dest_dir) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments