Changeset 94123 in vbox for trunk/src/VBox
- Timestamp:
- Mar 8, 2022 1:51:26 PM (3 years ago)
- Location:
- trunk/src/VBox/ValidationKit
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/ValidationKit/common/utils.py
r93115 r94123 214 214 try: 215 215 # try /etc/lsb-release first to distinguish between Debian and Ubuntu 216 oFile = open('/etc/lsb-release');217 for sLine in oFile:218 oMatch = re.search(r'(?:DISTRIB_DESCRIPTION\s*=)\s*"*(.*)"', sLine);219 if oMatch is not None:220 sDist = oMatch.group(1).strip();216 with open('/etc/lsb-release') as oFile: 217 for sLine in oFile: 218 oMatch = re.search(r'(?:DISTRIB_DESCRIPTION\s*=)\s*"*(.*)"', sLine); 219 if oMatch is not None: 220 sDist = oMatch.group(1).strip(); 221 221 except: 222 222 pass; … … 235 235 if os.path.isfile(sFile): 236 236 try: 237 oFile = open(sFile); 238 sLine = oFile.readline(); 239 oFile.close(); 237 with open(sFile) as oFile: 238 sLine = oFile.readline(); 240 239 except: 241 240 continue; … … 249 248 if os.path.isfile('/etc/release'): 250 249 try: 251 oFile = open('/etc/release'); 252 sLast = oFile.readlines()[-1]; 253 oFile.close(); 250 with open('/etc/release') as oFile: 251 sLast = oFile.readlines()[-1]; 254 252 sLast = sLast.strip(); 255 253 if sLast: … … 364 362 uPythonVer = (sys.version_info[0] << 16) | (sys.version_info[1] & 0xffff); 365 363 if uPythonVer >= ((3 << 16) | 4): 366 oFile = open(sFile, sMode); 364 oFile = open(sFile, sMode); # pylint: disable=consider-using-with 367 365 else: 368 366 try: … … 374 372 offComma = sMode.find(','); 375 373 if offComma < 0: 376 return open(sFile, sMode + 'N'); 377 return open(sFile, sMode[:offComma] + 'N' + sMode[offComma:]); # pylint: disable=bad-open-mode 374 return open(sFile, sMode + 'N'); # pylint: disable=consider-using-with 375 return open(sFile, # pylint: disable=consider-using-with,bad-open-mode 376 sMode[:offComma] + 'N' + sMode[offComma:]); 378 377 379 378 # Just in case. 380 return open(sFile, sMode); 381 382 oFile = open(sFile, sMode); 379 return open(sFile, sMode); # pylint: disable=consider-using-with 380 381 oFile = open(sFile, sMode); # pylint: disable=consider-using-with 383 382 #try: 384 383 fcntl(oFile, F_SETFD, fcntl(oFile, F_GETFD) | FD_CLOEXEC); … … 434 433 fOpen |= os.O_APPEND; 435 434 if 'b' in sMode or 't' in sMode: 436 fOpen |= os.O_TEXT; 435 fOpen |= os.O_TEXT; # pylint: disable=no-member 437 436 fdFile = msvcrt.open_osfhandle(hDetachedFile, fOpen); 438 437 … … 440 439 oFile = os.fdopen(fdFile, sMode); 441 440 else: 442 oFile = open(sFile, sMode); 441 oFile = open(sFile, sMode); # pylint: disable=consider-using-with 443 442 444 443 # Python 3.4 and later automatically creates non-inherit handles. See PEP-0446. … … 446 445 if uPythonVer < ((3 << 16) | 4): 447 446 try: 448 from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=import-error447 from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=import-error 449 448 except: 450 449 pass; … … 469 468 Reads the entire file. 470 469 """ 471 oFile = open(sFile, sMode); 472 sRet = oFile.read(); 473 oFile.close(); 470 with open(sFile, sMode) as oFile: 471 sRet = oFile.read(); 474 472 return sRet; 475 473 … … 675 673 if dKeywordArgs.get('creationflags', 0) == 0: 676 674 dKeywordArgs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP; 677 return subprocess.Popen(*aPositionalArgs, **dKeywordArgs); 675 return subprocess.Popen(*aPositionalArgs, **dKeywordArgs); # pylint: disable=consider-using-with 678 676 679 677 def processCall(*aPositionalArgs, **dKeywordArgs): … … 778 776 except: 779 777 sVersion = '1.7.0'; 780 sVersion = sVersion.strip().split('\n' )[0];778 sVersion = sVersion.strip().split('\n', 1)[0]; 781 779 sVersion = sVersion.replace('Sudo version', '').strip(); 782 780 g_fOldSudo = len(sVersion) >= 4 \ … … 1039 1037 if asPsCmd is not None: 1040 1038 try: 1041 oPs = subprocess.Popen(asPsCmd, stdout=subprocess.PIPE); 1039 oPs = subprocess.Popen(asPsCmd, stdout=subprocess.PIPE); # pylint: disable=consider-using-with 1042 1040 sCurName = oPs.communicate()[0]; 1043 1041 iExitCode = oPs.wait(); … … 1341 1339 sFull = os.path.join(sDir, sEntry); 1342 1340 try: 1343 oFile = open(sFull, 'r'); 1344 sFirstLine = oFile.readline(); 1345 oFile.close(); 1341 with open(sFull, 'r') as oFile: 1342 sFirstLine = oFile.readline(); 1346 1343 except: 1347 1344 continue; … … 2127 2124 2128 2125 # Open it. 2129 try: oZipFile = zipfile.ZipFile(sArchive, 'r') 2126 try: oZipFile = zipfile.ZipFile(sArchive, 'r'); # pylint: disable=consider-using-with 2130 2127 except Exception as oXcpt: 2131 2128 fnError('Error opening "%s" for unpacking into "%s": %s' % (sArchive, sDstDir, oXcpt,)); … … 2210 2207 try: 2211 2208 if sys.hexversion >= 0x03060000: 2212 oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize, copybufsize = g_cbGoodBufferSize); 2209 oTarFile = tarfile.open(sArchive, 'r|*', # pylint: disable=consider-using-with 2210 bufsize = g_cbGoodBufferSize, copybufsize = g_cbGoodBufferSize); 2213 2211 else: 2214 oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize); 2212 oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize); # pylint: disable=consider-using-with 2215 2213 except Exception as oXcpt: 2216 2214 fnError('Error opening "%s" for unpacking into "%s": %s' % (sArchive, sDstDir, oXcpt,)); … … 2404 2402 2405 2403 2404 def calcCrc32OfFile(sFile): 2405 """ 2406 Simple helper for calculating the CRC32 of a file. 2407 2408 Throws stuff if the file cannot be opened or read successfully. 2409 """ 2410 import zlib; 2411 2412 uCrc32 = 0; 2413 with open(sFile, 'rb') as oFile: 2414 while True: 2415 oBuf = oFile.read(1024 * 1024); 2416 if not oBuf: 2417 break 2418 uCrc32 = zlib.crc32(oBuf, uCrc32); 2419 2420 return uCrc32 % 2**32; 2421 2422 2406 2423 # 2407 2424 # Unit testing. -
trunk/src/VBox/ValidationKit/tests/storage/tdStorageRawDrive1.py
r93115 r94123 31 31 # Standard Python imports. 32 32 import os; 33 import re; 33 34 import sys; 34 import re;35 import zlib;36 35 37 36 # Only the main script needs to modify the path. … … 42 41 43 42 # Validation Kit imports. 43 from common import utils; 44 44 from testdriver import reporter; 45 45 from testdriver import base; … … 49 49 from testdriver import vboxwrappers; 50 50 51 52 def crc32_of_file(filepath):53 fileobj = open(filepath,'rb');54 current = 0;55 56 while True:57 buf = fileobj.read(1024 * 1024);58 if not buf:59 break60 current = zlib.crc32(buf, current);61 62 fileobj.close();63 return current % 2**32;64 51 65 52 class tdStorageRawDriveOs(vboxtestvms.BaseTestVm): … … 1212 1199 reporter.error('Download vmdktest-pt.vmdk from guest to host failed'); 1213 1200 else: 1214 uResCrc32 = crc32_of_file(sDstFile);1201 uResCrc32 = utils.calcCrc32OfFile(sDstFile); 1215 1202 if uResCrc32 != action['data-crc'][sHdd]: 1216 1203 fRc = reporter.error('vmdktest-pt.vmdk does not match what was expected'); -
trunk/src/VBox/ValidationKit/tests/storage/tdStorageSnapshotMerging1.py
r93115 r94123 43 43 44 44 # Validation Kit imports. 45 from common import utils; 45 46 from testdriver import reporter; 46 47 from testdriver import base; … … 52 53 if sys.version_info[0] >= 3: 53 54 long = int; # pylint: disable=redefined-builtin,invalid-name 54 55 56 def crc32_of_file(filepath):57 fileobj = open(filepath,'rb');58 current = 0;59 60 while True:61 buf = fileobj.read(1024 * 1024);62 if not buf:63 break64 current = zlib.crc32(buf, current);65 66 fileobj.close();67 return current % 2**32;68 55 69 56 … … 349 336 uResCrc32 = long(0); 350 337 if fRc: 351 uResCrc32 = long( crc32_of_file(sResFilePathRaw));338 uResCrc32 = long(utils.calcCrc32OfFile(sResFilePathRaw)); 352 339 if uResCrc32 == uOrigCrc: 353 340 reporter.log('Snapshot merged successfully. Crc32 is correct');
Note:
See TracChangeset
for help on using the changeset viewer.