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