Skip to content

Instantly share code, notes, and snippets.

@matsubo
Forked from narusemotoki/dirwatch.py
Last active December 13, 2015 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsubo/4992894 to your computer and use it in GitHub Desktop.
Save matsubo/4992894 to your computer and use it in GitHub Desktop.
Improved detecting performance.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import datetime
import time
import os
from stat import *
import commands
import fnmatch
def watch(dir, command, extension):
timestamp = time.mktime(datetime.datetime.now().utctimetuple())
while True:
for root, dirnames, filenames in os.walk(dir):
for filename in fnmatch.filter(filenames, '*' + extension):
file = os.path.join(root, filename)
file_timestamp = os.stat(file)[ST_MTIME]
if timestamp < file_timestamp:
timestamp = file_timestamp
print "Executing..."
print(commands.getoutput(command))
break
# 100ミリ秒待機
time.sleep(0.1)
def help():
print(u'第一引数が監視対象のディレクトリです.')
print(u'第二引数が監視下のファイルに変更があった場合に実行するコマンドです.')
print(u'第三引数が拡張子')
print(u'例: % dirwatch . \'phpunit\' \'php\'')
print(u'例ではカレントディレクトリ内のファイルに変更があった場合にhelloと表示します.')
if __name__ == '__main__':
# 引数足りない場合にヘルプを表示する.
if 4 > len(sys.argv):
help()
else:
watch(sys.argv[1], sys.argv[2], sys.argv[3])
@matsubo
Copy link
Author

matsubo commented Feb 20, 2013

IDEっぽいことが出来る。「保存した瞬間にビルド」的な。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment