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