mirror of
https://github.com/GNS3/gns3-gui.git
synced 2026-05-17 00:46:01 +03:00
Migration to QT5
I drop some related cloud support, we will restore it when we will be ready. * Switch to Python3 super style * Drop old object inheritance * Fixed old super call
This commit is contained in:
@@ -9,17 +9,36 @@ import sys
|
||||
import stat
|
||||
import shutil
|
||||
import subprocess
|
||||
import re
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--qt4", help="compilation with QT4", action="store_true")
|
||||
parser.add_argument("--force", help="force rebuild all files", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
if sys.platform.startswith('win'):
|
||||
PATH = os.path.join(os.path.dirname(sys.executable), "Lib/site-packages/PyQt4")
|
||||
if os.access(os.path.join(PATH, "bin"), os.R_OK):
|
||||
PATH = os.path.join(PATH, "bin")
|
||||
PYUIC4 = os.path.join(PATH, "pyuic4")
|
||||
PYRCC4 = os.path.join(PATH, "pyrcc4")
|
||||
if args.qt4:
|
||||
if sys.platform.startswith('win'):
|
||||
PATH = os.path.join(os.path.dirname(sys.executable), "Lib/site-packages/PyQt4")
|
||||
if os.access(os.path.join(PATH, "bin"), os.R_OK):
|
||||
PATH = os.path.join(PATH, "bin")
|
||||
PYUIC = os.path.join(PATH, "pyuic4")
|
||||
PYRCC = os.path.join(PATH, "pyrcc4")
|
||||
else:
|
||||
PYUIC = shutil.which("pyuic4")
|
||||
PYRCC = shutil.which("pyrcc4")
|
||||
PYRCC_PYTHON3_FLAG = "-py3"
|
||||
else:
|
||||
PYUIC4 = shutil.which("pyuic4")
|
||||
PYRCC4 = shutil.which("pyrcc4")
|
||||
if sys.platform.startswith('win'):
|
||||
PATH = os.path.join(os.path.dirname(sys.executable), "Lib/site-packages/PyQt5")
|
||||
if os.access(os.path.join(PATH, "bin"), os.R_OK):
|
||||
PATH = os.path.join(PATH, "bin")
|
||||
PYUIC = os.path.join(PATH, "pyuic5")
|
||||
PYRCC = os.path.join(PATH, "pyrcc5")
|
||||
else:
|
||||
PYUIC = shutil.which("pyuic5")
|
||||
PYRCC = shutil.which("pyrcc5")
|
||||
PYRCC_PYTHON3_FLAG = None
|
||||
|
||||
|
||||
def build_ui(path):
|
||||
@@ -27,10 +46,13 @@ def build_ui(path):
|
||||
source = os.path.join(path, file)
|
||||
if source.endswith(".ui"):
|
||||
target = os.path.join(path, file.replace(".ui", "_ui.py"))
|
||||
if not os.access(target, os.F_OK) or (os.stat(source)[stat.ST_MTIME] > os.stat(target)[stat.ST_MTIME]):
|
||||
command = [PYUIC4, "--from-imports", "-o", target, source]
|
||||
if not os.access(target, os.F_OK) or (os.stat(source)[stat.ST_MTIME] > os.stat(target)[stat.ST_MTIME]) or args.force:
|
||||
command = [PYUIC, "--from-imports", "-o", target, source]
|
||||
print("Building UI {}".format(source))
|
||||
if args.force and os.access(target, os.F_OK):
|
||||
os.remove(target)
|
||||
subprocess.call(command)
|
||||
patch_file_qt4_5(target)
|
||||
|
||||
|
||||
def build_resources(path, target):
|
||||
@@ -38,10 +60,30 @@ def build_resources(path, target):
|
||||
source = os.path.join(path, file)
|
||||
if source.endswith(".qrc"):
|
||||
target = os.path.join(target, file.replace(".qrc", "_rc.py"))
|
||||
if not os.access(target, os.F_OK) or (os.stat(source)[stat.ST_MTIME] > os.stat(target)[stat.ST_MTIME]):
|
||||
command = [PYRCC4, "-py3", "-compress", "9", "-o", target, source]
|
||||
if not os.access(target, os.F_OK) or (os.stat(source)[stat.ST_MTIME] > os.stat(target)[stat.ST_MTIME]) or args.force:
|
||||
if PYRCC_PYTHON3_FLAG is not None:
|
||||
command = [PYRCC, PYRCC_PYTHON3_FLAG, "-compress", "9", "-o", target, source]
|
||||
else:
|
||||
command = [PYRCC, "-compress", "9", "-o", target, source]
|
||||
print("Building resources {}".format(source))
|
||||
if args.force and os.access(target, os.F_OK):
|
||||
os.remove(target)
|
||||
subprocess.call(command)
|
||||
patch_file_qt4_5(target)
|
||||
|
||||
|
||||
def patch_file_qt4_5(target):
|
||||
"""
|
||||
Patch a file for supporting Qt4 and Qt5
|
||||
"""
|
||||
# We patch the file in order to support both version of Qt
|
||||
out = ""
|
||||
print("Patch {} for Qt4 and Qt5 support".format(target))
|
||||
with open(target) as f:
|
||||
for line in f.readlines():
|
||||
out += re.sub(r"^from PyQt[45] ", "from gns3.qt ", line)
|
||||
with open(target, 'w') as f:
|
||||
f.write(out)
|
||||
|
||||
|
||||
def recursive(function, path):
|
||||
@@ -52,8 +94,8 @@ def recursive(function, path):
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if not PYUIC4 or not PYRCC4:
|
||||
raise RuntimeError("pyuic4 or pyrcc4 could't be found, please install PyQt4 development tools")
|
||||
if not PYUIC or not PYRCC:
|
||||
raise RuntimeError("pyuic5 or pyrcc5 could't be found, please install PyQt5 development tools")
|
||||
|
||||
cwd = os.path.dirname(os.path.abspath(__file__))
|
||||
gns3_path = os.path.abspath(os.path.join(cwd, "../gns3/"))
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
|
||||
set -e
|
||||
|
||||
SIP=4.16.5
|
||||
PYQT=4.11.3
|
||||
SIP=4.16.7
|
||||
PYQT=5.4.1
|
||||
|
||||
echo "Install sip $SIP and PyQT $PYQT"
|
||||
|
||||
@@ -54,5 +54,5 @@ fi
|
||||
cd PyQt-x11-gpl-${PYQT}
|
||||
sudo make install
|
||||
|
||||
python -c 'import PyQt4' # Check if it's ok
|
||||
python -c 'import PyQt5' # Check if it's ok
|
||||
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
"""
|
||||
This script can be used to ssh to a cloud server started by GNS3. It copies
|
||||
the ssh keys for a server to a temp file on disk and starts ssh using the
|
||||
keys.
|
||||
|
||||
Right now it only connects to the first cloud server listed in the config
|
||||
file.
|
||||
"""
|
||||
|
||||
import getopt
|
||||
import os
|
||||
import sys
|
||||
|
||||
from PyQt4 import QtCore, QtGui
|
||||
|
||||
|
||||
SCRIPT_NAME = os.path.basename(__file__)
|
||||
|
||||
|
||||
def parse_cmd_line(argv):
|
||||
"""
|
||||
Parse command line arguments
|
||||
|
||||
argv: Passed in sys.argv
|
||||
"""
|
||||
|
||||
usage = """
|
||||
USAGE: %s [-l] [-s <server_num>]
|
||||
|
||||
If no options are supplied a connection to server 1 will be opened.
|
||||
Options:
|
||||
|
||||
-h, --help Display this menu :)
|
||||
-l, --list List instances that are tracked
|
||||
-s, --server-num Connect to this server number (1-indexed)
|
||||
""" % (SCRIPT_NAME)
|
||||
|
||||
short_args = "hls:"
|
||||
long_args = ("help", "list", "server-num=")
|
||||
try:
|
||||
opts, extra_opts = getopt.getopt(argv[1:], short_args, long_args)
|
||||
except getopt.GetoptError as e:
|
||||
print("Unrecognized command line option or missing required argument: %s" % (e))
|
||||
print(usage)
|
||||
sys.exit(2)
|
||||
|
||||
cmd_line_option_list = {'action': 'ssh', 'server': '1'}
|
||||
|
||||
for opt, val in opts:
|
||||
if opt in ("-h", "--help"):
|
||||
print(usage)
|
||||
sys.exit(0)
|
||||
elif opt in ("-l", "--list"):
|
||||
cmd_line_option_list['action'] = 'list'
|
||||
elif opt in ("-s", "--server-num"):
|
||||
cmd_line_option_list['server'] = val
|
||||
|
||||
return cmd_line_option_list
|
||||
|
||||
|
||||
def setup():
|
||||
if sys.platform.startswith('win') or sys.platform.startswith('darwin'):
|
||||
QtCore.QSettings.setDefaultFormat(QtCore.QSettings.IniFormat)
|
||||
|
||||
app = QtGui.QApplication([])
|
||||
app.setOrganizationName("GNS3")
|
||||
app.setOrganizationDomain("gns3.net")
|
||||
app.setApplicationName("GNS3")
|
||||
|
||||
if not os.path.isfile(QtCore.QSettings().fileName()):
|
||||
print('Config file {} not found! Aborting...'.format(QtCore.QSettings().fileName()))
|
||||
sys.exit(1)
|
||||
|
||||
print('Config file: {}'.format(QtCore.QSettings().fileName()))
|
||||
|
||||
|
||||
def read_cloud_settings():
|
||||
settings = QtCore.QSettings()
|
||||
settings.beginGroup("CloudInstances")
|
||||
|
||||
instances = []
|
||||
# Load the instances
|
||||
size = settings.beginReadArray("cloud_instance")
|
||||
for index in range(0, size):
|
||||
settings.setArrayIndex(index)
|
||||
name = settings.value('name')
|
||||
host = settings.value('host')
|
||||
private_key = settings.value('private_key')
|
||||
public_key = settings.value('public_key')
|
||||
uid = settings.value('id')
|
||||
|
||||
instances.append((name, host, private_key, public_key, uid))
|
||||
|
||||
if len(instances) == 0:
|
||||
raise Exception("Could not find any servers")
|
||||
|
||||
return instances
|
||||
|
||||
|
||||
def main():
|
||||
options = parse_cmd_line(sys.argv)
|
||||
setup()
|
||||
instances = read_cloud_settings()
|
||||
|
||||
if options['action'] == 'ssh':
|
||||
name, host, private_key, public_key, uid = instances[int(options['server']) - 1]
|
||||
print('Instance name: {}'.format(name))
|
||||
print('Host ip: {}'.format(host))
|
||||
|
||||
public_key_path = '/tmp/id_rsa.pub'
|
||||
open(public_key_path, 'w').write(public_key)
|
||||
private_key_path = '/tmp/id_rsa'
|
||||
open(private_key_path, 'w').write(private_key)
|
||||
cmd = 'chmod 0600 {}'.format(private_key_path)
|
||||
os.system(cmd)
|
||||
print('Per-instance ssh keys written to {}'.format(private_key_path))
|
||||
|
||||
cmd = 'ssh -i /tmp/id_rsa root@{}'.format(host)
|
||||
print(cmd)
|
||||
os.system(cmd)
|
||||
elif options['action'] == 'list':
|
||||
print('ID Name IP UID')
|
||||
for idx, info in enumerate(instances):
|
||||
name, host, private_key, public_key, uid = info
|
||||
print('{:2d} {} {} {}'.format(idx + 1, name, host, uid))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user