LoginSignup
5
4

More than 5 years have passed since last update.

Pythonで作ったコマンドラインツールをbrew install出来るようにする

Last updated at Posted at 2015-07-13

tl;dr

  1. homebrew-[name] というリポジトリを作る
  2. ごにょごにょする(これから説明)
  3. brew tap [user name]/[name]
  4. brew install [name]

ごにょごにょ

今回は、ドルを円に換算するだけの単純なスクリプトを用意しました
Pythonを使ってWebAPIをコマンドラインツールに落としこむまでの流れ
これを、brew install出来るようにします

まずは、homebrew-usdexという名のリポジトリを作成して、クローン

$ git clone https://github.com/kei-sato/homebrew-usdex.git
$ cd homebrew-usdex

ルートに[name].rbというファイルを作成します

usdex.rb
class Usdex < Formula
  desc "Exchange usd to any currency, or any currency to usd"
  homepage "https://gist.github.com/kei-sato/98675769952ec7538d6a"
  url "https://gist.githubusercontent.com/kei-sato/98675769952ec7538d6a/raw/3b041cdd0e93c93986b15d158bff158927cb84b1/usdex"
  sha256 "976ae1b2066b2cb40be934fb2265ccd3bd8489f78683c6eaecba29c50edd4758"
  version "1.0.0"

  def install
    bin.install "usdex"
  end
end
  • desc - アプリケーションの説明
  • homepage - あればホームページ、なければgitリポジトリ
  • url - ダウンロードするファイルURL(Gistの場合はRawというボタンのリンク先がファイルへの直URL)
  • sha256 - ダウンロードするファイルのハッシュ値(後述)
  • version - 必須

あとは push, tap, install

$ git push origin master
$ brew tap kei-sato/usdex
$ brew install usdex
$ usdex -p 2.5 -v
1(USD) => 122.54078(JPY)
2.5(USD) => 306.35195(JPY)

SHA256?

SHA-256 (Secure Hash Algorithm 256-bit)

SHA-256とは、任意長の原文から固定長の特徴的な値である「ハッシュ値」を求める計算手順(アルゴリズム)の一つ。

  • データを64文字(ハッシュ値)にしてくれるアルゴリズム
  • 同じデータは同じハッシュ値になる + 違うデータは(ほぼ確実に)違うハッシュ値になる
  • つまり、ファイルが2つあった時に、それぞれをハッシュ値にして、同じハッシュ値だったら同じファイルだと言える
  • すると、ファイルとそのファイルに対応するハッシュ値が公開されていれば、ファイルをダウンロードした時に、ダウンロードしたファイルのハッシュ値を取って、それを公開されているハッシュ値に照らしあわせて、同じだったらダウンロードしたファイルが改ざんされていないことが確認できる

opensslで生成できます

$ echo "hello" | openssl dgst -sha256
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03
$ echo "hello, world" | openssl dgst -sha256
853ff93762a06ddbf722c4ebe9ddd66d8f63ddaea97f521c3ecc20da7c976020
$ echo "hello, world" > /tmp/hello
$ openssl dgst -sha256 /tmp/hello
SHA256(/tmp/hello)= 853ff93762a06ddbf722c4ebe9ddd66d8f63ddaea97f521c3ecc20da7c976020

Homebrewでダウンロードするファイルのハッシュ値を生成します

$ openssl dgst -sha256 usdex
SHA256(usdex)= 976ae1b2066b2cb40be934fb2265ccd3bd8489f78683c6eaecba29c50edd4758

参考サイト

http://deeeet.com/writing/2014/05/20/brew-tap/
http://qiita.com/masawada/items/484bbf83ef39cad7af74

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4