Project

General

Profile

Actions

Support #59374

closed

Teuthology code confusion

Added by bugwz bugwz about 1 year ago. Updated about 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Category:
teuthology
Target version:
-
% Done:

0%

Tags:
Reviewed:
Affected Versions:
Pull request ID:

Description

When i read the source code of teuthology about ceph ansible, i found a code problem:

def get_scratch_devices(remote):
    """ 
    Read the scratch disk list from remote host
    """ 
    devs = []
    try:
        file_data = remote.read_file("/scratch_devs").decode()
        devs = file_data.split()
    except Exception:
        devs = remote.sh('ls /dev/[sv]d?').strip().split('\n')

    # Remove root device (vm guests) from the disk list
    for dev in devs:
        if 'vda' in dev:
            devs.remove(dev)
            log.warning("Removing root device: %s from device list" % dev)

    log.debug('devs={d}'.format(d=devs))

    retval = []
    for dev in devs:
        dev_checks = [
            [['stat', dev], "does not exist"],
            [['sudo', 'dd', 'if=%s' % dev, 'of=/dev/null', 'count=1'], "is not readable"],
            [
                [run.Raw('!'), 'mount', run.Raw('|'), 'grep', '-v', 'devtmpfs', run.Raw('|'),
                'grep', '-q', dev],
                "is in use" 
            ],
        ]
        for args, msg in dev_checks:
            try:
                remote.run(args=args)
            except CommandFailedError:
                log.debug(f"get_scratch_devices: {dev} {msg}")
                break
        else:
            retval.append(dev)
            continue
        break
    return retval

and you can found this code on: https://github.com/ceph/teuthology/blob/main/teuthology/misc.py#L790

Please check the `else` position is ok ? I am a fresh man in Python. Is `else`'s writing method legal?

Based on the semantics here, I plan to change it to this format:

def get_scratch_devices(remote):
    """ 
    Read the scratch disk list from remote host
    """ 
    devs = []
    try:
        file_data = remote.read_file("/scratch_devs").decode()
        devs = file_data.split()
    except Exception:
        devs = remote.sh('ls /dev/[sv]d?').strip().split('\n')

    # Remove root device (vm guests) from the disk list
    for dev in devs:
        if 'vda' in dev:
            devs.remove(dev)
            log.warning("Removing root device: %s from device list" % dev)

    log.debug('devs={d}'.format(d=devs))

    retval = []
    for dev in devs:
        dev_checks = [
            [['stat', dev], "does not exist"],
            [['sudo', 'dd', 'if=%s' % dev, 'of=/dev/null', 'count=1'], "is not readable"],
            [
                [run.Raw('!'), 'mount', run.Raw('|'), 'grep', '-v', 'devtmpfs', run.Raw('|'),
                'grep', '-q', dev],
                "is in use" 
            ],
        ]

        checkRet = True
        for args, msg in dev_checks:
            try:
                remote.run(args=args)
            except CommandFailedError:
                checkRet = False
                log.debug(f"get_scratch_devices: {dev} {msg}")
                break

        if checkRet:
            retval.append(dev)

    return retval

I don't know if my ideas are correct. If they are correct, I am willing to submit a PR.

Actions #1

Updated by Dan Mick about 1 year ago

What do you believe is the problem? Be specific.

Actions #2

Updated by bugwz bugwz about 1 year ago

when i read the source codes, i have doubts about the last else, i don't think there is an if statement corresponding to him.

Actions #3

Updated by Dan Mick about 1 year ago

did you research the syntax of the for loop in Python?

is "having doubts" enough reason to change the code?

Actions #4

Updated by bugwz bugwz about 1 year ago

Yes, you're right. I haven't studied this grammar before, but now I know it. Thank you for your reply~

Actions #5

Updated by Dan Mick about 1 year ago

  • Status changed from New to Rejected

This would have been a syntax error if for didn't support else. In general, we try to avoid change unless there's a good supportable reason why.

Actions

Also available in: Atom PDF