-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_repos
72 lines (45 loc) · 1.83 KB
/
check_repos
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import os
import multiprocessing
import pathlib
import time
import subprocess
import sys
REPOS_DIRECTORY = pathlib.Path(os.path.expanduser('~/repos/'))
REPOS_TO_CHECK =['dotfiles']
LAST_CHECK_PATH = pathlib.Path(os.path.expanduser('~/repos/.last_time_checked'))
TIME_BETWEEN_CHECKS = 14 * 24 * 60 * 60
def pull_new_commits(repo):
response = subprocess.run(['git', 'pull'], cwd=str(repo), stdout=subprocess.PIPE)
print(response.stdout)
def has_new_commits(repo):
subprocess.run(['git', 'remote', 'update'], cwd=str(repo))
response = subprocess.run(['git', 'rev-list', 'HEAD..origin/master', '--count'], stdout=subprocess.PIPE, cwd=str(repo))
return int(response.stdout) > 0
def check_repo(repo_name):
print('Checking repo {}'.format(repo_name))
repo_path = REPOS_DIRECTORY / repo_name
if has_new_commits(repo_path):
try:
pull_new_commits(repo_path)
except RuntimeError:
print('Could not fetch new changes in repo {}'.format(repo_name))
def check_repos():
with multiprocessing.Pool() as pool:
pool.map(check_repo, REPOS_TO_CHECK, chunksize=1)
def time_last_checked():
last_check_time = 0
if LAST_CHECK_PATH.is_file():
with LAST_CHECK_PATH.open() as last_check_file:
last_check_time = int(last_check_file.readline())
return last_check_time
def offer_to_check():
user_response = input("Check if there were updates in github repos? [Y/n]").lower()
write_check_time(LAST_CHECK_PATH)
return user_response.lower() in ['y', '']
def write_check_time(filepath):
with LAST_CHECK_PATH.open('w') as last_check_file:
last_check_file.write(str(int(time.time())) + '\n')
if __name__ == '__main__':
if time.time() - time_last_checked() > TIME_BETWEEN_CHECKS and offer_to_check():
check_repos()