-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.py
247 lines (190 loc) · 9.56 KB
/
Worker.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
# -*- coding:ISO8859-1 -*-
import Operations as op
import subprocess as sp
from Package import Package
import NodeManager
from Except import (ScriptTestErr, InstallErr, TestErr, NoDependencyChange)
class Worker():
def __init__(self, reader, version, clone, delete, checkout, checkout_p, aDate):
self.onlyVersion = version
self.delete = delete
self.reader = reader
self.aDate = aDate
self.toClone = clone
self.checkout = checkout
self.checkout_p = checkout_p
if version.__eq__('-1'):
self.oneVersion = False
else:
self.oneVersion = True
# start work
def start(self):
self.currentDirectory = sp.getstatusoutput('pwd')[1] # pwd -> /home/user/path/to/BCDetect
self.fullCSV = self.reader.getFull() # get all versions of csv file
qtdSucess = 0
qtdFail = 0
self.client_name = self.reader.client_name
self.pathName = 'workspace/' + self.client_name
sp.getstatusoutput('mkdir workspace/') # if this path doesnt exists, create
# if --no-clone was specified
if self.toClone:
# clone repository
op.clone(self.reader.urlRepo, self.client_name)
# file to write
writer = open('workspace/' + self.client_name+'_results.csv', 'w')
writer.write("version,dependency_changed,script_test,install,test,node_on_date,node_sucess\n")
self.tags = op.getTags(self.pathName)
# for each version: ['3.5.0', '3.1.0', '1.0.0', '2.1.0' ...]
keys = list(self.fullCSV.keys())
keys.sort() # sort the keys
executed = False # if executed only one version
for version in keys:
# if oneVersion was specified, skip anothers versions
if self.oneVersion and self.onlyVersion.__eq__(version):
executed = True
elif self.oneVersion:
continue # search to specify version
self.release = self.fullCSV[version]# get the release
# initializing all info variables
# ERR is the default value
values = {
'script_test': 'ERR', # has scripts->test
'codeInstall': 'ERR', # if get any err in install
'codeTest': 'ERR', # if get any err in test
'node_on_date': 'ERR', # latest node version in date of release
'node_sucess': 'ERR', # node version that test had sucess
'dependency_changed': 'YES' # if none dependency has changed from the latest release
}
# checkout before get package
op.cleanAndReset(self.client_name, self.currentDirectory)
# change the repository to specify date
op.checkout(self.pathName, self.release, self.tags)
if self.checkout:
op.getHEAD(self.pathName, self.release)
if self.aDate:
op.addDate(self.release, self.pathName+'/package.json')
exit(0)
try:
# get the list of node versions and sort decrescent
package = Package(self.pathName+'/package.json')
versions = self.getListVersions(values, package)
if self.checkout_p:
print(' update package.json')
operation = 'UPDATE-PACKAGE'
op.updatePackage(self.release, package, self.pathName)
exit(0)
# for each version of node before the release date
for version_node in versions:
try:
self.execute(version_node, package, values)
values['node_sucess'] = version_node
except NoDependencyChange as ex: # don't wrong
print('EXC: ' + str(ex).upper())
values['dependency_changed'] = 'NO'
break
except ScriptTestErr as ex: # don't wrong
values['script_test'] = 'ERR'
print('EXC: ' + str(ex).upper())
break
except InstallErr as ex: # npm install wrong
values['codeInstall'] = 'ERR'
print('ERR: ' + str(ex).upper())
# continue to next node version
except TestErr as ex: # npm test wrong
values['codeTest'] = 'ERR'
print('ERR: ' + str(ex).upper())
# continue to next node version
except sp.TimeoutExpired:
break
except Exception as ex:
print("Algum erro inesperado::::::::::::::::::::: " + str(ex))
else:
qtdSucess += 1 # update the count
break # quit of for statement
except FileNotFoundError:
pass # do something
#input()
# delete folder node_modules
if self.delete:
op.deleteCurrentFolder('{0}/node_modules'.format(self.client_name))
# version, dependency_changed, script_test, install, test, node_on_date, node_sucess
# save result
writer.write('{0},{1},{2},{3},{4},{5},{6}\n'.format(self.release.version, values['dependency_changed'], values['script_test'], values['codeInstall'], values['codeTest'], values['node_on_date'], values['node_sucess']))
if executed: # if the specify version are executed
break
writer.close()
if self.delete:
op.deleteCurrentFolder('{0}'.format(self.client_name))
if self.oneVersion and not executed:
print()
print('** VERSION {0} NOT FOUND **'.format(self.onlyVersion))
print()
else:
print("Sucess:", qtdSucess)
print("Fail:", qtdFail)
# check test, install and test with the specify node version
def execute(self, version_node, package, values):
try:
op.printTableInfo(' {0}@{1} {2}--{3} NodeJs-{4} '.format(self.client_name, self.release, self.release.client_timestamp, self.release.client_previous_timestamp, version_node))
# verify if any dependency has changed
operation = 'DEP-CHANGE'
self.release.verifyDependencyChange(self.oneVersion)
values['dependency_changed'] = 'YES'
#operation = 'COMMIT'
#op.commitAll(self.client_name, self.currentDirectory)
operation = 'RESET'
op.cleanAndReset(self.client_name, self.currentDirectory)
# change the repository to specify date
operation = 'CHECKOUT'
op.checkout(self.pathName, self.release, self.tags)
#input()
# verify if package.json has test
operation = 'VERIFY-TEST'
op.verifyTest(package, self.onlyVersion)
values['script_test'] = 'OK'
print(' update package.json')
operation = 'UPDATE-PACKAGE'
op.updatePackage(self.release, package, self.pathName)
# close package.json and save changes
package.save()
# install all dependencies and test in specify version package
operation = 'INSTALL'
if not values['codeInstall'].__eq__('OK'):
op.npmInstall(self.pathName, version_node, self.client_name)
values['node_sucess'] = version_node # version which install has sucess
values['codeInstall'] = 'OK'
operation = 'TEST'
op.npmTest(self.pathName, version_node)
values['codeTest'] = 'OK'
except FileNotFoundError as ex: # se não foi possível abrir o package.json
raise
except sp.TimeoutExpired as ex: # se a o teste ou o install demorar mais que 10 minutos
raise
except Exception as ex:
print('------------\n{0}: ERR: {1}\n------------\n'.format(operation, str(ex)))
raise
# return the list of versions node to execute
# in future, this will be changed to return the specify version of node
def getListVersions(self, values, package):
# get all node versions
versionsNode = list(NodeManager.nodeVersions.values())
versionsNode.sort()
versionsNode.reverse()
# get the node version based in date
values['node_on_date'] = NodeManager.getVersionOnDate(self.release.client_timestamp)
# get only versions to execute
posCurrentVersion = versionsNode.index(values['node_on_date'])
versions = versionsNode[posCurrentVersion:] # get only versions to execute
# try get the version in package.json engines->node
# if this versions doesn't exists in versions list, insert
try:
versionEngines = NodeManager.getVersionOnPackage(package)
if not versions.__contains__(versionEngines):
versions.insert(0, versionEngines)
except (KeyError, AttributeError, TypeError):
pass # do nothing
# try with the lattest
if not versions.__contains__('10.12.0'):
versions.insert(0, '10.12.0')
op.printTableInfo('NodeJS versions to run in {0}: {1}'.format(self.client_name, str(versions)))
return versions