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