Skip to content

Instantly share code, notes, and snippets.

@narusemotoki
Created November 12, 2012 12:35
Show Gist options
  • Select an option

  • Save narusemotoki/4059125 to your computer and use it in GitHub Desktop.

Select an option

Save narusemotoki/4059125 to your computer and use it in GitHub Desktop.

Revisions

  1. narusemotoki created this gist Nov 12, 2012.
    36 changes: 36 additions & 0 deletions dirwatch.py
    Original 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])