http://reval-mocchi.rhcloud.com

ClojureとMavenとOpenShift

OpenShiftでClojureを動かそうとしていた時に,起きた問題. ; ほぼほぼ同じ構成の別アプリケーションでは動いていたのに,動かなくなったので原因を追究したいがわからなかった...

Could not transfer artifact lein-ring:lein-ring:pom:0.8.13 from/to clojars (https://clojars.org/repo/): Specified destination directory cannot be created: /.m2/repository/lein-ring/lein-ring/0.8.13
This could be due to a typo in :dependencies or network issues.
If you are behind a proxy, try setting the 'http_proxy' environment variable.

option:enable style:terminal title:error messages

解決方法

export LEIN_JVM_OPTS="-Duser.home=$HOME/clojure"

option:enable style:terminal

参考元 Wrong directories in c9.io · Issue #1214 · technomancy/leiningen · GitHub

LuminusでCookieを扱った際に起きたバグ

responseの:cookiesの中にある:valueでnilがあると例外が起きる.

{:status 200, 
 :headers {"Content-Type" "text/html; charset=utf-8"}, 
 :body "response", 
 :session {:key "value"}, 
 :cookies {"app-session-id" {:value nil}}}

option:enable title:responseをdumpした結果

nilがないようにすれば解決した.

                                                           java.lang.Thread.run                    Thread.java:  745
                             java.util.concurrent.ThreadPoolExecutor$Worker.run        ThreadPoolExecutor.java:  617
                              java.util.concurrent.ThreadPoolExecutor.runWorker        ThreadPoolExecutor.java: 1142
                                    io.undertow.server.HttpServerExchange$1.run        HttpServerExchange.java:  759
                               io.undertow.server.Connectors.executeRootHandler                Connectors.java:  197
              io.undertow.server.session.SessionAttachmentHandler.handleRequest  SessionAttachmentHandler.java:   68
org.projectodd.wunderboss.web.async.websocket.UndertowWebsocket$2.handleRequest         UndertowWebsocket.java:  103
         immutant.web.internal.undertow/create-http-handler/reify/handleRequest                   undertow.clj:  130
                                   astragalus.middleware/wrap-internal-error/fn                 middleware.clj:   33
                                          astragalus.middleware/wrap-context/fn                 middleware.clj:   28
                         ring.middleware.x-headers/wrap-content-type-options/fn                  x_headers.clj:   53
                                ring.middleware.x-headers/wrap-frame-options/fn                  x_headers.clj:   38
                               ring.middleware.x-headers/wrap-xss-protection/fn                  x_headers.clj:   71
                              ring.middleware.not-modified/wrap-not-modified/fn               not_modified.clj:   52
                        ring.middleware.default-charset/wrap-default-charset/fn            default_charset.clj:   26
                              ring.middleware.content-type/wrap-content-type/fn               content_type.clj:   30
                                      ring.middleware.resource/wrap-resource/fn                   resource.clj:   28
                  ring.middleware.absolute-redirects/wrap-absolute-redirects/fn         absolute_redirects.clj:   36
                                        ring.middleware.cookies/wrap-cookies/fn                    cookies.clj:  161
                                          ring.middleware.params/wrap-params/fn                     params.clj:   64
                      ring.middleware.multipart-params/wrap-multipart-params/fn           multipart_params.clj:  117
                            ring.middleware.nested-params/wrap-nested-params/fn              nested_params.clj:   84
                          ring.middleware.keyword-params/wrap-keyword-params/fn             keyword_params.clj:   35
                                        ring.middleware.session/wrap-session/fn                    session.clj:  103
                                                                            ...                                     
                                       ring.middleware.session/session-response                    session.clj:   70
                                                                            ...                                     
                                       ring.middleware.cookies/cookies-response                    cookies.clj:  121
                                            ring.middleware.cookies/set-cookies                    cookies.clj:  102
                                                             clojure.core/doall                       core.clj: 3025
                                                             clojure.core/dorun                       core.clj: 3009
                                                               clojure.core/seq                       core.clj:  137
                                                                            ...                                     
                                  ring.middleware.cookies/write-cookies/iter/fn                    cookies.clj:   91
                                            ring.middleware.cookies/write-value                    cookies.clj:   61
                                                                            ...                                     
                                                    ring.util.codec/form-encode                      codec.clj:  108
                                                  ring.util.codec/eval1716/fn/G                      codec.clj:   81
                                                    ring.util.codec/eval1740/fn                      codec.clj:   93
                                                                            ...                                     
                                                            clojure.core/mapcat                       core.clj: 2660
                                                             clojure.core/apply                       core.clj:  630
                                                               clojure.core/seq                       core.clj:  137
                                                                            ...                                     
                                                            clojure.core/map/fn                       core.clj: 2624
                                                 ring.util.codec/eval1740/fn/fn                      codec.clj:   97
                                       ring.util.codec/eval1740/fn/encode-param                      codec.clj:   91
                                             ring.util.codec/eval1740/fn/encode                      codec.clj:   90
                                                  ring.util.codec/eval1716/fn/G                      codec.clj:   81
                                                clojure.core/-cache-protocol-fn               core_deftype.clj:  554
java.lang.IllegalArgumentException: No implementation of method: :form-encode* of protocol: #'ring.util.codec/FormEncodeable found for class: nil

option:enable title:発生した例外 style:terminal

2015-09-21 追記

Luminusというと語弊がありました. ringですね.

文字列をsbclで実行

ruby -eに近い事をsbclでやりたい.

--no-userinit

解決方法として,--no-userinitをコマンドライン引数に含めればいい.

echo '(+ 1 3)' | sbcl --no-userinit

option:enable style:terminal

catでヒアドキュメント

2行以上になる場合ならこれでも

cat << EOS | sbcl --no-userinit
(load "quicklisp.lisp")
(quicklisp-quickstart:install)
(ql:add-to-init-file)
EOS

option:enable style:terminal


あとで他の処理系も追記するかも.

shellで文字列を分割し,配列にする

やりたいこと

$ add-export RBENV_HOME=$HOME/.rbenv
#=> ~/.export/RBENV_HOME/export.zsh
$ cat ~/.export/RBENV_HOME/export.zsh
#=>export RBENV_HOME=/Users/mocchi/.rbenv

option:enable style:terminal

function add-exportでは,引数に渡されたRBENV_HOME=$HOME/.rbenv=で分割する. 分割した値でディレクトリを作成し,そのディレクトリ内へファイルを作成・値の書き込みをしたい.

解決方法

sedを使い=を空白へ置き換える. その結果をevalで評価して配列にする. あとはそのまま値を使えば良い.

add-export(){
  str=$(echo $1 | sed 's/=/ /')
  eval "array=($str)"
  mkdir -p ~/.export/$array[1]/
  ...
  ...
}

Rubyでgitconfigを書き出す

最近,script中でgitconfigを操作する必要がありました. しかし,単純に追記するだけでは,同じ設定を二度書いてしまう可能性があります. なので,configの妥当性を保つためには,中身を一度解析してから書き込む必要があります.

そこで,ライブラリであるgitconfigioを使用しました. gitconfigioは内部で,gitconfigをHashに変換して操作をするので,Keyの重複を防ぐことができます.

導入方法としては,rubygemsに登録してあるので,gem installで済みます.

gem install gitconfig

option:enable style:terminal

以下のようにして,gitconfigを操作しました.

require 'gitconfigio'

fp = './.git/config'
GitConfigIO::merge!(fp,<<-CONFIG)
[core]
  user=username
  email=test@case.email.addr
CONFIG

option:enable title:sample.rb

mocchit/gitconfigio

==であいまい比較をしてくれる言語は便利だよね

PHPの比較は糞だって話があるけど,そのアバウトさがプログラミングのしやすさを底上げしていると思う. 今書いているプログラムで,プログラミングがしやすく可読性の高いコードが書けたので紹介する.

内容としては,スクリプトの中でエディタを立ち上げてユーザに何かしらを書かせるモジュールだ. しかし,エディタは環境によってはインストールされていない可能性がある.それを解決するために, シェルの環境変数$EDITORがあればその値をエディタとし,なければvi,emacsなどを探してくるコードを書いた.

Cなどであればif文などを使用しているが,今回はRubyを使用したのでガードっぽくコードを書いた.あいまい比較では文字列がtrueとなることを利用したHackだ. そして||は,左辺がtrueの場合にはその値を返し,falseの場合に右辺の処理を行うことを利用して処理をつなげる. それらによって,エディタが存在すればその値がRubyの定数EDITORに入る.

p EDITOR = ->{
  which = lambda{|cmd|
    `which #{cmd}`.chomp.length > 0 ? cmd : false
  }
  return which.call(ENV['EDITOR']) ||
    which.call('vi') || which.call('emacs') ||
    which.call('vim') || which.call('nano') ||
    which.call('cat')
}.call
#=> vi

option:enable title:pattern.rb

家の監視システム用にWebカメラを買った

監視システムのためにWebカメラ(マイクロソフト HD Webカメラ LifeCam HD-3000 v2 T3H-00017 )を購入した.

Microsoft製で対応環境にWindowsしか書いてなかったので,OS XLinuxで動くか心配だったけど,OS Xで動いたからLinuxでも動くと思う. (あとで手元のFedora21で確かめてみる

マイクロソフト HD Webカメラ LifeCam HD-3000 v2 T3H-00017