]>
code.delx.au - monosys/blob - osx_bin/maildirstat.py
3 import os
, os
.path
, sys
8 def watchDirectories(directories
):
9 """ Given a list of directories to monitor, returns a function which when
10 called will sleep until a directory changes, and then return a list of
16 for event
in kqueue
.kevent(kq
, events
, 10, None):
17 changed
.append(directories
[dirfds
.index(event
.ident
)])
20 # Open all the directories
22 for directory
in directories
:
23 dirfds
.append(os
.open(directory
, os
.O_RDONLY
))
25 # Set up the kqueue events
27 filter = kqueue
.EV_ADD | kqueue
.EV_CLEAR | kqueue
.EV_ENABLE
28 fflags
= kqueue
.NOTE_WRITE | kqueue
.NOTE_DELETE | kqueue
.NOTE_EXTEND
30 e
=kqueue
.Event(fd
, kqueue
.EVFILT_VNODE
, filter, fflags
=fflags
, data
=fd
)
33 # Give back a function pointer
38 """ Clears the screen """
41 def checkMailbox(mailbox
):
42 """ Ensure that mailbox is a maildir directory """
43 if not (os
.path
.isdir(mailbox
) and
44 os
.path
.isdir(j(mailbox
, "cur")) and
45 os
.path
.isdir(j(mailbox
, "new")) and
46 os
.path
.isdir(j(mailbox
, "tmp"))):
47 print >> sys
.stderr
, "Error! Not a maildir mailbox."
50 def extractFlags(messages
):
51 """ Extract the flags from the messages """
52 return [m
.rsplit(":", 1)[1].split(",")[1] for m
in messages
]
54 def maildirStat(mailbox
):
55 """ Returns newCount, unreadCount for the given maildir mailbox """
58 # Reuse the directory listing
59 curList
= extractFlags(os
.listdir(j(mailbox
, "cur")))
61 # Get the new messages
62 newCount
= len(os
.listdir(j(mailbox
, "new")))
63 newCount
+= len([m
for m
in curList
if "N" in m
])
65 # Get the unread messages
66 unreadCount
= len([m
for m
in curList
if "S" not in m
])
68 return newCount
, unreadCount
70 def multiStat(mailboxes
, counts
):
71 for mailbox
in mailboxes
:
72 counts
[mailbox
] = maildirStat(mailbox
)
74 def printCounts(mailboxes
, counts
):
75 """ Prints the counts to the screen ordered """
77 for mailbox
in mailboxes
:
78 newCount
, unreadCount
= counts
[mailbox
]
79 mailbox
= mailbox
.rsplit("/", 1)[-1]
80 print "%s:\n\t%d\t%d\n" % (mailbox
, newCount
, unreadCount
)
83 """ Stat all the mailboxes and then watch for changes """
85 multiStat(mailboxes
, counts
)
86 printCounts(mailboxes
, counts
)
88 watcher
= watchDirectories([j(m
, "cur") for m
in mailboxes
] +
89 [j(m
, "new") for m
in mailboxes
])
92 changed
= [sep
.join(d
.split(os
.path
.sep
)[:-1]) for d
in changed
]
93 changed
= list(set(changed
))
94 multiStat(changed
, counts
)
95 printCounts(mailboxes
, counts
)
98 if __name__
== "__main__":
100 mailboxes
= sys
.argv
[1:]
102 print >> sys
.stderr
, "Usage: %s mailbox [mailbox ...]" % sys
.argv
[0]
107 except KeyboardInterrupt: