diff options
| author | Christian Breunig <christian@breunig.cc> | 2025-07-22 16:03:57 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 16:03:57 +0200 |
| commit | dea50dfb0ded322d0f2d3385e11658143a1d210a (patch) | |
| tree | 8dee6b19d109b487bca23c3ab4b4e4262a550825 /python | |
| parent | fe8559ec092aea60bf4104dfb1f91cd790ce9b55 (diff) | |
| parent | b7a24e7f4945d43deebaa5b33fbcc54e29618938 (diff) | |
| download | vyos-1x-dea50dfb0ded322d0f2d3385e11658143a1d210a.tar.gz vyos-1x-dea50dfb0ded322d0f2d3385e11658143a1d210a.zip | |
Merge pull request #4613 from c-po/add-pylint
T7648: Set up a linter check to check complete files for syntax errors and missing imports
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/ifconfig/control.py | 2 | ||||
| -rw-r--r-- | python/vyos/ifconfig/interface.py | 7 | ||||
| -rw-r--r-- | python/vyos/utils/dict.py | 3 | ||||
| -rw-r--r-- | python/vyos/utils/file.py | 36 | ||||
| -rw-r--r-- | python/vyos/utils/misc.py | 2 | ||||
| -rw-r--r-- | python/vyos/utils/network.py | 17 | ||||
| -rw-r--r-- | python/vyos/utils/permission.py | 24 |
7 files changed, 33 insertions, 58 deletions
diff --git a/python/vyos/ifconfig/control.py b/python/vyos/ifconfig/control.py index e5672ed50..3cf3297b4 100644 --- a/python/vyos/ifconfig/control.py +++ b/python/vyos/ifconfig/control.py @@ -29,6 +29,8 @@ class Control(Section): _command_get = {} _command_set = {} _signature = {} + config = {} + ifname = None def __init__(self, **kargs): # some commands (such as operation comands - show interfaces, etc.) diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index 787364483..3b3536301 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -23,10 +23,9 @@ from glob import glob from ipaddress import IPv4Network from ipaddress import IPv6Interface -from netifaces import ifaddresses -# this is not the same as socket.AF_INET/INET6 -from netifaces import AF_INET -from netifaces import AF_INET6 +from netifaces import ifaddresses # pylint: disable = no-name-in-module +from socket import AF_INET +from socket import AF_INET6 from netaddr import EUI from netaddr import mac_unix_expanded diff --git a/python/vyos/utils/dict.py b/python/vyos/utils/dict.py index e6ef943c6..ff21f0677 100644 --- a/python/vyos/utils/dict.py +++ b/python/vyos/utils/dict.py @@ -347,8 +347,6 @@ class FixedDict(dict): raise ConfigError(f'Option "{k}" has no defined default') """ - from vyos import ConfigError - def __init__(self, **options): self._allowed = options.keys() super().__init__(**options) @@ -368,6 +366,7 @@ class FixedDict(dict): >>> d {'key': 'value'} """ + from vyos import ConfigError if k not in self._allowed: raise ConfigError(f'Option "{k}" has no defined default') super().__setitem__(k, v) diff --git a/python/vyos/utils/file.py b/python/vyos/utils/file.py index 31c2361df..1e2de2b39 100644 --- a/python/vyos/utils/file.py +++ b/python/vyos/utils/file.py @@ -92,36 +92,6 @@ def read_json(fname, defaultonfailure=None): return defaultonfailure raise e -def chown(path, user=None, group=None, recursive=False): - """ change file/directory owner """ - from pwd import getpwnam - from grp import getgrnam - - if user is None and group is None: - return False - - # path may also be an open file descriptor - if not isinstance(path, int) and not os.path.exists(path): - return False - - # keep current value if not specified otherwise - uid = -1 - gid = -1 - - if user: - uid = getpwnam(user).pw_uid - if group: - gid = getgrnam(group).gr_gid - - if recursive: - for dirpath, dirnames, filenames in os.walk(path): - os.chown(dirpath, uid, gid) - for filename in filenames: - os.chown(os.path.join(dirpath, filename), uid, gid) - else: - os.chown(path, uid, gid) - return True - def chmod(path, bitmask): # path may also be an open file descriptor @@ -175,12 +145,6 @@ def file_permissions(path): """ Return file permissions in string format, e.g '0755' """ return oct(os.stat(path).st_mode)[4:] -def makedir(path, user=None, group=None): - if os.path.exists(path): - return - os.makedirs(path, mode=0o755) - chown(path, user, group) - def wait_for_inotify(file_path, pre_hook=None, event_type=None, timeout=None, sleep_interval=0.1): """ Waits for an inotify event to occur """ if not os.path.dirname(file_path): diff --git a/python/vyos/utils/misc.py b/python/vyos/utils/misc.py index 0ffd82696..c7f7e7343 100644 --- a/python/vyos/utils/misc.py +++ b/python/vyos/utils/misc.py @@ -48,7 +48,7 @@ def install_into_config(conf, config_paths, override_prompt=True): for path in config_paths: if override_prompt and conf.exists(path) and not conf.is_multi(path): - if not ask_yes_no(f'Config node "{node}" already exists. Do you want to overwrite it?'): + if not ask_yes_no(f'Config node "{path}" already exists. Do you want to overwrite it?'): continue try: diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index 2182642dd..0e2cc58cf 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -13,9 +13,10 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. +from socket import AF_INET +from socket import AF_INET6 + def _are_same_ip(one, two): - from socket import AF_INET - from socket import AF_INET6 from socket import inet_pton from vyos.template import is_ipv4 # compare the binary representation of the IP @@ -347,7 +348,7 @@ def is_ipv6_link_local(addr): def is_addr_assigned(ip_address, vrf=None, return_ifname=False, include_vrf=False) -> bool | str: """ Verify if the given IPv4/IPv6 address is assigned to any interface """ - from netifaces import interfaces + from netifaces import interfaces # pylint: disable = no-name-in-module from vyos.utils.network import get_interface_config from vyos.utils.dict import dict_search @@ -445,10 +446,8 @@ def is_subnet_connected(subnet, primary=False): from ipaddress import ip_address from ipaddress import ip_network - from netifaces import ifaddresses - from netifaces import interfaces - from netifaces import AF_INET - from netifaces import AF_INET6 + from netifaces import ifaddresses # pylint: disable = no-name-in-module + from netifaces import interfaces # pylint: disable = no-name-in-module from vyos.template import is_ipv6 @@ -482,9 +481,7 @@ def is_subnet_connected(subnet, primary=False): def is_afi_configured(interface: str, afi): """ Check if given address family is configured, or in other words - an IP address is assigned to the interface. """ - from netifaces import ifaddresses - from netifaces import AF_INET - from netifaces import AF_INET6 + from netifaces import ifaddresses # pylint: disable = no-name-in-module if afi not in [AF_INET, AF_INET6]: raise ValueError('Address family must be in [AF_INET, AF_INET6]') diff --git a/python/vyos/utils/permission.py b/python/vyos/utils/permission.py index efd44bfeb..08bb7bdbc 100644 --- a/python/vyos/utils/permission.py +++ b/python/vyos/utils/permission.py @@ -15,23 +15,37 @@ import os -def chown(path, user, group): +def chown(path, user=None, group=None, recursive=False): """ change file/directory owner """ from pwd import getpwnam from grp import getgrnam - if user is None or group is None: + if user is None and group is None: return False # path may also be an open file descriptor if not isinstance(path, int) and not os.path.exists(path): return False - uid = getpwnam(user).pw_uid - gid = getgrnam(group).gr_gid - os.chown(path, uid, gid) + # keep current value if not specified otherwise + uid = -1 + gid = -1 + + if user: + uid = getpwnam(user).pw_uid + if group: + gid = getgrnam(group).gr_gid + + if recursive: + for dirpath, dirnames, filenames in os.walk(path): + os.chown(dirpath, uid, gid) + for filename in filenames: + os.chown(os.path.join(dirpath, filename), uid, gid) + else: + os.chown(path, uid, gid) return True + def chmod(path, bitmask): # path may also be an open file descriptor if not isinstance(path, int) and not os.path.exists(path): |
