-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode_versioning_process.py
More file actions
248 lines (220 loc) · 12.3 KB
/
code_versioning_process.py
File metadata and controls
248 lines (220 loc) · 12.3 KB
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
import requests
import json
import logging
import os
import time
# Log messages written into file
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
filename='code_versioning.log',
filemode='w',
level=logging.INFO)
# Functions to convert Unicode files into UTF-8. (Red u problem bugfix)
# Mostly taken from internet
def json_load_byteified(file_handle):
return _byteify(
json.load(file_handle, object_hook=_byteify),
ignore_dicts=True
)
# Functions to convert Unicode files into UTF-8. (Red u problem bugfix)
# Mostly taken from internet
def json_loads_byteified(json_text):
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
)
# Functions to convert Unicode files into UTF-8. (Red u problem bugfix)
# Mostly taken from internet
def _byteify(data, ignore_dicts = False):
# if this is a unicode string, return its string representation
if isinstance(data, unicode):
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.iteritems()
}
# if it's anything else, return it in its original form
return data
# Class for managing code versioning and its operations.
class code_versioning:
comingJson = json # Incoming request file
sendJson = json # Outgoing response file
# The init method or constructor
def __init__(self, jsonFile):
logging.info('code_versioning object created.')
# Parses the incoming JSON file, converts it to an object.
self.comingJson = json_loads_byteified(jsonFile)
# Creates a JSON file in the current directory. (It will be used for both incoming requests and outgoing responses.)
def createJsonFileInDirectory(self):
print(self.sendJson)
with open('cv_response.json', 'w') as outfile:
writeString=(str)(self.sendJson).replace("\'","\"")
outfile.write(writeString)
logging.info('Response JSON file created.')
def splitPathAndReturnFilename(self,path):
return path.split("/")[-1]
# git commit
def commit(self,repositoryPath,commitFilePath):
logging.info('Commit function called.')
# Finds filename from the incoming path.
filename=self.splitPathAndReturnFilename(commitFilePath)
# If it is a directory, copies it to repository and deletes from current directory.
if os.path.isdir(commitFilePath):
os.popen('cp -r '+commitFilePath+' '+repositoryPath)
#os.popen('rm -rf '+commitFilePath)
# If it is a file, copies it to repository and deletes from current directory.
elif os.path.isfile(commitFilePath):
os.popen('cp '+commitFilePath+' '+repositoryPath)
#os.popen('rm -f '+commitFilePath)
# Adds copied file or directory to the repository, then commits it.
time.sleep(1)
os.popen('git -C '+repositoryPath+' add -f '+filename)
time.sleep(1)
os.popen('git -C '+repositoryPath+' commit -m \"'+filename+'\"')
logging.info('Commit function finished.')
# git push
def push(self,repositoryPath,id,password,url):
logging.info('Push function called.')
# Finds project name by parsing the incoming url.
projectName=self.splitPathAndReturnFilename(url)
# Updates remote repository url by using the incoming github id and password.
#os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/'+projectName+'.git')
#Bilgisayarda calismasi icin
os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/GtuDevOps'+'.git')
time.sleep(1)
# Performs push
#os.popen('git -C '+repositoryPath+' push -u origin master')
#Alternatif olarak
os.popen('git -C '+repositoryPath+' push -f -u origin master')
logging.info('Push function finished.')
# git pull
def pull(self,repositoryPath,id,password,url):
logging.info('Pull function called.')
# Finds project name by parsing the incoming url.
projectName=self.splitPathAndReturnFilename(url)
# Updates remote repository url by using the incoming github id and password.
#os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/'+projectName+'.git')
os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/GtuDevOps'+'.git')
time.sleep(1)
# Performs pull
os.popen('git -C '+repositoryPath+' pull origin master')
logging.info('Pull function finished.')
# git merge
# Hata vermiyor calisip calismadigindan emin degilim
# TODO: Needs test
def merge(self,repositoryPath,id,password,url):
logging.info('Merge function called.')
# Finds project name by parsing the incoming url.
projectName=self.splitPathAndReturnFilename(url)
# Updates remote repository url by using the incoming github id and password.
#os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/'+projectName+'.git')
os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/GtuDevOps'+'.git')
time.sleep(1)
# Performs merge
os.popen('git -C '+repositoryPath+' merge')
logging.info('Merge function finished.')
# git revert
def revert(self,repositoryPath,id,password,url):
logging.info('Revert function called.')
# Finds project name by parsing the incoming url.
projectName=self.splitPathAndReturnFilename(url)
# Updates remote repository url by using the incoming github id and password.
#os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/'+projectName+'.git')
os.popen('git -C '+repositoryPath+' remote set-url origin https://'+id+':'+password+'@github.com/'+id+'/GtuDevOps'+'.git')
time.sleep(1)
# Performs revert
os.popen('git -C '+repositoryPath+' revert -m 1 HEAD')
time.sleep(1)
# Performs commit
os.popen('git -C '+repositoryPath+' commit -m "Revert commit"')
time.sleep(1)
# Performs push
os.popen('git -C '+repositoryPath+' push -f -u origin master')#DIKKAT -f KOYDUM
logging.info('Revert function finished.')
# Parses the coming json file / Gelen json dosyasini parse eder.
def parseJson(self):
logging.info('parseJson called.')
# If the destination is not 'Code Versioning', log the error, then exit from the program.
if(self.comingJson['destination']!='6'):
logging.error('Incoming request destination is not code versioning!')
exit()
# If the request coming from the 'Plan' group and the operation is 'repository_creation'
# create JSON response file in the directory for both request and the commit operation
elif(self.comingJson['origin']=='2' and self.comingJson['operation']=='repository_creation'):
self.sendJson = self.comingJson
self.sendJson['title']='Code versioning request'
self.sendJson['description']='will build'
self.sendJson['destination']='2'
self.sendJson['origin']='6'
self.sendJson['operation']='build'
self.createJsonFileInDirectory()
logging.info('Repository informations are received.')
# If the request coming from the 'Plan' group and the operation is 'commit'
# read data from the response file, perform commit and push, send response back.
elif(self.comingJson['origin']=='2' and self.comingJson['operation']=='commit'):
#onceden cv_response.json olarak yazdigi dosyayi okur
path=os.getcwd()
path=path+'/cv_response.json'
jsonFile=open(path,'r')
self.sendJson = json.load(jsonFile)
if os.path.isdir(self.comingJson['project_path']) or os.path.isfile(self.comingJson['project_path']):
self.pull(self.sendJson['repository_path'],self.sendJson['github_login'],self.sendJson['github_password'],self.sendJson['repository_url'])
self.commit(self.sendJson['repository_path'],self.comingJson['project_path'])
self.push(self.sendJson['repository_path'],self.sendJson['github_login'],self.sendJson['github_password'],self.sendJson['repository_url'])
r = requests.post(url = 'http://localhost:8081', data = json.dumps(self.sendJson))
else:
logging.error('Folder or file is not here!')
exit()
# If the request coming from the 'Plan' group and the operation is 'push'
# read data from the response file, perform push, send response back.
elif(self.comingJson['origin']=='2' and self.comingJson['operation']=='push'):
#onceden cv_response.json olarak yazdigi dosyayi okur
path=os.getcwd()
path=path+'/cv_response.json'
jsonFile=open(path,'r')
self.sendJson =json.load(jsonFile)
self.push(self.sendJson['repository_path'],self.sendJson['github_login'],self.sendJson['github_password'],self.sendJson['repository_url'])
r = requests.post(url = 'http://localhost:8081', data = json.dumps(self.sendJson))
# If the request coming from the 'Plan' group and the operation is 'pull'
# read data from the response file, perform pull.
elif(self.comingJson['origin']=='2' and self.comingJson['operation']=='pull'):
#onceden cv_response.json olarak yazdigi dosyayi okur
path=os.getcwd()
path=path+'/cv_response.json'
jsonFile=open(path,'r')
readJson =json.load(jsonFile)
self.pull(readJson['repository_path'],readJson['github_login'],readJson['github_password'],readJson['repository_url'])
# If the request coming from the 'Plan' group and the operation is 'merge'
# read data from the response file, perform merge.
elif(self.comingJson['origin']=='2' and self.comingJson['operation']=='merge'):
#onceden cv_response.json olarak yazdigi dosyayi okur
path=os.getcwd()
path=path+'/cv_response.json'
jsonFile=open(path,'r')
readJson =json.load(jsonFile)
self.merge(readJson['repository_path'],readJson['github_login'],readJson['github_password'],readJson['repository_url'])
# If the request coming from the 'Plan' group and the operation is 'revert'
# read data from the response file, perform revert.
elif(self.comingJson['origin']=='2' and self.comingJson['operation']=='revert'):
#onceden cv_response.json olarak yazdigi dosyayi okur
path=os.getcwd()
path=path+'/cv_response.json'
jsonFile=open(path,'r')
readJson =json.load(jsonFile)
self.revert(readJson['repository_path'],readJson['github_login'],readJson['github_password'],readJson['repository_url'])
logging.info('parseJson finished.')
# Driver Code
def main(jfile):
#alttaki json datasi mule tarafindan direk verilecek.
#ilk calismasi icin gereken json file
#jfile = open(r'/home/fatihselimyakar/Desktop/CSE343_Code_Versioning/initial_request.json', 'r')
#jfile = open(r'/home/fatihselimyakar/Desktop/CSE343_Code_Versioning/process_file.json', 'r')
cv_obj=code_versioning(jfile)
cv_obj.parseJson()
main(jfile)