-
Notifications
You must be signed in to change notification settings - Fork 14
/
latest.py
executable file
·113 lines (98 loc) · 3.32 KB
/
latest.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python2
'''
This utility shows what files were recently modified, regardless
of their folder depth. It starts by default on the current folder,
but you can also specify any folder you wish in the cmd line.
It will then display all files found under any depth, sorted by
their modification time (oldest to earliest). You can also sort
by accesstime (-a) or ctime (-c).
Think of it as a recursive "ls -ltra" - I use it to see what files
have recently changed, regardless of folder depth.
'''
import os
import sys
import time
import getopt
import itertools
import collections
from stat import S_ISDIR
def usage():
print '''\
Usage: {mainApp} <options> <folderToScan>
where folderToScan is . by default, and options can be:
-h, --help show this help message
-l, --symlinks show symlinks
-m, --mtime order by modification time (default)
-a, --atime order by access time
-c, --ctime order by ctime (creation time under Windows,
inode modification time under Unix)
'''.format(mainApp=os.path.basename(sys.argv[0]))
sys.exit(1)
def main():
try:
args = sys.argv[1:]
optlist, args = getopt.gnu_getopt(
args, "hlmac", [
'help', 'nosymlink', 'mtime', 'atime', 'ctime'])
except:
usage()
timemode = "st_mtime"
show_symlinks = False
for opt, unused_arg in optlist:
if opt in ("-h", "--help"):
usage()
elif opt in ("-l", "--symlinks"):
show_symlinks = True
elif opt in ("-m", "--mtime"):
timemode = "st_mtime"
elif opt in ("-a", "--atime"):
timemode = "st_atime"
elif opt in ("-c", "--ctime"):
timemode = "st_ctime"
else:
usage()
timemodeFunc = lambda x: getattr(x, timemode)
if args:
if len(args) > 1:
usage()
elif not os.path.isdir(args[0]):
print args[0], "is not a folder... Aborting..."
sys.exit(1)
else:
target = args[0]
else:
target = "."
target = os.path.abspath(target)
if sys.platform.startswith("linux"):
special_char = {
'st_mtime': 'T',
'st_ctime': 'C',
'st_atime': 'A'
}[timemode]
cmd = 'find "{0}" ! -type d '.format(target)
if not show_symlinks:
cmd += ' ! -type l '
os.system(
cmd + '-printf "%{0}+ %11s %p\\n" | sort -n'.format(
special_char))
else:
maxSize = 0
completeList = collections.defaultdict(list)
for p, dirlist, filelist in os.walk(target):
for f in itertools.chain(filelist, dirlist):
fullpath = p + os.sep + f
try:
statdata = os.lstat(fullpath)
timestamp = timemodeFunc(statdata)
if not S_ISDIR(statdata.st_mode):
si = statdata.st_size
completeList[timestamp].append((fullpath, si))
maxSize = max(maxSize, si)
except:
pass
span = len(str(maxSize))
for k, l in sorted(completeList.items()):
for v in l:
print "%s %*d %s" % (time.ctime(k), span, v[1], v[0])
if __name__ == "__main__":
main()