Drkcore

06 04 2011 Python macbook Tweet

Pythonでファイル更新検知して任意のコマンドを実行する簡易コマンドを作った

Sphinx使っているのだけどファイルを更新するたびにmake htmlと打って確認するのがめんどくさい。linuxだったらpyinotifyがあるんだけど、macbookでは使えない。

簡易コマンド書いた。

#!/usr/bin/env python

import os
from time import sleep, strftime, localtime
import sys
import commands

filename = sys.argv[1]
mycommand = sys.argv[2]

def get_mtime():
    return os.stat(filename).st_mtime

mtime = get_mtime()
while 1:
    sleep(1)
    new_mtime = get_mtime()
    if mtime != new_mtime:
        mtime = new_mtime
        commands.getoutput(mycommand)
    print "done: %s ( %s )" % (mycommand, strftime("%a, %d %b %Y %H:%M:%S", localtime(mtime)))

監視対象のファイルを第一引数に、更新されたら実行するコマンドを第二引数に。

$ wdo docutils.rst "make html"
done: make html ( Wed, 06 Apr 2011 05:12:30 )
done: make html ( Wed, 06 Apr 2011 05:26:27 )
done: make html ( Wed, 06 Apr 2011 05:28:36 )
done: make html ( Wed, 06 Apr 2011 05:33:35 )
done: make html ( Wed, 06 Apr 2011 05:33:45 )

そうです、お分かりの通り早朝からdocutilsのソースを読んでいるのです。

ちなみにpyhttpd='python -m SimpleHTTPServer'っていうalias切っているので、makeで生成されたhtmlはlocalhost:8000で見てる

参考

  • ファイル更新の監視 / どう書く?org

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021