Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file.stat function not working under Python 3 #52174

Closed
amendlik opened this issue Mar 14, 2019 · 5 comments

Comments

@amendlik
Copy link
Contributor

commented Mar 14, 2019

Description of Issue/Question

The file.stat execution function does return file permissions properly under Python 3.
Under Python 2, the mode item would be all numbers, such as 0660.
Under Python 3, the mode item is prefixed with 0o, such as 0o660

This would seem to be due to a change in the oct() built-in function between Python 2 and 3:

https://docs.python.org/2/library/functions.html#oct
Convert an integer number (of any size) to an octal string.

https://docs.python.org/3/library/functions.html#oct
Convert an integer number to an octal string prefixed with “0o”.

This is causing dependent modules, such as states.file to fail. #52173

@Ch3LL

This comment has been minimized.

Copy link
Contributor

commented Mar 14, 2019

was this working previously?

The last time I saw this line ret['mode'] = six.text_type(oct(stat.S_IMODE(pstat.st_mode))) (which i believe is what you are referring to) change was 2017-12-12

@Ch3LL Ch3LL added the Info Needed label Mar 14, 2019
@Ch3LL Ch3LL added this to the Blocked milestone Mar 14, 2019
@amendlik

This comment has been minimized.

Copy link
Contributor Author

commented Mar 14, 2019

I only recently switched to the Python 3 version. I have not seen this working correctly since moving to Python 3.

@markuskramerIgitt

This comment has been minimized.

Copy link
Contributor

commented Mar 15, 2019

I think the file module unit test does not cover file.stat.

I could only add a test that is run by Jenkins, or I would need to learn how to run test locally.

@amendlik

This comment has been minimized.

Copy link
Contributor Author

commented Mar 15, 2019

There is an existing function to handle this difference between Python 2 & 3 in salt.utils.file:

def normalize_mode(mode):
    '''
    Return a mode value, normalized to a string and containing a leading zero
    if it does not have one.

    Allow "keep" as a valid mode (used by file state/module to preserve mode
    from the Salt fileserver in file states).
    '''
    if mode is None:
        return None
    if not isinstance(mode, six.string_types):
        mode = six.text_type(mode)
    if six.PY3:
        mode = mode.replace('0o', '0')
    # Strip any quotes any initial zeroes, then though zero-pad it up to 4.
    # This ensures that somethign like '00644' is normalized to '0644'
    return mode.strip('"').strip('\'').lstrip('0').zfill(4)

I will make the changes to call the normalize_mode function.

@Ch3LL

This comment has been minimized.

Copy link
Contributor

commented Mar 20, 2019

Thanks for the PR! I'll go ahead close here and we can discuss on the PR. let me know if i need to re-open though

@Ch3LL Ch3LL closed this Mar 20, 2019
@Ch3LL Ch3LL added Bug High Severity P3 and removed Info Needed labels Mar 20, 2019
@Ch3LL Ch3LL modified the milestones: Blocked, Approved Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.