-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainArea.py
executable file
·708 lines (636 loc) · 27 KB
/
MainArea.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys, os
import glob
from PySide.QtCore import *
from PySide.QtGui import *
from paramiko import SSHClient, AutoAddPolicy
import zipfile
import shutil
import time
from yelib.qt.layout import *
from yelib.qt.widgets import *
from yelib.task import *
from yelib.util import *
from SettingsDlg import decrypt
def dpath(app, path):
return os.path.join("data", app, path)
class MainArea(QWidget):
def __init__(self, parent=None):
super(MainArea, self).__init__(parent)
self.settings = parent.dlgSettings
# ==== File List ====
# Application Combo
self.cboApp = QComboBox()
self.cboApp.activated.connect(self.selectApp)
self.txtSrv = QLineEdit()
self.txtSrv.setReadOnly(True)
self.txtSrcRoot = QLineEdit()
self.txtSrcRoot.setReadOnly(True)
self.txtSrcRoot.setMinimumWidth(350)
self.txtTgRoot = QLineEdit()
self.txtTgRoot.setReadOnly(True)
self.txtTgRoot.setMinimumWidth(350)
# Main Group
self.grpMain = QGroupBox('Work Area')
tb = QTableWidget()
tb.setColumnCount(5)
tb.setSelectionBehavior(QAbstractItemView.SelectRows)
tb.setEditTriggers(QAbstractItemView.NoEditTriggers)
tb.setHorizontalHeaderLabels(("Check", "Icon", "Status", "File", "FullFile"))
tb.horizontalHeader().setResizeMode(0, QHeaderView.Fixed)
tb.horizontalHeader().setResizeMode(1, QHeaderView.Fixed)
tb.horizontalHeader().setResizeMode(3, QHeaderView.Stretch)
tb.horizontalHeader().resizeSection(0, 30)
tb.horizontalHeader().resizeSection(1, 25)
tb.setColumnHidden(2, True)
tb.setColumnHidden(4, True)
tb.horizontalHeader().hide()
tb.verticalHeader().hide()
tb.setAlternatingRowColors(True)
self.lstFiles = tb
ltMain = yBoxLayout([
[
yGridLayout([
[ 'App', self.cboApp ],
[ 'Server', self.txtSrv ],
]),
None,
yGridLayout([
[ 'Source Root', self.txtSrcRoot ],
[ 'Target Root', self.txtTgRoot ],
]),
],
[ self.lstFiles ],
])
self.grpMain.setLayout(ltMain)
self.grpMain.setMinimumSize(700, 400)
# ==== Main ====
# ==== Log ====
self.grpLog = QGroupBox(u'Information of Execution')
self.txtLog = QTextBrowser()
self.txtLog.setReadOnly(True)
self.txtLog.setMinimumHeight(150)
self.txtLog.setOpenExternalLinks(True)
ltLog = yBoxLayout([
[ self.txtLog ],
])
self.grpLog.setLayout(ltLog)
self.grpLog.setSizePolicy(
QSizePolicy.Policy(QSizePolicy.Preferred),
QSizePolicy.Policy(QSizePolicy.Fixed),
)
# ==== Log ====
# ==== Buttons ====
self.btnGetStatus = QPushButton(QIcon('image/refresh.png'), u'Get Status')
self.btnGetStatus.setFixedSize(110, 28)
self.btnMakeDiff = QPushButton(QIcon('image/game-chip.png'), u'Make Diff')
self.btnMakeDiff.setFixedSize(110, 28)
self.btnDeployNew = QPushButton(QIcon('image/organization.png'), u'Deploy New')
self.btnDeployNew.setFixedSize(110, 28)
self.chkBackup = QCheckBox('Backup')
self.btnRestore = QPushButton(QIcon('image/undo.png'), u'Restore')
self.btnRestore.setFixedSize(110, 28)
# ==== Buttons ====
# ==== Main Layout ====
#self.btnExit = QPushButton(u'Exit')
self.txtBugId = QLineEdit(self.settings.conf('diff', 'last bug'))
self.txtBugId.setFixedWidth(100)
lt = yBoxLayout([
[ self.grpMain ],
[ self.btnGetStatus, None,
QLabel('BugId'), self.txtBugId, self.btnMakeDiff, None,
self.btnDeployNew, self.chkBackup, self.btnRestore ],
[ self.grpLog ],
])
#self.btnExit.clicked.connect(self.close)
self.setLayout(lt)
self.updateAppList()
# ==== Main Layout ====
self.statusIcons = {
'A': QIcon('image/file_add.png'),
'M': QIcon('image/file_edit.png'),
'D': QIcon('image/file_delete.png'),
}
self.worker = TaskWorker()
self.btnGetStatus.clicked.connect(self.getStatus)
self.btnMakeDiff.clicked.connect(self.makeDiff)
self.btnDeployNew.clicked.connect(self.deployNew)
self.btnRestore.clicked.connect(self.restoreFiles)
self.workdir = os.getcwdu()
self.targetFile = None
def appendLog(self, log, print_output=False):
if log.type == OutputType.NOTIFY:
return
if log.type == OutputType.OUTPUT:
if print_output:
self.txtLog.append(unicode(logtext))
else:
self.txtLog.append(log.formatted_html())
def echoMsg(self, msg):
(yield TaskOutput(msg))
def taskHandler(self, taskmsg, loading=None, btn=None, finalword=None):
pt = self.parent()
if taskmsg.type == OutputType.NOTIFY:
if taskmsg.output == u'ENTER':
if btn: btn.setDisabled(True)
if loading: pt.showLoading(loading, True)
elif taskmsg.output.startswith('EXIT '):
code = int(taskmsg.output.split()[1])
if code == 0 and finalword:
self.appendLog(TaskOutput(finalword))
if loading: pt.showLoading('', False)
if btn: btn.setDisabled(False)
return None
self.appendLog(taskmsg)
return taskmsg.type == OutputType.OUTPUT and taskmsg.output or None
def selectApp(self, idx):
sect = self.cboApp.itemText(idx)
st = self.settings
self.txtSrv.setText(st.conf(sect, 'server'))
self.txtSrcRoot.setText(st.conf(sect, 'source root'))
self.txtTgRoot.setText(st.conf(sect, 'target root'))
def updateAppList(self):
st = self.settings
applist = st.conf('app', 'list').split(',')
self.cboApp.clear()
for app in applist:
self.cboApp.addItem(app.strip())
if self.cboApp.count() > 0:
self.selectApp(0)
def defaultHandler(self, msg):
if msg.type == OutputType.NOTIFY and msg.output.startswith('EXIT '):
code = int(msg.output.split()[1])
if code != 0:
self.worker.stop_task()
self.appendLog(msg)
def getStatus(self):
def begin():
self.btnGetStatus.setDisabled(True)
self.parent().showLoading(u'Getting svn status ... ', True)
tb = self.lstFiles
for i in xrange(tb.rowCount()):
tb.removeRow(0)
def end():
self.parent().showLoading('', False)
self.btnGetStatus.setDisabled(False)
srcdir = self.txtSrcRoot.text()
if srcdir == "":
self.appendLog(TaskOutput(u'!!! Please set source root !!!', OutputType.WARN))
return
task = Task(CmdTask([os.path.join("bin", "svndiff"), "-c", "-s", srcdir]))
task.init(
TaskHandler(begin), TaskHandler(end),
TaskHandler(self.getStatusHandler),
TaskHandler(self.defaultHandler)
)
self.worker.add_task(task)
def getStatusHandler(self, msg):
if msg.type != OutputType.OUTPUT:
return
tb = self.lstFiles
m = msg.output.split()
if m[0] not in self.statusIcons:
self.appendLog(
TaskOutput(u'Not recognized flag: %s' % m[0],
OutputType.ERROR)
)
return
n = tb.rowCount()
tb.insertRow(n)
item = QTableWidgetItem()
item.setCheckState(Qt.Checked)
tb.setItem(n, 0, item)
tb.setItem(n, 1, QTableWidgetItem(self.statusIcons[m[0]], m[0]))
tb.setItem(n, 2, QTableWidgetItem(m[0]))
tb.setItem(n, 3, QTableWidgetItem(m[1]))
tb.setItem(n, 4, QTableWidgetItem(m[2]))
def makeDiff(self):
st = self.settings
app = self.cboApp.itemText(self.cboApp.currentIndex())
hdiff = dpath(app, 'hdiff')
def begin():
self.btnMakeDiff.setDisabled(True)
self.parent().showLoading(u'Making diff files ... ', True)
def end():
self.parent().showLoading('', False)
self.btnMakeDiff.setDisabled(False)
if self.txtBugId.text() == "":
self.appendLog(TaskOutput(u"!!! Please input Bug Id !!!", OutputType.WARN))
return
srcdir = self.txtSrcRoot.text()
files = []
tb = self.lstFiles
for i in xrange(tb.rowCount()):
item = tb.item(i, 0)
if item.checkState() == Qt.Checked:
files.append(tb.item(i, 4).text())
cmds = [
os.path.join("bin", "svndiff"),
"-s", srcdir, "-d", hdiff,
"-f", os.path.join("bin", "diff"),
"-v", os.path.join("bin", "svn"),
"-t", os.path.join("html", "diff_template.html"),
] + files
task = Task(CmdTask(cmds))
task.init(
TaskHandler(begin), TaskHandler(end),
TaskHandler(self.defaultHandler)
)
path = os.path.join(os.getcwdu(), hdiff).replace(os.sep, '/')
task.put(self.echoMsg(
u"Go to <a href='{}' style='color:dodgerblue;'>HDIFF Directory</a> "
"to check the result.".format(path)
))
st = self.settings
bugid = self.txtBugId.text()
rmtdir = st.conf('diff', 'remote dir')
svnid = st.conf('svn', 'username')
httpurl = st.conf('diff', 'http url')
if svnid == "":
svnid = "yanpeng.wang"
result_url = "{}/{}/{}".format(httpurl.rstrip('/'), svnid, bugid)
rmtdir = os.path.join(rmtdir, svnid, bugid).replace(os.sep, '/')
sshargs = {
'hostname': st.conf('diff', 'server'),
'username': st.conf('diff', 'username'),
'password': decrypt(st.conf('diff', 'password')),
'timeout' : 10,
'compress': True,
}
task.put(self._uploadDiffs(rmtdir, sshargs))
task.put(self.echoMsg(
u"Click <a href='{0}' style='color:dodgerblue;'>{0}</a> "
u"to review the result.".format(result_url),
))
self.worker.add_task(task)
def _uploadDiffs(self, dstdir, sshargs):
st = self.settings
app = self.cboApp.itemText(self.cboApp.currentIndex())
hdiff = dpath(app, 'hdiff')
sshcli = SSHClient()
sftpcli = None
code = 0
try:
if not (yield TaskOutput(u'Connecting to %s ...' % sshargs['hostname'])):
raise CommandTerminated()
sshcli.set_missing_host_key_policy(AutoAddPolicy())
sshcli.connect(**sshargs)
if not (yield TaskOutput(u'Connected, ready to upload ...')):
raise CommandTerminated()
ret = sshcli.exec_command("[ -d {0} ] && rm -rf {0}; mkdir -p {0}".format(dstdir))
errstr = ret[2].read()
if errstr != '':
raise Exception(errstr)
sftpcli = sshcli.open_sftp()
for f in os.listdir(hdiff):
if f.lower().endswith('.html'):
localfile = os.path.join(hdiff, f)
remotefile = os.path.join(dstdir, f).replace(os.sep, '/')
if not (yield TaskOutput(u'Uploading %s ...' % f)):
raise CommandTerminated()
sftpcli.put(localfile, remotefile)
except CommandTerminated:
code = -2
(yield TaskOutput(u'Uploading Terminited', OutputType.WARN))
except Exception as ex:
code = -1
(yield TaskOutput(ex.message, OutputType.ERROR))
finally:
if sftpcli: sftpcli.close()
if sshcli: sshcli.close()
(yield TaskOutput(u'EXIT %d' % code, OutputType.NOTIFY))
def _copyfile(self, ssh, sftp, from_file, to_file, method='get', overwrite=True):
if not sftp or method == 'get':
if os.path.isdir(to_file):
fname = os.path.basename(from_file)
to_dir = to_file
to_file = os.path.join(to_file, fname)
else:
to_dir = os.path.dirname(to_file)
if not overwrite and os.path.exists(to_file):
return
if not os.path.exists(to_dir):
mkdir_p(to_dir)
if sftp:
if method == 'put':
to_dir = os.path.dirname(to_file)
_,stdout,stderr = ssh.exec_command('mkdir -p ' + to_dir)
stdout.read()
errmsg = stderr.read()
if errmsg: raise Exception(errmsg)
sftp.put(from_file, to_file)
else:
sftp.get(from_file, to_file)
else:
try:
shutil.copy(from_file, to_file)
except Exception as ex:
raise ex
def _copyfiles(self, ssh, sftp, wildcard, to_dir, method='get', overwrite=True):
files = []
if sftp:
dn = os.path.dirname(wildcard)
bn = os.path.basename(wildcard)
_, stdout, stderr = ssh.exec_command(
"find %s -maxdepth 1 -type f -name %s" % (dn, bn)
)
files = stdout.read().split()
errmsg = stderr.read()
if errmsg: raise Exception(errmsg)
else:
files = glob.glob(wildcard)
mkdir_p(to_dir)
#for fn in files:
# self._copyfile(ssh, sftp, fn, to_dir, method, overwrite)
if len(files) == 1:
self._copyfile(ssh, sftp, files[0], to_dir, method, overwrite)
# data/appname
# backup
# |_ other files
# |_ WEB-INF/classes/com/.../*.class
# \_ WEB-INF/lib/*.jar
# approot ----------- Stores all new stuff to be deployed
# |_ other files
# |_ WEB-INF/classes/com/.../*.class
# \_ WEB-INF/lib/*.jar
# clstemp ----------- new classes to be put in jar
# \_ com/.../*.class
def deployNew(self):
st = self.settings
app = self.cboApp.itemText(self.cboApp.currentIndex())
sshcli = SSHClient()
hostname = st.conf(app, 'server')
sshargs = {}
if hostname not in ('localhost', '127.0.0.1'):
sshargs = {
'hostname': hostname,
'username': st.conf(app, 'username'),
'password': decrypt(st.conf(app, 'password')),
'timeout' : 10,
'compress': True,
}
def begin():
self.btnDeployNew.setDisabled(True)
self.parent().showLoading(u'Deploying Target File ... ', True)
def end():
if sshcli: sshcli.close()
self.parent().showLoading('', False)
self.btnDeployNew.setDisabled(False)
try:
clstemp = dpath(app, 'clstemp')
approot = dpath(app, 'approot')
force_rmdir(approot)
force_rmdir(clstemp)
mkdir_p(approot)
mkdir_p(clstemp)
except Exception as ex:
self.appendLog(TaskOutput(ex.message, OutputType.ERROR))
return
task = Task(self._deployNew(st, app, sshargs, sshcli))
task.init(
TaskHandler(begin), TaskHandler(end),
TaskHandler(self.defaultHandler),
)
self.worker.add_task(task)
# def deployNewHandler(self, msg):
# if msg.type == OutputType.NOTIFY and msg.output.startswith('EXIT '):
# code = int(msg.output.split()[1])
# if code == 0:
# self.worker.
# Fetch and backup files
def _deployNew(self, st, app, sshargs, sshcli):
tb = self.lstFiles
code = 0
srcroot = st.conf(app, 'source root')
webappdir = st.conf(app, 'webapp dir')
javadir = st.conf(app, 'java dir')
clsdir = st.conf(app, 'class dir')
tgroot = st.conf(app, 'target root')
tgsep = '/'
tgclsdir = st.conf(app, 'target class dir')
bakdir = dpath(app, 'backup')
approot = dpath(app, 'approot')
clsitems = [] # items found in class dir or non-java-file
sftpcli = None
try:
if sshargs:
self.appendLog(TaskOutput(u'Conntecting to %s ...' % sshargs['hostname']))
sshcli.set_missing_host_key_policy(AutoAddPolicy())
sshcli.connect(**sshargs)
sftpcli = sshcli.open_sftp()
self.appendLog(TaskOutput(u'Connected'))
items = [ tb.item(i,3).text() for i in xrange(tb.rowCount())
if tb.item(i,0).checkState() == Qt.Checked ]
# === Fetch and backup files ====
# backup class files, and copy them to approot dir
if not (yield TaskOutput(u'Fetching, making backup and copying files ... ')):
raise CommandTerminated()
for fn in items:
if fn.endswith('.java'):
fn = fn.replace(javadir.replace(os.sep, '/'), '').lstrip('/')
_clsfn = fn[0:-5]+'.class'
clsfn = os.path.join(tgclsdir, _clsfn).replace(os.sep, tgsep)
tgt_f = os.path.join(tgroot, clsfn).replace(os.sep, tgsep)
bak_f = os.path.join(bakdir, clsfn)
new_f = os.path.join(srcroot, clsdir, _clsfn)
tmp_f = os.path.join(approot, clsfn)
clsitems.append(_clsfn)
else:
fn = fn.replace(webappdir.replace(os.sep, '/'), '').lstrip('/')
tgt_f = os.path.join(tgroot, fn).replace(os.sep, tgsep)
bak_f = os.path.join(bakdir, fn)
new_f = os.path.join(srcroot, webappdir, fn)
tmp_f = os.path.join(approot, fn)
try:
overwrite = (self.chkBackup.checkState() == Qt.Checked and True or False)
self._copyfile(sshcli, sftpcli, tgt_f, bak_f, overwrite=overwrite)
self._copyfile(None, None, new_f, tmp_f)
except IOError as ex: # No file found
if ex.args[0] != 2:
raise ex
if not (yield TaskOutput(u'Copying %s ... ' % tgt_f)):
raise CommandTerminated()
self.worker.add_step(self._updateNew(st, app, sshcli, sftpcli, clsitems))
except CommandTerminated:
code = -2
(yield TaskOutput(u'Fetching Terminited', OutputType.WARN))
except Exception as ex:
code = -1
(yield TaskOutput(repr(ex), OutputType.ERROR))
finally:
(yield TaskOutput(u'EXIT %d' % code, OutputType.NOTIFY))
def _updateNew(self, st, app, sshcli, sftpcli, clsitems):
# 1. backup jar files
# 2. copy them to approot dir
# 3. will be updated with clstemp fileslater
code = 0
srcroot = st.conf(app, 'source root')
clsdir = st.conf(app, 'class dir')
tgroot = st.conf(app, 'target root')
bakdir = dpath(app, 'backup')
clstemp = dpath(app, 'clstemp')
approot = dpath(app, 'approot')
tgclsdir = st.conf(app, 'target class dir')
tgjarfile = st.conf(app, 'target jar file')
jardir = os.path.dirname(tgjarfile)
bakjardir = os.path.join(bakdir, jardir)
newjardir = os.path.join(approot, jardir)
tgsep = '/'
jaritems = {}
try:
overwrite = (self.chkBackup.checkState() == Qt.Checked and True or False)
self._copyfiles(sshcli, sftpcli,
os.path.join(tgroot, tgjarfile).replace(os.sep, tgsep),
bakjardir, overwrite=overwrite)
self._copyfiles(None, None,
os.path.join(bakdir, tgjarfile),
newjardir)
if not (yield TaskOutput(u'Copying new class files ... ')):
raise CommandTerminated()
# copy new class files to temp dir, for updating jar file above
for jar in os.listdir(newjardir):
zf = zipfile.ZipFile(os.path.join(newjardir, jar))
zflist = zf.namelist()
jaritems[jar] = []
for fn in clsitems:
if fn in zflist:
new_f = os.path.join(srcroot, clsdir, fn)
tmp_f = os.path.join(clstemp, fn)
self._copyfile(None, None, new_f, tmp_f)
jaritems[jar].append(fn) # clsss in which jarfile
(yield TaskOutput((jar, fn), OutputType.OUTPUT))
if not (yield TaskOutput(u'Updating target files ... ')):
raise CommandTerminated()
for jar, files in jaritems.items():
if len(files) == 0:
continue
jar_bin = self.settings.conf('app', 'jar bin')
args = [ os.path.join(jar_bin), "-uf",
os.path.join("../approot", jardir, jar)
] + files
self.worker.add_step(CmdTask(args, clstemp))
for fn in clsitems:
notjaritem = True
for v in jaritems.values():
if fn in v:
notjaritem = False
break
if notjaritem:
self._copyfile(None, None,
os.path.join(srcroot, clsdir, fn),
os.path.join(approot, tgclsdir, fn)
)
self.worker.add_step(self._uploadNew(st, app, sshcli, sftpcli, 'approot'))
except CommandTerminated:
code = -2
(yield TaskOutput(u'Updating Terminited', OutputType.WARN))
except Exception as ex:
code = -1
(yield TaskOutput(repr(ex), OutputType.ERROR))
finally:
(yield TaskOutput(u'EXIT %d' % code, OutputType.NOTIFY))
def _uploadNew(self, st, app, sshcli, sftpcli, rootpath):
tgroot = st.conf(app, 'target root')
uproot = dpath(app, rootpath)
tgsep = '/'
code = 0
startcmd = st.conf(app, 'startup')
stopcmd = st.conf(app, 'shutdown')
try:
if stopcmd:
if not (yield TaskOutput(u'Stopping service ... ')):
raise CommandTerminated()
if sftpcli:
_,stdout,stderr = sshcli.exec_command(stopcmd)
stdout.read()
errmsg = stderr.read()
if errmsg:
raise Exception(errmsg)
else:
os.system(stopcmd)
for r,_,files in os.walk(uproot):
for f in files:
srcfile = os.path.join(r, f)
root = r.replace(uproot, '').lstrip(os.sep)
tgfile = os.path.join(tgroot, root, f).replace(os.sep, tgsep)
if not (yield TaskOutput(u'Uploading %s ... ' % srcfile)):
raise CommandTerminated()
self._copyfile(sshcli, sftpcli, srcfile, tgfile, method='put')
if startcmd:
if not (yield TaskOutput(u'Starting service ... ')):
raise CommandTerminated()
if sftpcli:
sshcli.exec_command(startcmd)
stdout.read()
errmsg = stderr.read()
if errmsg:
raise Exception(errmsg)
else:
os.system(startcmd)
(yield TaskOutput(u'Files Uploaded'))
except CommandTerminated:
code = -2
(yield TaskOutput(u'Uploading Terminited', OutputType.WARN))
except Exception as ex:
code = -1
(yield TaskOutput(repr(ex), OutputType.ERROR))
finally:
(yield TaskOutput(u'EXIT %d' % code, OutputType.NOTIFY))
def restoreFiles(self):
sshcli = SSHClient()
def begin():
self.btnRestore.setDisabled(True)
self.parent().showLoading(u'Restoring files ... ', True)
def end():
if sshcli: sshcli.close()
self.btnRestore.setDisabled(False)
self.parent().showLoading('', False)
task = Task(self._restoreFiles(sshcli))
task.init(
TaskHandler(begin), TaskHandler(end),
TaskHandler(self.defaultHandler),
)
self.worker.add_task(task)
def _restoreFiles(self, sshcli):
st = self.settings
app = self.cboApp.itemText(self.cboApp.currentIndex())
code = 0
bakdir = dpath(app, 'backup')
hostname = st.conf(app, 'server')
sftpcli = None
sshargs = {}
if hostname not in ('localhost', '127.0.0.1'):
sshargs = {
'hostname': hostname,
'username': st.conf(app, 'username'),
'password': decrypt(st.conf(app, 'password')),
'timeout' : 10,
'compress': True,
}
try:
if sshargs:
if not (yield TaskOutput(u'Connecting to %s ...' % sshargs['hostname'])):
raise CommandTerminated()
sshcli.set_missing_host_key_policy(AutoAddPolicy())
sshcli.connect(**sshargs)
if not (yield TaskOutput(u'Connected, ready to upload ...')):
raise CommandTerminated()
sftpcli = sshcli.open_sftp()
self.worker.add_step(self._uploadNew(st, app, sshcli, sftpcli, 'backup'))
except Exception as ex:
code = -1
(yield TaskOutput(repr(ex), OutputType.ERROR))
finally:
(yield TaskOutput(u'EXIT %d' % code, OutputType.NOTIFY))
def closeEvent(self, event):
#for i in xrange(self.tab.count()):
# self.tab.widget(i).close()
self.worker.stop()
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
win.center()
sys.exit(app.exec_())