-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
90 lines (77 loc) · 2.94 KB
/
Main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import socket
import json
import datetime
import os
from subprocess import call
import git
baseDir = "/tmp/UmonsITListener"
targetDir = baseDir+"/CompiledPDFs"
pdfRepoUrl= "https://github.com/UMonsIT/CompiledPDFs.git"
def mainLoop(serversocket):
while True:
connection, address = serversocket.accept()
buf = connection.recv(500000)
if len(buf) > 0:
#isolate JSON from message
isJson = False
js = ""
for line in buf:
if line[0]=="{":
isJson = True
if isJson:
js += line
#extract repo name
jsdoc = json.JSONDecoder().decode(js)
repository = jsdoc["repository"]["name"]
repoUrl = jsdoc["repository"]["git_url"]
print "Detected changes in", repository, "("+repoUrl+")"
#launch the update routine
#first check if repo has already been cloned
dirName = baseDir+"/"+repository
if not (os.path.exists(dirName) and os.path.isdir(dirName)):
clone(repoUrl, dirName)
pull(repository, dirName)
if not (os.path.exists(targetDir) and os.path.isdir(targetDir)):
clone(pdfRepoUrl, targetDir)
compileAndMove(dirName, repository, targetDir)
commitAndPush(targetDir)
def clone(url, dirName):
print "Cloning", url, "in", dirName
repo = git.Repo.clone_from(url, dirName)
print "Done cloning..."
def pull(repo, dirName):
print "Pulling", repo, "in", dirName
git.Repo(dirName).remote().pull()
def hasMakefile(compileDir):
for fl in os.listdir(compileDir):
if os.path.isfile(compileDir+"/"+fl) and fl == "Makefile":
return True
def compileAndMove(compileDir, repoName, targetDir):
if hasMakefile(compileDir):
print "Compiling", repoName, "in", compileDir
call("cd "+compileDir+" && make")
move(compileDir, repoName, targetDir)
else:
for subFolder in os.listdir(compileDir):
if os.path.isdir(compileDir + "/" + subFolder):
compileAndMove(subFolder, repoName, targetDir)
def move(compileDir, repoName, targetDir):
print "Copying PDFs in", compileDir, "to", targetDir+repoName
call("mv "+compileDir+"/*.pdf "+targetDir+"")
def commitAndPush(target):
message = "Auto-compiled on " + datetime.datetime.now().isoformat()
print "Commiting", target, "with message", message
git.Repo(target).commit(message)
print "Pushing", target
git.Repo(target).remote().push()
if __name__ == "__main__":
port = 4242
print "Starting server on port ", port, "..."
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', port))
serversocket.listen(5) # become a server socket, maximum 5 connections
try:
while True :
mainLoop(serversocket)
finally:
serversocket.close()