Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

taskbar: merge with rpcserver.py, remove win32 package

Changeset 516c35f8da25

Parent 1f5a7c1a9606

by Steve Borho

Changes to 3 files · Browse files at 516c35f8da25 Showing diff from parent 1f5a7c1a9606 Diff from another changeset...

Change 1 of 4 Show Entire File taskbar.py Stacked
 
1
2
3
 
 
 
 
 
 
 
4
5
6
7
8
 
 
 
9
10
11
12
13
14
 
 
15
16
17
 
124
125
126
127
128
 
129
130
131
 
140
141
142
143
 
144
145
146
 
154
155
156
157
 
158
159
160
161
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
164
165
 
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
 
133
134
135
 
 
136
137
138
139
 
148
149
150
 
151
152
153
154
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
@@ -1,17 +1,26 @@
 # Creates a task-bar icon. Run from Python.exe to see the  # messages printed.   +import gc +import os +import sys +import time +import threading +import Queue +  from win32api import *  from win32gui import *  import win32ui  import win32pipe  import win32con +import win32event +import win32file +import winerror  import pywintypes -import sys, os    from mercurial import demandimport ; demandimport.enable() -from thgutil import thread2 -from win32 import rpcserver +from mercurial import ui +from thgutil import thread2, paths, shlib    APP_TITLE = "TortoiseHg RPC server"   @@ -124,8 +133,7 @@
  print "testing pipe [try %d] ..." % cnt   try:   self.pipethread.terminate() - win32pipe.CallNamedPipe(rpcserver.PIPENAME, '', - rpcserver.PIPEBUFSIZE, 0) + win32pipe.CallNamedPipe(PIPENAME, '', PIPEBUFSIZE, 0)   except:   pass   cnt += 1 @@ -140,7 +148,7 @@
  def launch():   import gtk   from hggtk import taskbarui, hgtk - dlg = taskbarui.TaskBarUI(rpcserver.logq) + dlg = taskbarui.TaskBarUI(logq)   dlg.show_all()   dlg.connect('destroy', gtk.main_quit)   self.dialog = dlg @@ -154,12 +162,200 @@
    def start_pipe_server(self):   def servepipe(): - self.svc = rpcserver.PipeServer() + self.svc = PipeServer()   self.svc.SvcDoRun()     self.pipethread = thread2.Thread(target=servepipe)   self.pipethread.start()   + +PIPENAME = r"\\.\pipe\TortoiseHgRpcServer-bc0c27107423-" +PIPENAME += GetUserName() + +PIPEBUFSIZE = 4096 + +logq = Queue.Queue(0) +def logmsg(msg): + if logq.qsize() < 100: + ts = '[%s] ' % time.strftime('%c') + logq.put(ts + msg) + +def getrepos(batch): + roots = set() + notifypaths = set() + for path in batch: + r = paths.find_root(path) + if r is None: + for n in os.listdir(path): + r = paths.find_root(os.path.join(path, n)) + if (r is not None): + roots.add(r) + notifypaths.add(r) + else: + roots.add(r); + notifypaths.add(path) + return roots, notifypaths + +def update_batch(batch): + '''updates thgstatus for all paths in batch''' + roots, notifypaths = getrepos(batch) + if roots: + _ui = ui.ui(); + for r in sorted(roots): + logmsg('Updating ' + r) + shlib.update_thgstatus(_ui, r, wait=False) + shlib.shell_notify([r]) + if notifypaths: + time.sleep(2) + shlib.shell_notify(list(notifypaths)) + logmsg('Shell notified') + +requests = Queue.Queue(0) + +def update(args): + batch = [] + r = args[0] + print "got update request %s (first in batch)" % r + batch.append(r) + print "wait a bit for additional requests..." + time.sleep(0.2) + deferred_requests = [] + try: + while True: + req = requests.get_nowait() + s = req.split('|') + cmd, args = s[0], s[1:] + if cmd == 'update': + print "got update request %s" % req + batch.append(args[0]) + else: + deferred_requests.append(req) + except Queue.Empty: + pass + for req in deferred_requests: + requests.put(req) + msg = "processing batch with %i update requests" + print msg % len(batch) + update_batch(batch) + +def remove(args): + path = args[0] + logmsg('Removing ' + path) + roots, notifypaths = getrepos([path]) + if roots: + for r in sorted(roots): + try: + os.remove(os.path.join(r, '.hg', 'thgstatus')) + except OSError: + pass + if notifypaths: + shlib.shell_notify(list(notifypaths)) + +def dispatch(req, cmd, args): + print "dispatch(%s)" % req + if cmd == 'update': + update(args) + elif cmd == 'remove': + remove(args) + else: + logmsg("Error: unknown request '%s'" % req) + +class Updater(threading.Thread): + def run(self): + while True: + req = requests.get() + s = req.split('|') + cmd, args = s[0], s[1:] + if cmd == 'terminate': + logmsg('Updater thread terminating') + return + dispatch(req, cmd, args) + gc.collect() + +Updater().start() + +class PipeServer: + def __init__(self): + # Create an event which we will use to wait on. + # The "service stop" request will set this event. + self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) + + # We need to use overlapped IO for this, so we dont block when + # waiting for a client to connect. This is the only effective way + # to handle either a client connection, or a service stop request. + self.overlapped = pywintypes.OVERLAPPED() + + # And create an event to be used in the OVERLAPPED object. + self.overlapped.hEvent = win32event.CreateEvent(None,0,0,None) + + def SvcStop(self): + print 'PipeServer thread terminating' + win32event.SetEvent(self.hWaitStop) + requests.put('terminate') + + def SvcDoRun(self): + # We create our named pipe. + pipeName = PIPENAME + openMode = win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED + pipeMode = win32pipe.PIPE_TYPE_MESSAGE + + # When running as a service, we must use special security for the pipe + sa = pywintypes.SECURITY_ATTRIBUTES() + # Say we do have a DACL, and it is empty + # (ie, allow full access!) + sa.SetSecurityDescriptorDacl ( 1, None, 0 ) + + pipeHandle = win32pipe.CreateNamedPipe(pipeName, + openMode, + pipeMode, + win32pipe.PIPE_UNLIMITED_INSTANCES, + 0, 0, 6000, # default buffers, and 6 second timeout. + sa) + + # Loop accepting and processing connections + while True: + try: + hr = win32pipe.ConnectNamedPipe(pipeHandle, self.overlapped) + except pywintypes.error, inst: + print "Error connecting pipe: ", inst + pipeHandle.Close() + break + + if hr==winerror.ERROR_PIPE_CONNECTED: + # Client is fast, and already connected - signal event + win32event.SetEvent(self.overlapped.hEvent) + # Wait for either a connection, or a service stop request. + timeout = win32event.INFINITE + waitHandles = self.hWaitStop, self.overlapped.hEvent + rc = win32event.WaitForMultipleObjects(waitHandles, 0, timeout) + if rc==win32event.WAIT_OBJECT_0: + # Stop event + return + else: + # read pipe and process request + try: + hr, data = win32file.ReadFile(pipeHandle, PIPEBUFSIZE) + if not data: + raise SystemExit # signal by dispatch terminate + win32pipe.DisconnectNamedPipe(pipeHandle) + except win32file.error: + # Client disconnected without sending data + # or before reading the response. + # Thats OK - just get the next connection + continue + + try: + requests.put(data) + except SystemExit: + raise SystemExit # interrupted by thread2.terminate() + except: + import traceback + print "WARNING: something went wrong in requests.put" + print traceback.format_exc() + status = "ERROR" + # Clean up when we exit + self.SvcStop() +  def main():   w=MainWindow()   PumpMessages()
