forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 6
/
InteractiveRun.py
124 lines (103 loc) · 3.98 KB
/
InteractiveRun.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2023/02/01
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: [email protected]
@file: InteractiveRun.py
@description:
"""
import os
import sys
try:
import chardet
except ImportError:
print('chardet not found')
try:
from PyQt5.QtCore import QProcess
from PyQt5.QtWidgets import (QApplication, QLineEdit, QPushButton,
QTextBrowser, QVBoxLayout, QWidget)
except ImportError:
from PySide2.QtCore import QProcess
from PySide2.QtWidgets import (QApplication, QLineEdit, QPushButton,
QTextBrowser, QVBoxLayout, QWidget)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
command = 'ping www.baidu.com'
if sys.platform.startswith('win'):
command = 'ping -n 5 www.baidu.com'
elif sys.platform.startswith('darwin') or sys.platform.startswith(
'linux'):
command = 'ping -c 5 www.baidu.com'
else:
raise RuntimeError('Unsupported platform: %s' % sys.platform)
self.cmdEdit = QLineEdit(command, self)
layout.addWidget(self.cmdEdit)
self.buttonRun = QPushButton('执行命令', self)
layout.addWidget(self.buttonRun)
self.buttonRun.clicked.connect(self.run_command)
self.resultView = QTextBrowser(self)
layout.addWidget(self.resultView)
self._cmdProcess = None
self._init()
def closeEvent(self, event):
if self._cmdProcess:
self._cmdProcess.writeData('exit'.encode() + os.linesep.encode())
self._cmdProcess.waitForFinished()
if self._cmdProcess:
self._cmdProcess.terminate()
super(Window, self).closeEvent(event)
def _init(self):
if self._cmdProcess:
return
# 打开终端shell
self._cmdProcess = QProcess(self)
self._cmdProcess.setProgram(
'cmd' if sys.platform.startswith('win') else 'bash')
# 合并输出流和错误流,只从标准输出流读取数据
self._cmdProcess.setProcessChannelMode(QProcess.MergedChannels)
self._cmdProcess.started.connect(self.on_started)
self._cmdProcess.finished.connect(self.on_finished)
self._cmdProcess.errorOccurred.connect(self.on_error)
self._cmdProcess.readyReadStandardOutput.connect(
self.on_readyReadStandardOutput)
self._cmdProcess.start()
def run_command(self):
self._init()
command = self.cmdEdit.text().strip()
if not command:
return
command = command.encode(sys.getdefaultencoding()) + os.linesep.encode(
sys.getdefaultencoding())
self._cmdProcess.writeData(command)
def on_started(self):
self.resultView.append('ping process started, pid: %s' %
self._cmdProcess.processId())
def on_finished(self, exitCode, exitStatus):
print('ping process finished, exitCode: %s, exitStatus: %s' %
(exitCode, exitStatus))
self._cmdProcess.kill()
self._cmdProcess = None
def on_error(self, error):
self.resultView.append('ping process error: %s, message: %s' %
(error, self._cmdProcess.errorString()))
self._cmdProcess.kill()
self._cmdProcess = None
def on_readyReadStandardOutput(self):
# 读取已有结果
result = self._cmdProcess.readAllStandardOutput().data()
try:
encoding = chardet.detect(result)
self.resultView.append(result.decode(encoding['encoding']))
except Exception:
self.resultView.append(result.decode('utf-8', errors='ignore'))
if __name__ == '__main__':
import cgitb
cgitb.enable(format='text')
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())