#!/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", required = True)
parser.add_argument("--package", required = True)
parser.add_argument("--version", required = True)
parser.add_argument("--formula", type = argparse.FileType("w"), nargs = "?", default = sys.stdout)
parser.add_argument("--conflicts", nargs = "*")
args = parser.parse_args()
template = '''\
# This Homebrew forumula was automatically generated by a script.
# You might not want to edit it manually.
class {classname} < Formula
desc "Software for working with Cryptech Alpha board HSM"
homepage "https://cryptech.is/"
version "{version}"
url "{url}"
sha256 "{sha256}"
{conflicts}
# 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
resource "PyYAML" do
url "http://pyyaml.org/download/pyyaml/PyYAML-3.11.tar.gz"
sha256 "c36c938a872e5ff494938b33b14aaa156cb439ec67548fcab3535bb78b0846e8"
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/libcryptech-pkcs11.dylib"
#bin.install "sw/pkcs11/p11util"
end
end
'''
with open(args.tarball, "rb") as f:
digest = hashlib.sha256(f.read()).hexdigest()
classname = "".join(word.capitalize() for word in args.package.split("-"))
conflicts = "".join(" conflicts_with \"{}\", :because => \"HSM firmware and PKCS #11 library must match each other\"\n".format(i)
for i in args.conflicts)
args.formula.write(template.format(
version = args.version,
url = os.path.join(args.url_base, os.path.basename(args.tarball)),
sha256 = digest,
classname = classname,
conflicts = conflicts))