Yeela Kaplan has uploaded a new change for review. Change subject: Change file() to open() continued ......................................................................
Change file() to open() continued Update file calls to open calls due to file's deprecation, and also use with statements to take care of closing files. Change-Id: Iab7c37fdc18cafa9dfcbd2e9a8fe4f82022dcca0 Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1128715 Signed-off-by: Yeela Kaplan <[email protected]> --- M lib/vdsm/netinfo.py M lib/vdsm/utils.py M tests/hooksTests.py M tests/netconfTests.py M tests/tcTests.py M vds_bootstrap/vds_bootstrap.py M vdsm/API.py M vdsm/caps.py M vdsm/hooks.py M vdsm/ppc64HardwareInfo.py M vdsm/substitute_constants.py M vdsm/virt/sampling.py M vdsm_hooks/hostusb/after_vm_destroy.py M vdsm_hooks/hostusb/before_vm_start.py M vdsm_hooks/hugepages/after_vm_destroy.py M vdsm_hooks/hugepages/before_vm_migrate_destination.py M vdsm_hooks/hugepages/before_vm_start.py M vdsm_reg/deployUtil.py.in 18 files changed, 157 insertions(+), 152 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/59/33859/1 diff --git a/lib/vdsm/netinfo.py b/lib/vdsm/netinfo.py index 072396b..b7deb65 100644 --- a/lib/vdsm/netinfo.py +++ b/lib/vdsm/netinfo.py @@ -398,11 +398,12 @@ paddr = {} for b in bondings(): slave = '' - for line in file('/proc/net/bonding/' + b): - if line.startswith('Slave Interface: '): - slave = line[len('Slave Interface: '):-1] - if line.startswith('Permanent HW addr: '): - paddr[slave] = line[len('Permanent HW addr: '):-1] + with open('/proc/net/bonding/' + b) as f: + for line in f: + if line.startswith('Slave Interface: '): + slave = line[len('Slave Interface: '):-1] + if line.startswith('Permanent HW addr: '): + paddr[slave] = line[len('Permanent HW addr: '):-1] return paddr diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py index 23c63e8..322d5d4 100644 --- a/lib/vdsm/utils.py +++ b/lib/vdsm/utils.py @@ -728,14 +728,15 @@ macs = [] for b in glob.glob('/sys/class/net/*/device'): - mac = file(os.path.join(os.path.dirname(b), "address")). \ - readline().replace("\n", "") + with open(os.path.join(os.path.dirname(b), "address")) as a: + mac = a.readline().replace("\n", "") macs.append(mac) for b in glob.glob('/proc/net/bonding/*'): - for line in file(b): - if line.startswith("Permanent HW addr: "): - macs.append(line.split(": ")[1].replace("\n", "")) + with open(b) as bond: + for line in bond: + if line.startswith("Permanent HW addr: "): + macs.append(line.split(": ")[1].replace("\n", "")) return set(macs) - set(["", "00:00:00:00:00:00"]) diff --git a/tests/hooksTests.py b/tests/hooksTests.py index b27b02c..57154df 100644 --- a/tests/hooksTests.py +++ b/tests/hooksTests.py @@ -114,9 +114,10 @@ import os import hooking -domXMLFile = file(os.environ['_hook_domxml'], 'a') +domXMLFile = open(os.environ['_hook_domxml'], 'a') customProperty = os.environ['customProperty'] domXMLFile.write(customProperty) +domXMLFile.close() """ f.write(code) os.chmod(f.name, 0o775) diff --git a/tests/netconfTests.py b/tests/netconfTests.py index ebf12a1..13d8c18 100644 --- a/tests/netconfTests.py +++ b/tests/netconfTests.py @@ -58,7 +58,8 @@ if content is None: self.assertFalse(os.path.exists(fn)) else: - restoredContent = file(fn).read() + with open(fn) as f: + restoredContent = f.read() self.assertEqual(content, restoredContent) def assertEqualXml(self, a, b, msg=None): diff --git a/tests/tcTests.py b/tests/tcTests.py index 9017746..65fab3d 100644 --- a/tests/tcTests.py +++ b/tests/tcTests.py @@ -197,7 +197,8 @@ def test_filter_objs(self): dirName = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(dirName, "tc_filter_show.out") - out = file(path).read() + with open(path) as f: + out = f.read() PARSED_FILTERS = ( tc.Filter(prio=49149, handle='803::800', actions=[tc.MirredAction(target='tap1')]), diff --git a/vds_bootstrap/vds_bootstrap.py b/vds_bootstrap/vds_bootstrap.py index 242608f..a94c9c3 100755 --- a/vds_bootstrap/vds_bootstrap.py +++ b/vds_bootstrap/vds_bootstrap.py @@ -241,9 +241,10 @@ def _constantTSC(): - return all(' constant_tsc ' in line - for line in file('/proc/cpuinfo') - if line.startswith('flags\t')) + with open('/proc/cpuinfo') as info: + return all(' constant_tsc ' in line + for line in info + if line.startswith('flags\t')) class Deploy: diff --git a/vdsm/API.py b/vdsm/API.py index e3248ff..a7d61c4 100644 --- a/vdsm/API.py +++ b/vdsm/API.py @@ -1623,8 +1623,8 @@ if v.conf['pid'] == '0': continue try: - statmfile = file('/proc/' + v.conf['pid'] + '/statm') - resident += int(statmfile.read().split()[1]) + with open('/proc/' + v.conf['pid'] + '/statm') as statmfile: + resident += int(statmfile.read().split()[1]) except: pass resident *= PAGE_SIZE_BYTES diff --git a/vdsm/caps.py b/vdsm/caps.py index da4e462..5c8aafd 100644 --- a/vdsm/caps.py +++ b/vdsm/caps.py @@ -152,15 +152,16 @@ p = {} self._arch = platform.machine() - for line in file(cpuinfo): - if line.strip() == '': - p = {} - continue - key, value = map(str.strip, line.split(':', 1)) - if key == 'processor': - self._info[value] = p - else: - p[key] = value + with open(cpuinfo) as info: + for line in info: + if line.strip() == '': + p = {} + continue + key, value = map(str.strip, line.split(':', 1)) + if key == 'processor': + self._info[value] = p + else: + p[key] = value def flags(self): if self._arch == Architecture.X86_64: @@ -489,8 +490,8 @@ def _getIscsiIniName(): try: - return _parseKeyVal( - file('/etc/iscsi/initiatorname.iscsi'))['InitiatorName'] + with open('/etc/iscsi/initiatorname.iscsi') as f: + return _parseKeyVal(f)['InitiatorName'] except: logging.error('reporting empty InitiatorName', exc_info=True) return '' diff --git a/vdsm/hooks.py b/vdsm/hooks.py index c0d5d0d..5b1b457 100644 --- a/vdsm/hooks.py +++ b/vdsm/hooks.py @@ -109,7 +109,8 @@ if errorSeen and raiseError: raise HookError() - final_data = file(data_filename).read() + with open(data_filename) as f: + final_data = f.read() finally: os.unlink(data_filename) if hookType == _DOMXML_HOOK: diff --git a/vdsm/ppc64HardwareInfo.py b/vdsm/ppc64HardwareInfo.py index 1a4b30e..d88aeff 100644 --- a/vdsm/ppc64HardwareInfo.py +++ b/vdsm/ppc64HardwareInfo.py @@ -30,17 +30,18 @@ 'systemUUID': 'unavailable', 'systemManufacturer': 'unavailable'} - for line in file('/proc/cpuinfo'): - if line.strip() == '': - continue - key, value = map(str.strip, line.split(':', 1)) + with open('/proc/cpuinfo') as info: + for line in info: + if line.strip() == '': + continue + key, value = map(str.strip, line.split(':', 1)) - if key == 'platform': - infoStructure['systemFamily'] = value - elif key == 'model': - infoStructure['systemSerialNumber'] = value - elif key == 'machine': - infoStructure['systemVersion'] = value + if key == 'platform': + infoStructure['systemFamily'] = value + elif key == 'model': + infoStructure['systemSerialNumber'] = value + elif key == 'machine': + infoStructure['systemVersion'] = value if os.path.exists('/proc/device-tree/system-id'): with open('/proc/device-tree/system-id') as f: diff --git a/vdsm/substitute_constants.py b/vdsm/substitute_constants.py index f1a0b4e..6cecb5c 100644 --- a/vdsm/substitute_constants.py +++ b/vdsm/substitute_constants.py @@ -40,7 +40,7 @@ if fname == '-': f = sys.stdin else: - f = file(fname) + f = open(fname) s = f.read() r = re.sub('@[^@\n]*@', replacement, s) @@ -48,6 +48,6 @@ if fname == '-': f = sys.stdout else: - f = file(fname, 'w') + f = open(fname, 'w') f.write(r) diff --git a/vdsm/virt/sampling.py b/vdsm/virt/sampling.py index 0b2409d..c021ce7 100644 --- a/vdsm/virt/sampling.py +++ b/vdsm/virt/sampling.py @@ -124,8 +124,9 @@ The sample is taken at initialization time and can't be updated. """ def __init__(self): - self.user, userNice, self.sys, self.idle = \ - map(int, file('/proc/stat').readline().split()[1:5]) + with open('/proc/stat') as f: + self.user, userNice, self.sys, self.idle = \ + map(int, f.readline().split()[1:5]) self.user += userNice diff --git a/vdsm_hooks/hostusb/after_vm_destroy.py b/vdsm_hooks/hostusb/after_vm_destroy.py index a3e0e70..2603066 100755 --- a/vdsm_hooks/hostusb/after_vm_destroy.py +++ b/vdsm_hooks/hostusb/after_vm_destroy.py @@ -22,20 +22,18 @@ if not os.path.isfile(HOOK_HOSTUSB_PATH): return uid, pid - f = file(HOOK_HOSTUSB_PATH, 'r') - for line in f: - if len(line) > 0 and line.split(':')[0] == devpath: - entry = line.split(':') - uid = entry[1] - pid = entry[2] - elif len(line) > 0: - content += line + '\n' + with open(HOOK_HOSTUSB_PATH, 'r') as f: + for line in f: + if len(line) > 0 and line.split(':')[0] == devpath: + entry = line.split(':') + uid = entry[1] + pid = entry[2] + elif len(line) > 0: + content += line + '\n' - f.close() if uid != -1: - f = file(HOOK_HOSTUSB_PATH, 'w') - f.writelines(content) - f.close() + with open(HOOK_HOSTUSB_PATH, 'w') as f: + f.writelines(content) return uid, pid diff --git a/vdsm_hooks/hostusb/before_vm_start.py b/vdsm_hooks/hostusb/before_vm_start.py index 2b0beea..5403515 100755 --- a/vdsm_hooks/hostusb/before_vm_start.py +++ b/vdsm_hooks/hostusb/before_vm_start.py @@ -48,15 +48,13 @@ os.mkdir(os.path.dirname(HOOK_HOSTUSB_PATH)) if os.path.isfile(HOOK_HOSTUSB_PATH): - f = file(HOOK_HOSTUSB_PATH, 'r') - for line in f: - if entry == line: - f.close() - return + with open(HOOK_HOSTUSB_PATH, 'r') as f: + for line in f: + if entry == line: + return - f = file(HOOK_HOSTUSB_PATH, 'a') - f.writelines(entry) - f.close() + with open(HOOK_HOSTUSB_PATH, 'a') as f: + f.writelines(entry) # !TODO: diff --git a/vdsm_hooks/hugepages/after_vm_destroy.py b/vdsm_hooks/hugepages/after_vm_destroy.py index 2fb5e85..d9e378c 100755 --- a/vdsm_hooks/hugepages/after_vm_destroy.py +++ b/vdsm_hooks/hugepages/after_vm_destroy.py @@ -8,9 +8,8 @@ def removeSysHugepages(pages): - f = file(NUMBER_OF_HUGETPAGES, 'r') - currPages = int(f.read()) - f.close() + with open(NUMBER_OF_HUGETPAGES, 'r') as f: + currPages = int(f.read()) totalPages = currPages - pages os.system('sudo sysctl vm.nr_hugepages=%d' % totalPages) diff --git a/vdsm_hooks/hugepages/before_vm_migrate_destination.py b/vdsm_hooks/hugepages/before_vm_migrate_destination.py index d076398..af1c4af 100755 --- a/vdsm_hooks/hugepages/before_vm_migrate_destination.py +++ b/vdsm_hooks/hugepages/before_vm_migrate_destination.py @@ -10,9 +10,8 @@ def addSysHugepages(pages): - f = file(NUMBER_OF_HUGETPAGES, 'r') - currPages = int(f.read()) - f.close() + with open(NUMBER_OF_HUGETPAGES, 'r') as f: + currPages = int(f.read()) totalPages = pages + currPages # command: sysctl vm.nr_hugepages=256 @@ -23,17 +22,15 @@ 'command: %s, err = %s\n' % (' '.join(command), err)) sys.exit(2) - f = file(NUMBER_OF_HUGETPAGES, 'r') - newCurrPages = int(f.read()) - f.close() + with open(NUMBER_OF_HUGETPAGES, 'r') as f: + newCurrPages = int(f.read()) return (newCurrPages - currPages) def freeSysHugepages(pages): - f = file(NUMBER_OF_HUGETPAGES, 'r') - currPages = int(f.read()) - f.close() + with open(NUMBER_OF_HUGETPAGES, 'r') as f: + currPages = int(f.read()) if pages > 0: # command: sysctl vm.nr_hugepages=0 diff --git a/vdsm_hooks/hugepages/before_vm_start.py b/vdsm_hooks/hugepages/before_vm_start.py index cddf475..37b655e 100755 --- a/vdsm_hooks/hugepages/before_vm_start.py +++ b/vdsm_hooks/hugepages/before_vm_start.py @@ -35,9 +35,8 @@ def addSysHugepages(pages): - f = file(NUMBER_OF_HUGETPAGES, 'r') - currPages = int(f.read()) - f.close() + with open(NUMBER_OF_HUGETPAGES, 'r') as f: + currPages = int(f.read()) totalPages = pages + currPages # command: sysctl vm.nr_hugepages=256 @@ -48,17 +47,15 @@ (' '.join(command), err)) sys.exit(2) - f = file(NUMBER_OF_HUGETPAGES, 'r') - newCurrPages = int(f.read()) - f.close() + with open(NUMBER_OF_HUGETPAGES, 'r') as f: + newCurrPages = int(f.read()) return (newCurrPages - currPages) def freeSysHugepages(pages): - f = file(NUMBER_OF_HUGETPAGES, 'r') - currPages = int(f.read()) - f.close() + with open(NUMBER_OF_HUGETPAGES, 'r') as f: + currPages = int(f.read()) if pages > 0: # command: sysctl vm.nr_hugepages=0 diff --git a/vdsm_reg/deployUtil.py.in b/vdsm_reg/deployUtil.py.in index 4b2e439..2899411 100644 --- a/vdsm_reg/deployUtil.py.in +++ b/vdsm_reg/deployUtil.py.in @@ -352,8 +352,8 @@ elif arch == "ppc64": if os.path.exists('/proc/device-tree/system-id'): # eg. output IBM,03061C14A - return file('/proc/device-tree/system-id').readline().\ - rstrip('\0').replace(",", "") + with open('/proc/device-tree/system-id') as f: + return f.readline().rstrip('\0').replace(",", "") logging.error("getMachineUUID: Could not find machine's UUID.") @@ -374,14 +374,15 @@ macs = [] for b in glob.glob('/sys/class/net/*/device'): - mac = file(os.path.join(os.path.dirname(b), "address")). \ - readline().replace("\n", "") + with open(os.path.join(os.path.dirname(b), "address")) as f: + mac = f.readline().replace("\n", "") macs.append(mac) for b in glob.glob('/proc/net/bonding/*'): - for line in file(b): - if line.startswith("Permanent HW addr: "): - macs.append(line.split(": ")[1].replace("\n", "")) + with open(b) as bond: + for line in bond: + if line.startswith("Permanent HW addr: "): + macs.append(line.split(": ")[1].replace("\n", "")) return set(macs) - set(["", "00:00:00:00:00:00"]) @@ -421,13 +422,14 @@ def _getIfaceByIP(addr): remote = struct.unpack('I', socket.inet_aton(addr))[0] - for line in file('/proc/net/route').readlines()[1:]: - iface, dest, gateway, flags, refcnt, use, metric, \ - mask, mtu, window, irtt = line.split() - dest = int(dest, 16) - mask = int(mask, 16) - if remote & mask == dest & mask: - return iface + with open('/proc/net/route') as f: + for line in f.readlines()[1:]: + iface, dest, gateway, flags, refcnt, use, metric, \ + mask, mtu, window, irtt = line.split() + dest = int(dest, 16) + mask = int(mask, 16) + if remote & mask == dest & mask: + return iface return None # Should never get here w/ default gw @@ -713,12 +715,13 @@ resKeys = [] try: - for key in file(path): - if not key.endswith('\n'): # make sure we have complete lines - key += '\n' - if key != '\n' and not key.endswith(" ovirt-engine\n") or \ - key.startswith("#"): - resKeys.append(key) + with open(path) as f: + for key in f: + if not key.endswith('\n'): # make sure we have complete lines + key += '\n' + if key != '\n' and not key.endswith(" ovirt-engine\n") or \ + key.startswith("#"): + resKeys.append(key) except IOError: logging.debug("Failed to read %s", path) if not strKey.endswith('\n'): @@ -914,24 +917,25 @@ fileName = IFACE_CONFIG + bridgeName try: - for line in file(fileName): - line = line.strip() - if not line or line.startswith('#'): - continue - elif line.startswith("DEVICE=") or line.startswith("HWADDR="): - continue - elif line.startswith("NM_CONTROLLED="): - continue - elif line.startswith("TYPE="): - t = line.split("=", 1)[1].strip() - fIsBridgeDevice = (t == "Bridge") - else: - try: - line = ''.join(shlex.split(line)) - except: - logging.warn("_getBridgeParams: failed to read parse line " - "%s", line) - lstReturn.append(line) + with open(fileName) as f: + for line in f: + line = line.strip() + if not line or line.startswith('#'): + continue + elif line.startswith("DEVICE=") or line.startswith("HWADDR="): + continue + elif line.startswith("NM_CONTROLLED="): + continue + elif line.startswith("TYPE="): + t = line.split("=", 1)[1].strip() + fIsBridgeDevice = (t == "Bridge") + else: + try: + line = ''.join(shlex.split(line)) + except: + logging.warn("_getBridgeParams: failed to read parse" + " line %s", line) + lstReturn.append(line) except Exception as e: logging.error("_getBridgeParams: failed to read params of file " + fileName + ".\n Error:" + str(e)) @@ -1465,18 +1469,18 @@ def _cpuid(func): - f = file('/dev/cpu/0/cpuid') - f.seek(func) - return struct.unpack('IIII', f.read(16)) + with open('/dev/cpu/0/cpuid') as f: + f.seek(func) + return struct.unpack('IIII', f.read(16)) def _prdmsr(cpu, index): - f = file("/dev/cpu/%d/msr" % cpu) - f.seek(index) - try: - return struct.unpack('L', f.read(8))[0] - except: - return -1 + with open("/dev/cpu/%d/msr" % cpu) as f: + f.seek(index) + try: + return struct.unpack('L', f.read(8))[0] + except: + return -1 def _cpu_has_vmx_support(): @@ -1521,31 +1525,33 @@ def _check_kvm_support_on_power(): - for line in file('/proc/cpuinfo').readlines(): - if ':' in line: - k, v = line.split(':', 1) - k = k.strip() - v = v.strip() - if k == 'platform': - if 'powernv' in v.lower(): - return 1 + with open('/proc/cpuinfo') as info: + for line in info.readlines(): + if ':' in line: + k, v = line.split(':', 1) + k = k.strip() + v = v.strip() + if k == 'platform': + if 'powernv' in v.lower(): + return 1 return 0 def cpuVendorID(): - for line in file('/proc/cpuinfo').readlines(): - if ':' in line: - k, v = line.split(':', 1) - k = k.strip() - v = v.strip() - if k == 'vendor_id' or k == 'cpu': - if v == 'GenuineIntel': - return v - elif v == 'AuthenticAMD': - return v - elif 'power' in v.lower(): - return 'IBM_POWER' + with open('/proc/cpuinfo') as info: + for line in info.readlines(): + if ':' in line: + k, v = line.split(':', 1) + k = k.strip() + v = v.strip() + if k == 'vendor_id' or k == 'cpu': + if v == 'GenuineIntel': + return v + elif v == 'AuthenticAMD': + return v + elif 'power' in v.lower(): + return 'IBM_POWER' return '' -- To view, visit http://gerrit.ovirt.org/33859 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iab7c37fdc18cafa9dfcbd2e9a8fe4f82022dcca0 Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Yeela Kaplan <[email protected]> _______________________________________________ vdsm-patches mailing list [email protected] https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches
