Skip to content

add utilities for running commands and folder permission checking#153

Open
Jun Aishima (JunAishima) wants to merge 1 commit intoNSLS2:mainfrom
JunAishima:add-folder-and-command-running-utils
Open

add utilities for running commands and folder permission checking#153
Jun Aishima (JunAishima) wants to merge 1 commit intoNSLS2:mainfrom
JunAishima:add-folder-and-command-running-utils

Conversation

@JunAishima
Copy link
Copy Markdown

  • from py4xs.utils and lix_profile_collection/03-security,
    respectively
  • check_access was requested by Maksim, run looked useful to me

 * from py4xs.utils and lix_profile_collection/03-security,
   respectively
Copy link
Copy Markdown
Contributor

@mrakitin Max Rakitin (mrakitin) left a comment

Choose a reason for hiding this comment

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

Thanks for porting the tools. The code needs a little work to make it more compatible with the rest of the code base.

Comment thread nslsii/utils.py
import subprocess


def run(cmd, path="", ignoreErrors=True, returnError=False, debug=False):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer to name the kwargs in the snake_case style, i.e.:

  • ignoreErrors -> ignore_errors
  • returnError -> return_error

Comment thread nslsii/utils.py


def run(cmd, path="", ignoreErrors=True, returnError=False, debug=False):
"""cmd should be a list, e.g. ["ls", "-lh"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The docstring should have a break between the first line and the following lines. Maybe an introductory sentence about this function can be useful.

Also, run seems to be too vague. Maybe something more specific such as execute_command can be more appropriate.

Comment thread nslsii/utils.py
Comment on lines +13 to +21
if debug:
print(out.decode(), err.decode())
if len(err) > 0 and not ignoreErrors:
print(err.decode())
raise Exception(err.decode())
if returnError:
return out.decode(), err.decode()
else:
return out.decode()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

out.decode() and err.decode() are used a few times. Maybe worth calling them once, and reuse via variables?

Comment thread nslsii/utils.py

# this below may not be necessary
out = run(["getfacl", "-cn", fn])
wgrps = [int(t[:-4].lstrip("group:")) for t in re.findall("groups:[0-9]*:rw.", out)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is -4 here for? Can it ever be more or less than 4 symbols?
Maybe it's worth adding an example (anonymized) output of that search to have a better understanding of what kind of data we are dealing with here.

Comment thread nslsii/utils.py
if not os.path.exists(fn):
raise Exception(f"{fn} does not exist ...")
if os.access(fn, os.W_OK):
print(f"write access to {fn} verified ...")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this message, I think it will make sense to add a few words about this being verified via Unix permissions.

Comment thread nslsii/utils.py
print("user group membership: ", ugrps)
raise Exception(f"the current user does not have write access to {fn}")
else:
print(f"write access to {fn} verified ...")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this message, I think it will make sense to add a few words about this being verified via ACL (getfacl).

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.

2 participants