-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
293 lines (243 loc) · 10.9 KB
/
project.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
import http.server
import re, requests
from urllib.parse import unquote, parse_qs
import os
import threading
from socketserver import ThreadingMixIn
from os import environ
from time import gmtime, strftime
import subprocess
from sys import argv
from os import environ
import simplejson
try:
import git
if git.__version__ < '0.3.1':
raise ImportError("Your version of git is %s. Upgrade to 0.3.1 or better." % git.__version__)
have_git = True
except ImportError as e:
have_git = False
GIT_MISSING = 'Requires gitpython module, but not installed or incompatible version: %s' % e
print('Working directory (initial): ')
initialWorkingDirectory = os.getcwd()
print(initialWorkingDirectory)
print('git help -a')
print('\n' + subprocess.check_output('git help -a',
shell=True).decode())
print('\n' + subprocess.check_output('git --version',
shell=True).decode())
main_page_content = '''
<html>
<body>
{content}
</body>
</html>
'''
my_name = os.environ.get('my_name', 'undefined')
my_user = os.environ.get('my_user', 'undefined')
my_email = os.environ.get('my_email', 'undefined')
my_password = os.environ.get('my_password', 'undefined')
print(
'my_user: ' + my_user + '\n' +
'my_email: ' + my_email + '\n' +
'my_password: ' + my_password + '\n')
print('git config --global user.name \"' + my_name + '\"')
print('\n' + subprocess.check_output('git config --global user.name \"' + my_name + '\"',
shell=True).decode())
print('git config --global user.email ' + my_email)
print('\n' + subprocess.check_output('git config --global user.email ' + my_email,
shell=True).decode())
print('git config --global credential.helper cache')
print('\n' + subprocess.check_output('git config --global credential.helper cache',
shell=True).decode())
print('printf \'protocol=https\\nhost=github.com\\nusername=' + my_user + '\\npassword=' + my_password + '\\n\' | git credential approve')
print('\n' + subprocess.check_output('printf \'protocol=https\\nhost=github.com\\nusername=' + my_user + '\\npassword=' + my_password + '\\n\' | git credential approve',
shell=True).decode())
repository_url = 'https://github.com/' + my_user + '/contributions-cal'
local_repository_name = repository_url.rsplit('/', 1)[-1]
file_of_evidences = 'index.html'
print('To perform git clone or git fetch and git pull...')
if (os.path.exists(local_repository_name) == False):
print('git clone ' + repository_url)
print(subprocess.check_output('git clone ' + repository_url,
shell=True).decode())
else:
print('git fetch ' + repository_url)
print(subprocess.check_output('git fetch ' + repository_url,
shell=True).decode())
print('git pull ' + repository_url)
print(subprocess.check_output('git pull ' + repository_url,
shell=True).decode())
nextCurrentDirectory = initialWorkingDirectory + '/' + local_repository_name
if os.name == 'nt':
nextCurrentDirectory = nextCurrentDirectory.replace('/', '\\')
print('Changing current directory from ' + initialWorkingDirectory + ' to ' + nextCurrentDirectory)
os.chdir(nextCurrentDirectory)
print('Working directory: ')
cwd = os.getcwd()
print(cwd)
print('git remote -v')
print(subprocess.check_output('git remote -v', shell=True).decode())
print('Checking existence of \"' + file_of_evidences + '\"...')
if(os.path.exists(file_of_evidences) == False):
print('FILE DOES NOT EXIST. Creating...')
with open(file_of_evidences, 'w'):
pass
else:
print('FILE EXIST')
file_of_evidences_fullPath = '/' + file_of_evidences
if os.name == 'nt':
file_of_evidences_fullPath = os.getcwd() + file_of_evidences_fullPath.replace('/', '\\')
else:
file_of_evidences_fullPath = os.getcwd() + file_of_evidences_fullPath
print('Full path: \"' + file_of_evidences_fullPath + '\"...')
class ThreadHTTPServer(ThreadingMixIn, http.server.HTTPServer):
"This is an HTTPServer that supports thread-based concurrency."
class Shortener(http.server.BaseHTTPRequestHandler):
def do_POST(self):
request_path = self.path
print("\n----- Request Start ----->\n")
print(request_path)
request_headers = self.headers
print(request_headers)
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
resp_parsed = re.sub(r'^jsonp\d+\(|\)\s+$', '', self.data_string.decode("utf-8"))
data = simplejson.loads(resp_parsed)
print(data)
print("<----- Request End -----\n")
repository_type = ''
if 'request' in self.path:
if('gitlab' in request_headers.as_string().lower()):
repository_type = 'Gitlab'
print('The header contain gitlab, so we assume the request is comming from gitlab...')
author = data['user_username']
print("Author: {}".format(author))
hash = data['commits'][0]['id'][0:6]
print("Hash: {}".format(hash))
summary = data['commits'][0]['message'].rstrip()[0:6] + '...'
print("Summary: {}".format(summary))
elif('git-event' in request_headers.as_string().lower()):
repository_type = 'Local Git'
print('The header contain git-event, so we assume the request is comming from local git...')
author = data['author']
print("Author: {}".format(author))
hash = data['hash']
print("Hash: {}".format(hash))
summary = data['summary'].rstrip()[0:6] + '...'
print("Summary: {}".format(summary))
else:
repository_type = 'Bitbucket'
print('The header does NOT contain gitlab, so we assume the request is comming from github...')
author = data['push']['changes'][0]['new']['target']['author']['user']['username']
print("Author: {}".format(author))
hash = data['push']['changes'][0]['new']['target']['hash'][0:6]
print("Hash: {}".format(hash))
summary = data['push']['changes'][0]['new']['target']['message'].rstrip()[0:6] + '...'
print("Summary: {}".format(summary))
print("my_user: \"" + my_user.rstrip() + "\"; author: \"" + author.rstrip() + "\"")
if ( my_user.rstrip() != author.rstrip()):
print("Disconsidering this change as it was not done by me this time!")
else:
repo = git.Repo(".")
print("Location "+ repo.working_tree_dir)
print("Remote: " + repo.remote("origin").url)
the_date = strftime("%Y-%m-%d", gmtime())
the_time = strftime("%H:%M:%S", gmtime())
commit_message = the_date + ' ' + the_time + ' ' + hash + ' ' + summary
##
with open(file_of_evidences_fullPath, "r") as in_file:
buf = in_file.readlines()
with open(file_of_evidences_fullPath, "w") as out_file:
for line in buf:
if line.strip() == "<tbody>":
newContent = "\
<tr> \
<td>\
" + the_date + " " + the_time + "\
</td>\
<td>\
" + hash + " " + summary + "\
</td>\
<td class=\"" + repository_type + "\">\
" + repository_type + "\
</td>\
</tr>\
"
line = line + newContent
out_file.write(line)
##
index = repo.index
index.add([repo.working_tree_dir + '/*'])
new_commit = index.commit(commit_message)
origin = repo.remotes.origin
origin.push()
fileName = file_of_evidences_fullPath
f = open(fileName, 'rb') #open requested file
#send code 200 response
self.send_response(200)
#send header first
self.send_header('Content-type','text-html')
self.end_headers()
#send file content to client
textToBeSent = main_page_content.format(content=f.read().decode("utf-8"))
#print(textToBeSent)
self.wfile.write(textToBeSent.encode())
f.close()
return
def do_GET(self):
request_path = self.path
print("\n----- Request Start ----->\n")
print(request_path)
print(self.headers)
print("<----- Request End -----\n")
if 'request' in self.path:
if not have_git:
print(GIT_MISSING)
else:
repo = git.Repo(".")
print("Location "+ repo.working_tree_dir)
print("Remote: " + repo.remote("origin").url)
commit_message = strftime("%Y-%m-%d %H:%M:%S", gmtime())
lastPartOfThePath = self.path.rsplit('/', 1)[-1]
if(lastPartOfThePath != 'request'):
commit_message += ' ' + lastPartOfThePath
with open(file_of_evidences_fullPath, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(commit_message + '<BR>' + content)
f.close()
index = repo.index
index.add([repo.working_tree_dir + '/*'])
new_commit = index.commit(commit_message)
origin = repo.remotes.origin
origin.push()
fileName = file_of_evidences_fullPath
if (self.path.endswith('.html')):
if os.name == 'nt':
fileNameTemp = os.getcwd() + self.path.replace('/', '\\')
else:
fileNameTemp = os.getcwd() + self.path
print(fileName)
if (os.path.exists(fileNameTemp) == False):
print('\"' + fileName + '\" file not exist')
else:
fileName = fileNameTemp
print('\"' + fileName + '\" file exist')
f = open(fileName, 'rb') #open requested file
#send code 200 response
self.send_response(200)
#send header first
self.send_header('Content-type','text-html')
self.end_headers()
#send file content to client
textToBeSent = main_page_content.format(content=f.read().decode("utf-8"))
#print(textToBeSent)
self.wfile.write(textToBeSent.encode())
f.close()
return
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8001)) # Use PORT if it's there.
server_address = ('', port)
httpd = ThreadHTTPServer(server_address, Shortener)
httpd.serve_forever()