Created
May 4, 2012 10:29
-
-
Save methane/2593937 to your computer and use it in GitHub Desktop.
crontab の自動バックアップ
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 characters
| #!/usr/bin/env python | |
| # coding: utf-8 | |
| import os | |
| import subprocess | |
| import sys | |
| def escape(x): | |
| return x.replace("'", "\\'").replace('"', '\\"') | |
| def sh(command, *args): | |
| if args: | |
| command = command % tuple(map(escape, args)) | |
| return subprocess.call(command, shell=True) | |
| # git init しておく | |
| GIT_PATH = os.path.expanduser('~/crontab') | |
| CRONTAB = '/usr/bin/crontab' | |
| if '-e' in sys.argv: | |
| ret = sh([CRONTAB] + sys.argv[1:]) | |
| if ret == 0: | |
| sh("%s -l > '%s'", CRONTAB, GIT_PATH+'/crontab') | |
| os.chdir(GIT_PATH) | |
| sh("git add crontab") | |
| sh("git commit -m 'autocommit'") | |
| else: | |
| os.execl(CRONTAB, CRONTAB, *sys.argv[1:]) | |
| # ft=python |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment