def chmod_recursive(path, recursive_mode_flags, recursion_follow_links=False): """ Change recursively permissions on directories or files :type path str :type recursive_mode_flags :type recursion_follow_links bool """ dir_attrib = recursive_mode_flags[ "d"] if "d" in recursive_mode_flags else None files_attrib = recursive_mode_flags[ "f"] if "d" in recursive_mode_flags else None for root, dirs, files in unicode_walk( path, followlinks=recursion_follow_links): if dir_attrib is not None: for dir_name in dirs: full_dir_path = os.path.join(root, dir_name) chmod( full_dir_path, attr_to_bitmask( dir_attrib, initial_bitmask=os.stat(full_dir_path).st_mode)) if files_attrib is not None: for file_name in files: full_file_path = os.path.join(root, file_name) chmod( full_file_path, attr_to_bitmask( files_attrib, initial_bitmask=os.stat(full_file_path).st_mode))
def chmod_extended(path, mode): """ :type path str :type mode str """ st = os.stat(path) os.chmod(path, attr_to_bitmask(mode, initial_bitmask=st.st_mode))
def test_attr_to_bitmask(self): test_set = [ ["+r", stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH, 0], ["u+w", stat.S_IWUSR, 0], ["uo+x", stat.S_IXUSR | stat.S_IXOTH, 0], ["-x", stat.S_IRUSR, stat.S_IXUSR | stat.S_IXOTH | stat.S_IRUSR], ["=x", stat.S_IXUSR | stat.S_IXOTH | stat.S_IXGRP, stat.S_IRUSR | stat.S_IRGRP] ] for test in test_set: test_pattern, expected, initial_val = test bitmask = attr_to_bitmask(test_pattern, initial_bitmask= initial_val) self.assertEquals(expected, bitmask, "Test set \"{0}\" failed, expected: {1} but got {2}".format( test_pattern, expected, bitmask))