MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / chmod

Function chmod

src/commoncode/fileutils.py:489–517  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

487
488# FIXME: This was an expensive operation that used to recurse of the parent directory
489def 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
520def chmod_tree(location, flags):

Callers 5

create_dirFunction · 0.70
copytreeFunction · 0.70
copyfileFunction · 0.70
chmod_treeFunction · 0.70
_rm_handlerFunction · 0.70

Calls 4

is_rwxFunction · 0.90
chmod_treeFunction · 0.85
is_dirMethod · 0.80
existsMethod · 0.45

Tested by

no test coverage detected