diff options
author | vboxsync <vboxsync@cfe28804-0f27-0410-a406-dd0f0b0b656f> | 2022-03-08 13:51:26 +0000 |
---|---|---|
committer | vboxsync <vboxsync@cfe28804-0f27-0410-a406-dd0f0b0b656f> | 2022-03-08 13:51:26 +0000 |
commit | dfcd535d45ec474527d6546f10518a6ba40860d9 (patch) | |
tree | f773060e44bbdfb60b6941ff13c02aa10b3c3e8a /src/VBox/ValidationKit | |
parent | 8f46685f0ec71e0d266d73395ffe600f199c3740 (diff) | |
download | VirtualBox-svn-dfcd535d45ec474527d6546f10518a6ba40860d9.tar.gz |
ValKit/utils.py,tests: Move crc32_of_file to common/utils.py as calcCrc32OfFile, no point in having two copies of it. pylint fixes.
git-svn-id: https://www.virtualbox.org/svn/vbox/trunk@94123 cfe28804-0f27-0410-a406-dd0f0b0b656f
Diffstat (limited to 'src/VBox/ValidationKit')
-rw-r--r-- | src/VBox/ValidationKit/common/utils.py | 79 | ||||
-rwxr-xr-x | src/VBox/ValidationKit/tests/storage/tdStorageRawDrive1.py | 19 | ||||
-rwxr-xr-x | src/VBox/ValidationKit/tests/storage/tdStorageSnapshotMerging1.py | 17 |
3 files changed, 53 insertions, 62 deletions
diff --git a/src/VBox/ValidationKit/common/utils.py b/src/VBox/ValidationKit/common/utils.py index aef8bd7ff23..dfd027d47b1 100644 --- a/src/VBox/ValidationKit/common/utils.py +++ b/src/VBox/ValidationKit/common/utils.py @@ -213,11 +213,11 @@ def getHostOsVersion(): sDist = ''; try: # try /etc/lsb-release first to distinguish between Debian and Ubuntu - oFile = open('/etc/lsb-release'); - for sLine in oFile: - oMatch = re.search(r'(?:DISTRIB_DESCRIPTION\s*=)\s*"*(.*)"', sLine); - if oMatch is not None: - sDist = oMatch.group(1).strip(); + with open('/etc/lsb-release') as oFile: + for sLine in oFile: + oMatch = re.search(r'(?:DISTRIB_DESCRIPTION\s*=)\s*"*(.*)"', sLine); + if oMatch is not None: + sDist = oMatch.group(1).strip(); except: pass; if sDist: @@ -234,9 +234,8 @@ def getHostOsVersion(): for sFile, sPrefix in asFiles: if os.path.isfile(sFile): try: - oFile = open(sFile); - sLine = oFile.readline(); - oFile.close(); + with open(sFile) as oFile: + sLine = oFile.readline(); except: continue; sLine = sLine.strip() @@ -248,9 +247,8 @@ def getHostOsVersion(): sVersion = platform.version(); if os.path.isfile('/etc/release'): try: - oFile = open('/etc/release'); - sLast = oFile.readlines()[-1]; - oFile.close(); + with open('/etc/release') as oFile: + sLast = oFile.readlines()[-1]; sLast = sLast.strip(); if sLast: sVersion += ' (' + sLast + ')'; @@ -363,7 +361,7 @@ def openNoInherit(sFile, sMode = 'r'): # Python 3.4 and later automatically creates non-inherit handles. See PEP-0446. uPythonVer = (sys.version_info[0] << 16) | (sys.version_info[1] & 0xffff); if uPythonVer >= ((3 << 16) | 4): - oFile = open(sFile, sMode); + oFile = open(sFile, sMode); # pylint: disable=consider-using-with else: try: from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=import-error @@ -373,13 +371,14 @@ def openNoInherit(sFile, sMode = 'r'): if uPythonVer < (3 << 16): offComma = sMode.find(','); if offComma < 0: - return open(sFile, sMode + 'N'); - return open(sFile, sMode[:offComma] + 'N' + sMode[offComma:]); # pylint: disable=bad-open-mode + return open(sFile, sMode + 'N'); # pylint: disable=consider-using-with + return open(sFile, # pylint: disable=consider-using-with,bad-open-mode + sMode[:offComma] + 'N' + sMode[offComma:]); # Just in case. - return open(sFile, sMode); + return open(sFile, sMode); # pylint: disable=consider-using-with - oFile = open(sFile, sMode); + oFile = open(sFile, sMode); # pylint: disable=consider-using-with #try: fcntl(oFile, F_SETFD, fcntl(oFile, F_GETFD) | FD_CLOEXEC); #except: @@ -433,19 +432,19 @@ def openNoDenyDeleteNoInherit(sFile, sMode = 'r'): if 'a' in sMode: fOpen |= os.O_APPEND; if 'b' in sMode or 't' in sMode: - fOpen |= os.O_TEXT; # pylint: disable=no-member + fOpen |= os.O_TEXT; # pylint: disable=no-member fdFile = msvcrt.open_osfhandle(hDetachedFile, fOpen); # Tell python to use this handle. oFile = os.fdopen(fdFile, sMode); else: - oFile = open(sFile, sMode); + oFile = open(sFile, sMode); # pylint: disable=consider-using-with # Python 3.4 and later automatically creates non-inherit handles. See PEP-0446. uPythonVer = (sys.version_info[0] << 16) | (sys.version_info[1] & 0xffff); if uPythonVer < ((3 << 16) | 4): try: - from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=import-error + from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=import-error except: pass; else: @@ -468,9 +467,8 @@ def readFile(sFile, sMode = 'rb'): """ Reads the entire file. """ - oFile = open(sFile, sMode); - sRet = oFile.read(); - oFile.close(); + with open(sFile, sMode) as oFile: + sRet = oFile.read(); return sRet; def noxcptReadFile(sFile, sXcptRet, sMode = 'rb', sEncoding = 'utf-8'): @@ -674,7 +672,7 @@ def processPopenSafe(*aPositionalArgs, **dKeywordArgs): if getHostOs() == 'win': if dKeywordArgs.get('creationflags', 0) == 0: dKeywordArgs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP; - return subprocess.Popen(*aPositionalArgs, **dKeywordArgs); + return subprocess.Popen(*aPositionalArgs, **dKeywordArgs); # pylint: disable=consider-using-with def processCall(*aPositionalArgs, **dKeywordArgs): """ @@ -777,7 +775,7 @@ def _sudoFixArguments(aPositionalArgs, dKeywordArgs, fInitialEnv = True): sVersion = str(processOutputChecked(['sudo', '-V'])); except: sVersion = '1.7.0'; - sVersion = sVersion.strip().split('\n')[0]; + sVersion = sVersion.strip().split('\n', 1)[0]; sVersion = sVersion.replace('Sudo version', '').strip(); g_fOldSudo = len(sVersion) >= 4 \ and sVersion[0] == '1' \ @@ -1038,7 +1036,7 @@ def processCheckPidAndName(uPid, sName): if asPsCmd is not None: try: - oPs = subprocess.Popen(asPsCmd, stdout=subprocess.PIPE); + oPs = subprocess.Popen(asPsCmd, stdout=subprocess.PIPE); # pylint: disable=consider-using-with sCurName = oPs.communicate()[0]; iExitCode = oPs.wait(); except: @@ -1340,9 +1338,8 @@ def processCollectCrashInfo(uPid, fnLog, fnCrashFile): # The pid can be found at the end of the first line. sFull = os.path.join(sDir, sEntry); try: - oFile = open(sFull, 'r'); - sFirstLine = oFile.readline(); - oFile.close(); + with open(sFull, 'r') as oFile: + sFirstLine = oFile.readline(); except: continue; if len(sFirstLine) <= 4 or sFirstLine[-2] != ']': @@ -2126,7 +2123,7 @@ def unpackZipFile(sArchive, sDstDir, fnLog, fnError = None, fnFilter = None): fnLog('Unzipping "%s" to "%s"...' % (sArchive, sDstDir)); # Open it. - try: oZipFile = zipfile.ZipFile(sArchive, 'r') + try: oZipFile = zipfile.ZipFile(sArchive, 'r'); # pylint: disable=consider-using-with except Exception as oXcpt: fnError('Error opening "%s" for unpacking into "%s": %s' % (sArchive, sDstDir, oXcpt,)); return None; @@ -2209,9 +2206,10 @@ def unpackTarFile(sArchive, sDstDir, fnLog, fnError = None, fnFilter = None): # try: if sys.hexversion >= 0x03060000: - oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize, copybufsize = g_cbGoodBufferSize); + oTarFile = tarfile.open(sArchive, 'r|*', # pylint: disable=consider-using-with + bufsize = g_cbGoodBufferSize, copybufsize = g_cbGoodBufferSize); else: - oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize); + oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize); # pylint: disable=consider-using-with except Exception as oXcpt: fnError('Error opening "%s" for unpacking into "%s": %s' % (sArchive, sDstDir, oXcpt,)); return None; @@ -2403,6 +2401,25 @@ def areBytesEqual(oLeft, oRight): return True; +def calcCrc32OfFile(sFile): + """ + Simple helper for calculating the CRC32 of a file. + + Throws stuff if the file cannot be opened or read successfully. + """ + import zlib; + + uCrc32 = 0; + with open(sFile, 'rb') as oFile: + while True: + oBuf = oFile.read(1024 * 1024); + if not oBuf: + break + uCrc32 = zlib.crc32(oBuf, uCrc32); + + return uCrc32 % 2**32; + + # # Unit testing. # diff --git a/src/VBox/ValidationKit/tests/storage/tdStorageRawDrive1.py b/src/VBox/ValidationKit/tests/storage/tdStorageRawDrive1.py index 8747e66ceac..fa764166996 100755 --- a/src/VBox/ValidationKit/tests/storage/tdStorageRawDrive1.py +++ b/src/VBox/ValidationKit/tests/storage/tdStorageRawDrive1.py @@ -30,9 +30,8 @@ __version__ = "$Id$" # Standard Python imports. import os; -import sys; import re; -import zlib; +import sys; # Only the main script needs to modify the path. try: __file__ @@ -41,6 +40,7 @@ g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.a sys.path.append(g_ksValidationKitDir); # Validation Kit imports. +from common import utils; from testdriver import reporter; from testdriver import base; from testdriver import vbox; @@ -49,19 +49,6 @@ from testdriver import vboxtestvms; from testdriver import vboxwrappers; -def crc32_of_file(filepath): - fileobj = open(filepath,'rb'); - current = 0; - - while True: - buf = fileobj.read(1024 * 1024); - if not buf: - break - current = zlib.crc32(buf, current); - - fileobj.close(); - return current % 2**32; - class tdStorageRawDriveOs(vboxtestvms.BaseTestVm): """ Base autostart helper class to provide common methods. @@ -1211,7 +1198,7 @@ class tdStorageRawDriveOs(vboxtestvms.BaseTestVm): if not fRc: reporter.error('Download vmdktest-pt.vmdk from guest to host failed'); else: - uResCrc32 = crc32_of_file(sDstFile); + uResCrc32 = utils.calcCrc32OfFile(sDstFile); if uResCrc32 != action['data-crc'][sHdd]: fRc = reporter.error('vmdktest-pt.vmdk does not match what was expected'); (fRc1, _, _, _) = self._callVBoxManage(oGuestSession, 'Delete VMDK disk', 60 * 1000, diff --git a/src/VBox/ValidationKit/tests/storage/tdStorageSnapshotMerging1.py b/src/VBox/ValidationKit/tests/storage/tdStorageSnapshotMerging1.py index 53dcd773051..22e237fb0a5 100755 --- a/src/VBox/ValidationKit/tests/storage/tdStorageSnapshotMerging1.py +++ b/src/VBox/ValidationKit/tests/storage/tdStorageSnapshotMerging1.py @@ -42,6 +42,7 @@ g_ksValidationKitDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.a sys.path.append(g_ksValidationKitDir); # Validation Kit imports. +from common import utils; from testdriver import reporter; from testdriver import base; from testdriver import vbox; @@ -53,20 +54,6 @@ if sys.version_info[0] >= 3: long = int; # pylint: disable=redefined-builtin,invalid-name -def crc32_of_file(filepath): - fileobj = open(filepath,'rb'); - current = 0; - - while True: - buf = fileobj.read(1024 * 1024); - if not buf: - break - current = zlib.crc32(buf, current); - - fileobj.close(); - return current % 2**32; - - class tdStorageSnapshot(vbox.TestDriver): # pylint: disable=too-many-instance-attributes """ Storage benchmark. @@ -348,7 +335,7 @@ class tdStorageSnapshot(vbox.TestDriver): # uResCrc32 = long(0); if fRc: - uResCrc32 = long(crc32_of_file(sResFilePathRaw)); + uResCrc32 = long(utils.calcCrc32OfFile(sResFilePathRaw)); if uResCrc32 == uOrigCrc: reporter.log('Snapshot merged successfully. Crc32 is correct'); fRc = True; |