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