-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
44 lines (37 loc) · 1.3 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# import grp
import pwd
import time
import os
import stat
def fileProperty(filepath):
# return information from given file in this format:
# -{Unix permissions code} {User} {Group} {fileSize} {Date} {fileName}
st = os.stat(filepath)
fileMessage = []
def _getFileMode():
modes = [
stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR,
stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP,
stat.S_IROTH, stat.S_IWOTH, stat.SIXOTH,
]
mode = st.st_mode
fullmode = ''
fullmode += os.path.isdir(filepath) and 'd' or '-'
for i in range(9):
fullmode += bool(mode & modes[i]) and 'rwxrwxrwx'[i] or '-'
return fullmode
def _getFilesNumber():
return str(st.st_nlink)
def _getUser():
return pwd.getpwuid(st.st_uid).pw_name
# def _getGroup():
# return grp.getgrgid(st.st_gid).gr_name
def _getSize():
return str(st.st_size)
def _getLastTime():
return time.strftime("%b %d %H:%M", time.gmtime(st.st_mtime))
for func in ('_getFileMode()', '_getFilesNumber()', '_getUser()', '_getGroup()', '_getSize()', '_getLastTime()'):
fileMessage.append(eval(func))
fileMessage.append(os.path.basename(filepath))
return ' '.join(fileMessage)