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