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

[tools] Add chmod in tools.files #17800

Open
wants to merge 11 commits into
base: develop2
Choose a base branch
from

Conversation

uilianries
Copy link
Member

@uilianries uilianries commented Feb 18, 2025

Changelog: Feature: Integrate chmod feature in tools.files
Docs: TODO

The tools.files.chmod is a wrapper for os.chmod, simplifying it's usage.

It will change permissions only related to the file's owner and will follow os.chmod behavior as well. On Windows, chmod is limited to writing permission changes; adding executable permission is out of scope.

Reference: https://docs.python.org/3/library/os.html#os.chmod

closes #8003

/cc @memsharded

  • Refer to the issue that supports this Pull Request.
  • If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
  • I've read the Contributing guide.
  • I've followed the PEP8 style guides for Python code.
  • I've opened another PR in the Conan docs repo to the develop branch, documenting this one.

Signed-off-by: Uilian Ries <[email protected]>
Signed-off-by: Uilian Ries <[email protected]>
Signed-off-by: Uilian Ries <[email protected]>
@@ -263,6 +264,47 @@ def chdir(conanfile, newdir):
os.chdir(old_path)


def chmod(conanfile, path:str, read:bool, write:bool, execute:bool, recursive:bool=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def chmod(conanfile, path:str, read:bool, write:bool, execute:bool, recursive:bool=False):
def chmod(conanfile, path:str, read:bool=None, write:bool=None, execute:bool=None, recursive:bool=False):

Using None to mean "unchanged" for a chmod +x or chmod +w equivalent would provide a very useful and intuitive interface, e.g. chmod(self, some_exe, execute=True)?

:param execute: If ``True``, the file or directory will have execute permissions for any user.
:param recursive: (Optional, Defaulted to ``False``) If ``True``, the permissions will be applied
"""
if platform.system() == "Windows":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? ConanCenter recipes very rarely do this, only in a couple of occcasions.
And chmod in Windows still can change the read-only flag, so if the function has read argument, then it should work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but what should be done when passing execute or write? Just ignore or rise an error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever the underlying os.chmod does. This is mostly a convenience wrapper for it, the overall expected behavior should be aligned.

def _change_permission(it_path:str):
mode = os.stat(it_path).st_mode
permissions = [
(read, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure the default should be setting the permissions for everyone, but just for the current user.
Recipes in ConanCenter set S_IXUSR/S_IXEXEC only, not all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know some recipes also do os.chmod(filename, os.stat(filename).st_mode | 0o111), but maybe this has to be checked with @jcar87 too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Mostly of the cases are related to tool-requires only.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, looking better now, still I wonder why the read permission will be different to the others, is it not enough that it has "user" read permissions (if for some reason it didn't have it)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, only changed owner permission.

Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me.
Assigning it to @jcar87 for review and check regarding ConanCenter usage

def _change_permission(it_path:str):
mode = os.stat(it_path).st_mode
permissions = [
(read, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, looking better now, still I wonder why the read permission will be different to the others, is it not enough that it has "user" read permissions (if for some reason it didn't have it)?

@uilianries
Copy link
Member Author

Ok, looking better now, still I wonder why the read permission will be different to the others, is it not enough that it has "user" read permissions (if for some reason it didn't have it)?

Only leftover, gonna change it too.

@uilianries uilianries marked this pull request as ready for review February 19, 2025 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] Internalize chmod as Conan tool
4 participants