Sphinx のビルドをファイル監視して自動で行う

Sphinx でドキュメントを書く必要があったので、せっかくなので make html をいちいち手打ちしないで自動でビルドするようにした。

先人はいる

ファイル監視してスクリプトってのはまあある話で、Python なら watchdog というのが使えるようだ。
参考
watchdogモジュールのwatchmedoコマンドが便利 - 偏った言語信者の垂れ流し
omakeもmakeも使わずに、Sphinxドキュメントの継続的ビルド - Study08.net 対シンバシ殲滅用人型機動兵器

pip install watchdog
watchmedo shell-command --patterns="*.rst" --recursive --command='make html'
# あるいは
watchmedo shell-command -p="*.rst" -R -c="make html"

なんてことはない話ですね。

で、できると思った?

残念さやかちゃんでした!

Vim だとファイル更新が検知されない

VimFiler で新規作成したときは検知されて更新がはしるけど、ファイル保存時には検知されない。で、ちょっと聞いてみると Vim 特有の問題で他のエディターだと問題がなかった。たとえば、CotEditor だと特に問題なく検知され更新が走った。

これの問題は gorakhargosh/watchdog · GitHub にあった。

About using watchdog with editors like Vim

Vim does not modify files unless directed to do so. It creates backup files and then swaps them in to replace the files you are editing on the disk. This means that if you use Vim to edit your files, the on-modified events for those files will not be triggered by watchdog. You may need to configure Vim to appropriately to disable this feature.

Vim だとバックアップファイルの関係でファイル編集イベントが拾えないよ。だからなんか Vim の設定を無効にしてね」とある。でも set nobackup set noswapfile はしている。つまり、スワップファイルもバックアップファイルもつくらないようにしている。なぜだろう。さがした。あった。

python - Writing a file with vim doesn't fire a file change event on OS X - Stack Overflow

ということで、以下のようになった

set noswapfile
set nobackup
set nowritebackup

まあ

勝手にビルドしてくれるのはすごい楽ですね