aboutsummaryrefslogblamecommitdiff
path: root/build-homebrew-formula.py
blob: c6aa0fa9f6a698ac880c48cd1a16c8c8dcbc20cf (plain) (tree)



























                                                                                                

                                                                                                                                     
 

                                                                                                     
 





                                                                                                                                      

             
 




                                                                     
 
                                                                                      

                                                                        
 





                                                                               
                       

                                                                                                                         

                                    
 


                                                                    
                                                         
                                                        


                                                                               
 


                                                  











                                                                          
#!/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.

    sbin.install  "sw/stm32/projects/hsm/cryptech_upload"
    sbin.install  "sw/stm32/projects/hsm/cryptech_probe"
    sbin.env_script_all_files(libexec/"sbin", :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"
    sbin.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))