1 ;;; package-admin.el --- Installation and Maintenance of XEmacs packages
3 ;; Copyright (C) 1997 by Free Software Foundation, Inc.
5 ;; Author: SL Baur <steve@xemacs.org>
8 ;; This file is part of XEmacs.
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 ;;; Synched up with: Not in FSF
29 ;; First pass at lisp front end to package maintenance.
35 (defvar package-admin-xemacs (concat invocation-directory invocation-name)
36 "Location of XEmacs binary to use.")
38 (defvar package-admin-temp-buffer "*Package Output*"
39 "Temporary buffer where output of backend commands is saved.")
41 (defvar package-admin-install-function (if (eq system-type 'windows-nt)
42 'package-admin-install-function-mswindows
43 'package-admin-default-install-function)
44 "The function to call to install a package.
45 Three args are passed: FILENAME PKG-DIR BUFFER
46 Install package FILENAME into directory PKG-DIR, with any messages output
49 (defvar package-admin-error-messages '(
50 "No space left on device"
51 "No such file or directory"
53 "Read-only file system"
60 "Unable to create directory"
61 "Directory checksum error"
62 "Cannot exclusively open file"
67 "invalid compressed data"
68 "too many leaves in Huffman tree"
69 "not a valid zip file"
70 "first entry not deflated or stored"
72 "unexpected end of file"
74 "Regular expressions of possible error messages.
75 After each package extraction, the `package-admin-temp-buffer' buffer is
76 scanned for these messages. An error code is returned if one of these are
79 This is awful, but it exists because error return codes aren't reliable
82 (defvar package-admin-tar-filename-regexps
85 ;; drwxrwxr-x john/doe 123 1997-02-18 15:48 pathname
86 "\\S-+\\s-+[-a-z0-9_/]+\\s-+[0-9]+\\s-+[-0-9]+\\s-+[0-9:]+\\s-+\\(\\S-.*\\)"
88 ;; rwxrwxr-x 501/501 123 Feb 18 15:46 1997 pathname
89 ;; Solaris tar (phooey!):
90 ;; rwxrwxr-x501/501 123 Feb 18 15:46 1997 pathname
92 ;; -rw-r--r-- 147 1019 32919 Mar 26 12:00:09 1992 pathname
93 "\\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-.*\\)"
96 ;; drwx Aug 31 02:01:41 1998 123 pathname
97 "\\S-+\\s-+[a-z][a-z][a-z]\\s-+[0-9]+\\s-+[0-9:]+\\s-+[0-9]+\\s-+[0-9]+\\s-+\\(\\S-.*\\)"
100 "List of regexps to use to search for tar filenames.
101 Note that \"\\(\" and \"\\)\" must be used to delimit the pathname (as
102 match #1). Don't put \"^\" to match the beginning of the line; this
103 is already implicit, as `looking-at' is used. Filenames can,
104 unfortunately, contain spaces, so be careful in constructing any
108 (defun package-admin-add-single-file-package (file destdir &optional pkg-dir)
109 "Install a single file Lisp package into XEmacs package hierarchy.
110 `file' should be the full path to the lisp file to install.
111 `destdir' should be a simple directory name.
112 The optional `pkg-dir' can be used to override the default package hierarchy
113 \(car \(last late-packages))."
114 (interactive "fLisp File: \nsDestination: ")
116 (setq pkg-dir (car (last late-packages))))
117 (let ((destination (concat pkg-dir "/lisp/" destdir))
118 (buf (get-buffer-create package-admin-temp-buffer)))
119 (call-process "add-little-package.sh"
123 ;; rest of command line follows
124 package-admin-xemacs file destination)))
126 (defun package-admin-install-function-mswindows (file pkg-dir buffer)
127 "Install function for mswindows."
128 (let ((default-directory (file-name-as-directory pkg-dir)))
129 (unless (file-directory-p default-directory)
130 (make-directory default-directory t))
131 (call-process "minitar" nil buffer t file)))
133 (defun package-admin-default-install-function (filename pkg-dir buffer)
134 "Default function to install a package.
135 Install package FILENAME into directory PKG-DIR, with any messages output
137 (let* ((pkg-dir (file-name-as-directory pkg-dir))
138 (default-directory pkg-dir)
139 (filename (expand-file-name filename)))
140 (unless (file-directory-p pkg-dir)
141 (make-directory pkg-dir t))
142 ;; Don't assume GNU tar.
143 (if (shell-command (concat "gunzip -c " filename " | tar xvf -") buffer)
148 ; (call-process "add-big-package.sh"
152 ; ;; rest of command line follows
153 ; package-admin-xemacs file pkg-dir))
155 (defun package-admin-get-install-dir (package pkg-dir &optional mule-related)
156 "If PKG-DIR is non-nil return that,
157 else return the current location of the package if it is already installed
158 or return a location appropriate for the package otherwise."
161 (let ((package-feature (intern-soft (concat
162 (symbol-name package) "-autoloads")))
164 (when (and (not (eq package 'unknown))
165 (featurep package-feature)
166 (setq autoload-dir (feature-file package-feature))
167 (setq autoload-dir (file-name-directory autoload-dir))
168 (member autoload-dir (append early-package-load-path late-package-load-path)))
169 ;; Find the corresponding entry in late-package
171 (car-safe (member-if (lambda (h)
172 (string-match (concat "^" (regexp-quote h))
174 (append (cdr early-packages) late-packages)))))
177 ;; Ok we need to guess
179 (package-admin-get-install-dir 'mule-base nil nil)
180 (if (eq package 'xemacs-base)
181 (car (last late-packages))
182 (package-admin-get-install-dir 'xemacs-base nil nil)))))))
186 (defun package-admin-get-manifest-file (pkg-topdir package)
187 "Return the name of the MANIFEST file for package PACKAGE.
188 Note that PACKAGE is a symbol, and not a string."
190 (setq dir (expand-file-name "pkginfo" pkg-topdir))
191 (expand-file-name (concat "MANIFEST." (symbol-name package)) dir)
194 (defun package-admin-check-manifest (pkg-outbuf pkg-topdir)
195 "Check for a MANIFEST.<package> file in the package distribution.
196 If it doesn't exist, create and write one.
197 PKG-OUTBUF is the buffer that holds the output from `tar', and PKG-TOPDIR
198 is the top-level directory under which the package was installed."
199 (let ( (manifest-buf " *pkg-manifest*")
200 old-case-fold-search regexp package-name pathname regexps)
201 ;; Save and restore the case-fold-search status.
202 ;; We do this in case we have to screw with it (as it the case of
203 ;; case-insensitive filesystems such as MS Windows).
204 (setq old-case-fold-search case-fold-search)
206 (save-excursion ;; Probably redundant.
207 (set-buffer (get-buffer pkg-outbuf)) ;; Probably already the
209 (goto-char (point-min))
211 ;; Make filenames case-insensitive, if necessary
212 (if (eq system-type 'windows-nt)
213 (setq case-fold-search t))
215 ;; We really should compute the regexp.
216 ;; However, directory-sep-char is currently broken, but we need
217 ;; functional code *NOW*.
218 (setq regexp "\\bpkginfo[\\/]MANIFEST\\...*")
220 ;; Look for the manifest.
221 (if (not (re-search-forward regexp nil t))
223 ;; We didn't find a manifest. Make one.
225 ;; Yuk. We weren't passed the package name, and so we have
226 ;; to dig for it. Look for it as the subdirectory name below
227 ;; "lisp", "man", "info", or "etc".
228 ;; Here, we don't use a single regexp because we want to search
229 ;; the directories for a package name in a particular order.
230 ;; The problem is that packages could have directories like
231 ;; "etc/sounds/" or "etc/photos/" and we don't want to get
232 ;; these confused with the actual package name (although, in
233 ;; the case of "etc/sounds/", it's probably correct).
235 (let ( (dirs '("lisp" "info" "man" "etc")) rexp)
237 (setq rexp (concat "\\b" (car dirs)
238 "[\\/]\\([^\\/]+\\)[\//]"))
239 (if (re-search-forward rexp nil t)
241 (setq dirs (cdr dirs))
244 (setq package-name (buffer-substring (match-beginning 1)
247 ;; Get and erase the manifest buffer
248 (setq manifest-buf (get-buffer-create manifest-buf))
249 (buffer-disable-undo manifest-buf)
250 (erase-buffer manifest-buf)
252 ;; Now, scan through the output buffer, looking for
253 ;; file and directory names.
254 (goto-char (point-min))
256 (while (< (point) (point-max))
260 ;; scan through the regexps, looking for a pathname
261 (if (catch 'found-path
262 (setq regexps package-admin-tar-filename-regexps)
264 (if (looking-at (car regexps))
270 (throw 'found-path t)
272 (setq regexps (cdr regexps))
276 ;; found a pathname -- add it to the manifest
279 (set-buffer manifest-buf)
280 (goto-char (point-max))
281 (insert pathname "\n")
287 ;; Processed all lines.
288 ;; Now, create the file, pkginfo/MANIFEST.<pkgname>
290 ;; We use `expand-file-name' instead of `concat',
292 (setq pathname (expand-file-name "pkginfo"
294 ;; Create pkginfo, if necessary
295 (if (not (file-directory-p pathname))
296 (make-directory pathname))
297 (setq pathname (expand-file-name
298 (concat "MANIFEST." package-name)
301 (set-buffer manifest-buf)
302 ;; Put the files in sorted order
303 (sort-lines nil (point-min) (point-max))
305 ;; Note that using `write-region' *BYPASSES* any check
306 ;; to see if XEmacs is currently editing/visiting the
308 (write-region (point-min) (point-max) pathname)
310 (kill-buffer manifest-buf)
313 ;; We can't determine the package name from an extracted
314 ;; file in the tar output buffer.
318 ;; Restore old case-fold-search status
319 (setq case-fold-search old-case-fold-search))
323 (defun package-admin-add-binary-package (file &optional pkg-dir)
324 "Install a pre-bytecompiled XEmacs package into package hierarchy."
325 (interactive "fPackage tarball: ")
326 (let ((buf (get-buffer-create package-admin-temp-buffer))
330 (setq pkg-dir (package-admin-get-install-dir 'unknown pkg-dir))
331 ;; Ensure that the current directory doesn't change
334 ;; This is not really needed
335 (setq default-directory (file-name-as-directory pkg-dir))
336 (setq case-fold-search t)
337 (buffer-disable-undo)
338 (goto-char (setq start (point-max)))
339 (if (= 0 (setq status (funcall package-admin-install-function
342 ;; First, check for errors.
343 ;; We can't necessarily rely upon process error codes.
346 (setq err-list package-admin-error-messages)
348 (if (re-search-forward (car err-list) nil t)
353 (setq err-list (cdr err-list))
356 ;; Make sure that the MANIFEST file exists
357 (package-admin-check-manifest buf pkg-dir)
363 (defun package-admin-rmtree (directory)
364 "Delete a directory and all of its contents, recursively.
365 This is a feeble attempt at making a portable rmdir."
366 (setq directory (file-name-as-directory directory))
367 (let ((files (directory-files directory nil nil nil t))
368 (dirs (directory-files directory nil nil nil 'dirs)))
370 (if (not (member (car dirs) '("." "..")))
371 (let ((dir (expand-file-name (car dirs) directory)))
373 (if (file-symlink-p dir) ;; just in case, handle symlinks
375 (package-admin-rmtree dir))
377 (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err)))))
378 (setq dirs (cdr dirs))))
381 (delete-file (expand-file-name (car files) directory))
383 (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err))))
384 (setq files (cdr files)))
386 (delete-directory directory)
388 (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err))))))
390 (defun package-admin-get-lispdir (pkg-topdir package)
391 (let (package-lispdir)
392 (if (and (setq package-lispdir (expand-file-name "lisp" pkg-topdir))
393 (setq package-lispdir (expand-file-name (symbol-name package)
395 (file-accessible-directory-p package-lispdir))
399 (defun package-admin-delete-binary-package (package pkg-topdir)
400 "Delete a binary installation of PACKAGE below directory PKG-TOPDIR.
401 PACKAGE is a symbol, not a string."
402 (let ( (tmpbuf " *pkg-manifest*") manifest-file package-lispdir dirs file)
403 (setq pkg-topdir (package-admin-get-install-dir package pkg-topdir))
404 (setq manifest-file (package-admin-get-manifest-file pkg-topdir package))
405 (if (file-exists-p manifest-file)
407 ;; The manifest file exists! Use it to delete the old distribution.
408 (message "Removing old files for package \"%s\" ..." package)
410 (setq tmpbuf (get-buffer-create tmpbuf))
411 (with-current-buffer tmpbuf
412 (buffer-disable-undo)
414 (insert-file-contents manifest-file)
415 (goto-char (point-min))
417 ;; For each entry in the MANIFEST ...
418 (while (< (point) (point-max))
420 (setq file (expand-file-name (buffer-substring
424 (if (file-directory-p file)
425 ;; Keep a record of each directory
426 (setq dirs (cons file dirs))
428 ;; Make sure that the file is writable.
429 ;; (This is important under MS Windows.)
430 ;; I do not know why it important under MS Windows but
431 ;; 1. It bombs out out when the file does not exist. This can be condition-cased
432 ;; 2. If I removed the write permissions, I do not want XEmacs to just ignore them.
433 ;; If it wants to, XEmacs may ask, but that is about all
434 ;; (set-file-modes file 438) ;; 438 -> #o666
435 ;; Note, user might have removed the file!
438 (error nil))) ;; We may want to turn the error into a Warning?
441 ;; Delete empty directories.
443 (let ( (orig-default-directory default-directory)
444 ;; directory files file
446 ;; Make sure we preserve the existing `default-directory'.
447 ;; JV, why does this change the default directory? Does it indeed?
450 ;; Warning: destructive sort!
451 (setq dirs (nreverse (sort dirs 'string<)))
452 ; ;; For each directory ...
454 ; (setq directory (file-name-as-directory (car dirs)))
455 ; (setq files (directory-files directory))
456 ; ;; Delete the directory if it's empty.
459 ; (setq file (car files))
460 ; (if (and (not (string= file "."))
461 ; (not (string= file "..")))
463 ; (setq files (cdr files))
467 ; (delete-directory directory))
468 ; (setq dirs (cdr dirs))
470 ;; JV, On all OS's that I know of delete-directory fails on
471 ;; on non-empty dirs anyway
475 (delete-directory dir)))
477 (setq default-directory orig-default-directory)
481 ;; Delete the MANIFEST file
482 ;; (set-file-modes manifest-file 438) ;; 438 -> #o666
483 ;; Note. Packages can have MANIFEST in MANIFEST.
485 (delete-file manifest-file)
486 (error nil)) ;; Do warning?
487 (message "Removing old files for package \"%s\" ... done" package))
488 ;; The manifest file doesn't exist. Fallback to just deleting the
489 ;; package-specific lisp directory, if it exists.
491 ;; Delete old lisp directory, if any
492 ;; Gads, this is ugly. However, we're not supposed to use `concat'
493 ;; in the name of portability.
494 (when (setq package-lispdir (package-admin-get-lispdir pkg-topdir
496 (message "Removing old lisp directory \"%s\" ..."
499 (package-admin-rmtree package-lispdir)
500 (message "Removing old lisp directory \"%s\" ... done"
503 ;; Delete the package from the database of installed packages.
504 (package-delete-name package)))
506 (provide 'package-admin)
508 ;;; package-admin.el ends here