blob: 3fde556f903737f0296d215529ca5f7ae1a30edf [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 Jansena950d7b2003-04-16 12:17:56 +000050USER_INSTALL_DIR = os.path.join(os.environ.get('HOME', ''),
Jack Jansenf776dee2003-04-22 13:53:33 +000051 'Library',
52 'Python',
53 sys.version[:3],
54 'site-packages')
55
Jack Jansen113af982003-02-12 12:47:56 +000056class PackageManagerMain(Wapplication.Application):
Jack Jansen73019a62003-02-11 23:15:33 +000057
58 def __init__(self):
Jack Jansen113af982003-02-12 12:47:56 +000059 self.preffilepath = os.path.join("Python", "Package Install Manager Prefs")
Jack Jansen73019a62003-02-11 23:15:33 +000060 Wapplication.Application.__init__(self, 'Pimp')
61 from Carbon import AE
62 from Carbon import AppleEvents
63
64 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEOpenApplication,
65 self.ignoreevent)
66 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEReopenApplication,
67 self.ignoreevent)
68 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEPrintDocuments,
69 self.ignoreevent)
70 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEQuitApplication,
71 self.quitevent)
Jack Jansen113af982003-02-12 12:47:56 +000072 if 1:
Jack Jansen73019a62003-02-11 23:15:33 +000073 import PyConsole
74 # With -D option (OSX command line only) keep stderr, for debugging the IDE
75 # itself.
76 debug_stderr = None
77 if len(sys.argv) >= 2 and sys.argv[1] == '-D':
78 debug_stderr = sys.stderr
79 del sys.argv[1]
80 PyConsole.installoutput()
Jack Jansen73019a62003-02-11 23:15:33 +000081 if debug_stderr:
82 sys.stderr = debug_stderr
83 self.opendoc(None)
84 self.mainloop()
85
86 def makeusermenus(self):
87 m = Wapplication.Menu(self.menubar, "File")
Jack Jansen4ab84372003-02-14 14:13:25 +000088 newitem = FrameWork.MenuItem(m, "Open Standard Database", "N", 'openstandard')
89 openitem = FrameWork.MenuItem(m, "Open"+ELIPSES, "O", 'open')
90 openURLitem = FrameWork.MenuItem(m, "Open URL"+ELIPSES, "D", 'openURL')
Jack Jansen73019a62003-02-11 23:15:33 +000091 FrameWork.Separator(m)
92 closeitem = FrameWork.MenuItem(m, "Close", "W", 'close')
93## saveitem = FrameWork.MenuItem(m, "Save", "S", 'save')
Jack Jansen113af982003-02-12 12:47:56 +000094## saveasitem = FrameWork.MenuItem(m, "Save as"+ELIPSES, None, 'save_as')
Jack Jansen4ab84372003-02-14 14:13:25 +000095## FrameWork.Separator(m)
Jack Jansen73019a62003-02-11 23:15:33 +000096
97 m = Wapplication.Menu(self.menubar, "Edit")
98 undoitem = FrameWork.MenuItem(m, "Undo", 'Z', "undo")
99 FrameWork.Separator(m)
100 cutitem = FrameWork.MenuItem(m, "Cut", 'X', "cut")
101 copyitem = FrameWork.MenuItem(m, "Copy", "C", "copy")
102 pasteitem = FrameWork.MenuItem(m, "Paste", "V", "paste")
103 FrameWork.MenuItem(m, "Clear", None, "clear")
104 FrameWork.Separator(m)
105 selallitem = FrameWork.MenuItem(m, "Select all", "A", "selectall")
106
107 m = Wapplication.Menu(self.menubar, "Package")
108 runitem = FrameWork.MenuItem(m, "Install", "I", 'install')
109 homepageitem = FrameWork.MenuItem(m, "Visit Homepage", None, 'homepage')
110
111 self.openwindowsmenu = Wapplication.Menu(self.menubar, 'Windows')
112 self.makeopenwindowsmenu()
Jack Jansen31fa8452003-05-27 14:10:37 +0000113 self.makehelpmenu()
Jack Jansenffb8fef2003-02-12 15:39:56 +0000114 self._menustocheck = [closeitem,
Jack Jansen73019a62003-02-11 23:15:33 +0000115 undoitem, cutitem, copyitem, pasteitem,
116 selallitem,
117 runitem, homepageitem]
118
Jack Jansen31fa8452003-05-27 14:10:37 +0000119 def makehelpmenu(self):
120 python_app = os.path.join(sys.prefix, 'Resources/Python.app')
121 help_source = os.path.join(python_app, 'Contents/Resources/English.lproj/Documentation')
122 hashelp = os.path.isdir(help_source)
123
124 self.helpmenu = m = self.gethelpmenu()
125 helpitem1 = FrameWork.MenuItem(m, "PackageManager Help", None, self.domenu_packmanhelp)
126 helpitem1.enable(hashelp)
127 helpitem2 = FrameWork.MenuItem(m, "MacPython Help", None, self.domenu_pythonhelp)
128 helpitem2.enable(hashelp)
129
Jack Jansen73019a62003-02-11 23:15:33 +0000130 def quitevent(self, theAppleEvent, theReply):
Jack Jansen73019a62003-02-11 23:15:33 +0000131 self._quit()
132
133 def ignoreevent(self, theAppleEvent, theReply):
134 pass
135
136 def opendocsevent(self, theAppleEvent, theReply):
137 W.SetCursor('watch')
138 import aetools
139 parameters, args = aetools.unpackevent(theAppleEvent)
140 docs = parameters['----']
141 if type(docs) <> type([]):
142 docs = [docs]
143 for doc in docs:
144 fsr, a = doc.FSResolveAlias(None)
145 path = fsr.as_pathname()
146 path = urllib.pathname2url(path)
147 self.opendoc(path)
148
149 def opendoc(self, url):
150 PackageBrowser(url)
151
152 def getabouttext(self):
Jack Jansen113af982003-02-12 12:47:56 +0000153 return "About Package Manager"+ELIPSES
Jack Jansen73019a62003-02-11 23:15:33 +0000154
155 def do_about(self, id, item, window, event):
Jack Jansen113af982003-02-12 12:47:56 +0000156 EasyDialogs.Message("Package Install Manager for Python")
Jack Jansen4ab84372003-02-14 14:13:25 +0000157
158 def domenu_openstandard(self, *args):
159 self.opendoc(None)
160
Jack Jansen73019a62003-02-11 23:15:33 +0000161 def domenu_open(self, *args):
162 filename = EasyDialogs.AskFileForOpen(typeList=("TEXT",))
163 if filename:
164 filename = urllib.pathname2url(filename)
165 self.opendoc(filename)
Jack Jansen4ab84372003-02-14 14:13:25 +0000166
167 def domenu_openURL(self, *args):
168 ok = EasyDialogs.AskYesNoCancel(
169 "Warning: by opening a non-standard database "
170 "you are trusting the maintainer of it "
171 "to run arbitrary code on your machine.",
172 yes="OK", no="")
173 if ok <= 0: return
174 url = EasyDialogs.AskString("URL of database to open:", ok="Open")
175 if url:
176 self.opendoc(url)
Jack Jansen73019a62003-02-11 23:15:33 +0000177
178 def domenu_openbyname(self, *args):
179 url = EasyDialogs.AskString("Open URL:", ok="Open")
180 if url:
181 self.opendoc(url)
182
183 def makeopenwindowsmenu(self):
184 for i in range(len(self.openwindowsmenu.items)):
185 self.openwindowsmenu.menu.DeleteMenuItem(1)
186 self.openwindowsmenu.items = []
187 windows = []
188 self._openwindows = {}
189 for window in self._windows.keys():
190 title = window.GetWTitle()
191 if not title:
192 title = "<no title>"
193 windows.append((title, window))
194 windows.sort()
195 for title, window in windows:
196 shortcut = None
197 item = FrameWork.MenuItem(self.openwindowsmenu, title, shortcut, callback = self.domenu_openwindows)
198 self._openwindows[item.item] = window
199 self._openwindowscheckmark = 0
200 self.checkopenwindowsmenu()
201
202 def domenu_openwindows(self, id, item, window, event):
203 w = self._openwindows[item]
204 w.ShowWindow()
205 w.SelectWindow()
206
207 def domenu_quit(self):
208 self._quit()
209
210 def domenu_save(self, *args):
211 print "Save"
212
Jack Jansen31fa8452003-05-27 14:10:37 +0000213 def domenu_pythonhelp(self, *args):
214 from Carbon import AH
215 AH.AHGotoPage("MacPython Help", None, None)
216
217 def domenu_packmanhelp(self, *args):
218 from Carbon import AH
219 AH.AHGotoPage("MacPython Help", "packman.html", None)
220
Jack Jansen73019a62003-02-11 23:15:33 +0000221 def _quit(self):
222## import PyConsole, PyEdit
223 for window in self._windows.values():
224 try:
225 rv = window.close() # ignore any errors while quitting
226 except:
Jack Jansenf776dee2003-04-22 13:53:33 +0000227 rv = 0 # (otherwise, we can get stuck!)
Jack Jansen73019a62003-02-11 23:15:33 +0000228 if rv and rv > 0:
229 return
230## try:
231## PyConsole.console.writeprefs()
232## PyConsole.output.writeprefs()
233## PyEdit.searchengine.writeprefs()
234## except:
235## # Write to __stderr__ so the msg end up in Console.app and has
236## # at least _some_ chance of getting read...
237## # But: this is a workaround for way more serious problems with
238## # the Python 2.2 Jaguar addon.
239## sys.__stderr__.write("*** PythonIDE: Can't write preferences ***\n")
240 self.quitting = 1
241
242class PimpInterface:
243
244 def setuppimp(self, url):
245 self.pimpprefs = pimp.PimpPreferences()
246 self.pimpdb = pimp.PimpDatabase(self.pimpprefs)
Jack Jansen113af982003-02-12 12:47:56 +0000247 self.pimpinstaller = pimp.PimpInstaller(self.pimpdb)
Jack Jansen73019a62003-02-11 23:15:33 +0000248 if not url:
249 url = self.pimpprefs.pimpDatabase
Jack Jansen4ab84372003-02-14 14:13:25 +0000250 try:
251 self.pimpdb.appendURL(url)
252 except IOError, arg:
Jack Jansen36b51982003-04-16 12:40:21 +0000253 rv = "Cannot open %s: %s\n" % (url, arg)
254 rv += "\nSee MacPython Package Manager help page."
255 return rv
Jack Jansena950d7b2003-04-16 12:17:56 +0000256 # Check whether we can write the installation directory.
257 # If not, set to the per-user directory, possibly
258 # creating it, if needed.
259 installDir = self.pimpprefs.installDir
260 if not os.access(installDir, os.R_OK|os.W_OK|os.X_OK):
261 rv = self.setuserinstall(1)
262 if rv: return rv
Jack Jansend5532af2003-02-28 15:19:51 +0000263 return self.pimpprefs.check()
Jack Jansen4ab84372003-02-14 14:13:25 +0000264
265 def closepimp(self):
266 self.pimpdb.close()
267 self.pimpprefs = None
268 self.pimpdb = None
269 self.pimpinstaller = None
270 self.packages = []
Jack Jansen73019a62003-02-11 23:15:33 +0000271
Jack Jansena950d7b2003-04-16 12:17:56 +0000272 def setuserinstall(self, onoff):
273 rv = ""
274 if onoff:
275 if not os.path.exists(USER_INSTALL_DIR):
276 try:
277 os.makedirs(USER_INSTALL_DIR)
278 except OSError, arg:
279 rv = rv + arg + "\n"
280 if not USER_INSTALL_DIR in sys.path:
281 import site
282 reload(site)
283 self.pimpprefs.setInstallDir(USER_INSTALL_DIR)
284 else:
285 self.pimpprefs.setInstallDir(None)
286 rv = rv + self.pimpprefs.check()
287 return rv
288
289 def getuserinstall(self):
290 return self.pimpprefs.installDir == USER_INSTALL_DIR
291
292 def getbrowserdata(self, show_hidden=1):
Jack Jansenf776dee2003-04-22 13:53:33 +0000293 packages = self.pimpdb.list()
294 if show_hidden:
295 self.packages = packages
296 else:
297 self.packages = []
298 for pkg in packages:
299 name = pkg.fullname()
300 if name[0] == '(' and name[-1] == ')' and not show_hidden:
301 continue
302 self.packages.append(pkg)
Jack Jansen73019a62003-02-11 23:15:33 +0000303 rv = []
304 for pkg in self.packages:
305 name = pkg.fullname()
306 status, _ = pkg.installed()
307 description = pkg.description()
Jack Jansen9f0c5752003-05-29 22:07:27 +0000308 description_line1 = description.split('\n')[0]
309 rv.append((status, name, description_line1))
Jack Jansen73019a62003-02-11 23:15:33 +0000310 return rv
311
312 def getstatus(self, number):
313 pkg = self.packages[number]
314 return pkg.installed()
Jack Jansen113af982003-02-12 12:47:56 +0000315
Jack Jansena950d7b2003-04-16 12:17:56 +0000316 def installpackage(self, sel, output, recursive, force):
Jack Jansen113af982003-02-12 12:47:56 +0000317 pkg = self.packages[sel]
318 list, messages = self.pimpinstaller.prepareInstall(pkg, force, recursive)
319 if messages:
320 return messages
321 messages = self.pimpinstaller.install(list, output)
322 return messages
Jack Jansen73019a62003-02-11 23:15:33 +0000323
324class PackageBrowser(PimpInterface):
325
326 def __init__(self, url = None):
327 self.ic = None
Jack Jansena950d7b2003-04-16 12:17:56 +0000328 messages = self.setuppimp(url)
Jack Jansen73019a62003-02-11 23:15:33 +0000329 self.setupwidgets()
330 self.updatestatus()
Jack Jansena950d7b2003-04-16 12:17:56 +0000331 self.showmessages(messages)
Jack Jansen4ab84372003-02-14 14:13:25 +0000332
333 def close(self):
334 self.closepimp()
Jack Jansen73019a62003-02-11 23:15:33 +0000335
Jack Jansen43230902003-04-15 21:59:42 +0000336 def setupwidgets(self):
Jack Jansen9f0c5752003-05-29 22:07:27 +0000337 DESCRIPTION_HEIGHT = 140
Jack Jansen43230902003-04-15 21:59:42 +0000338 INSTALL_POS = -30
Jack Jansen9f0c5752003-05-29 22:07:27 +0000339 STATUS_POS = INSTALL_POS - (70 + DESCRIPTION_HEIGHT)
340 self.w = W.Window((580, 600), "Python Install Manager", minsize = (400, 400), tabbable = 0)
Jack Jansena950d7b2003-04-16 12:17:56 +0000341 self.w.titlebar = W.TextBox((4, 8, 60, 18), 'Packages:')
342 self.w.hidden_button = W.CheckBox((-100, 4, 0, 18), 'Show Hidden', self.updatestatus)
Jack Jansen73019a62003-02-11 23:15:33 +0000343 data = self.getbrowserdata()
Jack Jansena950d7b2003-04-16 12:17:56 +0000344 self.w.packagebrowser = W.MultiList((4, 24, 0, STATUS_POS-2), data, self.listhit, cols=3)
Jack Jansen43230902003-04-15 21:59:42 +0000345
Jack Jansen9f0c5752003-05-29 22:07:27 +0000346 self.w.installed_l = W.TextBox((4, STATUS_POS, 70, 12), 'Installed:')
347 self.w.installed = W.TextBox((74, STATUS_POS, 0, 12), '')
348 self.w.message_l = W.TextBox((4, STATUS_POS+20, 70, 12), 'Status:')
349 self.w.message = W.TextBox((74, STATUS_POS+20, 0, 12), '')
Jack Jansen43230902003-04-15 21:59:42 +0000350 self.w.homepage_button = W.Button((4, STATUS_POS+40, 96, 18), 'View homepage', self.do_homepage)
Jack Jansen9f0c5752003-05-29 22:07:27 +0000351 self.w.description_l = W.TextBox((4, STATUS_POS+70, 70, 12), 'Description:')
352 self.w.description = W.EditText((74, STATUS_POS+70, 0, DESCRIPTION_HEIGHT-4))
Jack Jansen43230902003-04-15 21:59:42 +0000353
Jack Jansena950d7b2003-04-16 12:17:56 +0000354 self.w.divline = W.HorizontalLine((0, INSTALL_POS-4, 0, 0))
355 self.w.verbose_button = W.CheckBox((84, INSTALL_POS+4, 60, 18), 'Verbose')
356 self.w.recursive_button = W.CheckBox((146, INSTALL_POS+4, 120, 18), 'Install dependencies', self.updatestatus)
Jack Jansen113af982003-02-12 12:47:56 +0000357 self.w.recursive_button.set(1)
Jack Jansena950d7b2003-04-16 12:17:56 +0000358 self.w.force_button = W.CheckBox((268, INSTALL_POS+4, 70, 18), 'Overwrite', self.updatestatus)
359 self.w.user_button = W.CheckBox((340, INSTALL_POS+4, 140, 18), 'For Current User Only', self.do_user)
360 self.w.install_button = W.Button((4, INSTALL_POS+4, 56, 18), 'Install:', self.do_install)
Jack Jansen73019a62003-02-11 23:15:33 +0000361 self.w.open()
Jack Jansen9f0c5752003-05-29 22:07:27 +0000362 self.w.description.enable(0)
Jack Jansen73019a62003-02-11 23:15:33 +0000363
364 def updatestatus(self):
365 sel = self.w.packagebrowser.getselection()
Jack Jansena950d7b2003-04-16 12:17:56 +0000366 data = self.getbrowserdata(self.w.hidden_button.get())
Jack Jansen113af982003-02-12 12:47:56 +0000367 self.w.packagebrowser.setitems(data)
Jack Jansena950d7b2003-04-16 12:17:56 +0000368 self.w.user_button.set(self.getuserinstall())
Jack Jansen73019a62003-02-11 23:15:33 +0000369 if len(sel) != 1:
370 self.w.installed.set('')
371 self.w.message.set('')
372 self.w.install_button.enable(0)
373 self.w.homepage_button.enable(0)
Jack Jansen9f0c5752003-05-29 22:07:27 +0000374 self.w.description.set('')
Jack Jansen73019a62003-02-11 23:15:33 +0000375 self.w.verbose_button.enable(0)
Jack Jansen113af982003-02-12 12:47:56 +0000376 self.w.recursive_button.enable(0)
Jack Jansen73019a62003-02-11 23:15:33 +0000377 self.w.force_button.enable(0)
Jack Jansen43230902003-04-15 21:59:42 +0000378 self.w.user_button.enable(0)
Jack Jansen73019a62003-02-11 23:15:33 +0000379 else:
380 sel = sel[0]
Jack Jansen113af982003-02-12 12:47:56 +0000381 self.w.packagebrowser.setselection([sel])
Jack Jansen73019a62003-02-11 23:15:33 +0000382 installed, message = self.getstatus(sel)
383 self.w.installed.set(installed)
384 self.w.message.set(message)
385 self.w.install_button.enable(installed != "yes" or self.w.force_button.get())
386 self.w.homepage_button.enable(not not self.packages[sel].homepage())
Jack Jansen9f0c5752003-05-29 22:07:27 +0000387 description = self.packages[sel].description()
388 description = description.split('\r\n')
389 description = '\r'.join(description)
390 self.w.description.set(description)
Jack Jansen73019a62003-02-11 23:15:33 +0000391 self.w.verbose_button.enable(1)
Jack Jansen113af982003-02-12 12:47:56 +0000392 self.w.recursive_button.enable(1)
Jack Jansen73019a62003-02-11 23:15:33 +0000393 self.w.force_button.enable(1)
Jack Jansena950d7b2003-04-16 12:17:56 +0000394 self.w.user_button.enable(1)
Jack Jansen73019a62003-02-11 23:15:33 +0000395
396 def listhit(self, *args, **kwargs):
397 self.updatestatus()
398
399 def do_install(self):
Jack Jansen113af982003-02-12 12:47:56 +0000400 sel = self.w.packagebrowser.getselection()[0]
401 if self.w.verbose_button.get():
402 output = sys.stdout
403 else:
404 output = None
405 recursive = self.w.recursive_button.get()
406 force = self.w.force_button.get()
Jack Jansena950d7b2003-04-16 12:17:56 +0000407 messages = self.installpackage(sel, output, recursive, force)
408
409 # Re-read .pth files
410 import site
411 reload(site)
412
Jack Jansen113af982003-02-12 12:47:56 +0000413 self.updatestatus()
Jack Jansena950d7b2003-04-16 12:17:56 +0000414 self.showmessages(messages)
415
416 def showmessages(self, messages):
Jack Jansen113af982003-02-12 12:47:56 +0000417 if messages:
Jack Jansena950d7b2003-04-16 12:17:56 +0000418 if type(messages) == list:
419 messages = '\n'.join(messages)
420 if self.w.verbose_button.get():
421 sys.stdout.write(messages + '\n')
422 EasyDialogs.Message(messages)
Jack Jansen73019a62003-02-11 23:15:33 +0000423
424 def do_homepage(self):
425 sel = self.w.packagebrowser.getselection()[0]
426 if not self.ic:
427 import ic
428
429 self.ic = ic.IC()
430 self.ic.launchurl(self.packages[sel].homepage())
431
Jack Jansena950d7b2003-04-16 12:17:56 +0000432 def do_user(self):
433 messages = self.setuserinstall(self.w.user_button.get())
434 self.updatestatus()
435 self.showmessages(messages)
436
Jack Jansen73019a62003-02-11 23:15:33 +0000437if __name__ == '__main__':
Jack Jansen113af982003-02-12 12:47:56 +0000438 PackageManagerMain()