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