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