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