-
Notifications
You must be signed in to change notification settings - Fork 25
add utilities for running commands and folder permission checking #153
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import os | ||
| import re | ||
| import subprocess | ||
|
|
||
|
|
||
| def run(cmd, path="", ignoreErrors=True, returnError=False, debug=False): | ||
| """cmd should be a list, e.g. ["ls", "-lh"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
| path is for the cmd, not the same as cwd | ||
| """ | ||
| cmd[0] = path + cmd[0] | ||
| p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
| out, err = p.communicate() | ||
| 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() | ||
|
Comment on lines
+13
to
+21
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| def check_access(fn): | ||
| 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 ...") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| return | ||
|
|
||
| # 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)] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
| ugrps = os.getgroups() | ||
| if len(set(wgrps) & set(ugrps)) == 0: | ||
| print("groups with write permission: ", wgrps) | ||
| 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 ...") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( |
||
There was a problem hiding this comment.
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_errorsreturnError->return_error