From beb81ea551c965548d769e6bc488672115b47d40 Mon Sep 17 00:00:00 2001 From: Dimitris Aragiorgis <dimara@grnet.gr> Date: Fri, 12 Oct 2012 14:18:28 +0300 Subject: [PATCH] Fixes to pass pylint (make lint) Signed-off-by: Dimitris Aragiorgis <dimara@grnet.gr> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/client/gnt_network.py | 20 +++---- lib/cmdlib.py | 112 ++++++++++++++++++++++++-------------- lib/config.py | 36 ++++++------ lib/network.py | 13 +++-- lib/query.py | 13 +++-- 5 files changed, 113 insertions(+), 81 deletions(-) diff --git a/lib/client/gnt_network.py b/lib/client/gnt_network.py index 207873ce5..cde1358c1 100644 --- a/lib/client/gnt_network.py +++ b/lib/client/gnt_network.py @@ -20,7 +20,7 @@ """IP pool related commands""" -# pylint: disable-msg=W0401,W0614 +# pylint: disable=W0401,W0614 # W0401: Wildcard import ganeti.cli # W0614: Unused import %s from wildcard import (since we need cli) @@ -68,7 +68,8 @@ def AddNetwork(opts, args): network6=opts.network6, mac_prefix=opts.mac_prefix, network_type=opts.network_type, - add_reserved_ips=_HandleReservedIPs(opts.add_reserved_ips), + add_reserved_ips=\ + _HandleReservedIPs(opts.add_reserved_ips), tags=tags) SubmitOpCode(op, opts=opts) @@ -167,7 +168,7 @@ def ListNetworkFields(opts, args): not opts.no_headers) -def ShowNetworkConfig(opts, args): +def ShowNetworkConfig(_, args): """Show network information. @param opts: the command line options selected by the user @@ -190,7 +191,7 @@ def ShowNetworkConfig(opts, args): for (name, network, gateway, network6, gateway6, mac_prefix, network_type, free_count, reserved_count, - map, group_list, instances, ext_res) in result: + mapping, group_list, instances, ext_res) in result: size = free_count + reserved_count ToStdout("Network name: %s", name) ToStdout(" subnet: %s", network) @@ -204,7 +205,7 @@ def ShowNetworkConfig(opts, args): 100 * float(free_count)/float(size)) ToStdout(" usage map:") idx = 0 - for line in wrap(map, width=64): + for line in wrap(mapping, width=64): ToStdout(" %s %s %d", str(idx).rjust(3), line.ljust(64), idx + 63) idx += 64 ToStdout(" (X) used (.) free") @@ -228,11 +229,11 @@ def ShowNetworkConfig(opts, args): ["nic.ips", "nic.networks"], use_locking=False) - l = lambda value: ", ".join(`idx`+":"+str(ip) + l = lambda value: ", ".join(str(idx)+":"+str(ip) for idx, (ip, net) in enumerate(value) if net == name) - ToStdout(" %s : %s", inst, l(zip(ips,networks))) + ToStdout(" %s : %s", inst, l(zip(ips, networks))) else: ToStdout(" not used by any instances") @@ -264,9 +265,8 @@ def SetNetworkParams(opts, args): ToStderr("Please give at least one of the parameters.") return 1 - op = opcodes.OpNetworkSetParams(network_name=args[0], - # pylint: disable-msg=W0142 - **all_changes) + # pylint: disable=W0142 + op = opcodes.OpNetworkSetParams(network_name=args[0], **all_changes) # TODO: add feedback to user, e.g. list the modifications SubmitOrSend(op, opts) diff --git a/lib/cmdlib.py b/lib/cmdlib.py index 864425630..eaa46b01b 100644 --- a/lib/cmdlib.py +++ b/lib/cmdlib.py @@ -40,7 +40,6 @@ import tempfile import shutil import itertools import operator -import ipaddr from ganeti import ssh from ganeti import utils @@ -1315,13 +1314,36 @@ def _ExpandInstanceName(cfg, name): """Wrapper over L{_ExpandItemName} for instance.""" return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance") -def _BuildNetworkHookEnv(name, network, gateway, network6, gateway6, + +def _BuildNetworkHookEnv(name, subnet, gateway, network6, gateway6, network_type, mac_prefix, tags): + """Builds network related env variables for hooks + + This builds the hook environment from individual variables. + + @type name: string + @param name: the name of the network + @type network: string + @param network: the ipv4 subnet + @type gateway: string + @param gateway: the ipv4 gateway + @type network6: string + @param network6: the ipv6 subnet + @type gateway6: string + @param gateway6: the ipv6 gateway + @type network_type: string + @param network_type: the type of the network + @type mac_prefix: string + @param mac_prefix: the mac_prefix + @type tags: list + @param tags: the tags of the network + + """ env = dict() if name: env["NETWORK_NAME"] = name - if network: - env["NETWORK_SUBNET"] = network + if subnet: + env["NETWORK_SUBNET"] = subnet if gateway: env["NETWORK_GATEWAY"] = gateway if network6: @@ -1338,18 +1360,27 @@ def _BuildNetworkHookEnv(name, network, gateway, network6, gateway6, return env -def _BuildNetworkHookEnvByObject(lu, network): +def _BuildNetworkHookEnvByObject(net): + """Builds network related env varliables for hooks + + @type lu: L{objects.LogicalUnit} + @param lu: the corresponding LU + @type network: L{objects.Network} + @param network: the network object + + """ args = { - "name": network.name, - "network": network.network, - "gateway": network.gateway, - "network6": network.network6, - "gateway6": network.gateway6, - "network_type": network.network_type, - "mac_prefix": network.mac_prefix, - "tags" : network.tags, + "name": net.name, + "subnet": net.network, + "gateway": net.gateway, + "network6": net.network6, + "gateway6": net.gateway6, + "network_type": net.network_type, + "mac_prefix": net.mac_prefix, + "tags": net.tags, } - return _BuildNetworkHookEnv(**args) + + return _BuildNetworkHookEnv(**args) # pylint: disable=W0142 def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, @@ -1411,7 +1442,7 @@ def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, } if nics: nic_count = len(nics) - for idx, (ip, mac, mode, link, network, netinfo) in enumerate(nics): + for idx, (ip, mac, mode, link, net, netinfo) in enumerate(nics): if ip is None: ip = "" env["INSTANCE_NIC%d_IP" % idx] = ip @@ -1419,7 +1450,7 @@ def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, env["INSTANCE_NIC%d_MODE" % idx] = mode env["INSTANCE_NIC%d_LINK" % idx] = link if network: - env["INSTANCE_NIC%d_NETWORK" % idx] = network + env["INSTANCE_NIC%d_NETWORK" % idx] = net if netinfo: nobj = objects.Network.FromDict(netinfo) if nobj.network: @@ -1464,6 +1495,7 @@ def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, return env + def _NICToTuple(lu, nic): """Build a tupple of nic information. @@ -1473,20 +1505,21 @@ def _NICToTuple(lu, nic): @param nic: nic to convert to hooks tuple """ - cluster = lu.cfg.GetClusterInfo() ip = nic.ip mac = nic.mac + cluster = lu.cfg.GetClusterInfo() filled_params = cluster.SimpleFillNIC(nic.nicparams) mode = filled_params[constants.NIC_MODE] link = filled_params[constants.NIC_LINK] - network = nic.network + net = nic.network netinfo = None - if network: - net_uuid = lu.cfg.LookupNetwork(network) + if net: + net_uuid = lu.cfg.LookupNetwork(net) if net_uuid: nobj = lu.cfg.GetNetwork(net_uuid) netinfo = objects.Network.ToDict(nobj) - return (ip, mac, mode, link, network, netinfo) + return (ip, mac, mode, link, net, netinfo) + def _NICListToTuple(lu, nics): """Build a list of nic information tuples. @@ -1501,11 +1534,11 @@ def _NICListToTuple(lu, nics): """ hooks_nics = [] - cluster = lu.cfg.GetClusterInfo() for nic in nics: hooks_nics.append(_NICToTuple(lu, nic)) return hooks_nics + def _BuildInstanceHookEnvByObject(lu, instance, override=None): """Builds instance related env variables for hooks from an object. @@ -9476,7 +9509,7 @@ def _ComputeNics(op, cluster, default_ip, cfg, proc): """ nics = [] - for idx, nic in enumerate(op.nics): + for nic in op.nics: nic_mode_req = nic.get(constants.INIC_MODE, None) nic_mode = nic_mode_req if nic_mode is None or nic_mode == constants.VALUE_AUTO: @@ -10238,7 +10271,6 @@ class LUInstanceCreate(LogicalUnit): (idx, netparams.values())) nic.nicparams = dict(netparams) if nic.ip is not None: - filled_params = cluster.SimpleFillNIC(nic.nicparams) if nic.ip.lower() == constants.NIC_IP_POOL: try: nic.ip = self.cfg.GenerateIp(net, self.proc.GetECId()) @@ -12962,7 +12994,7 @@ class LUInstanceSetParams(LogicalUnit): errors.ECODE_NOTUNIQUE) elif new_ip.lower() == constants.NIC_IP_POOL: raise errors.OpPrereqError("ip=pool, but no network found", - ECODEE_INVAL) + errors.ECODE_INVAL) else: # new net is None if self.op.conflicts_check: @@ -13228,7 +13260,7 @@ class LUInstanceSetParams(LogicalUnit): nic.nicparams, cluster, pnode) return None - def _PrepareNicRemove(_, params, private): + def _PrepareNicRemove(_, params, __): ip = params.ip net = params.network if net is not None and ip is not None: @@ -13454,16 +13486,16 @@ class LUInstanceSetParams(LogicalUnit): """ mac = params[constants.INIC_MAC] ip = params.get(constants.INIC_IP, None) - network = params.get(constants.INIC_NETWORK, None) + net = params.get(constants.INIC_NETWORK, None) #TODO: not private.filled?? can a nic have no nicparams?? nicparams = private.filled - return (objects.NIC(mac=mac, ip=ip, network=network, nicparams=nicparams), [ + return (objects.NIC(mac=mac, ip=ip, network=net, nicparams=nicparams), [ ("nic.%d" % idx, "add:mac=%s,ip=%s,mode=%s,link=%s,network=%s" % (mac, ip, private.filled[constants.NIC_MODE], private.filled[constants.NIC_LINK], - network)), + net)), ]) @staticmethod @@ -15571,7 +15603,7 @@ class LUNetworkAdd(LogicalUnit): """ args = { "name": self.op.network_name, - "network": self.op.network, + "subnet": self.op.network, "gateway": self.op.gateway, "network6": self.op.network6, "gateway6": self.op.gateway6, @@ -15579,7 +15611,7 @@ class LUNetworkAdd(LogicalUnit): "network_type": self.op.network_type, "tags": self.op.tags, } - return _BuildNetworkHookEnv(**args) + return _BuildNetworkHookEnv(**args) # pylint: disable=W0142 def Exec(self, feedback_fn): """Add the ip pool to the cluster. @@ -15662,8 +15694,8 @@ class LUNetworkRemove(LogicalUnit): # Verify that the network is not conncted. node_groups = [group.name for group in self.cfg.GetAllNodeGroupsInfo().values() - for network in group.networks.keys() - if network == self.network_uuid] + for net in group.networks.keys() + if net == self.network_uuid] if node_groups: self.LogWarning("Nework '%s' is connected to the following" @@ -15779,7 +15811,7 @@ class LUNetworkSetParams(LogicalUnit): """ args = { "name": self.op.network_name, - "network": self.network.network, + "subnet": self.network.network, "gateway": self.gateway, "network6": self.network6, "gateway6": self.gateway6, @@ -15787,7 +15819,7 @@ class LUNetworkSetParams(LogicalUnit): "network_type": self.network_type, "tags": self.tags, } - return _BuildNetworkHookEnv(**args) + return _BuildNetworkHookEnv(**args) # pylint: disable=W0142 def BuildHooksNodes(self): """Build hooks nodes. @@ -15892,7 +15924,6 @@ class _NetworkQuery(_QueryBase): do_instances = query.NETQ_INST in self.requested_data do_groups = do_instances or (query.NETQ_GROUP in self.requested_data) do_stats = query.NETQ_STATS in self.requested_data - cluster = lu.cfg.GetClusterInfo() network_to_groups = None network_to_instances = None @@ -15902,7 +15933,6 @@ class _NetworkQuery(_QueryBase): if do_groups: all_groups = lu.cfg.GetAllNodeGroupsInfo() network_to_groups = dict((uuid, []) for uuid in self.wanted) - default_nicpp = cluster.nicparams[constants.PP_DEFAULT] if do_instances: all_instances = lu.cfg.GetAllInstancesInfo() @@ -16015,7 +16045,7 @@ class LUNetworkConnect(LogicalUnit): ret["GROUP_NAME"] = self.group_name ret["GROUP_NETWORK_MODE"] = self.network_mode ret["GROUP_NETWORK_LINK"] = self.network_link - ret.update(_BuildNetworkHookEnvByObject(self, self.network)) + ret.update(_BuildNetworkHookEnvByObject(self.network)) return ret def BuildHooksNodes(self): @@ -16049,7 +16079,7 @@ class LUNetworkConnect(LogicalUnit): instances = [(instance.name, idx, nic.ip) for instance in groupinstances for idx, nic in enumerate(instance.nics) - if (not nic.network and pool._Contains(nic.ip))] + if (not nic.network and pool.Contains(nic.ip))] if instances: self.LogWarning("Following occurences use IPs from network %s" " that is about to connect to nodegroup %s: %s" % @@ -16110,7 +16140,7 @@ class LUNetworkDisconnect(LogicalUnit): def BuildHooksEnv(self): ret = dict() ret["GROUP_NAME"] = self.group_name - ret.update(_BuildNetworkHookEnvByObject(self, self.network)) + ret.update(_BuildNetworkHookEnvByObject(self.network)) return ret def BuildHooksNodes(self): @@ -16192,7 +16222,7 @@ def _CheckForConflictingIp(lu, ip, node): @param node: node name """ - (conf_net, conf_netparams) = lu.cfg.CheckIPInNodeGroup(ip, node) + (conf_net, _) = lu.cfg.CheckIPInNodeGroup(ip, node) if conf_net is not None: raise errors.OpPrereqError("Conflicting IP found:" " %s <> %s." % (ip, conf_net), diff --git a/lib/config.py b/lib/config.py index a98779ced..c56ef7549 100644 --- a/lib/config.py +++ b/lib/config.py @@ -162,6 +162,16 @@ def _CheckInstanceDiskIvNames(disks): return result +def _GenerateMACSuffix(): + """Generate one mac address + + """ + byte1 = random.randrange(0, 256) + byte2 = random.randrange(0, 256) + byte3 = random.randrange(0, 256) + suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3) + return suffix + class ConfigWriter: """The interface to the cluster configuration. @@ -235,16 +245,6 @@ class ConfigWriter: return wraps(view_func)(_decorator) return _get_mac_prefix - def _GenerateMACSuffix(self): - """Generate one mac address - - """ - byte1 = random.randrange(0, 256) - byte2 = random.randrange(0, 256) - byte3 = random.randrange(0, 256) - suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3) - return suffix - @locking.ssynchronized(_config_lock, shared=1) def GetNdParams(self, node): """Get the node params populated with cluster defaults. @@ -299,7 +299,7 @@ class ConfigWriter: """ existing = self._AllMACs() - gen_mac = self._GenerateMACPrefix(net)(self._GenerateMACSuffix) + gen_mac = self._GenerateMACPrefix(net)(_GenerateMACSuffix) return self._temporary_ids.Generate(existing, gen_mac, ec_id) @locking.ssynchronized(_config_lock, shared=1) @@ -343,18 +343,16 @@ class ConfigWriter: as reserved. """ - nobj = self._UnlockedGetNetwork(net_uuid) - pool = network.AddressPool(nobj) self._temporary_ips.Reserve(ec_id, ('release', address, net_uuid)) @locking.ssynchronized(_config_lock, shared=1) - def ReleaseIp(self, network, address, ec_id): + def ReleaseIp(self, net, address, ec_id): """Give a specified IP address back to an IP pool. This is just a wrapper around _UnlockedReleaseIp. """ - net_uuid = self._UnlockedLookupNetwork(network) + net_uuid = self._UnlockedLookupNetwork(net) if net_uuid: self._UnlockedReleaseIp(net_uuid, address, ec_id) @@ -375,7 +373,7 @@ class ConfigWriter: raise errors.ReservationError("Cannot generate IP. Network is full") return ("reserve", ip, net_uuid) - _ ,address, _ = self._temporary_ips.Generate([], gen_one, ec_id) + _, address, _ = self._temporary_ips.Generate([], gen_one, ec_id) return address def _UnlockedReserveIp(self, net_uuid, address, ec_id): @@ -2409,8 +2407,8 @@ class ConfigWriter: """Get a list of network names """ - names = [network.name - for network in self._config_data.networks.values()] + names = [net.name + for net in self._config_data.networks.values()] return names def _UnlockedGetNetwork(self, uuid): @@ -2568,7 +2566,7 @@ class ConfigWriter: for net_uuid in nodegroup_info.networks.keys(): net_info = self._UnlockedGetNetwork(net_uuid) pool = network.AddressPool(net_info) - if pool._Contains(ip): + if pool.Contains(ip): return (net_info.name, nodegroup_info.networks[net_uuid]) return (None, None) diff --git a/lib/network.py b/lib/network.py index 96ed654ad..4fbceb3ed 100644 --- a/lib/network.py +++ b/lib/network.py @@ -63,18 +63,20 @@ class AddressPool(object): self.reservations = bitarray(self.net.reservations) else: self.reservations = bitarray(self.network.numhosts) + # pylint: disable=E1103 self.reservations.setall(False) if self.net.ext_reservations: self.ext_reservations = bitarray(self.net.ext_reservations) else: self.ext_reservations = bitarray(self.network.numhosts) + # pylint: disable=E1103 self.ext_reservations.setall(False) assert len(self.reservations) == self.network.numhosts assert len(self.ext_reservations) == self.network.numhosts - def _Contains(self, address): + def Contains(self, address): if address is None: return False addr = ipaddr.IPAddress(address) @@ -90,8 +92,9 @@ class AddressPool(object): return int(addr) - int(self.network.network) - def _Update(self): + def Update(self): """Write address pools back to the network object""" + # pylint: disable=E1103 self.net.ext_reservations = self.ext_reservations.to01() self.net.reservations = self.reservations.to01() @@ -101,7 +104,7 @@ class AddressPool(object): self.ext_reservations[idx] = value else: self.reservations[idx] = value - self._Update() + self.Update() def _GetSize(self): return 2**(32 - self.network.prefixlen) @@ -173,7 +176,7 @@ class AddressPool(object): def _iter_free(): for idx in self.all_reservations.search("0", 64): yield str(self.network[idx]) - + # pylint: disable=E1101 return _iter_free().next def GetExternalReservations(self): @@ -189,7 +192,7 @@ class AddressPool(object): """ obj = cls(net) - obj._Update() + obj.Update() for ip in [obj.network[0], obj.network[-1]]: obj.Reserve(ip, external=True) if obj.net.gateway is not None: diff --git a/lib/query.py b/lib/query.py index 224e5b4a1..27d3d469f 100644 --- a/lib/query.py +++ b/lib/query.py @@ -2494,13 +2494,14 @@ _NETWORK_SIMPLE_FIELDS = { _NETWORK_STATS_FIELDS = { "free_count": ("FreeCount", QFT_NUMBER, 0, "How many addresses are free"), - "reserved_count": ("ReservedCount", QFT_NUMBER, 0, "How many addresses are reserved"), + "reserved_count": ("ReservedCount", QFT_NUMBER, 0, + "How many addresses are reserved"), "map": ("Map", QFT_TEXT, 0, "The actual mapping"), - "external_reservations": ("ExternalReservations", QFT_TEXT, 0, "The external reservations"), + "external_reservations": ("ExternalReservations", QFT_TEXT, 0, + "The external reservations"), } - -def _GetNetworkStatsField(field, kind, ctx, net): +def _GetNetworkStatsField(field, kind, ctx): """Gets the value of a "stats" field from L{NetworkQueryData}. @param field: Field name @@ -2541,7 +2542,7 @@ def _BuildNetworkFields(): fields.extend([ (_MakeField(name, title, kind, doc), NETQ_CONFIG, 0, _GetItemAttr(name)) - for (name, (title, kind, flags, doc)) in _NETWORK_SIMPLE_FIELDS.items() + for (name, (title, kind, _, doc)) in _NETWORK_SIMPLE_FIELDS.items() ]) def _GetLength(getter): @@ -2573,7 +2574,7 @@ def _BuildNetworkFields(): fields.extend([ (_MakeField(name, title, kind, doc), NETQ_STATS, 0, compat.partial(_GetNetworkStatsField, name, kind)) - for (name, (title, kind, flags, doc)) in _NETWORK_STATS_FIELDS.items() + for (name, (title, kind, _, doc)) in _NETWORK_STATS_FIELDS.items() ]) return _PrepareFieldList(fields, []) -- GitLab