1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-10-18 00:50:07 +00:00

refactor(console): replace deprecated optparse with argparse and fix regex escape sequences

This commit is contained in:
catborise 2026-09-24 22:08:08 +03:00
parent dc6327c554
commit 3633c0096e
5 changed files with 132 additions and 74 deletions

View file

@ -23,14 +23,15 @@ import socket
# from six.moves import http_cookies as Cookie
from http import cookies as Cookie
from webvirtcloud.settings import WS_PORT, WS_HOST, WS_CERT
from vrtManager.connection import CONN_SSH, CONN_SOCKET
import argparse
from console.sshtunnels import SSHTunnels
from optparse import OptionParser
from vrtManager.connection import CONN_SOCKET, CONN_SSH
from webvirtcloud.settings import WS_CERT, WS_HOST, WS_PORT
parser = OptionParser()
parser.add_option(
def get_parser(default_host=WS_HOST, default_port=WS_PORT or 6080, default_cert=WS_CERT or CERT):
parser = argparse.ArgumentParser(description="WebVirtCloud noVNC daemon")
parser.add_argument(
"-v",
"--verbose",
dest="verbose",
@ -38,34 +39,33 @@ parser.add_option(
help="Verbose mode",
default=False,
)
parser.add_option(
parser.add_argument(
"-d", "--debug", dest="debug", action="store_true", help="Debug mode", default=False
)
parser.add_option(
"-H", "--host", dest="host", action="store", help="Listen host", default=WS_HOST
parser.add_argument(
"-H", "--host", dest="host", action="store", help="Listen host", default=default_host
)
parser.add_option(
parser.add_argument(
"-p",
"--port",
dest="port",
action="store",
type=int,
help="Listen port",
default=WS_PORT or 6080,
default=default_port,
)
parser.add_option(
parser.add_argument(
"-c",
"--cert",
dest="cert",
action="store",
help="Certificate file path",
default=WS_CERT or CERT,
default=default_cert,
)
return parser
(options, args) = parser.parse_args()
options = get_parser().parse_args()
FORMAT = "%(asctime)s - %(name)s - %(levelname)s : %(message)s"
if options.debug:
@ -184,7 +184,7 @@ class CompatibilityMixIn(object):
self.msg("Try to open local socket %s" % console_socket)
tsock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
tsock.connect(console_socket)
elif console_socket or re.match("^127\.", console_host):
elif console_socket or re.match(r"^127\.", console_host):
# Need tunnel to physical host
if conntype != CONN_SSH:
self.msg(

View file

@ -39,11 +39,12 @@ import libvirt
from http import cookies as Cookie
from webvirtcloud.settings import SOCKETIO_PORT, SOCKETIO_HOST
from vrtManager.connection import CONN_SSH, CONN_SOCKET
from optparse import OptionParser
import argparse
parser = OptionParser()
parser.add_option(
def get_parser(default_host=SOCKETIO_HOST, default_port=SOCKETIO_PORT or 6081):
parser = argparse.ArgumentParser(description="WebVirtCloud Socket.IO daemon")
parser.add_argument(
"-v",
"--verbose",
dest="verbose",
@ -51,30 +52,30 @@ parser.add_option(
help="Verbose mode",
default=False,
)
parser.add_option(
parser.add_argument(
"-d", "--debug", dest="debug", action="store_true", help="Debug mode", default=False
)
parser.add_option(
parser.add_argument(
"-H",
"--host",
dest="host",
action="store",
help="Listen host",
default=SOCKETIO_HOST,
default=default_host,
)
parser.add_option(
parser.add_argument(
"-p",
"--port",
dest="port",
action="store",
type=int,
help="Listen port",
default=SOCKETIO_PORT or 6081,
default=default_port,
)
return parser
(options, args) = parser.parse_args()
options = get_parser().parse_args()
FORMAT = "%(asctime)s - %(name)s - %(levelname)s : %(message)s"
if options.debug:

View file

@ -0,0 +1,57 @@
import argparse
import unittest
class TestCLIParsers(unittest.TestCase):
def test_novncd_parser_defaults(self):
# We test the parser specification without importing django
parser = argparse.ArgumentParser(description="WebVirtCloud noVNC daemon")
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False)
parser.add_argument("-d", "--debug", dest="debug", action="store_true", default=False)
parser.add_argument("-H", "--host", dest="host", action="store", default="0.0.0.0")
parser.add_argument("-p", "--port", dest="port", action="store", type=int, default=6080)
parser.add_argument("-c", "--cert", dest="cert", action="store", default="cert.pem")
opts = parser.parse_args([])
self.assertFalse(opts.verbose)
self.assertFalse(opts.debug)
self.assertEqual(opts.host, "0.0.0.0")
self.assertEqual(opts.port, 6080)
self.assertEqual(opts.cert, "cert.pem")
def test_novncd_parser_custom_args(self):
parser = argparse.ArgumentParser(description="WebVirtCloud noVNC daemon")
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False)
parser.add_argument("-d", "--debug", dest="debug", action="store_true", default=False)
parser.add_argument("-H", "--host", dest="host", action="store", default="0.0.0.0")
parser.add_argument("-p", "--port", dest="port", action="store", type=int, default=6080)
parser.add_argument("-c", "--cert", dest="cert", action="store", default="cert.pem")
opts = parser.parse_args(["-v", "-d", "-H", "192.168.1.50", "-p", "7000", "-c", "/etc/ssl/cert.pem"])
self.assertTrue(opts.verbose)
self.assertTrue(opts.debug)
self.assertEqual(opts.host, "192.168.1.50")
self.assertEqual(opts.port, 7000)
self.assertEqual(opts.cert, "/etc/ssl/cert.pem")
def test_socketiod_parser_defaults_and_custom(self):
parser = argparse.ArgumentParser(description="WebVirtCloud Socket.IO daemon")
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False)
parser.add_argument("-d", "--debug", dest="debug", action="store_true", default=False)
parser.add_argument("-H", "--host", dest="host", action="store", default="localhost")
parser.add_argument("-p", "--port", dest="port", action="store", type=int, default=6081)
opts_def = parser.parse_args([])
self.assertFalse(opts_def.verbose)
self.assertFalse(opts_def.debug)
self.assertEqual(opts_def.host, "localhost")
self.assertEqual(opts_def.port, 6081)
opts_custom = parser.parse_args(["--verbose", "--host", "10.0.0.1", "--port", "9000"])
self.assertTrue(opts_custom.verbose)
self.assertEqual(opts_custom.host, "10.0.0.1")
self.assertEqual(opts_custom.port, 9000)
if __name__ == "__main__":
unittest.main()

View file

@ -180,7 +180,7 @@ class InstancesTestCase(TestCase):
def test_create_from_xml(self):
uuid = randomUUID()
xml = self.xml.replace("test-vm", "test-vm-xml")
xml = re.sub("\s?<uuid>.*?</uuid>", f"<uuid>{uuid}</uuid>", xml)
xml = re.sub(r"\s?<uuid>.*?</uuid>", f"<uuid>{uuid}</uuid>", xml)
response = self.client.post(
reverse("instances:create_instance_select_type", args=[self.compute.id]),
{

View file

@ -38,7 +38,7 @@ class AddStgPool(forms.Form):
def clean_target(self):
storage_type = self.cleaned_data["stg_type"]
target = self.cleaned_data["target"]
have_symbol = re.match("^[^-](\/?((?:[0-9a-zA-Z_])|(?:\.)|((?<!\/)-))*)+$", target)
have_symbol = re.match(r"^[^-](\/?((?:[0-9a-zA-Z_])|(?:\.)|((?<!\/)-))*)+$", target)
if storage_type in ["dir", "netfs"] and not have_symbol:
raise forms.ValidationError(
_("The target must not contain any special characters")
@ -50,7 +50,7 @@ class AddStgPool(forms.Form):
def clean_source(self):
storage_type = self.cleaned_data["stg_type"]
source = self.cleaned_data["source"]
have_symbol = re.match("^[a-zA-Z0-9\/]+$", source)
have_symbol = re.match(r"^[a-zA-Z0-9\/]+$", source)
if storage_type in ["logical", "netfs"]:
if not source:
raise forms.ValidationError(_("No device or path has been entered"))