Created
November 12, 2012 12:35
-
-
Save narusemotoki/4059125 to your computer and use it in GitHub Desktop.
Revisions
-
narusemotoki created this gist
Nov 12, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import datetime import time import os from stat import * import commands def watch(dir, command): timestamp = time.mktime(datetime.datetime.now().utctimetuple()) while True: for file in os.listdir(dir): # 隠しファイルは無視 if 0 is file.find('.'): continue file_timestamp = os.stat(file)[ST_MTIME] if timestamp < file_timestamp: timestamp = file_timestamp print(commands.getoutput(command)) break # 100ミリ秒待機 time.sleep(0.1) def help(): print(u'第一引数が監視対象のディレクトリです.') print(u'第二匹数が監視下のファイルに変更があった場合に実行するコマンドです.') print(u'例: % dirwatch . \'echo "hello"\'') print(u'例ではカレントディレクトリ内のファイルに変更があった場合にhelloと表示します.') if __name__ == '__main__': # 引数足りない場合にヘルプを表示する. if 3 > len(sys.argv): help() else: watch(sys.argv[1], sys.argv[2])