File: test2.py

package info (click to toggle)
python-fam 1.1.1-2.2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 112 kB
  • ctags: 119
  • sloc: ansic: 707; python: 270; makefile: 43
file content (188 lines) | stat: -rwxr-xr-x 5,644 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python2.2
# This file is part of Python FAM.
# Copyright (C) 2002 Robert Sander <robert.sander@epigenomics.com>
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
# $Id: test2.py,v 1.5 2005/04/10 07:48:45 loewis Exp $
#

import os, sys

if os.name != 'posix':
    print 'I am sorry, but this script will run only on POSIX platforms.'
    sys.exit(1)

import _fam, time, signal, getopt, errno, select

# hash of filename => TestRequest
requests = {}

showreqid = None
suspend = None
cont = None
intr = None
usr1 = None
usr2 = None

class TestRequest:
    def __init__(self, userData, isDir):
        self.fr = None
        self.userData = userData
        self.isDir = isDir

def handle_stop(signum, frame):
    global suspend
    
    print 'Suspended!'
    suspend = 1

def handle_cont(signum, frame):
    global cont
    
    print 'Resumed!'
    signal.signal(signal.SIGCONT, handle_cont)
    cont = 1

def handle_int(signum, frame):
    global intr
    
    print 'Interupted!'
    signal.signal(signal.SIGINT, handle_int)
    intr = 1

def handle_usr1(signum, frame):
    global usr1
    
    print 'Got USR1!'
    signal.signal(signal.SIGUSR1, handle_usr1)
    usr1 = 1

def handle_usr2(signum, frame):
    global usr2
    
    print 'Got USR2!'
    signal.signal(signal.SIGUSR2, handle_usr2)
    usr2 = 1

def sendRequest(request, path):
    if request.isDir:
        request.fr = fc.monitorDirectory(path, request.userData)
    else:
        request.fr = fc.monitorFile(path, request.userData)
    if showreqid:
        print 'req %d' % request.fr.requestID(),
    if request.isDir:
        print 'FAMMonitorDirectory("%s")' % path
    else:
        print 'FAMMonitorFile("%s")' % path

def sendRequests():
    for path in requests.keys():
        sendRequest(requests[path], path)

def processDirEvents(fe):
    if fe.userData:
        print fe.userData,
    if showreqid:
        print 'req %d' % fe.requestID,
    print fe.filename, fe.code2str()
    newfile = os.path.join(fe.userData, fe.filename)
    if fe.code == _fam.Exists and fe.filename != fe.userData and os.path.isdir(newfile):
        requests[newfile] = TestRequest(newfile, 1)
        sendRequest(requests[newfile], newfile)
    if fe.code == _fam.Created and os.path.isdir(newfile):
        requests[newfile] = TestRequest(newfile, 1)
        sendRequest(requests[newfile], newfile)
    if fe.code == _fam.Deleted and os.path.isdir(newfile):
        del requests[newfile].fr
        del requests[newfile]
        
if len(sys.argv) < 2:
    print 'usage: %s [-r] [-f <filename>] [-d <dirname>]' % sys.argv[0]
    sys.exit(1)

fc = _fam.open()

optlist, args = getopt.getopt(sys.argv[1:], 'f:d:r')

for arg, value in optlist:
    if arg == '-r':
        showreqid = 1

for arg, filename in optlist:
    if arg == '-f':
        requests[filename] = TestRequest('%s' % filename, None)
    if arg == '-d':
        requests[filename] = TestRequest('%s' % filename, 1)
        
signal.signal(signal.SIGTSTP, handle_stop)
signal.signal(signal.SIGCONT, handle_cont)
signal.signal(signal.SIGINT, handle_int)
signal.signal(signal.SIGUSR1, handle_usr1)
signal.signal(signal.SIGUSR2, handle_usr2)

sendRequests()

while 1:
    if suspend:
        for request in requests.values():
            request.fr.suspendMonitor()
            print 'Suspended monitoring of request %i' % request.fr.requestID()
        suspend = None
        signal.signal(signal.SIGTSTP, handle_stop)
    if cont:
        for request in requests.values():
            request.fr.resumeMonitor()
            print 'Resumed monitoring of request %i' % request.fr.requestID()
        cont = None
    if intr:
        # The dealloc methods of all objects should handle
        # FAMCancelMonitor and FAMClose
        sys.exit(0)
    if usr1:
        # Cancel all requests, close the connection, and reopen it.
        # This makes sure long-lived clients can connect, monitor, and
        # disconnect repeatedly without leaking.
        usr1 = None
        sleeptime = 1
        for request in requests.values():
            print 'Cancelling monitoring of request %i' % request.fr.requestID()
            request.fr.cancelMonitor()
        fc.close()
        print 'Closed connection, sleeping %d...' % sleeptime
        time.sleep(sleeptime)
        fc = _fam.open()
        sendRequests()
    if usr2:
        # Clean things up like a well-behaved client and exit.
        for request in requests.values():
            print 'Cancelling monitoring of request %i' % request.fr.requestID()
            request.fr.cancelMonitor()
        fc.close()
        print 'Closed connection'
        sys.exit(0)
    try:
        ri, ro, re = select.select([fc], [], [])
    except select.error, er:
        errnumber, strerr = er
        if errnumber == errno.EINTR:
            continue
        else:
            print strerr
            sys.exit(1)
    while fc.pending():
        fe = fc.nextEvent()
        processDirEvents(fe)