XEmacs 21.2-b3
[chise/xemacs-chise.git.1] / lisp / package-get.el
1 ;;; package-get.el --- Retrieve XEmacs package
2
3 ;; Copyright (C) 1998 by Pete Ware
4
5 ;; Author: Pete Ware <ware@cis.ohio-state.edu>
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 ;; package-get -
30 ;;      Retrieve a package and any other required packages from an archive
31 ;;
32 ;; The idea:
33 ;;      A new XEmacs lisp-only release is generated with the following steps:
34 ;;      1. The maintainer runs some yet to be written program that
35 ;;         generates all the dependency information.  This should
36 ;;         determine all the require and provide statements and associate
37 ;;         them with a package.
38 ;;      2. All the packages are then bundled into their own tar balls
39 ;;         (or whatever format)
40 ;;      3. Maintainer automatically generates a new `package-get-base'
41 ;;         data structure which contains information such as the
42 ;;         package name, the file to be retrieved, an md5 checksum,
43 ;;         etc (see `package-get-base').
44 ;;      4. The maintainer posts an announcement with the new version
45 ;;         of `package-get-base'.
46 ;;      5. A user/system manager saves this posting and runs
47 ;;         `package-get-update' which uses the previously saved list
48 ;;         of packages, `package-get-here' that the user/site
49 ;;         wants to determine what new versions to download and
50 ;;         install.
51 ;;
52 ;;      A user/site manager can generate a new `package-get-here' structure
53 ;;      by using `package-get-setup' which generates a customize like
54 ;;      interface to the list of packages.  The buffer looks something
55 ;;      like:
56 ;;
57 ;;      gnus    - a mail and news reader
58 ;;      []      Always install
59 ;;      []      Needs updating
60 ;;      []      Required by other [packages]
61 ;;      version: 2.0
62 ;;
63 ;;      vm      - a mail reader
64 ;;      []      Always install
65 ;;      []      Needs updating
66 ;;      []      Required by other [packages]    
67 ;;
68 ;;      Where `[]' indicates a toggle box
69 ;;
70 ;;      - Clicking on "Always install" puts this into
71 ;;        `package-get-here' list.  "Needs updating" indicates a new
72 ;;        version is available.  Anything already in
73 ;;        `package-get-here' has this enabled.
74 ;;      - "Required by other" means some other packages are going to force
75 ;;        this to be installed.  Clicking on  [packages] gives a list
76 ;;        of packages that require this.
77 ;;      
78 ;;      The `package-get-base' should be installed in a file in
79 ;;      `data-directory'.  The `package-get-here' should be installed in
80 ;;      site-lisp.  Both are then read at run time.
81 ;;
82 ;; TODO:
83 ;;      - Implement `package-get-setup'
84 ;;      - Actually put `package-get-base' and `package-get-here' into
85 ;;        files that are read.
86 ;;      - Allow users to have their own packages that they want installed
87 ;;        in ~/.xemacs/.
88 ;;      - SOMEONE needs to write the programs that generate the
89 ;;        provides/requires database and makes it into a lisp data
90 ;;        structure suitable for `package-get-base'
91 ;;      - Handle errors such as no package providing a required symbol.
92 ;;      - Tie this into the `require' function to download packages
93 ;;        transparently.
94
95 ;;; Change Log
96
97 ;;; Code:
98
99 (require 'package-admin)
100 ;; (require 'package-get-base)
101
102 (defgroup package-tools nil
103   "Tools to manipulate packages."
104   :group 'emacs)
105
106 (defgroup package-get nil
107   "Automatic Package Fetcher and Installer."
108   :prefix "package-get"
109   :group 'package-tools)
110   
111 (defvar package-get-base nil
112   "List of packages that are installed at this site.
113 For each element in the alist,  car is the package name and the cdr is
114 a plist containing information about the package.   Typical fields
115 kept in the plist are:
116
117 version         - version of this package
118 provides        - list of symbols provided
119 requires        - list of symbols that are required.
120                   These in turn are provided by other packages.
121 filename        - name of the file.
122 size            - size of the file (aka the bundled package)
123 md5sum          - computed md5 checksum
124 description     - What this package is for.
125 type            - Whether this is a 'binary (default) or 'single file package
126
127 More fields may be added as needed.  An example:
128
129 '(
130  (name
131   (version \"<version 2>\"
132    file \"filename\"
133    description \"what this package is about.\"
134    provides (<list>)
135    requires (<list>)
136    size <integer-bytes>
137    md5sum \"<checksum\"
138    type single
139    )
140   (version \"<version 1>\"
141    file \"filename\"
142    description \"what this package is about.\"
143    provides (<list>)
144    requires (<list>)
145    size <integer-bytes>
146    md5sum \"<checksum\"
147    type single
148    )
149    ...
150    ))
151
152 For version information, it is assumed things are listed in most
153 recent to least recent -- in other words, the version names don't have to
154 be lexically ordered.  It is debatable if it makes sense to have more than
155 one version of a package available.")
156
157 (defcustom package-get-dir (temp-directory)
158   "*Where to store temporary files for staging."
159   :tag "Temporary directory"
160   :type 'directory
161   :group 'package-get)
162
163 ;; JV Any Custom expert know to get "Host" and "Dir" for the remote option
164 (defcustom package-get-remote
165   '(("ftp.xemacs.org" "/pub/xemacs/packages"))
166   "*List of remote sites to contact for downloading packages.
167 List format is '(site-name directory-on-site).  Each site is tried in
168 order until the package is found.  As a special case, `site-name' can be
169 `nil', in which case `directory-on-site' is treated as a local directory."
170   :tag "Package repository"
171   :type '(repeat (choice (list :tag "Local" (const :tag "Local" nil) directory )
172                          (list :tag "Remote" string string) ))
173   :group 'package-get)
174
175 (defcustom package-get-remove-copy nil
176   "*After copying and installing a package, if this is T, then remove the
177 copy.  Otherwise, keep it around."
178   :type 'boolean
179   :group 'package-get)
180
181 (defcustom package-get-base-filename
182   "/ftp.xemacs.org:/pub/xemacs/packages/package-index.LATEST"
183   "*Name of the default package database file, usually on ftp.xemacs.org."
184   :type 'file
185   :group 'package-get)
186
187 ;;;###autoload
188 (defun package-get-require-base ()
189   "Require that a package-get database has been loaded."
190   (when (or (not (boundp 'package-get-base))
191             (not package-get-base))
192     (package-get-update-base))
193   (when (or (not (boundp 'package-get-base))
194             (not package-get-base))
195     (error "Package-get database not loaded")))
196
197 (defconst package-get-pgp-signed-begin-line "^-----BEGIN PGP SIGNED MESSAGE-----"
198   "Text for start of PGP signed messages.")
199 (defconst package-get-pgp-signature-begin-line "^-----BEGIN PGP SIGNATURE-----"
200   "Text for beginning of PGP signature.")
201 (defconst package-get-pgp-signature-end-line "^-----END PGP SIGNATURE-----"
202   "Text for end of PGP signature.")
203
204 ;;;###autoload
205 (defun package-get-update-base-entry (entry)
206   "Update an entry in `package-get-base'."
207   (let ((existing (assoc (car entry) package-get-base)))
208     (if existing
209         (setcdr existing (cdr entry))
210       (setq package-get-base (cons entry package-get-base)))))
211
212 ;;;###autoload
213 (defun package-get-update-base (&optional db-file)
214   "Update the package-get database file with entries from DB-FILE."
215   (interactive (list
216                 (read-file-name "Load package-get database: "
217                                 (file-name-directory package-get-base-filename)
218                                 package-get-base-filename
219                                 t
220                                 (file-name-nondirectory package-get-base-filename))))
221   (setq db-file (expand-file-name (or db-file package-get-base-filename)))
222   (if (not (file-exists-p db-file))
223       (error "Package-get database file `%s' does not exist" db-file))
224   (if (not (file-readable-p db-file))
225       (error "Package-get database file `%s' not readable" db-file))
226   (let ((buf (get-buffer-create "*package database*")))
227     (unwind-protect
228         (save-excursion
229           (set-buffer buf)
230           (erase-buffer buf)
231           (insert-file-contents-internal db-file)
232           (package-get-update-base-from-buffer buf))
233       (kill-buffer buf))))
234
235 ;;;###autoload
236 (defun package-get-update-base-from-buffer (&optional buf)
237   "Update the package-get database with entries from BUFFER.
238 BUFFER defaults to the current buffer.  This command can be
239 used interactively, for example from a mail or news buffer."
240   (interactive)
241   (setq buf (or buf (current-buffer)))
242   (let (content-beg content-end beg end)
243     (save-excursion
244       (set-buffer buf)
245       (goto-char (point-min))
246       (setq content-beg (point))
247       (setq content-end (save-excursion (goto-char (point-max)) (point)))
248       (when (re-search-forward package-get-pgp-signed-begin-line nil t)
249         (setq beg (match-beginning 0))
250         (setq content-beg (match-end 0)))
251       (when (re-search-forward package-get-pgp-signature-begin-line nil t)
252         (setq content-end (match-beginning 0)))
253       (when (re-search-forward package-get-pgp-signature-end-line nil t)
254         (setq end (point)))
255       (if (not (and content-beg content-end beg end))
256           (or (yes-or-no-p "Package-get entries not PGP signed, continue? ")
257               (error "Package-get database not updated")))
258       (if (and content-beg content-end beg end)
259           (if (not (condition-case nil
260                        (or (fboundp 'mc-pgp-verify-region)
261                            (load-library "mc-pgp")
262                            (fboundp 'mc-pgp-verify-region))
263                      (error nil)))
264               (or (yes-or-no-p
265                    "No mailcrypt; can't verify package-get DB signature, continue? ")
266                   (error "Package-get database not updated"))))
267       (if (and beg end
268                (fboundp 'mc-pgp-verify-region)
269                (or (not
270                     (condition-case err
271                         (mc-pgp-verify-region beg end)
272                       (file-error
273                        (and (string-match "No such file" (nth 2 err))
274                             (yes-or-no-p
275                              "Can't find PGP, continue without package-get DB verification? ")))
276                       (t nil)))))
277           (error "Package-get PGP signature failed to verify"))
278       (package-get-update-base-entries content-beg content-end)
279       (message "Updated package-get database"))))
280
281 (defun package-get-update-base-entries (beg end)
282   "Update the package-get database with the entries found between
283 BEG and END in the current buffer."
284   (save-excursion
285     (goto-char beg)
286     (if (not (re-search-forward "^(package-get-update-base-entry" nil t))
287         (error "Buffer does not contain package-get database entries"))
288     (beginning-of-line)
289     (let ((count 0))
290       (while (and (< (point) end)
291                   (re-search-forward "^(package-get-update-base-entry" nil t))
292         (beginning-of-line)
293         (let ((entry (read (current-buffer))))
294           (if (or (not (consp entry))
295                   (not (eq (car entry) 'package-get-update-base-entry)))
296               (error "Invalid package-get database entry found"))
297           (package-get-update-base-entry
298            (car (cdr (car (cdr entry)))))
299           (setq count (1+ count))))
300       (message "Got %d package-get database entries" count))))
301
302 (defun package-get-interactive-package-query (get-version package-symbol)
303   "Perform interactive querying for package and optional version.
304 Query for a version if GET-VERSION is non-nil.  Return package name as
305 a symbol instead of a string if PACKAGE-SYMBOL is non-nil.
306 The return value is suitable for direct passing to `interactive'."
307   (package-get-require-base)
308   (let ( (table (mapcar '(lambda (item)
309                            (let ( (name (symbol-name (car item))) )
310                              (cons name name)
311                              ))
312                         package-get-base)) 
313          package package-symbol default-version version)
314     (save-window-excursion
315       (setq package (completing-read "Package: " table nil t))
316       (setq package-symbol (intern package))
317       (if get-version
318           (progn
319             (setq default-version 
320                   (package-get-info-prop 
321                    (package-get-info-version
322                     (package-get-info-find-package package-get-base
323                                                    package-symbol) nil)
324                    'version))
325             (while (string=
326                     (setq version (read-string "Version: " default-version))
327                     "")
328               )
329             (if package-symbol
330                 (list package-symbol version)
331               (list package version))
332             )
333         (if package-symbol
334             (list package-symbol)
335           (list package)))
336       )))
337
338 ;;;###autoload
339 (defun package-get-delete-package (package &optional pkg-topdir)
340   "Delete an installation of PACKAGE below directory PKG-TOPDIR.
341 PACKAGE is a symbol, not a string.
342 This is just an interactive wrapper for `package-admin-delete-binary-package'."
343   (interactive (package-get-interactive-package-query nil t))
344   (package-admin-delete-binary-package package pkg-topdir))
345
346 ;;;###autoload
347 (defun package-get-update-all ()
348   "Fetch and install the latest versions of all currently installed packages."
349   (interactive)
350   (package-get-require-base)
351   ;; Load a fresh copy
352   (catch 'exit
353     (mapcar (lambda (pkg)
354               (if (not (package-get (car pkg) nil 'never))
355                   (throw 'exit nil)             ;; Bail out if error detected
356                   ))
357             packages-package-list)))
358
359 ;;;###autoload
360 (defun package-get-all (package version &optional fetched-packages install-dir)
361   "Fetch PACKAGE with VERSION and all other required packages.
362 Uses `package-get-base' to determine just what is required and what
363 package provides that functionality.  If VERSION is nil, retrieves
364 latest version.  Optional argument FETCHED-PACKAGES is used to keep
365 track of packages already fetched.  Optional argument INSTALL-DIR,
366 if non-nil, specifies the package directory where fetched packages
367 should be installed.
368
369 Returns nil upon error."
370   (interactive (package-get-interactive-package-query t nil))
371   (let* ((the-package (package-get-info-find-package package-get-base
372                                                      package))
373          (this-package (package-get-info-version
374                         the-package version))
375          (this-requires (package-get-info-prop this-package 'requires))
376          )
377     (catch 'exit
378       (setq version (package-get-info-prop this-package 'version))
379       (unless (package-get-installedp package version)
380         (if (not (package-get package version nil install-dir))
381             (progn
382               (setq fetched-packages nil)
383               (throw 'exit nil))))
384       (setq fetched-packages
385             (append (list package)
386                     (package-get-info-prop this-package 'provides)
387                     fetched-packages))
388       ;; grab everything that this package requires plus recursively
389       ;; grab everything that the requires require.  Keep track
390       ;; in `fetched-packages' the list of things provided -- this
391       ;; keeps us from going into a loop
392       (while this-requires
393         (if (not (member (car this-requires) fetched-packages))
394             (let* ((reqd-package (package-get-package-provider
395                                   (car this-requires)))
396                    (reqd-version (cadr reqd-package))
397                    (reqd-name (car reqd-package)))
398               (if (null reqd-name)
399                   (error "Unable to find a provider for %s"
400                          (car this-requires)))
401               (if (not (setq fetched-packages
402                              (package-get-all reqd-name reqd-version
403                                               fetched-packages
404                                               install-dir)))
405                   (throw 'exit nil)))
406           )
407         (setq this-requires (cdr this-requires)))
408       )
409     fetched-packages
410     ))
411
412 ;;;###autoload
413 (defun package-get-dependencies (packages)
414   "Compute dependencies for PACKAGES.
415 Uses `package-get-base' to determine just what is required and what
416 package provides that functionality.  Returns the list of packages
417 required by PACKAGES."
418   (package-get-require-base)
419   (let ((orig-packages packages)
420         dependencies provided)
421     (while packages
422       (let* ((package (car packages))
423              (the-package (package-get-info-find-package
424                            package-get-base package))
425              (this-package (package-get-info-version
426                             the-package nil))
427              (this-requires (package-get-info-prop this-package 'requires))
428              (new-depends   (set-difference
429                              (mapcar
430                               #'(lambda (reqd)
431                                   (let* ((reqd-package (package-get-package-provider reqd))
432                                          (reqd-version (cadr reqd-package))
433                                          (reqd-name    (car reqd-package)))
434                                     (if (null reqd-name)
435                                         (error "Unable to find a provider for %s" reqd))
436                                     reqd-name))
437                               this-requires)
438                              dependencies))
439              (this-provides (package-get-info-prop this-package 'provides)))
440         (setq dependencies
441               (union dependencies new-depends))
442         (setq provided
443               (union provided (union (list package) this-provides)))
444         (setq packages
445               (union new-depends (cdr packages)))))
446     (set-difference dependencies orig-packages)))
447
448 (defun package-get-load-package-file (lispdir file)
449   (let (pathname)
450     (setq pathname (expand-file-name file lispdir))
451     (condition-case err
452         (progn
453           (load pathname t)
454           t)
455       (t
456        (message "Error loading package file \"%s\" %s!" pathname err)
457        nil))
458     ))
459
460 (defun package-get-init-package (lispdir)
461   "Initialize the package.
462 This really assumes that the package has never been loaded.  Updating
463 a newer package can cause problems, due to old, obsolete functions in
464 the old package.
465
466 Return `t' upon complete success, `nil' if any errors occurred."
467   (progn
468     (if (and lispdir
469              (file-accessible-directory-p lispdir))
470         (progn
471           ;; Add lispdir to load-path if it doesn't already exist.
472           ;; NOTE: this does not take symlinks, etc., into account.
473           (if (let ( (dirs load-path) )
474                 (catch 'done
475                   (while dirs
476                     (if (string-equal (car dirs) lispdir)
477                         (throw 'done nil))
478                     (setq dirs (cdr dirs))
479                     )
480                   t))
481               (setq load-path (cons lispdir load-path)))
482           (if (not (package-get-load-package-file lispdir "auto-autoloads"))
483               (package-get-load-package-file lispdir "_pkg"))
484           t)
485       nil)
486     ))
487
488 ;;;###autoload
489 (defun package-get (package &optional version conflict install-dir)
490   "Fetch PACKAGE from remote site.
491 Optional arguments VERSION indicates which version to retrieve, nil
492 means most recent version.  CONFLICT indicates what happens if the
493 package is already installed.  Valid values for CONFLICT are:
494 'always always retrieve the package even if it is already installed
495 'never  do not retrieve the package if it is installed.
496 INSTALL-DIR, if non-nil, specifies the package directory where
497 fetched packages should be installed.
498
499 The value of `package-get-base' is used to determine what files should 
500 be retrieved.  The value of `package-get-remote' is used to determine
501 where a package should be retrieved from.  The sites are tried in
502 order so one is better off listing easily reached sites first.
503
504 Once the package is retrieved, its md5 checksum is computed.  If that
505 sum does not match that stored in `package-get-base' for this version
506 of the package, an error is signalled.
507
508 Returns `t' upon success, the symbol `error' if the package was
509 successfully installed but errors occurred during initialization, or
510 `nil' upon error."
511   (interactive (package-get-interactive-package-query nil t))
512   (let* ((this-package
513           (package-get-info-version
514            (package-get-info-find-package package-get-base
515                                           package) version))
516          (this-requires (package-get-info-prop this-package 'requires))
517          (found nil)
518          (search-dirs package-get-remote)
519          (base-filename (package-get-info-prop this-package 'filename))
520          (package-status t)
521          filenames full-package-filename)
522     (if (null this-package)
523         (error "Couldn't find package %s with version %s"
524                package version))
525     (if (null base-filename)
526         (error "No filename associated with package %s, version %s"
527                package version))
528     (setq install-dir
529           (package-admin-get-install-dir package install-dir
530                 (or (eq package 'mule-base) (memq 'mule-base this-requires))))
531
532     ;; Contrive a list of possible package filenames.
533     ;; Ugly.  Is there a better way to do this?
534     (setq filenames (cons base-filename nil))
535     (if (string-match "^\\(..*\\)\.tar\.gz$" base-filename)
536         (setq filenames (append filenames
537                                 (list (concat (match-string 1 base-filename)
538                                               ".tgz")))))
539
540     (setq version (package-get-info-prop this-package 'version))
541     (unless (and (eq conflict 'never)
542                  (package-get-installedp package version))
543       ;; Find the package from the search list in package-get-remote
544       ;; and copy it into the staging directory.  Then validate
545       ;; the checksum.  Finally, install the package.
546       (catch 'done
547         (let (search-filenames current-dir-entry host dir current-filename
548                                dest-filename)
549           ;; In each search directory ...
550           (while search-dirs
551             (setq current-dir-entry (car search-dirs)
552                   host (car current-dir-entry)
553                   dir (car (cdr current-dir-entry))
554                   search-filenames filenames
555                   )
556
557             ;; Look for one of the possible package filenames ...
558             (while search-filenames
559               (setq current-filename (car search-filenames)
560                     dest-filename (package-get-staging-dir current-filename))
561               (cond
562                ;; No host means look on the current system.
563                ( (null host)
564                  (setq full-package-filename
565                        (substitute-in-file-name
566                         (expand-file-name current-filename
567                                           (file-name-as-directory dir))))
568                  )
569
570                ;; If it's already on the disk locally, and the size is
571                ;; greater than zero ...
572                ( (and (file-exists-p dest-filename)
573                       (let (attrs)
574                         ;; file-attributes could return -1 for LARGE files,
575                         ;; but, hopefully, packages won't be that large.
576                         (and (setq attrs (file-attributes dest-filename))
577                              (> (nth 7 attrs) 0))))
578                  (setq full-package-filename dest-filename)
579                  )
580
581                ;; If the file exists on the remote system ...
582                ( (file-exists-p (package-get-remote-filename
583                                  current-dir-entry current-filename))
584                  ;; Get it
585                  (setq full-package-filename dest-filename)
586                  (message "Retrieving package `%s' ..." 
587                           current-filename)
588                  (sit-for 0)
589                  (copy-file (package-get-remote-filename current-dir-entry
590                                                          current-filename)
591                             full-package-filename t)
592                  )
593                )
594
595               ;; If we found it, we're done.
596               (if (and full-package-filename
597                        (file-exists-p full-package-filename))
598                   (throw 'done nil))
599               ;; Didn't find it.  Try the next possible filename.
600               (setq search-filenames (cdr search-filenames))
601               )
602             ;; Try looking in the next possible directory ...
603             (setq search-dirs (cdr search-dirs))
604             )
605           ))
606
607       (if (or (not full-package-filename)
608               (not (file-exists-p full-package-filename)))
609           (error "Unable to find file %s" base-filename))
610       ;; Validate the md5 checksum
611       ;; Doing it with XEmacs removes the need for an external md5 program
612       (message "Validating checksum for `%s'..." package) (sit-for 0)
613       (with-temp-buffer
614         ;; What ever happened to i-f-c-literally
615         (let (file-name-handler-alist)
616           (insert-file-contents-internal full-package-filename))
617         (if (not (string= (md5 (current-buffer))
618                           (package-get-info-prop this-package
619                                                  'md5sum)))
620             (error "Package %s does not match md5 checksum" base-filename)))
621
622       (package-admin-delete-binary-package package install-dir)
623
624       (message "Installing package `%s' ..." package) (sit-for 0)
625       (let ((status
626              (package-admin-add-binary-package full-package-filename
627                                                install-dir)))
628         (if (= status 0)
629             (progn
630               ;; clear messages so that only messages from
631               ;; package-get-init-package are seen, below.
632               (clear-message)
633               (if (package-get-init-package (package-admin-get-lispdir
634                                              install-dir package))
635                   (progn
636                     (message "Added package `%s'" package)
637                     (sit-for 0)
638                     )
639                 (progn
640                   ;; display message only if there isn't already one.
641                   (if (not (current-message))
642                       (progn
643                         (message "Added package `%s' (errors occurred)"
644                                  package)
645                         (sit-for 0)
646                         ))
647                   (if package-status
648                       (setq package-status 'errors))
649                   ))
650               )
651           (message "Installation of package %s failed." base-filename)
652           (sit-for 0)
653           (switch-to-buffer package-admin-temp-buffer)
654           (setq package-status nil)
655           ))
656       (setq found t))
657     (if (and found package-get-remove-copy)
658         (delete-file full-package-filename))
659     package-status
660     ))
661
662 (defun package-get-info-find-package (which name)
663   "Look in WHICH for the package called NAME and return all the info
664 associated with it.  See `package-get-base' for info on the format
665 returned.
666
667  To access fields returned from this, use
668 `package-get-info-version' to return information about particular a
669 version.  Use `package-get-info-find-prop' to find particular property 
670 from a version returned by `package-get-info-version'."
671   (interactive "xPackage list: \nsPackage Name: ")
672   (if which
673       (if (eq (caar which) name)
674           (cdar which)
675         (if (cdr which)
676             (package-get-info-find-package (cdr which) name)))))
677
678 (defun package-get-info-version (package version)
679   "In PACKAGE, return the plist associated with a particular VERSION of the
680   package.  PACKAGE is typically as returned by
681   `package-get-info-find-package'.  If VERSION is nil, then return the 
682   first (aka most recent) version.  Use `package-get-info-find-prop'
683   to retrieve a particular property from the value returned by this."
684   (interactive (package-get-interactive-package-query t t))
685   (while (and version package (not (string= (plist-get (car package) 'version) version)))
686     (setq package (cdr package)))
687   (if package (car package)))
688
689 (defun package-get-info-prop (package-version property)
690   "In PACKAGE-VERSION, return the value associated with PROPERTY.
691 PACKAGE-VERSION is typically returned by `package-get-info-version'
692 and PROPERTY is typically (although not limited to) one of the
693 following:
694
695 version         - version of this package
696 provides                - list of symbols provided
697 requires                - list of symbols that are required.
698                   These in turn are provided by other packages.
699 size            - size of the bundled package
700 md5sum          - computed md5 checksum"
701   (interactive "xPackage Version: \nSProperty")
702   (plist-get package-version property))
703
704 (defun package-get-info-version-prop (package-list package version property)
705   "In PACKAGE-LIST, search for PACKAGE with this VERSION and return
706   PROPERTY value."
707   (package-get-info-prop
708    (package-get-info-version
709     (package-get-info-find-package package-list package) version) property))
710
711 (defun package-get-set-version-prop (package-list package version
712                                                   property value)
713   "A utility to make it easier to add a VALUE for a specific PROPERTY
714   in this VERSION of a specific PACKAGE kept in the PACKAGE-LIST.
715 Returns the modified PACKAGE-LIST.  Any missing fields are created."
716   )
717
718 (defun package-get-staging-dir (filename)
719   "Return a good place to stash FILENAME when it is retrieved.
720 Use `package-get-dir' for directory to store stuff.
721 Creates `package-get-dir'  it it doesn't exist."
722   (interactive "FPackage filename: ")
723   (if (not (file-exists-p package-get-dir))
724       (make-directory package-get-dir))
725   (expand-file-name
726    (file-name-nondirectory (or (and (fboundp 'efs-ftp-path)
727                                     (nth 2 (efs-ftp-path filename)))
728                                filename))
729    (file-name-as-directory package-get-dir)))
730
731 (defun package-get-remote-filename (search filename)
732   "Return FILENAME as a remote filename.
733 It first checks if FILENAME already is a remote filename.  If it is
734 not, then it uses the (car search) as the remote site-name and the (cadr
735 search) as the remote-directory and concatenates filename.  In other
736 words
737         site-name:remote-directory/filename
738 "
739   (if (efs-ftp-path filename)
740       filename
741     (let ((dir (cadr search)))
742       (concat "/"
743               (car search) ":"
744               (if (string-match "/$" dir)
745                   dir
746                 (concat dir "/"))
747               filename))))
748
749
750 (defun package-get-installedp (package version)
751   "Determine if PACKAGE with VERSION has already been installed.
752 I'm not sure if I want to do this by searching directories or checking 
753 some built in variables.  For now, use packages-package-list."
754   ;; Use packages-package-list which contains name and version
755   (equal (plist-get
756           (package-get-info-find-package packages-package-list
757                                          package) ':version)
758          (if (floatp version) version (string-to-number version))))
759
760 ;;;###autoload
761 (defun package-get-package-provider (sym)
762   "Search for a package that provides SYM and return the name and
763   version.  Searches in `package-get-base' for SYM.   If SYM is a
764   consp, then it must match a corresponding (provide (SYM VERSION)) from 
765   the package."
766   (interactive "SSymbol: ")
767   (package-get-require-base)
768   (let ((packages package-get-base)
769         (done nil)
770         (found nil))
771     (while (and (not done) packages)
772       (let* ((this-name (caar packages))
773              (this-package (cdr (car packages)))) ;strip off package name
774         (while (and (not done) this-package)
775           (if (or (eq this-name sym)
776                   (eq (cons this-name
777                             (package-get-info-prop (car this-package) 'version))
778                       sym)
779                   (member sym (package-get-info-prop (car this-package) 'provides)))
780               (progn (setq done t)
781                      (setq found (list (caar packages)
782                                        (package-get-info-prop (car this-package) 'version))))
783             (setq this-package (cdr this-package)))))
784       (setq packages (cdr packages)))
785     found))
786
787 ;;
788 ;; customize interfaces.
789 ;; The group is in this file so that custom loads includes this file.
790 ;;
791 (defgroup packages nil
792   "Configure XEmacs packages."
793   :group 'emacs)
794
795 ;;;###autoload
796 (defun package-get-custom ()
797   "Fetch and install the latest versions of all customized packages."
798   (interactive)
799   (package-get-require-base)
800   ;; Load a fresh copy
801   (load "package-get-custom.el")
802   (mapcar (lambda (pkg)
803             (if (eval (intern (concat (symbol-name (car pkg)) "-package")))
804                 (package-get-all (car pkg) nil))
805             t)
806           package-get-base))
807
808 (defun package-get-ever-installed-p (pkg &optional notused)
809   (string-match "-package$" (symbol-name pkg))
810   (custom-initialize-set 
811    pkg 
812    (if (package-get-info-find-package 
813         packages-package-list 
814         (intern (substring (symbol-name pkg) 0 (match-beginning 0))))
815        t)))
816
817 (defun package-get-file-installed-p (file &optional paths)
818   "Return absolute-path of FILE if FILE exists in PATHS.
819 If PATHS is omitted, `load-path' is used."
820   (if (null paths)
821       (setq paths load-path)
822     )
823   (catch 'tag
824     (let (path)
825       (while paths
826         (setq path (expand-file-name file (car paths)))
827         (if (file-exists-p path)
828             (throw 'tag path)
829           )
830         (setq paths (cdr paths))
831         ))))
832
833 (defun package-get-create-custom ()
834   "Creates a package customization file package-get-custom.el.
835 Entries in the customization file are retrieved from package-get-base.el."
836   (interactive)
837   ;; Load a fresh copy
838   (let ((custom-buffer (find-file-noselect 
839                         (or (package-get-file-installed-p 
840                              "package-get-custom.el")
841                             (expand-file-name
842                              "package-get-custom.el"
843                              (file-name-directory 
844                               (package-get-file-installed-p 
845                                "package-get-base.el"))
846                              ))))
847         (pkg-groups nil))
848
849     ;; clear existing stuff
850     (delete-region (point-min custom-buffer) 
851                    (point-max custom-buffer) custom-buffer)
852     (insert-string "(require 'package-get)\n" custom-buffer)
853
854     (mapcar (lambda (pkg)
855               (let ((category (plist-get (car (cdr pkg)) 'category)))
856                 (or (memq (intern category) pkg-groups)
857                     (progn
858                       (setq pkg-groups (cons (intern category) pkg-groups))
859                       (insert-string 
860                        (concat "(defgroup " category "-packages nil\n"
861                                "  \"" category " package group\"\n"
862                                "  :group 'packages)\n\n") custom-buffer)))
863                 
864                 (insert-string 
865                  (concat "(defcustom " (symbol-name (car pkg)) 
866                          "-package nil \n"
867                          "  \"" (plist-get (car (cdr pkg)) 'description) "\"\n"
868                          "  :group '" category "-packages\n"
869                          "  :initialize 'package-get-ever-installed-p\n"
870                          "  :type 'boolean)\n\n") custom-buffer)))
871             package-get-base) custom-buffer)
872   )
873
874 ;; need this first to avoid infinite dependency loops
875 (provide 'package-get)
876
877 ;; potentially update the custom dependencies every time we load this
878 (when nil ;; #### disable for now... -gk
879 (unless noninteractive
880 (let ((custom-file (package-get-file-installed-p "package-get-custom.el"))
881       (package-file (package-get-file-installed-p "package-get-base.el")))
882   ;; update custom file if it doesn't exist
883   (if (or (not custom-file)
884           (and (< (car (nth 5 (file-attributes custom-file)))
885                   (car (nth 5 (file-attributes package-file))))
886                (< (car (nth 5 (file-attributes custom-file)))
887                   (car (nth 5 (file-attributes package-file))))))
888       (save-excursion
889         (message "generating package customizations...")
890         (set-buffer (package-get-create-custom))
891         (save-buffer)
892         (message "generating package customizations...done")))
893   (load "package-get-custom.el")))
894 )
895
896 ;;; package-get.el ends here