1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import stat
18 import os
19 import pwd
20 import grp
21
22 from libxyz.vfs import types as vfstypes
23
24 _types = (
25 (stat.S_ISDIR, vfstypes.VFSTypeDir),
26 (stat.S_ISCHR, vfstypes.VFSTypeChar),
27 (stat.S_ISBLK, vfstypes.VFSTypeBlock),
28 (stat.S_ISREG, vfstypes.VFSTypeFile),
29 (stat.S_ISFIFO, vfstypes.VFSTypeFifo),
30 (stat.S_ISLNK, vfstypes.VFSTypeLink),
31 (stat.S_ISSOCK, vfstypes.VFSTypeSocket),
32 )
33
35 """
36 Find out file type
37 @param st_mode: Raw st_mode obtained from os.stat()
38 """
39
40 global _types
41
42 for _test, _type in _types:
43 if _test(st_mode):
44 return _type()
45
46 return vfstypes.VFSTypeUnknown()
47
48
49
68
69
70
72 return [x for x in path.split(os.sep) if x]
73
74
75
77 """
78 Get user name by UID
79
80 @param uid: User ID
81 @return: username or None
82 """
83
84 if uid is None:
85 uid = os.getuid()
86
87 try:
88 name = pwd.getpwuid(uid).pw_name
89 except (KeyError, TypeError):
90 name = None
91
92 return name
93
94
95
97 """
98 Get group name by GID
99
100 @param gid: Group ID
101 @return: group name or None
102 """
103
104 if gid is None:
105 gid = os.getuid()
106
107 try:
108 name = grp.getgrgid(gid).gr_name
109 except (KeyError, TypeError):
110 name = None
111
112 return name
113