VirtualBox

Changeset 94123 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Mar 8, 2022 1:51:26 PM (3 years ago)
Author:
vboxsync
Message:

ValKit/utils.py,tests: Move crc32_of_file to common/utils.py as calcCrc32OfFile, no point in having two copies of it. pylint fixes.

Location:
trunk/src/VBox/ValidationKit
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/common/utils.py

    r93115 r94123  
    214214        try:
    215215            # 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();
    221221        except:
    222222            pass;
     
    235235                if os.path.isfile(sFile):
    236236                    try:
    237                         oFile = open(sFile);
    238                         sLine = oFile.readline();
    239                         oFile.close();
     237                        with open(sFile) as oFile:
     238                            sLine = oFile.readline();
    240239                    except:
    241240                        continue;
     
    249248        if os.path.isfile('/etc/release'):
    250249            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];
    254252                sLast = sLast.strip();
    255253                if sLast:
     
    364362    uPythonVer = (sys.version_info[0] << 16) | (sys.version_info[1] & 0xffff);
    365363    if uPythonVer >= ((3 << 16) | 4):
    366         oFile = open(sFile, sMode);
     364        oFile = open(sFile, sMode);                             # pylint: disable=consider-using-with
    367365    else:
    368366        try:
     
    374372                    offComma = sMode.find(',');
    375373                    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:]);
    378377
    379378            # 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
    383382        #try:
    384383        fcntl(oFile, F_SETFD, fcntl(oFile, F_GETFD) | FD_CLOEXEC);
     
    434433            fOpen |= os.O_APPEND;
    435434        if 'b' in sMode or 't' in sMode:
    436             fOpen |= os.O_TEXT;                                                                 # pylint: disable=no-member
     435            fOpen |= os.O_TEXT;                                         # pylint: disable=no-member
    437436        fdFile = msvcrt.open_osfhandle(hDetachedFile, fOpen);
    438437
     
    440439        oFile = os.fdopen(fdFile, sMode);
    441440    else:
    442         oFile = open(sFile, sMode);
     441        oFile = open(sFile, sMode);                                     # pylint: disable=consider-using-with
    443442
    444443        # Python 3.4 and later automatically creates non-inherit handles. See PEP-0446.
     
    446445        if uPythonVer < ((3 << 16) | 4):
    447446            try:
    448                 from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl; # pylint: disable=import-error
     447                from fcntl import FD_CLOEXEC, F_GETFD, F_SETFD, fcntl;  # pylint: disable=import-error
    449448            except:
    450449                pass;
     
    469468    Reads the entire file.
    470469    """
    471     oFile = open(sFile, sMode);
    472     sRet = oFile.read();
    473     oFile.close();
     470    with open(sFile, sMode) as oFile:
     471        sRet = oFile.read();
    474472    return sRet;
    475473
     
    675673        if dKeywordArgs.get('creationflags', 0) == 0:
    676674            dKeywordArgs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP;
    677     return subprocess.Popen(*aPositionalArgs, **dKeywordArgs);
     675    return subprocess.Popen(*aPositionalArgs, **dKeywordArgs);  # pylint: disable=consider-using-with
    678676
    679677def processCall(*aPositionalArgs, **dKeywordArgs):
     
    778776            except:
    779777                sVersion = '1.7.0';
    780             sVersion = sVersion.strip().split('\n')[0];
     778            sVersion = sVersion.strip().split('\n', 1)[0];
    781779            sVersion = sVersion.replace('Sudo version', '').strip();
    782780            g_fOldSudo = len(sVersion) >= 4 \
     
    10391037        if asPsCmd is not None:
    10401038            try:
    1041                 oPs = subprocess.Popen(asPsCmd, stdout=subprocess.PIPE);
     1039                oPs = subprocess.Popen(asPsCmd, stdout=subprocess.PIPE);    # pylint: disable=consider-using-with
    10421040                sCurName = oPs.communicate()[0];
    10431041                iExitCode = oPs.wait();
     
    13411339                sFull = os.path.join(sDir, sEntry);
    13421340                try:
    1343                     oFile = open(sFull, 'r');
    1344                     sFirstLine = oFile.readline();
    1345                     oFile.close();
     1341                    with open(sFull, 'r') as oFile:
     1342                        sFirstLine = oFile.readline();
    13461343                except:
    13471344                    continue;
     
    21272124
    21282125    # Open it.
    2129     try: oZipFile = zipfile.ZipFile(sArchive, 'r')
     2126    try: oZipFile = zipfile.ZipFile(sArchive, 'r');             # pylint: disable=consider-using-with
    21302127    except Exception as oXcpt:
    21312128        fnError('Error opening "%s" for unpacking into "%s": %s' % (sArchive, sDstDir, oXcpt,));
     
    22102207    try:
    22112208        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);
    22132211        else:
    2214             oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize);
     2212            oTarFile = tarfile.open(sArchive, 'r|*', bufsize = g_cbGoodBufferSize); # pylint: disable=consider-using-with
    22152213    except Exception as oXcpt:
    22162214        fnError('Error opening "%s" for unpacking into "%s": %s' % (sArchive, sDstDir, oXcpt,));
     
    24042402
    24052403
     2404def 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
    24062423#
    24072424# Unit testing.
  • trunk/src/VBox/ValidationKit/tests/storage/tdStorageRawDrive1.py

    r93115 r94123  
    3131# Standard Python imports.
    3232import os;
     33import re;
    3334import sys;
    34 import re;
    35 import zlib;
    3635
    3736# Only the main script needs to modify the path.
     
    4241
    4342# Validation Kit imports.
     43from common     import utils;
    4444from testdriver import reporter;
    4545from testdriver import base;
     
    4949from testdriver import vboxwrappers;
    5050
    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             break
    60         current = zlib.crc32(buf, current);
    61 
    62     fileobj.close();
    63     return current % 2**32;
    6451
    6552class tdStorageRawDriveOs(vboxtestvms.BaseTestVm):
     
    12121199                                        reporter.error('Download vmdktest-pt.vmdk from guest to host failed');
    12131200                                    else:
    1214                                         uResCrc32 = crc32_of_file(sDstFile);
     1201                                        uResCrc32 = utils.calcCrc32OfFile(sDstFile);
    12151202                                        if uResCrc32 != action['data-crc'][sHdd]:
    12161203                                            fRc = reporter.error('vmdktest-pt.vmdk does not match what was expected');
  • trunk/src/VBox/ValidationKit/tests/storage/tdStorageSnapshotMerging1.py

    r93115 r94123  
    4343
    4444# Validation Kit imports.
     45from common     import utils;
    4546from testdriver import reporter;
    4647from testdriver import base;
     
    5253if sys.version_info[0] >= 3:
    5354    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             break
    64         current = zlib.crc32(buf, current);
    65 
    66     fileobj.close();
    67     return current % 2**32;
    6855
    6956
     
    349336                    uResCrc32 = long(0);
    350337                    if fRc:
    351                         uResCrc32 = long(crc32_of_file(sResFilePathRaw));
     338                        uResCrc32 = long(utils.calcCrc32OfFile(sResFilePathRaw));
    352339                        if uResCrc32 == uOrigCrc:
    353340                            reporter.log('Snapshot merged successfully. Crc32 is correct');
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette