Fix linter failures
This commit is contained in:
+21
-17
@@ -17,10 +17,11 @@ with pyVmomi's type system and invoked through the same SOAP stub.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import hashlib
|
||||
import socket
|
||||
import ssl
|
||||
from typing import Optional
|
||||
from typing import Self
|
||||
|
||||
from pyVim.connect import Disconnect, SmartConnect
|
||||
from pyVmomi import vim
|
||||
@@ -45,12 +46,10 @@ def _register_nfc_types() -> None:
|
||||
global _NFC_TYPES_REGISTERED
|
||||
if _NFC_TYPES_REGISTERED:
|
||||
return
|
||||
try:
|
||||
with contextlib.suppress(Exception):
|
||||
GetVmodlType("vim.NfcService")
|
||||
_NFC_TYPES_REGISTERED = True
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
CreateManagedType(
|
||||
"vim.NfcService",
|
||||
@@ -136,7 +135,7 @@ def connect_vim(
|
||||
username: str,
|
||||
password: str,
|
||||
port: int = 443,
|
||||
thumbprint: Optional[str] = None,
|
||||
thumbprint: str | None = None,
|
||||
allow_untrusted: bool = False,
|
||||
) -> vim.ServiceInstance:
|
||||
"""Login to vCenter or ESXi using pyVim.connect.SmartConnect.
|
||||
@@ -186,10 +185,10 @@ def _virtual_disk_key(vm: vim.VirtualMachine, disk_path: str) -> int:
|
||||
def get_nfc_ticket(
|
||||
si: vim.ServiceInstance,
|
||||
vm: vim.VirtualMachine,
|
||||
disk_device_key: Optional[int] = None,
|
||||
host_for_access: Optional[vim.HostSystem] = None,
|
||||
disk_device_key: int | None = None,
|
||||
host_for_access: vim.HostSystem | None = None,
|
||||
read_only: bool = True,
|
||||
disk_path: Optional[str] = None,
|
||||
disk_path: str | None = None,
|
||||
) -> vim.HostServiceTicket:
|
||||
"""Return a one-time NFC HostServiceTicket for ``vm``.
|
||||
|
||||
@@ -235,7 +234,7 @@ def get_ssl_cert_thumbprint(
|
||||
host: str,
|
||||
port: int = 443,
|
||||
digest_algorithm: str = "sha1",
|
||||
ssl_context: Optional[ssl.SSLContext] = None,
|
||||
ssl_context: ssl.SSLContext | None = None,
|
||||
timeout: float = 30.0,
|
||||
) -> str:
|
||||
"""Return the TLS certificate thumbprint of ``host``:``port``.
|
||||
@@ -257,9 +256,11 @@ def get_ssl_cert_thumbprint(
|
||||
"""
|
||||
if ssl_context is None:
|
||||
ssl_context = _ssl_client_context(verify=False)
|
||||
with socket.create_connection((host, port), timeout=timeout) as sock:
|
||||
with ssl_context.wrap_socket(sock, server_hostname=host) as ssock:
|
||||
cert = ssock.getpeercert(binary_form=True)
|
||||
with (
|
||||
socket.create_connection((host, port), timeout=timeout) as sock,
|
||||
ssl_context.wrap_socket(sock, server_hostname=host) as ssock,
|
||||
):
|
||||
cert = ssock.getpeercert(binary_form=True)
|
||||
if not cert:
|
||||
raise ConnectionError(f"no peer certificate from {host}:{port}")
|
||||
return _format_thumbprint(hashlib.new(digest_algorithm, cert).digest())
|
||||
@@ -341,7 +342,10 @@ def connect_authd(
|
||||
|
||||
try:
|
||||
if not allow_untrusted and ticket.sslThumbprint:
|
||||
peer = _sha1_thumbprint(ssock.getpeercert(True))
|
||||
der_cert = ssock.getpeercert(True)
|
||||
if not der_cert:
|
||||
raise ConnectionError(f"no peer certificate from {host}:{port}")
|
||||
peer = _sha1_thumbprint(der_cert)
|
||||
if _normalize_thumbprint(peer) != _normalize_thumbprint(
|
||||
ticket.sslThumbprint
|
||||
):
|
||||
@@ -391,7 +395,7 @@ class NfcAuthSession:
|
||||
finally:
|
||||
Disconnect(self.si)
|
||||
|
||||
def __enter__(self) -> "NfcAuthSession":
|
||||
def __enter__(self) -> Self:
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb) -> None:
|
||||
@@ -404,10 +408,10 @@ def authenticate(
|
||||
password: str,
|
||||
vm_moref: str,
|
||||
port: int = 443,
|
||||
thumbprint: Optional[str] = None,
|
||||
thumbprint: str | None = None,
|
||||
allow_untrusted: bool = False,
|
||||
disk_device_key: Optional[int] = None,
|
||||
disk_path: Optional[str] = None,
|
||||
disk_device_key: int | None = None,
|
||||
disk_path: str | None = None,
|
||||
read_only: bool = True,
|
||||
nfc_ssl: bool = True,
|
||||
) -> NfcAuthSession:
|
||||
|
||||
@@ -25,6 +25,7 @@ import os
|
||||
import socket
|
||||
import ssl
|
||||
import struct
|
||||
from typing import Self
|
||||
|
||||
from openvixdisklib import fastlz
|
||||
from openvixdisklib.nfc_auth import NfcAuthSession, _ssl_client_context
|
||||
@@ -384,7 +385,7 @@ class NfcDisk:
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def __enter__(self) -> "NfcDisk":
|
||||
def __enter__(self) -> Self:
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb) -> None:
|
||||
@@ -485,6 +486,7 @@ def open_disk(
|
||||
raise NotImplementedError(
|
||||
f"NFC compression type {compression} is not supported"
|
||||
)
|
||||
sock: socket.socket
|
||||
if session.nfc_ssl:
|
||||
sock = wrap_nfcssl_socket(session.authd_sock, session.ticket.host)
|
||||
else:
|
||||
|
||||
@@ -19,7 +19,7 @@ import contextlib
|
||||
import ctypes
|
||||
import logging
|
||||
import os
|
||||
from typing import Iterator, Optional, Union
|
||||
from collections.abc import Iterator
|
||||
|
||||
from pyVim.connect import Disconnect
|
||||
from pyVmomi import vim
|
||||
@@ -70,7 +70,7 @@ def get_buffer(size: int):
|
||||
return ctypes.create_string_buffer(size)
|
||||
|
||||
|
||||
def _parse_vm_moref(vmx_spec: Optional[str]) -> str:
|
||||
def _parse_vm_moref(vmx_spec: str | None) -> str:
|
||||
if not vmx_spec:
|
||||
raise ValueError("vmx_spec is required (for example 'moref=vm-13098')")
|
||||
if "=" in vmx_spec:
|
||||
@@ -81,7 +81,7 @@ def _parse_vm_moref(vmx_spec: Optional[str]) -> str:
|
||||
return vmx_spec
|
||||
|
||||
|
||||
def _select_transport(transport_modes: Optional[str]) -> str:
|
||||
def _select_transport(transport_modes: str | None) -> str:
|
||||
"""Return the first requested transport this replacement implements.
|
||||
|
||||
``None`` defaults to ``nbdssl``. A colon-separated list (VDDK
|
||||
@@ -105,8 +105,8 @@ class _Connection:
|
||||
self,
|
||||
si: vim.ServiceInstance,
|
||||
vm_moref: str,
|
||||
snapshot_ref: Optional[str],
|
||||
thumbprint: Optional[str],
|
||||
snapshot_ref: str | None,
|
||||
thumbprint: str | None,
|
||||
allow_untrusted: bool,
|
||||
read_only: bool,
|
||||
transport_mode: str,
|
||||
@@ -134,8 +134,8 @@ class VixDiskLibHandle:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_path: Optional[str] = None,
|
||||
vixdisklib_compatibility_version: Optional[str] = None,
|
||||
config_path: str | None = None,
|
||||
vixdisklib_compatibility_version: str | None = None,
|
||||
) -> None:
|
||||
"""Accept the VDDK wrapper constructor; no native library is loaded.
|
||||
|
||||
@@ -166,7 +166,7 @@ class VixDiskLibHandle:
|
||||
break
|
||||
|
||||
if not version_used:
|
||||
raise Exception(
|
||||
raise RuntimeError(
|
||||
"Could not initialize vixDiskLib with any of the following "
|
||||
"versions: %s" % target_versions
|
||||
)
|
||||
@@ -194,13 +194,13 @@ class VixDiskLibHandle:
|
||||
def connect(
|
||||
self,
|
||||
server_name: str,
|
||||
thumbprint: Optional[str],
|
||||
thumbprint: str | None,
|
||||
username: str,
|
||||
password: str,
|
||||
vmx_spec: Optional[str] = None,
|
||||
snapshot_ref: Optional[str] = None,
|
||||
vmx_spec: str | None = None,
|
||||
snapshot_ref: str | None = None,
|
||||
read_only: bool = True,
|
||||
transport_modes: Optional[str] = None,
|
||||
transport_modes: str | None = None,
|
||||
port: int = 443,
|
||||
allow_untrusted: bool = False,
|
||||
) -> Iterator[_Connection]:
|
||||
@@ -318,7 +318,7 @@ class VixDiskLibHandle:
|
||||
disk_handle: _DiskHandle,
|
||||
start_sector: int,
|
||||
num_sectors: int,
|
||||
buf: Union[ctypes.Array, bytearray, memoryview],
|
||||
buf: ctypes.Array | bytearray | memoryview,
|
||||
) -> None:
|
||||
"""Read ``num_sectors`` from ``start_sector`` into ``buf``.
|
||||
|
||||
@@ -331,7 +331,7 @@ class VixDiskLibHandle:
|
||||
data = disk_handle.disk.read(start_sector, num_sectors)
|
||||
if isinstance(buf, (bytearray, memoryview)):
|
||||
if len(buf) < len(data):
|
||||
raise Exception(f"read buffer is {len(buf)} bytes, need {len(data)}")
|
||||
raise RuntimeError(f"read buffer is {len(buf)} bytes, need {len(data)}")
|
||||
buf[: len(data)] = data
|
||||
return
|
||||
ctypes.memmove(buf, data, len(data))
|
||||
@@ -341,7 +341,7 @@ class VixDiskLibHandle:
|
||||
disk_handle: _DiskHandle,
|
||||
start_sector: int,
|
||||
num_sectors: int,
|
||||
buf: Union[ctypes.Array, bytes, bytearray, memoryview],
|
||||
buf: ctypes.Array | bytes | bytearray | memoryview,
|
||||
) -> None:
|
||||
"""Write ``num_sectors`` from ``buf`` starting at ``start_sector``.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user