/
main.py
108 lines (95 loc) · 3.12 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#coding: utf-8
import json
import urllib
import time
import db
import os
import shutil
serverBasePath = '/home/guessever/Project/cloudcalc/cloudcalc-server'
print '+-------------------------------------+'
print '| |'
print '| CLOUDCALC |'
print '| |'
print '| |'
print '| Current Time |'
print time.strftime('| %Y-%m-%d %H:%M:%S |', time.localtime(time.time()))
print '| |'
print '| |'
print '| Now Running |'
print '| |'
print '| |'
print '+-------------------------------------+\n\n\n\n\n\n'
def calculatingMission():
cid = db.getMissionCid()
if cid != False:
print 'Processing calculating mission: cid = %d ...' % (cid)
db.changeStatus('calculation', 'cid', cid, 'status', 1)
data = db.fetchMissionData(cid)
path = data['path']
inputData = dict()
for item in data['input']:
inputData[item['name']] = item['value']
print 'Copying plugin files into run/'
if os.path.exists('run'):
os.popen('rm -rf run')
shutil.copytree(path, 'run')
print 'Plugin copied.'
filename = 'input_%d.json' % time.time()
print 'Creating input file: %s into run/' % filename
f = open('run/%s' % (filename), 'w')
inputStr = f.write(json.dumps(inputData, ensure_ascii = False).encode('utf-8'))
f.close()
print 'Input file writed.'
print 'Start running program...........'
value = os.system('python run/core/main.py < run/%s > run/output' % (filename)) >> 8
f = open('run/output', 'rb')
output = f.read()
f.close()
print 'Returned value:', value
print 'Output:', output
print 'Deleting folder run/ ....'
os.popen('rm -rf run')
print 'Deleted'
print 'Writing calculation result into MySQL ...'
if value != 0:
output = '[程序返回错误代码:' + str(value) + ']\n' + output
output = urllib.quote_plus(output)
db.writeResult(cid, output)
print 'Writed'
status = value
if value == 0:
status = 2
else:
if value == 1:
status = 3
else:
status = 5
db.changeStatus('calculation', 'cid', cid, 'status', status)
print 'Calculation mission %d completed!' % (cid)
def gittingMission():
pid = db.getGittingPid()
if pid != 0:
print 'Processing gitting mission: pid = %d' % (pid)
db.changeStatus('plugin', 'pid', pid, 'gitStatus', 3)
path, git = db.fetchPluginData(pid)
print 'Git:', git
print 'Folder:', path
if os.path.exists('%s' % (path)):
print 'Path exists, deleting...'
os.popen('rm -rf %s' % (path))
print 'Deleted.'
value = os.system('git clone %s %s' % (git, path)) >> 8
if value == 0:
db.changeStatus('plugin', 'pid', pid, 'gitStatus', 1)
else:
db.changeStatus('plugin', 'pid', pid, 'gitStatus', 4)
Flag = False
while True:
print time.strftime('--------- %Y-%m-%d %H:%M:%S ---------', time.localtime(time.time()))
if Flag:
calculatingMission()
else:
gittingMission()
Flag = not Flag
time.sleep(2)
print '\n\n\n'