blob: 5f299bcc480ab8d647f79ceb8e615e05ed65f054 [file] [log] [blame]
Jack Jansenffb8fef2003-02-12 15:39:56 +00001# Prelude to allow running this as a main program
2def _init():
3 import macresource
4 import sys, os
5 macresource.need('DITL', 468, "PythonIDE.rsrc")
6 widgetrespathsegs = [sys.exec_prefix, "Mac", "Tools", "IDE", "Widgets.rsrc"]
7 widgetresfile = os.path.join(*widgetrespathsegs)
8 if not os.path.exists(widgetresfile):
9 widgetrespathsegs = [os.pardir, "Tools", "IDE", "Widgets.rsrc"]
10 widgetresfile = os.path.join(*widgetrespathsegs)
11 refno = macresource.need('CURS', 468, widgetresfile)
12 if os.environ.has_key('PYTHONIDEPATH'):
13 # For development set this environment variable
14 ide_path = os.environ['PYTHONIDEPATH']
15 elif refno:
16 # We're not a fullblown application
17 idepathsegs = [sys.exec_prefix, "Mac", "Tools", "IDE"]
18 ide_path = os.path.join(*idepathsegs)
19 if not os.path.exists(ide_path):
20 idepathsegs = [os.pardir, "Tools", "IDE"]
21 for p in sys.path:
22 ide_path = os.path.join(*([p]+idepathsegs))
23 if os.path.exists(ide_path):
24 break
25
26 else:
27 # We are a fully frozen application
28 ide_path = sys.argv[0]
29 if ide_path not in sys.path:
30 sys.path.insert(0, ide_path)
31
32if __name__ == '__main__':
33 _init()
34
Jack Jansen73019a62003-02-11 23:15:33 +000035import W
36import Wapplication
37from Carbon import Evt
38import EasyDialogs
39import FrameWork
40
41import sys
42import string
43import os
Jack Jansen4ab84372003-02-14 14:13:25 +000044import urllib
Jack Jansen73019a62003-02-11 23:15:33 +000045
46import pimp
47
48ELIPSES = '...'
49
Jack Jansen113af982003-02-12 12:47:56 +000050class PackageManagerMain(Wapplication.Application):
Jack Jansen73019a62003-02-11 23:15:33 +000051
52 def __init__(self):
Jack Jansen113af982003-02-12 12:47:56 +000053 self.preffilepath = os.path.join("Python", "Package Install Manager Prefs")
Jack Jansen73019a62003-02-11 23:15:33 +000054 Wapplication.Application.__init__(self, 'Pimp')
55 from Carbon import AE
56 from Carbon import AppleEvents
57
58 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEOpenApplication,
59 self.ignoreevent)
60 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEReopenApplication,
61 self.ignoreevent)
62 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEPrintDocuments,
63 self.ignoreevent)
64 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEQuitApplication,
65 self.quitevent)
Jack Jansen113af982003-02-12 12:47:56 +000066 if 1:
Jack Jansen73019a62003-02-11 23:15:33 +000067 import PyConsole
68 # With -D option (OSX command line only) keep stderr, for debugging the IDE
69 # itself.
70 debug_stderr = None
71 if len(sys.argv) >= 2 and sys.argv[1] == '-D':
72 debug_stderr = sys.stderr
73 del sys.argv[1]
74 PyConsole.installoutput()
Jack Jansen73019a62003-02-11 23:15:33 +000075 if debug_stderr:
76 sys.stderr = debug_stderr
77 self.opendoc(None)
78 self.mainloop()
79
80 def makeusermenus(self):
81 m = Wapplication.Menu(self.menubar, "File")
Jack Jansen4ab84372003-02-14 14:13:25 +000082 newitem = FrameWork.MenuItem(m, "Open Standard Database", "N", 'openstandard')
83 openitem = FrameWork.MenuItem(m, "Open"+ELIPSES, "O", 'open')
84 openURLitem = FrameWork.MenuItem(m, "Open URL"+ELIPSES, "D", 'openURL')
Jack Jansen73019a62003-02-11 23:15:33 +000085 FrameWork.Separator(m)
86 closeitem = FrameWork.MenuItem(m, "Close", "W", 'close')
87## saveitem = FrameWork.MenuItem(m, "Save", "S", 'save')
Jack Jansen113af982003-02-12 12:47:56 +000088## saveasitem = FrameWork.MenuItem(m, "Save as"+ELIPSES, None, 'save_as')
Jack Jansen4ab84372003-02-14 14:13:25 +000089## FrameWork.Separator(m)
Jack Jansen73019a62003-02-11 23:15:33 +000090
91 m = Wapplication.Menu(self.menubar, "Edit")
92 undoitem = FrameWork.MenuItem(m, "Undo", 'Z', "undo")
93 FrameWork.Separator(m)
94 cutitem = FrameWork.MenuItem(m, "Cut", 'X', "cut")
95 copyitem = FrameWork.MenuItem(m, "Copy", "C", "copy")
96 pasteitem = FrameWork.MenuItem(m, "Paste", "V", "paste")
97 FrameWork.MenuItem(m, "Clear", None, "clear")
98 FrameWork.Separator(m)
99 selallitem = FrameWork.MenuItem(m, "Select all", "A", "selectall")
100
101 m = Wapplication.Menu(self.menubar, "Package")
102 runitem = FrameWork.MenuItem(m, "Install", "I", 'install')
103 homepageitem = FrameWork.MenuItem(m, "Visit Homepage", None, 'homepage')
104
105 self.openwindowsmenu = Wapplication.Menu(self.menubar, 'Windows')
106 self.makeopenwindowsmenu()
Jack Jansenffb8fef2003-02-12 15:39:56 +0000107 self._menustocheck = [closeitem,
Jack Jansen73019a62003-02-11 23:15:33 +0000108 undoitem, cutitem, copyitem, pasteitem,
109 selallitem,
110 runitem, homepageitem]
111
112 def quitevent(self, theAppleEvent, theReply):
Jack Jansen73019a62003-02-11 23:15:33 +0000113 self._quit()
114
115 def ignoreevent(self, theAppleEvent, theReply):
116 pass
117
118 def opendocsevent(self, theAppleEvent, theReply):
119 W.SetCursor('watch')
120 import aetools
121 parameters, args = aetools.unpackevent(theAppleEvent)
122 docs = parameters['----']
123 if type(docs) <> type([]):
124 docs = [docs]
125 for doc in docs:
126 fsr, a = doc.FSResolveAlias(None)
127 path = fsr.as_pathname()
128 path = urllib.pathname2url(path)
129 self.opendoc(path)
130
131 def opendoc(self, url):
132 PackageBrowser(url)
133
134 def getabouttext(self):
Jack Jansen113af982003-02-12 12:47:56 +0000135 return "About Package Manager"+ELIPSES
Jack Jansen73019a62003-02-11 23:15:33 +0000136
137 def do_about(self, id, item, window, event):
Jack Jansen113af982003-02-12 12:47:56 +0000138 EasyDialogs.Message("Package Install Manager for Python")
Jack Jansen4ab84372003-02-14 14:13:25 +0000139
140 def domenu_openstandard(self, *args):
141 self.opendoc(None)
142
Jack Jansen73019a62003-02-11 23:15:33 +0000143 def domenu_open(self, *args):
144 filename = EasyDialogs.AskFileForOpen(typeList=("TEXT",))
145 if filename:
146 filename = urllib.pathname2url(filename)
147 self.opendoc(filename)
Jack Jansen4ab84372003-02-14 14:13:25 +0000148
149 def domenu_openURL(self, *args):
150 ok = EasyDialogs.AskYesNoCancel(
151 "Warning: by opening a non-standard database "
152 "you are trusting the maintainer of it "
153 "to run arbitrary code on your machine.",
154 yes="OK", no="")
155 if ok <= 0: return
156 url = EasyDialogs.AskString("URL of database to open:", ok="Open")
157 if url:
158 self.opendoc(url)
Jack Jansen73019a62003-02-11 23:15:33 +0000159
160 def domenu_openbyname(self, *args):
161 url = EasyDialogs.AskString("Open URL:", ok="Open")
162 if url:
163 self.opendoc(url)
164
165 def makeopenwindowsmenu(self):
166 for i in range(len(self.openwindowsmenu.items)):
167 self.openwindowsmenu.menu.DeleteMenuItem(1)
168 self.openwindowsmenu.items = []
169 windows = []
170 self._openwindows = {}
171 for window in self._windows.keys():
172 title = window.GetWTitle()
173 if not title:
174 title = "<no title>"
175 windows.append((title, window))
176 windows.sort()
177 for title, window in windows:
178 shortcut = None
179 item = FrameWork.MenuItem(self.openwindowsmenu, title, shortcut, callback = self.domenu_openwindows)
180 self._openwindows[item.item] = window
181 self._openwindowscheckmark = 0
182 self.checkopenwindowsmenu()
183
184 def domenu_openwindows(self, id, item, window, event):
185 w = self._openwindows[item]
186 w.ShowWindow()
187 w.SelectWindow()
188
189 def domenu_quit(self):
190 self._quit()
191
192 def domenu_save(self, *args):
193 print "Save"
194
195 def _quit(self):
196## import PyConsole, PyEdit
197 for window in self._windows.values():
198 try:
199 rv = window.close() # ignore any errors while quitting
200 except:
201 rv = 0 # (otherwise, we can get stuck!)
202 if rv and rv > 0:
203 return
204## try:
205## PyConsole.console.writeprefs()
206## PyConsole.output.writeprefs()
207## PyEdit.searchengine.writeprefs()
208## except:
209## # Write to __stderr__ so the msg end up in Console.app and has
210## # at least _some_ chance of getting read...
211## # But: this is a workaround for way more serious problems with
212## # the Python 2.2 Jaguar addon.
213## sys.__stderr__.write("*** PythonIDE: Can't write preferences ***\n")
214 self.quitting = 1
215
216class PimpInterface:
217
218 def setuppimp(self, url):
219 self.pimpprefs = pimp.PimpPreferences()
220 self.pimpdb = pimp.PimpDatabase(self.pimpprefs)
Jack Jansen113af982003-02-12 12:47:56 +0000221 self.pimpinstaller = pimp.PimpInstaller(self.pimpdb)
Jack Jansen73019a62003-02-11 23:15:33 +0000222 if not url:
223 url = self.pimpprefs.pimpDatabase
Jack Jansen4ab84372003-02-14 14:13:25 +0000224 try:
225 self.pimpdb.appendURL(url)
226 except IOError, arg:
227 return "Cannot open %s: %s" % (url, arg)
Jack Jansend5532af2003-02-28 15:19:51 +0000228 return self.pimpprefs.check()
Jack Jansen4ab84372003-02-14 14:13:25 +0000229
230 def closepimp(self):
231 self.pimpdb.close()
232 self.pimpprefs = None
233 self.pimpdb = None
234 self.pimpinstaller = None
235 self.packages = []
Jack Jansen73019a62003-02-11 23:15:33 +0000236
237 def getbrowserdata(self):
238 self.packages = self.pimpdb.list()
239 rv = []
240 for pkg in self.packages:
241 name = pkg.fullname()
242 status, _ = pkg.installed()
243 description = pkg.description()
244 rv.append((status, name, description))
245 return rv
246
247 def getstatus(self, number):
248 pkg = self.packages[number]
249 return pkg.installed()
Jack Jansen113af982003-02-12 12:47:56 +0000250
251 def installpackage(self, sel, output, recursive, force):
252 pkg = self.packages[sel]
253 list, messages = self.pimpinstaller.prepareInstall(pkg, force, recursive)
254 if messages:
255 return messages
256 messages = self.pimpinstaller.install(list, output)
257 return messages
Jack Jansen73019a62003-02-11 23:15:33 +0000258
259class PackageBrowser(PimpInterface):
260
261 def __init__(self, url = None):
262 self.ic = None
Jack Jansen4ab84372003-02-14 14:13:25 +0000263 msg = self.setuppimp(url)
264 if msg:
265 EasyDialogs.Message(msg)
Jack Jansen73019a62003-02-11 23:15:33 +0000266 self.setupwidgets()
267 self.updatestatus()
Jack Jansen4ab84372003-02-14 14:13:25 +0000268
269 def close(self):
270 self.closepimp()
Jack Jansen73019a62003-02-11 23:15:33 +0000271
272 def setupwidgets(self):
Jack Jansenf3ef0382003-03-16 21:04:50 +0000273 self.w = W.Window((580, 400), "Python Install Manager", minsize = (400, 200), tabbable = 0)
Jack Jansen73019a62003-02-11 23:15:33 +0000274## self.w.divline = W.HorizontalLine((0, 20, 0, 0))
275 self.w.titlebar = W.TextBox((4, 4, 40, 12), 'Packages:')
276 data = self.getbrowserdata()
277 self.w.packagebrowser = W.MultiList((4, 20, 0, -70), data, self.listhit, cols=3)
278 self.w.installed_l = W.TextBox((4, -66, 60, 12), 'Installed:')
279 self.w.installed = W.TextBox((64, -66, 0, 12), '')
280 self.w.message_l = W.TextBox((4, -48, 60, 12), 'Status:')
281 self.w.message = W.TextBox((64, -48, 0, 12), '')
282 self.w.homepage_button = W.Button((4, -28, 96, 18), 'View homepage', self.do_homepage)
Jack Jansenf3ef0382003-03-16 21:04:50 +0000283 self.w.verbose_button = W.CheckBox((-358, -26, 60, 18), 'Verbose')
284 self.w.recursive_button = W.CheckBox((-284, -26, 140, 18), 'Install dependencies', self.updatestatus)
Jack Jansen113af982003-02-12 12:47:56 +0000285 self.w.recursive_button.set(1)
Jack Jansenf3ef0382003-03-16 21:04:50 +0000286 self.w.force_button = W.CheckBox((-160, -26, 80, 18), 'Overwrite', self.updatestatus)
Jack Jansen73019a62003-02-11 23:15:33 +0000287 self.w.install_button = W.Button((-76, -28, 56, 18), 'Install', self.do_install)
288 self.w.open()
289
290 def updatestatus(self):
291 sel = self.w.packagebrowser.getselection()
Jack Jansen113af982003-02-12 12:47:56 +0000292 data = self.getbrowserdata()
293 self.w.packagebrowser.setitems(data)
Jack Jansen73019a62003-02-11 23:15:33 +0000294 if len(sel) != 1:
295 self.w.installed.set('')
296 self.w.message.set('')
297 self.w.install_button.enable(0)
298 self.w.homepage_button.enable(0)
299 self.w.verbose_button.enable(0)
Jack Jansen113af982003-02-12 12:47:56 +0000300 self.w.recursive_button.enable(0)
Jack Jansen73019a62003-02-11 23:15:33 +0000301 self.w.force_button.enable(0)
302 else:
303 sel = sel[0]
Jack Jansen113af982003-02-12 12:47:56 +0000304 self.w.packagebrowser.setselection([sel])
Jack Jansen73019a62003-02-11 23:15:33 +0000305 installed, message = self.getstatus(sel)
306 self.w.installed.set(installed)
307 self.w.message.set(message)
308 self.w.install_button.enable(installed != "yes" or self.w.force_button.get())
309 self.w.homepage_button.enable(not not self.packages[sel].homepage())
310 self.w.verbose_button.enable(1)
Jack Jansen113af982003-02-12 12:47:56 +0000311 self.w.recursive_button.enable(1)
Jack Jansen73019a62003-02-11 23:15:33 +0000312 self.w.force_button.enable(1)
313
314 def listhit(self, *args, **kwargs):
315 self.updatestatus()
316
317 def do_install(self):
Jack Jansen113af982003-02-12 12:47:56 +0000318 sel = self.w.packagebrowser.getselection()[0]
319 if self.w.verbose_button.get():
320 output = sys.stdout
321 else:
322 output = None
323 recursive = self.w.recursive_button.get()
324 force = self.w.force_button.get()
325 messages = self.installpackage(sel, output, recursive, force)
326 self.updatestatus()
327 if messages:
328 EasyDialogs.Message('\n'.join(messages))
Jack Jansen73019a62003-02-11 23:15:33 +0000329
330 def do_homepage(self):
331 sel = self.w.packagebrowser.getselection()[0]
332 if not self.ic:
333 import ic
334
335 self.ic = ic.IC()
336 self.ic.launchurl(self.packages[sel].homepage())
337
338if __name__ == '__main__':
Jack Jansen113af982003-02-12 12:47:56 +0000339 PackageManagerMain()