Created
February 24, 2010 04:18
-
-
Save eight/313094 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
#祝祭日定義ファイルの例 | |
#シャープで始まる行はコメント | |
#スペース区切りで1列目が日付として認識される | |
# $Id: hol,v 1.1 2004/02/16 03:09:59 ymo Exp $ | |
#2004年 | |
2004-01-01 元旦 | |
2004-01-12 成人の日 | |
2004-02-11 建国記念日 | |
2004-03-20 春分の日 | |
2004-04-29 みどりの日 | |
2004-05-03 憲法記念日 | |
2004-05-04 国民の休日 | |
2004-05-05 こどもの日 | |
2004-07-19 海の日 | |
#この辺りにお盆休み | |
2004-08-12 | |
2004-08-13 | |
2004-09-20 敬老の日 | |
2004-09-23 秋分の日 | |
2004-10-11 体育の日 | |
2004-11-03 文化の日 | |
2004-11-23 勤労感謝の日 | |
2004-12-23 天皇誕生日 | |
#この辺りに年末 | |
2004-12-27 | |
2004-12-28 | |
2004-12-29 | |
2004-12-30 | |
2004-12-31 |
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 | |
# | |
# 今日が休日ならば引数のコマンドを実行しない | |
# 休日じゃなかったら引数のコマンドを実行する | |
# cronで起動するプログラムを、祝日や休日には実行させたくない場合に使う。 | |
# 最近当社でブームのPythonで作ったもの。 | |
# とりあえず使えますが、初めてPythonで作った実用スクリプトであまり自信なし。 | |
# | |
# 使い方 | |
# runsch.py [-q|--quiet] [-d YYYY-MM-DD] [-c 休日ファイル] [-w 曜日] コマンドライン | |
# | |
# 休日かどうかは2種類の判定方法がある | |
# | |
# -c オプションで休日ファイル指定 | |
# 休日ファイル指定(-c holidays.txt) | |
# 休日ファイルはカンマで区切って複数指定可。 | |
# 休日ファイルにはYYYY-MM-DD形式で1行に1日の休日を書いておく | |
# | |
# -w オプションで曜日指定(cronを使うならこのオプションは不要だ) | |
# 曜日指定(-w sat,sun,wed,fri) | |
# 英語3文字で曜日を指定する。複数の場合はカンマで区切る。 | |
# sun,mon,tue,wed,thu,fri,satの7つが使える。大文字小文字は区別しない。 | |
# | |
# -c オプションで休日ファイルが指定されないと、 | |
# /etc/holidaysというファイルを探す。 | |
# | |
# -d オプションはチェックする対象の日付を指定できる | |
# -dが省略されると今日の日付になる | |
# | |
# -q オプションはメッセージを出力しない | |
# | |
# | |
#例1. | |
# runsch.py -c holidays ls -l | |
#holidaysというファイルを休日ファイルとして読み込んで、もし今日が含まれていたら、何もしない。 | |
#もし今日の日付がholidaysに含まれていなければ、"ls -l"を実行する。 | |
# | |
#例2. | |
# runsch.py -w sat ls -l | |
#もし今日が土曜日だったら何もしない。もし今日が土曜日以外なら、"ls -l"を実行する。 | |
#この例はcronを使うなら意味なし。 | |
# | |
# $Id: runsch,v 1.2 2004/02/16 02:33:09 ymo Exp $ | |
# | |
import sys,os,time,datetime,getopt | |
HOWTOUSE='usage: %s [-q|--quiet] [-c holiday_file1,holiday_file2...] [-w sun,sat,...] command [param1,param2...]' | |
DAYOFF=' %s(%s) is a day OFF! Skip command execution. -> %s' | |
DAYON=' %s(%s) is a day ON! -> %s' | |
HOLIDAYS='/etc/holidays' | |
DATE_FORMAT='%Y-%m-%d' | |
DEBUG = False | |
def isWeeks(weeks,target): | |
""" | |
曜日指定されていた場合で、指定日が対象の曜日だったらTrueを返す | |
""" | |
for w in weeks: | |
print target.strftime('%a').lower(),w.lower() | |
if target.strftime('%a').lower() == w.lower(): | |
return True | |
return False | |
def isHoliday(holfiles,target): | |
global DAYOFF_MESSAGE | |
""" | |
休日ファイルに、指定日が含まれていたらTrueを返す | |
休日ファイルにはカンマ区切りで複数のファイル名を指定できる | |
休日ファイルには、#で始まるコメント欄と空白(無視)を書ける | |
""" | |
targetDay = target.strftime(DATE_FORMAT) | |
for holfile in holfiles.split(','): | |
try: | |
for hol in file(holfile): | |
#空行は無視 | |
if len(hol.strip())<1: continue | |
holiday = hol.split(' ')[0].strip() | |
#コメント欄を無視 | |
if holiday[0] == '#': continue | |
if holiday == targetDay: return True | |
except IOError,e: | |
print "Skip %s ... %s" % (holfile,e) | |
pass | |
return False | |
if __name__=='__main__': | |
optlist,args = getopt.getopt(sys.argv[1:],'qd:c:w:') | |
if DEBUG: | |
print " option:",optlist | |
print " args:",args | |
if len(args) < 1: | |
print HOWTOUSE % sys.argv[0] | |
sys.exit(1) | |
#オプションのチェック | |
verbose = True | |
holfile = HOLIDAYS | |
weeks = False | |
day = time | |
for o,v in optlist: | |
if o == '-w': | |
weeks = v.split(',') | |
if o == '-c': | |
holfile = v | |
if o == '-d': | |
y,m,d = v.split('-') | |
day = datetime.date(int(y),int(m),int(d)) | |
if (o == '-q') or (o == '--quiet'): | |
verbose = False | |
#コマンドの取得 | |
cmd = '' | |
for arg in args: | |
cmd = cmd+" "+arg | |
cmd = cmd.strip() | |
#曜日のチェック | |
if weeks: | |
if isWeeks(weeks,day): | |
if verbose: | |
print DAYOFF % (day.strftime(DATE_FORMAT),day.strftime('%a'),cmd) | |
sys.exit(1) | |
#休日チェック | |
if not isHoliday(holfile,day): | |
if verbose: | |
print DAYON % (day.strftime(DATE_FORMAT),day.strftime('%a'),cmd) | |
os.system(cmd) | |
else: | |
if verbose: | |
print DAYOFF % (day.strftime(DATE_FORMAT),day.strftime('%a'),cmd) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment