aboutsummaryrefslogtreecommitdiff
path: root/build-homebrew-formula.py
blob: cbaa35c8150b373ecaf1b1f8119a67b625118372 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python

# Yes, this is a Python program writing a Ruby program.

import argparse
import hashlib
import sys
import os

parser = argparse.ArgumentParser()
parser.add_argument("--url-base", default = "https://brew.cryptech.is/tarballs/")
parser.add_argument("tarball")
parser.add_argument("version")
parser.add_argument("formula", type = argparse.FileType("w"), nargs = "?", default = sys.stdout)
args = parser.parse_args()

template = '''\
# This Homebrew forumula was automatically generated by a script.
# You might not want to edit it manually.

class CryptechAlpha < Formula

  desc     "Software for working with Cryptech Alpha board HSM"
  homepage "https://cryptech.is/"
  version  "{version}"
  url      "{url}"
  sha256   "{sha256}"

  # See https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Formula-Cookbook.md#specifying-other-formulae-as-dependencies
  # for details on handling dependencies on other homebrew packages (eg, sqlite3).

  # See https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Python-for-Formula-Authors.md
  # for details on handling dependencies on Python libraries (eg, pyserial).

  depends_on "sqlite3"

  resource "pyserial" do
    url    "https://pypi.python.org/packages/3c/d8/a9fa247ca60b02b3bebbd61766b4f321393b57b13c53b18f6f62cf172c08/pyserial-3.1.1.tar.gz"
    sha256 "d657051249ce3cbd0446bcfb2be07a435e1029da4d63f53ed9b4cdde7373364c"
  end

  def install

    # Installation is a bit complex due to the way Homebrew handles
    # Python library dependencies and due to our stuff being a mix of
    # Python and C.

    # Set PYTHONPATH to point to our private library location.

    ENV.prepend_create_path "PYTHONPATH", libexec/"vendor/lib/python2.7/site-packages"

    # Add all resources (and assume they are all Python, be careful...).

    resources.each do |r|
      r.stage do
        system "python", *Language::Python.setup_install_args(libexec/"vendor")
      end
    end

    # Build everything.

    ohai "Building PKCS #11 code (including crypto and bignum libraries) from source, this is slow, please be patient..."
    ENV.deparallelize
    system "make", "-C", "sw/pkcs11"

    # Install the Python scripts, then replace them with stubs which
    # set PYTHONPATH before calling the real scripts.

    bin.install   "sw/stm32/projects/hsm/cryptech_upload"
    bin.install   "sw/stm32/projects/hsm/cryptech_probe"
    bin.install   "sw/stm32/projects/hsm/cryptech_miniterm"
    bin.env_script_all_files(libexec/"bin", :PYTHONPATH => ENV["PYTHONPATH"])

    # Install other (non-Python) stuff, then we are done.

    share.install "cryptech-alpha-firmware.tar.gz"
    lib.install   "sw/pkcs11/libpkcs11.dylib"
    bin.install   "sw/pkcs11/p11util"
  end

end
'''

with open(args.tarball, "rb") as f:
    digest = hashlib.sha256(f.read()).hexdigest()

args.formula.write(template.format(
    version = args.version,
    url     = os.path.join(args.url_base, os.path.basename(args.tarball)),
    sha256  = digest))