Coverage for pyguymer3/perms.py: 2%

42 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-16 08:31 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def perms( 

5 path, 

6 /, 

7 *, 

8 allowHidden = False, 

9 debug = __debug__, 

10 ensureNFC = True, 

11 filePerms = None, 

12 folderPerms = None, 

13 follow_symlinks = True, 

14 group = None, 

15 return_dsstore = True, 

16 return_symlinks = False, 

17 skips = None, 

18 user = None, 

19): 

20 """Set permissions within a path. 

21 

22 This function sets the file/folder mode and group/user owner of all files 

23 and folders within the path. 

24 

25 Parameters 

26 ---------- 

27 path : str 

28 the path to set permissions within 

29 allowHidden : bool, optional 

30 allow hidden files 

31 debug : bool, optional 

32 print debug messages 

33 ensureNFC : bool, optional 

34 ensure that the Unicode encoding is NFC 

35 filePerms : int, default=None 

36 desired file mode (recommended to supply integer in octal to improve clarity) 

37 folderPerms : int, default=None 

38 desired folder mode (recommended to supply integer in octal to improve clarity) 

39 group : str, default=None 

40 desired group owner 

41 user : str, default=None 

42 desired user owner 

43 follow_symlinks : bool, optional 

44 follow symbolic links 

45 return_dsstore : bool, optional 

46 include ".DS_Store" files in the returned list 

47 return_symlinks : bool, optional 

48 set permissions on symbolic links 

49 skips : list of str, default=[] 

50 a list of strings which, if any are present in a file/folder name, result in the file/folder being skipped 

51 

52 Notes 

53 ----- 

54 Copyright 2017 Thomas Guymer [1]_ 

55 

56 References 

57 ---------- 

58 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

59 """ 

60 

61 # Import standard modules ... 

62 import os 

63 import shutil 

64 import stat 

65 

66 # Import sub-functions ... 

67 from .return_file_list import return_file_list 

68 from .return_folder_list import return_folder_list 

69 from .stat import stat as myStat # NOTE: To avoid name clash. 

70 

71 # Populate default values ... 

72 if skips is None: 

73 skips = [] 

74 

75 # Fetch lists ... 

76 flist = return_file_list( 

77 path, 

78 allowHidden = allowHidden, 

79 debug = debug, 

80 ensureNFC = ensureNFC, 

81 follow_symlinks = follow_symlinks, 

82 return_dsstore = return_dsstore, 

83 return_symlinks = return_symlinks, 

84 ) 

85 dlist = return_folder_list( 

86 path, 

87 allowHidden = allowHidden, 

88 debug = debug, 

89 ensureNFC = ensureNFC, 

90 follow_symlinks = follow_symlinks, 

91 return_symlinks = return_symlinks, 

92 ) 

93 

94 # Loop over files and folders ... 

95 for name in flist + dlist: 

96 # Skip if it needs to be skipped ... 

97 toSkip = False 

98 for skip in skips: 

99 if skip in name: 

100 toSkip = True 

101 break 

102 if toSkip: 

103 continue 

104 

105 # Fetch information ... 

106 info = myStat(name, follow_symlinks = follow_symlinks) 

107 

108 # Set group owner if it is wrong ... 

109 if group is not None: 

110 if info["group"] != group: 

111 if debug: 

112 print(f'INFO: Changing group owner of \"{name}\" to \"{group}\" (it is \"{info["group"]}\").') 

113 shutil.chown(name, group = group) 

114 

115 # Set user owner if it is wrong ... 

116 if user is not None: 

117 if info["user"] != user: 

118 if debug: 

119 print(f'INFO: Changing user owner of \"{name}\" to \"{user}\" (it is \"{info["user"]}\").') 

120 shutil.chown(name, user = user) 

121 

122 # Set file permissions if it is wrong ... 

123 if stat.S_ISREG(info["st_mode"]): 

124 if filePerms is not None: 

125 if stat.S_IMODE(info["st_mode"]) != filePerms: 

126 if debug: 

127 print(f'INFO: Changing file permissions of \"{name}\" to \"{oct(filePerms)}\" (it is \"{oct(stat.S_IMODE(info["st_mode"]))}\").') 

128 os.chmod( 

129 name, 

130 filePerms, 

131 follow_symlinks = follow_symlinks, 

132 ) 

133 

134 # Set folder permissions if it is wrong ... 

135 if stat.S_ISDIR(info["st_mode"]): 

136 if folderPerms is not None: 

137 if stat.S_IMODE(info["st_mode"]) != folderPerms: 

138 if debug: 

139 print(f'INFO: Changing folder permissions of \"{name}\" to \"{oct(folderPerms)}\" (it is \"{oct(stat.S_IMODE(info["st_mode"]))}\").') 

140 os.chmod( 

141 name, 

142 folderPerms, 

143 follow_symlinks = follow_symlinks, 

144 )