-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo_observer.py
73 lines (64 loc) · 2.78 KB
/
repo_observer.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
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
72
73
import os
import time
import socket
import argparse
import subprocess
import helpers
class RepoObserver:
@staticmethod
def get_commit_id():
return str(subprocess.check_output(["git", "rev-parse", "HEAD"]), encoding="utf8").strip("\n")
def update_repo(self, repo_path):
os.chdir(repo_path)
commit_id = self.get_commit_id()
err = os.system("git pull")
if err:
print("Can not run `git pull` command")
return
new_commit_id = self.get_commit_id()
if commit_id != new_commit_id:
with open('./.commit_id', 'w') as f:
f.write(new_commit_id)
def poll(self):
parser = argparse.ArgumentParser()
parser.add_argument("--dispatcher-server",
help="dispatcher host:port, " \
"by default it uses localhost:8888",
default="localhost:8888",
action="store")
parser.add_argument("repo", metavar="REPO", type=str,
help="path to the repository this will observe")
args = parser.parse_args()
dispatcher_host, dispatcher_port = args.dispatcher_server.split(":")
while True:
try:
# call the bash script that will update the repo and check
# for changes. If there's a change, it will drop a .commit_id file
# with the latest commit in the current working directory
self.update_repo(args.repo)
except Exception as e:
raise Exception(e)
if os.path.isfile(".commit_id"):
try:
response = helpers.communicate(dispatcher_host,
int(dispatcher_port),
"status")
except socket.error as e:
raise Exception("Could not communicate with dispatcher server: %s" % e)
if response == "OK":
with open(".commit_id", "r") as f:
commit = f.readline()
response = helpers.communicate(dispatcher_host,
int(dispatcher_port),
"dispatch:%s" % commit)
if response != "OK":
raise Exception("Could not dispatch the test: %s" %
response)
print("dispatched!")
else:
raise Exception("Could not dispatch the test: %s" %
response)
time.sleep(5)
repo_observer = RepoObserver()
if __name__ == '__main__':
repo_observer.poll()