|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import os |
| 5 | +import re |
| 6 | +import shutil |
| 7 | +import subprocess as sp |
| 8 | + |
| 9 | +from string import Template |
| 10 | +from zipfile import ZipFile |
| 11 | + |
| 12 | +brew_formula_template = Template(""" |
| 13 | +# This file was generated by release.py |
| 14 | +require 'formula' |
| 15 | +class Pup < Formula |
| 16 | + homepage 'https://github.com/EricChiang/pup' |
| 17 | + version '${version}' |
| 18 | +
|
| 19 | + if Hardware.is_64_bit? |
| 20 | + url 'https://github.com/EricChiang/pup/releases/download/v${version}/pup_darwin_amd64.zip' |
| 21 | + sha1 '${darwin_amd64_sha1}' |
| 22 | + else |
| 23 | + url 'https://github.com/EricChiang/pup/releases/download/v${version}/pup_darwin_386.zip' |
| 24 | + sha1 '${darwin_386_sha1}' |
| 25 | + end |
| 26 | +
|
| 27 | + def install |
| 28 | + bin.install 'pup' |
| 29 | + end |
| 30 | +end |
| 31 | +""".strip()) |
| 32 | + |
| 33 | +version_re = re.compile(r'\nvar VERSION string = \"([0-9\.]+)\"\n') |
| 34 | + |
| 35 | +if __name__ == "__main__": |
| 36 | + with open("pup.go", "r") as f: |
| 37 | + version = version_re.findall(f.read()) |
| 38 | + |
| 39 | + assert len(version) == 1, "[-] Failed to find current version %s" % (version,) |
| 40 | + version = version[0] |
| 41 | + print "[+] Version found:", version |
| 42 | + |
| 43 | + if os.path.isdir("./dist"): |
| 44 | + print "[+] Directory 'dist' exists. Removing it." |
| 45 | + shutil.rmtree("./dist") |
| 46 | + |
| 47 | + print "[+] Cross-compiling pup." |
| 48 | + sp.check_call(["gox", "-output", "dist/{{.Dir}}_{{.OS}}_{{.Arch}}"]) |
| 49 | + |
| 50 | + print "[+] Zipping executables." |
| 51 | + sha1sums = {} |
| 52 | + for fi in os.listdir("./dist"): |
| 53 | + exe = os.path.join("dist", "pup") |
| 54 | + zipname = os.path.join("dist", fi) |
| 55 | + if fi.endswith(".exe"): |
| 56 | + exe += ".exe" |
| 57 | + zipname = zipname.rstrip(".exe") |
| 58 | + zipname += ".zip" |
| 59 | + os.rename(os.path.join("dist", fi), exe) |
| 60 | + with ZipFile(zipname, "w") as z: |
| 61 | + z.write(exe, os.path.basename(exe)) |
| 62 | + os.remove(exe) |
| 63 | + h = hashlib.sha1() |
| 64 | + with open(zipname, "r") as z: |
| 65 | + h.update(z.read()) |
| 66 | + sha1sum = h.hexdigest() |
| 67 | + print "[+] %s: %s" % (sha1sum, zipname,) |
| 68 | + sha1sums[zipname] = sha1sum |
| 69 | + |
| 70 | + t = brew_formula_template.substitute( |
| 71 | + version=version, |
| 72 | + darwin_amd64_sha1 = sha1sums["dist/pup_darwin_amd64.zip"], |
| 73 | + darwin_386_sha1 = sha1sums["dist/pup_darwin_386.zip"], |
| 74 | + ) |
| 75 | + print "[+] Updating brew formula." |
| 76 | + with open("pup.rb", "w") as f: |
| 77 | + f.write(t) |
0 commit comments