Update permissions for `location` with with `flags`. `flags` is one of R, RW, RX or RWX with the same semantics as in the chmod command. Update is done recursively if `recurse`.
(location, flags, recurse=False)
| 487 | |
| 488 | # FIXME: This was an expensive operation that used to recurse of the parent directory |
| 489 | def chmod(location, flags, recurse=False): |
| 490 | """ |
| 491 | Update permissions for `location` with with `flags`. `flags` is one of R, |
| 492 | RW, RX or RWX with the same semantics as in the chmod command. Update is |
| 493 | done recursively if `recurse`. |
| 494 | """ |
| 495 | if not location or not os.path.exists(location): |
| 496 | return |
| 497 | location = os.path.abspath(location) |
| 498 | |
| 499 | new_flags = flags |
| 500 | if filetype.is_dir(location): |
| 501 | # POSIX dirs need to be executable to be readable, |
| 502 | # and to be writable so we can change perms of files inside |
| 503 | new_flags = RWX |
| 504 | |
| 505 | # FIXME: do we really need to change the parent directory perms? |
| 506 | # FIXME: may just check them instead? |
| 507 | parent = os.path.dirname(location) |
| 508 | current_stat = stat.S_IMODE(os.stat(parent).st_mode) |
| 509 | if not is_rwx(parent): |
| 510 | os.chmod(parent, current_stat | RWX) |
| 511 | |
| 512 | if filetype.is_regular(location): |
| 513 | current_stat = stat.S_IMODE(os.stat(location).st_mode) |
| 514 | os.chmod(location, current_stat | new_flags) |
| 515 | |
| 516 | if recurse: |
| 517 | chmod_tree(location, flags) |
| 518 | |
| 519 | |
| 520 | def chmod_tree(location, flags): |
no test coverage detected