1 ;;; package-admin.el --- Installation and Maintenance of XEmacs packages
3 ;; Copyright (C) 1997 by Free Software Foundation, Inc.
5 ;; Author: SL Baur <steve@altair.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 'package-admin-default-install-function
42 "The function to call to install a package.
43 Three args are passed: FILENAME PKG-DIR BUF
44 Install package FILENAME into directory PKG-DIR, with any messages output
47 (defvar package-admin-error-messages '(
48 "No space left on device"
49 "No such file or directory"
51 "Read-only file system"
58 "Unable to create directory"
59 "Directory checksum error"
60 "Cannot exclusively open file"
65 "invalid compressed data"
66 "too many leaves in Huffman tree"
67 "not a valid zip file"
68 "first entry not deflated or stored"
70 "unexpected end of file"
72 "Regular expressions of possible error messages.
73 After each package extraction, the `package-admin-temp-buffer' buffer is
74 scanned for these messages. An error code is returned if one of these are
77 This is awful, but it exists because error return codes aren't reliable
80 (defvar package-admin-tar-filename-regexps
83 ;; drwxrwxr-x john/doe 123 1997-02-18 15:48 pathname
84 "\\S-+\\s-+[-a-z0-9_/]+\\s-+[0-9]+\\s-+[-0-9]+\\s-+[0-9:]+\\s-+\\(\\S-.*\\)"
86 ;; rwxrwxr-x 501/501 123 Feb 18 15:46 1997 pathname
87 ;; Solaris tar (phooey!):
88 ;; rwxrwxr-x501/501 123 Feb 18 15:46 1997 pathname
90 ;; -rw-r--r-- 147 1019 32919 Mar 26 12:00:09 1992 pathname
91 "\\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-.*\\)"
94 ;; drwx Aug 31 02:01:41 1998 123 pathname
95 "\\S-+\\s-+[a-z][a-z][a-z]\\s-+[0-9]+\\s-+[0-9:]+\\s-+[0-9]+\\s-+[0-9]+\\s-+\\(\\S-.*\\)"
98 "List of regexps to use to search for tar filenames.
99 Note that \"\\(\" and \"\\)\" must be used to delimit the pathname (as
100 match #1). Don't put \"^\" to match the beginning of the line; this
101 is already implicit, as `looking-at' is used. Filenames can,
102 unfortunately, contain spaces, so be careful in constructing any
106 (defun package-admin-add-single-file-package (file destdir &optional pkg-dir)
107 "Install a single file Lisp package into XEmacs package hierarchy.
108 `file' should be the full path to the lisp file to install.
109 `destdir' should be a simple directory name.
110 The optional `pkg-dir' can be used to override the default package hierarchy
111 \(car \(last late-packages))."
112 (interactive "fLisp File: \nsDestination: ")
114 (setq pkg-dir (car (last late-packages))))
115 (let ((destination (concat pkg-dir "/lisp/" destdir))
116 (buf (get-buffer-create package-admin-temp-buffer)))
117 (call-process "add-little-package.sh"
121 ;; rest of command line follows
122 package-admin-xemacs file destination)))
124 (defun package-admin-install-function-mswindows (file pkg-dir buf)
125 "Install function for mswindows"
126 (let ( (default-directory pkg-dir) )
127 (call-process "djtar" nil buf t "-x" file)
130 (defun package-admin-default-install-function (file pkg-dir buf)
131 "Default function to install a package.
132 Install package FILENAME into directory PKG-DIR, with any messages output
135 (setq filename (expand-file-name file pkg-dir))
136 ;; Don't assume GNU tar.
137 (if (shell-command (concat "gunzip -c " filename " | tar xvf -") buf)
142 ; (call-process "add-big-package.sh"
146 ; ;; rest of command line follows
147 ; package-admin-xemacs file pkg-dir))
149 (defun package-admin-get-install-dir (pkg-dir)
151 (when (or (not (listp late-packages))
153 (error "No package path"))
154 (setq pkg-dir (car (last late-packages))))
158 (defun package-admin-get-manifest-file (pkg-topdir package)
159 "Return the name of the MANIFEST file for package PACKAGE.
160 Note that PACKAGE is a symbol, and not a string."
162 (setq dir (expand-file-name "pkginfo" pkg-topdir))
163 (expand-file-name (concat "MANIFEST." (symbol-name package)) dir)
166 (defun package-admin-check-manifest (pkg-outbuf pkg-topdir)
167 "Check for a MANIFEST.<package> file in the package distribution.
168 If it doesn't exist, create and write one.
169 PKG-OUTBUF is the buffer that holds the output from `tar', and PKG-TOPDIR
170 is the top-level directory under which the package was installed."
171 (let ( (manifest-buf " *pkg-manifest*")
172 old-case-fold-search regexp package-name pathname regexps)
173 ;; Save and restore the case-fold-search status.
174 ;; We do this in case we have to screw with it (as it the case of
175 ;; case-insensitive filesystems such as MS Windows).
176 (setq old-case-fold-search case-fold-search)
178 (save-excursion ;; Probably redundant.
179 (set-buffer (get-buffer pkg-outbuf)) ;; Probably already the
181 (goto-char (point-min))
183 ;; Make filenames case-insensitive, if necessary
184 (if (eq system-type 'windows-nt)
185 (setq case-fold-search t))
187 ;; We really should compute the regexp.
188 ;; However, directory-sep-char is currently broken, but we need
189 ;; functional code *NOW*.
190 (setq regexp "\\bpkginfo[\\/]MANIFEST\\...*")
192 ;; Look for the manifest.
193 (if (not (re-search-forward regexp nil t))
195 ;; We didn't find a manifest. Make one.
197 ;; Yuk. We weren't passed the package name, and so we have
198 ;; to dig for it. Look for it as the subdirectory name below
199 ;; "lisp", "man", "info", or "etc".
200 ;; Here, we don't use a single regexp because we want to search
201 ;; the directories for a package name in a particular order.
202 ;; The problem is that packages could have directories like
203 ;; "etc/sounds/" or "etc/photos/" and we don't want to get
204 ;; these confused with the actual package name (although, in
205 ;; the case of "etc/sounds/", it's probably correct).
207 (let ( (dirs '("lisp" "info" "man" "etc")) rexp)
209 (setq rexp (concat "\\b" (car dirs)
210 "[\\/]\\([^\\/]+\\)[\//]"))
211 (if (re-search-forward rexp nil t)
213 (setq dirs (cdr dirs))
216 (setq package-name (buffer-substring (match-beginning 1)
219 ;; Get and erase the manifest buffer
220 (setq manifest-buf (get-buffer-create manifest-buf))
221 (buffer-disable-undo manifest-buf)
222 (erase-buffer manifest-buf)
224 ;; Now, scan through the output buffer, looking for
225 ;; file and directory names.
226 (goto-char (point-min))
228 (while (< (point) (point-max))
232 ;; scan through the regexps, looking for a pathname
233 (if (catch 'found-path
234 (setq regexps package-admin-tar-filename-regexps)
236 (if (looking-at (car regexps))
242 (throw 'found-path t)
244 (setq regexps (cdr regexps))
248 ;; found a pathname -- add it to the manifest
251 (set-buffer manifest-buf)
252 (goto-char (point-max))
253 (insert pathname "\n")
259 ;; Processed all lines.
260 ;; Now, create the file, pkginfo/MANIFEST.<pkgname>
262 ;; We use `expand-file-name' instead of `concat',
264 (setq pathname (expand-file-name "pkginfo"
266 ;; Create pkginfo, if necessary
267 (if (not (file-directory-p pathname))
268 (make-directory pathname))
269 (setq pathname (expand-file-name
270 (concat "MANIFEST." package-name)
273 (set-buffer manifest-buf)
274 ;; Put the files in sorted order
275 (sort-lines nil (point-min) (point-max))
277 ;; Note that using `write-region' *BYPASSES* any check
278 ;; to see if XEmacs is currently editing/visiting the
280 (write-region (point-min) (point-max) pathname)
282 (kill-buffer manifest-buf)
285 ;; We can't determine the package name from an extracted
286 ;; file in the tar output buffer.
290 ;; Restore old case-fold-search status
291 (setq case-fold-search old-case-fold-search))
295 (defun package-admin-add-binary-package (file &optional pkg-dir)
296 "Install a pre-bytecompiled XEmacs package into package hierarchy."
297 (interactive "fPackage tarball: ")
298 (let ((buf (get-buffer-create package-admin-temp-buffer))
302 (setq pkg-dir (package-admin-get-install-dir pkg-dir))
303 ;; Insure that the current directory doesn't change
306 (setq default-directory pkg-dir)
307 (setq case-fold-search t)
308 (buffer-disable-undo)
309 (goto-char (setq start (point-max)))
310 (if (= 0 (setq status (funcall package-admin-install-function
313 ;; First, check for errors.
314 ;; We can't necessarily rely upon process error codes.
317 (setq err-list package-admin-error-messages)
319 (if (re-search-forward (car err-list) nil t)
324 (setq err-list (cdr err-list))
327 ;; Make sure that the MANIFEST file exists
328 (package-admin-check-manifest buf pkg-dir)
334 (defun package-admin-rmtree (directory)
335 "Delete a directory and all of its contents, recursively.
336 This is a feeble attempt at making a portable rmdir."
337 (let ( (orig-default-directory default-directory) files dirs dir)
340 (setq directory (file-name-as-directory directory))
341 (setq files (directory-files directory nil nil nil t))
342 (setq dirs (directory-files directory nil nil nil 'dirs))
344 (setq dir (car dirs))
345 (if (file-symlink-p dir) ;; just in case, handle symlinks
347 (if (not (or (string-equal dir ".") (string-equal dir "..")))
348 (package-admin-rmtree (expand-file-name dir directory))))
349 (setq dirs (cdr dirs))
351 (setq default-directory directory)
355 (delete-file (car files))
356 (setq files (cdr files))
358 (delete-directory directory)
361 (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err)))
365 (setq default-directory orig-default-directory)
369 (defun package-admin-get-lispdir (pkg-topdir package)
370 (let (package-lispdir)
371 (if (and (setq package-lispdir (expand-file-name "lisp" pkg-topdir))
372 (setq package-lispdir (expand-file-name (symbol-name package)
374 (file-accessible-directory-p package-lispdir))
378 (defun package-admin-delete-binary-package (package pkg-topdir)
379 "Delete a binary installation of PACKAGE below directory PKG-TOPDIR.
380 PACKAGE is a symbol, not a string."
381 (let ( (tmpbuf " *pkg-manifest*") manifest-file package-lispdir dirs file)
383 (setq pkg-topdir (package-admin-get-install-dir nil)))
384 (setq manifest-file (package-admin-get-manifest-file pkg-topdir package))
385 (if (file-exists-p manifest-file)
387 ;; The manifest file exists! Use it to delete the old distribution.
388 (message "Removing old files for package \"%s\" ..." package)
390 (setq tmpbuf (get-buffer-create tmpbuf))
393 (buffer-disable-undo tmpbuf)
394 (erase-buffer tmpbuf)
395 (insert-file-contents manifest-file)
396 (goto-char (point-min))
397 ;; For each entry in the MANIFEST ...
398 (while (< (point) (point-max))
400 (setq file (expand-file-name (buffer-substring
402 (save-excursion (end-of-line)
405 (if (file-directory-p file)
406 ;; Keep a record of each directory
407 (setq dirs (cons file dirs))
410 ;; Make sure that the file is writable.
411 ;; (This is important under MS Windows.)
412 (set-file-modes file 438) ;; 438 -> #o666
417 ;; Delete empty directories.
419 (let ( (orig-default-directory default-directory)
420 directory files file )
421 ;; Make sure we preserve the existing `default-directory'.
424 ;; Warning: destructive sort!
425 (setq dirs (nreverse (sort dirs 'string<)))
426 ;; For each directory ...
428 (setq directory (file-name-as-directory (car dirs)))
429 (setq files (directory-files directory))
430 ;; Delete the directory if it's empty.
433 (setq file (car files))
434 (if (and (not (string= file "."))
435 (not (string= file "..")))
437 (setq files (cdr files))
440 (delete-directory directory))
441 (setq dirs (cdr dirs))
444 (setq default-directory orig-default-directory)
448 ;; Delete the MANIFEST file
449 (set-file-modes manifest-file 438) ;; 438 -> #o666
450 (delete-file manifest-file)
451 (message "Removing old files for package \"%s\" ... done" package)
454 ;; The manifest file doesn't exist. Fallback to just deleting the
455 ;; package-specific lisp directory, if it exists.
457 ;; Delete old lisp directory, if any
458 ;; Gads, this is ugly. However, we're not supposed to use `concat'
459 ;; in the name of portability.
460 (if (setq package-lispdir (package-admin-get-lispdir pkg-topdir
463 (message "Removing old lisp directory \"%s\" ..."
466 (package-admin-rmtree package-lispdir)
467 (message "Removing old lisp directory \"%s\" ... done"
471 ;; Delete the package from the database of installed packages.
472 (package-delete-name package)
475 (provide 'package-admin)
477 ;;; package-admin.el ends here