gitcfg: Use st_mtime as the cache key
[git-cola.git] / darwin / __boot__.py
blob134e172504e283696ddae12ede13cf81f387ae49
1 def _get_argvemulator():
2 """argvemulator - create sys.argv from OSA events. Used by applets that
3 want unix-style arguments.
4 """
6 import sys
7 import traceback
8 from Carbon import AE
9 from Carbon.AppleEvents import kCoreEventClass, kAEOpenApplication, \
10 kAEOpenDocuments, keyDirectObject, typeAEList, typeAlias
11 from Carbon import Evt
12 from Carbon import File
13 from Carbon.Events import highLevelEventMask, kHighLevelEvent
15 class ArgvCollector:
17 """A minimal FrameWork.Application-like class"""
19 def __init__(self):
20 self.quitting = 0
22 AE.AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
23 self.__runapp)
24 AE.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
25 self.__openfiles)
27 def close(self):
28 AE.AERemoveEventHandler(kCoreEventClass, kAEOpenApplication)
29 AE.AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments)
31 def mainloop(self, mask = highLevelEventMask, timeout = 1*60):
32 # Note: this is not the right way to run an event loop in OSX or
33 # even "recent" versions of MacOS9. This is however code that has
34 # proven itself.
36 # Remove the funny -psn_xxx_xxx argument
37 if len(sys.argv) > 1 and sys.argv[1][:4] == '-psn':
38 del sys.argv[1]
40 stoptime = Evt.TickCount() + timeout
41 while not self.quitting and Evt.TickCount() < stoptime:
42 self._dooneevent(mask, timeout)
44 if not self.quitting:
45 print "argvemulator: timeout waiting for arguments"
47 self.close()
49 def _dooneevent(self, mask = highLevelEventMask, timeout = 1*60):
50 got, event = Evt.WaitNextEvent(mask, timeout)
51 if got:
52 self._lowlevelhandler(event)
54 def _lowlevelhandler(self, event):
55 what, message, when, where, modifiers = event
56 h, v = where
57 if what == kHighLevelEvent:
58 try:
59 AE.AEProcessAppleEvent(event)
60 except AE.Error, err:
61 msg = "High Level Event: %r %r" % (hex(message),
62 hex(h | (v<<16)))
63 print 'AE error: ', err
64 print 'in', msg
65 traceback.print_exc()
66 return
67 else:
68 print "Unhandled event:", event
71 def _quit(self):
72 self.quitting = 1
74 def __runapp(self, requestevent, replyevent):
75 self._quit()
77 def __openfiles(self, requestevent, replyevent):
78 try:
79 listdesc = requestevent.AEGetParamDesc(keyDirectObject,
80 typeAEList)
81 for i in range(listdesc.AECountItems()):
82 aliasdesc = listdesc.AEGetNthDesc(i+1, typeAlias)[1]
83 alias = File.Alias(rawdata=aliasdesc.data)
84 fsref = alias.FSResolveAlias(None)[0]
85 pathname = fsref.as_pathname()
86 sys.argv.append(pathname)
87 except Exception, e:
88 print "argvemulator.py warning: can't unpack an open document event"
89 import traceback
90 traceback.print_exc()
92 self._quit()
94 return ArgvCollector()
96 def _argv_emulation():
97 import sys
98 # only use if started by LaunchServices
99 for arg in sys.argv[1:]:
100 if arg.startswith('-psn'):
101 _get_argvemulator().mainloop()
102 break
103 _argv_emulation()
107 def _argv_inject(argv):
108 import sys
109 # only use if started by LaunchServices
110 if len(sys.argv) > 1 and sys.argv[1].startswith('-psn'):
111 sys.argv[1:2] = argv
112 elif len(sys.argv) == 2:
113 sys.argv[1:1] = argv
116 _argv_inject(['--repo'])
119 def _disable_linecache():
120 import linecache
121 def fake_getline(*args, **kwargs):
122 return ''
123 linecache.orig_getline = linecache.getline
124 linecache.getline = fake_getline
125 _disable_linecache()
128 def _run(*scripts):
129 global __file__
130 import os, sys, site
131 sys.frozen = 'macosx_app'
132 base = os.environ['RESOURCEPATH']
133 site.addsitedir(base)
134 site.addsitedir(os.path.join(base, 'Python', 'site-packages'))
135 if not scripts:
136 import __main__
137 for script in scripts:
138 path = os.path.join(base, script)
139 sys.argv[0] = __file__ = path
140 execfile(path, globals(), globals())
143 _run('git-cola.py')