-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathupdate-time.py
executable file
·43 lines (29 loc) · 1.14 KB
/
update-time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
from time import gmtime, strftime
from sys import exit, stderr
from subprocess import call, check_output
def main():
git_log = check_output("git log".split(' ')).decode()
if git_log.split('\n')[4].find("Git Hook: Updated 'last_updated' time") >= 0:
exit(0)
new_date = 'last_updated: {}'.format(strftime("\"%a %b %d %H:%M:%S UTC %Y\"", gmtime()))
with open('_config.yml', 'r') as FILE:
filedata = FILE.read()
old_date_loc = filedata.find('last_updated: \"')
if old_date_loc < 0:
print_err()
old_date = filedata[old_date_loc:filedata.find("\n", old_date_loc)]
filedata = filedata.replace(old_date, new_date)
with open('_config.yml', 'w') as FILE:
FILE.write(filedata)
if call("git add _config.yml".split(' ')):
print_err()
if call(["git", "commit", "-m", "Git Hook: Updated 'last_updated' time"]):
print_err()
print("[+] Updated 'last_updated' time. Please `git push` again.")
exit(1)
def print_err():
stderr.write("[!] Error updating 'last_updated:' in '_config.yml. Exiting...\n")
exit(1)
if __name__ == '__main__':
main()