blob: de56d59ecaf5518d3ea983f91a2dcad0ce5a76f0 [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', ''),
51 '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 Jansenffb8fef2003-02-12 15:39:56 +0000113 self._menustocheck = [closeitem,
Jack Jansen73019a62003-02-11 23:15:33 +0000114 undoitem, cutitem, copyitem, pasteitem,
115 selallitem,
116 runitem, homepageitem]
117
118 def quitevent(self, theAppleEvent, theReply):
Jack Jansen73019a62003-02-11 23:15:33 +0000119 self._quit()
120
121 def ignoreevent(self, theAppleEvent, theReply):
122 pass
123
124 def opendocsevent(self, theAppleEvent, theReply):
125 W.SetCursor('watch')
126 import aetools
127 parameters, args = aetools.unpackevent(theAppleEvent)
128 docs = parameters['----']
129 if type(docs) <> type([]):
130 docs = [docs]
131 for doc in docs:
132 fsr, a = doc.FSResolveAlias(None)
133 path = fsr.as_pathname()
134 path = urllib.pathname2url(path)
135 self.opendoc(path)
136
137 def opendoc(self, url):
138 PackageBrowser(url)
139
140 def getabouttext(self):
Jack Jansen113af982003-02-12 12:47:56 +0000141 return "About Package Manager"+ELIPSES
Jack Jansen73019a62003-02-11 23:15:33 +0000142
143 def do_about(self, id, item, window, event):
Jack Jansen113af982003-02-12 12:47:56 +0000144 EasyDialogs.Message("Package Install Manager for Python")
Jack Jansen4ab84372003-02-14 14:13:25 +0000145
146 def domenu_openstandard(self, *args):
147 self.opendoc(None)
148
Jack Jansen73019a62003-02-11 23:15:33 +0000149 def domenu_open(self, *args):
150 filename = EasyDialogs.AskFileForOpen(typeList=("TEXT",))
151 if filename:
152 filename = urllib.pathname2url(filename)
153 self.opendoc(filename)
Jack Jansen4ab84372003-02-14 14:13:25 +0000154
155 def domenu_openURL(self, *args):
156 ok = EasyDialogs.AskYesNoCancel(
157 "Warning: by opening a non-standard database "
158 "you are trusting the maintainer of it "
159 "to run arbitrary code on your machine.",
160 yes="OK", no="")
161 if ok <= 0: return
162 url = EasyDialogs.AskString("URL of database to open:", ok="Open")
163 if url:
164 self.opendoc(url)
Jack Jansen73019a62003-02-11 23:15:33 +0000165
166 def domenu_openbyname(self, *args):
167 url = EasyDialogs.AskString("Open URL:", ok="Open")
168 if url:
169 self.opendoc(url)
170
171 def makeopenwindowsmenu(self):
172 for i in range(len(self.openwindowsmenu.items)):
173 self.openwindowsmenu.menu.DeleteMenuItem(1)
174 self.openwindowsmenu.items = []
175 windows = []
176 self._openwindows = {}
177 for window in self._windows.keys():
178 title = window.GetWTitle()
179 if not title:
180 title = "<no title>"
181 windows.append((title, window))
182 windows.sort()
183 for title, window in windows:
184 shortcut = None
185 item = FrameWork.MenuItem(self.openwindowsmenu, title, shortcut, callback = self.domenu_openwindows)
186 self._openwindows[item.item] = window
187 self._openwindowscheckmark = 0
188 self.checkopenwindowsmenu()
189
190 def domenu_openwindows(self, id, item, window, event):
191 w = self._openwindows[item]
192 w.ShowWindow()
193 w.SelectWindow()
194
195 def domenu_quit(self):
196 self._quit()
197
198 def domenu_save(self, *args):
199 print "Save"
200
201 def _quit(self):
202## import PyConsole, PyEdit
203 for window in self._windows.values():
204 try:
205 rv = window.close() # ignore any errors while quitting
206 except:
207 rv = 0 # (otherwise, we can get stuck!)
208 if rv and rv > 0:
209 return
210## try:
211## PyConsole.console.writeprefs()
212## PyConsole.output.writeprefs()
213## PyEdit.searchengine.writeprefs()
214## except:
215## # Write to __stderr__ so the msg end up in Console.app and has
216## # at least _some_ chance of getting read...
217## # But: this is a workaround for way more serious problems with
218## # the Python 2.2 Jaguar addon.
219## sys.__stderr__.write("*** PythonIDE: Can't write preferences ***\n")
220 self.quitting = 1
221
222class PimpInterface:
223
224 def setuppimp(self, url):
225 self.pimpprefs = pimp.PimpPreferences()
226 self.pimpdb = pimp.PimpDatabase(self.pimpprefs)
Jack Jansen113af982003-02-12 12:47:56 +0000227 self.pimpinstaller = pimp.PimpInstaller(self.pimpdb)
Jack Jansen73019a62003-02-11 23:15:33 +0000228 if not url:
229 url = self.pimpprefs.pimpDatabase
Jack Jansen4ab84372003-02-14 14:13:25 +0000230 try:
231 self.pimpdb.appendURL(url)
232 except IOError, arg:
Jack Jansen36b51982003-04-16 12:40:21 +0000233 rv = "Cannot open %s: %s\n" % (url, arg)
234 rv += "\nSee MacPython Package Manager help page."
235 return rv
Jack Jansena950d7b2003-04-16 12:17:56 +0000236 # Check whether we can write the installation directory.
237 # If not, set to the per-user directory, possibly
238 # creating it, if needed.
239 installDir = self.pimpprefs.installDir
240 if not os.access(installDir, os.R_OK|os.W_OK|os.X_OK):
241 rv = self.setuserinstall(1)
242 if rv: return rv
Jack Jansend5532af2003-02-28 15:19:51 +0000243 return self.pimpprefs.check()
Jack Jansen4ab84372003-02-14 14:13:25 +0000244
245 def closepimp(self):
246 self.pimpdb.close()
247 self.pimpprefs = None
248 self.pimpdb = None
249 self.pimpinstaller = None
250 self.packages = []
Jack Jansen73019a62003-02-11 23:15:33 +0000251
Jack Jansena950d7b2003-04-16 12:17:56 +0000252 def setuserinstall(self, onoff):
253 rv = ""
254 if onoff:
255 if not os.path.exists(USER_INSTALL_DIR):
256 try:
257 os.makedirs(USER_INSTALL_DIR)
258 except OSError, arg:
259 rv = rv + arg + "\n"
260 if not USER_INSTALL_DIR in sys.path:
261 import site
262 reload(site)
263 self.pimpprefs.setInstallDir(USER_INSTALL_DIR)
264 else:
265 self.pimpprefs.setInstallDir(None)
266 rv = rv + self.pimpprefs.check()
267 return rv
268
269 def getuserinstall(self):
270 return self.pimpprefs.installDir == USER_INSTALL_DIR
271
272 def getbrowserdata(self, show_hidden=1):
Jack Jansen73019a62003-02-11 23:15:33 +0000273 self.packages = self.pimpdb.list()
274 rv = []
275 for pkg in self.packages:
276 name = pkg.fullname()
Jack Jansena950d7b2003-04-16 12:17:56 +0000277 if name[0] == '(' and name[-1] == ')' and not show_hidden:
278 continue
Jack Jansen73019a62003-02-11 23:15:33 +0000279 status, _ = pkg.installed()
280 description = pkg.description()
281 rv.append((status, name, description))
282 return rv
283
284 def getstatus(self, number):
285 pkg = self.packages[number]
286 return pkg.installed()
Jack Jansen113af982003-02-12 12:47:56 +0000287
Jack Jansena950d7b2003-04-16 12:17:56 +0000288 def installpackage(self, sel, output, recursive, force):
Jack Jansen113af982003-02-12 12:47:56 +0000289 pkg = self.packages[sel]
290 list, messages = self.pimpinstaller.prepareInstall(pkg, force, recursive)
291 if messages:
292 return messages
293 messages = self.pimpinstaller.install(list, output)
294 return messages
Jack Jansen73019a62003-02-11 23:15:33 +0000295
296class PackageBrowser(PimpInterface):
297
298 def __init__(self, url = None):
299 self.ic = None
Jack Jansena950d7b2003-04-16 12:17:56 +0000300 messages = self.setuppimp(url)
Jack Jansen73019a62003-02-11 23:15:33 +0000301 self.setupwidgets()
302 self.updatestatus()
Jack Jansena950d7b2003-04-16 12:17:56 +0000303 self.showmessages(messages)
Jack Jansen4ab84372003-02-14 14:13:25 +0000304
305 def close(self):
306 self.closepimp()
Jack Jansen73019a62003-02-11 23:15:33 +0000307
Jack Jansen43230902003-04-15 21:59:42 +0000308 def setupwidgets(self):
309 INSTALL_POS = -30
Jack Jansena950d7b2003-04-16 12:17:56 +0000310 STATUS_POS = INSTALL_POS - 70
Jack Jansenf3ef0382003-03-16 21:04:50 +0000311 self.w = W.Window((580, 400), "Python Install Manager", minsize = (400, 200), tabbable = 0)
Jack Jansena950d7b2003-04-16 12:17:56 +0000312 self.w.titlebar = W.TextBox((4, 8, 60, 18), 'Packages:')
313 self.w.hidden_button = W.CheckBox((-100, 4, 0, 18), 'Show Hidden', self.updatestatus)
Jack Jansen73019a62003-02-11 23:15:33 +0000314 data = self.getbrowserdata()
Jack Jansena950d7b2003-04-16 12:17:56 +0000315 self.w.packagebrowser = W.MultiList((4, 24, 0, STATUS_POS-2), data, self.listhit, cols=3)
Jack Jansen43230902003-04-15 21:59:42 +0000316
317 self.w.installed_l = W.TextBox((4, STATUS_POS, 60, 12), 'Installed:')
318 self.w.installed = W.TextBox((64, STATUS_POS, 0, 12), '')
319 self.w.message_l = W.TextBox((4, STATUS_POS+20, 60, 12), 'Status:')
320 self.w.message = W.TextBox((64, STATUS_POS+20, 0, 12), '')
321 self.w.homepage_button = W.Button((4, STATUS_POS+40, 96, 18), 'View homepage', self.do_homepage)
322
Jack Jansena950d7b2003-04-16 12:17:56 +0000323 self.w.divline = W.HorizontalLine((0, INSTALL_POS-4, 0, 0))
324 self.w.verbose_button = W.CheckBox((84, INSTALL_POS+4, 60, 18), 'Verbose')
325 self.w.recursive_button = W.CheckBox((146, INSTALL_POS+4, 120, 18), 'Install dependencies', self.updatestatus)
Jack Jansen113af982003-02-12 12:47:56 +0000326 self.w.recursive_button.set(1)
Jack Jansena950d7b2003-04-16 12:17:56 +0000327 self.w.force_button = W.CheckBox((268, INSTALL_POS+4, 70, 18), 'Overwrite', self.updatestatus)
328 self.w.user_button = W.CheckBox((340, INSTALL_POS+4, 140, 18), 'For Current User Only', self.do_user)
329 self.w.install_button = W.Button((4, INSTALL_POS+4, 56, 18), 'Install:', self.do_install)
Jack Jansen73019a62003-02-11 23:15:33 +0000330 self.w.open()
331
332 def updatestatus(self):
333 sel = self.w.packagebrowser.getselection()
Jack Jansena950d7b2003-04-16 12:17:56 +0000334 data = self.getbrowserdata(self.w.hidden_button.get())
Jack Jansen113af982003-02-12 12:47:56 +0000335 self.w.packagebrowser.setitems(data)
Jack Jansena950d7b2003-04-16 12:17:56 +0000336 self.w.user_button.set(self.getuserinstall())
Jack Jansen73019a62003-02-11 23:15:33 +0000337 if len(sel) != 1:
338 self.w.installed.set('')
339 self.w.message.set('')
340 self.w.install_button.enable(0)
341 self.w.homepage_button.enable(0)
342 self.w.verbose_button.enable(0)
Jack Jansen113af982003-02-12 12:47:56 +0000343 self.w.recursive_button.enable(0)
Jack Jansen73019a62003-02-11 23:15:33 +0000344 self.w.force_button.enable(0)
Jack Jansen43230902003-04-15 21:59:42 +0000345 self.w.user_button.enable(0)
Jack Jansen73019a62003-02-11 23:15:33 +0000346 else:
347 sel = sel[0]
Jack Jansen113af982003-02-12 12:47:56 +0000348 self.w.packagebrowser.setselection([sel])
Jack Jansen73019a62003-02-11 23:15:33 +0000349 installed, message = self.getstatus(sel)
350 self.w.installed.set(installed)
351 self.w.message.set(message)
352 self.w.install_button.enable(installed != "yes" or self.w.force_button.get())
353 self.w.homepage_button.enable(not not self.packages[sel].homepage())
354 self.w.verbose_button.enable(1)
Jack Jansen113af982003-02-12 12:47:56 +0000355 self.w.recursive_button.enable(1)
Jack Jansen73019a62003-02-11 23:15:33 +0000356 self.w.force_button.enable(1)
Jack Jansena950d7b2003-04-16 12:17:56 +0000357 self.w.user_button.enable(1)
Jack Jansen73019a62003-02-11 23:15:33 +0000358
359 def listhit(self, *args, **kwargs):
360 self.updatestatus()
361
362 def do_install(self):
Jack Jansen113af982003-02-12 12:47:56 +0000363 sel = self.w.packagebrowser.getselection()[0]
364 if self.w.verbose_button.get():
365 output = sys.stdout
366 else:
367 output = None
368 recursive = self.w.recursive_button.get()
369 force = self.w.force_button.get()
Jack Jansena950d7b2003-04-16 12:17:56 +0000370 messages = self.installpackage(sel, output, recursive, force)
371
372 # Re-read .pth files
373 import site
374 reload(site)
375
Jack Jansen113af982003-02-12 12:47:56 +0000376 self.updatestatus()
Jack Jansena950d7b2003-04-16 12:17:56 +0000377 self.showmessages(messages)
378
379 def showmessages(self, messages):
Jack Jansen113af982003-02-12 12:47:56 +0000380 if messages:
Jack Jansena950d7b2003-04-16 12:17:56 +0000381 if type(messages) == list:
382 messages = '\n'.join(messages)
383 if self.w.verbose_button.get():
384 sys.stdout.write(messages + '\n')
385 EasyDialogs.Message(messages)
Jack Jansen73019a62003-02-11 23:15:33 +0000386
387 def do_homepage(self):
388 sel = self.w.packagebrowser.getselection()[0]
389 if not self.ic:
390 import ic
391
392 self.ic = ic.IC()
393 self.ic.launchurl(self.packages[sel].homepage())
394
Jack Jansena950d7b2003-04-16 12:17:56 +0000395 def do_user(self):
396 messages = self.setuserinstall(self.w.user_button.get())
397 self.updatestatus()
398 self.showmessages(messages)
399
Jack Jansen73019a62003-02-11 23:15:33 +0000400if __name__ == '__main__':
Jack Jansen113af982003-02-12 12:47:56 +0000401 PackageManagerMain()