Created
November 12, 2012 12:35
-
-
Save narusemotoki/4059125 to your computer and use it in GitHub Desktop.
ディレクトリを監視して,ファイルに変更があった場合に任意のコマンドを実行します.
This file contains 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 characters
#!/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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment