XEmacs 21.2.9
[chise/xemacs-chise.git.1] / lisp / package-admin.el
1 ;;; package-admin.el --- Installation and Maintenance of XEmacs packages
2
3 ;; Copyright (C) 1997 by Free Software Foundation, Inc.
4
5 ;; Author: SL Baur <steve@altair.xemacs.org>
6 ;; Keywords: internal
7
8 ;; This file is part of XEmacs.
9
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)
13 ;; any later version.
14
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.
19
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
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: Not in FSF
26
27 ;;; Commentary:
28
29 ;; First pass at lisp front end to package maintenance.
30
31 ;;; Code:
32
33 (require 'config)
34
35 (defvar package-admin-xemacs (concat invocation-directory invocation-name)
36   "Location of XEmacs binary to use.")
37
38 (defvar package-admin-temp-buffer "*Package Output*"
39   "Temporary buffer where output of backend commands is saved.")
40
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 BUF
46 Install package FILENAME into directory PKG-DIR, with any messages output
47 to buffer BUF.")
48
49 (defvar package-admin-error-messages '(
50                                        "No space left on device"
51                                        "No such file or directory"
52                                        "Filename too long"
53                                        "Read-only file system"
54                                        "File too large"
55                                        "Too many open files"
56                                        "Not enough space"
57                                        "Permission denied"
58                                        "Input/output error"
59                                        "Out of memory"
60                                        "Unable to create directory"
61                                        "Directory checksum error"
62                                        "Cannot exclusively open file"
63                                        "corrupted file"
64                                        "incomplete .* tree"
65                                        "Bad table"
66                                        "corrupt input"
67                                        "invalid compressed data"
68                                        "too many leaves in Huffman tree"
69                                        "not a valid zip file"
70                                        "first entry not deflated or stored"
71                                        "encrypted file --"
72                                        "unexpected end of file"
73                                        )
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
77 found.
78
79 This is awful, but it exists because error return codes aren't reliable
80 under MS Windows.")
81
82 (defvar package-admin-tar-filename-regexps
83   '(
84     ;; GNU tar:
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-.*\\)"
87     ;; HP-UX & SunOS tar:
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
91     ;; AIX tar:
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-.*\\)"
94
95     ;; djtar:
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-.*\\)"
98
99     )
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
105 regexps.")
106
107 ;;;###autoload
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: ")
115   (when (null pkg-dir)
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"
120                   nil
121                   buf
122                   t
123                   ;; rest of command line follows
124                   package-admin-xemacs file destination)))
125
126 (defun package-admin-install-function-mswindows (file pkg-dir buf)
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 buf t file)))
132
133 (defun package-admin-default-install-function (file pkg-dir buf)
134   "Default function to install a package.
135 Install package FILENAME into directory PKG-DIR, with any messages output
136 to buffer BUF."
137   (let* ((pkg-dir (file-name-as-directory pkg-dir))
138          (default-directory pkg-dir)
139          (filename (expand-file-name file)))
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 -") buf)
144         0
145       1)
146     ))
147
148 ;  (call-process "add-big-package.sh"
149 ;               nil
150 ;               buf
151 ;               t
152 ;               ;; rest of command line follows
153 ;               package-admin-xemacs file pkg-dir))
154
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."
159   (if pkg-dir
160       pkg-dir
161     (let ((package-feature (intern-soft (concat
162                                          (symbol-name package) "-autoloads")))
163           autoload-dir)
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 late-package-load-path))
169         ;; Find the corresonding entry in late-package
170         (setq pkg-dir
171               (car-safe (member-if (lambda (h)
172                            (string-match (concat "^" (regexp-quote h))
173                                          autoload-dir))
174                          late-packages))))
175       (if pkg-dir
176           pkg-dir
177         ;; Ok we need to guess
178         (if mule-related
179             (package-admin-get-install-dir 'mule-base nil nil)
180           (car (last late-packages)))))))
181           
182
183
184 (defun package-admin-get-manifest-file (pkg-topdir package)
185   "Return the name of the MANIFEST file for package PACKAGE.
186 Note that PACKAGE is a symbol, and not a string."
187   (let (dir)
188     (setq dir (expand-file-name "pkginfo" pkg-topdir))
189     (expand-file-name (concat "MANIFEST." (symbol-name package)) dir)
190     ))
191
192 (defun package-admin-check-manifest (pkg-outbuf pkg-topdir)
193   "Check for a MANIFEST.<package> file in the package distribution.
194 If it doesn't exist, create and write one.
195 PKG-OUTBUF is the buffer that holds the output from `tar', and PKG-TOPDIR
196 is the top-level directory under which the package was installed."
197   (let ( (manifest-buf " *pkg-manifest*")
198          old-case-fold-search regexp package-name pathname regexps)
199     ;; Save and restore the case-fold-search status.
200     ;; We do this in case we have to screw with it (as it the case of
201     ;; case-insensitive filesystems such as MS Windows).
202     (setq old-case-fold-search case-fold-search)
203     (unwind-protect
204         (save-excursion                         ;; Probably redundant.
205           (set-buffer (get-buffer pkg-outbuf))  ;; Probably already the
206                                                 ;; current buffer.
207           (goto-char (point-min))
208
209           ;; Make filenames case-insensitive, if necessary
210           (if (eq system-type 'windows-nt)
211               (setq case-fold-search t))
212
213           ;; We really should compute the regexp.
214           ;; However, directory-sep-char is currently broken, but we need
215           ;; functional code *NOW*.
216           (setq regexp "\\bpkginfo[\\/]MANIFEST\\...*")
217
218           ;; Look for the manifest.
219           (if (not (re-search-forward regexp nil t))
220               (progn
221                 ;; We didn't find a manifest.  Make one.
222
223                 ;; Yuk.  We weren't passed the package name, and so we have
224                 ;; to dig for it.  Look for it as the subdirectory name below
225                 ;; "lisp", "man", "info", or "etc".
226                 ;; Here, we don't use a single regexp because we want to search
227                 ;; the directories for a package name in a particular order.
228                 ;; The problem is that packages could have directories like
229                 ;; "etc/sounds/" or "etc/photos/" and we don't want to get
230                 ;; these confused with the actual package name (although, in
231                 ;; the case of "etc/sounds/", it's probably correct).
232                 (if (catch 'done
233                       (let ( (dirs '("lisp" "info" "man" "etc")) rexp)
234                         (while dirs
235                           (setq rexp (concat "\\b" (car dirs)
236                                              "[\\/]\\([^\\/]+\\)[\//]"))
237                           (if (re-search-forward rexp nil t)
238                               (throw 'done t))
239                           (setq dirs (cdr dirs))
240                           )))
241                     (progn
242                       (setq package-name (buffer-substring (match-beginning 1)
243                                                            (match-end 1)))
244
245                       ;; Get and erase the manifest buffer
246                       (setq manifest-buf (get-buffer-create manifest-buf))
247                       (buffer-disable-undo manifest-buf)
248                       (erase-buffer manifest-buf)
249
250                       ;; Now, scan through the output buffer, looking for
251                       ;; file and directory names.
252                       (goto-char (point-min))
253                       ;; for each line ...
254                       (while (< (point) (point-max))
255                         (beginning-of-line)
256                         (setq pathname nil)
257
258                         ;; scan through the regexps, looking for a pathname
259                         (if (catch 'found-path
260                               (setq regexps package-admin-tar-filename-regexps)
261                               (while regexps
262                                 (if (looking-at (car regexps))
263                                     (progn
264                                       (setq pathname
265                                             (buffer-substring
266                                              (match-beginning 1)
267                                              (match-end 1)))
268                                       (throw 'found-path t)
269                                       ))
270                                 (setq regexps (cdr regexps))
271                                 )
272                               )
273                             (progn
274                               ;; found a pathname -- add it to the manifest
275                               ;; buffer
276                               (save-excursion
277                                 (set-buffer manifest-buf)
278                                 (goto-char (point-max))
279                                 (insert pathname "\n")
280                                 )
281                               ))
282                         (forward-line 1)
283                         )
284
285                       ;; Processed all lines.
286                       ;; Now, create the file, pkginfo/MANIFEST.<pkgname>
287
288                       ;; We use `expand-file-name' instead of `concat',
289                       ;; for portability.
290                       (setq pathname (expand-file-name "pkginfo"
291                                                        pkg-topdir))
292                       ;; Create pkginfo, if necessary
293                       (if (not (file-directory-p pathname))
294                           (make-directory pathname))
295                       (setq pathname (expand-file-name 
296                                       (concat "MANIFEST." package-name)
297                                       pathname))
298                       (save-excursion
299                         (set-buffer manifest-buf)
300                         ;; Put the files in sorted order
301                         (sort-lines nil (point-min) (point-max))
302                         ;; Write the file.
303                         ;; Note that using `write-region' *BYPASSES* any check
304                         ;; to see if XEmacs is currently editing/visiting the
305                         ;; file.
306                         (write-region (point-min) (point-max) pathname)
307                         )
308                       (kill-buffer manifest-buf)
309                       )
310                   (progn
311                     ;; We can't determine the package name from an extracted
312                     ;; file in the tar output buffer.
313                     ))
314                 ))
315           )
316       ;; Restore old case-fold-search status
317       (setq case-fold-search old-case-fold-search))
318     ))
319
320 ;;;###autoload
321 (defun package-admin-add-binary-package (file &optional pkg-dir)
322   "Install a pre-bytecompiled XEmacs package into package hierarchy."
323   (interactive "fPackage tarball: ")
324   (let ((buf (get-buffer-create package-admin-temp-buffer))
325         (status 1)
326         start err-list
327         )
328     (setq pkg-dir (package-admin-get-install-dir 'unknown pkg-dir))
329     ;; Ensure that the current directory doesn't change
330     (save-excursion
331       (set-buffer buf)
332       ;; This is not really needed
333       (setq default-directory (file-name-as-directory pkg-dir))
334       (setq case-fold-search t)
335       (buffer-disable-undo)
336       (goto-char (setq start (point-max)))
337       (if (= 0 (setq status (funcall package-admin-install-function
338                                      file pkg-dir buf)))
339           (progn
340             ;; First, check for errors.
341             ;; We can't necessarily rely upon process error codes.
342             (catch 'done
343               (goto-char start)
344               (setq err-list package-admin-error-messages)
345               (while err-list
346                 (if (re-search-forward (car err-list) nil t)
347                     (progn
348                       (setq status 1)
349                       (throw 'done nil)
350                       ))
351                 (setq err-list (cdr err-list))
352                 )
353               )
354             ;; Make sure that the MANIFEST file exists
355             (package-admin-check-manifest buf pkg-dir)
356             ))
357       )
358     status
359     ))
360
361 (defun package-admin-rmtree (directory)
362   "Delete a directory and all of its contents, recursively.
363 This is a feeble attempt at making a portable rmdir."
364   (setq directory (file-name-as-directory directory))
365   (let ((files (directory-files directory nil nil nil t))
366         (dirs (directory-files directory nil nil nil 'dirs)))
367     (while dirs
368       (if (not (member (car dirs) '("." "..")))
369           (let ((dir (expand-file-name (car dirs) directory)))
370             (condition-case err
371                 (if (file-symlink-p dir) ;; just in case, handle symlinks
372                     (delete-file dir)
373                   (package-admin-rmtree dir))
374               (file-error
375                (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err)))))
376         (setq dirs (cdr dirs))))
377     (while files
378       (condition-case err
379           (delete-file (expand-file-name (car files) directory))
380         (file-error
381          (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err))))
382       (setq files (cdr files)))
383     (condition-case err
384         (delete-directory directory)
385       (file-error
386        (message "%s: %s: \"%s\"" (nth 1 err) (nth 2 err) (nth 3 err))))))
387
388 (defun package-admin-get-lispdir  (pkg-topdir package)
389   (let (package-lispdir)
390     (if (and (setq package-lispdir (expand-file-name "lisp" pkg-topdir))
391              (setq package-lispdir (expand-file-name (symbol-name package)
392                                                      package-lispdir))
393              (file-accessible-directory-p package-lispdir))
394         package-lispdir)
395     ))
396
397 (defun package-admin-delete-binary-package (package pkg-topdir)
398   "Delete a binary installation of PACKAGE below directory PKG-TOPDIR.
399 PACKAGE is a symbol, not a string."
400   (let ( (tmpbuf " *pkg-manifest*") manifest-file package-lispdir dirs file)
401     (setq pkg-topdir (package-admin-get-install-dir package pkg-topdir))
402     (setq manifest-file (package-admin-get-manifest-file pkg-topdir package))
403     (if (file-exists-p manifest-file)
404         (progn
405           ;; The manifest file exists!  Use it to delete the old distribution.
406           (message "Removing old files for package \"%s\" ..." package)
407           (sit-for 0)
408           (setq tmpbuf (get-buffer-create tmpbuf))
409           (with-current-buffer tmpbuf
410             (buffer-disable-undo)
411             (erase-buffer)
412             (insert-file-contents manifest-file)
413             (goto-char (point-min))
414
415             ;; For each entry in the MANIFEST ...
416             (while (< (point) (point-max))
417               (beginning-of-line)
418               (setq file (expand-file-name (buffer-substring
419                                             (point)
420                                             (point-at-eol))
421                                            pkg-topdir))
422               (if (file-directory-p file)
423                   ;; Keep a record of each directory
424                   (setq dirs (cons file dirs))
425                   ;; Delete each file.
426                   ;; Make sure that the file is writable.
427                   ;; (This is important under MS Windows.)
428                   ;; I do not know why it important under MS Windows but
429                   ;;    1. It bombs out out when the file does not exist. This can be condition-cased
430                   ;;    2. If I removed the write permissions, I do not want XEmacs to just ignore them.
431                   ;;       If it wants to, XEmacs may ask, but that is about all
432                   ;; (set-file-modes file 438) ;; 438 -> #o666
433                   ;; Note, user might have removed the file!
434                 (condition-case ()
435                     (delete-file file)
436                   (error nil)))         ;; We may want to turn the error into a Warning?   
437               (forward-line 1))
438               
439             ;; Delete empty directories.
440             (if dirs
441                 (let ( (orig-default-directory default-directory)
442                        directory files file )
443                   ;; Make sure we preserve the existing `default-directory'.
444                   ;; JV, why does this change the default directory? Does it indeed?
445                   (unwind-protect
446                       (progn
447                         ;; Warning: destructive sort!
448                         (setq dirs (nreverse (sort dirs 'string<)))
449 ;                       ;; For each directory ...
450 ;                       (while dirs
451 ;                         (setq directory (file-name-as-directory (car dirs)))
452 ;                         (setq files (directory-files directory))
453 ;                         ;; Delete the directory if it's empty.
454 ;                         (if (catch 'done
455 ;                               (while files
456 ;                                 (setq file (car files))
457 ;                                 (if (and (not (string= file "."))
458 ;                                          (not (string= file "..")))
459 ;                                     (throw 'done nil))
460 ;                                 (setq files (cdr files))
461 ;                                 )
462 ;                               t)
463 ;                             (
464 ;                             (delete-directory directory))
465 ;                         (setq dirs (cdr dirs))
466 ;                         )
467                         ;; JV, On all OS's that I know of delete-directory fails on
468                         ;; on non-empty dirs anyway
469                         (mapc
470                            (lambda (dir)
471                              (condition-case ()
472                                  (delete-directory dir)))
473                            dirs))                       
474                     (setq default-directory orig-default-directory)
475                     )))
476             )
477           (kill-buffer tmpbuf)
478           ;; Delete the MANIFEST file
479           ;; (set-file-modes manifest-file 438) ;; 438 -> #o666
480           ;; Note. Packages can have MANIFEST in MANIFEST.
481           (condition-case ()
482               (delete-file manifest-file)
483             (error nil)) ;; Do warning?
484           (message "Removing old files for package \"%s\" ... done" package))
485         ;; The manifest file doesn't exist.  Fallback to just deleting the
486         ;; package-specific lisp directory, if it exists.
487         ;;
488         ;; Delete old lisp directory, if any
489         ;; Gads, this is ugly.  However, we're not supposed to use `concat'
490         ;; in the name of portability.
491         (when (setq package-lispdir (package-admin-get-lispdir pkg-topdir
492                                                              package))
493               (message "Removing old lisp directory \"%s\" ..."
494                        package-lispdir)
495               (sit-for 0)
496               (package-admin-rmtree package-lispdir)
497               (message "Removing old lisp directory \"%s\" ... done"
498                        package-lispdir)
499               ))        
500     ;; Delete the package from the database of installed packages.
501     (package-delete-name package)))
502
503 (provide 'package-admin)
504
505 ;;; package-admin.el ends here