1 ;;; package-admin.el --- Installation and Maintenance of XEmacs packages
3 ;; Copyright (C) 1997 by Free Software Foundation, Inc.
4 ;; Copyright (C) 2003, Steve Youngs.
6 ;; Author: SL Baur <steve@xemacs.org>
9 ;; This file is part of XEmacs.
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 ;;; Synched up with: Not in FSF
30 ;; First pass at lisp front end to package maintenance.
36 (defvar package-admin-xemacs (concat invocation-directory invocation-name)
37 "Location of XEmacs binary to use.")
39 (defvar package-admin-temp-buffer "*Package Output*"
40 "Temporary buffer where output of backend commands is saved.")
42 (defvar package-admin-install-function (if (eq system-type 'windows-nt)
43 'package-admin-install-function-mswindows
44 'package-admin-default-install-function)
45 "The function to call to install a package.
46 Three args are passed: FILENAME PKG-DIR BUFFER
47 Install package FILENAME into directory PKG-DIR, with any messages output
50 (defvar package-admin-error-messages '(
51 "No space left on device"
52 "No such file or directory"
54 "Read-only file system"
61 "Unable to create directory"
62 "Directory checksum error"
63 "Cannot exclusively open file"
68 "invalid compressed data"
69 "too many leaves in Huffman tree"
70 "not a valid zip file"
71 "first entry not deflated or stored"
73 "unexpected end of file"
75 "Regular expressions of possible error messages.
76 After each package extraction, the `package-admin-temp-buffer' buffer is
77 scanned for these messages. An error code is returned if one of these are
80 This is awful, but it exists because error return codes aren't reliable
83 (defvar package-admin-tar-filename-regexps
86 ;; drwxrwxr-x john/doe 123 1997-02-18 15:48 pathname
87 "\\S-+\\s-+[-a-z0-9_/]+\\s-+[0-9]+\\s-+[-0-9]+\\s-+[0-9:]+\\s-+\\(\\S-.*\\)"
89 ;; rwxrwxr-x 501/501 123 Feb 18 15:46 1997 pathname
90 ;; Solaris tar (phooey!):
91 ;; rwxrwxr-x501/501 123 Feb 18 15:46 1997 pathname
93 ;; -rw-r--r-- 147 1019 32919 Mar 26 12:00:09 1992 pathname
94 "\\S-+\\s-*[-a-z0-9_]+[/ ][-a-z0-9_]+\\s-+[0-9]+\\s-+[a-z][a-z][a-z]\\s-+[0-9]+\\s-+[0-9:]+\\s-+[0-9]+\\s-+\\(\\S-.*\\)"
97 ;; drwx Aug 31 02:01:41 1998 123 pathname
98 "\\S-+\\s-+[a-z][a-z][a-z]\\s-+[0-9]+\\s-+[0-9:]+\\s-+[0-9]+\\s-+[0-9]+\\s-+\\(\\S-.*\\)"
101 "List of regexps to use to search for tar filenames.
102 Note that \"\\(\" and \"\\)\" must be used to delimit the pathname (as
103 match #1). Don't put \"^\" to match the beginning of the line; this
104 is already implicit, as `looking-at' is used. Filenames can,
105 unfortunately, contain spaces, so be careful in constructing any
108 (defvar package-install-hook nil
109 "*List of hook functions to be called when a new package is successfully
110 installed. The hook function is passed two arguments: the package name, and
111 the install directory.")
113 (defvar package-delete-hook nil
114 "*List of hook functions to be called when a package is deleted. The
115 hook is called *before* the package is deleted. The hook function is passed
116 two arguments: the package name, and the install directory.")
118 (defun package-admin-install-function-mswindows (file pkg-dir buffer)
119 "Install function for mswindows."
120 (let ((default-directory (file-name-as-directory pkg-dir)))
121 (unless (file-directory-p default-directory)
122 (make-directory default-directory t))
123 (call-process "minitar" nil buffer t file)))
125 (defun package-admin-default-install-function (filename pkg-dir buffer)
126 "Default function to install a package.
127 Install package FILENAME into directory PKG-DIR, with any messages output
129 (let* ((pkg-dir (file-name-as-directory pkg-dir))
130 (default-directory pkg-dir)
131 (filename (expand-file-name filename)))
132 (unless (file-directory-p pkg-dir)
133 (make-directory pkg-dir t))
134 ;; Don't assume GNU tar.
135 (if (shell-command (concat "gunzip -c " filename " | tar xvf -") buffer)
139 ;; A few things needed by the following 2 functions.
142 (autoload 'package-get-info "package-get")
143 (autoload 'paths-decode-directory-path "find-paths")
144 (defvar package-get-install-to-user-init-directory))
146 (defun package-admin-find-top-directory (type &optional user-dir)
147 "Return the top level directory for a package.
149 Argument TYPE is a symbol that determines the type of package we're
150 trying to find a directory for.
152 Optional Argument USER-DIR if non-nil use directories off
153 `user-init-directory'. This overrides everything except
154 \"EMACSPACKAGEPATH\".
156 This function honours the environment variable \"EMACSPACKAGEPATH\"
157 and returns directories found there as a priority. If that variable
158 doesn't exist and USER-DIR is nil, check in the normal places.
160 If we still can't find a suitable directory, return nil.
162 Possible values for TYPE are:
164 std == For \"standard\" packages that go in '/xemacs-packages/'
165 mule == For \"mule\" packages that go in '/mule-packages/'
166 site == For \"unsupported\" packages that go in '/site-packages/'
168 Note: Type \"site\" is not yet fully supported."
169 (let* ((env-value (getenv "EMACSPACKAGEPATH"))
171 ;; First, check the environment var.
173 (let ((path-list (paths-decode-directory-path env-value 'drop-empties)))
174 (cond ((eq type 'std)
176 (if (equal (file-name-nondirectory
177 (directory-file-name (car path-list)))
179 (setq top-dir (car path-list)))
180 (setq path-list (cdr path-list))))
183 (if (equal (file-name-nondirectory
184 (directory-file-name (car path-list)))
186 (setq top-dir (car path-list)))
187 (setq path-list (cdr path-list)))))))
188 ;; Wasn't in the environment, try `user-init-directory' if
189 ;; USER-DIR is non-nil.
192 (cond ((eq type 'std)
193 (setq top-dir (file-name-as-directory
194 (expand-file-name "xemacs-packages" user-init-directory))))
196 (setq top-dir (file-name-as-directory
197 (expand-file-name "mule-packages" user-init-directory))))))
198 ;; Finally check the normal places
200 (let ((path-list (nth 1 (packages-find-packages
202 (packages-compute-package-locations user-init-directory)))))
203 (cond ((eq type 'std)
205 (if (equal (file-name-nondirectory
206 (directory-file-name (car path-list)))
208 (setq top-dir (car path-list)))
209 (setq path-list (cdr path-list))))
212 (if (equal (file-name-nondirectory
213 (directory-file-name (car path-list)))
215 (setq top-dir (car path-list)))
216 (setq path-list (cdr path-list)))))))
217 ;; Now return either the directory or nil.
220 (defun package-admin-get-install-dir (package &optional pkg-dir)
221 "Find a suitable installation directory for a package.
223 Argument PACKAGE is the package to find a installation directory for.
224 Optional Argument PKG-DIR, if non-nil is a directory to use for
227 If PKG-DIR is non-nil and writable, return that. Otherwise check to
228 see if the PACKAGE is already installed and return that location, if
229 it is writable. Finally, fall back to the `user-init-directory' if
230 all else fails. As a side effect of installing packages under
231 `user-init-directory' these packages become part of `early-packages'."
232 ;; If pkg-dir specified, return that if writable.
234 (file-writable-p (directory-file-name pkg-dir)))
236 ;; If the user want her packages under ~/.xemacs/, do so.
237 (let ((type (package-get-info package 'category)))
238 (if package-get-install-to-user-init-directory
240 (cond ((equal type "standard")
241 (setq pkg-dir (package-admin-find-top-directory 'std 'user-dir)))
243 (setq pkg-dir (package-admin-find-top-directory 'mule 'user-dir))))
245 ;; Maybe the package has been installed before, if so, return
247 (let ((package-feature (intern-soft (concat
248 (symbol-name package) "-autoloads")))
250 (when (and (not (eq package 'unknown))
251 (featurep package-feature)
252 (setq autoload-dir (feature-file package-feature))
253 (setq autoload-dir (file-name-directory autoload-dir))
254 (member autoload-dir (append early-package-load-path late-package-load-path)))
255 ;; Find the corresponding entry in late-package
257 (car-safe (member-if (lambda (h)
258 (string-match (concat "^" (regexp-quote h))
260 (append (cdr early-packages) late-packages)))))
262 (file-writable-p (directory-file-name pkg-dir)))
264 ;; OK, the package hasn't been previously installed so we need
265 ;; to guess where it should go.
266 (cond ((equal type "standard")
267 (setq pkg-dir (package-admin-find-top-directory 'std)))
269 (setq pkg-dir (package-admin-find-top-directory 'mule)))
271 (error 'invalid-operation
272 "Invalid package type")))
274 (file-writable-p (directory-file-name pkg-dir)))
276 ;; Oh no! Either we still haven't found a suitable
277 ;; directory, or we can't write to the one we did find.
278 ;; Drop back to the `user-init-directory'.
279 (if (y-or-n-p (format "Directory isn't writable, use %s instead? "
280 user-init-directory))
282 (cond ((equal type "standard")
283 (setq pkg-dir (package-admin-find-top-directory 'std 'user-dir)))
285 (setq pkg-dir (package-admin-find-top-directory 'mule 'user-dir)))
287 (error 'invalid-operation
288 "Invalid package type")))
289 ;; Turn on `package-get-install-to-user-init-directory'
290 ;; so we don't get asked for each package we try to
291 ;; install in this session.
292 (setq package-get-install-to-user-init-directory t)
294 ;; If we get to here XEmacs can't make up its mind and
295 ;; neither can the user, nothing left to do except barf. :-(
296 (error 'search-failed
298 "Can't find suitable installation directory for package: %s"
301 (defun package-admin-get-manifest-file (pkg-topdir package)
302 "Return the name of the MANIFEST file for package PACKAGE.
303 Note that PACKAGE is a symbol, and not a string."
304 (let ((dir (file-name-as-directory
305 (expand-file-name "pkginfo" pkg-topdir))))
306 (expand-file-name (concat "MANIFEST." (symbol-name package)) dir)))
308 (defun package-admin-check-manifest (pkg-outbuf pkg-topdir)
309 "Check for a MANIFEST.<package> file in the package distribution.
310 If it doesn't exist, create and write one.
311 PKG-OUTBUF is the buffer that holds the output from `tar', and PKG-TOPDIR
312 is the top-level directory under which the package was installed."
313 (let ((manifest-buf " *pkg-manifest*")
314 (old-case-fold-search case-fold-search)
315 regexp package-name pathname regexps)
317 (save-excursion ;; Probably redundant.
318 (set-buffer (get-buffer pkg-outbuf)) ;; Probably already the current buffer.
319 (goto-char (point-min))
321 ;; Make filenames case-insensitive, if necessary
322 (if (eq system-type 'windows-nt)
323 (setq case-fold-search t))
325 (setq regexp (concat "\\bpkginfo"
326 (char-to-string directory-sep-char)
329 ;; Look for the manifest.
330 (if (not (re-search-forward regexp nil t))
332 ;; We didn't find a manifest. Make one.
334 ;; Yuk. We weren't passed the package name, and so we have
335 ;; to dig for it. Look for it as the subdirectory name below
337 ;; Here, we don't use a single regexp because we want to search
338 ;; the directories for a package name in a particular order.
340 (let ((dirs '("lisp" "man"))
343 (setq rexp (concat "\\b" (car dirs)
344 "[\\/]\\([^\\/]+\\)[\//]"))
345 (if (re-search-forward rexp nil t)
347 (setq dirs (cdr dirs)))))
349 (setq package-name (buffer-substring (match-beginning 1)
352 ;; Get and erase the manifest buffer
353 (setq manifest-buf (get-buffer-create manifest-buf))
354 (buffer-disable-undo manifest-buf)
355 (erase-buffer manifest-buf)
357 ;; Now, scan through the output buffer, looking for
358 ;; file and directory names.
359 (goto-char (point-min))
361 (while (< (point) (point-max))
365 ;; scan through the regexps, looking for a pathname
366 (if (catch 'found-path
367 (setq regexps package-admin-tar-filename-regexps)
369 (if (looking-at (car regexps))
375 (throw 'found-path t)))
376 (setq regexps (cdr regexps))))
378 ;; found a pathname -- add it to the manifest
381 (set-buffer manifest-buf)
382 (goto-char (point-max))
383 (insert pathname "\n"))))
386 ;; Processed all lines.
387 ;; Now, create the file, pkginfo/MANIFEST.<pkgname>
389 ;; We use `expand-file-name' instead of `concat',
391 (setq pathname (expand-file-name "pkginfo"
393 ;; Create pkginfo, if necessary
394 (if (not (file-directory-p pathname))
395 (make-directory pathname))
396 (setq pathname (expand-file-name
397 (concat "MANIFEST." package-name)
400 (set-buffer manifest-buf)
401 ;; Put the files in sorted order
402 (if (fboundp 'sort-lines)
403 (sort-lines nil (point-min) (point-max))
404 (warn "`xemacs-base' not installed, MANIFEST.%s not sorted"
407 ;; Note that using `write-region' *BYPASSES* any check
408 ;; to see if XEmacs is currently editing/visiting the
410 (write-region (point-min) (point-max) pathname))
411 (kill-buffer manifest-buf))))))
412 ;; Restore old case-fold-search status
413 (setq case-fold-search old-case-fold-search))))
416 (defun package-admin-add-binary-package (file &optional pkg-dir)
417 "Install a pre-bytecompiled XEmacs package into package hierarchy."
418 (interactive "fPackage tarball: ")
419 (let ((buf (get-buffer-create package-admin-temp-buffer))
422 (setq pkg-dir (package-admin-get-install-dir 'unknown pkg-dir))
423 ;; Ensure that the current directory doesn't change
426 ;; This is not really needed
427 (setq default-directory (file-name-as-directory pkg-dir))
428 (setq case-fold-search t)
429 (buffer-disable-undo)
430 (goto-char (setq start (point-max)))
431 (if (= 0 (setq status (funcall package-admin-install-function
434 ;; First, check for errors.
435 ;; We can't necessarily rely upon process error codes.
438 (setq err-list package-admin-error-messages)
440 (if (re-search-forward (car err-list) nil t)
444 (setq err-list (cdr err-list))))
445 ;; Make sure that the MANIFEST file exists
446 (package-admin-check-manifest buf pkg-dir))))
449 (defun package-admin-rmtree (directory)
450 "Delete a directory and all of its contents, recursively.
451 This is a feeble attempt at making a portable rmdir."
452 (setq directory (file-name-as-directory directory))
453 (let ((files (directory-files directory nil nil nil t))
454 (dirs (directory-files directory nil nil nil 'dirs)))
456 (if (not (member (car dirs) '("." "..")))
457 (let ((dir (expand-file-name (car dirs) directory)))
459 (if (file-symlink-p dir) ;; just in case, handle symlinks
461 (package-admin-rmtree dir))
463 (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err)))))
464 (setq dirs (cdr dirs))))
467 (delete-file (expand-file-name (car files) directory))
469 (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err))))
470 (setq files (cdr files)))
472 (delete-directory directory)
474 (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err))))))
476 (defun package-admin-get-lispdir (pkg-topdir package)
477 (let (package-lispdir)
478 (if (and (setq package-lispdir (expand-file-name "lisp" pkg-topdir))
479 (setq package-lispdir (expand-file-name (symbol-name package)
481 (file-accessible-directory-p package-lispdir))
484 (defun package-admin-delete-binary-package (package pkg-topdir)
485 "Delete a binary installation of PACKAGE below directory PKG-TOPDIR.
486 PACKAGE is a symbol, not a string."
487 (let (manifest-file package-lispdir dirs file)
488 (setq pkg-topdir (package-admin-get-install-dir package pkg-topdir))
489 (setq manifest-file (package-admin-get-manifest-file pkg-topdir package))
490 (run-hook-with-args 'package-delete-hook package pkg-topdir)
491 (if (file-exists-p manifest-file)
493 ;; The manifest file exists! Use it to delete the old distribution.
494 (message "Removing old files for package \"%s\" ..." package)
497 (buffer-disable-undo)
499 (insert-file-contents manifest-file)
500 (goto-char (point-min))
502 ;; For each entry in the MANIFEST ...
503 (while (< (point) (point-max))
505 (setq file (expand-file-name (buffer-substring
509 (if (file-directory-p file)
510 ;; Keep a record of each directory
511 (setq dirs (cons file dirs))
513 ;; Make sure that the file is writable.
514 ;; (This is important under MS Windows.)
515 ;; I do not know why it important under MS Windows but
516 ;; 1. It bombs out when the file does not exist. This can be condition-cased
517 ;; 2. If I removed the write permissions, I do not want XEmacs to just ignore them.
518 ;; If it wants to, XEmacs may ask, but that is about all
519 ;; (set-file-modes file 438) ;; 438 -> #o666
520 ;; Note, user might have removed the file!
523 (error nil))) ;; We may want to turn the error into a Warning?
526 ;; Delete empty directories.
532 (delete-directory dir)))
534 ;; Delete the MANIFEST file
535 ;; (set-file-modes manifest-file 438) ;; 438 -> #o666
536 ;; Note. Packages can have MANIFEST in MANIFEST.
538 (delete-file manifest-file)
539 (error nil)) ;; Do warning?
540 (message "Removing old files for package \"%s\" ... done" package)))
541 ;; The manifest file doesn't exist. Fallback to just deleting the
542 ;; package-specific lisp directory, if it exists.
544 ;; Delete old lisp directory, if any
545 ;; Gads, this is ugly. However, we're not supposed to use `concat'
546 ;; in the name of portability.
547 (setq package-lispdir (package-admin-get-lispdir pkg-topdir package))
548 (when package-lispdir
549 (message "Removing old lisp directory \"%s\" ..." package-lispdir)
551 (package-admin-rmtree package-lispdir)
552 (message "Removing old lisp directory \"%s\" ... done" package-lispdir)))
553 ;; Delete the package from the database of installed packages.
554 (package-delete-name package)))
556 (provide 'package-admin)
558 ;;; package-admin.el ends here