Change 1 of 1 Show Entire File win32/​__init__.py Stacked
 
1
 
 
@@ -1,1 +0,0 @@
-#placeholder
Change 1 of 1 Show Entire File win32/​rpcserver.py Stacked
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -1,210 +0,0 @@
-import os -import gc -import win32api -import win32con - -from win32com.shell import shell, shellcon -import _winreg - -from mercurial import ui - -from thgutil import paths, shlib - -import sys -import time -import Queue -import threading - -import win32event -import win32pipe -import win32file -import pywintypes -import winerror - - -PIPENAME = r"\\.\pipe\TortoiseHgRpcServer-bc0c27107423-" -PIPENAME += win32api.GetUserName() - -PIPEBUFSIZE = 4096 - -logq = Queue.Queue(0) -def logmsg(msg): - if logq.qsize() < 100: - ts = '[%s] ' % time.strftime('%c') - logq.put(ts + msg) - -def getrepos(batch): - roots = set() - notifypaths = set() - for path in batch: - r = paths.find_root(path) - if r is None: - for n in os.listdir(path): - r = paths.find_root(os.path.join(path, n)) - if (r is not None): - roots.add(r) - notifypaths.add(r) - else: - roots.add(r); - notifypaths.add(path) - return roots, notifypaths - -def update_batch(batch): - '''updates thgstatus for all paths in batch''' - roots, notifypaths = getrepos(batch) - if roots: - _ui = ui.ui(); - for r in sorted(roots): - logmsg('Updating ' + r) - shlib.update_thgstatus(_ui, r, wait=False) - shlib.shell_notify([r]) - if notifypaths: - time.sleep(2) - shlib.shell_notify(list(notifypaths)) - logmsg('Shell notified') - -requests = Queue.Queue(0) - -def update(args): - batch = [] - r = args[0] - print "got update request %s (first in batch)" % r - batch.append(r) - print "wait a bit for additional requests..." - time.sleep(0.2) - deferred_requests = [] - try: - while True: - req = requests.get_nowait() - s = req.split('|') - cmd, args = s[0], s[1:] - if cmd == 'update': - print "got update request %s" % req - batch.append(args[0]) - else: - deferred_requests.append(req) - except Queue.Empty: - pass - for req in deferred_requests: - requests.put(req) - msg = "processing batch with %i update requests" - print msg % len(batch) - update_batch(batch) - -def remove(args): - path = args[0] - logmsg('Removing ' + path) - roots, notifypaths = getrepos([path]) - if roots: - for r in sorted(roots): - try: - os.remove(os.path.join(r, '.hg', 'thgstatus')) - except OSError: - pass - if notifypaths: - shlib.shell_notify(list(notifypaths)) - -def dispatch(req, cmd, args): - print "dispatch(%s)" % req - if cmd == 'update': - update(args) - elif cmd == 'remove': - remove(args) - else: - logmsg("Error: unknown request '%s'" % req) - -class Updater(threading.Thread): - def run(self): - while True: - req = requests.get() - s = req.split('|') - cmd, args = s[0], s[1:] - if cmd == 'terminate': - logmsg('Updater thread terminating') - return - dispatch(req, cmd, args) - gc.collect() - -Updater().start() - -class PipeServer: - def __init__(self): - # Create an event which we will use to wait on. - # The "service stop" request will set this event. - self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) - - # We need to use overlapped IO for this, so we dont block when - # waiting for a client to connect. This is the only effective way - # to handle either a client connection, or a service stop request. - self.overlapped = pywintypes.OVERLAPPED() - - # And create an event to be used in the OVERLAPPED object. - self.overlapped.hEvent = win32event.CreateEvent(None,0,0,None) - - def SvcStop(self): - print 'PipeServer thread terminating' - win32event.SetEvent(self.hWaitStop) - requests.put('terminate') - - def SvcDoRun(self): - # We create our named pipe. - pipeName = PIPENAME - openMode = win32pipe.PIPE_ACCESS_DUPLEX | win32file.FILE_FLAG_OVERLAPPED - pipeMode = win32pipe.PIPE_TYPE_MESSAGE - - # When running as a service, we must use special security for the pipe - sa = pywintypes.SECURITY_ATTRIBUTES() - # Say we do have a DACL, and it is empty - # (ie, allow full access!) - sa.SetSecurityDescriptorDacl ( 1, None, 0 ) - - pipeHandle = win32pipe.CreateNamedPipe(pipeName, - openMode, - pipeMode, - win32pipe.PIPE_UNLIMITED_INSTANCES, - 0, 0, 6000, # default buffers, and 6 second timeout. - sa) - - # Loop accepting and processing connections - while True: - try: - hr = win32pipe.ConnectNamedPipe(pipeHandle, self.overlapped) - except pywintypes.error, inst: - print "Error connecting pipe: ", inst - pipeHandle.Close() - break - - if hr==winerror.ERROR_PIPE_CONNECTED: - # Client is fast, and already connected - signal event - win32event.SetEvent(self.overlapped.hEvent) - # Wait for either a connection, or a service stop request. - timeout = win32event.INFINITE - waitHandles = self.hWaitStop, self.overlapped.hEvent - rc = win32event.WaitForMultipleObjects(waitHandles, 0, timeout) - if rc==win32event.WAIT_OBJECT_0: - # Stop event - return - else: - # read pipe and process request - try: - hr, data = win32file.ReadFile(pipeHandle, PIPEBUFSIZE) - if not data: - raise SystemExit # signal by dispatch terminate - win32pipe.DisconnectNamedPipe(pipeHandle) - except win32file.error: - # Client disconnected without sending data - # or before reading the response. - # Thats OK - just get the next connection - continue - - try: - requests.put(data) - except SystemExit: - raise SystemExit # interrupted by thread2.terminate() - except: - import traceback - print "WARNING: something went wrong in requests.put" - print traceback.format_exc() - status = "ERROR" - # Clean up when we exit - self.SvcStop()