4222b78fbca4559fc4a92f62face0b02088cd6f1
[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 ;; Copyright (C) 2002 Ben Wing.
5 ;; Copyright (C) 2003, Steve Youngs
6
7 ;; Author: Pete Ware <ware@cis.ohio-state.edu>
8 ;; Heavy-Modifications: Greg Klanderman <greg@alphatech.com>
9 ;;                      Jan Vroonhof    <vroonhof@math.ethz.ch>
10 ;;                      Steve Youngs    <youngs@xemacs.org>
11 ;; Keywords: internal
12
13 ;; This file is part of XEmacs.
14
15 ;; XEmacs is free software; you can redistribute it and/or modify it
16 ;; under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; XEmacs is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 ;; General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
27 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 ;; 02111-1307, USA.
29
30 ;;; Synched up with: Not in FSF
31
32 ;;; Commentary:
33
34 ;; package-get -
35 ;;      Retrieve a package and any other required packages from an archive
36 ;;
37 ;;
38 ;; Note (JV): Most of this no longer applies!
39 ;;
40 ;; The idea:
41 ;;      A new XEmacs lisp-only release is generated with the following steps:
42 ;;      1. The maintainer runs some yet to be written program that
43 ;;         generates all the dependency information.  This should
44 ;;         determine all the require and provide statements and associate
45 ;;         them with a package.
46 ;;      2. All the packages are then bundled into their own tar balls
47 ;;         (or whatever format)
48 ;;      3. Maintainer automatically generates a new `package-get-base'
49 ;;         data structure which contains information such as the
50 ;;         package name, the file to be retrieved, an md5 checksum,
51 ;;         etc (see `package-get-base').
52 ;;      4. The maintainer posts an announcement with the new version
53 ;;         of `package-get-base'.
54 ;;      5. A user/system manager saves this posting and runs
55 ;;         `package-get-update' which uses the previously saved list
56 ;;         of packages, `package-get-here' that the user/site
57 ;;         wants to determine what new versions to download and
58 ;;         install.
59 ;;
60 ;;      A user/site manager can generate a new `package-get-here' structure
61 ;;      by using `package-get-setup' which generates a customize like
62 ;;      interface to the list of packages.  The buffer looks something
63 ;;      like:
64 ;;
65 ;;      gnus    - a mail and news reader
66 ;;      []      Always install
67 ;;      []      Needs updating
68 ;;      []      Required by other [packages]
69 ;;      version: 2.0
70 ;;
71 ;;      vm      - a mail reader
72 ;;      []      Always install
73 ;;      []      Needs updating
74 ;;      []      Required by other [packages]
75 ;;
76 ;;      Where `[]' indicates a toggle box
77 ;;
78 ;;      - Clicking on "Always install" puts this into
79 ;;        `package-get-here' list.  "Needs updating" indicates a new
80 ;;        version is available.  Anything already in
81 ;;        `package-get-here' has this enabled.
82 ;;      - "Required by other" means some other packages are going to force
83 ;;        this to be installed.  Clicking on  [packages] gives a list
84 ;;        of packages that require this.
85 ;;
86 ;;      The `package-get-base' should be installed in a file in
87 ;;      `data-directory'.  The `package-get-here' should be installed in
88 ;;      site-lisp.  Both are then read at run time.
89 ;;
90 ;; TODO:
91 ;;      - Implement `package-get-setup'
92 ;;      - Actually put `package-get-base' and `package-get-here' into
93 ;;        files that are read.
94 ;;      - Allow users to have their own packages that they want installed
95 ;;        in ~/.xemacs/.
96 ;;      - SOMEONE needs to write the programs that generate the
97 ;;        provides/requires database and makes it into a lisp data
98 ;;        structure suitable for `package-get-base'
99 ;;      - Handle errors such as no package providing a required symbol.
100 ;;      - Tie this into the `require' function to download packages
101 ;;        transparently.
102
103 ;;; Change Log
104
105 ;;; Code:
106
107 (require 'package-admin)
108 ;; (require 'package-get-base)
109
110 (defgroup package-tools nil
111   "Tools to manipulate packages."
112   :group 'emacs)
113
114 (defgroup package-get nil
115   "Automatic Package Fetcher and Installer."
116   :prefix "package-get"
117   :group 'package-tools)
118
119 ;;;###autoload
120 (defvar package-get-base nil
121   "List of packages that are installed at this site.
122 For each element in the alist,  car is the package name and the cdr is
123 a plist containing information about the package.   Typical fields
124 kept in the plist are:
125
126 version         - version of this package
127 provides        - list of symbols provided
128 requires        - list of symbols that are required.
129                   These in turn are provided by other packages.
130 filename        - name of the file.
131 size            - size of the file (aka the bundled package)
132 md5sum          - computed md5 checksum
133 description     - What this package is for.
134 type            - Whether this is a 'binary (default) or 'single file package
135
136 More fields may be added as needed.  An example:
137
138 '(
139  (name
140   (version \"<version 2>\"
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   (version \"<version 1>\"
150    file \"filename\"
151    description \"what this package is about.\"
152    provides (<list>)
153    requires (<list>)
154    size <integer-bytes>
155    md5sum \"<checksum\"
156    type single
157    )
158    ...
159    ))
160
161 For version information, it is assumed things are listed in most
162 recent to least recent -- in other words, the version names don't have to
163 be lexically ordered.  It is debatable if it makes sense to have more than
164 one version of a package available.")
165
166 (defcustom package-get-dir (temp-directory)
167   "*Where to store temporary files for staging."
168   :tag "Temporary directory"
169   :type 'directory
170   :group 'package-get)
171
172 ;;;###autoload
173 (defcustom package-get-package-index-file-location 
174   (or (getenv "EMACSPACKAGEPATH")
175       user-init-directory)
176   "*The directory where the package-index file can be found."
177   :type 'directory
178   :group 'package-get)
179
180 ;;;###autoload
181 (defcustom package-get-install-to-user-init-directory nil
182   "*If non-nil install packages under `user-init-directory'."
183   :type 'boolean
184   :group 'package-get)
185
186 (define-widget 'host-name 'string
187   "A Host name."
188   :tag "Host")
189
190 (defcustom package-get-remote nil
191   "*The remote site to contact for downloading packages.
192 Format is '(site-name directory-on-site).  As a special case, `site-name'
193 can be `nil', in which case `directory-on-site' is treated as a local
194 directory."
195   :tag "Package repository"
196   :type '(set (choice (const :tag "None" nil)
197                       (list :tag "Local" (const :tag "Local" nil) directory)
198                       (list :tag "Remote" host-name directory)))
199   :group 'package-get)
200
201 ;;;###autoload
202 (defcustom package-get-download-sites
203   '(
204     ;; Main XEmacs Site (ftp.xemacs.org)
205     ("US (Main XEmacs Site)"
206      "ftp.xemacs.org" "pub/xemacs/packages")
207     ;; In alphabetical order of Country, our mirrors...
208     ("Australia (aarnet.edu.au)" "mirror.aarnet.edu.au" "pub/xemacs/packages")
209     ("Australia (au.xemacs.org)" "ftp.au.xemacs.org" "pub/xemacs/packages")
210     ("Austria (at.xemacs.org)" "ftp.at.xemacs.org" "editors/xemacs/packages")
211     ("Belgium (be.xemacs.org)" "ftp.be.xemacs.org" "xemacs/packages")
212     ("Brazil (br.xemacs.org)" "ftp.br.xemacs.org" "pub/xemacs/packages")
213     ("Canada (ca.xemacs.org)" "ftp.ca.xemacs.org" "pub/Mirror/xemacs/packages")
214     ("Canada (crc.ca)" "ftp.crc.ca" "pub/packages/editors/xemacs/packages")
215     ("Canada (ualberta.ca)" "sunsite.ualberta.ca" "pub/Mirror/xemacs/packages")
216     ("Czech Republic (cz.xemacs.org)" "ftp.cz.xemacs.org" "MIRRORS/ftp.xemacs.org/pub/xemacs/packages")
217     ("Denmark (dk.xemacs.org)" "ftp.dk.xemacs.org" "pub/emacs/xemacs/packages")
218     ("Finland (fi.xemacs.org)" "ftp.fi.xemacs.org" "pub/mirrors/ftp.xemacs.org/pub/tux/xemacs/packages")
219     ("France (fr.xemacs.org)" "ftp.fr.xemacs.org" "pub/xemacs/packages")
220     ("France (mirror.cict.fr)" "mirror.cict.fr" "xemacs/packages")
221     ("France (pasteur.fr)" "ftp.pasteur.fr" "pub/computing/xemacs/packages")
222     ("Germany (de.xemacs.org)" "ftp.de.xemacs.org" "pub/ftp.xemacs.org/tux/xemacs/packages")
223     ("Germany (tu-darmstadt.de)" "ftp.tu-darmstadt.de" "pub/editors/xemacs/packages")
224     ("Ireland (ie.xemacs.org)" "ftp.ie.xemacs.org" "mirrors/ftp.xemacs.org/pub/xemacs/packages")
225     ("Italy (it.xemacs.org)" "ftp.it.xemacs.org" "unix/packages/XEMACS/packages")
226     ("Japan (aist.go.jp)" "ring.aist.go.jp" "pub/text/xemacs/packages")
227     ("Japan (asahi-net.or.jp)" "ring.asahi-net.or.jp" "pub/text/xemacs/packages")
228     ("Japan (dti.ad.jp)" "ftp.dti.ad.jp" "pub/unix/editor/xemacs/packages")
229     ("Japan (jaist.ac.jp)" "ftp.jaist.ac.jp" "pub/GNU/xemacs/packages")
230     ("Japan (jp.xemacs.org)" "ftp.jp.xemacs.org" "pub/GNU/xemacs/packages")
231     ("Japan (nucba.ac.jp)" "mirror.nucba.ac.jp" "mirror/xemacs/packages")
232     ("Japan (sut.ac.jp)" "sunsite.sut.ac.jp" "pub/archives/packages/xemacs/packages")
233     ("Korea (kr.xemacs.org)" "ftp.kr.xemacs.org" "pub/tools/emacs/xemacs/packages")
234     ("New Zealand (nz.xemacs.org)" "ftp.nz.xemacs.org" "mirror/ftp.xemacs.org/packages")
235     ("Norway (no.xemacs.org)" "ftp.no.xemacs.org" "pub/xemacs/packages")
236     ("Poland (pl.xemacs.org)" "ftp.pl.xemacs.org" "pub/unix/editors/xemacs/packages")
237     ("Russia (ru.xemacs.org)" "ftp.ru.xemacs.org" "pub/xemacs/packages")
238     ("Slovakia (sk.xemacs.org)" "ftp.sk.xemacs.org" "pub/mirrors/xemacs/packages")
239     ("South Africa (za.xemacs.org)" "ftp.za.xemacs.org" "mirrorsites/ftp.xemacs.org/packages")
240     ("Sweden (se.xemacs.org)" "ftp.se.xemacs.org" "pub/gnu/xemacs/packages")
241     ("Switzerland (ch.xemacs.org)" "ftp.ch.xemacs.org" "mirror/xemacs/packages")
242     ("UK (uk.xemacs.org)" "ftp.uk.xemacs.org" "sites/ftp.xemacs.org/pub/xemacs/packages")
243     ("US (ibiblio.org)" "ibiblio.org" "pub/packages/editors/xemacs/packages")
244     ("US (stealth.net)" "ftp.stealth.net" "pub/mirrors/ftp.xemacs.org/pub/xemacs/packages")
245     ("US (unc.edu)" "metalab.unc.edu" "pub/packages/editors/xemacs/packages")
246     ("US (us.xemacs.org)" "ftp.us.xemacs.org" "pub/xemacs/packages")
247     ("US (utk.edu)" "ftp.sunsite.utk.edu" "pub/xemacs/packages")
248     )
249   "*List of remote sites available for downloading packages.
250 List format is '(site-description site-name directory-on-site).
251 SITE-DESCRIPTION is a textual description of the site.  SITE-NAME
252 is the internet address of the download site.  DIRECTORY-ON-SITE
253 is the directory on the site in which packages may be found.
254 This variable is used to initialize `package-get-remote', the
255 variable actually used to specify package download sites."
256   :tag "Package download sites"
257   :type '(repeat (list (string :tag "Name") host-name directory))
258   :group 'package-get)
259
260 ;;;###autoload
261 (defcustom package-get-pre-release-download-sites
262   '(
263     ;; Main XEmacs Site (ftp.xemacs.org)
264     ("Pre-Releases (Main XEmacs Site)" "ftp.xemacs.org"
265      "pub/xemacs/beta/experimental/packages")
266     ;; In alphabetical order of Country, our mirrors...
267     ("Australia Pre-Releases (aarnet.edu.au)" "mirror.aarnet.edu.au"
268      "pub/xemacs/beta/experimental/packages")
269     ("Australia Pre-Releases (au.xemacs.org)" "ftp.au.xemacs.org"
270      "pub/xemacs/beta/experimental/packages")
271     ("Austria Pre-Releases (at.xemacs.org)" "ftp.at.xemacs.org"
272      "editors/xemacs/beta/experimentsl/packages")
273     ("Brazil Pre-Releases (br.xemacs.org)" "ftp.br.xemacs.org"
274      "pub/xemacs/xemacs-21.5/experimental/packages")
275     ("Canada Pre-Releases (ca.xemacs.org)" "ftp.ca.xemacs.org"
276      "pub/Mirror/xemacs/beta/experimental/packages")
277     ("Canada Pre-Releases (crc.ca)" "ftp.crc.ca"
278      "pub/packages/editors/xemacs/beta/experimental/packages")
279     ("Canada Pre-Releases (ualberta.ca)" "sunsite.ualberta.ca"
280      "pub/Mirror/xemacs/beta/experimental/packages")
281     ("Czech Republic Pre-Releases (cz.xemacs.org)" "ftp.cz.xemacs.org"
282      "MIRRORS/ftp.xemacs.org/pub/xemacs/xemacs-21.5/experimental/packages")
283     ("Denmark Pre-Releases (dk.xemacs.org)" "ftp.dk.xemacs.org"
284      "pub/emacs/xemacs/beta/experimental/packages")
285     ("Finland Pre-Releases (fi.xemacs.org)" "ftp.fi.xemacs.org"
286      "pub/mirrors/ftp.xemacs.org/pub/tux/xemacs/beta/experimental/packages")
287     ("France Pre-Releases (fr.xemacs.org)" "ftp.fr.xemacs.org"
288      "pub/xemacs/beta/experimental/packages")
289     ("France Pre-Releases (mirror.cict.fr)" "mirror.cict.fr"
290      "xemacs/beta/experimental/packages")
291     ("France Pre-Releases (pasteur.fr)" "ftp.pasteur.fr"
292      "pub/computing/xemacs/beta/experimental/packages")
293     ("Germany Pre-Releases (de.xemacs.org)" "ftp.de.xemacs.org"
294      "pub/ftp.xemacs.org/tux/xemacs/beta/experimental/packages")
295     ("Germany Pre-Releases (tu-darmstadt.de)" "ftp.tu-darmstadt.de"
296      "pub/editors/xemacs/beta/experimental/packages")
297     ("Ireland Pre-Releases (ie.xemacs.org)" "ftp.ie.xemacs.org"
298      "mirrors/ftp.xemacs.org/pub/xemacs/beta/experimental/packages")
299     ("Italy Pre-Releases (it.xemacs.org)" "ftp.it.xemacs.org"
300      "unix/packages/XEMACS/beta/experimental/packages")
301     ("Japan Pre-Releases (aist.go.jp)" "ring.aist.go.jp"
302      "pub/text/xemacs/beta/experimental/packages")
303     ("Japan Pre-Releases (asahi-net.or.jp)" "ring.asahi-net.or.jp"
304      "pub/text/xemacs/beta/experimental/packages")
305     ("Japan Pre-Releases (dti.ad.jp)" "ftp.dti.ad.jp"
306      "pub/unix/editor/xemacs/beta/experimental/packages")
307     ("Japan Pre-Releases (jaist.ac.jp)" "ftp.jaist.ac.jp"
308      "pub/GNU/xemacs/beta/experimental/packages")
309     ("Japan Pre-Releases (jp.xemacs.org)" "ftp.jp.xemacs.org"
310      "pub/GNU/xemacs/beta/experimental/packages")
311     ("Japan Pre-Releases (sut.ac.jp)" "sunsite.sut.ac.jp"
312      "pub/archives/packages/xemacs/xemacs-21.5/experimental/packages")
313     ("New Zealand Pre-Releases (nz.xemacs.org)" "ftp.nz.xemacs.org" "mirror/ftp.xemacs.org/packages")
314     ("Norway Pre-Releases (no.xemacs.org)" "ftp.no.xemacs.org"
315      "pub/xemacs/beta/experimental/packages")
316     ("Poland Pre-Releases (pl.xemacs.org)" "ftp.pl.xemacs.org"
317      "pub/unix/editors/xemacs/beta/experimental/packages")
318     ("Russia Pre-Releases (ru.xemacs.org)" "ftp.ru.xemacs.org"
319      "pub/xemacs/beta/experimental/packages")
320     ("Saudi Arabia Pre-Releases (sa.xemacs.org)" "ftp.sa.xemacs.org"
321      "pub/mirrors/ftp.xemacs.org/xemacs/xemacs-21.5/experimental/packages")
322     ("Slovakia Pre-Releases (sk.xemacs.org)" "ftp.sk.xemacs.org"
323      "pub/mirrors/xemacs/beta/experimental/packages")
324     ("South Africa Pre-Releases (za.xemacs.org)" "ftp.za.xemacs.org"
325      "mirrorsites/ftp.xemacs.org/beta/experimental/packages")
326     ("Sweden Pre-Releases (se.xemacs.org)" "ftp.se.xemacs.org"
327      "pub/gnu/xemacs/beta/experimental/packages")
328     ("Switzerland Pre-Releases (ch.xemacs.org)" "ftp.ch.xemacs.org"
329      "mirror/xemacs/beta/experimental/packages")
330     ("UK Pre-Releases (uk.xemacs.org)" "ftp.uk.xemacs.org"
331      "sites/ftp.xemacs.org/pub/xemacs/beta/experimental/packages")
332     ("US Pre-Releases (ibiblio.org)" "ibiblio.org"
333      "pub/packages/editors/xemacs/beta/experimental/packages")
334     ("US Pre-Releases (stealth.net)" "ftp.stealth.net"
335      "pub/mirrors/ftp.xemacs.org/pub/xemacs/beta/experimental/packages")
336     ("US Pre-Releases (unc.edu)" "metalab.unc.edu"
337      "pub/packages/editors/xemacs/beta/experimental/packages")
338     ("US Pre-Releases (us.xemacs.org)" "ftp.us.xemacs.org"
339      "pub/xemacs/beta/experimental/packages")
340     ("US Pre-Releases (utk.edu)" "ftp.sunsite.utk.edu"
341      "pub/xemacs/beta/experimental/packages"))
342   "*List of remote sites available for downloading \"Pre-Release\" packages.
343 List format is '(site-description site-name directory-on-site).
344 SITE-DESCRIPTION is a textual description of the site.  SITE-NAME
345 is the internet address of the download site.  DIRECTORY-ON-SITE
346 is the directory on the site in which packages may be found.
347 This variable is used to initialize `package-get-remote', the
348 variable actually used to specify package download sites."
349   :tag "Pre-Release Package download sites"
350   :type '(repeat (list (string :tag "Name") host-name directory))
351   :group 'package-get)
352
353 ;;;###autoload
354 (defcustom package-get-site-release-download-sites
355   nil
356   "*List of remote sites available for downloading \"Site Release\" packages.
357 List format is '(site-description site-name directory-on-site).
358 SITE-DESCRIPTION is a textual description of the site.  SITE-NAME
359 is the internet address of the download site.  DIRECTORY-ON-SITE
360 is the directory on the site in which packages may be found.
361 This variable is used to initialize `package-get-remote', the
362 variable actually used to specify package download sites."
363   :tag "Site Release Package download sites"
364   :type '(repeat (list (string :tag "Name") host-name directory))
365   :group 'package-get)
366
367 (defcustom package-get-remove-copy t
368   "*After copying and installing a package, if this is t, then remove the
369 copy.  Otherwise, keep it around."
370   :type 'boolean
371   :group 'package-get)
372
373 ;; #### it may make sense for this to be a list of names.
374 ;; #### also, should we rename "*base*" to "*index*" or "*db*"?
375 ;;      "base" is a pretty poor name.
376 (defcustom package-get-base-filename "package-index.LATEST.gpg"
377   "*Name of the default package-get database file.
378 This may either be a relative path, in which case it is interpreted
379 with respect to `package-get-remote', or an absolute path."
380   :type 'file
381   :group 'package-get)
382
383 (defcustom package-get-always-update nil
384   "*If Non-nil always make sure we are using the latest package index (base).
385 Otherwise respect the `force-current' argument of `package-get-require-base'."
386   :type 'boolean
387   :group 'package-get)
388
389 (defun package-get-pgp-available-p ()
390   "Checks the availability of Mailcrypt and PGP executable.
391
392 Returns t if both are found, nil otherwise.  As a side effect, set
393 `mc-default-scheme' dependent on the PGP executable found."
394   (let (result)
395     (when (featurep 'mailcrypt-autoloads)
396       (autoload 'mc-setversion "mc-setversion"))
397     (when (fboundp 'mc-setversion)
398       (cond ((locate-file "gpg" exec-path
399                           '("" ".btm" ".bat" ".cmd" ".exe" ".com")
400                           'executable)
401              (mc-setversion "gpg")
402              (setq result t))
403             ((locate-file "pgpe" exec-path
404                           '("" ".btm" ".bat" ".cmd" ".exe" ".com")
405                           'executable)
406              (mc-setversion "5.0")
407              (setq result t))
408             ((locate-file "pgp" exec-path
409                           '("" ".btm" ".bat" ".cmd" ".exe" ".com")
410                           'executable)
411              (mc-setversion "2.6")
412              (setq result t))))
413     (if result
414         result
415       nil)))
416
417 (defcustom package-get-require-signed-base-updates (package-get-pgp-available-p)
418   "*If non-nil, try to verify the package index database via PGP.
419
420 If nil, no PGP verification is done.  If the package index database
421 entries are not PGP signed and this variable is non-nil, require user
422 confirmation to continue with the package-get procedure.
423
424 The default for this variable is the return value of
425 `package-get-pgp-available-p', non-nil if both the \"Mailcrypt\"
426 package and a suitable PGP executable are available, nil otherwise."
427   :type 'boolean
428   :group 'package-get)
429
430 (defvar package-entries-are-signed nil
431   "Non-nil when the package index file has been PGP signed.")
432
433 (defvar package-get-continue-update-base nil
434   "Non-nil update the index even if it hasn't been signed.")
435
436 (defvar package-get-was-current nil
437   "Non-nil we did our best to fetch a current database.")
438
439 ;;;###autoload
440 (defun package-get-require-base (&optional force-current)
441   "Require that a package-get database has been loaded.
442 If the optional FORCE-CURRENT argument or the value of
443 `package-get-always-update' is Non-nil, try to update the database
444 from a location in `package-get-remote'. Otherwise a local copy is used
445 if available and remote access is never done.
446
447 Please use FORCE-CURRENT only when the user is explictly dealing with packages
448 and remote access is likely in the near future."
449   (setq force-current (or force-current package-get-always-update))
450   (unless (and (boundp 'package-get-base)
451                package-get-base
452                (or (not force-current) package-get-was-current))
453     (package-get-update-base nil force-current))
454   (if (or (not (boundp 'package-get-base))
455           (not package-get-base))
456       (error 'void-variable
457              "Package-get database not loaded")
458     (setq package-get-was-current force-current)))
459
460 (defconst package-get-pgp-signed-begin-line "^-----BEGIN PGP SIGNED MESSAGE-----"
461   "Text for start of PGP signed messages.")
462 (defconst package-get-pgp-signature-begin-line "^-----BEGIN PGP SIGNATURE-----"
463   "Text for beginning of PGP signature.")
464 (defconst package-get-pgp-signature-end-line "^-----END PGP SIGNATURE-----"
465   "Text for end of PGP signature.")
466
467 ;;;###autoload
468 (defun package-get-update-base-entry (entry)
469   "Update an entry in `package-get-base'."
470   (let ((existing (assq (car entry) package-get-base)))
471     (if existing
472         (setcdr existing (cdr entry))
473       (setq package-get-base (cons entry package-get-base)))))
474
475 (defun package-get-locate-file (file &optional nil-if-not-found no-remote)
476   "Locate an existing FILE with respect to `package-get-remote'.
477 If FILE is an absolute path or is not found, simply return FILE.
478 If optional argument NIL-IF-NOT-FOUND is non-nil, return nil
479 if FILE can not be located.
480 If NO-REMOTE is non-nil never search remote locations."
481   (if (file-name-absolute-p file)
482       file
483     (let ((site package-get-remote)
484           (expanded nil))
485       (when site
486         (unless (and no-remote (caar (list site)))
487           (let ((expn (package-get-remote-filename (car (list site)) file)))
488             (if (and expn (file-exists-p expn))
489                 (setq site nil
490                       expanded expn)))))
491       (or expanded
492           (and (not nil-if-not-found)
493                file)))))
494
495 (defun package-get-locate-index-file (no-remote)
496   "Locate the package-get index file.  
497
498 Do not return remote paths if NO-REMOTE is non-nil.  If the index
499 file doesn't exist in `package-get-package-index-file-location', ask
500 the user if one should be created using the index file in core as a
501 template."
502   (or (package-get-locate-file package-get-base-filename t no-remote)
503       (if (file-exists-p (expand-file-name package-get-base-filename
504                                            package-get-package-index-file-location))
505           (expand-file-name package-get-base-filename
506                             package-get-package-index-file-location)
507         (if (y-or-n-p (format "No index file, shall I create one in %s? "
508                               package-get-package-index-file-location))
509             (progn
510               (save-excursion
511                 (set-buffer 
512                  (find-file-noselect (expand-file-name
513                                       package-get-base-filename
514                                       package-get-package-index-file-location)))
515                 (let ((coding-system-for-write 'binary))
516                   (erase-buffer)
517                   (insert-file-contents-literally
518                    (locate-data-file package-get-base-filename))
519                   (save-buffer (current-buffer))
520                   (kill-buffer (current-buffer))))
521               (expand-file-name package-get-base-filename
522                                 package-get-package-index-file-location))
523           (error 'search-failed
524                  "Can't locate a package index file.")))))
525
526 (defun package-get-maybe-save-index (filename)
527   "Offer to save the current buffer as the local package index file,
528 if different."
529   (let ((location (package-get-locate-index-file t)))
530     (unless (and filename (equal filename location))
531       (unless (and location
532                    (equal (md5 (current-buffer))
533                           (with-temp-buffer
534                             (insert-file-contents-literally location)
535                             (md5 (current-buffer)))))
536         (when (not (file-writable-p location))
537           (if (y-or-n-p (format "Sorry, %s is read-only, can I use %s? "
538                                 location user-init-directory))
539               (setq location (expand-file-name
540                               package-get-base-filename
541                               package-get-package-index-file-location))
542             (error 'file-error
543                    (format "%s is read-only" location))))
544         (when (y-or-n-p (concat "Update package index in " location "? "))
545           (let ((coding-system-for-write 'binary))
546             (write-file location)))))))
547
548 ;;;###autoload
549 (defun package-get-update-base (&optional db-file force-current)
550   "Update the package-get database file with entries from DB-FILE.
551 Unless FORCE-CURRENT is non-nil never try to update the database."
552   (interactive
553    (let ((dflt (package-get-locate-index-file nil)))
554      (list (read-file-name "Load package-get database: "
555                            (file-name-directory dflt)
556                            dflt
557                            t
558                            (file-name-nondirectory dflt)))))
559   (setq db-file (expand-file-name (or db-file
560                                       (package-get-locate-index-file
561                                          (not force-current)))))
562   (if (not (file-exists-p db-file))
563       (error 'file-error
564              (format "Package-get database file `%s' does not exist" db-file)))
565   (if (not (file-readable-p db-file))
566       (error 'file-error
567              (format "Package-get database file `%s' not readable" db-file)))
568   (let ((buf (get-buffer-create "*package database*")))
569     (unwind-protect
570         (save-excursion
571           (set-buffer buf)
572           (erase-buffer buf)
573           (insert-file-contents-literally db-file)
574           (package-get-update-base-from-buffer buf)
575           (if (file-remote-p db-file)
576               (package-get-maybe-save-index db-file)))
577       (kill-buffer buf))))
578
579 ;;;###autoload
580 (defun package-get-update-base-from-buffer (&optional buf)
581   "Update the package-get database with entries from BUFFER.
582 BUFFER defaults to the current buffer.  This command can be
583 used interactively, for example from a mail or news buffer."
584   (interactive)
585   (setq buf (or buf (current-buffer)))
586   (let (content-beg content-end)
587     (save-excursion
588       (set-buffer buf)
589       (goto-char (point-min))
590       (setq content-beg (point))
591       (setq content-end (save-excursion (goto-char (point-max)) (point)))
592       (when (re-search-forward package-get-pgp-signed-begin-line nil t)
593         (setq content-beg (match-end 0)))
594       (when (re-search-forward package-get-pgp-signature-begin-line nil t)
595         (setq content-end (match-beginning 0))
596         (setq package-entries-are-signed t))
597       (re-search-forward package-get-pgp-signature-end-line nil t)
598       (setq package-get-continue-update-base t)
599       ;; This is a little overkill because the default value of
600       ;; `package-get-require-signed-base-updates' is the return of
601       ;; `package-get-pgp-available-p', but we have to allow for
602       ;; someone explicitly setting
603       ;; `package-get-require-signed-base-updates' to t. --SY
604       (when (and package-get-require-signed-base-updates
605                  (package-get-pgp-available-p))
606         (if package-entries-are-signed
607             (let (good-sig)
608               (setq package-get-continue-update-base nil)
609               (autoload 'mc-verify "mc-toplev")
610               (when (mc-verify)
611                 (setq good-sig t))
612               (if good-sig
613                   (setq package-get-continue-update-base t)
614                 (error 'process-error 
615                        "GnuPG error.  Package database not updated")))
616           (if (yes-or-no-p
617                "Package Index is not PGP signed.  Continue anyway? ")
618               (setq package-get-continue-update-base t)
619             (setq package-get-continue-update-base nil)
620             (warn "Package database not updated"))))
621       ;; ToDo: We should call package-get-maybe-save-index on the region
622       (when package-get-continue-update-base
623         (package-get-update-base-entries content-beg content-end)
624         (message "Updated package database")))))
625
626 (defun package-get-update-base-entries (start end)
627   "Update the package-get database with the entries found between
628 START and END in the current buffer."
629   (save-excursion
630     (goto-char start)
631     (if (not (re-search-forward "^(package-get-update-base-entry" nil t))
632         (error 'search-failed
633                "Buffer does not contain package-get database entries"))
634     (beginning-of-line)
635     (let ((count 0))
636       (while (and (< (point) end)
637                   (re-search-forward "^(package-get-update-base-entry" nil t))
638         (beginning-of-line)
639         (let ((entry (read (current-buffer))))
640           (if (or (not (consp entry))
641                   (not (eq (car entry) 'package-get-update-base-entry)))
642               (error 'syntax-error
643                      "Invalid package-get database entry found"))
644           (package-get-update-base-entry
645            (car (cdr (car (cdr entry)))))
646           (setq count (1+ count))))
647       (message "Got %d package-get database entries" count))))
648
649 ;;;###autoload
650 (defun package-get-save-base (file)
651   "Write the package-get database to FILE.
652
653 Note: This database will be unsigned of course."
654   (interactive "FSave package-get database to: ")
655   (package-get-require-base t)
656   (let ((buf (get-buffer-create "*package database*")))
657     (unwind-protect
658         (save-excursion
659           (set-buffer buf)
660           (erase-buffer buf)
661           (goto-char (point-min))
662           (let ((entries package-get-base) entry plist)
663             (insert ";; Package Index file -- Do not edit manually.\n")
664             (insert ";;;@@@\n")
665             (while entries
666               (setq entry (car entries))
667               (setq plist (car (cdr entry)))
668               (insert "(package-get-update-base-entry (quote\n")
669               (insert (format "(%s\n" (symbol-name (car entry))))
670               (while plist
671                 (insert (format "  %s%s %S\n"
672                                 (if (eq plist (car (cdr entry))) "(" " ")
673                                 (symbol-name (car plist))
674                                 (car (cdr plist))))
675                 (setq plist (cdr (cdr plist))))
676               (insert "))\n))\n;;;@@@\n")
677               (setq entries (cdr entries))))
678           (insert ";; Package Index file ends here\n")
679           (write-region (point-min) (point-max) file))
680       (kill-buffer buf))))
681
682 (defun package-get-interactive-package-query (get-version package-symbol)
683   "Perform interactive querying for package and optional version.
684 Query for a version if GET-VERSION is non-nil.  Return package name as
685 a symbol instead of a string if PACKAGE-SYMBOL is non-nil.
686 The return value is suitable for direct passing to `interactive'."
687   (package-get-require-base t)
688   (let ((table (mapcar #'(lambda (item)
689                            (let ((name (symbol-name (car item))))
690                              (cons name name)))
691                        package-get-base))
692         package package-symbol default-version version)
693     (save-window-excursion
694       (setq package (completing-read "Package: " table nil t))
695       (setq package-symbol (intern package))
696       (if get-version
697           (progn
698             (setq default-version
699                   (package-get-info-prop
700                    (package-get-info-version
701                     (package-get-info-find-package package-get-base
702                                                    package-symbol) nil)
703                    'version))
704             (while (string=
705                     (setq version (read-string "Version: " default-version))
706                     ""))
707             (if package-symbol
708                 (list package-symbol version)
709               (list package version)))
710         (if package-symbol
711             (list package-symbol)
712           (list package))))))
713
714 ;;;###autoload
715 (defun package-get-delete-package (package &optional pkg-topdir)
716   "Delete an installation of PACKAGE below directory PKG-TOPDIR.
717 PACKAGE is a symbol, not a string.
718 This is just an interactive wrapper for `package-admin-delete-binary-package'."
719   (interactive (package-get-interactive-package-query nil t))
720   (package-admin-delete-binary-package package pkg-topdir))
721
722 ;;;###autoload
723 (defun package-get-update-all ()
724   "Fetch and install the latest versions of all currently installed packages."
725   (interactive)
726   (package-get-require-base t)
727   ;; Load a fresh copy
728   (catch 'exit
729     (mapcar (lambda (pkg)
730               (if (not (package-get (car pkg) nil 'never))
731                   (throw 'exit nil)))           ;; Bail out if error detected
732             packages-package-list))
733   (package-net-update-installed-db))
734
735 ;;;###autoload
736 (defun package-get-all (package version &optional fetched-packages install-dir)
737   "Fetch PACKAGE with VERSION and all other required packages.
738 Uses `package-get-base' to determine just what is required and what
739 package provides that functionality.  If VERSION is nil, retrieves
740 latest version.  Optional argument FETCHED-PACKAGES is used to keep
741 track of packages already fetched.  Optional argument INSTALL-DIR,
742 if non-nil, specifies the package directory where fetched packages
743 should be installed.
744
745 Returns nil upon error."
746   (interactive (package-get-interactive-package-query t nil))
747   (let* ((the-package (package-get-info-find-package package-get-base
748                                                      package))
749          (this-package (package-get-info-version
750                         the-package version))
751          (this-requires (package-get-info-prop this-package 'requires)))
752     (catch 'exit
753       (setq version (package-get-info-prop this-package 'version))
754       (unless (package-get-installedp package version)
755         (if (not (package-get package version nil install-dir))
756             (progn
757               (setq fetched-packages nil)
758               (throw 'exit nil))))
759       (setq fetched-packages
760             (append (list package)
761                     (package-get-info-prop this-package 'provides)
762                     fetched-packages))
763       ;; grab everything that this package requires plus recursively
764       ;; grab everything that the requires require.  Keep track
765       ;; in `fetched-packages' the list of things provided -- this
766       ;; keeps us from going into a loop
767       (while this-requires
768         (if (not (member (car this-requires) fetched-packages))
769             (let* ((reqd-package (package-get-package-provider
770                                   (car this-requires) t))
771                    (reqd-version (cadr reqd-package))
772                    (reqd-name (car reqd-package)))
773               (if (null reqd-name)
774                   (error 'search-failed
775                          (format "Unable to find a provider for %s"
776                                  (car this-requires))))
777               (if (not (setq fetched-packages
778                              (package-get-all reqd-name reqd-version
779                                               fetched-packages
780                                               install-dir)))
781                   (throw 'exit nil))))
782         (setq this-requires (cdr this-requires))))
783     fetched-packages))
784
785 ;;;###autoload
786 (defun package-get-dependencies (packages)
787   "Compute dependencies for PACKAGES.
788 Uses `package-get-base' to determine just what is required and what
789 package provides that functionality.  Returns the list of packages
790 required by PACKAGES."
791   (package-get-require-base t)
792   (let ((orig-packages packages)
793         dependencies provided)
794     (while packages
795       (let* ((package (car packages))
796              (the-package (package-get-info-find-package
797                            package-get-base package))
798              (this-package (package-get-info-version
799                             the-package nil))
800              (this-requires (package-get-info-prop this-package 'requires))
801              (new-depends   (set-difference
802                              (mapcar
803                               #'(lambda (reqd)
804                                   (let* ((reqd-package (package-get-package-provider reqd))
805                                          (reqd-name    (car reqd-package)))
806                                     (if (null reqd-name)
807                                         (error 'search-failed
808                                                (format "Unable to find a provider for %s" reqd)))
809                                     reqd-name))
810                               this-requires)
811                              dependencies))
812              (this-provides (package-get-info-prop this-package 'provides)))
813         (setq dependencies
814               (union dependencies new-depends))
815         (setq provided
816               (union provided (union (list package) this-provides)))
817         (setq packages
818               (union new-depends (cdr packages)))))
819     (set-difference dependencies orig-packages)))
820
821 (defun package-get-load-package-file (lispdir file)
822   (let (pathname)
823     (setq pathname (expand-file-name file lispdir))
824     (condition-case err
825         (progn
826           (load pathname t)
827           t)
828       (t
829        (message "Error loading package file \"%s\" %s!" pathname err)
830        nil))
831     ))
832
833 (defun package-get-init-package (lispdir)
834   "Initialize the package.
835 This really assumes that the package has never been loaded.  Updating
836 a newer package can cause problems, due to old, obsolete functions in
837 the old package.
838
839 Return `t' upon complete success, `nil' if any errors occurred."
840   (progn
841     (if (and lispdir
842              (file-accessible-directory-p lispdir))
843         (progn
844           ;; Add lispdir to load-path if it doesn't already exist.
845           ;; NOTE: this does not take symlinks, etc., into account.
846           (if (let ((dirs load-path))
847                 (catch 'done
848                   (while dirs
849                     (if (string-equal (car dirs) lispdir)
850                         (throw 'done nil))
851                     (setq dirs (cdr dirs)))
852                   t))
853               (setq load-path (cons lispdir load-path)))
854           (if (not (package-get-load-package-file lispdir "auto-autoloads"))
855               (package-get-load-package-file lispdir "_pkg"))
856           t)
857       nil)))
858
859 ;;;###autoload
860 (defun package-get-info (package information &optional arg remote)
861   "Get information about a package.
862
863 Quite similar to `package-get-info-prop', but can retrieve a lot more
864 information.
865
866 Argument PACKAGE is the name of an XEmacs package (a symbol).  It must
867 be a valid package, ie, a member of `package-get-base'.
868
869 Argument INFORMATION is a symbol that can be any one of:
870
871    standards-version     Package system version (not used).
872    version               Version of the XEmacs package.
873    author-version        The upstream version of the package.
874    date                  The date the package was last modified.
875    build-date            The date the package was last built.
876    maintainer            The maintainer of the package.
877    distribution          Will always be \"xemacs\" (not used).
878    priority              \"low\", \"medium\", or \"high\" (not used).
879    category              Either \"standard\", \"mule\", or \"unsupported\"..
880    dump                  Is the package dumped (not used).
881    description           A description of the package.
882    filename              The filename of the binary tarball of the package.
883    md5sum                The md5sum of filename.
884    size                  The size in bytes of filename.
885    provides              A list of symbols that this package provides.
886    requires              A list of packages that this package requires.
887    type                  Can be either \"regular\" or \"single-file\".
888
889 If optional argument ARG is non-nil insert INFORMATION into current
890 buffer at point.  This is very useful for doing things like inserting
891 a maintainer's email address into a mail buffer.
892
893 If optional argument REMOTE is non-nil use a package list from a
894 remote site.  For this to work `package-get-remote' must be non-nil.
895
896 If this function is called interactively it will display INFORMATION
897 in the minibuffer."
898   (interactive "SPackage: \nSInfo: \nP")
899     (if remote
900         (package-get-require-base t)
901       (package-get-require-base nil))
902     (let ((all-pkgs package-get-base)
903           info)
904       (loop until (equal package (caar all-pkgs))
905         do (setq all-pkgs (cdr all-pkgs))
906         do (if (not all-pkgs)
907                (error 'invalid-argument
908                       (format "%s is not a valid package" package))))
909       (setq info (plist-get (cadar all-pkgs) information))
910       (if (interactive-p)
911           (if arg
912               (insert (format "%s" info))
913             (if (package-get-key package :version)
914                 (message "%s" info)
915               (message "%s (Package: %s is not installed)" info package)))
916         (if arg
917             (insert (format "%s" info))
918           info))))
919
920 ;;;###autoload
921 (defun package-get-list-packages-where (item field &optional arg)
922   "Return a list of packages that fulfill certain criteria.
923
924 Argument ITEM, a symbol, is what you want to check for.  ITEM must be a
925 symbol even when it doesn't make sense to be a symbol \(think, searching
926 maintainers, descriptions, etc\).  The function will convert the symbol
927 to a string if a string is what is needed.  The downside to this is that
928 ITEM can only ever be a single word.
929
930 Argument FIELD, a symbol, is the field to check in.  You can specify
931 any one of:
932
933       Field            Sane or Allowable Content
934     description          any single word
935     category             `standard' or `mule'
936     maintainer           any single word
937     build-date           yyyy-mm-dd
938     date                 yyyy-mm-dd
939     type                 `regular' or `single'
940     requires             any package name
941     provides             any symbol
942     priority             `low', `medium', or `high'
943
944 Optional Argument ARG, a prefix arg, insert output at point in the
945 current buffer."
946   (interactive "SList packages that have (item): \nSin their (field): \nP")
947   (package-get-require-base nil)
948   (let ((pkgs package-get-base)
949         (strings '(description category maintainer build-date date))
950         (symbols '(type requires provides priority))
951         results)
952     (cond ((memq field strings)
953            (setq item (symbol-name item))
954            (while pkgs
955              (when (string-match item (package-get-info (caar pkgs) field))
956                (setq results (push (caar pkgs) results)))
957              (setq pkgs (cdr pkgs))))
958           ((memq field symbols)
959            (if (or (eq field 'type)
960                    (eq field 'priority))
961                (while pkgs
962                  (when (eq item (package-get-info (caar pkgs) field))
963                    (setq results (push (caar pkgs) results)))
964                  (setq pkgs (cdr pkgs)))
965              (while pkgs
966                (when (memq item (package-get-info (caar pkgs) field))
967                  (setq results (push (caar pkgs) results)))
968                (setq pkgs (cdr pkgs)))))
969           (t 
970            (error 'wrong-type-argument field)))
971     (if (interactive-p)
972         (if arg
973             (insert (format "%s" results))
974           (message "%s" results)))
975     results))
976
977 ;;;###autoload
978 (defun package-get (package &optional version conflict install-dir)
979   "Fetch PACKAGE from remote site.
980 Optional arguments VERSION indicates which version to retrieve, nil
981 means most recent version.  CONFLICT indicates what happens if the
982 package is already installed.  Valid values for CONFLICT are:
983 'always always retrieve the package even if it is already installed
984 'never  do not retrieve the package if it is installed.
985 INSTALL-DIR, if non-nil, specifies the package directory where
986 fetched packages should be installed.
987
988 The value of `package-get-base' is used to determine what files should
989 be retrieved.  The value of `package-get-remote' is used to determine
990 where a package should be retrieved from.
991
992 Once the package is retrieved, its md5 checksum is computed.  If that
993 sum does not match that stored in `package-get-base' for this version
994 of the package, an error is signalled.
995
996 Returns `t' upon success, the symbol `error' if the package was
997 successfully installed but errors occurred during initialization, or
998 `nil' upon error."
999   (interactive (package-get-interactive-package-query nil t))
1000   (catch 'skip-update
1001   (let* ((this-package
1002           (package-get-info-version
1003            (package-get-info-find-package package-get-base
1004                                           package) version))
1005          (latest (package-get-info-prop this-package 'version))
1006          (installed (package-get-key package :version))
1007          (found nil)
1008          (search-dir package-get-remote)
1009          (base-filename (package-get-info-prop this-package 'filename))
1010          (package-status t)
1011          filenames full-package-filename)
1012     (if (and (equal (package-get-info package 'category) "mule")
1013              (not (featurep 'mule)))
1014         (error 'invalid-state 
1015                "Mule packages can't be installed with a non-Mule XEmacs"))
1016     (if (null this-package)
1017         (if package-get-remote
1018             (error 'search-failed
1019                    (format "Couldn't find package %s with version %s"
1020                            package version))
1021           (error 'syntax-error
1022                  "No download site or local package location specified.")))
1023     (if (null base-filename)
1024         (error 'syntax-error
1025                (format "No filename associated with package %s, version %s"
1026                        package version)))
1027     (setq install-dir (package-admin-get-install-dir package install-dir))
1028
1029     ;; If they asked for the latest using version=nil, don't get an older
1030     ;; version than we already have.
1031     (if installed
1032         (if (> (if (stringp installed)
1033                    (string-to-number installed)
1034                  installed)
1035                (if (stringp latest)
1036                    (string-to-number latest)
1037                  latest))
1038             (if (not (null version))
1039                 (warn "Installing %s package version %s, you had a newer version %s"
1040                   package latest installed)
1041               (warn "Skipping %s package, you have a newer version %s"
1042                 package installed)
1043               (throw 'skip-update t))))
1044
1045     ;; Contrive a list of possible package filenames.
1046     ;; Ugly.  Is there a better way to do this?
1047     (setq filenames (cons base-filename nil))
1048     (if (string-match "^\\(..*\\)\.tar\.gz$" base-filename)
1049         (setq filenames (append filenames
1050                                 (list (concat (match-string 1 base-filename)
1051                                               ".tgz")))))
1052
1053     (setq version latest)
1054     (unless (and (eq conflict 'never)
1055                  (package-get-installedp package version))
1056       ;; Find the package from the search list in package-get-remote
1057       ;; and copy it into the staging directory.  Then validate
1058       ;; the checksum.  Finally, install the package.
1059       (catch 'done
1060         (let (search-filenames host dir current-filename dest-filename)
1061           ;; In each search directory ...
1062           (when search-dir
1063             (setq host (car search-dir)
1064                   dir (car (cdr search-dir))
1065                   search-filenames filenames)
1066
1067             ;; Look for one of the possible package filenames ...
1068             (while search-filenames
1069               (setq current-filename (car search-filenames)
1070                     dest-filename (package-get-staging-dir current-filename))
1071               (cond
1072                ;; No host means look on the current system.
1073                ((null host)
1074                 (setq full-package-filename
1075                       (substitute-in-file-name
1076                        (expand-file-name current-filename
1077                                          (file-name-as-directory dir)))))
1078
1079                ;; If it's already on the disk locally, and the size is
1080                ;; correct
1081                ((and (file-exists-p dest-filename)
1082                      (eq (nth 7 (file-attributes dest-filename))
1083                          (package-get-info package 'size)))
1084                  (setq full-package-filename dest-filename))
1085
1086                ;; If the file exists on the remote system ...
1087                ((file-exists-p (package-get-remote-filename
1088                                 search-dir current-filename))
1089                 ;; Get it
1090                 (setq full-package-filename dest-filename)
1091                 (message "Retrieving package `%s' ..."
1092                          current-filename)
1093                 (sit-for 0)
1094                 (copy-file (package-get-remote-filename search-dir
1095                                                         current-filename)
1096                            full-package-filename t)))
1097
1098               ;; If we found it, we're done.
1099               (if (and full-package-filename
1100                        (file-exists-p full-package-filename))
1101                   (throw 'done nil))
1102               ;; Didn't find it.  Try the next possible filename.
1103               (setq search-filenames (cdr search-filenames))))))
1104
1105       (if (or (not full-package-filename)
1106               (not (file-exists-p full-package-filename)))
1107           (if package-get-remote
1108               (error 'search-failed
1109                      (format "Unable to find file %s" base-filename))
1110             (error 'syntax-error
1111                    "No download sites or local package locations specified.")))
1112       ;; Validate the md5 checksum
1113       ;; Doing it with XEmacs removes the need for an external md5 program
1114       (message "Validating checksum for `%s'..." package) (sit-for 0)
1115       (with-temp-buffer
1116         (insert-file-contents-literally full-package-filename)
1117         (if (not (string= (md5 (current-buffer))
1118                           (package-get-info-prop this-package
1119                                                  'md5sum)))
1120             (progn
1121               (delete-file full-package-filename)
1122               (error 'process-error
1123                      (format "Package %s does not match md5 checksum %s has been deleted"
1124                              base-filename full-package-filename)))))
1125
1126       (package-admin-delete-binary-package package install-dir)
1127
1128       (message "Installing package `%s' ..." package) (sit-for 0)
1129       (let ((status
1130              (package-admin-add-binary-package full-package-filename
1131                                                install-dir)))
1132         (if (= status 0)
1133             (progn
1134               ;; clear messages so that only messages from
1135               ;; package-get-init-package are seen, below.
1136               (clear-message)
1137               (if (package-get-init-package (package-admin-get-lispdir
1138                                              install-dir package))
1139                   (progn
1140                     (run-hook-with-args 'package-install-hook package install-dir)
1141                     (message "Added package `%s'" package)
1142                     (sit-for 0))
1143                 (progn
1144                   ;; display message only if there isn't already one.
1145                   (if (not (current-message))
1146                       (progn
1147                         (message "Added package `%s' (errors occurred)"
1148                                  package)
1149                         (sit-for 0)))
1150                   (if package-status
1151                       (setq package-status 'errors)))))
1152           (message "Installation of package %s failed." base-filename)
1153           (sit-for 0)
1154           (switch-to-buffer package-admin-temp-buffer)
1155           (delete-file full-package-filename)
1156           (setq package-status nil)))
1157       (setq found t))
1158     (if (and found package-get-remove-copy)
1159         (delete-file full-package-filename))
1160     package-status)))
1161
1162 (defun package-get-info-find-package (which name)
1163   "Look in WHICH for the package called NAME and return all the info
1164 associated with it.  See `package-get-base' for info on the format
1165 returned.
1166
1167  To access fields returned from this, use
1168 `package-get-info-version' to return information about particular a
1169 version.  Use `package-get-info-find-prop' to find particular property
1170 from a version returned by `package-get-info-version'."
1171   (interactive "xPackage list: \nsPackage Name: ")
1172   (if which
1173       (if (eq (caar which) name)
1174           (cdar which)
1175         (if (cdr which)
1176             (package-get-info-find-package (cdr which) name)))))
1177
1178 (defun package-get-info-version (package version)
1179   "In PACKAGE, return the plist associated with a particular VERSION of the
1180   package.  PACKAGE is typically as returned by
1181   `package-get-info-find-package'.  If VERSION is nil, then return the
1182   first (aka most recent) version.  Use `package-get-info-find-prop'
1183   to retrieve a particular property from the value returned by this."
1184   (interactive (package-get-interactive-package-query t t))
1185   (while (and version package (not (string= (plist-get (car package) 'version) version)))
1186     (setq package (cdr package)))
1187   (if package (car package)))
1188
1189 (defun package-get-info-prop (package-version property)
1190   "In PACKAGE-VERSION, return the value associated with PROPERTY.
1191 PACKAGE-VERSION is typically returned by `package-get-info-version'
1192 and PROPERTY is typically (although not limited to) one of the
1193 following:
1194
1195 version         - version of this package
1196 provides                - list of symbols provided
1197 requires                - list of symbols that are required.
1198                   These in turn are provided by other packages.
1199 size            - size of the bundled package
1200 md5sum          - computed md5 checksum"
1201   (interactive "xPackage Version: \nSProperty")
1202   (plist-get package-version property))
1203
1204 (defun package-get-info-version-prop (package-list package version property)
1205   "In PACKAGE-LIST, search for PACKAGE with this VERSION and return
1206   PROPERTY value."
1207   (package-get-info-prop
1208    (package-get-info-version
1209     (package-get-info-find-package package-list package) version) property))
1210
1211 (defun package-get-staging-dir (filename)
1212   "Return a good place to stash FILENAME when it is retrieved.
1213 Use `package-get-dir' for directory to store stuff.
1214 Creates `package-get-dir'  if it doesn't exist."
1215   (interactive "FPackage filename: ")
1216   (if (not (file-exists-p package-get-dir))
1217       (make-directory package-get-dir))
1218   (expand-file-name
1219    (file-name-nondirectory (or (and (fboundp 'efs-ftp-path)
1220                                     (nth 2 (efs-ftp-path filename)))
1221                                filename))
1222    (file-name-as-directory package-get-dir)))
1223
1224 (defun package-get-remote-filename (search filename)
1225   "Return FILENAME as a remote filename.
1226 It first checks if FILENAME already is a remote filename.  If it is
1227 not, then it uses the (car search) as the remote site-name and the (cadr
1228 search) as the remote-directory and concatenates filename.  In other
1229 words
1230         site-name:remote-directory/filename.
1231
1232 If (car search) is nil, (cadr search is interpreted as  a local directory).
1233 "
1234   (if (file-remote-p filename)
1235       filename
1236     (let ((dir (cadr search)))
1237       (concat (when (car search)
1238                 (concat
1239                  (if (string-match "@" (car search))
1240                      "/"
1241                    "/anonymous@")
1242                  (car search) ":"))
1243               (if (string-match "/$" dir)
1244                   dir
1245                 (concat dir "/"))
1246               filename))))
1247
1248 (defun package-get-installedp (package version)
1249   "Determine if PACKAGE with VERSION has already been installed.
1250 I'm not sure if I want to do this by searching directories or checking
1251 some built in variables.  For now, use packages-package-list."
1252   ;; Use packages-package-list which contains name and version
1253   (equal (plist-get
1254           (package-get-info-find-package packages-package-list
1255                                          package) ':version)
1256          (if (floatp version)
1257              version
1258            (string-to-number version))))
1259
1260 ;;;###autoload
1261 (defun package-get-package-provider (sym &optional force-current)
1262   "Search for a package that provides SYM and return the name and
1263   version.  Searches in `package-get-base' for SYM.   If SYM is a
1264   consp, then it must match a corresponding (provide (SYM VERSION)) from
1265   the package.
1266
1267 If FORCE-CURRENT is non-nil make sure the database is up to date. This might
1268 lead to Emacs accessing remote sites."
1269   (interactive "SSymbol: ")
1270   (package-get-require-base force-current)
1271   (let ((packages package-get-base)
1272         (done nil)
1273         (found nil))
1274     (while (and (not done) packages)
1275       (let* ((this-name (caar packages))
1276              (this-package (cdr (car packages)))) ;strip off package name
1277         (while (and (not done) this-package)
1278           (if (or (eq this-name sym)
1279                   (eq (cons this-name
1280                           (package-get-info-prop (car this-package) 'version))
1281                       sym)
1282                   (member sym
1283                         (package-get-info-prop (car this-package) 'provides)))
1284               (progn (setq done t)
1285                      (setq found
1286                        (list (caar packages)
1287                          (package-get-info-prop (car this-package) 'version))))
1288             (setq this-package (cdr this-package)))))
1289       (setq packages (cdr packages)))
1290     (when (interactive-p)
1291       (if found
1292           (message "%S" found)
1293         (message "No appropriate package found")))
1294     found))
1295
1296 (defun package-get-ever-installed-p (pkg &optional notused)
1297   (string-match "-package$" (symbol-name pkg))
1298   (custom-initialize-set
1299    pkg
1300    (if (package-get-info-find-package
1301         packages-package-list
1302         (intern (substring (symbol-name pkg) 0 (match-beginning 0))))
1303        t)))
1304
1305 (provide 'package-get)
1306 ;;; package-get.el ends here